-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
tracker.go
319 lines (243 loc) · 6.33 KB
/
tracker.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
package irc
// TODO: store all nicks by uuid and map them in outgoing seabird events rather
// than passing the nicks around directly
// TODO: properly handle figuring out the mode when it changes for a user.
import (
"errors"
"strings"
"sync"
)
// Tracker provides a convenient interface to track users, the channels they are
// in, and what modes they have in those channels.
type Tracker struct {
sync.RWMutex
channels map[string]*ChannelState
isupport *ISupportTracker
currentNick string
}
// NewTracker creates a new tracker instance.
func NewTracker(isupport *ISupportTracker) *Tracker {
return &Tracker{
channels: make(map[string]*ChannelState),
isupport: isupport,
}
}
// ChannelState represents the current state of a channel, including the name,
// topic, and all users in it.
type ChannelState struct {
Name string
Topic string
Users map[string]struct{}
}
// ListChannels will list the names of all known channels.
func (t *Tracker) ListChannels() []string {
t.RLock()
defer t.RUnlock()
ret := make([]string, 0, len(t.channels))
for channel := range t.channels {
ret = append(ret, channel)
}
return ret
}
// GetChannel will look up the ChannelState for a given channel name. It will
// return nil if the channel is unknown.
func (t *Tracker) GetChannel(name string) *ChannelState {
t.RLock()
defer t.RUnlock()
return t.channels[name]
}
// Handle needs to be called for all 001, 332, 353, JOIN, TOPIC, PART, KICK,
// QUIT, and NICK messages. All other messages will be ignored. Note that this
// will not handle calling the underlying ISupportTracker's Handle method.
func (t *Tracker) Handle(msg *Message) error {
switch msg.Command {
case "001":
return t.handle001(msg)
case "332":
return t.handleRplTopic(msg)
case "353":
return t.handleRplNamReply(msg)
case "JOIN":
return t.handleJoin(msg)
case "TOPIC":
return t.handleTopic(msg)
case "PART":
return t.handlePart(msg)
case "KICK":
return t.handleKick(msg)
case "QUIT":
return t.handleQuit(msg)
case "NICK":
return t.handleNick(msg)
}
return nil
}
func (t *Tracker) handle001(msg *Message) error {
if len(msg.Params) != 2 {
return errors.New("malformed RPL_WELCOME message")
}
t.Lock()
defer t.Unlock()
t.currentNick = msg.Params[0]
return nil
}
func (t *Tracker) handleTopic(msg *Message) error {
if len(msg.Params) != 2 {
return errors.New("malformed TOPIC message")
}
channel := msg.Params[0]
topic := msg.Trailing()
t.Lock()
defer t.Unlock()
if _, ok := t.channels[channel]; !ok {
return errors.New("received TOPIC message for unknown channel")
}
t.channels[channel].Topic = topic
return nil
}
func (t *Tracker) handleRplTopic(msg *Message) error {
if len(msg.Params) != 3 {
return errors.New("malformed RPL_TOPIC message")
}
// client set channel topic to topic
// client := msg.Params[0]
channel := msg.Params[1]
topic := msg.Trailing()
t.Lock()
defer t.Unlock()
if _, ok := t.channels[channel]; !ok {
return errors.New("received RPL_TOPIC for unknown channel")
}
t.channels[channel].Topic = topic
return nil
}
func (t *Tracker) handleJoin(msg *Message) error {
if len(msg.Params) != 1 {
return errors.New("malformed JOIN message")
}
// user joined channel
user := msg.Prefix.Name
channel := msg.Trailing()
t.Lock()
defer t.Unlock()
_, ok := t.channels[channel]
if !ok {
if user != t.currentNick {
return errors.New("received JOIN message for unknown channel")
}
t.channels[channel] = &ChannelState{Name: channel, Users: make(map[string]struct{})}
}
state := t.channels[channel]
state.Users[user] = struct{}{}
return nil
}
func (t *Tracker) handlePart(msg *Message) error {
if len(msg.Params) < 1 {
return errors.New("malformed PART message")
}
// user joined channel
user := msg.Prefix.Name
channel := msg.Params[0]
t.Lock()
defer t.Unlock()
if _, ok := t.channels[channel]; !ok {
return errors.New("received PART message for unknown channel")
}
// If we left the channel, we can drop the whole thing, otherwise just drop
// this user from the channel.
if user == t.currentNick {
delete(t.channels, channel)
} else {
state := t.channels[channel]
delete(state.Users, user)
}
return nil
}
func (t *Tracker) handleKick(msg *Message) error {
if len(msg.Params) != 3 {
return errors.New("malformed KICK message")
}
// user was kicked from channel by actor
// actor := msg.Prefix.Name
user := msg.Params[1]
channel := msg.Params[0]
t.Lock()
defer t.Unlock()
if _, ok := t.channels[channel]; !ok {
return errors.New("received KICK message for unknown channel")
}
// If we left the channel, we can drop the whole thing, otherwise just drop
// this user from the channel.
if user == t.currentNick {
delete(t.channels, channel)
} else {
state := t.channels[channel]
delete(state.Users, user)
}
return nil
}
func (t *Tracker) handleQuit(msg *Message) error {
if len(msg.Params) != 1 {
return errors.New("malformed QUIT message")
}
// user quit
user := msg.Prefix.Name
t.Lock()
defer t.Unlock()
for _, state := range t.channels {
delete(state.Users, user)
}
return nil
}
func (t *Tracker) handleNick(msg *Message) error {
if len(msg.Params) != 1 {
return errors.New("malformed NICK message")
}
// oldUser renamed to newUser
oldUser := msg.Prefix.Name
newUser := msg.Params[0]
t.Lock()
defer t.Unlock()
if t.currentNick == oldUser {
t.currentNick = newUser
}
for _, state := range t.channels {
if _, ok := state.Users[oldUser]; ok {
delete(state.Users, oldUser)
state.Users[newUser] = struct{}{}
}
}
return nil
}
func (t *Tracker) handleRplNamReply(msg *Message) error {
if len(msg.Params) != 4 {
return errors.New("malformed RPL_NAMREPLY message")
}
channel := msg.Params[2]
users := strings.Split(strings.TrimSpace(msg.Trailing()), " ")
prefixes, ok := t.isupport.GetPrefixMap()
if !ok {
return errors.New("ISupport missing prefix map")
}
t.Lock()
defer t.Unlock()
if _, ok := t.channels[channel]; !ok {
return errors.New("received RPL_NAMREPLY message for untracked channel")
}
for _, user := range users {
i := strings.IndexFunc(user, func(r rune) bool {
_, ok := prefixes[r]
return !ok
})
if i != -1 {
user = user[i:]
}
// The bot user should be added via JOIN
if user == t.currentNick {
continue
}
state := t.channels[channel]
state.Users[user] = struct{}{}
}
return nil
}