-
Notifications
You must be signed in to change notification settings - Fork 6
/
bot.go
106 lines (85 loc) · 2.38 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
package main
import (
"log"
"github.com/MemeLabs/dggchat"
)
type bot struct {
log []dggchat.Message
maxLogLines int
parsers []func(m dggchat.Message, s *dggchat.Session)
lastNukeVictims []string
randomizer int
authCookie string
}
func newBot(authCookie string, maxLogLines int) *bot {
if maxLogLines < 0 {
maxLogLines = 0
}
b := bot{
log: make([]dggchat.Message, maxLogLines),
maxLogLines: maxLogLines,
randomizer: 0, // TODO workaround for dup msgs, remove me...
authCookie: authCookie,
}
return &b
}
func (b *bot) addParser(p ...func(m dggchat.Message, s *dggchat.Session)) {
b.parsers = append(b.parsers, p...)
}
func (b *bot) onMessage(m dggchat.Message, s *dggchat.Session) {
// remember maxLogLines messages
if len(b.log) >= b.maxLogLines {
b.log = b.log[1:]
}
b.log = append(b.log, m)
log.Printf("%s: %s\n", m.Sender.Nick, m.Message)
for _, p := range b.parsers {
p(m, s)
}
}
func (b *bot) onError(e string, s *dggchat.Session) {
log.Printf("[#] error: '%s'\n", e)
}
func (b *bot) onMute(m dggchat.Mute, s *dggchat.Session) {
log.Printf("[#] mute: '%s' by '%s'\n", m.Target.Nick, m.Sender.Nick)
}
func (b *bot) onUnmute(m dggchat.Mute, s *dggchat.Session) {
log.Printf("[#] unmute: '%s' by '%s'\n", m.Target.Nick, m.Sender.Nick)
}
func (b *bot) onBan(m dggchat.Ban, s *dggchat.Session) {
log.Printf("[#] ban: '%s' by '%s'\n", m.Target.Nick, m.Sender.Nick)
}
func (b *bot) onUnban(m dggchat.Ban, s *dggchat.Session) {
log.Printf("[#] unban: '%s' by '%s'\n", m.Target.Nick, m.Sender.Nick)
}
func (b *bot) onSocketError(err error, s *dggchat.Session) {
log.Printf("[#] socket error: '%s'\n", err.Error())
}
func (b *bot) onPMHandler(m dggchat.PrivateMessage, s *dggchat.Session) {
log.Printf("[#] PM: %s: %s\n", m.User.Nick, m.Message)
if isMod(m.User) {
// handle PM as command, TODO: rules shouldn't be handled here...
msg := dggchat.Message{
Sender: m.User,
Timestamp: m.Timestamp,
Message: m.Message,
}
for _, p := range b.parsers {
p(msg, s)
}
}
}
// return last n messsages for given user from log
func (b *bot) getLastMessages(nick string, n int) []dggchat.Message {
var output []dggchat.Message
for i := len(b.log) - 1; i >= 0; i-- {
if len(output) >= n {
return output
}
msg := b.log[i]
if msg.Sender.Nick == nick {
output = append(output, msg)
}
}
return output
}