-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ircservices.tcl
851 lines (738 loc) · 24.3 KB
/
ircservices.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
# irc.tcl --
#
# irc services implementation for Tcl.
# based from irc package by tcllib
#
# -------------------------------------------------------------------------
namespace eval ::IRCServices {
variable PKG
variable DIR
set DIR(CUR) [file dirname [file dirname [file normalize [file join [info script] ...]]]]
array set PKG {
"version" "0.0.7"
"need_tcl" "8.6"
"need_tls" "1.7.16"
"need_logger" "0.9.4"
"need_zct" "0.0.10"
"name" "package IRCServices"
}
variable conn 0
variable botn 0
variable config
variable irctclfile [info script]
variable newCharArray [list]
array set config {
debug 0
logger 0
}
if { [file exists ${DIR(CUR)}/TCL-ZCT/ZCT.tcl] } { catch { source ${DIR(CUR)}/TCL-ZCT/ZCT.tcl } }
if { [catch { package require ZCT ${PKG(need_zct)} } err] } {
die "\[${PKG(name)} - erreur\] Nécessite le package ZCT ${PKG(need_zct)} (ou plus) pour fonctionner, Télécharger sur 'https://github.com/ZarTek-Creole/TCL-ZCT'.\nLe chargement du script a été annulé." ;
} else { namespace import -force ::ZCT::* }
pkg load Tcl ${PKG(need_tcl)} ${PKG(name)}
pkg load logger ${PKG(need_logger)} ${PKG(name)}
}
proc ::IRCServices::nextLetter { char } {
if { ${char} == "Z" } { set char "A" }
scan ${char} %c i;
set ALPHA_NEW [format %c [expr ${i}+1]]
return ${ALPHA_NEW}
}
proc ::IRCServices::unshift { words } {
set RES ""
for {set i [string length ${words}] } {0 < ${i}} { set i [expr ${i}-1] } {
set RES "${RES}[string index ${words} [expr ${i}-1]]"
}
return ${RES}
}
proc ::IRCServices::incrementChar { l } {
global newCharArray
set l [string toupper ${l}]
set lastChar [string index ${l} end]
set remString [string range ${l} 0 end-1]
if { ${lastChar} == "" } { set newChar "A" } else { set newChar [::IRCServices::nextLetter ${lastChar}] }
lappend newCharArray [::IRCServices::unshift ${newChar}]
if { ${lastChar} == "Z" } { return [::IRCServices::incrementChar ${remString}] }
set batchString "${remString}[lreverse ${newCharArray}]"
set newCharArray [list]
return [join $batchString ""]
}
proc ::IRCServices::config { args } {
variable config
if { [llength ${args}] == 0 } {
return [array get config]
} elseif { [llength ${args}] == 1 } {
set key [lindex ${args} 0]
return $config(${key})
} elseif { [llength ${args}] > 2 } {
error "wrong # args: should be \"config key ?val?\""
}
# llength ${args} == 2
set key [lindex ${args} 0]
set value [lindex ${args} 1]
foreach ns [namespace children] {
if {
[info exists config(${key})] && \
[info exists ${ns}::config(${key})] && \
[set ${ns}::config(${key})] == $config(${key})
} {
${ns}::cmd-config ${key} ${value}
}
}
set config(${key}) ${value}
}
# ::IRCServices::listnetworks --
#
# Return a list of handles to all existing listnetworks
# Renvoie une liste de descripteurs de toutes les connexions existantes
proc ::IRCServices::listnetworks { } {
set r {}
foreach ns [namespace children] {
lappend r ${ns}::network
}
return ${r}
}
# ::IRCServices::reload --
#
# Reload this file, and merge the current listnetworks into
# the new one.
proc ::IRCServices::reload { } {
variable conn
set oldconn ${conn}
namespace eval :: {
source [set ::IRCServices::IRCServicestclfile]
}
foreach ns [namespace children] {
foreach var {sock logger host port} {
set ${var} [set ${ns}::${var}]
}
array set dispatch [array get ${ns}::dispatch]
array set config [array get ${ns}::config]
# make sure our new connection uses the same namespace
set conn [string range ${ns} 10 end]
::IRCServices::connection
foreach var {sock logger host port} {
set ${ns}::${var} [set ${var}]
}
array set ${ns}::dispatch [array get dispatch]
array set ${ns}::config [array get config]
}
set conn ${oldconn}
}
# ::IRCServices::connection --
#
# Create an IRC connection namespace and associated commands.
proc ::IRCServices::connection { args } {
variable conn
variable config
variable sid ""
# Create a unique namespace of the form irc${conn}::${host}
set name [format "%s::IRCServices%s" [namespace current] ${conn}]
namespace eval ${name} {
variable sock
variable dispatch
variable linedata
variable config
variable UID_DB
variable [namespace current]::UID_LAST_INSERT
variable botn 0
variable sid ""
set sock {}
array set dispatch {}
array set linedata {}
set UID_LAST_INSERT {}
array set config [array get ::IRCServices::config]
if {
${config(logger)} || \
${config(debug)}
} {
variable logger
set logger [logger::init [namespace tail [namespace current]]]
if { !${config(debug)} } { ${logger}::disable debug }
}
proc TLSSocketCallBack { level args } {
set SOCKET_NAME [lindex ${args} 0]
set type [lindex ${args} 1]
set socketid [lindex ${args} 2]
set what [lrange ${args} 3 end]
cmd-log debug "Socket '${SOCKET_NAME}' callback ${type}: ${what}"
if { [string match -nocase "*certificate*verify*failed*" ${what}] } {
cmd-log error "IRCServices Socket erreur: Vous essayez de vous connecter a un serveur TLS auto-signé. (${what}) [tls::status ${socketid}]"
}
if { [string match -nocase "*wrong*version*number*" ${what}] } {
cmd-log error "IRCServices Socket erreur: Vous essayez sans doute de connecter en SSL sur un port Non-SSL. (${what})"
}
}
# send --
# send text to the IRC server
proc send { msg } {
variable sock
variable dispatch
if { ${sock} eq "" } { return }
cmd-log debug "send: '${msg}'"
if { [catch { puts ${sock} ${msg} } err] } {
catch { close ${sock} }
set sock {}
if { [info exists dispatch(EOF)] } {
eval ${dispatch(EOF)}
}
cmd-log error "Error in send: ${err}"
}
}
proc UID_GET { user } {
variable [namespace current]::UID_DB
variable [namespace current]::UID_LAST_INSERT
variable serverinfo
if { [UID_EXIST ${user}] } {
return "$UID_DB([string toupper ${user}])"
} else {
if { ${UID_LAST_INSERT} == "" } {
set UID_LAST_INSERT "${serverinfo(ID)}AAAAAA"
return ${UID_LAST_INSERT}
}
set UID_NOW [::IRCServices::incrementChar ${UID_LAST_INSERT}]
set UID_LAST_INSERT ${UID_NOW}
set UID_DB([string toupper ${user}]) ${UID_NOW}
return ${UID_NOW}
}
}
proc UID_CONVERT { ID } {
variable [namespace current]::UID_DB
if { [info exists UID_DB([string toupper ${ID}])] } {
return "$UID_DB([string toupper ${ID}])"
} else {
return ${ID}
}
}
proc UID_EXIST { CIBLE } {
variable config
variable [namespace current]::UID_DB
if { [info exists UID_DB([string toupper ${CIBLE}])]} {
return 1
} else {
return 0
}
}
proc UID_PREFIX_RANDOM { } {
# Pas de 0 ni de 1 car, trop probable de tombé sur un sid déjà utiliser
set CHAR_LIST {ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789}
set LENGTH [string length ${CHAR_LIST}]
set CHAR_WIN [expr int(rand()*(${LENGTH}+1))]
return "00[string index ${CHAR_LIST} ${CHAR_WIN}]"
}
#########################################################
# Implemented user-side commands, meaning that these commands
# cause the calling user to perform the given action.
#########################################################
# cmd-config --
#
# Set or return per-connection configuration options.
#
# Arguments:
#
# key name of the configuration option to change.
#
# value value (optional) of the configuration option.
proc cmd-config { args } {
variable config
variable logger
if { [llength ${args}] == 0 } {
return [array get config]
} elseif { [llength ${args}] == 1 } {
set key [lindex ${args} 0]
return $config(${key})
} elseif { [llength ${args}] > 2 } {
error "wrong # args: should be \"config key ?val?\""
}
set key [lindex ${args} 0]
set value [lindex ${args} 1]
if { ${key} eq "debug" } {
if {${value}} {
if { !${config(logger)} } { cmd-config logger 1 }
${logger}::enable debug
} elseif { [info exists logger] } {
${logger}::disable debug
}
}
if { ${key} eq "logger" } {
if {
${value} && \
!${config(logger)}
} {
set logger [logger::init [namespace tail [namespace current]]]
} elseif { [info exists logger] } {
${logger}::delete
unset logger
}
}
set config(${key}) ${value}
}
proc cmd-log {level text} {
variable logger
if { ![info exists logger] } return
${logger}::${level} ${text}
}
proc cmd-logname { } {
variable logger
if { ![info exists logger] } return
return ${logger}
}
# cmd-destroy --
#
# destroys the current connection and its namespace
proc cmd-destroy { } {
variable logger
variable sock
if { [info exists logger] } { ${logger}::delete }
catch { close ${sock} }
namespace delete [namespace current]
}
proc cmd-connected { } {
variable sock
if { ${sock} eq "" } { return 0 }
return 1
}
proc cmd-user { username hostname servername {userinfo ""} } {
if { ${userinfo} eq "" } {
send "USER ${username} ${hostname} server :${servername}"
} else {
send "USER ${username} ${hostname} ${servername} :${userinfo}"
}
}
proc cmd-ping { target } {
send "PRIVMSG ${target} :\001PING [clock seconds]\001"
}
proc cmd-serverping { } {
send "PING [clock seconds]"
}
proc cmd-ctcp { target line } {
send "PRIVMSG ${target} :\001${line}\001"
}
proc cmd-quit { {msg {tcl irc services module - https://github.com/ZarTek-Creole/TCL-PKG-IRCServices}} } {
send "QUIT :${msg}"
}
proc cmd-notice { target msg } {
send "NOTICE ${target} :${msg}"
}
proc cmd-kick { chan target {msg {}} } {
send "KICK ${chan} ${target} :${msg}"
}
proc cmd-mode { DEST {MODE ""} {CIBLE ""} } {
variable sid
send ":${sid} MODE ${DEST} ${MODE} ${CIBLE}"
}
proc cmd-topic { chan msg } {
send "TOPIC ${chan} :${msg}"
}
proc cmd-vusercreate { usernick username {userhost {localhost}} {usergecos {Package TCL IRCServices}} {usermodes {+qioS}} } {
variable sid
if { [UID_EXIST ${usernick}] } {
set usernick ${usernick}_2
set username ${username}2
}
set VU_UID [UID_GET ${usernick}]
send ":${sid} UID ${usernick} 1 [clock seconds] ${username} ${userhost} ${VU_UID} * ${usermodes} * * * :${usergecos}"
return ${VU_UID}
}
proc cmd-invite { chan target } {
send "INVITE ${target} ${chan}"
}
proc cmd-send { line } {
send ${line}
}
proc cmd-peername { } {
variable sock
if { ${sock} eq "" } { return {} }
return [fconfigure ${sock} -peername]
}
proc cmd-sockname { } {
variable sock
if { ${sock} eq "" } { return {} }
return [fconfigure ${sock} -sockname]
}
proc cmd-socket { } {
variable sock
return ${sock}
}
proc cmd-disconnect { } {
variable sock
if { ${sock} eq "" } { return -1 }
catch { close ${sock} }
set sock {}
return 0
}
# Connect --
# Create the actual tcp connection.
proc cmd-connect { Server_HostName Server_Port Server_Password { Server_Protocol 1 } { Server_Name Extra-Cool.FR } { Server_ID "" } } {
variable serverinfo
variable sock
variable ::IRCServices::PKG
set serverinfo(hostname) ${Server_HostName}
set serverinfo(password) ${Server_Password}
set serverinfo(name) ${Server_Name}
if { ${Server_ID} == "" } {
set serverinfo(ID) [UID_PREFIX_RANDOM]
} else {
set serverinfo(ID) ${Server_ID}
}
if { [string range ${Server_Port} 0 0] == "+" } {
set serverinfo(ssl) 1;
set serverinfo(port) [string range ${Server_Port} 1 end]
# Necesite le package tls, tentative de chargment de celui-ci via la fct de ZCT
::ZCT::pkg::load tls ${PKG(need_tls)} ${PKG(name)}
set socket_binary "::tls::socket \
-require 0 \
-request 0 \
-command \"[namespace current]::TLSSocketCallBack ${sock}\" \
";
} else {
set serverinfo(ssl) 0;
set serverinfo(port) ${Server_Port}
set socket_binary "::socket"
}
if { ${sock} eq "" } {
if {
[catch {
set sock [{*}${socket_binary} ${Server_HostName} ${Server_Port}]
} err]
} {
die "\[${PKG(name)} - Erreur\] Impossible de ce connecter sur ${Server_HostName}:${Server_Port}: ${err}"
}
fconfigure ${sock} -translation crlf -buffering line
fileevent ${sock} readable [namespace current]::GetLine
# https://www.unrealircd.org/docs/Server_protocol
if { ${Server_Protocol} } {
send "PASS :${Server_Password}"
# https://www.unrealircd.org/docs/Server_protocol:PROTOCTL_commandµ
send "PROTOCTL NICKv2 VHP UMODE2 NICKIP SJOIN SJOIN2 SJ3 NOQUIT TKLEXT MLOCK SID"
# EAUTH=my.server.name[,protocolversion[,versionflags,fullversiontext]]
send "PROTOCTL EAUTH=${Server_Name},${PKG(version)},IRCService,IRCService-${PKG(version)}"
send "PROTOCTL SID=${serverinfo(ID)}"
send ":${serverinfo(ID)} SERVER ${Server_Name} 1 :TCL IRCServices for IRC Networks"
send "EOS"
} else {
send "PASS ${Server_Password}"
send "SERVER ${Server_Name} 1 :Services for IRC Networks"
send "EOS"
}
}
return 1
}
proc cmd-bot { args } {
variable botn
variable config
variable [namespace current]::UID_DB
variable sid
# Create a unique namespace of the form irc${botn}::${host}
# ::IRCServices::IRCServices0::IRCServices0::bot
set name [format "%s::b%s" [namespace current] ${botn}]
namespace eval ${name} {
variable sock
variable dispatch
variable linedata
variable config
variable [namespace parent]::sid
set sock {}
array set dispatch {}
array set linedata {}
set UID_LAST_INSERT {}
array set config [array get ::IRCServices::config]
proc cmd-create { botnick botident bothost {botgecos {Package TCL IRCServices}} {botmodes {+qioS}} } {
# ${bn1} connect ClaraServ identserv MyHost.be; # Creation d'un bot service ClaraServ
variable bnick
variable ident
variable host
variable config
variable sid
variable bid
set bnick ${botnick}
set ident ${botident}
set host ${bothost}
set bid [[namespace parent]::UID_GET ${bnick}]
set sid [set [namespace parent]::sid]
[namespace parent]::send ":${sid} SQLINE ${bnick} :Reserved for services"
[namespace parent]::send ":${sid} UID ${bnick} 1 [clock seconds] ${ident} ${host} ${bid} * ${botmodes} * * * :${botgecos}"
return 0
}
proc cmd-privmsg { target msg } {
variable bid
[namespace parent]::send ":${bid} PRIVMSG ${target} :${msg}"
}
proc cmd-notice { target msg } {
variable bid
[namespace parent]::send ":${bid} NOTICE ${target} :${msg}"
}
proc cmd-join { chan } {
variable sid
variable bid
[namespace parent]::send ":${sid} SJOIN [clock seconds] ${chan} + :${bid}"
}
proc cmd-part { chan {msg ""} } {
variable bid
if { ${msg} eq "" } {
[namespace parent]::send ":${bid} PART ${chan}"
} else {
[namespace parent]::send ":${bid} PART ${chan} :${msg}"
}
}
proc cmd-mode { DEST {MODE ""} {CIBLE ""} } {
variable bid
[namespace parent]::send ":${bid} MODE ${DEST} ${MODE} ${CIBLE}"
}
proc cmd-send { line } {
[namespace parent]::send ${line}
}
proc cmd-mesend { line } {
variable bid
[namespace parent]::send ":${bid} ${line}"
}
# eventbind --
proc cmd-eventbind { event cmd } {
variable dispatch
set dispatch(${event}) ${cmd}
if { ${cmd} eq "" } {
unset dispatch(${event})
}
}
# eventget --
proc cmd-eventget { {event ""} } {
variable dispatch
if { ${event} == "" } {
return [array names dispatch];
}
if { [info exists dispatch(${event})] } {
return $dispatch(${event})
}
return {}
}
# eventexists --
proc cmd-eventexists { event } {
variable dispatch
return [info exists dispatch(${event})]
}
proc bot { cmd args } {
if { [info proc [namespace current]::cmd-${cmd}] == "" } {
return "sub-cmd inconnu. List: [join [string map [list "[namespace current]::cmd-" ""] [info proc [namespace current]::cmd-*]] ", "]"
} else {
eval [linsert ${args} 0 [namespace current]::cmd-${cmd}]
}
}
# Create default handlers.
set dispatch(PING) {network send "PONG :[msg]"}
set dispatch(defaultevent) #
set dispatch(defaultcmd) #
set dispatch(defaultnumeric) #
}
set returncommand [format "%s::b%s::bot" [namespace current] ${botn}]
incr botn
return ${returncommand}
}
# Callback API:
# These are all available from within callbacks, so as to
# provide an interface to provide some information on what is
# coming out of the server.
# action --
# Action returns the action performed, such as KICK, PRIVMSG,
# MODE etc, including numeric actions such as 001, 252, 353,
# and so forth.
proc action { } {
variable linedata
return ${linedata(action)}
}
proc msg { } {
variable linedata
return ${linedata(msg)}
}
proc who { {address 0} } {
variable linedata
if { ${address} == 0 } {
return [lindex [split ${linedata(who)} !] 0]
} else {
return ${linedata(who)}
}
}
proc who2 { {address 0} } {
variable linedata
if { ${address} == 0 } {
return [lindex [split ${linedata(who2)} !] 0]
} else {
return ${linedata(who2)}
}
}
proc sid { } {
variable linedata
return ${linedata(sid)}
}
proc bid { } {
variable linedata
return ${linedata(bid)}
}
proc target { } {
variable linedata
return ${linedata(target)}
}
proc target2 { } {
variable linedata
return ${linedata(target2)}
}
proc additional { } {
variable linedata
return ${linedata(additional)}
}
proc rawline { } {
variable linedata
return ${linedata(rawline)}
}
proc header { } {
variable linedata
return [concat [list ${linedata(who)} {$linedata(action)} \
${linedata(target)}] ${linedata(additional)}]
}
proc GetError { Message } {
set RE_Closing {ERROR\s:Closing\sLink:\s([^\[]+)\[([^\]]+)\]\s\((.*)\)}
set DIE ""
# connexion du services fermer par le serveur
if { [regexp -nocase ${RE_Closing} ${Message} match hostname ip raison] } {
if {[string match -nocase "*Authentication*failed*" ${raison}]} {
set DIE "Authentification du service '${hostname}' à échoué (mot de passe?)"
} elseif {[string match -nocase "*SID*collision*" ${raison}]} {
variable serverinfo
set DIE [format "Le 'SID' (Server ID:%s) est déjà utiliser. Choisissez un autre." ${serverinfo(ID)}]
}
}
if { ${DIE} != "" } { die "\[Error - IRCServices\] ${DIE}" }
}
# GetLine --
# Get a line from the server and dispatch it.
proc GetLine { } {
variable linedata
variable sock
variable dispatch
variable [namespace current]::UID_DB
array set linedata {}
set line "eof"
if {
[eof ${sock}] || \
[catch { gets ${sock} } line]
} {
close ${sock}
set sock {}
cmd-log error "Error receiving from network: ${line}"
if { [info exists dispatch(EOF)] } {
eval ${dispatch(EOF)}
}
die "Error IRCServices: perte de la connexion au serveur"
return
}
set line [string map { "\{" "\\\{" "\}" "\\\}" "\"" "\\\"" } ${line}]
cmd-log debug "Recieved: ${line}"
if { [string match -nocase "error*" [lindex ${line} 0]] } { GetError ${line} }
if { [set pos [string first " :" ${line}]] > -1 } {
set header [string range ${line} 0 [expr {${pos} - 1}]]
set linedata(msg) [string range ${line} [expr {${pos} + 2}] end]
} else {
set header [string trim ${line}]
set linedata(msg) {}
}
if { [string match :* ${header}] } {
set header [split [string trimleft ${header} :]]
} else {
set header [linsert [split ${header}] 0 {}]
}
set linedata(rawline) ${line}
set linedata(who) [lindex ${header} 0]
set linedata(who2) [[namespace current]::UID_CONVERT ${linedata(who)}]
set linedata(action) [lindex ${header} 1]
set linedata(target) [lindex ${header} 2]
set linedata(target2) [[namespace current]::UID_CONVERT ${linedata(target)}]
set linedata(additional) [lrange ${header} 3 end]
set linedata(sid) [namespace current]::network
foreach t [namespace children] {
set linedata(bid) ${t}::bot
if { [info exists ${t}::dispatch(${linedata(action)})] } {
catch {eval [set ${t}::dispatch(${linedata(action)})]}
} elseif { [string match {[0-9]??} ${t}::dispatch(action)] } {
eval [set ${t}::dispatch(defaultnumeric)]
} elseif { ${linedata(who)} eq "" } {
eval [set ${t}::dispatch(defaultcmd)]
} else {
eval [set ${t}::dispatch(defaultevent)]
}
}
if { [info exists dispatch(${linedata(action)})] } {
catch {eval $dispatch(${linedata(action)})}
} elseif { [string match {[0-9]??} ${linedata(action)}] } {
eval ${dispatch(defaultnumeric)}
} elseif { ${linedata(who)} eq "" } {
eval ${dispatch(defaultcmd)}
} else {
eval ${dispatch(defaultevent)}
}
if { [action] == "SLOG" } {
die ${linedata(msg)}
}
if { [action] == "UID" } {
# PROTOCOL TS6 : enregistre des ID<->nick en DB
set uid [string toupper [lindex [additional] 4]]
set UID_DB([string toupper [target]]) ${uid}
set UID_DB([string toupper ${uid}]) [target]
}
}
# eventbind --
# Register an event in the dispatch table.
# Arguments:
# event: name of event as sent by IRC server.
# cmd: proc to register as the event handler
proc cmd-eventbind { event cmd } {
variable dispatch
set dispatch(${event}) ${cmd}
if { ${cmd} eq "" } {
unset dispatch(${event})
}
}
# eventget --
# Return the currently registered handler for the event.
# Arguments:
# event: name of event as sent by IRC server.
proc cmd-eventget { event } {
variable dispatch
if { [info exists dispatch(${event})] } {
return $dispatch(${event})
}
return {}
}
# eventexists --
# Return a boolean value indicating if there is a handler
# registered for the event.
# Arguments:
# event: name of event as sent by IRC server.
proc cmd-eventexists { event } {
variable dispatch
return [info exists dispatch(${event})]
}
# network --
# Accepts user commands and dispatches them.
# Arguments:
# cmd: command to invoke
# args: arguments to the command
proc network { cmd args } {
if { [info proc [namespace current]::cmd-${cmd}] == "" } {
return "sub-cmd inconnu. List: [join [string map [list "[namespace current]::cmd-" ""] [info proc [namespace current]::cmd-*]] ", "]"
} else {
eval [linsert ${args} 0 [namespace current]::cmd-${cmd}]
}
}
# Create default handlers.
set dispatch(PING) {network send "PONG :[msg]"}
set dispatch(defaultevent) #
set dispatch(defaultcmd) #
set dispatch(defaultnumeric) #
}
set returncommand [format "%s::IRCServices%s::network" [namespace current] ${conn}]
incr conn
return ${returncommand}
}
# -------------------------------------------------------------------------
package provide IRCServices ${::IRCServices::PKG(version)}
# -------------------------------------------------------------------------
return