-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.go
More file actions
399 lines (363 loc) · 10.3 KB
/
commands.go
File metadata and controls
399 lines (363 loc) · 10.3 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
package ircb
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"sort"
"strings"
"time"
)
const handled = true
const nothandled = false
// Command is what executes using a parsed IRC message
// irc message '!echo arg1 arg2' gets parsed as:
// 'irc.Command = echo', 'irc.CommandArguments = []string{"arg1","arg2"}
// Command will be executed if it is in CommandMap or MasterMap
// Map Commands before connecting:
// ircb.CommandMap
// ircb.DefaultCommandMaps() // load defaults, optional.
// ircb.CommandMap["echo"] = CommandEcho
// // Add a new command called hello, executed with !hello
// !hello responds in channel, using name of user commander
// ircb.CommandMap["hello"] = func(c *Connection, irc *IRC){
// irc.Reply(c, fmt.Sprintf("hello, %s!", irc.ReplyTo))
// }
// // Command parser will deal with authentication.
// // This makes adding new master commands easy:
// ircb.MasterMap["stat"] = func(c *Connection, irc.*IRC){
// irc.ReplyUser(c, fmt.Sprintf("lines received: %v", c.lines))
// }
// Reply with irc.ReplyUser (for /msg reply) or irc.Reply (for channel)
type Command func(c *Connection, irc *IRC)
func nilcommand(c *Connection, irc *IRC) {
c.Log.Println("nil command ran successfully")
}
// AddMasterCommand adds a new master command, named 'name' to the MasterMap
func (c *Connection) AddMasterCommand(name string, fn Command) {
c.maplock.Lock()
defer c.maplock.Unlock()
c.MasterMap[name] = fn
return
}
// AddCommand adds a new public command, named 'name' to the CommandMap
func (c *Connection) AddCommand(name string, fn Command) {
c.maplock.Lock()
defer c.maplock.Unlock()
c.CommandMap[name] = fn
}
// RemoveMasterCommand adds a new public command, named 'name' to the CommandMap
func (c *Connection) RemoveMasterCommand(name string) {
c.maplock.Lock()
defer c.maplock.Unlock()
delete(c.MasterMap, name)
}
// RemoveCommand removes a public command, named 'name' to the CommandMap
func (c *Connection) RemoveCommand(name string) {
c.maplock.Lock()
defer c.maplock.Unlock()
delete(c.CommandMap, name)
}
// DefaultCommandMap returns default command map
func DefaultCommandMap() map[string]Command {
m := make(map[string]Command)
m["quiet"] = commandQuiet // quiet
m["up"] = commandUptime // bot uptime
m["help"] = commandHelp // list commands
m["about"] = commandAbout // about link
m["karma"] = commandKarma // karma system
m["define"] = commandDefine // define system
return m
}
// DefaultMasterMap returns default master command map
func DefaultMasterMap() map[string]Command {
m := make(map[string]Command)
m["do"] = commandMasterDo // do <raw irc>
m["upgrade"] = commandMasterUpgrade // upgrade and redeploy
m["r"] = commandMasterReboot // reboot
m["part"] = commandMasterPart // part <channel>
m["echo"] = commandEcho // echo back
m["quit"] = commandMasterQuit // cant quit
m["q"] = commandMasterQuit // cant quit
m["help"] = commandMasterHelp // list master commands
m["set"] = commandMasterSet // set (some) config options
m["plugin"] = masterCommandLoadPlugin // load a local .so
m["fetch"] = masterCommandFetchPlugin // fetch latest plugin from repo
return m
}
func commandUptime(c *Connection, irc *IRC) {
irc.Reply(c, time.Now().Sub(c.since).String())
}
func commandHostUptime(c *Connection, irc *IRC) {
// output of uptime command
uptime := exec.Command("/usr/bin/uptime")
out, err := uptime.CombinedOutput()
if err != nil {
c.Log.Println(irc, err)
c.SendMaster("%s", err)
}
output := strings.Split(string(out), "\n")[0]
if strings.TrimSpace(output) != "" {
irc.Reply(c, output)
}
}
func commandEcho(c *Connection, irc *IRC) {
irc.Reply(c, fmt.Sprint(strings.Join(irc.Arguments, " ")))
}
func commandQuiet(c *Connection, irc *IRC) {
c.quiet = !c.quiet
if !c.quiet {
c.Log.Println("no longer quiet")
irc.Reply(c, "\x01ACTION gasps for air\x01")
}
c.Log.Println("muted")
}
func commandMasterHelp(c *Connection, irc *IRC) {
if len(irc.Arguments) < 2 || irc.Arguments[0] == "" {
var list []string
for i := range c.MasterMap {
list = append(list, i)
}
sort.Strings(list)
irc.Reply(c, fmt.Sprintf("%v master commands: %s", len(list), list))
return
}
}
func commandHelp(c *Connection, irc *IRC) {
if len(irc.Arguments) < 2 || irc.Arguments[0] == "" {
var list []string
for i := range c.CommandMap {
list = append(list, i)
}
sort.Strings(list)
irc.Reply(c, fmt.Sprintf("%v commands: %s", len(list), list))
return
}
}
func commandAbout(c *Connection, irc *IRC) {
irc.Reply(c, "I'm a robot. You can learn more at https://aerth.github.io/ircb/")
}
func commandLineCount(c *Connection, irc *IRC) {}
func commandDefine(c *Connection, irc *IRC) {
if !c.config.Define {
return
}
if len(irc.Arguments) < 2 || irc.Arguments[0] == "" {
irc.Reply(c, "usage: define [word] [text]")
return
}
action := irc.Arguments[0]
if _, ok := c.CommandMap[action]; ok {
irc.Reply(c, fmt.Sprintf("already defined as command: %q", action))
return
}
definition := strings.Join(irc.Arguments[1:], " ")
c.databaseDefine(action, definition)
irc.Reply(c, fmt.Sprintf("defined: %q", action))
}
func commandMasterDo(c *Connection, irc *IRC) {
c.Log.Println("GOT DO:", irc)
c.Write([]byte(strings.Join(irc.Arguments, " ")))
}
func commandMasterReboot(c *Connection, irc *IRC) {
b := c.MarshalConfig()
err := ioutil.WriteFile("config.json", b, 0600)
if err != nil {
c.Log.Printf("error while trying to write config file for respawn: %v", err)
irc.Reply(c, "cant reboot, check logs")
return
}
irc.Reply(c, "brb")
c.Respawn()
}
func commandMasterQuit(c *Connection, irc *IRC) {
irc.Reply(c, "I am unstoppable. Did you mean... reboot ? upgrade ?")
}
func commandMasterPart(c *Connection, irc *IRC) {
part := func(ch string) []byte {
if strings.HasPrefix(ch, "#") {
return []byte("PART :" + ch)
}
return nil
}
if strings.HasPrefix(irc.To, "#") {
irc.Reply(c, ":(")
c.Write(part(irc.To))
c.SendMaster("Parted channel: %q", irc.To)
return
}
if len(irc.Arguments) == 1 {
c.Write(part(irc.Arguments[0]))
c.SendMaster("Parted channel: %q", irc.Arguments[0])
}
}
func commandMasterDebug(c *Connection, irc *IRC) {
c.Log.Println(c, irc)
}
func commandMasterSet(c *Connection, irc *IRC) {
if len(irc.Arguments) != 2 {
irc.Reply(c, `usage: set optionname on|off`)
return
}
option := irc.Arguments[0]
value := irc.Arguments[1]
switch option {
default:
irc.Reply(c, `no option like that, 'links' 'define' or 'karma'`)
return
case "links":
switch value {
case "on":
c.config.ParseLinks = true
case "off":
c.config.ParseLinks = false
default:
irc.Reply(c, `usage: set optionname on|off`)
return
}
case "define":
switch value {
case "on":
c.config.Define = true
case "off":
c.config.Define = false
default:
irc.Reply(c, `usage: set optionname on|off`)
return
}
case "karma":
switch value {
case "on":
c.config.Karma = true
case "off":
c.config.Karma = false
default:
irc.Reply(c, `usage: set optionname on|off`)
return
}
}
}
func commandMasterUpgrade(c *Connection, irc *IRC) {
checkout := exec.Command("git", "checkout", "master")
out, err := checkout.CombinedOutput()
c.Log.Println(string(out))
if err != nil {
c.Log.Println(irc, err)
c.SendMaster("Could not checkout 'master' branch: %v", err)
c.SendMaster(string(out))
return
}
update := exec.Command("git", "pull", "origin", "master")
out, err = update.CombinedOutput()
c.Log.Println(string(out))
if err != nil {
c.Log.Println(irc, err)
c.SendMaster("Could not pull 'master' branch: %v", err)
c.SendMaster(string(out))
return
}
upgrade := exec.Command("make", "all")
out, err = upgrade.CombinedOutput()
c.Log.Println(string(out))
if err != nil {
c.Log.Println(irc, err)
c.SendMaster("Could not rebuild: %v", err)
c.SendMaster(string(out))
return
}
c.SendMaster("respawning now")
c.Respawn()
}
func masterCommandLoadPlugin(c *Connection, irc *IRC) {
if len(irc.Arguments) != 1 {
irc.Reply(c, "need plugin name")
return
}
name := strings.TrimSpace(irc.Arguments[0])
err := LoadPlugin(c, name)
if err != nil {
c.SendMaster("error loading plugin: %v", err)
return
}
irc.Reply(c, "plugin loaded: "+irc.Arguments[0])
}
func masterCommandFetchPlugin(c *Connection, irc *IRC) {
os.Setenv("CGO_ENABLED", "1")
if irc.Arguments == nil || len(irc.Arguments) != 1 {
irc.Reply(c, Red+"need plugin name")
return
}
name := irc.Arguments[0]
if strings.TrimSpace(name) == "" || strings.Contains(name, "..") {
return
}
irc.Reply(c, "fetching plugin")
fetch := exec.Command("go", "get", "-v", "-d", "github.com/aerth/ircb-plugins/...")
out, err := fetch.CombinedOutput()
if err != nil {
c.Log.Printf("error while fetching plugin %q: %v", name, err)
c.SendMaster(Red+"error: %s %v", string(out), err)
return
}
irc.Reply(c, "compiling plugin")
build := exec.Command("go", "build",
"-o", name+".so", "-v", "-buildmode=plugin",
"github.com/aerth/ircb-plugins/"+name)
out, err = build.CombinedOutput()
if err != nil {
c.Log.Printf("error while fetching plugin %q: %v", name, err)
c.SendMaster(Red+"error: %s %v", string(out), err)
return
}
err = LoadPlugin(c, name)
if err != nil {
c.Log.Printf("error while loading plugin %q: %v", name, err)
c.SendMaster(Red+"error loading: %v", err)
return
}
irc.Reply(c, fmt.Sprintf(Green+"plugin loaded: %q", name))
}
func commandKarma(c *Connection, irc *IRC) {
if !c.config.Karma {
irc.ReplyUser(c, "karma is disabled")
return
}
if len(irc.Arguments) != 1 {
irc.Reply(c, c.karmaShow(irc.ReplyTo))
return
}
irc.Reply(c, c.karmaShow(irc.Arguments[0]))
}
func (c *Connection) parseKarma(input string) bool {
if !c.config.Karma {
return nothandled
}
split := strings.Split(input, " ")
if len(split) < 1 {
return nothandled
}
// input is more than one word, try "thank" method
if len(split) > 1 {
if strings.Contains(input, "thank") {
if i := strings.Index(input, ":"); i != -1 && i != 0 {
c.Log.Println("Karma:", input[0:i])
c.karmaUp(input[0:i])
return handled
}
return nothandled
}
return nothandled
}
// user+
if strings.HasSuffix(input, "+") {
c.karmaUp(strings.Replace(input, "+", "", -1))
return handled
}
// user-
if strings.HasSuffix(input, "-") {
c.karmaDown(strings.Replace(input, "-", "", -1))
return handled
}
// no karma found
return nothandled
}