-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
192 lines (167 loc) · 4.53 KB
/
handlers.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
package state
import (
"github.com/thoj/go-ircevent"
"strings"
)
func (st *StateTracker) joined(event *irc.Event) {
// Wait until ready
st.mutex.Lock()
channel := st.channels[event.Arguments[0]]
nick := st.nicks[event.Nick]
// Haven't seen this channel before
if channel == nil {
// Create a channel object
channel = &Channel{
Name: event.Arguments[0],
Nicks: make(map[string]*ChannelPrivileges),
}
// Put it in the channels map
st.channels[event.Arguments[0]] = channel
// Get some initial info about it
st.conn.Mode(channel.Name)
st.conn.Who(channel.Name)
}
// Not seen this nick before
if nick == nil {
// Create a nick object
nick = &Nick{
Nick: event.Nick,
User: event.User,
Host: event.Host,
Channels: make(map[string]*ChannelPrivileges),
}
// Put it in the nicks map
st.nicks[event.Nick] = nick
// Get some inital info about it
st.conn.Who(nick.Nick)
}
// Associate the nick with the channel
st.associate(event.Nick, event.Arguments[0])
// Ready for next event
st.mutex.Unlock()
}
func (st *StateTracker) kicked(event *irc.Event) {
st.mutex.Lock()
st.disassociate(event.Nick, event.Arguments[0])
st.mutex.Unlock()
}
func (st *StateTracker) nickChanged(event *irc.Event) {
st.mutex.Lock()
st.changeNick(event.Nick, event.Arguments[0])
st.mutex.Unlock()
}
func (st *StateTracker) parted(event *irc.Event) {
st.mutex.Lock()
st.disassociate(event.Nick, event.Arguments[0])
st.mutex.Unlock()
}
func (st *StateTracker) quitted(event *irc.Event) {
st.mutex.Lock()
st.deleteNick(event.Nick)
st.mutex.Unlock()
}
func (st *StateTracker) topicSet(event *irc.Event) {
st.mutex.Lock()
st.setTopic(event.Arguments[0], event.Arguments[1])
st.mutex.Unlock()
}
func (st *StateTracker) whoisReply(event *irc.Event) {
st.mutex.Lock()
nick := st.nicks[event.Arguments[1]]
if nick != nil && nick != st.GetNick(st.conn.GetNick()) {
nick.User = event.Arguments[2]
nick.Host = event.Arguments[3]
nick.Name = event.Arguments[5]
}
st.mutex.Unlock()
}
func (st *StateTracker) modeReply(event *irc.Event) {
st.mutex.Lock()
if channel, ok := st.channels[event.Arguments[0]]; ok {
channel.ParseModes(event.Arguments[1], event.Arguments[2:]...)
}
st.mutex.Unlock()
}
func (st *StateTracker) topicReply(event *irc.Event) {
st.mutex.Lock()
if channel := st.channels[event.Arguments[0]]; channel != nil {
st.setTopic(channel.Name, event.Arguments[1])
}
st.mutex.Unlock()
}
func (st *StateTracker) whoReply(event *irc.Event) {
st.mutex.Lock()
if nick, ok := st.nicks[event.Arguments[5]]; ok {
nick.User = event.Arguments[2]
nick.Host = event.Arguments[3]
if idx := strings.Index(event.Arguments[6], "*"); idx != -1 {
nick.Modes.Oper = true
}
if idx := strings.Index(event.Arguments[6], "H"); idx != -1 {
nick.Modes.Invisible = true
}
}
st.mutex.Unlock()
}
func (st *StateTracker) namesReply(event *irc.Event) {
st.mutex.Lock()
if channel, ok := st.channels[event.Arguments[2]]; ok {
names := strings.Split(strings.TrimSpace(event.Arguments[len(event.Arguments)-1]), " ")
for _, name := range names {
switch priv := name[0]; priv {
case '~', '&', '@', '%', '+':
name = name[1:]
fallthrough
default:
nick := st.nicks[name]
if nick == nil {
st.nicks[name] = &Nick{
Nick: name,
Channels: make(map[string]*ChannelPrivileges),
}
}
privs, ok := channel.Nicks[name]
if !ok {
privs = st.associate(name, channel.Name)
}
switch priv {
case '~':
privs.Owner = true
case '&':
privs.Admin = true
case '@':
privs.Op = true
case '%':
privs.HalfOp = true
case '+':
privs.Voice = true
}
}
}
}
st.mutex.Unlock()
}
func (st *StateTracker) whoisReplySSL(event *irc.Event) {
st.mutex.Lock()
if nick, ok := st.nicks[event.Arguments[0]]; ok && nick != st.GetNick(st.conn.GetNick()) {
nick.User = event.Arguments[1]
nick.Host = event.Arguments[2]
nick.Name = event.Arguments[4]
nick.Modes.SSL = true
}
st.mutex.Unlock()
}
func (st *StateTracker) InitStateCallbacks() {
st.conn.AddCallback("JOIN", st.joined)
st.conn.AddCallback("KICK", st.kicked)
st.conn.AddCallback("NICK", st.nickChanged)
st.conn.AddCallback("PART", st.parted)
st.conn.AddCallback("QUIT", st.quitted)
st.conn.AddCallback("TOPIC", st.topicSet)
st.conn.AddCallback("311", st.whoisReply)
st.conn.AddCallback("MODE", st.modeReply)
st.conn.AddCallback("332", st.topicReply)
st.conn.AddCallback("352", st.whoReply)
st.conn.AddCallback("353", st.namesReply)
st.conn.AddCallback("671", st.whoisReplySSL)
}