-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlocal_user.go
2375 lines (1998 loc) Β· 58.1 KB
/
local_user.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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"fmt"
"log"
"regexp"
"strings"
"time"
"github.com/horgh/irc"
)
// LocalUser holds information relevant only to a regular user (non-server)
// client.
type LocalUser struct {
// Local users are local clients and have the same attributes. Embed the type.
*LocalClient
// A reference to their user information.
User *User
// The last time we heard anything from the client.
LastActivityTime time.Time
// The last time we sent the client a PING.
LastPingTime time.Time
// The last time the client sent a PRIVMSG/NOTICE. We use this to decide
// idle time.
LastMessageTime time.Time
// MessageCounter is part of flood control. It tells us how many messages we
// have remaining before flood control kicks in. If it's 0, a message gets
// queued.
MessageCounter int
// MessageQueue holds queued messages from the client.
MessageQueue []irc.Message
}
// NewLocalUser makes a LocalUser from a LocalClient.
func NewLocalUser(c *LocalClient) *LocalUser {
now := time.Now()
u := &LocalUser{
LocalClient: c,
LastActivityTime: now,
LastPingTime: now,
LastMessageTime: now,
MessageCounter: UserMessageLimit,
MessageQueue: []irc.Message{},
}
return u
}
func (u *LocalUser) String() string {
return fmt.Sprintf("%s %s", u.User.String(), u.Conn.RemoteAddr())
}
// Message from this local user to another user, remote or local.
func (u *LocalUser) messageUser(to *User, command string, params []string) {
if to.isLocal() {
to.LocalUser.maybeQueueMessage(irc.Message{
Prefix: u.User.nickUhost(),
Command: command,
Params: params,
})
return
}
to.ClosestServer.maybeQueueMessage(irc.Message{
Prefix: string(u.User.UID),
Command: command,
Params: params,
})
}
func (u *LocalUser) serverNotice(s string) {
u.messageFromServer("NOTICE", []string{
u.User.DisplayNick,
fmt.Sprintf("*** Notice --- %s", s),
})
}
// Make TS6 UID. UID = SID concatenated with ID
func (u *LocalUser) makeTS6UID(id uint64) (TS6UID, error) {
ts6id, err := makeTS6ID(u.ID)
if err != nil {
return TS6UID(""), err
}
return TS6UID(string(u.Catbox.Config.TS6SID) + string(ts6id)), nil
}
// Send an IRC message to a client. Appears to be from the server.
// This works by writing to a client's channel.
//
// Note: Only the server goroutine should call this (due to channel use).
func (u *LocalUser) messageFromServer(command string, params []string) {
// For numeric messages, we need to prepend the nick.
if isNumericCommand(command) {
newParams := []string{u.User.DisplayNick}
newParams = append(newParams, params...)
params = newParams
}
u.maybeQueueMessage(irc.Message{
Prefix: u.Catbox.Config.ServerName,
Command: command,
Params: params,
})
}
// join tries to join the client to a channel.
//
// We've validated the name is valid and have canonicalized it.
func (u *LocalUser) join(channelName string) {
// Is the client in the channel already? Ignore it if so.
if u.User.onChannel(&Channel{Name: channelName}) {
return
}
// Look up the channel. Create it if necessary.
channel, channelExists := u.Catbox.Channels[channelName]
if !channelExists {
channel = &Channel{
Name: channelName,
Members: make(map[TS6UID]struct{}),
Ops: make(map[TS6UID]*User),
Modes: make(map[byte]struct{}),
TS: time.Now().Unix(),
}
u.Catbox.Channels[channelName] = channel
channel.grantOps(u.User)
channel.Modes['n'] = struct{}{}
channel.Modes['s'] = struct{}{}
}
// Add them to the channel.
channel.Members[u.User.UID] = struct{}{}
u.User.Channels[channelName] = channel
// Tell the client about the join.
// This is what RFC says to send: JOIN, RPL_TOPIC, and RPL_NAMREPLY.
// JOIN comes from the client, to the client.
u.messageUser(u.User, "JOIN", []string{channel.Name})
// If this is a new channel, send them the modes we set by default.
if !channelExists {
u.messageFromServer("MODE", []string{channel.Name, "+ns"})
}
// It appears RPL_TOPIC is optional, at least ircd-ratbox does always send it.
// Presumably if there is no topic.
if len(channel.Topic) > 0 {
// 332 RPL_TOPIC
u.messageFromServer("332", []string{channel.Name, channel.Topic})
// 333 tells about who set the topic and topic TS (when set). This is not
// standard.
u.messageFromServer("333", []string{
channel.Name,
channel.TopicSetter,
fmt.Sprintf("%d", channel.TopicTS),
})
}
// 353 RPL_NAMREPLY: This tells the client about who is in the channel
// (including itself).
// Format: :<server> 353 <targetNick> <channel flag> <#channel> :<nicks>
// <nicks> is a list of nicknames in the channel. Each is prefixed with @
// or + to indicate opped/voiced). Apparently only one or the other.
// Channel flag: = (public), * (private), @ (secret)
// When we have more chan modes (-s / +p) this needs to vary
channelFlag := "@"
// We put as many nicks per line as possible.
// First build the portion that is common to every NAMREPLY so we can get
// its length.
namMessage := irc.Message{
Prefix: u.Catbox.Config.ServerName,
Command: "353",
// Last parameter is where nicks go. We'll have " :" since it's blank
// right now (when we encode to determine base size).
Params: []string{u.User.DisplayNick, channelFlag, channel.Name, ""},
}
// If encoding the message truncates before we add any nicks, then there is no
// point continuing.
messageBuf, err := namMessage.Encode()
if err != nil {
log.Printf("Unable to generate RPL_NAMREPLY: %s", err)
return
}
baseSize := len(messageBuf)
nicks := ""
for memberUID := range channel.Members {
member := u.Catbox.Users[memberUID]
// We send the nick with its mode prefix.
sendNick := member.DisplayNick
if channel.userHasOps(member) {
sendNick = "@" + sendNick
}
// Assume 1 nick will always be okay to send.
if len(nicks) == 0 {
nicks += sendNick
continue
}
// If we add another nick, will we be above our line length? If so, fire off
// the message and start with the nick in a new list.
// +1 for " "
if baseSize+len(nicks)+1+len(member.DisplayNick) > irc.MaxLineLength {
namMessage.Params[3] = nicks
u.maybeQueueMessage(namMessage)
nicks = "" + sendNick
continue
}
nicks += " " + sendNick
}
if len(nicks) > 0 {
namMessage.Params[3] = nicks
u.maybeQueueMessage(namMessage)
}
// 366 RPL_ENDOFNAMES: Ends NAMES list.
u.messageFromServer("366", []string{channel.Name, "End of NAMES list"})
// Tell each member in the channel about the client.
// Only local clients. Servers will tell their own clients.
for memberUID := range channel.Members {
member := u.Catbox.Users[memberUID]
if !member.isLocal() {
continue
}
// Don't tell the client. We already did (above).
if member.UID == u.User.UID {
continue
}
// From the client to each member.
u.messageUser(member, "JOIN", []string{channel.Name})
}
// Tell servers about this.
// If it's a new channel, then use SJOIN. Otherwise JOIN.
for _, server := range u.Catbox.LocalServers {
if !channelExists {
server.maybeQueueMessage(irc.Message{
Prefix: string(u.Catbox.Config.TS6SID),
Command: "SJOIN",
Params: []string{
fmt.Sprintf("%d", channel.TS),
channel.Name,
"+ns",
"@" + string(u.User.UID),
},
})
} else {
server.maybeQueueMessage(irc.Message{
Prefix: string(u.User.UID),
Command: "JOIN",
Params: []string{
fmt.Sprintf("%d", channel.TS),
channel.Name,
"+",
},
})
}
}
}
// part tries to remove the client from the channel.
//
// We send a reply to the client. We also inform any other clients that need to
// know.
//
// NOTE: Only the server goroutine should call this (as we interact with its
// member variables).
func (u *LocalUser) part(channelName, message string) {
channelName = canonicalizeChannel(channelName)
if !isValidChannel(channelName) {
// 403 ERR_NOSUCHCHANNEL. Used to indicate channel name is invalid.
u.messageFromServer("403", []string{channelName, "Invalid channel name"})
return
}
// Find the channel.
channel, exists := u.Catbox.Channels[channelName]
if !exists {
// 403 ERR_NOSUCHCHANNEL. Used to indicate channel name is invalid.
u.messageFromServer("403", []string{channelName, "No such channel"})
return
}
// Are they on the channel?
if !u.User.onChannel(channel) {
// 403 ERR_NOSUCHCHANNEL. Used to indicate channel name is invalid.
u.messageFromServer("403", []string{channelName, "You are not on that channel"})
return
}
partParams := []string{channelName}
if len(message) > 0 {
partParams = append(partParams, message)
}
// Tell local clients (including the client) about the part.
for memberUID := range channel.Members {
member := u.Catbox.Users[memberUID]
if !member.isLocal() {
continue
}
member.LocalUser.maybeQueueMessage(irc.Message{
Prefix: u.User.nickUhost(),
Command: "PART",
Params: partParams,
})
}
// Tell all servers. Looks like for TS6, or ratbox at least, channel
// membership is known globally, even if no clients present in the channel.
for _, server := range u.Catbox.LocalServers {
server.maybeQueueMessage(irc.Message{
Prefix: string(u.User.UID),
Command: "PART",
Params: partParams,
})
}
// Remove the client from the channel.
channel.removeUser(u.User)
// If they are the last member, then drop the channel completely.
if len(channel.Members) == 0 {
delete(u.Catbox.Channels, channel.Name)
}
}
// We inform servers about a QUIT if propagate is true. You may not want to
// do so if the client is getting kicked for another reason, such as KILL.
//
// In the case of KILL, you propagate a KILL message to servers rather than
// QUIT. This function does not do that for you. It propagates only a QUIT
// message if you ask it.
//
// Note: Only the server goroutine should call this (due to closing channel).
func (u *LocalUser) quit(msg string, propagate bool) {
// May already be cleaning up.
if _, exists := u.Catbox.LocalUsers[u.ID]; !exists {
return
}
log.Printf("Losing user %s", u)
// Tell all clients the client is in the channel with, and remove the client
// from each channel it is in.
// Tell each client only once.
// Tell only local clients. We tell servers separately, who in turn tell their
// clients.
toldClients := map[TS6UID]struct{}{}
for _, channel := range u.User.Channels {
for memberUID := range channel.Members {
member := u.Catbox.Users[memberUID]
if !member.isLocal() {
continue
}
if _, exists := toldClients[member.UID]; exists {
continue
}
toldClients[member.UID] = struct{}{}
member.LocalUser.maybeQueueMessage(irc.Message{
Prefix: u.User.nickUhost(),
Command: "QUIT",
Params: []string{msg},
})
}
channel.removeUser(u.User)
if len(channel.Members) == 0 {
delete(u.Catbox.Channels, channel.Name)
}
}
// Ensure we tell the client (e.g., if in no channels).
if _, exists := toldClients[u.User.UID]; !exists {
u.messageUser(u.User, "QUIT", []string{msg})
}
// Tell all servers. They need to know about client departing.
if propagate {
for _, server := range u.Catbox.LocalServers {
server.maybeQueueMessage(irc.Message{
Prefix: string(u.User.UID),
Command: "QUIT",
Params: []string{msg},
})
}
}
u.messageFromServer("ERROR", []string{msg})
close(u.WriteChan)
delete(u.Catbox.Nicks, canonicalizeNick(u.User.DisplayNick))
delete(u.Catbox.LocalUsers, u.ID)
if u.User.isOperator() {
delete(u.Catbox.Opers, u.User.UID)
}
delete(u.Catbox.Users, u.User.UID)
}
// Set the user away. We've been given a non-blank message.
func (u *LocalUser) setAway(message string) {
// Flag him as being away
u.User.AwayMessage = message
// Reply to the user.
// 306 RPL_NOWAWAY
u.maybeQueueMessage(irc.Message{
Prefix: u.Catbox.Config.ServerName,
Command: "306",
Params: []string{u.User.DisplayNick, "You have been marked as away"},
})
// Propagate.
for _, server := range u.Catbox.LocalServers {
server.maybeQueueMessage(irc.Message{
Prefix: string(u.User.UID),
Command: "AWAY",
Params: []string{message},
})
}
}
// Set the user back from away.
func (u *LocalUser) setUnaway() {
// If they're not away, don't do anything.
if u.User.AwayMessage == "" {
return
}
// Flag him as back.
u.User.AwayMessage = ""
// 305 RPL_UNAWAY
u.maybeQueueMessage(irc.Message{
Prefix: u.Catbox.Config.ServerName,
Command: "305",
Params: []string{
u.User.DisplayNick,
"You are no longer been marked as being away",
},
})
// Propagate.
for _, server := range u.Catbox.LocalServers {
server.maybeQueueMessage(irc.Message{
Prefix: string(u.User.UID),
Command: "AWAY",
Params: []string{},
})
}
}
// The user sent us a message. Deal with it.
func (u *LocalUser) handleMessage(m irc.Message) {
// Record that client said something to us just now.
u.LastActivityTime = time.Now()
// Clients SHOULD NOT (section 2.3) send a prefix. I'm going to disallow it
// completely for all commands.
if m.Prefix != "" {
u.messageFromServer("ERROR", []string{"Do not send a prefix"})
return
}
// Flood protection. If we've used all our available message space for now,
// queue it.
if !u.User.isFloodExempt() {
if u.MessageCounter == 0 {
log.Printf("%s is flooding. Queueing their message.", u.User.DisplayNick)
u.MessageQueue = append(u.MessageQueue, m)
// Check for overwhelming their queue and disconnect them if so.
if len(u.MessageQueue) >= ExcessFloodThreshold {
u.quit("Excess flood", true)
return
}
return
}
u.MessageCounter--
}
// Non-RFC command that appears to be widely supported. Just ignore it for
// now.
if m.Command == "CAP" {
return
}
if m.Command == "NICK" {
u.nickCommand(m)
return
}
if m.Command == "USER" {
u.userCommand(m)
return
}
if m.Command == "JOIN" {
u.joinCommand(m)
return
}
if m.Command == "PART" {
u.partCommand(m)
return
}
// Per RFC these commands are near identical.
if m.Command == "PRIVMSG" || m.Command == "NOTICE" {
u.privmsgCommand(m)
return
}
if m.Command == "LUSERS" {
u.lusersCommand()
return
}
if m.Command == "MOTD" {
u.motdCommand()
return
}
if m.Command == "QUIT" {
u.quitCommand(m)
return
}
if m.Command == "PONG" {
// Not doing anything with this. Just accept it.
return
}
if m.Command == "PING" {
u.pingCommand(m)
return
}
if m.Command == "DIE" {
u.dieCommand(m)
return
}
if m.Command == "RESTART" {
u.restartCommand(m)
return
}
if m.Command == "WHOIS" {
u.whoisCommand(m)
return
}
if m.Command == "OPER" {
u.operCommand(m)
return
}
if m.Command == "MODE" {
u.modeCommand(m)
return
}
if m.Command == "WHO" {
u.whoCommand(m)
return
}
if m.Command == "TOPIC" {
u.topicCommand(m)
return
}
if m.Command == "CONNECT" {
u.connectCommand(m)
return
}
if m.Command == "LINKS" {
u.linksCommand(m)
return
}
if m.Command == "WALLOPS" {
u.wallopsCommand(m)
return
}
if m.Command == "KILL" {
u.killCommand(m)
return
}
if m.Command == "KLINE" {
u.klineCommand(m)
return
}
if m.Command == "UNKLINE" {
u.unklineCommand(m)
return
}
if m.Command == "STATS" {
u.statsCommand(m)
return
}
if m.Command == "REHASH" {
u.rehashCommand(m)
return
}
if m.Command == "MAP" {
u.mapCommand(m)
return
}
if m.Command == "VERSION" {
u.versionCommand(m)
return
}
if m.Command == "TIME" {
u.timeCommand(m)
return
}
if m.Command == "WHOWAS" {
u.whowasCommand(m)
return
}
if m.Command == "AWAY" {
u.awayCommand(m)
return
}
if m.Command == "INVITE" {
u.inviteCommand(m)
return
}
if m.Command == "OPME" {
u.opmeCommand(m)
return
}
if m.Command == "SQUIT" {
u.squitCommand(m)
return
}
// Unknown command. We don't handle it yet anyway.
// 421 ERR_UNKNOWNCOMMAND
u.messageFromServer("421", []string{m.Command, "Unknown command"})
}
// The NICK command to happen both at connection registration time and
// after. There are different rules.
func (u *LocalUser) nickCommand(m irc.Message) {
// We should have one parameter: The nick they want.
if len(m.Params) == 0 {
// 431 ERR_NONICKNAMEGIVEN
u.messageFromServer("431", []string{"No nickname given"})
return
}
nick := m.Params[0]
// Truncate and validate the nick.
if len(nick) > u.Catbox.Config.MaxNickLength {
nick = nick[0:u.Catbox.Config.MaxNickLength]
}
if !isValidNick(u.Catbox.Config.MaxNickLength, nick) {
// 432 ERR_ERRONEUSNICKNAME
u.messageFromServer("432", []string{nick, "Erroneous nickname"})
return
}
// Ignore the command if it's the exact same as the current nick.
// This is a case sensitive comparison.
if nick == u.User.DisplayNick {
return
}
newNickCanon := canonicalizeNick(nick)
oldNickCanon := canonicalizeNick(u.User.DisplayNick)
// Nick must be unique. However, allow them to change their nick to a
// different case. e.g. "user" may change to "User", but no one else may.
if newNickCanon != oldNickCanon {
_, exists := u.Catbox.Nicks[newNickCanon]
if exists {
// 433 ERR_NICKNAMEINUSE
u.messageFromServer("433", []string{nick, "Nickname is already in use"})
return
}
}
// Free the old nick.
delete(u.Catbox.Nicks, oldNickCanon)
// Flag the nick as taken by this client.
u.Catbox.Nicks[newNickCanon] = u.User.UID
// Nick TS changes when nick is set.
u.User.NickTS = time.Now().Unix()
// We need to inform other clients about the nick change.
// Any that are in the same channel as this client.
// Tell only local clients. Tell all servers after.
// Tell each client only once.
// Message needs to come from the OLD nick.
informedClients := map[TS6UID]struct{}{}
for _, channel := range u.User.Channels {
for memberUID := range channel.Members {
member := u.Catbox.Users[memberUID]
if !member.isLocal() {
continue
}
_, exists := informedClients[member.UID]
if exists {
continue
}
informedClients[member.UID] = struct{}{}
u.messageUser(member, "NICK", []string{nick})
}
}
// Reply to the client. We should have above, but if they were not on any
// channels then we did not.
_, exists := informedClients[u.User.UID]
if !exists {
u.messageUser(u.User, "NICK", []string{nick})
}
// Finally, make the update. Do this last as we need to ensure we act as the
// old nick when crafting messages.
u.User.DisplayNick = nick
// Propagate to servers.
for _, server := range u.Catbox.LocalServers {
server.maybeQueueMessage(irc.Message{
Prefix: string(u.User.UID),
Command: "NICK",
Params: []string{u.User.DisplayNick, fmt.Sprintf("%d", u.User.NickTS)},
})
}
}
// The USER command only occurs during connection registration.
func (u *LocalUser) userCommand(m irc.Message) {
// 462 ERR_ALREADYREGISTRED
u.messageFromServer("462", []string{"Unauthorized command (already registered)"})
}
func (u *LocalUser) joinCommand(m irc.Message) {
// Parameters: ( <channel> *( "," <channel> ) [ <key> *( "," <key> ) ] ) / "0"
if len(m.Params) == 0 {
// 461 ERR_NEEDMOREPARAMS
u.messageFromServer("461", []string{"JOIN", "Not enough parameters"})
return
}
// JOIN 0 is a special case. Client leaves all channels.
if m.Params[0] == "0" {
for _, channel := range u.User.Channels {
u.part(channel.Name, "")
}
return
}
// May have multiple channels in a single command.
channels := commaChannelsToChannelNames(m.Params[0])
// We could support keys.
// Try to join the client to the channels.
for _, channelName := range channels {
u.join(channelName)
}
}
func (u *LocalUser) partCommand(m irc.Message) {
// Parameters: <channel> *( "," <channel> ) [ <Part Message> ]
if len(m.Params) == 0 {
// 461 ERR_NEEDMOREPARAMS
u.messageFromServer("461", []string{"PART", "Not enough parameters"})
return
}
partMessage := ""
if len(m.Params) >= 2 {
partMessage = m.Params[1]
}
// May have multiple channels in a single command.
channels := commaChannelsToChannelNames(m.Params[0])
for _, channel := range channels {
u.part(channel, partMessage)
}
}
// Per RFC 2812, PRIVMSG and NOTICE are essentially the same, so both PRIVMSG
// and NOTICE use this command function.
func (u *LocalUser) privmsgCommand(m irc.Message) {
// Parameters: <msgtarget> <text to be sent>
if len(m.Params) == 0 {
// 411 ERR_NORECIPIENT
u.messageFromServer("411", []string{"No recipient given (PRIVMSG)"})
return
}
if len(m.Params) == 1 || len(m.Params[1]) == 0 {
// 412 ERR_NOTEXTTOSEND
u.messageFromServer("412", []string{"No text to send"})
return
}
// I don't check if there are too many parameters. They get ignored anyway.
target := m.Params[0]
msg := m.Params[1]
// Are we messaging a channel? Note I only support # channels right now.
if target[0] == '#' {
channelName := canonicalizeChannel(target)
if !isValidChannel(channelName) {
// 404 ERR_CANNOTSENDTOCHAN
u.messageFromServer("404", []string{channelName, "Cannot send to channel"})
return
}
channel, exists := u.Catbox.Channels[channelName]
if !exists {
// 403 ERR_NOSUCHCHANNEL
u.messageFromServer("403", []string{channelName, "No such channel"})
return
}
// Are they on it?
// Technically we should allow messaging if they aren't on it
// depending on the mode.
if !u.User.onChannel(channel) {
// 404 ERR_CANNOTSENDTOCHAN
u.messageFromServer("404", []string{channelName, "Cannot send to channel"})
return
}
u.LastMessageTime = time.Now()
// Send to all members of the channel. Except the client itself it seems.
// Tell local users directly.
// If a user is remote, record the server we should propagate the message
// towards. Tell each server only once.
toServers := make(map[*LocalServer]struct{})
for memberUID := range channel.Members {
member := u.Catbox.Users[memberUID]
if member.UID == u.User.UID {
continue
}
if member.isLocal() {
// From the client to each member.
u.messageUser(member, m.Command, []string{channel.Name, msg})
continue
}
toServers[member.ClosestServer] = struct{}{}
}
// Propagate message to any servers that need it.
for server := range toServers {
server.maybeQueueMessage(irc.Message{
Prefix: string(u.User.UID),
Command: m.Command,
Params: []string{channel.Name, msg},
})
}
return
}
// We're messaging a nick directly.
nickName := canonicalizeNick(target)
if !isValidNick(u.Catbox.Config.MaxNickLength, nickName) {
// 401 ERR_NOSUCHNICK
u.messageFromServer("401", []string{nickName, "No such nick/channel"})
return
}
targetUID, exists := u.Catbox.Nicks[nickName]
if !exists {
// 401 ERR_NOSUCHNICK
u.messageFromServer("401", []string{nickName, "No such nick/channel"})
return
}
targetUser := u.Catbox.Users[targetUID]
u.LastMessageTime = time.Now()
if targetUser.isLocal() {
u.messageUser(targetUser, m.Command, []string{nickName, msg})
} else {
u.messageUser(targetUser, m.Command, []string{string(targetUser.UID),
msg})
}
// Reply with 301 RPL_AWAY if they're away.
if len(targetUser.AwayMessage) > 0 {
u.maybeQueueMessage(irc.Message{
Prefix: u.Catbox.Config.ServerName,
Command: "301",
Params: []string{
u.User.DisplayNick,
targetUser.DisplayNick,
targetUser.AwayMessage,
},
})
}
}
func (u *LocalUser) lusersCommand() {
// We always send RPL_LUSERCLIENT and RPL_LUSERME.
// The others only need be sent if the counts are non-zero.
// 251 RPL_LUSERCLIENT
u.messageFromServer("251", []string{
fmt.Sprintf("There are %d users and %d services on %d servers.",
len(u.Catbox.Users),
0,
// +1 to count ourself.
len(u.Catbox.Servers)+1),
})
// 252 RPL_LUSEROP
operCount := 0
for _, user := range u.Catbox.Users {
if user.isOperator() {
operCount++
}
}
if operCount > 0 {
// 252 RPL_LUSEROP
u.messageFromServer("252", []string{
fmt.Sprintf("%d", operCount),
"operator(s) online",
})
}
// 253 RPL_LUSERUNKNOWN
// Unregistered connections.
numUnknown := len(u.Catbox.LocalClients)
if numUnknown > 0 {
u.messageFromServer("253", []string{
fmt.Sprintf("%d", numUnknown),
"unknown connection(s)",
})