-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.go
369 lines (338 loc) · 11.3 KB
/
commands.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
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
package papaBot
// Handlers for default bot commands.
import (
"fmt"
"github.com/pawelszydlo/papa-bot/events"
"github.com/sirupsen/logrus"
"math/rand"
"strings"
)
// initBotCommands registers bot commands.
func (bot *Bot) initBotCommands() {
// Help.
bot.RegisterCommand(&BotCommand{
[]string{"help", "h"},
false, false, false,
"[pub]", "Send help text to you privately. Adding [pub] will print help on the same channel you asked.",
commandHelp})
// Auth.
bot.RegisterCommand(&BotCommand{
[]string{"auth"},
true, false, false,
"<username> <password>", "Authenticate with the bot.",
commandAuth})
// Useradd.
bot.RegisterCommand(&BotCommand{
[]string{"useradd"},
true, false, false,
"<username> <password>", "Create user account.",
commandUserAdd})
// Find.
bot.RegisterCommand(&BotCommand{
[]string{"f", "find"},
false, false, false,
"<token1> <token2> <token3> ...", "Look for URLs containing all the tokens.",
commandFindUrl})
// More.
bot.RegisterCommand(&BotCommand{
[]string{"m", "more", "moar"},
false, false, false,
"", "Say more about last link.",
commandSayMore})
// Var.
bot.RegisterCommand(&BotCommand{
[]string{"var", "v"},
true, true, false,
"list / get <name> / set <name> <value>", "Controls custom variables.",
commandVar})
// Ignore.
bot.RegisterCommand(&BotCommand{
[]string{"ignore"},
false, true, true,
"add <userName> / remove <userName>", "Manages ignore list.",
commandIgnore})
// Version.
bot.RegisterCommand(&BotCommand{
[]string{"ver", "version"},
false, false, false,
"", "Prints bot's version.",
commandVer})
bot.commandsHideParams["auth"] = true
bot.commandsHideParams["useradd"] = true
}
// handleBotCommand handles commands directed at the bot.
func (bot *Bot) handleBotCommand(sourceEvent *events.EventMessage) {
// Catch errors.
defer func() {
// Run a work done event.
bot.EventDispatcher.Trigger(events.EventMessage{
sourceEvent.TransportName,
sourceEvent.TransportFormatting,
events.EventBotDone,
"", "", sourceEvent.Channel, "", sourceEvent.Context, false,
})
if Debug {
return
} // When in debug mode fail on all errors.
if r := recover(); r != nil {
bot.Log.Errorf("FATAL ERROR in bot command: %s", r)
}
}()
// Was the command sent by the owner?
owner := bot.UserIsOwner(sourceEvent.UserId)
admin := bot.UserIsAdmin(sourceEvent.UserId)
params := strings.Split(sourceEvent.Message, " ")
command := params[0]
params = params[1:]
paramsDisplay := fmt.Sprintf("%+v", params)
if bot.commandsHideParams[command] {
paramsDisplay = "<hidden>"
}
bot.Log.WithFields(
logrus.Fields{"channel": sourceEvent.Channel, "cmd": command, "params": paramsDisplay},
).Infof("Received command from %s.", sourceEvent.Nick)
if !sourceEvent.IsPrivate() && !owner && !admin { // Command limits apply.
if bot.commandUseLimit[command+sourceEvent.Nick] >= bot.Config.CommandsPer5 { // Per command+person.
if !bot.commandWarn[sourceEvent.Channel] { // Warning was not yet sent.
bot.SendMessage(sourceEvent, fmt.Sprintf("%s, %s", sourceEvent.Nick, bot.Texts.CommandLimit))
bot.commandWarn[sourceEvent.Channel] = true
}
return
} else {
bot.commandUseLimit[command+sourceEvent.Nick] += 1
}
}
if cmd, exists := bot.commands[command]; exists {
// Check if command needs to be run through private message.
if cmd.Private && !sourceEvent.IsPrivate() {
bot.SendMessage(sourceEvent, fmt.Sprintf("%s, %s", sourceEvent.Nick, bot.Texts.NeedsPriv))
return
}
// Check if command needs to be run by the owner.
if cmd.Owner && !owner || cmd.Admin && !admin {
bot.SendMessage(sourceEvent, fmt.Sprintf("%s, %s", sourceEvent.Nick, bot.Texts.NeedsAdmin))
return
}
// Run a work start event.
bot.EventDispatcher.Trigger(events.EventMessage{
sourceEvent.TransportName,
sourceEvent.TransportFormatting,
events.EventBotWorking,
"", "", sourceEvent.Channel, "", "", false,
})
// Execute the command.
cmd.CommandFunc(bot, sourceEvent, params)
} else { // Unknown command.
if sourceEvent.IsPrivate() && rand.Int()%10 > 5 { // Talk back only on private chats.
bot.SendMessage(
sourceEvent, fmt.Sprintf("%s", bot.Texts.WrongCommand[rand.Intn(len(bot.Texts.WrongCommand))]))
}
}
}
// commandHelp will print help for all the commands.
func commandHelp(bot *Bot, sourceEvent *events.EventMessage, params []string) {
forcePriv := false
if len(params) == 0 || params[0] != "pub" { // By default help only gets sent on priv.
forcePriv = true
}
owner := bot.UserIsOwner(sourceEvent.UserId)
admin := bot.UserIsAdmin(sourceEvent.UserId)
// Build a list of all command aliases.
helpCommandKeys := map[string][]string{}
helpCommands := map[string]*BotCommand{}
for key, cmd := range bot.commands {
pointerStr := fmt.Sprintf("%p", cmd)
helpCommandKeys[pointerStr] = append(helpCommandKeys[pointerStr], key)
helpCommands[pointerStr] = cmd
}
// Print help.
results := []string{}
for pointerStr, cmd := range helpCommands {
commands := strings.Join(helpCommandKeys[pointerStr], ", ")
options := ""
if cmd.Private {
if sourceEvent.TransportFormatting == events.FormatIRC {
options = " \x0300(private only)\x03"
} else if sourceEvent.TransportFormatting == events.FormatMarkdown {
options = " **(private only)**"
} else {
options = " (private only)"
}
}
if cmd.Owner && !owner {
continue
}
if cmd.Admin && !admin {
continue
}
result := ""
if sourceEvent.TransportFormatting == events.FormatIRC {
result = fmt.Sprintf(
"\x0308%s\x03 \x0310%s\x03 - %s%s", commands, cmd.HelpParams, cmd.HelpDescription, options)
} else if sourceEvent.TransportFormatting == events.FormatMarkdown {
result = fmt.Sprintf("| %s | %s | %s%s |", commands, cmd.HelpParams, cmd.HelpDescription, options)
} else {
result = fmt.Sprintf("%s %s - %s%s", commands, cmd.HelpParams, cmd.HelpDescription, options)
}
results = append(results, result)
}
// Send the help messages.
if sourceEvent.TransportFormatting == events.FormatMarkdown {
result := "\n\n| Command | Parameters | Help |\n| :-- | :-- | :-- |\n"
result += strings.Join(results, "\n")
if forcePriv {
bot.SendPrivateMessage(sourceEvent, sourceEvent.Nick, result)
} else {
bot.SendMessage(sourceEvent, result)
}
} else {
for _, result := range results {
if forcePriv {
bot.SendPrivateMessage(sourceEvent, sourceEvent.Nick, result)
} else {
bot.SendMessage(sourceEvent, result)
}
}
}
return
}
// commandAuth is a command for authenticating an user with the bot.
func commandAuth(bot *Bot, sourceEvent *events.EventMessage, params []string) {
if len(params) == 2 {
if err := bot.authenticateUser(params[0], sourceEvent.UserId, params[1]); err != nil {
bot.Log.Warningf("Couldn't authenticate %s: %s", params[0], err)
bot.SendMessage(sourceEvent, "That's not right.")
return
}
bot.SendMessage(sourceEvent, "You are now logged in.")
} else {
bot.SendMessage(sourceEvent, bot.Texts.SeeHelp)
}
}
// commandIgnore will control the ignore list.
func commandIgnore(bot *Bot, sourceEvent *events.EventMessage, params []string) {
if len(params) == 2 {
command := params[0]
userId := params[1]
if command == "add" {
if bot.UserIsOwner(userId) {
bot.SendMessage(sourceEvent, "You cannot ignore the owner.")
return
}
bot.AddToIgnoreList(userId)
} else if command == "remove" {
bot.RemoveFromIgnoreList(userId)
}
bot.SendMessage(sourceEvent, "Ignore list changed.")
} else {
bot.SendMessage(sourceEvent, bot.Texts.SeeHelp)
}
}
// commandUserAdd will add a new user to bot's database and authenticate.
func commandUserAdd(bot *Bot, sourceEvent *events.EventMessage, params []string) {
if len(params) == 2 {
if bot.UserIsAuthenticated(sourceEvent.UserId) {
bot.SendMessage(sourceEvent, "You are already authenticated.")
return
}
if err := bot.addUser(params[0], params[1], false, false); err != nil {
bot.Log.Warningf("Couldn't add user %s: %s", params[0], err)
bot.SendMessage(sourceEvent, fmt.Sprintf("Can't add user: %s", err))
return
}
if err := bot.authenticateUser(params[0], sourceEvent.UserId, params[1]); err != nil {
bot.Log.Warningf("Couldn't authenticate %s: %s", params[0], err)
return
}
bot.SendMessage(sourceEvent, "User added. You are now logged in.")
return
}
bot.SendMessage(sourceEvent, bot.Texts.SeeHelp)
}
// commandVar gets, sets and lists custom variables.
func commandVar(bot *Bot, sourceEvent *events.EventMessage, params []string) {
if len(params) < 1 {
return
}
command := params[0]
if command == "list" {
bot.SendMessage(sourceEvent, "Custom variables:")
for key, val := range bot.customVars {
bot.SendMessage(sourceEvent, fmt.Sprintf("%s = %s", key, val))
}
return
}
if len(params) == 2 && command == "get" {
name := params[1]
bot.SendMessage(sourceEvent, fmt.Sprintf("%s = %s", name, bot.GetVar(name)))
return
}
if len(params) >= 3 && command == "set" {
name := params[1]
value := strings.Join(params[2:], " ")
bot.SetVar(name, value)
bot.SendMessage(sourceEvent, fmt.Sprintf("%s = %s", name, bot.GetVar(name)))
return
}
bot.SendMessage(sourceEvent, bot.Texts.SeeHelp)
}
// commandSayMore gives more info, if bot has any.
func commandSayMore(bot *Bot, sourceEvent *events.EventMessage, params []string) {
if bot.urlMoreInfo[sourceEvent.TransportName+sourceEvent.Channel] == "" {
bot.SendMessage(sourceEvent, fmt.Sprintf("%s, %s", sourceEvent.Nick, bot.Texts.NothingToAdd))
return
} else {
bot.SendMessage(sourceEvent, bot.urlMoreInfo[sourceEvent.TransportName+sourceEvent.Channel])
delete(bot.urlMoreInfo, sourceEvent.TransportName+sourceEvent.Channel)
}
}
// commandFindUrl searches bot's database using FTS for links matching the query.
func commandFindUrl(bot *Bot, sourceEvent *events.EventMessage, params []string) {
if len(params) == 0 {
bot.SendMessage(sourceEvent, bot.Texts.SeeHelp)
return
}
token := strings.Join(params, " AND ")
query1 := "SELECT nick, timestamp, link, title FROM urls_search WHERE "
query2 := ""
if !sourceEvent.IsPrivate() {
query2 = fmt.Sprintf("channel=\"%s\" AND ", sourceEvent.Channel)
}
query3 := "search MATCH ? GROUP BY link ORDER BY timestamp DESC LIMIT 5"
// Query FTS table.
result, err := bot.Db.Query(query1+query2+query3, token)
if err != nil {
bot.Log.Warningf("Can't search for URLs: %s", err)
return
}
defer result.Close()
// Announce results.
found := []string{}
for result.Next() {
var nick, timestr, link, title string
if err = result.Scan(&nick, ×tr, &link, &title); err != nil {
bot.Log.Warningf("Error getting search results: %s", err)
} else {
if sourceEvent.IsPrivate() { // skip the author and time when not on a channel.
found = append(found, fmt.Sprintf("%s (%s)", link, title))
} else {
found = append(found, fmt.Sprintf("%s | %s | %s (%s)", nick, timestr, link, title))
}
}
}
if len(found) > 0 {
if sourceEvent.IsPrivate() {
bot.SendMessage(sourceEvent, bot.Texts.SearchPrivateNotice)
}
bot.SendMessage(sourceEvent, fmt.Sprintf("%s, %s", sourceEvent.Nick, bot.Texts.SearchResults))
for i := range found {
bot.SendMessage(sourceEvent, found[i])
}
} else {
bot.SendMessage(sourceEvent, fmt.Sprintf("%s", bot.Texts.SearchNoResults))
}
}
// commandVer will print the bot's version.
func commandVer(bot *Bot, sourceEvent *events.EventMessage, params []string) {
bot.SendMessage(sourceEvent, bot.version())
}