-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathirc.go
305 lines (250 loc) · 7.4 KB
/
irc.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
package main
import (
"fmt"
"math/rand"
"os"
"strings"
"time"
"github.com/osm/irc"
)
func init() {
rand.Seed(time.Now().Unix())
}
// VERSION will be set to the current git commit id on build.
var VERSION string
// initIRC connects to the IRC server that is defined in the configuration.
// The connection is launched within a goroutine. So this function will not
// block, so we have a wait group that controls the IRC life cycle.
func (b *bot) initIRC() {
// Make sure that all the below options are set in the configuration.
if b.IRC.Address == "" {
b.logger.Fatalf("irc.address is missing in %v", b.configFile)
}
if b.IRC.Channel == "" {
b.logger.Fatalf("irc.channel is missing in %v", b.configFile)
}
if b.IRC.Nick == "" {
b.logger.Fatalf("irc.nick is missing in %v", b.configFile)
}
if b.IRC.RealName == "" {
b.logger.Fatalf("irc.realName is missing in %v", b.configFile)
}
if b.IRC.User == "" {
b.logger.Fatalf("irc.user is missing in %v", b.configFile)
}
if b.IRC.Version == "" {
b.logger.Fatalf("irc.version is missing in %v", b.configFile)
}
// We'll set the grace period to 750 ms by default if no value was set
// in the config.
if b.IRC.GracePeriod == 0 {
b.IRC.GracePeriod = 750
}
// Prepare an array of irc options.
opts := []irc.Option{
irc.WithAddr(b.IRC.Address),
irc.WithChannel(b.IRC.Channel),
irc.WithDebug(),
irc.WithNick(b.IRC.Nick),
irc.WithRealName(b.IRC.RealName),
irc.WithUser(b.IRC.User),
irc.WithVersion(fmt.Sprintf("%s %s", b.IRC.Version, VERSION)),
}
// Append post connect messages, if there are any.
for _, pcm := range b.IRC.PostConnectMessages {
opts = append(opts, irc.WithPostConnectMessage(pcm.Target, pcm.Message))
}
// Append post connect modes, if there are any.
for _, m := range b.IRC.PostConnectModes {
opts = append(opts, irc.WithPostConnectMode(m))
}
b.IRC.client = irc.NewClient(opts...)
b.IRC.client.Handle("JOIN", b.operatorsHandler)
// Event handlers that are needed for the names map.
b.IRC.client.Handle("353", b.handleNamesReply)
b.IRC.client.Handle("JOIN", b.handleNamesAdd)
b.IRC.client.Handle("PART", b.handleNamesRemove)
b.IRC.client.Handle("QUIT", b.handleNamesRemove)
b.IRC.client.Handle("NICK", b.handleNamesChange)
if b.IRC.RejoinOnKick {
b.IRC.client.Handle("KICK", b.kickHandler)
}
if b.IRC.EnableFloodProt {
b.initFloodProt()
b.initFloodProtDefaults()
b.IRC.client.Handle("PRIVMSG", b.floodProtHandler)
}
if b.IRC.EnableUpdateNotifier {
b.initUpdateNotifier()
go b.updateNotifierHandler()
}
if b.IRC.EnableSMHI {
b.initSMHIDefaults()
b.IRC.client.Handle("PRIVMSG", b.smhiCommandHandler)
go b.smhiGetForecasts()
}
if b.IRC.EnableSupernytt {
b.initSupernytt()
go b.supernyttHandler()
}
if b.IRC.EnableQuiz {
b.initQuizDefaults()
b.IRC.client.Handle("PRIVMSG", b.quizHandler)
}
if b.IRC.EnableCommands {
b.initCommandDefaults()
b.IRC.client.Handle("PRIVMSG", b.commandHandler)
}
if b.IRC.EnableURLCheck {
b.initURLCheckDefaults()
b.IRC.client.Handle("PRIVMSG", b.urlCheckHandler)
}
if b.IRC.EnableURLMeta {
b.initURLMetaDefaults()
b.IRC.client.Handle("PRIVMSG", b.urlMetaHandler)
}
if b.IRC.EnableMarch {
b.initMarchDefaults()
b.IRC.client.Handle("PRIVMSG", b.marchHandler)
}
if b.IRC.EnableLogging {
b.IRC.client.Handle("PRIVMSG", b.loggingHandler)
b.IRC.client.Handle("PRIVMSG", b.seenHandler)
}
if b.IRC.EnableLyssnar {
b.initLyssnarDefaults()
b.IRC.client.Handle("PRIVMSG", b.lyssnarHandler)
}
if b.IRC.EnableGiphy {
b.initGiphyDefaults()
b.IRC.client.Handle("PRIVMSG", b.giphyHandler)
}
if b.IRC.EnableTenor {
b.initTenorDefaults()
b.IRC.client.Handle("PRIVMSG", b.tenorHandler)
}
if b.IRC.EnableChattistik {
b.initChattistikDefaults()
b.IRC.client.Handle("PRIVMSG", b.chattistikHandler)
}
if b.IRC.EnableFactoid {
b.initFactoidDefaults()
b.IRC.client.Handle("PRIVMSG", b.factoidHandler)
}
if b.IRC.EnableWeather {
b.initWeatherDefaults()
b.IRC.client.Handle("PRIVMSG", b.weatherHandler)
}
if b.IRC.EnableCron {
b.initCronDefaults()
b.IRC.client.Handle("PRIVMSG", b.cronHandler)
}
if len(b.IRC.Dictionaries) > 0 {
b.initDictionaries()
b.IRC.client.Handle("PRIVMSG", b.dictionaryHandler)
}
if b.IRC.EnableParcelTracking {
b.initParcelTrackingDefaults()
b.IRC.client.Handle("PRIVMSG", b.parcelTrackingCommandHandler)
}
if b.IRC.EnableWeek {
b.initWeekDefaults()
b.IRC.client.Handle("PRIVMSG", b.weekCommandHandler)
}
if b.IRC.EnableGoogleSearch {
b.initGoogleSearchDefaults()
b.IRC.client.Handle("PRIVMSG", b.googleSearchCommandHandler)
}
// This goroutine handles the connection to the IRC server. The IRC
// library will automatically try to reconnect if the connection dies
// for some reason.
go func() {
if err := b.IRC.client.Connect(); err != nil {
fmt.Printf("IRC connection died: %v\n", err)
os.Exit(1)
}
b.mainWG.Done()
}()
}
// preventSpam checks when the last message was sent and makes sure that the
// value defined in "GracePeriod" has been passed before we we proceed with
// the execution. This is used so that we don't spam the IRC server with
// messages too fast.
func (b *bot) preventSpam() {
// Acquire the lock and close it when we are done.
b.IRC.lastSentMessageMu.Lock()
defer b.IRC.lastSentMessageMu.Unlock()
// Get the current time and subtract it with when the last message was
// sent.
last := time.Now().Sub(b.IRC.lastSentMessage)
// If the last message was sent prior to the grace period we'll sleep
// for the remaining time.
if last < b.IRC.gracePeriod {
time.Sleep(b.IRC.gracePeriod - last)
}
// Set the last sent message to now.
b.IRC.lastSentMessage = time.Now()
}
// privmsg sends the given message back to the channel set from the
// configuration.
func (b *bot) privmsg(msg string) {
b.preventSpam()
b.IRC.client.Privmsg(b.IRC.Channel, msg)
}
// privmsgph replaces the keys of the phs map with the values and sends the
// message to the configured channel.
func (b *bot) privmsgph(msg string, phs map[string]string) {
b.preventSpam()
for k, v := range phs {
msg = strings.ReplaceAll(msg, k, v)
}
b.IRC.client.Privmsg(b.IRC.Channel, msg)
}
// privmsgpht replaces the keys of the phs map with the values and sends the
// message to the specified target.
func (b *bot) privmsgpht(msg, target string, phs map[string]string) {
b.preventSpam()
for k, v := range phs {
msg = strings.ReplaceAll(msg, k, v)
}
b.IRC.client.Privmsg(target, msg)
}
// action sends the given message back to the channel set from the
// configuration as an ACTION message.
func (b *bot) action(msg string) {
b.preventSpam()
b.IRC.client.Privmsg(b.IRC.Channel, "\u0001ACTION "+msg+"\u0001")
}
// rndName returns a random name from the names map.
func (b *bot) rndName() string {
i := 0
stop := rand.Intn(len(b.IRC.names))
for n := range b.IRC.names {
if i == stop {
return n
}
i++
}
return b.IRC.Nick
}
// shouldIgnore determines if the given message should be ignored by the bot
// or not.
func (b *bot) shouldIgnore(m *irc.Message) bool {
h := parseHost(m)
// Check if the message comes from a permanently ignored user.
for _, r := range b.IRC.ignorePerm {
if r.Match([]byte(h)) {
return true
}
}
if b.IRC.EnableFloodProt {
// Acquire the ignore mutex lock before we check if the dynamic ignore
// map contains the host.
b.IRC.ignoreDynMu.Lock()
defer b.IRC.ignoreDynMu.Unlock()
if _, ignored := b.IRC.ignoreDyn[h]; ignored {
return true
}
}
return false
}