-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.go
448 lines (367 loc) · 15.8 KB
/
bot.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
package main
import (
"database/sql"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"regexp"
"strings"
"sync"
"time"
"github.com/osm/irc"
"github.com/osm/jsonc"
)
// bot is the main structure that binds all bot methods together. It does also
// contain the configuration and objects that needs to be accessible during
// the bots lifetime.
type bot struct {
// mainWG keeps track of the goroutines needed to keep the bot alive.
// Currently this is just the IRC main loop, but this wait group
// should keep track of all long living goroutines that the bot needs
// to function correctly.
mainWG sync.WaitGroup
// Logger for all bot related errors.
logger *log.Logger
// configFile keeps a copy of the configuration file path.
configFile string
// The cron and cron parser objects are kept here, we might want to
// extend the cron implementation to run more than just IRC related
// things in the future.
cron *cron
// Timezone will contain the timezone which the bot is operating in.
Timezone string
timezone *time.Location
DB struct {
client *sql.DB
// Engine contains the engine type of the database, valid
// values are empty, sqlite or postgres. Empty defaults to
// sqlite to keep backwards compatability.
Engine string `json:"engine"`
// Path should point to the location of the SQLite database
// file.
Path string `json:"path"`
// A generic error to display in case of database problems.
Err string `json:"err"`
}
HTTP struct {
// Logger for all HTTP related things.
logger *log.Logger
// Toggle the HTTP server on/off.
EnableHTTP bool `json:"enableHTTP"`
// Listen address and port.
Address string `json:"address"`
Port string `json:"port"`
// EchoRoute defines the echo route, which can be used to post
// message to the IRC channel.
EnableEcho bool `json:"enableEcho"`
EchoRoute string `json:"echoRoute"`
EchoMethod string `json:"echoMethod"`
}
IRC struct {
client *irc.Client
PostConnectMessages []struct {
Target string `json:"target"`
Message string `json:"message"`
} `json:"postConnectMessages"`
PostConnectModes []string `json:"postConnectModes"`
// This is set to time.Now() each time a message is sent. We
// will check that time.Now() - lastSentMessage is greater
// than the value defined in "GracePeriod".
lastSentMessage time.Time
// lastSentMessageMu holds the mutex for the last sent
// message.
lastSentMessageMu sync.Mutex
// Grace period is how long we should wait before we are
// allowed to send a new message to the server. The value is
// defined in milliseconds.
GracePeriod int `json:"gracePeriod"`
// This will be the time.Duration equivalent of the integer
// GracePeriod. We'll convert this right away when the
// application is started.
gracePeriod time.Duration
// Rejoin the channel if the bot is kicked.
RejoinOnKick bool `json:"rejoinOnKick"`
// Connection related settings.
Address string `json:"address"`
Channel string `json:"channel"`
Nick string `json:"nick"`
RealName string `json:"realName"`
User string `json:"user"`
Version string `json:"version"`
// names contains all the nicks in the channel. The map is
// updated on JOIN and PART messages.
names map[string]bool
// namesMu holds the mutex for the names map.
namesMu sync.Mutex
// Update notifier.
EnableUpdateNotifier bool `json:"enableUpdateNotifier"`
UpdateNotifierMsg string `json:"updateNotifierMsg"`
UpdateNotifierNames []string `json:"updateNotifierNames"`
// Supernytt - news in Swedish from aftonbladet.
EnableSupernytt bool `json:"enableSupernytt"`
SupernyttGrammarMessage string `json:"supernyttGrammarMessage"`
// Quiz.
EnableQuiz bool `json:"enableQuiz"`
QuizSources map[string]string `json:"quizSources"`
quizSourcesCache map[string][]QuizQuestion
QuizCmd string `json:"quizCmd"`
QuizSubCmdStart string `json:"quizSubCmdStart"`
QuizSubCmdStop string `json:"quizSubCmdStop"`
QuizSubCmdStats string `json:"quizSubCmdStats"`
QuizHintInterval time.Duration `json:"quizHintInterval"`
QuizMsgNameDoesNotExist string `json:"quizMsgNameDoesNotExist"`
QuizMsgLoadError string `json:"quizMsgLoadError"`
QuizMsgAlreadyStarted string `json:"quizMsgAlreadyStarted"`
QuizMsgQuestion string `json:"quizMsgQuestion"`
QuizMsgHint string `json:"quizMsgHint"`
QuizMsgAnswer string `json:"quizMsgAnswer"`
QuizMsgCorrect string `json:"quizMsgCorrect"`
QuizMsgQuizEnd string `json:"quizMsgQuizEnd"`
quizRound *quizRound
EnableLyssnar bool `json:"enableLyssnar"`
LyssnarCmd string `json:"lyssnarcmd"`
Lyssnare map[string]string `json:"lyssnare"`
LyssnarErr string `json:"lyssnarErr"`
LyssnarErrUserNotConfigured string `json:"lyssnarErrUserNotConfigured"`
LyssnarMsg string `json:"lyssnarMsg"`
LyssnarMsgUserIsNotListening string `json:"lyssnarMsgUserIsNotListening"`
EnableGiphy bool `json:"enableGiphy"`
GiphyCmd string `json:"giphyCmd"`
GiphyLang string `json:"giphyLang"`
GiphyAPIKey string `json:"giphyAPIKey"`
GiphyMsgNothingFound string `json:"giphyMsgNothingFound"`
EnableTenor bool `json:"enableTenor"`
TenorCmd string `json:"tenorCmd"`
TenorLang string `json:"tenorLang"`
TenorAPIKey string `json:"tenorAPIKey"`
TenorMsgNothingFound string `json:"tenorMsgNothingFound"`
ChattistikCmd string `json:"chattistikCmd"`
ChattistikCmdToday string `json:"chattistikCmdToday"`
ChattistikCmdYesterday string `json:"chattistikCmdYesterday"`
ChattistikMsgNoStats string `json:"chattistikMsgNoStats"`
EnableChattistik bool `json:"enableChattistik"`
EnableLogging bool `json:"enableLogging"`
SeenCmd string `json:"seenCmd"`
SeenMsgFound string `json:"seenMsgFound"`
SeenMsgNotFound string `json:"seenMsgNotFound"`
EnableCron bool `json:"enableCron"`
CronCmd string `json:"cronCmd"`
CronSubCmdAdd string `json:"cronSubCmdAdd"`
CronSubCmdAddLimit string `json:"cronSubCmdAddLimit"`
CronSubCmdDelete string `json:"cronSubCmdDelete"`
CronSubCmdList string `json:"cronSubCmdList"`
CronSubCmdUpdate string `json:"cronSubCmdUpdate"`
CronErr string `json:"cronErr"`
CronMsgAdd string `json:"cronMsgAdd"`
CronMsgDelete string `json:"cronMsgDelete"`
CronMsgList string `json:"cronMsgList"`
CronMsgUpdate string `json:"cronMsgUpdate"`
CronGrammarMsgExecCount string `json:"cronGrammarMsgExecCount"`
CronGrammarMsgExecLimit string `json:"cronGrammarMsgExecLimit"`
CronGrammarMsgIsLimited string `json:"cronGrammarMsgIsLimited"`
CronGrammarMsgRandomWho string `json:"cronGrammarMsgRandomWho"`
CronGrammarWeek string `json:"cronGrammarWeek"`
CronGrammarGiphy string `json:"cronGrammarGiphy"`
CronGrammarGiphySearch string `json:"cronGrammarGiphySearch"`
CronGrammarTenorSearch string `json:"cronGrammarTenorSearch"`
CommandErrExec string `json:"commandErrExec"`
Commands map[string]string `json:"commands"`
CommandsStatic map[string]string `json:"commandsStatic"`
EnableCommands bool `json:"enableCommands"`
commands map[string]command
EnableWeather bool `json:"enableWeather"`
WeatherAPIKey string `json:"weatherAPIKey"`
WeatherCmd string `json:"weatherCmd"`
WeatherErr string `json:"weatherErr"`
WeatherMsg string `json:"weatherMsg"`
EnableURLCheck bool `json:"enableURLCheck"`
URLCheckMsg string `json:"urlCheckMsg"`
EnableURLMeta bool `json:"enableURLMeta"`
URLMetaMsg string `json:"urlMetaMsg"`
URLMetaURLs []string `json:"urlMetaURLs"`
URLMetaIgnoreURLs []string `json:"urlMetaIgnoreURLs"`
EnableMarch bool `json:"enableMarch"`
MarchURL string `json:"marchURL"`
MarchCredentials string `json:"marchCredentials"`
MarchURLRegexps []string `json:"marchURLRegexps"`
marchURLRegexps []*regexp.Regexp
operators []*regexp.Regexp
Operators []string `json:"operators"`
ignoreDyn map[string]bool
ignoreDynMu sync.Mutex
ignorePerm []*regexp.Regexp
Ignore []string `json:"ignore"`
EnableFloodProt bool `json:"enableFloodProt"`
FloodProtTimeThreshold int `json:"floodProtTimeThreshold"`
FloodProtCmdThreshold int `json:"floodProtCmdThreshold"`
FloodProtIgnoreTime int `json:"floodProtIgnoreTime"`
FloodProtMsgIgnore string `json:"floodProtMsgIgnore"`
FloodProtMsgUnignore string `json:"floodProtMsgUnignore"`
EnableFactoid bool `json:"enableFactoid"`
FactoidRate int `json:"factoidRate"`
FactoidCmd string `json:"factoidCmd"`
FactoidSubCmdAdd string `json:"factoidSubCmdAdd"`
FactoidSubCmdAddDelimiter string `json:"factoidSubCmdAddDelimiter"`
FactoidSubCmdDelete string `json:"factoidSubCmdDelete"`
FactoidSubCmdSnoop string `json:"factoidSubCmdSnoop"`
FactoidSubCmdSnoopAuthor string `json:"factoidSubCmdSnoopAuthor"`
FactoidSubCmdSnoopReply string `json:"factoidSubCmdSnoopReply"`
FactoidSubCmdCount string `json:"factoidSubCmdCount"`
FactoidGrammarAction string `json:"factoidGrammarAction"`
FactoidGrammarRandomWho string `json:"factoidGrammarRandomWho"`
FactoidGrammarRandomWord string `json:"factoidGrammarRandomWord"`
FactoidGrammarReply string `json:"factoidGrammarReply"`
FactoidGrammarWho string `json:"factoidGrammarWho"`
FactoidGrammarGiphy string `json:"factoidGrammarGiphy"`
FactoidGrammarGiphySearch string `json:"factoidGrammarGiphySearch"`
FactoidGrammarTenorSearch string `json:"factoidGrammarTenorSearch"`
FactoidMsgAdd string `json:"factoidMsgAdd"`
FactoidMsgDelete string `json:"factoidMsgDelete"`
FactoidMsgIs string `json:"factoidMsgIs"`
FactoidMsgSnoop string `json:"factoidMsgSnoop"`
FactoidMsgCount string `json:"factoidMsgCount"`
PastebinAPIKey string `json:"pastebinApiKey"`
EnableDumpinen bool `json:"enableDumpinen"`
Dictionaries []struct {
Trigger string `json:"trigger"`
Dictionary string `json:"dictionary"`
FoundMsg string `json:"foundMsg"`
NotFoundMsg string `json:"notFoundMsg"`
} `json:"dictionaries"`
EnableSMHI bool `json:"enableSMHI"`
SMHILanguage string `json:"smhiLanguage"`
SMHICmdWeather string `json:"smhiCmdWeather"`
SMHIMsgWeatherError string `json:"smhiMsgWeatherError"`
SMHIMsgWeather string `json:"smhiMsgWeather"`
SMHIMsgWeatherFull string `json:"smhiMsgWeatherFull"`
SMHIMsgSun string `json:"smhiMsgSun"`
SMHIForecastLocations map[string]struct {
Alias string `json:"alias"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
}
EnableParcelTracking bool `json:"enableParcelTracking"`
ParcelTrackingPostNordAPIKey string `json:"parcelTrackingPostNordAPIKey"`
ParcelTrackingLocale string `json:"parcelTrackingLocale"`
ParcelTrackingMsgInfo string `json:"parcelTrackingMsgInfo"`
ParcelTrackingMsgAliasRemoved string `json:"parcelTrackingMsgAliasRemoved"`
ParcelTrackingMsgAliasDoesNotExist string `json:"parcelTrackingMsgAliasDoesNotExist"`
ParcelTrackingErrNoData string `json:"parcelTrackingErrNoData"`
ParcelTrackingErrDuplicateAlias string `json:"parcelTrackingErrDuplicateAlias"`
ParcelTrackingCmd string `json:"parcelTrackingCmd"`
ParcelTrackingCmdAdd string `json:"parcelTrackingCmdAdd"`
ParcelTrackingCmdRemove string `json:"parcelTrackingCmdRemove"`
ParcelTrackingCmdInfo string `json:"parcelTrackingCmdInfo"`
ParcelTrackingCmdFull string `json:"parcelTrackingCmdFull"`
ParcelTrackingCmdList string `json:"parcelTrackingCmdList"`
EnableWeek bool `json:"enableWeek"`
WeekCmd string `json:"weekCmd"`
EnableGoogleSearch bool `json:"enableGoogleSearch"`
GoogleSearchCmd string `json:"googleSearchCmd"`
}
}
// newBotFromConfig reads the configuration file from the given path and
// initializes a new bot structure by the values that exists in the
// configuration. If something goes wrong here it means that the config has
// some incorrect value and the caller of this function should fail loudly.
func newBotFromConfig(c string) (*bot, error) {
file, err := ioutil.ReadFile(c)
if err != nil {
return nil, fmt.Errorf("error: can't open config, %v", err)
}
bot := bot{}
decoder := json.NewDecoder(strings.NewReader(jsonc.ToJSON(string(file))))
if err = decoder.Decode(&bot); err != nil {
return nil, fmt.Errorf("error: can't decode config, %v", err)
}
// Make sure there's a DB error present.
if bot.DB.Err == "" {
bot.DB.Err = "a database error occured, contact admin"
}
bot.configFile = c
// Always default to Europe/Stockholm.
if bot.Timezone == "" {
bot.Timezone = "Europe/Stockholm"
}
if bot.timezone, err = time.LoadLocation(bot.Timezone); err != nil {
return nil, fmt.Errorf("error: can't load timezone %s, %v", bot.Timezone, err)
}
// Initialize the names map.
bot.IRC.names = make(map[string]bool)
// Convert the Operators array into a map so lookups will be
// efficient.
if len(bot.IRC.Operators) > 0 {
for _, o := range bot.IRC.Operators {
bot.IRC.operators = append(bot.IRC.operators, regexp.MustCompile(o))
}
}
// Create a map for the dynamic ignores and if there are permanent
// ignores we'll add them as regexps to the ignorePerm array.
if bot.IRC.EnableFloodProt {
bot.IRC.ignoreDyn = make(map[string]bool)
}
if len(bot.IRC.Ignore) > 0 {
for _, r := range bot.IRC.Ignore {
bot.IRC.ignorePerm = append(bot.IRC.ignorePerm, regexp.MustCompile(r))
}
}
// Convert Commands and CommandsStatic to the internal command
// structure.
bot.IRC.commands = make(map[string]command)
if len(bot.IRC.Commands) > 0 {
for k, v := range bot.IRC.Commands {
bot.IRC.commands[k] = parseCommand(true, v)
}
}
if len(bot.IRC.CommandsStatic) > 0 {
for k, v := range bot.IRC.CommandsStatic {
bot.IRC.commands[k] = parseCommand(false, v)
}
}
// Set the lastSentMessage to time.Now().
bot.IRC.lastSentMessageMu.Lock()
defer bot.IRC.lastSentMessageMu.Unlock()
bot.IRC.lastSentMessage = time.Now()
// Convert the integer grace period to time.Duration.
bot.IRC.gracePeriod = time.Duration(bot.IRC.GracePeriod) * time.Millisecond
// Initialize the loggers.
bot.logger = log.New(os.Stdout, "BOT: ", log.LstdFlags)
bot.HTTP.logger = log.New(os.Stdout, "HTTP: ", log.LstdFlags)
return &bot, nil
}
// start fires up a new instance of the bot, it connects to the IRC server and
// starts handling requests immediately. This function is blocking so we won't
// get any errors returned back to us unless there's a problem with a database
// migration.
func (b *bot) start() error {
// Set up the database connection. The initDB method will also run
// migrations in the database. So any change that you make to the
// repository in db.go will be executed when the bot starts. If
// there's an error in a migration we'll return an error. When this
// occur we should terminate the execution of the bot immediately so
// that we can address the migration issue before we launch the bot.
if err := b.initDB(); err != nil {
return err
}
// Initialize all the cron related objects and start processing of the
// existing cron jobs.
b.cron = newCron()
b.initCron()
// Calculate how many wait groups to wait for.
wgs := 1
if b.HTTP.EnableHTTP {
wgs = wgs + 1
}
// The HTTP and IRC main loops runs in a goroutine So we'll add gw to
// our wait group and wait until it completes.
b.mainWG.Add(wgs)
b.initIRC()
// Start the HTTP server.
if b.HTTP.EnableHTTP {
b.initHTTP()
}
b.mainWG.Wait()
return nil
}