-
Notifications
You must be signed in to change notification settings - Fork 3
/
local_server.go
2492 lines (2096 loc) Β· 60 KB
/
local_server.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"
"strconv"
"strings"
"time"
"github.com/horgh/irc"
)
// LocalServer means the client registered as a server. This holds its info.
type LocalServer struct {
*LocalClient
Server *Server
// The last time we heard anything from it.
LastActivityTime time.Time
// The last time we sent it a PING.
LastPingTime time.Time
// Flags to know about our bursting state.
GotPING bool
GotPONG bool
Bursting bool
}
// NewLocalServer upgrades a LocalClient to a LocalServer.
func NewLocalServer(c *LocalClient) *LocalServer {
now := time.Now()
s := &LocalServer{
LocalClient: c,
LastActivityTime: now,
LastPingTime: now,
GotPING: false,
GotPONG: false,
Bursting: true,
}
return s
}
func (s *LocalServer) String() string {
return fmt.Sprintf("%s %s", s.Server.String(), s.Conn.RemoteAddr())
}
func (s *LocalServer) messageFromServer(command string, params []string) {
// For numeric messages, we need to prepend the nick.
// Use * for the nick in cases where the client doesn't have one yet.
// This is what ircd-ratbox does. Maybe not RFC...
if isNumericCommand(command) {
newParams := []string{string(s.Server.SID)}
newParams = append(newParams, params...)
params = newParams
}
s.maybeQueueMessage(irc.Message{
Prefix: string(s.Catbox.Config.TS6SID),
Command: command,
Params: params,
})
}
func (s *LocalServer) quit(msg string) {
// May already be cleaning up.
_, exists := s.Catbox.LocalServers[s.ID]
if !exists {
return
}
// When quitting, you may think we should send SQUIT to all servers.
// But we don't. Or ircd-ratbox does not. Do the same.
// Just send it to our local servers, they propagate it.
s.messageFromServer("ERROR", []string{msg})
close(s.WriteChan)
s.serverSplitCleanUp(s.Server)
// Inform other servers that we are connected to.
for _, server := range s.Catbox.LocalServers {
server.maybeQueueMessage(irc.Message{
Prefix: string(s.Catbox.Config.TS6SID),
Command: "SQUIT",
Params: []string{string(s.Server.SID), msg},
})
}
s.Catbox.noticeLocalOpers(fmt.Sprintf("Server %s delinked: %s",
s.Server.Name, msg))
}
// lostServer is departing the network.
//
// Inform all local users of QUITs for clients on the other side.
//
// Also do our local bookkeeping:
// - Forget the clients on the other side
// - Forget the servers on the other side
// - Forget the server
//
// This can happen when a local server delinks from us, or we're hearing about
// a server departing remotely (from a SQUIT command).
//
// This function does not propagate any messages to any servers. It only sends
// messages to local clients.
func (s *LocalServer) serverSplitCleanUp(lostServer *Server) {
// The server may have been linked to other servers. Figure out all servers
// we're losing.
lostServers := lostServer.getLinkedServers(s.Catbox.Servers)
// Include the one we're losing with its links.
lostServers = append(lostServers, lostServer)
// Look for users we are losing.
for _, user := range s.Catbox.Users {
if user.isLocal() {
continue
}
// Are we losing this user?
// We are if it is on a server we are losing.
keepingUser := true
for _, server := range lostServers {
if user.Server == server {
keepingUser = false
break
}
}
if keepingUser {
continue
}
log.Printf("Losing user %s", user)
// This user is gone.
// Tell local users about them quitting.
// Remote users will be told by their own servers.
// Quit message format is important. It tells that there was a netsplit,
// and between which two servers.
var quitMessage string
if lostServer.isLocal() {
quitMessage = fmt.Sprintf("%s %s", s.Catbox.Config.ServerName,
lostServer.Name)
} else {
quitMessage = fmt.Sprintf("%s %s", lostServer.LinkedTo.Name,
lostServer.Name)
}
s.Catbox.quitRemoteUser(user, quitMessage)
}
// Forget all lost servers.
for _, server := range lostServers {
log.Printf("Losing server %s", server)
if server.isLocal() {
delete(s.Catbox.LocalServers, server.LocalServer.ID)
}
delete(s.Catbox.Servers, server.SID)
}
}
// Send the burst. This tells the server about the state of the world as we see
// it.
// We send our burst after seeing SVINFO. This means we have not yet processed
// any SID, UID, or SJOIN messages from the other side.
func (s *LocalServer) sendBurst() {
// Tell it about all servers we know about.
// Use the SID command.
//
// We do tell it about servers even if they are not directly linked to us.
//
// We need to be sure we set the prefix/source correctly to indicate what
// server they are linked to.
//
// Parameters: <server name> <hop count> <SID> <description>
// e.g.: :8ZZ SID irc3.example.com 2 9ZQ :My Desc
//
// It's also critical the order we inform the server about other servers.
// If we tell it about server B linked to server C (i.e., prefix is server C)
// but we haven't told it about server C yet, then it does not have sufficient
// information to validate the server. The server could take it on faith that
// it will be told about server C shortly, but that is not very good.
//
// We can accomplish this through telling it about servers ordered by hopcount
// ascending.
servers := sortServersByHopCount(s.Catbox.Servers)
for _, server := range servers {
// Don't send it itself.
if server.LocalServer == s {
continue
}
var linkedTo TS6SID
if server.isLocal() {
linkedTo = s.Catbox.Config.TS6SID
} else {
linkedTo = server.LinkedTo.SID
}
s.maybeQueueMessage(irc.Message{
Prefix: string(linkedTo),
Command: "SID",
Params: []string{
server.Name,
// All servers we know are an additional 1 hop away for it.
fmt.Sprintf("%d", server.HopCount+1),
string(server.SID),
server.Description,
},
})
// Tell it about the capabilities of each server too. ratbox does this
// during server link.
s.maybeQueueMessage(irc.Message{
Prefix: string(server.SID),
Command: "ENCAP",
Params: []string{"*", "GCAP", server.capabsString()},
})
}
// Tell it about all users we know about. Use the UID command.
// Ensure we set the prefix/source to the server it is on.
// Parameters: <nick> <hopcount> <nick TS> <umodes> <username> <hostname> <IP> <UID> :<real name>
// :8ZZ UID will 1 1475024621 +i will blashyrkh. 0 8ZZAAAAAB :will
for _, user := range s.Catbox.Users {
var onServer TS6SID
if user.isLocal() {
onServer = s.Catbox.Config.TS6SID
} else {
onServer = user.Server.SID
}
s.maybeQueueMessage(irc.Message{
Prefix: string(onServer),
Command: "UID",
Params: []string{
user.DisplayNick,
// Hop count increases for them by one.
fmt.Sprintf("%d", user.HopCount+1),
fmt.Sprintf("%d", user.NickTS),
user.modesString(),
user.Username,
user.Hostname,
user.IP,
string(user.UID),
user.RealName,
},
})
// Send AWAY if they are away.
if len(user.AwayMessage) == 0 {
continue
}
s.maybeQueueMessage(irc.Message{
Prefix: string(user.UID),
Command: "AWAY",
Params: []string{user.AwayMessage},
})
}
// Send channels and the users in them with SJOIN commands.
// Parameters: <channel TS> <channel name> <modes> [mode params] :<UIDs>
// e.g., :8ZZ SJOIN 1475187553 #test2 +sn :@8ZZAAAAAB
// Each UID may be prefixed with @ and/or + if voiced/opped.
for _, channel := range s.Catbox.Channels {
// We want to combine as many UIDs into a single SJOIN message as possible.
// First make a message with what is common to all messages so that we can
// determine the base length.
sjoinMessage := irc.Message{
Prefix: string(s.Catbox.Config.TS6SID),
Command: "SJOIN",
Params: []string{
fmt.Sprintf("%d", channel.TS),
channel.Name,
// Currently we only support +ns.
"+ns",
// UIDs go in the last parameter. As it is blank, encoding will turn it
// into " :" for us. This is acceptable.
"",
},
}
// If encoding the prefix truncates then we have a big problem. We won't be
// able to include any UIDs. Killing the connection is perhaps extreme but
// we cannot fully synchronize in this case.
sjoinEncoded, err := sjoinMessage.Encode()
if err != nil {
s.quit(fmt.Sprintf("Unable to create SJOIN message: %s", err))
return
}
baseSize := len(sjoinEncoded)
uids := ""
for uid := range channel.Members {
member := s.Catbox.Users[uid]
uidStr := string(uid)
// Send with ops and/or voice prefix.
if channel.userHasOps(member) {
uidStr = "@" + uidStr
}
// Assume the first may fit.
if len(uids) == 0 {
uids += uidStr
continue
}
// If we'll exceed the max protocol message length, fire the message and
// start a new list.
// +1 to account for a space.
if baseSize+len(uids)+1+len(uidStr) > irc.MaxLineLength {
sjoinMessage.Params[3] = uids
s.maybeQueueMessage(sjoinMessage)
uids = "" + uidStr
continue
}
// Add it to the list.
uids += " " + uidStr
}
if len(uids) > 0 {
sjoinMessage.Params[3] = uids
s.maybeQueueMessage(sjoinMessage)
}
// If they support the TB capab then send them TB commands. This tells them
// the topic for each channel.
if s.Server.hasCapability("TB") && len(channel.Topic) > 0 {
s.maybeQueueMessage(irc.Message{
Prefix: string(s.Catbox.Config.TS6SID),
Command: "TB",
Params: []string{
channel.Name,
fmt.Sprintf("%d", channel.TopicTS),
channel.TopicSetter,
channel.Topic,
},
})
}
}
}
// Part a user from a channel.
// This updates our records and informs our local users of the part.
// It does not send any messages to remote servers.
func (s *LocalServer) partUser(user *User, channel *Channel,
partMessage string) {
// Remove them from the channel.
channel.removeUser(user)
if len(channel.Members) == 0 {
delete(s.Catbox.Channels, channel.Name)
}
// Tell local users about the part.
params := []string{channel.Name}
if len(partMessage) > 0 {
params = append(params, partMessage)
}
msg := irc.Message{
Prefix: user.nickUhost(),
Command: "PART",
Params: params,
}
s.Catbox.messageLocalUsersOnChannel(channel, msg)
}
// The server sent us a message. Deal with it.
func (s *LocalServer) handleMessage(m irc.Message) {
// Record that client said something to us just now.
s.LastActivityTime = time.Now()
// Ensure we always have a prefix. It removes the need to check this
// elsewhere.
if len(m.Prefix) == 0 {
m.Prefix = string(s.Server.SID)
}
if m.Command == "PING" {
s.pingCommand(m)
return
}
if m.Command == "PONG" {
s.pongCommand(m)
return
}
if m.Command == "ERROR" {
s.errorCommand(m)
return
}
if m.Command == "UID" {
s.uidCommand(m)
return
}
if m.Command == "PRIVMSG" || m.Command == "NOTICE" {
s.privmsgCommand(m)
return
}
if m.Command == "SID" {
s.sidCommand(m)
return
}
if m.Command == "SJOIN" {
s.sjoinCommand(m)
return
}
if m.Command == "TB" {
s.tbCommand(m)
return
}
if m.Command == "JOIN" {
s.joinCommand(m)
return
}
if m.Command == "NICK" {
s.nickCommand(m)
return
}
if m.Command == "PART" {
s.partCommand(m)
return
}
// ircd-ratbox sends OPERWALL between servers, like WALLOPS
if m.Command == "WALLOPS" || m.Command == "OPERWALL" {
s.wallopsCommand(m)
return
}
if m.Command == "QUIT" {
s.quitCommand(m)
return
}
if m.Command == "MODE" {
s.modeCommand(m)
return
}
if m.Command == "TOPIC" {
s.topicCommand(m)
return
}
if m.Command == "SQUIT" {
s.squitCommand(m)
return
}
if m.Command == "KILL" {
s.killCommand(m)
return
}
if m.Command == "ENCAP" {
s.encapCommand(m)
return
}
if m.Command == "WHOIS" {
s.whoisCommand(m)
return
}
if isNumericCommand(m.Command) {
s.numericCommand(m)
return
}
if m.Command == "CLICONN" {
s.cliconnCommand(m)
return
}
if m.Command == "AWAY" {
s.awayCommand(m)
return
}
if m.Command == "INVITE" {
s.inviteCommand(m)
return
}
if m.Command == "TMODE" {
s.tmodeCommand(m)
return
}
// 421 ERR_UNKNOWNCOMMAND
s.messageFromServer("421", []string{m.Command, "Unknown command"})
}
// We expect a PING from server as part of burst end. It also happens
// periodically.
func (s *LocalServer) pingCommand(m irc.Message) {
// PING <origin name> [Destination SID]
if len(m.Params) < 1 {
// 461 ERR_NEEDMOREPARAMS
s.messageFromServer("461", []string{"PING", "Not enough parameters"})
return
}
// :9ZQ PING irc3.example.com :000
// Where irc3.example.com == 9ZQ and it is remote
// We want to send back
// :000 PONG irc.example.com :9ZQ
// I don't use origin name. Instead, look only at the prefix.
sourceSID := TS6SID(m.Prefix)
// Do we know the server making the ping request?
_, exists := s.Catbox.Servers[sourceSID]
if !exists {
// 402 ERR_NOSUCHSERVER
s.maybeQueueMessage(irc.Message{
Prefix: string(s.Catbox.Config.TS6SID),
Command: "402",
Params: []string{string(sourceSID), "No such server"},
})
return
}
// Who's the destination of the ping? Default to us if there is none set.
destinationSID := s.Catbox.Config.TS6SID
if len(m.Params) >= 2 {
destinationSID = TS6SID(m.Params[1])
}
// If it's for us, reply.
// If it's not for us, propagate it to where it should go.
if destinationSID == s.Catbox.Config.TS6SID {
s.maybeQueueMessage(irc.Message{
Prefix: string(s.Catbox.Config.TS6SID),
Command: "PONG",
Params: []string{s.Catbox.Config.ServerName, string(sourceSID)},
})
// If we're bursting, is it over? We expect to be PINGed at the end of their
// burst.
if s.Bursting && sourceSID == s.Server.SID {
s.GotPING = true
if s.GotPONG {
s.Bursting = false
s.Catbox.noticeOpers(fmt.Sprintf("Burst with %s over.", s.Server.Name))
}
}
return
}
// Propagate it to where it should go.
destServer, exists := s.Catbox.Servers[destinationSID]
if !exists {
// 402 ERR_NOSUCHSERVER
s.maybeQueueMessage(irc.Message{
Prefix: string(s.Catbox.Config.TS6SID),
Command: "402",
Params: []string{string(destinationSID), "No such server"},
})
return
}
if destServer.isLocal() {
destServer.LocalServer.maybeQueueMessage(m)
return
}
destServer.ClosestServer.maybeQueueMessage(m)
}
func (s *LocalServer) pongCommand(m irc.Message) {
// We expect this at end of server link burst.
// :<Remote SID> PONG <Remote server name> <My SID>
// However we can also get it afterwards and may need to propagate it.
if len(m.Params) < 2 {
// 461 ERR_NEEDMOREPARAMS
s.messageFromServer("461", []string{"PONG", "Not enough parameters"})
return
}
// Check the source of the PONG.
_, exists := s.Catbox.Servers[TS6SID(m.Prefix)]
if !exists {
s.quit("Unknown source server (PONG)")
return
}
// We don't need to look at the remote server name. It should be referring to
// the same server as the source SID.
// The destination for the PONG.
destinationSID := TS6SID(m.Params[1])
// If it's for us, just accept it. There's no need to reply.
// If it's for another server, propagate it on its way.
if destinationSID == s.Catbox.Config.TS6SID {
s.GotPONG = true
if s.Bursting && s.GotPING {
s.Catbox.noticeOpers(fmt.Sprintf("Burst with %s over.", s.Server.Name))
s.Bursting = false
}
return
}
// It's for a different server. Propagate it.
destinationServer, exists := s.Catbox.Servers[destinationSID]
if !exists {
s.quit("Unknown destination server (PONG)")
return
}
if destinationServer.isLocal() {
destinationServer.LocalServer.maybeQueueMessage(m)
return
}
destinationServer.ClosestServer.maybeQueueMessage(m)
}
func (s *LocalServer) errorCommand(m irc.Message) {
if len(m.Params) != 1 {
s.quit(fmt.Sprintf("ERROR from %s with invalid number of parameters: %d",
s.Server.Name, len(m.Params)))
return
}
s.quit(fmt.Sprintf("ERROR from %s: %s", s.Server.Name, m.Params[0]))
}
// UID command introduces a client. It is on the server that is the source.
func (s *LocalServer) uidCommand(m irc.Message) {
// Parameters: <nick> <hopcount> <nick TS> <umodes> <username> <hostname> <IP> <UID> :<real name>
// :8ZZ UID will 1 1475024621 +i will blashyrkh. 0 8ZZAAAAAB :will
if len(m.Params) != 9 {
s.quit("Invalid UID command - invalid parameter count")
return
}
if !isValidSID(m.Prefix) {
s.quit("Invalid SID")
return
}
sid := TS6SID(m.Prefix)
// Do we know the server the message originates on?
usersServer, exists := s.Catbox.Servers[sid]
if !exists {
s.quit(fmt.Sprintf("UID message from unknown server %s", sid))
return
}
if !isValidUID(m.Params[7]) {
s.quit("Invalid UID")
return
}
uid := TS6UID(m.Params[7])
if _, ok := s.Catbox.Users[uid]; ok {
s.quit(fmt.Sprintf("%s sent me UID for %s, but I already know it!",
s.Server.Name, uid))
return
}
nickTS, err := strconv.ParseInt(m.Params[2], 10, 64)
if err != nil {
s.quit("Invalid nick TS")
return
}
if !isValidNick(s.Catbox.Config.MaxNickLength, m.Params[0]) {
log.Printf("Invalid nick (%s)", m.Params[0])
s.quit(fmt.Sprintf("Invalid NICK! (%s)", m.Params[0]))
return
}
displayNick := m.Params[0]
username := m.Params[4]
if !isValidUser(username) {
s.quit("Invalid username")
return
}
// We could validate hostname
hostname := m.Params[5]
// Is there a nick collision? If there is, and we're colliding this user, then
// don't continue.
if !s.Catbox.handleCollision(s, uid, displayNick, username, hostname, nickTS,
"UID") {
return
}
hopCount, err := strconv.ParseInt(m.Params[1], 10, 8)
if err != nil {
s.quit("Invalid hop count")
return
}
// I get Nick TS above.
umodes := make(map[byte]struct{})
for i, umode := range m.Params[3] {
if i == 0 {
if umode != '+' {
s.quit("Malformed umode")
return
}
continue
}
if umode == 'i' || umode == 'o' || umode == 'C' {
umodes[byte(umode)] = struct{}{}
continue
}
}
// We could validate IP
ip := m.Params[6]
// I get UID ahead of time, above.
if !isValidRealName(m.Params[8]) {
s.quit("Invalid real name")
return
}
realName := m.Params[8]
// OK, the user looks good.
u := &User{
DisplayNick: displayNick,
HopCount: int(hopCount),
NickTS: nickTS,
Modes: umodes,
Username: username,
Hostname: hostname,
IP: ip,
UID: uid,
RealName: realName,
Channels: make(map[string]*Channel),
ClosestServer: s,
Server: usersServer,
}
if u.isOperator() {
s.Catbox.Opers[u.UID] = u
}
s.Catbox.Nicks[canonicalizeNick(displayNick)] = u.UID
s.Catbox.Users[u.UID] = u
// No reply needed I think.
// Tell our other servers.
// However, we need to alter the message a bit. The hop count is +1 for them.
// The message comes in saying the hop count to *us*. We need to tell our
// servers the hop count to them.
newMsg := m
newMsg.Params[1] = fmt.Sprintf("%d", hopCount+1)
for _, server := range s.Catbox.LocalServers {
if server == s {
continue
}
server.maybeQueueMessage(newMsg)
}
// Tell local operators.
if !s.Bursting {
for _, oper := range s.Catbox.Opers {
if !oper.isLocal() {
continue
}
_, exists := oper.Modes['C']
if !exists {
continue
}
oper.LocalUser.serverNotice(fmt.Sprintf("CLICONN %s %s %s %s %s (%s)",
u.DisplayNick, u.Username, u.Hostname, u.IP, u.RealName, u.Server.Name))
}
}
s.Catbox.updateCounters()
}
func (s *LocalServer) privmsgCommand(m irc.Message) {
// Parameters: <msgtarget> <text to be sent>
if len(m.Params) == 0 {
// 411 ERR_NORECIPIENT
s.messageFromServer("411", []string{"No recipient given (PRIVMSG)"})
return
}
if len(m.Params) == 1 {
// 412 ERR_NOTEXTTOSEND
s.messageFromServer("412", []string{"No text to send"})
return
}
// Determine the source.
// We can receive NOTICE from servers.
// Otherwise it must be a user.
source := ""
if m.Command == "NOTICE" {
sourceServer, exists := s.Catbox.Servers[TS6SID(m.Prefix)]
if exists {
source = sourceServer.Name
}
}
// If we don't know source yet, then it must be a user.
if source == "" {
sourceUser, exists := s.Catbox.Users[TS6UID(m.Prefix)]
if exists {
source = sourceUser.nickUhost()
}
}
if source == "" {
s.quit(fmt.Sprintf("Unknown source (%s)", m.Command))
}
// Is target a user?
if isValidUID(m.Params[0]) {
targetUID := TS6UID(m.Params[0])
targetUser, exists := s.Catbox.Users[targetUID]
if exists {
// We either deliver it to a local user, and done, or we need to propagate
// it to another server.
if targetUser.isLocal() {
// Source and target were UIDs. Translate to uhost and nick
// respectively.
m.Params[0] = targetUser.DisplayNick
targetUser.LocalUser.maybeQueueMessage(irc.Message{
Prefix: source,
Command: m.Command,
Params: m.Params,
})
} else {
// Propagate to the server we know the target user through.
targetUser.ClosestServer.maybeQueueMessage(m)
}
return
}
// Fall through. Treat it as a channel name.
}
// See if it's a channel.
channel, exists := s.Catbox.Channels[canonicalizeChannel(m.Params[0])]
if !exists {
log.Printf("PRIVMSG to unknown target %s", m.Params[0])
return
}
// Inform all members of the channel.
// Message local users directly.
// If a user is remote, then we record the server to send the message towards.
toServers := make(map[*LocalServer]struct{})
for memberUID := range channel.Members {
member := s.Catbox.Users[memberUID]
if member.isLocal() {
member.LocalUser.maybeQueueMessage(irc.Message{
Prefix: source,
Command: m.Command,
Params: m.Params,
})
continue
}
// Remote user. We need to propagate it towards them.
if member.ClosestServer != s {
toServers[member.ClosestServer] = struct{}{}
}
}
// Propagate message to any servers that need it.
for server := range toServers {
server.maybeQueueMessage(m)
}
}
// SID tells us about a new server.
func (s *LocalServer) sidCommand(m irc.Message) {
// Parameters: <server name> <hop count> <SID> <description>
// e.g.: :8ZZ SID irc3.example.com 2 9ZQ :My Desc
if len(m.Params) < 4 {
// 461 ERR_NEEDMOREPARAMS
s.messageFromServer("461", []string{"SID", "Not enough parameters"})
return
}
// Do I know this origin? (The server it's linked to)
linkedToServer, exists := s.Catbox.Servers[TS6SID(m.Prefix)]
if !exists {
s.quit(fmt.Sprintf("Unknown origin (SID) %s", m.Prefix))
return
}
name := m.Params[0]
hopCount, err := strconv.ParseInt(m.Params[1], 10, 8)
if err != nil {
s.quit(fmt.Sprintf("Invalid hop count: %s", err))
return
}
if !isValidSID(m.Params[2]) {
s.quit("Invalid SID")
return
}
sid := TS6SID(m.Params[2])
desc := m.Params[3]
// If we receive an SID for a server we're already linked with in some way,
// delink. This can happen if two servers try to link to the same server at
// the "same" time. For example, we might have linked with it, and a remote
// one did as well.
if newServer, ok := s.Catbox.Servers[sid]; ok {
s.quit(fmt.Sprintf(
"%s sent me SID about %s (which is linked to %s), but I already know it!",
s.Server.Name, newServer.Name, linkedToServer.Name))
return
}
if sid == s.Catbox.Config.TS6SID {
s.quit(fmt.Sprintf("%s sent me SID command with my own SID!", s.Server.Name))
return
}
newServer := &Server{
SID: sid,
Name: name,
Description: desc,
HopCount: int(hopCount),
ClosestServer: s,
LinkedTo: linkedToServer,
}
s.Catbox.Servers[sid] = newServer
// Propagate to our connected servers.
// However, we need to alter the message a bit. The hop count is +1 for them.
// The message comes in saying the hop count to *us*. We need to tell our
// servers the hop count to them.
newMsg := m
newMsg.Params[1] = fmt.Sprintf("%d", hopCount+1)
for _, server := range s.Catbox.LocalServers {
// Don't tell the server we just heard it from.
if server == s {
continue
}
server.maybeQueueMessage(newMsg)
}
// We don't need to tell the new server about the servers we are connected to.
// They'll be informed by the server they linked to about us.
s.Catbox.noticeLocalOpers(fmt.Sprintf("%s is introducing server %s",
s.Server.Name, newServer.Name))
}
// SJOIN occurs in two contexts:
// 1. During bursts to inform us of channels and users in the channels.