forked from ugjka/kittybot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kittybot.go
428 lines (384 loc) · 10.7 KB
/
kittybot.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
// Package kitty is IRCv3 enabled framework for writing IRC bots
package kitty
import (
"bufio"
"fmt"
"net"
"strings"
"sync"
"time"
"github.com/rhinosf1/ircmsg"
log "gopkg.in/inconshreveable/log15.v2"
logext "gopkg.in/inconshreveable/log15.v2/ext"
"crypto/tls"
)
// Bot implements an irc bot to be connected to a given server
type Bot struct {
// This is set if we have hijacked a connection
reconnecting bool
// This is set if we have been hijacked
hijacked bool
con net.Conn
outgoing chan string
handlers []Handler
// -race complained a lot, we are thread safe now
mu sync.Mutex
// When did we start? Used for uptime
started time.Time
// Unix domain abstract socket address for reconnects (linux only)
unixastr string
// Unix domain socket address for other Unixes
unixsock string
unixlist *net.UnixListener
// Log15 loggger
log.Logger
joinOnce sync.Once
closeOnce sync.Once
wg sync.WaitGroup
// IRC CAPS and
// SASL credentials
capHandler *ircCaps
// Exported fields
Host string
Password string
Channels []string
SSL bool
SASL bool
HijackSession bool
// Set it if long messages get truncated
// on the receiving end
MsgSafetyBuffer bool
// HijackAfterFunc executes in its own goroutine after a succesful session hijack
// If you need to do something after a hijack
// for example, to run some irc commands or to restore some state
HijackAfterFunc func()
// Fires after joining the channels
Joined chan struct{}
// An optional function that connects to an IRC server over plaintext:
Dial func(network, addr string) (net.Conn, error)
// An optional function that connects to an IRC server over a secured connection:
DialTLS func(network, addr string, tlsConf *tls.Config) (*tls.Conn, error)
// This bots nick
Nick string
// Transient nick, that is used internally to track nick changes and calculate the prefix for the bot
nick string
// Duration to wait between sending of messages to avoid being
// kicked by the server for flooding (default 200ms)
ThrottleDelay time.Duration
// Maxmimum time between incoming data
PingTimeout time.Duration
TLSConfig tls.Config
// Bot's prefix
prefix *ircmsg.Prefix
prefixMu *sync.RWMutex
}
func (bot *Bot) String() string {
return fmt.Sprintf("Server: %s, Channels: %v, Nick: %s", bot.Host, bot.Channels, bot.getNick())
}
// NewBot creates a new instance of Bot
func NewBot(host, nick string, options ...func(*Bot)) *Bot {
// Defaults are set here
bot := Bot{
started: time.Now(),
unixastr: fmt.Sprintf("@%s-%s/bot", host, nick),
unixsock: fmt.Sprintf("/tmp/%s-%s-bot.sock", host, nick),
outgoing: make(chan string, 16),
Host: host,
Nick: nick,
nick: nick,
capHandler: &ircCaps{},
ThrottleDelay: 200 * time.Millisecond,
PingTimeout: 300 * time.Second,
HijackSession: false,
HijackAfterFunc: func() {},
Joined: make(chan struct{}),
SSL: false,
SASL: false,
Channels: []string{"#test"},
Password: "",
// Somewhat sane default if for some reason we can't retrieve bot's prefix
// for example, if the server doesn't advertise joins
prefix: &ircmsg.Prefix{
Name: nick,
User: nick,
Host: strings.Repeat("*", 510-353-len(nick)*2),
},
prefixMu: &sync.RWMutex{},
}
for _, option := range options {
option(&bot)
}
// Discard logs by default
bot.Logger = log.New("id", logext.RandId(8), "host", bot.Host, "nick", log.Lazy{Fn: bot.getNick})
bot.Logger.SetHandler(log.DiscardHandler())
bot.AddTrigger(pingPong)
bot.AddTrigger(joinChannels)
bot.AddTrigger(getPrefix)
bot.AddTrigger(setNick)
bot.AddTrigger(nickError)
bot.AddTrigger(bot.capHandler)
bot.AddTrigger(saslFail)
bot.AddTrigger(saslSuccess)
return &bot
}
// saslAuthenticate performs SASL authentication
// ref: https://github.com/atheme/charybdis/blob/master/doc/sasl.txt
func (bot *Bot) saslAuthenticate(user, pass string) {
bot.capHandler.saslEnable()
bot.capHandler.saslCreds(user, pass)
bot.Debug("beginning sasl authentication")
bot.Send("CAP LS")
bot.SetNick(bot.Nick)
bot.sendUserCommand(bot.Nick, bot.Nick, "0")
}
// standardRegistration performs a basic set of registration commands
func (bot *Bot) standardRegistration() {
bot.Send("CAP LS")
//Server registration
if bot.Password != "" {
bot.Send("PASS " + bot.Password)
}
bot.Debug("sending standard registration")
bot.sendUserCommand(bot.Nick, bot.Nick, "0")
bot.SetNick(bot.Nick)
}
// Set username, real name, and mode
func (bot *Bot) sendUserCommand(user, realname, mode string) {
bot.Send(fmt.Sprintf("USER %s %s * :%s", user, mode, realname))
}
func (bot *Bot) getNick() string {
bot.mu.Lock()
defer bot.mu.Unlock()
return bot.nick
}
func (bot *Bot) connect(host string) (err error) {
bot.Debug("connecting")
dial := bot.Dial
if dial == nil {
dial = net.Dial
}
dialTLS := bot.DialTLS
if dialTLS == nil {
dialTLS = tls.Dial
}
if bot.SSL {
bot.con, err = dialTLS("tcp", host, &bot.TLSConfig)
} else {
bot.con, err = dial("tcp", host)
}
return err
}
// Incoming message gathering routine
func (bot *Bot) handleIncomingMessages() {
defer bot.wg.Done()
scan := bufio.NewScanner(bot.con)
for scan.Scan() {
// Disconnect if we have seen absolutely nothing for 300 seconds
bot.con.SetDeadline(time.Now().Add(bot.PingTimeout))
msg := parseMessage(scan.Text())
bot.Debug("incoming", "raw", scan.Text(), "msg.To", msg.To, "msg.From", msg.From, "msg.Params", msg.Params, "msg.Tags", msg.Tags, "msg.Command", msg.Command, "msg.Trailing", msg.Trailing())
go func() {
for _, h := range bot.handlers {
go h.Handle(bot, msg)
}
}()
}
bot.close("incoming", scan.Err())
}
// Handles message speed throtling
func (bot *Bot) handleOutgoingMessages() {
defer bot.wg.Done()
for s := range bot.outgoing {
bot.Debug("outgoing", "data", s)
_, err := fmt.Fprint(bot.con, s+"\r\n")
if err != nil {
bot.close("outgoing", err)
return
}
time.Sleep(bot.ThrottleDelay)
}
}
// Run starts the bot and connects to the server. Blocks until we disconnect from the server.
// Returns true if we have been hijacked (if you loop over Run it might be wise to break on hijack
// to avoid looping between 2 instances).
func (bot *Bot) Run() (hijacked bool) {
bot.Debug("starting bot goroutines")
// Reset some things in case we re-run Run
bot.reset()
// Attempt reconnection
var hijack bool
if bot.HijackSession {
if bot.SSL {
bot.Crit("can't hijack an ssl connection")
return
}
hijack = bot.hijackSession()
bot.Debug("hijack", "did we?", hijack)
}
if !hijack {
err := bot.connect(bot.Host)
if err != nil {
bot.Crit("connect error", "err", err.Error())
return
}
bot.Info("connected successfully!")
}
bot.wg.Add(1)
go bot.handleIncomingMessages()
bot.wg.Add(1)
go bot.handleOutgoingMessages()
bot.wg.Add(1)
go bot.startUnixListener()
if hijack {
go bot.HijackAfterFunc()
}
// Only register on an initial connection
if !bot.reconnecting {
if bot.SASL {
bot.saslAuthenticate(bot.Nick, bot.Password)
} else {
bot.standardRegistration()
}
}
bot.wg.Wait()
bot.Info("disconnected")
return bot.hijacked
}
// CapStatus returns whether the server capability is enabled and present
func (bot *Bot) CapStatus(cap string) (enabled, present bool) {
bot.capHandler.mu.Lock()
defer bot.capHandler.mu.Unlock()
if v, ok := bot.capHandler.capsEnabled[cap]; ok {
return v, true
}
return false, false
}
// internal closer
func (bot *Bot) close(fault string, err error) {
bot.closeOnce.Do(func() {
if err != nil {
bot.Error(fault, "error", err)
}
if bot.unixlist != nil {
bot.unixlist.Close()
}
bot.con.Close()
select {
case bot.outgoing <- "PING":
default:
}
})
}
// Close closes the bot
func (bot *Bot) Close() {
bot.close("", nil)
}
// Prefix returns the bot's own prefix.
// Can be useful if for example you want to
// make an emoji wall that fits into one message perfectly
func (bot *Bot) Prefix() *ircmsg.Prefix {
bot.prefixMu.RLock()
prefix := &ircmsg.Prefix{
Name: bot.prefix.Name,
User: bot.prefix.User,
Host: bot.prefix.Host,
}
bot.prefixMu.RUnlock()
return prefix
}
// PrefixChange changes bot's prefix,
// use empty strings to make no change
func (bot *Bot) PrefixChange(name, user, host string) {
bot.prefixMu.Lock()
if name != "" {
bot.prefix.Name = name
}
if user != "" {
bot.prefix.User = user
}
if host != "" {
bot.prefix.Host = host
}
bot.prefixMu.Unlock()
}
// ReconOpt enables session hijacking
func ReconOpt() func(*Bot) {
return func(bot *Bot) {
bot.HijackSession = true
}
}
// SaslAuth enables SASL authentification
func SaslAuth(pass string) func(*Bot) {
return func(bot *Bot) {
bot.SASL = true
bot.Password = pass
}
}
// Uptime returns the uptime of the bot
func (bot *Bot) Uptime() string {
return fmt.Sprintf("Started: %s, Uptime: %s", bot.started, time.Since(bot.started))
}
func (bot *Bot) reset() {
// These need to be reset on each run
bot.mu.Lock()
bot.joinOnce = sync.Once{}
bot.closeOnce = sync.Once{}
bot.mu.Unlock()
bot.wg = sync.WaitGroup{}
bot.hijacked = false
bot.reconnecting = false
bot.capHandler.reset()
}
// Handler is used to subscribe and react to events on the bot Server
type Handler interface {
Handle(*Bot, *Message)
}
// Trigger is a Handler which is guarded by a condition.
// DO NOT alter *Message in your triggers or you'll have strange things happen.
type Trigger struct {
// Returns true if this trigger applies to the passed in message
Condition func(*Bot, *Message) bool
// The action to perform if Condition is true
Action func(*Bot, *Message)
}
// AddTrigger adds a trigger to the bot's handlers
func (bot *Bot) AddTrigger(h Handler) {
bot.handlers = append(bot.handlers, h)
}
// Handle executes the trigger action if the condition is satisfied
func (t Trigger) Handle(bot *Bot, m *Message) {
if t.Condition(bot, m) {
t.Action(bot, m)
}
}
// Message represents a message received from the server
type Message struct {
// ircmsg.Message with extended data, like GetTag() for IRCv3 tags
*ircmsg.Message
// Content generally refers to the text of a PRIVMSG
Content string
//Time at which this message was recieved
TimeStamp time.Time
// Entity that this message was addressed to (channel or user)
To string
// Nick of the messages sender (equivalent to Prefix.Name)
// Outdated, please use .Name
From string
}
// parseMessage takes a string and attempts to create a Message struct.
// Returns nil if the Message is invalid.
func parseMessage(raw string) (m *Message) {
m = new(Message)
m.Message = ircmsg.ParseMessage(raw)
m.Content = m.Trailing()
if len(m.Params) > 0 {
m.To = m.Params[0]
} else if m.Command == "JOIN" {
m.To = m.Trailing()
}
if m.Prefix != nil {
m.From = m.Prefix.Name
}
m.TimeStamp = time.Now()
return m
}