-
Notifications
You must be signed in to change notification settings - Fork 6
/
gonk.go
237 lines (186 loc) · 5.04 KB
/
gonk.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package main
import (
"crypto/tls"
"flag"
"fmt"
"github.com/Gonk/go-v8"
irc "github.com/Gonk/goirc/client"
log "github.com/fluffle/golog/logging"
"github.com/howeyc/fsnotify"
"io/ioutil"
"os"
"os/signal"
"strings"
)
func printUsageAndExit() {
flag.Usage()
os.Exit(1)
}
func loadModules(conn *irc.Conn) (modules []IModule) {
// Load each module in the modules directory
scripts, err := ioutil.ReadDir("modules")
if err != nil {
log.Fatal(err.Error())
}
// Read base module script
file, err := os.Open("module.js")
if err != nil {
log.Fatal("Error reading base module script:", err)
}
defer file.Close()
baseScript, err := ioutil.ReadAll(file)
if err != nil {
log.Fatal("Error reading base module script:", err)
}
for _, fileInfo := range scripts {
if !fileInfo.IsDir() {
if !strings.HasSuffix(fileInfo.Name(), "js") {
continue
}
filename := "modules/" + fileInfo.Name()
file, err := os.Open(filename)
if err != nil {
log.Error("Error loading module:", err)
continue
}
defer file.Close()
script, err := ioutil.ReadAll(file)
if err != nil {
log.Error("Error loading module:", err)
continue
}
module := NewModule(fileInfo.Name(), conn)
// Init module with base script and its own script
ret, err := module.Init(v8.NewContext(), string(baseScript)+string(script))
if err != nil {
log.Error("Error loading module: %s\n%s", err, ret)
continue
}
// Create file watcher
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Error("Error creating file watcher for %s:", fileInfo.Name(), err)
}
watcher.Watch(filename)
go func(filename string) {
for {
select {
case ev := <-watcher.Event:
watcher.Watch(filename) // Make sure we continue to watch the file at this location
if !ev.IsModify() {
// Only reload the module on a modification event
continue
}
case err := <-watcher.Error:
log.Error("Error watching file: %s", filename, err)
continue
}
// Reload script
log.Info("Reloading %s", filename)
file, err := os.Open(filename)
if err != nil {
log.Error("Error loading module:", err)
continue
}
defer file.Close()
script, err := ioutil.ReadAll(file)
if err != nil {
log.Error("Error loading module:", err)
continue
}
ret, err := module.Init(v8.NewContext(), string(baseScript)+string(script))
if err != nil {
log.Error("Error reloading module: %s\n%s", err, ret)
}
}
}(filename)
log.Info("Loaded module %s", fileInfo.Name())
modules = append(modules, &module)
}
}
return
}
func connect(c *irc.Conn, server string, password string, exiting chan bool) {
err := c.ConnectTo(server, password)
if err != nil {
log.Error(err.Error())
log.Info("Unable to connect to server")
exiting <- true
} else {
log.Info(c.String())
}
}
func main() {
exiting := make(chan bool)
// Set up signal handlers
quitting := make(chan os.Signal, 1)
signal.Notify(quitting, os.Interrupt)
signal.Notify(quitting, os.Kill)
// Parse flags
server := flag.String("server", "", "Hostname and/or port (e.g. 'localhost:6667')")
gonkNick := flag.String("nick", "gonk", "Nickname used for the connection")
password := flag.String("password", "", "Server password")
ssl := flag.Bool("ssl", false, "Use SSL")
verifyCert := flag.Bool("verify-ssl", true, "Verify SSL certificate")
flag.Parse()
// User must specify server
if *server == "" {
printUsageAndExit()
}
// Setup IRC client
c := irc.SimpleClient(*gonkNick, *gonkNick)
if *ssl {
c.Config().SSL = true
}
if !*verifyCert {
c.Config().SSLConfig = &tls.Config{InsecureSkipVerify: true}
}
// Load modules and connect them to the IRC client
modules := loadModules(c)
c.HandleFunc(irc.CONNECTED, func(conn *irc.Conn, line *irc.Line) {
// Join all specified channels upon connecting
for i := 0; i < len(flag.Args()); i++ {
channel := fmt.Sprintf("#%s", flag.Arg(i))
log.Info("Joining %s", channel)
conn.Join(channel)
conn.Privmsg(channel, "*"+strings.ToUpper(*gonkNick)+"*")
}
})
c.HandleFunc(irc.DISCONNECTED, func(conn *irc.Conn, line *irc.Line) {
log.Info("Disconnected from server; attempting to reconnect")
go connect(c, *server, *password, exiting)
})
c.HandleFunc("privmsg", func(conn *irc.Conn, line *irc.Line) {
// Determine reply target
target := line.Args[0]
if target == conn.Me().Nick {
// Reply via PM
target = line.Nick
}
text := strings.Join(line.Args[1:], "")
go func() {
for _, module := range modules {
if target == line.Nick || strings.HasPrefix(text, conn.Me().Nick) {
// Received a PM or addressed directly in a channel
if module.Respond(target, text, line.Nick) {
break
}
} else {
if module.Hear(target, text, line.Nick) {
break
}
}
}
}()
})
go func() {
connect(c, *server, *password, exiting)
}()
go func() {
<-quitting
c.Quit("*" + strings.ToUpper(*gonkNick) + "*")
exiting <- true
}()
<-exiting
log.Info("Shutting down")
}