This repository has been archived by the owner on Sep 18, 2019. It is now read-only.
/
pratbot.go
133 lines (115 loc) · 2.63 KB
/
pratbot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"authutil"
"bytes"
"crypto/tls"
"encoding/json"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
"bot"
"connection"
"dispatcher"
)
var (
server = flag.String("server", "", "Prat server")
apiKey = flag.String("apikey", "", "Prat API key")
secret = flag.String("secret", "", "Prat API secret")
useTls = flag.Bool("tls", true, "Connect via TLS")
port = flag.Int("port", 0, "Port (defaults to 80/443)")
botsString = flag.String("bots", "", "Comma-separated list of bots to initialize")
wsAddr string
httpAddr string
)
type newBotFunc func(*connection.Conn, *bot.UserInfo) bot.Bot
var (
botNameToFunc = map[string]newBotFunc{
"echo": bot.NewEcho,
"github": bot.NewGithub,
}
bots = make(map[string]newBotFunc)
disp = dispatcher.New()
)
func init() {
flag.Parse()
for _, f := range []string{*server, *apiKey, *secret} {
if f == "" {
flag.Usage()
os.Exit(-1)
}
}
if *port == 0 {
if *useTls {
*port = 443
} else {
*port = 80
}
}
proto := ""
if *useTls {
proto = "s"
}
wsAddr = fmt.Sprintf("ws%s://%s:%d", proto, *server, *port)
httpAddr = fmt.Sprintf("http%s://%s:%d", proto, *server, *port)
botList := strings.Split(*botsString, ",")
for _, bs := range botList {
if bs == "" {
continue
}
f, ok := botNameToFunc[bs]
if !ok {
log.Fatalln("Unrecognized bot:", bs)
}
bots[bs] = f
}
if len(bots) == 0 {
log.Fatalln("Must specify one or more bots to run.")
}
}
func main() {
// Connect
conn, err := connection.Connect(wsAddr, *apiKey, *secret)
if err != nil {
log.Fatal(err)
}
// Get info about ourself.
addr := httpAddr + authutil.SignRequest("/api/whoami", *apiKey, *secret)
// For some reason I can't verify the cert on pratchat.com :\
transport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: transport}
response, err := client.Get(addr)
if err != nil {
log.Fatal("Error fetching user info: " + err.Error())
}
var buf bytes.Buffer
io.Copy(&buf, response.Body)
userInfo := &bot.UserInfo{}
if err := json.Unmarshal(buf.Bytes(), userInfo); err != nil {
log.Fatal("Error getting user info: " + err.Error())
}
// Leave all current channels.
for _, channel := range userInfo.User.Channels {
conn.Leave(channel)
}
// Register bots
for _, f := range bots {
disp.Register(f(conn, userInfo))
}
log.Println("Bots started.")
// Send 'connected' message
connectedMsg := &bot.Event{
Type: bot.EventConnect,
}
disp.Send(connectedMsg)
// Loop, receiving messages, and send them through the dispatcher
for {
msg := <-conn.In
disp.SendRaw(msg)
}
}