forked from 42wim/matterircd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchannel.go
478 lines (369 loc) · 9.26 KB
/
channel.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
package irckit
import (
"sort"
"strings"
"sync"
"time"
"github.com/mattermost/mattermost-server/v6/model"
"github.com/muesli/reflow/wordwrap"
"github.com/sorcix/irc"
)
// Channel is a representation of a room in our server
type Channel interface {
Prefixer
// ID is a normalized unique identifier for the channel
ID() string
// Created returns the time when the Channel was created.
Created() time.Time
// Names returns a sorted slice of Nicks in the channel
Names() []string
// Users returns a slice of Users in the channel.
Users() []*User
// HasUser returns whether a User is in the channel.
HasUser(*User) bool
// Invite prompts the User to join the Channel on behalf of Prefixer.
Invite(from Prefixer, u *User) error
// SendNamesResponse sends a User messages indicating the current members of the Channel.
SendNamesResponse(u *User) error
// Join introduces the User to the channel (handler for JOIN).
Join(u *User) error
// BatchJoin
BatchJoin(users []*User) error
// Part removes the User from the channel (handler for PART).
Part(u *User, text string)
// Message transmits a message from a User to the channel (handler for PRIVMSG).
Message(u *User, text string)
// Service returns the service that set the channel
Service() string
// Topic sets the topic of the channel (handler for TOPIC).
Topic(from Prefixer, text string)
// GetTopic gets the topic of the channel
GetTopic() string
// Unlink will disassociate the Channel from its Server.
Unlink()
// Len returns the number of Users in the channel.
Len() int
// String returns the name of the channel
String() string
// Spoof message
SpoofMessage(from string, text string, maxlen ...int)
// Spoof notice
SpoofNotice(from string, text string, maxlen ...int)
IsPrivate() bool
}
type channel struct {
created time.Time
name string
server Server
id string
service string
private bool
mu sync.RWMutex
topic string
usersIdx map[string]*User
}
// NewChannel returns a Channel implementation for a given Server.
func NewChannel(server Server, channelID string, name string, service string, modes map[string]bool) Channel {
return &channel{
created: time.Now(),
server: server,
id: channelID,
name: name,
service: service,
private: modes["p"],
usersIdx: make(map[string]*User),
}
}
func (ch *channel) GetTopic() string {
return ch.topic
}
func (ch *channel) Prefix() *irc.Prefix {
return ch.server.Prefix()
}
func (ch *channel) Service() string {
return ch.service
}
func (ch *channel) String() string {
return ch.name
}
// Created returns the time when the Channel was created.
func (ch *channel) Created() time.Time {
return ch.created
}
// ID returns a normalized unique identifier for the channel.
func (ch *channel) ID() string {
return ID(ch.id)
}
func (ch *channel) Message(from *User, text string) {
text = wordwrap.String(text, 440)
lines := strings.Split(text, "\n")
for _, l := range lines {
msg := &irc.Message{
Prefix: from.Prefix(),
Command: irc.PRIVMSG,
Params: []string{ch.name},
Trailing: l,
EmptyTrailing: true,
}
ch.mu.RLock()
for _, to := range ch.usersIdx {
to.Encode(msg)
}
ch.mu.RUnlock()
}
}
// Quit will remove the user from the channel and emit a PART message.
func (ch *channel) Part(u *User, text string) {
msg := &irc.Message{
Prefix: u.Prefix(),
Command: irc.PART,
Params: []string{ch.name},
Trailing: text,
}
ch.mu.Lock()
if _, ok := ch.usersIdx[u.ID()]; !ok {
ch.mu.Unlock()
u.Encode(&irc.Message{
Prefix: ch.Prefix(),
Command: irc.ERR_NOTONCHANNEL,
Params: []string{ch.name},
Trailing: "You're not on that channel",
})
return
}
u.Encode(msg)
delete(ch.usersIdx, u.ID())
u.Lock()
delete(u.channels, ch)
u.Unlock()
for _, to := range ch.usersIdx {
if !to.Ghost {
to.Encode(msg)
}
}
ch.mu.Unlock()
}
// Unlink will disassociate the Channel from the Server.
func (ch *channel) Unlink() {
ch.server.UnlinkChannel(ch)
}
// Close will evict all users in the channel.
func (ch *channel) Close() error {
ch.mu.Lock()
for _, to := range ch.usersIdx {
to.Encode(&irc.Message{
Prefix: to.Prefix(),
Command: irc.PART,
Params: []string{ch.name},
})
}
ch.usersIdx = make(map[string]*User)
ch.mu.Unlock()
return nil
}
// Invite prompts the User to join the Channel on behalf of Prefixer.
func (ch *channel) Invite(from Prefixer, u *User) error {
return u.Encode(&irc.Message{
Prefix: from.Prefix(),
Command: irc.INVITE,
Params: []string{u.Nick, ch.name},
})
// TODO: Save state that the user is invited?
}
// Topic sets the topic of the channel (handler for TOPIC).
func (ch *channel) Topic(from Prefixer, text string) {
ch.mu.RLock()
// this probably an echo
if ch.topic == text {
ch.mu.RUnlock()
return
}
ch.topic = text
// no newlines in topic
ch.topic = strings.ReplaceAll(ch.topic, "\n", " ")
ch.topic = strings.ReplaceAll(ch.topic, "\r", " ")
msg := &irc.Message{
Prefix: from.Prefix(),
Command: irc.TOPIC,
Params: []string{ch.name},
Trailing: ch.topic,
}
// only send join messages to real users
for _, to := range ch.usersIdx {
if !to.Ghost {
to.Encode(msg)
}
}
ch.mu.RUnlock()
}
// SendNamesResponse sends a User messages indicating the current members of the Channel.
func (ch *channel) SendNamesResponse(u *User) error {
msgs := []*irc.Message{}
line := ""
i := 0
for _, name := range ch.Names() {
if i+len(name) < 400 {
line += name + " "
i += len(name)
} else {
msgs = append(msgs, &irc.Message{
Prefix: ch.Prefix(),
Command: irc.RPL_NAMREPLY,
Params: []string{u.Nick, "=", ch.name},
Trailing: line,
})
line = ""
line += name + " "
i = len(name)
}
}
msgs = append(msgs, &irc.Message{
Prefix: ch.Prefix(),
Command: irc.RPL_NAMREPLY,
Params: []string{u.Nick, "=", ch.name},
Trailing: line,
})
msgs = append(msgs, &irc.Message{
Prefix: ch.Prefix(),
Params: []string{u.Nick, ch.name},
Command: irc.RPL_ENDOFNAMES,
Trailing: "End of /NAMES list.",
})
return u.Encode(msgs...)
}
func (ch *channel) BatchJoin(inputusers []*User) error {
// TODO: Check if user is already here?
var users []*User
ch.mu.Lock()
for _, u := range inputusers {
if _, exists := ch.usersIdx[u.ID()]; !exists {
users = append(users, u)
}
}
ch.mu.Unlock()
for _, u := range users {
ch.mu.Lock()
ch.usersIdx[u.ID()] = u
ch.mu.Unlock()
u.Lock()
u.channels[ch] = struct{}{}
u.Unlock()
}
return nil
}
// Join introduces a User to the channel (sends relevant messages, stores).
func (ch *channel) Join(u *User) error {
// TODO: Check if user is already here?
ch.mu.Lock()
if u.ID() == "" {
ch.mu.Unlock()
return nil
}
if _, exists := ch.usersIdx[u.ID()]; exists {
ch.mu.Unlock()
return nil
}
ch.usersIdx[u.ID()] = u
ch.mu.Unlock()
u.Lock()
u.channels[ch] = struct{}{}
u.Unlock()
// speed up &users join
if ch.name == "&users" && u.Ghost {
return nil
}
msg := &irc.Message{
Prefix: u.Prefix(),
Command: irc.JOIN,
Params: []string{ch.name},
}
// send regular users a notification of the join
ch.mu.RLock()
for _, to := range ch.usersIdx {
// only send join messages to real users
if !to.Ghost {
to.Encode(msg)
}
}
ch.mu.RUnlock()
ch.SendNamesResponse(u)
return nil
}
func (ch *channel) HasUser(u *User) bool {
ch.mu.RLock()
_, ok := ch.usersIdx[u.ID()]
ch.mu.RUnlock()
return ok
}
// Users returns an unsorted slice of users who are in the channel.
func (ch *channel) Users() []*User {
ch.mu.RLock()
users := make([]*User, 0, len(ch.usersIdx))
for _, u := range ch.usersIdx {
users = append(users, u)
}
ch.mu.RUnlock()
return users
}
// Names returns a sorted slice of Nick strings of users who are in the channel.
func (ch *channel) Names() []string {
users := ch.Users()
names := make([]string, 0, len(users))
for _, u := range users {
if strings.Contains(u.Roles, model.SystemAdminRoleId) {
names = append(names, "@"+u.Nick)
} else {
names = append(names, u.Nick)
}
}
// TODO: Append in sorted order?
sort.Strings(names)
return names
}
// Len returns the number of users in the channel.
func (ch *channel) Len() int {
ch.mu.RLock()
defer ch.mu.RUnlock()
return len(ch.usersIdx)
}
func (ch *channel) Spoof(from string, text string, cmd string, maxlen ...int) {
if len(maxlen) == 0 {
text = wordwrap.String(text, 440)
} else {
text = wordwrap.String(text, maxlen[0])
}
lines := strings.Split(text, "\n")
for _, l := range lines {
msg := &irc.Message{
Prefix: &irc.Prefix{Name: from, User: from, Host: from},
Command: cmd,
Params: []string{ch.name},
Trailing: l,
EmptyTrailing: true,
}
ch.mu.RLock()
for _, to := range ch.usersIdx {
to.Encode(msg)
}
ch.mu.RUnlock()
}
}
func (ch *channel) SpoofMessage(from string, text string, maxlen ...int) {
if len(maxlen) == 0 {
ch.Spoof(from, text, irc.PRIVMSG, 440)
} else {
ch.Spoof(from, text, irc.PRIVMSG, maxlen[0])
}
}
func (ch *channel) SpoofNotice(from string, text string, maxlen ...int) {
if len(maxlen) == 0 {
ch.Spoof(from, text, irc.NOTICE, 440)
} else {
ch.Spoof(from, text, irc.NOTICE, maxlen[0])
}
}
func (ch *channel) IsPrivate() bool {
ch.mu.RLock()
defer ch.mu.RUnlock()
return ch.private
}