forked from ugjka/kittybot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
internal_triggers.go
109 lines (100 loc) · 2.7 KB
/
internal_triggers.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
package kitty
import (
"fmt"
"strings"
"github.com/rhinosf1/ircmsg"
)
// A trigger to respond to the servers ping pong messages.
// If PingPong messages are not responded to, the server assumes the
// client has timed out and will close the connection.
// Note: this is automatically added in the IrcCon constructor.
var pingPong = Trigger{
Condition: func(bot *Bot, m *Message) bool {
return m.Command == "PING"
},
Action: func(bot *Bot, m *Message) {
bot.Send("PONG :" + m.Content)
},
}
var joinChannels = Trigger{
Condition: func(bot *Bot, m *Message) bool {
return m.Command == "001" || m.Command == "372"
},
Action: func(bot *Bot, m *Message) {
bot.joinOnce.Do(func() {
for _, channel := range bot.Channels {
splitchan := strings.SplitN(channel, ":", 2)
bot.Info("joining", "splitchan", splitchan)
if len(splitchan) == 2 {
channel = splitchan[0]
password := splitchan[1]
bot.Send(fmt.Sprintf("JOIN %s %s", channel, password))
} else {
bot.Send(fmt.Sprintf("JOIN %s", channel))
}
}
// Fire Joined
select {
case <-bot.Joined:
default:
close(bot.Joined)
}
})
},
}
// Get bot's prefix by catching its own join
var getPrefix = Trigger{
Condition: func(bot *Bot, m *Message) bool {
return m.Command == "JOIN" && m.Name == bot.getNick()
},
Action: func(bot *Bot, m *Message) {
bot.prefixMu.Lock()
bot.prefix = &ircmsg.Prefix{
Name: m.Prefix.Name,
User: m.Prefix.User,
Host: m.Prefix.Host,
}
bot.prefixMu.Unlock()
bot.Debug("got prefix", "prefix", bot.Prefix().String())
},
}
// Track nick changes internally so we can adjust the bot's prefix
var setNick = Trigger{
Condition: func(bot *Bot, m *Message) bool {
return m.Command == "NICK" && m.From == bot.getNick()
},
Action: func(bot *Bot, m *Message) {
bot.mu.Lock()
bot.nick = m.To
bot.mu.Unlock()
bot.PrefixChange(m.To, "", "")
bot.Info("nick changed successfully")
},
}
// Throw errors on invalid nick changes
var nickError = Trigger{
Condition: func(bot *Bot, m *Message) bool {
return m.Command == "436" || m.Command == "433" ||
m.Command == "432" || m.Command == "431" || m.Command == "400"
},
Action: func(bot *Bot, m *Message) {
bot.Error("nick change error", m.Params[1], m.Content)
},
}
var saslFail = Trigger{
Condition: func(bot *Bot, m *Message) bool {
return m.Command == "904" || m.Command == "905" ||
m.Command == "906" || m.Command == "907"
},
Action: func(bot *Bot, m *Message) {
bot.Crit("sasl fail", "error", m.Content)
},
}
var saslSuccess = Trigger{
Condition: func(bot *Bot, m *Message) bool {
return m.Command == "900" || m.Command == "903"
},
Action: func(bot *Bot, m *Message) {
bot.Info("sasl success", "info", m.Content)
},
}