-
Notifications
You must be signed in to change notification settings - Fork 0
/
factoid.go
456 lines (408 loc) · 13 KB
/
factoid.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
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
package main
import (
"database/sql"
"fmt"
"math/rand"
"regexp"
"strings"
"time"
"github.com/osm/irc"
)
// factoidRandom initializes the random source.
var factoidRandom = rand.New(rand.NewSource(time.Now().UnixNano()))
// factoidGrammarGiphySearchRegexp contains the regexp that finds
// <giphy search="xxx"> tags.
var factoidGrammarGiphySearchRegexp *regexp.Regexp
// factoidGrammarTenorSearchRegexp contains the regexp that finds
// <tenor search="xxx"> tags.
var factoidGrammarTenorSearchRegexp *regexp.Regexp
// factoidGrammarRandomWord contains the regexp that finds
// <randomword words="xxx"> tags.
var factoidGrammarRandomWord *regexp.Regexp
// initFactoidDefaults sets default values for all settings.
func (b *bot) initFactoidDefaults() {
// Commands
if b.IRC.FactoidCmd == "" {
b.IRC.FactoidCmd = "!factoid"
}
if b.IRC.FactoidSubCmdAdd == "" {
b.IRC.FactoidSubCmdAdd = "add"
}
if b.IRC.FactoidSubCmdAddDelimiter == "" {
b.IRC.FactoidSubCmdAddDelimiter = " _is_ "
}
if b.IRC.FactoidSubCmdDelete == "" {
b.IRC.FactoidSubCmdDelete = "forget"
}
if b.IRC.FactoidSubCmdSnoop == "" {
b.IRC.FactoidSubCmdSnoop = "snoop"
}
if b.IRC.FactoidSubCmdSnoopAuthor == "" {
b.IRC.FactoidSubCmdSnoopAuthor = "snoop-author"
}
if b.IRC.FactoidSubCmdSnoopReply == "" {
b.IRC.FactoidSubCmdSnoopReply = "snoop-reply"
}
if b.IRC.FactoidSubCmdCount == "" {
b.IRC.FactoidSubCmdCount = "count"
}
// Messages
if b.IRC.FactoidMsgAdd == "" {
b.IRC.FactoidMsgAdd = "noted"
}
if b.IRC.FactoidMsgDelete == "" {
b.IRC.FactoidMsgDelete = "*removed*"
}
if b.IRC.FactoidMsgSnoop == "" {
b.IRC.FactoidMsgSnoop = "<id>: <author> taught me that <trigger> is <reply> <timestamp>"
}
if b.IRC.FactoidMsgCount == "" {
b.IRC.FactoidMsgCount = "<trigger> has <count> occurrences"
}
if b.IRC.FactoidMsgIs == "" {
b.IRC.FactoidMsgIs = "is"
}
// Grammar
if b.IRC.FactoidGrammarAction == "" {
b.IRC.FactoidGrammarAction = "<action>"
}
if b.IRC.FactoidGrammarRandomWho == "" {
b.IRC.FactoidGrammarRandomWho = "<randomwho>"
}
if b.IRC.FactoidGrammarRandomWord == "" {
b.IRC.FactoidGrammarRandomWord = "<randomword words=\"([a-zåäöüA-ZÅÄÖÜ0-9 ]+)\"[^>]*>"
}
factoidGrammarRandomWord = regexp.MustCompile(b.IRC.FactoidGrammarRandomWord)
if b.IRC.FactoidGrammarReply == "" {
b.IRC.FactoidGrammarReply = "<reply>"
}
if b.IRC.FactoidGrammarWho == "" {
b.IRC.FactoidGrammarWho = "<who>"
}
if b.IRC.FactoidGrammarGiphy == "" {
b.IRC.FactoidGrammarGiphy = "<giphy>"
}
if b.IRC.FactoidGrammarGiphySearch == "" {
b.IRC.FactoidGrammarGiphySearch = `<giphy search="([a-zåäöA-ZÅÄÖ0-9 ]+)"[^>]*>"`
}
factoidGrammarGiphySearchRegexp = regexp.MustCompile(b.IRC.FactoidGrammarGiphySearch)
if b.IRC.FactoidGrammarTenorSearch == "" {
b.IRC.FactoidGrammarTenorSearch = `<giphy search="([a-zåäöüA-ZÅÄÖÜ0-9 ]+)"[^>]*>"`
}
factoidGrammarTenorSearchRegexp = regexp.MustCompile(b.IRC.FactoidGrammarTenorSearch)
}
// factoidHandler is the main entry point for all factoid related commands.
func (b *bot) factoidHandler(m *irc.Message) {
// Parse the action
a := b.parseAction(m).(*privmsgAction)
if !a.validChannel {
return
}
// Determine which action to take.
subCmd := ""
if len(a.args) > 0 {
subCmd = a.args[0]
}
// First we'll prioritize commands, if the message isn't a command
// we'll check if it's a factoid and if we should send a reply to the
// channel.
if a.cmd == b.IRC.FactoidCmd && subCmd == b.IRC.FactoidSubCmdAdd && len(a.args) >= 4 {
if b.shouldIgnore(m) {
return
}
// Remove the factoid cmd and sub cmd from the message.
msg := strings.Replace(
a.msg,
fmt.Sprintf("%s %s ", b.IRC.FactoidCmd, b.IRC.FactoidSubCmdAdd),
"",
1,
)
// Make sure that the delimiter word is present
dpos := strings.Index(msg, b.IRC.FactoidSubCmdAddDelimiter)
if dpos == -1 {
return
}
// Insert the factoid.
b.factoidHandleInsertFact(
a.nick,
msg[0:dpos],
msg[dpos+len(b.IRC.FactoidSubCmdAddDelimiter):],
)
} else if a.cmd == b.IRC.FactoidCmd && subCmd == b.IRC.FactoidSubCmdDelete && len(a.args) == 2 {
if b.shouldIgnore(m) {
return
}
b.factoidHandleDelete(a.args[1])
} else if a.cmd == b.IRC.FactoidCmd && (subCmd == b.IRC.FactoidSubCmdSnoop ||
subCmd == b.IRC.FactoidSubCmdSnoopAuthor ||
subCmd == b.IRC.FactoidSubCmdSnoopReply) && len(a.args) >= 2 {
if b.shouldIgnore(m) {
return
}
if subCmd == b.IRC.FactoidSubCmdSnoop {
b.factoidHandleSnoop(strings.Replace(
a.msg,
fmt.Sprintf("%s %s ", b.IRC.FactoidCmd, b.IRC.FactoidSubCmdSnoop),
"",
1,
), "default")
} else if subCmd == b.IRC.FactoidSubCmdSnoopAuthor {
b.factoidHandleSnoop(strings.Replace(
a.msg,
fmt.Sprintf("%s %s ", b.IRC.FactoidCmd, b.IRC.FactoidSubCmdSnoopAuthor),
"",
1,
), "author")
} else {
b.factoidHandleSnoop(strings.Replace(
a.msg,
fmt.Sprintf("%s %s ", b.IRC.FactoidCmd, b.IRC.FactoidSubCmdSnoopReply),
"",
1,
), "reply")
}
} else if a.cmd == b.IRC.FactoidCmd && subCmd == b.IRC.FactoidSubCmdCount && len(a.args) >= 2 {
if b.shouldIgnore(m) {
return
}
b.factoidHandleCount(
strings.Replace(
a.msg,
fmt.Sprintf("%s %s ", b.IRC.FactoidCmd, b.IRC.FactoidSubCmdCount),
"",
1,
),
)
} else {
b.factoidHandleFact(a)
}
}
// factoidHandleDelete deletes the given factoid if the id exists. If the id
// doesn't exist it will silently ignore the message.
func (b *bot) factoidHandleDelete(id string) {
// We expect a valid UUID to be sent.
if !isUUID(id) {
return
}
// Prepare the UPDATE statement. We are not actually deleting the
// factoid, we'll just hide it so that it can be restored if we have
// someone deleting things we want to keep.
stmt, err := b.prepare("UPDATE factoid SET is_deleted = true WHERE id = $1")
if err != nil {
b.logger.Printf("factoidHandleDelete: %v", err)
b.privmsg(b.DB.Err)
return
}
defer stmt.Close()
// Execute the UPDATE statement.
_, err = stmt.Exec(id)
if err != nil {
b.logger.Printf("factoidHandleDelete: %v", err)
b.privmsg(b.DB.Err)
return
}
// Send a notice that the factoid was removed.
b.privmsg(b.IRC.FactoidMsgDelete)
}
// factoidHandleSnoop finds information about the given factoid. If there are
// more than five factoids found for the given trigger it'll send the message
// as a private message instead so we don't flood the channel.
func (b *bot) factoidHandleSnoop(ss, t string) {
var query string
if t == "default" {
query = "SELECT id, author, timestamp, reply, trigger FROM factoid WHERE trigger ILIKE $1 AND is_deleted = false"
} else if t == "author" {
query = "SELECT id, author, timestamp, reply, trigger FROM factoid WHERE author ILIKE $1 AND is_deleted = false"
} else {
query = "SELECT id, author, timestamp, reply, trigger FROM factoid WHERE reply ILIKE $1 AND is_deleted = false"
}
// Get all the relevant factoid information
rows, err := b.query(query, ss)
if err != nil {
b.logger.Printf("factoidHandleSnoop: %v", err)
b.privmsg(b.DB.Err)
return
}
defer rows.Close()
// We'll setup a struct so that we more easily can iterate over all
// the factoids.
type fact struct {
id string
author string
timestamp string
reply string
trigger string
}
// Fetch the facts.
var facts []fact
for rows.Next() {
var i, a, t, r, tr string
rows.Scan(&i, &a, &t, &r, &tr)
facts = append(facts, fact{i, a, t, r, tr})
}
// Determine the target of the information.
var target string
if len(facts) > 5 {
target = "pastebin"
} else {
target = b.IRC.Channel
}
// Send the information back to the given target.
var pastebinCode string
for _, f := range facts {
data := map[string]string{
"<id>": f.id,
"<author>": f.author,
"<trigger>": f.trigger,
"<reply>": f.reply,
"<timestamp>": f.timestamp,
}
if target == "pastebin" {
code := b.IRC.FactoidMsgSnoop
for k, v := range data {
code = strings.ReplaceAll(code, k, v)
}
if pastebinCode == "" {
pastebinCode = fmt.Sprintf("%s", code)
} else {
pastebinCode = fmt.Sprintf("%s\n%s", pastebinCode, code)
}
} else {
b.privmsgph(b.IRC.FactoidMsgSnoop, data)
}
}
if target == "pastebin" {
b.newPaste(ss, pastebinCode)
}
}
// factoidHandleCount returns the number of occurrences the given trigger has.
func (b *bot) factoidHandleCount(trigger string) {
// Get the count for the given trigger.
var count int
err := b.queryRow("SELECT COUNT(*) FROM factoid WHERE trigger = $1 AND is_deleted = false", trigger).Scan(&count)
if err != nil && err != sql.ErrNoRows {
b.logger.Printf("factoidHandleCount: %v", err)
b.privmsg(b.DB.Err)
return
}
// Return it to the channel.
b.privmsgph(b.IRC.FactoidMsgCount, map[string]string{
"<trigger>": trigger,
"<count>": fmt.Sprintf("%d", count),
})
}
// factoidHandleInsertFact inserts a new factoid into the database.
func (b *bot) factoidHandleInsertFact(author, trigger, reply string) {
// Prepare the INSERT statement.
stmt, err := b.prepare("INSERT INTO factoid (id, timestamp, author, trigger, reply, is_deleted) VALUES($1, $2, $3, $4, $5, false)")
if err != nil {
b.logger.Printf("factoidHandleInsertFact: %v", err)
b.privmsg(b.DB.Err)
return
}
defer stmt.Close()
// Execute it.
_, err = stmt.Exec(newUUID(), newTimestamp(), author, trigger, reply)
if err != nil {
b.logger.Printf("factoidHandleInsertFact: %v", err)
b.privmsg(b.DB.Err)
return
}
// ... and send a notice that the fact has been stored.
b.privmsg(b.IRC.FactoidMsgAdd)
}
// factoidHandleFact checks whether the message in the action is a known
// factoid. If it is, we'll parse the factoid and send the results back to the
// channel.
func (b *bot) factoidHandleFact(a *privmsgAction) {
// Let's check whether the message is a known trigger.
rows, err := b.query("SELECT reply, rate FROM factoid WHERE trigger = $1 AND is_deleted = false", a.msg)
if err != nil {
b.logger.Printf("factoidHandleFact: %v", err)
b.privmsg(b.DB.Err)
return
}
defer rows.Close()
// There can be more than one factoid for a trigger. So let's store
// all of them.
type entry struct {
fact string
rate *int
}
var factoids []entry
for rows.Next() {
var f string
var r *int
rows.Scan(&f, &r)
factoids = append(factoids, entry{f, r})
}
// No factoids, return early.
if len(factoids) < 1 {
return
}
// Get a random fact and rate from the slice of factoids.
idx := rand.Intn(len(factoids))
factoid := factoids[idx].fact
rate := factoids[idx].rate
// If factoid rate is set, we'll only reply with the found factoid if
// the random number is greater than the defined value on the fact, if
// there are no rate on the set on the fact we'll fallback to the rate
// defined in the configuration file.
if rate != nil && *rate >= factoidRandom.Intn(100) {
return
} else if rate == nil && b.IRC.FactoidRate > 0 && b.IRC.FactoidRate >= factoidRandom.Intn(100) {
return
}
// Replace all occurences of <who> with the senders nick.
i := strings.Index(factoid, b.IRC.FactoidGrammarWho)
for i != -1 {
factoid = factoid[0:i] + a.nick + factoid[i+len(b.IRC.FactoidGrammarWho):]
i = strings.Index(factoid, b.IRC.FactoidGrammarWho)
}
// Replace all occurences of <randomwho> with a random nick from the
// names map.
i = strings.Index(factoid, b.IRC.FactoidGrammarRandomWho)
for i != -1 {
factoid = factoid[0:i] + b.rndName() + factoid[i+len(b.IRC.FactoidGrammarRandomWho):]
i = strings.Index(factoid, b.IRC.FactoidGrammarRandomWho)
}
// Replace all occurences of <giphy> with a gif from giphy.
i = strings.Index(factoid, b.IRC.FactoidGrammarGiphy)
for i != -1 {
if url, _ := b.giphyRandom(); url != "" {
factoid = factoid[0:i] + url + factoid[i+len(b.IRC.FactoidGrammarGiphy):]
}
i = strings.Index(factoid, b.IRC.FactoidGrammarGiphy)
}
// Replace all <giphy search="<query>"> with replies from the giphy API.
for _, matches := range factoidGrammarGiphySearchRegexp.FindAllStringSubmatch(factoid, -1) {
if url, _ := b.giphySearch(matches[1]); url != "" {
factoid = strings.Replace(factoid, matches[0], url, 1)
} else {
factoid = strings.Replace(factoid, matches[0], "", 1)
}
}
// Replace all <tenor search="<query>"> with replies from the Tenor API.
for _, matches := range factoidGrammarTenorSearchRegexp.FindAllStringSubmatch(factoid, -1) {
if url, _ := b.tenorSearch(matches[1]); url != "" {
factoid = strings.Replace(factoid, matches[0], url, 1)
} else {
factoid = strings.Replace(factoid, matches[0], "", 1)
}
}
// Replace all <randomwords words="foo bar baz"> occurences with one of
// the words in the words attribute.
for _, matches := range factoidGrammarRandomWord.FindAllStringSubmatch(factoid, -1) {
strs := strings.Split(matches[1], " ")
str := strs[rand.Intn(len(strs))]
factoid = strings.Replace(factoid, matches[0], str, 1)
}
// Handle replies.
if strings.HasPrefix(factoid, b.IRC.FactoidGrammarReply) {
b.privmsg(factoid[len(b.IRC.FactoidGrammarReply)+1:])
} else if strings.HasPrefix(factoid, b.IRC.FactoidGrammarAction) {
b.action(factoid[len(b.IRC.FactoidGrammarAction)+1:])
} else {
b.privmsg(fmt.Sprintf("%s %s %s", a.msg, b.IRC.FactoidMsgIs, factoid))
}
}