-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tcl
executable file
·1299 lines (1203 loc) · 42.4 KB
/
main.tcl
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
#!/usr/bin/env tclsh8.6
# TCL Chatd
# Inspired by RONSOR's chat daemon
# Obviously we require tcllib.
package require md5
package require tls
package require sqlite3
package require dns
set opmodes "qaohv"
if {[catch {dict get {}} zun]} {source dict.tcl}
sqlite3 ircdb ./irc.db
ircdb eval {CREATE TABLE IF NOT EXISTS logins (uname text, pass text)}
array set dispnames {}
array set idents {}
array set rhostnames {}
array set hostnames {}
array set realnames {}
array set conpass {}
array set bans {}
array set rooms {}
array set servers {}
array set aways {}
array set servnick {}
array set modes {}
array set extinfo {}
array set pings {}
namespace eval config {
array set me {}
array set listen {}
array set servers {}
array set services {}
}
source ircd.conf
#source s2s.tcl
set prefixls ""
append prefixls [set opprefix(q) "$::config::me(qprefix)"]
append prefixls [set opprefix(a) "$::config::me(aprefix)"]
append prefixls [set opprefix(o) "$::config::me(oprefix)"]
append prefixls [set opprefix(h) "$::config::me(hprefix)"]
append prefixls [set opprefix(v) "$::config::me(vprefix)"]
proc login {fd user pass} {
set pq [::md5::md5 -hex $pass]
set zeb [ircdb eval {SELECT }]
}
proc getfdbynick {nick} {
foreach {fdp nic} [array get ::dispnames] {
if {[string tolower $nick] == [string tolower $nic]} {
return $fdp
}
}
return
}
proc getnickbyfd {nick} {
foreach {fdp nic} [array get ::dispnames] {
if {$nick == $fdp} {
return $nic
}
}
}
proc rand {minn maxx} {
set maxnum [expr {$maxx - $minn}]
set fp [open /dev/urandom r]
set bytes [read $fp 6]
close $fp
scan $bytes %c%c%c%c%c%c ca co ce cu ci ch
set co [expr {$co + pow(2,8)}]
set ce [expr {$ce + pow(2,16)}]
set cu [expr {$cu + pow(2,24)}]
set ci [expr {$ci + pow(2,32)}]
set ch [expr {$ch + pow(2,40)}]
return [expr {$minn+(int($ca+$co+$ce+$cu+$ci+$ch)%$maxnum)}]
}
proc makessl {fd} {
global modes
append modes($fd) "Z"
}
proc accept {chan addr port} {
global hostnames dispnames idents realnames modes rhostnames aways extinfo
dict set extinfo($chan) exists 1
set modes($chan) ""
set idents($chan) ""
set dispnames($chan) ""
set realnames($chan) ""
set rhostnames($chan) "$addr"
set aways($chan) ""
set ctr 0
set xddr [string match "*:*" $addr]
if {$xddr} {
set wddr ":"
set dnslen 1
} {
set dnslen 0
set wddr "."
}
set yddr [split $addr "$wddr"]
set zddr ""
if {$xddr} {foreach {x y} $yddr {
set w "$x"
append w "$y"
set srl [string length $w]
set newad [string range [::md5::md5 -hex $w] 0 11]
append zddr "$newad"
append zddr $wddr
} } { foreach {w} $yddr {
set srl [string length w]
set newad [string range [::md5::md5 -hex $w] 0 11]
append zddr "$newad"
append zddr $wddr
} }
append modes($chan) "x"
append zddr "IP"
set hostnames($chan) $zddr
fconfigure $chan -buffering line -blocking 0
putss $chan ":$::config::me(server) 020 * :$::config::me(welcome)"
fileevent $chan readable [list client'unreg $chan $addr]
}
proc gethostname {chan} {
if {[string match "*x*" $::modes($chan)]} {
return $::hostnames($chan)
} {
return $::rhostnames($chan)
}
}
proc accept-ssl {chan addr port} {
makessl $chan
global hostnames dispnames idents realnames modes rhostnames aways
set modes($chan) ""
set idents($chan) ""
set dispnames($chan) ""
set realnames($chan) ""
set rhostnames($chan) "$addr"
set aways($chan) ""
set ctr 0
set xddr [string match "*:*" $addr]
if {$xddr} {set wddr ":"} {set wddr "."}
set yddr [split $addr "$wddr"]
set zddr ""
if {$xddr} {foreach {x y} $yddr {
set w "$x"
append w "$y"
set srl [string length $w]
set newad [string range [::md5::md5 -hex $w] 0 11]
append zddr "$newad"
append zddr $wddr
} } { foreach {w} $yddr {
set srl [string length w]
set newad [string range [::md5::md5 -hex $w] 0 11]
append zddr "$newad"
append zddr $wddr
} }
append zddr "IP"
set hostnames($chan) $zddr
fconfigure $chan -buffering line -blocking 1
::tls::handshake $chan
fconfigure $chan -buffering line -blocking 0
append modes($chan) "x"
putss $chan ":$::config::me(server) 020 * :$::config::me(welcome)"
fileevent $chan readable [list client'unreg $chan $addr]
}
proc checknickname {nick} {
set good "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ[]{}~/\|-_^()"
set fishy "~-"
if {[string is digit [string index $nick 0]]} {
return 0
}
foreach {char} [split $fishy {}] {
if {[string match "${char}*" $nick]} {return 0}
}
foreach {letter} [split $nick {}] {
foreach {goodchar} [split $good {}] {
if {$letter == $goodchar} {
set isgood 1
} {
set isgood 0
}
}
if {$isgood} {} {return 1}
}
return 1
}
proc chgnick {chan nick} {
if {![checknickname $nick]} {
message'fd $chan $::config::me(server) [list "433" "$::dispnames($chan)" "$nick" "The nickname you have chosen is erroneous. Pick another."]
return
}
if {[getfdbynick $nick] != ""} {
message'fd $chan $::config::me(server) [list "433" "$::dispnames($chan)" "$nick" "The nickname you have chosen is already in use. Pick another."]
} {
sendtoneigh $chan "$::dispnames($chan)!$::idents($chan)@[gethostname $chan]" [list "NICK" "$nick"]
set ::dispnames($chan) $nick
}
}
proc sendtoneigh {chan src zarg} {
global rooms
set sendto [list $chan]
message'fd $chan $src $zarg
foreach {room} [array names rooms] {
if {[lsearch -exact $rooms($room) "$chan"] == -1} {continue}
foreach {niq} $rooms($room) {
if {[lsearch -exact $sendto "$niq"] != -1} {continue}
message'fd $niq $src $zarg
lappend $sendto $niq
}
}
putss stdout "[join $sendto "->"]"
}
proc sendtoneighchan {chan src zarg} {
global rooms
set sendto [list]
#message'fd $chan $src $zarg
foreach {room} [array names rooms] {
if {[lsearch -exact $rooms($room) "$chan"] == -1} {continue}
if {[string match "list,*" $room]} {continue}
lappend sendto $room
sendtochan $chan $room $src [lreplace $zarg [lsearch -exact $zarg "%chan"] [lsearch -exact $zarg "%chan"] $room]
}
putss stdout "[join $sendto "->"]"
}
proc retneighchan {chan} {
global rooms
set sendto [list]
#message'fd $chan $src $zarg
foreach {room} [array names rooms] {
if {[lsearch -exact $rooms($room) "$chan"] == -1} {continue}
if {[string match "list,*" $room]} {continue}
append sendto " $room"
}
return $sendto
}
proc sendtoneighbut {chan src zarg} {
global rooms
set sendto [list]
#message'fd $chan $src $zarg
foreach {room} [array names rooms] {
if {[lsearch -exact $rooms($room) "$chan"] == -1} {continue}
foreach {niq} $rooms($room) {
if {[lsearch -exact $sendto "$niq"] != -1} {continue}
if {$niq == $chan} {continue}
message'fd $niq $src $zarg
lappend $sendto $niq
}
}
putss stdout "[join $sendto "->"]"
}
proc pingout {chan} {
global pings
if {![info exists ::hostnames($chan)]} {
return
}
if {!$pings($chan)} {
client'err $chan "$::hostnames($chan)" "$::dispnames($chan)" "Ping timeout: 80 seconds"
}
putss $chan "PING $::config::me(server)"
set pings($chan) 0
after 80000 pingout $chan
}
proc client'connfooter {chan nick ident addr} {
putss stdout "Connection footers called for $nick $ident $::rhostnames($chan)"
global hostnames dispnames idents servnick pings
if {""==$nick} {
fileevent $chan readable [list client'unreg $chan $addr]
return
}
set pings($chan) 1
pingout $chan
set can "$chan"
set dispnames($can) $nick
set idents($can) $nick
if {$ident != ""} {set idents($can) $ident}
set hostnames($can) $addr
message'fd $chan $::config::me(server) [list "001" "$nick" "Welcome to the Internet Relay Network."]
message'fd $chan $::config::me(server) [list "002" "$nick" "Your host is $::config::me(server), running version tclchatd0.1 IRC protocol version 2.7"]
message'fd $chan $::config::me(server) [list "003" "$nick" "We never started. :P"]
message'fd $chan $::config::me(server) [list "004" "$nick" "$::config::me(server) tclchatd0.1 ZSaoisw bimnlsptkoOHVheIz beIklohvOHVz"]
message'fd $chan $::config::me(server) [list "005" "$nick" "PREFIX=($::opmodes)$::prefixls" "CHANMODES=QAzbeIOHV,k,l,imnspt" "CHANTYPES=#&!+" "IDCHAN=!:5" "FNC" "are supported by this server"]
set lusers [expr {int([array size ::dispnames]/2)+1}]
message'fd $chan $::config::me(server) [list "251" "$nick" "$lusers" "0" "There are $lusers users and 0 invisible on 1 server"]
message'fd $chan $::config::me(server) [list "375" "$nick" "Begin MOTD"]
foreach {lotd} $::config::me(motd) {
message'fd $chan $::config::me(server) [list "372" "$nick" "- $lotd"]
}
message'fd $chan $::config::me(server) [list "376" "$nick" "End of MOTD for $dispnames($chan)!$idents($chan)@[gethostname $chan]"]
fileevent $chan readable ""
fileevent $chan readable [list client'reg $chan $addr]
#server'introducecli $chan $::config::me(server)
sendtoallumode o $::config::me(server) [list "NOTICE" "*" "CONN $dispnames($chan)!$idents($chan)@[gethostname $chan] $::rhostnames($chan)"]
}
proc client'srvconnfooter {srv chan nick ident addr} {
global hostnames dispnames idents servnick
if {""==$nick} {
fileevent $chan readable [list client'unreg $chan $addr]
return
}
set can "$chan"
set dispnames($can) $nick
set idents($can) $nick
if {$ident != ""} {set idents($can) $ident}
set hostnames($can) $addr
#server'introducecli $chan $::config::me(server)
putss stdout "Connection footers called for $nick $ident $::rhostnames($chan) on $srv"
sendtoallumode o $::config::me(server) [list "NOTICE" "*" "CONN $dispnames($chan)!$idents($chan)@[gethostname $chan] $::rhostnames($chan)"]
}
proc gset {k v} {
uplevel "1" "set $k $v"
}
proc getss {chan} {
return [string trim [gets $chan]]
}
proc client'unreg {chann addr} {
global idents realnames conpass dispnames servers hostnames modes
set msg [message'parse [getss $chann]]
set chan "$chann"
switch [string tolower [lindex $msg 0]] {
"user" {
if {[checknickname [lindex $msg 1]]} {set idents($chan) [lindex $msg 1]} {
message'fd $chann $::config::me(server) [list "433" "*" [lindex $msg 1] "The ident you have chosen is erroneous. Pick another."]
}
set idents($chan) [lindex $msg 1]
set realnames($chan) [lindex $msg 4]
if {[info exists dispnames($chan)]} {
client'connfooter $chann $dispnames($chan) [lindex $msg 1] $::hostnames($chan)
fileevent $chann readable [list client'reg $chan $addr]
}
}
"pass" {
set conpass($chan) [join [lrange $msg 1 end] " "]
if {[lindex $msg 1] == "SERVICE"} {
set approved 0
foreach {k v} [array get ::config::services] {
set pass [dict get $v pass]
set host [dict get $v pass]
set canchan 0
set canchan [dict get $v canchan]
if {
([lindex $msg 2] == $k)
&& ([lindex $msg 3] == $pass)
&& ($addr == $host)
} {
set approved 1
set hostnames($chan) [dict get $v spoof]
set idents($chan) [dict get $v identspoof]
if {$canchan} {append modes($chan) "S"}
append modes($chan) "o"
}
}
if {$approved} {
message'fd $chan "$::config::me(server)" [list "NOTICE" "*" "You are now considered a Service. Please do not abuse your capabilities."]
}
}
}
"nick" {
global idents realnames conpass dispnames
set go 1
client`unreg`nick $chan $msg
}
}
}
proc client`randnick {chan} {
global dispnames
set rn [rand 100000 999999]
if {[getfdbynick "U$rn"] != ""} {
client`randnick $chan
} {
client'connfooter $chan "U$rn" $::idents($chan) $::hostnames($chan)
}
}
proc client`unreg`nick {chan msg} {
global dispnames idents
if {[getfdbynick [lindex $msg 1]] != ""} {
message'fd $chan $::config::me(server) [list "433" "*" [lindex $msg 1] "The nickname you have chosen is already in use. Forcing nick change to a random nick."]
#return
client`randnick $chan
}
if {[lindex $msg 1] == ""} {
message'fd $chan $::config::me(server) [list "433" "*" [lindex $msg 1] "The nickname you have chosen is already in use. Pick another."]
return
}
if {[getfdbynick [lindex $msg 1]] == ""} {
if {[checknickname [lindex $msg 1]]} {
set dispnames($chan) [lindex $msg 1]
client'connfooter $chan [lindex $msg 1] $idents($chan) $::hostnames($chan)
} {
message'fd $chan $::config::me(server) [list "433" "*" [lindex $msg 1] "The nickname you have chosen is erroneous. Pick another."]
set go 0
}
}
}
proc chan'names {chan room} {
putss stdout "Names called $chan $room"
foreach {nq} $::rooms($room) {
set name [getnickbyfd $nq]
set prefix ""
#append namreply [getnickbyfd $nq]
if {[info exists ::rooms(list,$room,v)]} { foreach {op} $::rooms(list,$room,v) {
if {$op == $nq} {set prefix $::opprefix(v)}
} }
if {[info exists ::rooms(list,$room,h)]} { foreach {op} $::rooms(list,$room,h) {
if {$op == $nq} {set prefix $::opprefix(h)}
} }
if {[info exists ::rooms(list,$room,o)]} { foreach {op} $::rooms(list,$room,o) {
if {$op == $nq} {set prefix $::opprefix(o)}
putss stdout "$prefix $nq"
} }
if {[info exists ::rooms(list,$room,a)]} { foreach {op} $::rooms(list,$room,a) {
if {$op == $nq} {set prefix $::opprefix(a)}
} }
if {[info exists ::rooms(list,$room,q)]} { foreach {op} $::rooms(list,$room,q) {
if {$op == $nq} {set prefix $::opprefix(q)}
} }
append namreply "$prefix"
append namreply "$name "
if {[string length $namreply] >= 372} {
message'fd $chan $::config::me(server) [list "353" [getnickbyfd $chan] "=" $room $namreply]
}
}
if {[string length $namreply] != 0} {message'fd $chan $::config::me(server) [list "353" [getnickbyfd $chan] "@" $room $namreply]}
message'fd $chan $::config::me(server) [list "366" [getnickbyfd $chan] $room "End of /NAMES reply."]
}
proc hasmode {room mode} {
foreach {mchar} [split [chan'retmode $room] {}] {
if {$mchar == $mode} {return 1}
}
return 0
}
proc chan'adduser {fd room {apass ""}} {
global rooms
set canjoin 1
set banned 0
set newchan 0
set mods ""
set chan $fd
if {![hasmode $room "r"]} {if {[info exists rooms($room)]} {if {[llength $rooms($room)] == 0} {
set rooms($room) [list]
set rooms(mode,$room) "nt"
set canjoin 2
set newchan 1
} } {
set rooms($room) [list]
set rooms(mode,$room) "nt"
set canjoin 2
set newchan 1
} }
if {[hasmode $room "i"]} {set canjoin 0}
if {$canjoin != 2} {
if {[info exists ::rooms(list,$room,b)]} {
foreach {banmask} $::rooms(list,$room,b) {
if {[string match -nocase $banmask "$::dispnames($fd)!$::idents($fd)@$::hostnames($fd)"]} {
set canjoin 0
set banned 1
}
} }
if {[info exists ::rooms(list,$room,e)]} {
foreach {banmask} $::rooms(list,$room,e) {
if {[string match -nocase $banmask "$::dispnames($fd)!$::idents($fd)@$::hostnames($fd)"]} {
set canjoin 1
}
} }
if {[info exists ::rooms(list,$room,I)]} {
foreach {banmask} $::rooms(list,$room,I) {
if {[string match -nocase $banmask "$::dispnames($fd)!$::idents($fd)@$::hostnames($fd)"]} {
if {[hasmode $room "i"] && !$banned} {set canjoin 1}
}
} }
if {$apass == ""} {set zzzzzzzzzzzzzzzzzzzzzzz z} {
if {[info exists ::rooms(list,$room,Q)]} {
foreach {banmask} $::rooms(list,$room,Q) {
if {$banmask == $apass} {
if {!$banned} {set canjoin 1
lappend rooms(list,$room,q) "$fd"
append mods q}
}
} }
if {[info exists ::rooms(list,$room,A)]} {
foreach {banmask} $::rooms(list,$room,A) {
if {$banmask == $apass} {
if {!$banned} {set canjoin 1
lappend rooms(list,$room,a) "$fd"
append mods a}
}
} }
if {[info exists ::rooms(list,$room,O)]} {
foreach {banmask} $::rooms(list,$room,O) {
if {$banmask == $apass} {
if {!$banned} {set canjoin 1
lappend rooms(list,$room,o) "$fd"
append mods o}
}
} }
if {[info exists ::rooms(list,$room,H)]} {
foreach {banmask} $::rooms(list,$room,H) {
if {$banmask == $apass} {
if {!$banned} {set canjoin 1;lappend rooms(list,$room,h) "$fd";append mods h}
}
} }
if {[info exists ::rooms(list,$room,V)]} {
foreach {banmask} $::rooms(list,$room,V) {
if {$banmask == $apass} {
if {!$banned} {set canjoin 1;lappend rooms(list,$room,v) "$fd";append mods v}
}
} }
}
}
if {$canjoin == 0} {
message\'fd $fd "-|-!-@$::config::me(server)" [list "474" [getnickbyfd $fd] $room "You cannot join $room (Banned: $banned, Modes: [chan'retmode $room]) "]
return
}
if {[lsearch -exact rooms($room) $fd] != -1} {
return
# User is already on channel.
}
if {$newchan} {
putss stdout "Channel created or apassed $room"
#set rooms($room) [list $fd]
set rooms(list,$room,q) [list $fd]
set rooms(list,$room,a) [list]
set rooms(list,$room,o) [list $fd]
set rooms(list,$room,h) [list]
set rooms(list,$room,v) [list]
}
lappend rooms($room) $fd
sendtochannoc $fd $room "$::dispnames($chan)!$::idents($chan)@[gethostname $chan]" [list "JOIN" $room]
message\'fdnoc $fd "$::dispnames($chan)!$::idents($chan)@[gethostname $chan]" [list "JOIN" $room]
set modus [list "MODE" $room "+$mods"]
for {set x 0} {$x<[string length $mods]} {incr x} {lappend modus [getnickbyfd $fd]}
if {""!=$mods} {sendtochannoc $fd $room "$::dispnames($chan)!$::idents($chan)@[gethostname $chan]" $modus}
chan'names $fd $room
chan'topic $fd $room -
}
proc chan'ls {src} {
foreach {room lusers} [array get ::rooms] {
if {[hasmode $room "s"]} {continue}
if {[string match "*,*" $room]} {continue}
if {[info exists rooms(topic,$room)]} {set m 332;set t $rooms(topic,$room)} {set m 331;set t " "}
message'fd $src "$::config::me(server)" [list "322" [getnickbyfd $src] $room [llength $lusers] $t]
}
message'fd $src "$::config::me(server)" [list "323" [getnickbyfd $src] "End of /LIST reply."]
}
proc chan'canchgmode {src room state modeletter {param ""}} {
if {[llength $::rooms($room)] == 0} {
return 1
}
if {[string match "*S*" $::modes($src)]} {
return 1
}
if {[info exists ::rooms(list,$room,q)]} {
foreach {surce} $::rooms(list,$room,q) {
if {$src == $surce} {return 1}
}
}
if {[info exists ::rooms(list,$room,a)]} {
foreach {surce} $::rooms(list,$room,a) {
if {$src == $surce} {
if {$modeletter !="q" && $modeletter !="Q"} {
return 1
}
}
}
}
if {[info exists ::rooms(list,$room,o)]} {
foreach {surce} $::rooms(list,$room,o) {
if {$src == $surce} {
if {$modeletter !="q" && $modeletter !="a" && $modeletter !="Q" && $modeletter !="A" } {
return 1
}
}
}
}
if {[info exists ::rooms(list,$room,h)]} {
foreach {surce} $::rooms(list,$room,h) {
if {$src == $surce} {
if {$modeletter !="o" && $modeletter !="O"} {
return 1
}
}
}
}
return 0
}
proc whois {fd nick {xtra "0"}} {
if {!([getfdbynick $nick] == "")} {
set fini [getfdbynick $nick]
message'fd $fd "$::config::me(server)" [list "311" [getnickbyfd $fd] "$nick" "$::idents($fini)" "$::hostnames($fini)" "*" "$::realnames($fini)"]
#message'fd $fd "$::config::me(server)" [list "319" [getnickbyfd $fd] "$nick" "Cannot show due to TCL error. :P"]
if {[info exists ::extinfo($fini)]} {
foreach {k v} $::extinfo($fini) {
if {$k=="umetadata"} {
foreach {x w} $v {message'fd $fd "$::config::me(server)" [list "309" [getnickbyfd $fd] "$nick" "Metadata: $x = $w"]}
}
if {$k=="account"} {
message'fd $fd "$::config::me(server)" [list "330" [getnickbyfd $fd] "$nick" "$v" "is authed as"]
}
}
}
if {$xtra} {message'fd $fd "$::config::me(server)" [list "378" [getnickbyfd $fd] "$nick" "is connecting from *@$::rhostnames($fini) $::rhostnames($fini)"]}
message'fd $fd "$::config::me(server)" [list "318" [getnickbyfd $fd] "$nick" "End of WHOIS list."]
}
}
proc slappend {k v} {
if {[info exists $k]} {uplevel "1" lappend $k $v} {uplevel "1" set $k [list $v]}
}
proc sappend {k v} {
if {[info exists $k]} {uplevel "1" append $k $v} {uplevel "1" set $k $v}
}
proc chan'topic {src room {topic "-"}} {
global rooms
if {"-"==$topic} {
if {[info exists rooms(topic,$room)]} {set m 332;set t $rooms(topic,$room)} {set m 331;set t "No topic is set."}
message'fd $src "$::config::me(server)" [list $m [getnickbyfd $src] $room $t]
} {
if {[hasmode $room t]} {
set cantopic [chan'canchgmode $src $room + "t" ""]
} {
if {![chan'canchgmode $src $room + "t" ""] && ([is_maskmode $room b $src] || [is_maskmode $room z $src])} { set cantopic 0 } { set cantopic 1 }
putss stdout "if this is executed, wrang."
}
if {$cantopic} {
set rooms(topic,$room) $topic
sendtochan $src $room "$::dispnames($src)!$::idents($src)@$::hostnames($src)" [list "TOPIC" $room $topic]
message'fd $src "$::dispnames($src)!$::idents($src)@$::hostnames($src)" [list "TOPIC" $room $topic]
}
}
}
proc chan'chgmode {src room state modeletter param} {
global rooms
set lst 0
set oponly 0
switch $modeletter {
q {set lst 728}
a {set lst 728}
o {set lst 728}
h {set lst 728}
v {set lst 728}
b {set lst 367}
e {set lst 346}
I {set lst 348}
z {set lst 367}
Q {set lst 1728;set oponly 1}
A {set lst 1728;set oponly 1}
O {set lst 1728;set oponly 1}
H {set lst 1728;set oponly 1}
V {set lst 1728;set oponly 1}
}
if {$lst != 0 && ""==$param} {
if {![info exists ::rooms(list,$room,$modeletter)]} {set ::rooms(list,$room,$modeletter) [list]}
foreach {prm} $::rooms(list,$room,$modeletter) {
if {$lst == 728 || ($lst == 1728) && (!$oponly || [chan'canchgmode $src $room $state $modeletter $param])} {
if {$lst == 1728} {set lst 728} {set prm [getnickbyfd $prm]}
if {$oponly} {break}
message'fd $src "$::config::me(server)" [list "$lst" [getnickbyfd $src] "$room" $modeletter $prm]
} {
message'fd $src "$::config::me(server)" [list "$lst" [getnickbyfd $src] "$room" $prm]
}
}
if {$lst > 500} {
if {$lst > 1000} {set lst [expr {$lst - 1000}]}
message'fd $src "$::config::me(server)" [list "[expr {$lst+1}]" [getnickbyfd $src] "$room" $modeletter "End of +$modeletter list."]
} {
message'fd $src "$::config::me(server)" [list "[expr {$lst+1}]" [getnickbyfd $src] "$room" "End of +$modeletter list."]
}
return
}
if {[chan'canchgmode $src $room $state $modeletter $param]} {
if {$lst > 100 && [string length $param] && ($state == "+")} {
if {$lst == 728} {set parm [getfdbynick $param]} {set parm $param}
lappend rooms(list,$room,$modeletter) $parm
}
if {$lst > 100 && [string length $param] && ($state == "-")} {
if {$lst > 500} {set parm [getfdbynick $param]} {set parm $param}
set rooms(list,$room,$modeletter) [lreplace $rooms(list,$room,$modeletter) [lsearch -exact $rooms(list,$room,$modeletter) $parm] [lsearch -exact $rooms(list,$room,$modeletter) $parm]]
}
if {!$lst && ($state == "+")} {append rooms(mode,$room) $modeletter}
if {!$lst && ($state == "-")} {set rooms(mode,$room) [string map [list $modeletter ""] $rooms(mode,$room)]}
set mch $state
append mch $modeletter
if {$oponly} {
sendtochannoc $src "list,$room,q" "$::dispnames($src)!$::idents($src)@$::hostnames($src)" [list "MODE" $room $mch $param]
sendtochannoc $src $room "$::dispnames($src)!$::idents($src)@$::hostnames($src)" [list "MODE" $room $mch "*"]
} {
sendtochannoc $src $room "$::dispnames($src)!$::idents($src)@$::hostnames($src)" [list "MODE" $room $mch $param]
}
message'fdnoc $src "$::dispnames($src)!$::idents($src)@$::hostnames($src)" [list "MODE" $room $mch $param]
}
}
proc sendtoallumode {mode src zarg} {
foreach {k v} [array get ::modes] {
if {![string match "*${mode}*" $v]} {continue}
message'fd $k $src $zarg
}
}
proc chan'retmode {room} {
if {[info exists ::rooms(mode,$room)]} {
return $::rooms(mode,$room)
}
return ""
}
proc client'reg {chan addr} {
set msg [message'parse [getss $chan]]
client'rreg $chan $msg
}
proc sendtobutserv {but msg} {
}
proc client'rreg {chan msg} {
global rooms modes aways pings extinfo
if {![string match "*L*" $modes($chan)]} {
switch [string tolower [lindex $msg 0]] {
"privmsg" {
if {[getfdbynick [lindex $msg 1]] != ""} {
message'send [lindex $msg 1] "$::dispnames($chan)!$::idents($chan)@[gethostname $chan]" [list "PRIVMSG" [lindex $msg 1] [lindex $msg 2]]
if {$aways([getfdbynick [lindex $msg 1]])!=""} {
message'fd $chan $::config::me(server) [list "301" $::dispnames($chan) [lindex $msg 1] $aways([getfdbynick [lindex $msg 1]])
}
} {
foreach {chn} [split [lindex $msg 1] ","] {
sendtochan $chan $chn "$::dispnames($chan)!$::idents($chan)@[gethostname $chan]" [list "PRIVMSG" $chn [lindex $msg 2]]
}
}
}
"pong" {
set pings($chan) 1
}
"away" {
set aways($chan) [lindex $msg 1]
}
"notice" {
if {[getfdbynick [lindex $msg 1]] != ""} {
message'send [lindex $msg 1] "$::dispnames($chan)!$::idents($chan)@[gethostname $chan]" [list "NOTICE" [lindex $msg 1] [lindex $msg 2]]
if {$aways([getfdbynick [lindex $msg 1]])!=""} {
message'fd $chan $::config::me(server) [list "301" $::dispnames($chan) [lindex $msg 1] $aways([getfdbynick [lindex $msg 1]])
}
} {
foreach {chn} [split [lindex $msg 1] ","] {
sendtochan $chan $chn "$::dispnames($chan)!$::idents($chan)@[gethostname $chan]" [list "NOTICE" $chn [lindex $msg 2]]
}
}
}
"names" {
if {[lindex $msg 1] != ""} {chan'names $chan [lindex $msg 1]}
}
"ping" {
message'fd $chan $::config::me(server) [list "PONG" $::config::me(server) [lindex $msg 1]]
}
"whois" {
if {[lindex $msg 1] != ""} {whois $chan [lindex $msg 1] [string match "*o*" $::modes($chan)]}
}
"list" {
chan'ls $chan
}
"who" {
if {[set niq [getfdbynick [lindex $msg 1]]] != ""} {
if {$::aways($niq) == ""} {
set hg "H"
} {
set hg "G"
}
message'fd $chan $::config::me(server) [list "352" "*" [lindex $msg 1] $::idents($niq) $::hostnames($niq) $::config::me(server) $::dispnames($niq) $hg "1 $::realnames($niq)"]
} {
foreach {niq} $rooms([lindex $msg 1]) {
if {$::aways($niq) == ""} {
set hg "H"
} {
set hg "G"
}
message'fd $chan $::config::me(server) [list "352" "*" [lindex $msg 1] $::idents($niq) $::hostnames($niq) $::config::me(server) $::dispnames($niq) $hg "1 $::realnames($niq)"]
}
}
message'fd $chan $::config::me(server) [list "315" [lindex $msg 1] "End of /WHO"]
}
"cloakme" {
global modes
sendtoneighbut $chan "$::dispnames($chan)!$::idents($chan)@[gethostname $chan]" [list "QUIT" "/CLOAKME used; usermode +x set; cycling"]
append modes($chan) "x"
sendtoneighchan $chan "$::dispnames($chan)!$::idents($chan)@[gethostname $chan]" [list "JOIN" "%chan"]
}
"decloakme" {
global modes
sendtoneighbut $chan "$::dispnames($chan)!$::idents($chan)@[gethostname $chan]" [list "QUIT" "/DECLOAKME used; usermode -x set; cycling"]
set modes($chan) [string map {x {}} modes($chan)]
sendtoneighchan $chan "$::dispnames($chan)!$::idents($chan)@[gethostname $chan]" [list "JOIN" "%chan"]
}
"mode" {
foreach {chn} [split [lindex $msg 1] ","] {
set okchan 0
switch [string index $chn 0] {
"#" {set okchan 1}
"&" {set okchan 1}
"!" {set okchan 1}
"+" {set okchan 1}
}
if {$okchan && [lindex $msg 2]!="" && [lindex $msg 2]!=":" && [lindex $msg 2]!=" " } {
set state +
set ctr 0
foreach {l} [split [lindex $msg 2] {}] {
if {$l == "+"} {set state +; continue}
if {$l == "-"} {set state -; continue}
switch -regexp $l {
"[qaQAzohvbeIOHV]" {chan'chgmode $chan $chn $state $l [lindex $msg [expr {2+[incr ctr]}]]}
"[imnsptrSCN]" {chan'chgmode $chan $chn $state $l ""}
}
}
} {
message'fdnoc $chan $::config::me(server) [list "324" "*" $chn "+[chan'retmode $chn]"]
}
}
}
"topic" {
if {""==[lindex $msg 1]} continue
foreach {chn} [split [lindex $msg 1] ","] {
set okchan 0
switch [string index $chn 0] {
"#" {set okchan 1}
"&" {set okchan 1}
"!" {set okchan 1;set $chn "!AAAAA[string range $chn 1 end]"}
"+" {set okchan 1}
}
if {$okchan} {
chan'topic $chan $chn [lindex $msg 2]
}
}
}
"join" {
foreach {chn} [split [lindex $msg 1] ","] {apass} [split [lindex $msg 2] ","] {
set okchan 0
switch [string index $chn 0] {
"#" {set okchan 1}
"&" {set okchan 1}
"!" {set okchan 1;set $chn "!AAAAA[string range $chn 1 end]"}
"+" {set okchan 1}
}
if {$okchan} {
if {![info exists apass]} {set apass ""}
chan'adduser $chan $chn $apass
}
}
}
"oper" {
set approved 0
foreach {k v} [array get ::config::services] {
set pass [dict get $v pass]
set host [dict get $v host]
if {
([lindex $msg 1] == $k)
&& ([lindex $msg 2] == $pass)
&& ($::rhostnames($chan) == $host)
} {
set approved 1
set hostnames($chan) [dict get $v spoof]
set idents($chan) [dict get $v identspoof]
append modes($chan) "S"
append modes($chan) "o"
}
}
if {$approved} {
message'fd $chan "$::config::me(server)" [list "NOTICE" $::dispnames($chan) "You are now considered an Oper. Please do not abuse your capabilities."]
}
}
"server" {
set approved 0
foreach {k v} [array get ::config::servers] {
set pass [dict get $v pass]
set host [dict get $v host]
if {
([lindex $msg 1] == $k)
&& ([lindex $msg 2] == $pass)
&& ($::rhostnames($chan) == $host)
} {
set approved 1
set dispnames($chan) "$k"
append modes($chan) "L"
}
}
if {$approved} {
global dispnames
message'fd $chan "$::config::me(server)" [list "NOTICE" $::dispnames($chan) "You are now considered a Server. If you are using an IRC client, please reconnect as this is not correct behaviour."]
}
}
"nick" {
chgnick $chan [lindex $msg 1]
}
"umetadata" {
set k [lindex $msg 1]
set v [lindex $msg 2]
if {$v==""} {catch {dict unset extinfo($chan) umetadata $k} zuzu} {dict set extinfo($chan) umetadata $k $v}
message'fd $chan "$::config::me(server)" [list "NOTICE" $::dispnames($chan) "New umetadata line: $k = $v"]
}
"quit" {
client'err $chan "$::idents($chan)@[gethostname $chan]" "$::dispnames($chan)" "Quit: [lindex $msg 1]"
}
"part" {
foreach {chn} [split [lindex $msg 1] ","] {
set rooms($chn) [lreplace $rooms($chn) [lsearch -exact $rooms($chn) $chan] [lsearch -exact $rooms($chn) $chan]]
foreach {l} [split $::opmodes {}] {set rooms(list,$chn,$l) [lreplace $rooms(list,$chn,$l) [lsearch -exact $rooms(list,$chn,$l) $chan] [lsearch -exact $rooms(list,$chn,$l) $chan]]}
sendtochan $chan $chn "$::dispnames($chan)!$::idents($chan)@[gethostname $chan]" [list "PART" $chn "Part: [lindex $msg 2]"]
message'fd $chan "$::dispnames($chan)!$::idents($chan)@[gethostname $chan]" [list "PART" $chn "Part: [lindex $msg 2]"]
}
}
"kick" {
set zhan [getfdbynick [lindex $msg 2]]
foreach {chn} [split [lindex $msg 1] ","] {
if {[string match "*S*" $::modes($zhan)]} {
message'fdnoc $chan "$::config::me(server)" [list "484" $room $param "Cannot kick or deop a service"]
return
}
if {![chan'canchgmode $chan $chn "+" "h"]} {continue}
if {![chan'canchgmode $chan $chn "+" "o"] && [chan'canchgmode $zhan $chn "+" "o"]} {continue}
if {![chan'canchgmode $chan $chn "+" "a"] && [chan'canchgmode $zhan $chn "+" "a"]} {continue}
if {![chan'canchgmode $chan $chn "+" "q"] && [chan'canchgmode $zhan $chn "+" "q"]} {continue}
set rooms($chn) [lreplace $rooms($chn) [lsearch -exact $rooms($chn) $zhan] [lsearch -exact $rooms($chn) $zhan]]
foreach {l} [split $::opmodes {}] {set rooms(list,$chn,$l) [lreplace $rooms(list,$chn,$l) [lsearch -exact $rooms(list,$chn,$l) $zhan] [lsearch -exact $rooms(list,$chn,$l) $zhan]]}
sendtochan $chan $chn "$::dispnames($chan)!$::idents($chan)@[gethostname $chan]" [list "KICK" $chn [lindex $msg 2] [lindex $msg 3]]
message'fd $chan "$::dispnames($chan)!$::idents($chan)@[gethostname $chan]" [list "KICK" $chn [lindex $msg 2] [lindex $msg 3]]
message'fd $zhan "$::dispnames($chan)!$::idents($chan)@[gethostname $chan]" [list "KICK" $chn [lindex $msg 2] [lindex $msg 3]]
}
}
}
}
if {[string match "*L*" $modes($chan)]} {
switch -nocase [lindex $msg 0] {
"kill" {
set killed [getfdbynick [lindex $msg 2]]
if {[string match "*&*" $killed]} {