chiark / gitweb /
Restart on death.
[ircbot] / bot.tcl
CommitLineData
281f2c0e
IJ
1# Actual IRC bot code
2
3set helpfile helpinfos
9bc33297 4
574abac6 5source irccore.tcl
281f2c0e
IJ
6source parsecmd.tcl
7source stdhelp.tcl
7818b6af 8
281f2c0e
IJ
9proc privmsg_unlogged {prefix ischan params} {
10 if {!$ischan ||
574abac6 11 [regexp {^![a-z][-a-z]*[a-z]( .*)?$} [lindex $params 1]]} {
281f2c0e 12 return 0
7818b6af 13 }
574abac6
IJ
14 # on-channel message, ignore
15 set chan [lindex $params 0]
16 upvar #0 chan_lastactivity([irctolower $chan]) la
17 set la [clock seconds]
18 catch { recordlastseen_p $prefix "talking on $chan" 1 }
281f2c0e 19 return 1
422f52e4
IJ
20}
21
ef177383
IJ
22proc showintervalsecs {howlong abbrev} {
23 return [showintervalsecs/[opt timeformat] $howlong $abbrev]
d83fb8db
IJ
24}
25
ef177383 26proc showintervalsecs/ks {howlong abbrev} {
7ce72032
IJ
27 if {$howlong < 1000} {
28 return "${howlong}s"
83dd1224
IJ
29 } else {
30 if {$howlong < 1000000} {
31 set pfx k
32 set scale 1000
33 } else {
34 set pfx M
35 set scale 1000000
36 }
37 set value [expr "$howlong.0 / $scale"]
38 foreach {min format} {100 %.0f 10 %.1f 1 %.2f} {
39 if {$value < $min} continue
7ce72032 40 return [format "$format${pfx}s" $value]
83dd1224
IJ
41 }
42 }
43}
44
ef177383 45proc format_qty {qty unit abbrev} {
d83fb8db 46 set o $qty
ef177383
IJ
47 if {$abbrev} {
48 append o [string range $unit 0 0]
49 } else {
50 append o " "
51 append o $unit
52 if {$qty != 1} { append o s }
53 }
d83fb8db
IJ
54 return $o
55}
56
ef177383 57proc showintervalsecs/hms {qty abbrev} {
d83fb8db
IJ
58 set ul {second 60 minute 60 hour 24 day 7 week}
59 set remainv 0
60 while {[llength $ul] > 1 && $qty >= [set uv [lindex $ul 1]]} {
61 set remainu [lindex $ul 0]
62 set remainv [expr {$qty % $uv}]
63 set qty [expr {($qty-$remainv)/$uv}]
64 set ul [lreplace $ul 0 1]
65 }
ef177383 66 set o [format_qty $qty [lindex $ul 0] $abbrev]
d83fb8db 67 if {$remainv} {
ef177383
IJ
68 if {!$abbrev} { append o " " }
69 append o [format_qty $remainv $remainu $abbrev]
d83fb8db
IJ
70 }
71 return $o
72}
73
7ce72032
IJ
74proc showinterval {howlong} {
75 if {$howlong <= 0} {
76 return {just now}
77 } else {
ef177383 78 return "[showintervalsecs $howlong 0] ago"
7ce72032
IJ
79 }
80}
81
83dd1224
IJ
82proc showtime {when} {
83 return [showinterval [expr {[clock seconds] - $when}]]
84}
85
86proc def_msgproc {name argl body} {
87 proc msg_$name "varbase $argl" "\
88 upvar #0 msg/\$varbase/dest d\n\
89 upvar #0 msg/\$varbase/str s\n\
90 upvar #0 msg/\$varbase/accum a\n\
91$body"
92}
93
94def_msgproc begin {dest str} {
95 set d $dest
96 set s $str
97 set a {}
98}
99
100def_msgproc append {str} {
101 set ns "$s$str"
102 if {[string length $s] && [string length $ns] > 65} {
103 msg__sendout $varbase
104 set s " [string trimleft $str]"
105 } else {
106 set s $ns
107 }
108}
109
110def_msgproc finish {} {
111 msg__sendout $varbase
112 unset s
113 unset d
114 return $a
115}
116
117def_msgproc _sendout {} {
118 lappend a [string trimright $s]
119 set s {}
120}
121
122proc looking_whenwhere {when where} {
123 set str [showtime [expr {$when-1}]]
124 if {[string length $where]} { append str " on $where" }
125 return $str
126}
127
128proc recordlastseen_n {n how here} {
129 global lastseen lookedfor
422f52e4 130 set lastseen([irctolower $n]) [list $n [clock seconds] $how]
83dd1224
IJ
131 if {!$here} return
132 upvar #0 lookedfor([irctolower $n]) lf
133 if {[info exists lf]} {
134 switch -exact [llength $lf] {
135 0 {
136 set ml {}
137 }
138 1 {
139 manyset [lindex $lf 0] when who where
140 set ml [list \
141 "FYI, $who was looking for you [looking_whenwhere $when $where]."]
142 }
143 default {
144 msg_begin tosend $n "FYI, people have been looking for you:"
145 set i 0
146 set fin ""
147 foreach e $lf {
148 incr i
149 if {$i == 1} {
150 msg_append tosend " "
151 } elseif {$i == [llength $lf]} {
152 msg_append tosend " and "
153 set fin .
154 } else {
155 msg_append tosend ", "
156 }
157 manyset $e when who where
158 msg_append tosend \
159 "$who ([looking_whenwhere $when $where])$fin"
160 }
161 set ml [msg_finish tosend]
162 }
163 }
164 unset lf
165 msendprivmsg_delayed 1000 $n $ml
166 }
422f52e4 167}
281b5f05
IJ
168
169proc note_topic {showoff whoby topic} {
35e19219 170 set msg "FYI, $whoby has changed the topic on $showoff"
281b5f05
IJ
171 if {[string length $topic] < 160} {
172 append msg " to $topic"
173 } else {
174 append msg " but it is too long to reproduce here !"
175 }
176 set showoff [irctolower $showoff]
177 set tell [chandb_get $showoff topictell]
178 if {[lsearch -exact $tell *] >= 0} {
179 set tryspies [chandb_list]
180 } else {
181 set tryspies $tell
182 }
281b5f05
IJ
183 foreach spy $tryspies {
184 set see [chandb_get $spy topicsee]
281b5f05
IJ
185 if {[lsearch -exact $see $showoff] >= 0 || \
186 ([lsearch -exact $see *] >= 0 && \
187 [lsearch -exact $tell $spy] >= 0)} {
188 sendprivmsg $spy $msg
189 }
190 }
191}
192
83dd1224 193proc recordlastseen_p {p how here} {
422f52e4 194 prefix_nick
83dd1224 195 recordlastseen_n $n $how $here
422f52e4
IJ
196}
197
198proc chanmode_arg {} {
199 upvar 2 args cm_args
200 set rv [lindex $cm_args 0]
201 set cm_args [lreplace cm_args 0 0]
202 return $rv
203}
204
83dd1224 205proc chanmode_o1 {m g p chan} {
20087363 206 global nick chan_initialop
83dd1224
IJ
207 prefix_nick
208 set who [chanmode_arg]
209 recordlastseen_n $n "being nice to $who" 1
210 if {"[irctolower $who]" == "[irctolower $nick]"} {
534e26a9
IJ
211 set nlower [irctolower $n]
212 upvar #0 nick_unique($nlower) u
20087363
IJ
213 if {[chandb_exists $chan]} {
214 sendprivmsg $n Thanks.
215 } elseif {![info exists u]} {
216 sendprivmsg $n {Op me while not on the channel, why don't you ?}
217 } else {
218 set chan_initialop([irctolower $chan]) $u
219 sendprivmsg $n \
220 "Thanks. You can use `channel manager ...' to register this channel."
221 if {![nickdb_exists $n] || ![string length [nickdb_get $n username]]} {
222 sendprivmsg $n \
223 "(But to do that you must register your nick securely first.)"
224 }
225 }
83dd1224
IJ
226 }
227}
228
422f52e4
IJ
229proc chanmode_o0 {m g p chan} {
230 global nick chandeop
231 prefix_nick
232 set who [chanmode_arg]
83dd1224 233 recordlastseen_p $p "being mean to $who" 1
422f52e4
IJ
234 if {"[irctolower $who]" == "[irctolower $nick]"} {
235 set chandeop($chan) [list [clock seconds] $p]
236 }
cc2d31de 237}
9bc33297 238
422f52e4
IJ
239proc msg_MODE {p c dest modelist args} {
240 if {![ischan $dest]} return
241 if {[regexp {^\-(.+)$} $modelist dummy modelist]} {
242 set give 0
243 } elseif {[regexp {^\+(.+)$} $modelist dummy modelist]} {
244 set give 1
245 } else {
246 error "invalid modelist"
247 }
248 foreach m [split $modelist] {
249 set procname chanmode_$m$give
250 if {[catch { info body $procname }]} {
83dd1224 251 recordlastseen_p $p "fiddling with $dest" 1
422f52e4
IJ
252 } else {
253 $procname $m $give $p $dest
254 }
255 }
256}
257
534e26a9
IJ
258proc leaving {lchan} {
259 foreach luser [array names nick_onchans] {
260 upvar #0 nick_onchans($luser) oc
261 set oc [grep tc {"$tc" != "$lchan"} $oc]
262 }
263 upvar #0 chan_nicks($lchan) nlist
264 unset nlist
ebbae0a9
IJ
265 upvar #0 chan_lastactivity($lchan) la
266 catch { unset la }
534e26a9
IJ
267}
268
cf6ea4de
IJ
269proc doleave {lchan} {
270 sendout PART $lchan
271 leaving $lchan
272}
273
534e26a9
IJ
274proc dojoin {lchan} {
275 global chan_nicks
276 sendout JOIN $lchan
277 set chan_nicks($lchan) {}
278}
279
280proc check_justme {lchan} {
281 global nick
282 upvar #0 chan_nicks($lchan) nlist
283 if {[llength $nlist] != 1} return
e2af9bcb 284 if {"[lindex $nlist 0]" != "[irctolower $nick]"} return
534e26a9
IJ
285 if {[chandb_exists $lchan]} {
286 set mode [chandb_get $lchan mode]
287 if {"$mode" != "*"} {
288 sendout MODE $lchan $mode
289 }
281b5f05
IJ
290 set topic [chandb_get $lchan topicset]
291 if {[string length $topic]} {
292 sendout TOPIC $lchan $topic
281b5f05 293 }
534e26a9 294 } else {
cf6ea4de 295 doleave $lchan
a4a1f396
IJ
296 }
297}
298
a056c4bd 299proc process_kickpart {chan user} {
a4a1f396 300 global nick
a056c4bd 301 check_nick $user
534e26a9
IJ
302 set luser [irctolower $user]
303 set lchan [irctolower $chan]
a056c4bd 304 if {![ischan $chan]} { error "not a channel" }
534e26a9
IJ
305 if {"$luser" == "[irctolower $nick]"} {
306 leaving $lchan
307 } else {
308 upvar #0 nick_onchans($luser) oc
309 upvar #0 chan_nicks($lchan) nlist
310 set oc [grep tc {"$tc" != "$lchan"} $oc]
311 set nlist [grep tn {"$tn" != "$luser"} $nlist]
312 nick_case $user
313 if {![llength $oc]} {
314 nick_forget $luser
315 } else {
316 check_justme $lchan
317 }
a4a1f396 318 }
534e26a9 319}
a056c4bd 320
281b5f05
IJ
321proc msg_TOPIC {p c dest topic} {
322 prefix_nick
323 if {![ischan $dest]} return
324 recordlastseen_n $n "changing the topic on $dest" 1
325 note_topic [irctolower $dest] $n $topic
326}
327
a056c4bd
IJ
328proc msg_KICK {p c chans users comment} {
329 set chans [split $chans ,]
330 set users [split $users ,]
331 if {[llength $chans] > 1} {
332 foreach chan $chans user $users { process_kickpart $chan $user }
333 } else {
334 foreach user $users { process_kickpart [lindex $chans 0] $user }
335 }
336}
337
338proc msg_KILL {p c user why} {
339 nick_forget $user
340}
341
20087363
IJ
342set nick_counter 0
343set nick_arys {onchans username unique}
534e26a9
IJ
344# nick_onchans($luser) -> [list ... $lchan ...]
345# nick_username($luser) -> <securely known local username>
346# nick_unique($luser) -> <counter>
347# nick_case($luser) -> $user (valid even if no longer visible)
ebbae0a9 348# nick_markid($luser) -> <after id for marktime>
534e26a9
IJ
349
350# chan_nicks($lchan) -> [list ... $luser ...]
ebbae0a9 351# chan_lastactivity($lchan) -> [clock seconds]
a056c4bd 352
534e26a9
IJ
353proc lnick_forget {luser} {
354 global nick_arys chan_nicks
ebbae0a9 355 lnick_marktime_cancel $luser
a056c4bd 356 foreach ary $nick_arys {
534e26a9 357 upvar #0 nick_${ary}($luser) av
a056c4bd
IJ
358 catch { unset av }
359 }
534e26a9
IJ
360 foreach lch [array names chan_nicks] {
361 upvar #0 chan_nicks($lch) nlist
362 set nlist [grep tn {"$tn" != "$luser"} $nlist]
363 check_justme $lch
364 }
365}
366
367proc nick_forget {user} {
368 global nick_arys chan_nicks
369 lnick_forget [irctolower $user]
370 nick_case $user
a4a1f396
IJ
371}
372
534e26a9 373proc nick_case {user} {
a4a1f396 374 global nick_case
534e26a9 375 set nick_case([irctolower $user]) $user
a056c4bd
IJ
376}
377
83dd1224 378proc msg_NICK {p c newnick} {
a44d4982 379 global nick_arys nick_case calling_nick
83dd1224
IJ
380 prefix_nick
381 recordlastseen_n $n "changing nicks to $newnick" 0
a44d4982 382 set calling_nick $newnick
83dd1224 383 recordlastseen_n $newnick "changing nicks from $n" 1
6425c2d6 384 set luser [irctolower $n]
ebbae0a9 385 lnick_marktime_cancel $luser
6425c2d6 386 set lusernew [irctolower $newnick]
a056c4bd 387 foreach ary $nick_arys {
6425c2d6
IJ
388 upvar #0 nick_${ary}($luser) old
389 upvar #0 nick_${ary}($lusernew) new
a056c4bd
IJ
390 if {[info exists new]} { error "nick collision ?! $ary $n $newnick" }
391 if {[info exists old]} { set new $old; unset old }
392 }
6425c2d6 393 upvar #0 nick_onchans($lusernew) oc
534e26a9
IJ
394 foreach ch $oc {
395 upvar #0 chan_nicks($ch) nlist
396 set nlist [grep tn {"$tn" != "$luser"} $nlist]
397 lappend nlist $lusernew
398 }
ebbae0a9 399 lnick_marktime_start $lusernew "Hi." 500
a4a1f396 400 nick_case $newnick
83dd1224
IJ
401}
402
20087363
IJ
403proc nick_ishere {n} {
404 global nick_counter
534e26a9 405 upvar #0 nick_unique([irctolower $n]) u
20087363
IJ
406 if {![info exists u]} { set u [incr nick_counter].$n.[clock seconds] }
407 nick_case $n
408}
409
a056c4bd
IJ
410proc msg_JOIN {p c chan} {
411 prefix_nick
412 recordlastseen_n $n "joining $chan" 1
cf6ea4de
IJ
413 set nl [irctolower $n]
414 set lchan [irctolower $chan]
415 upvar #0 nick_onchans($nl) oc
416 upvar #0 chan_nicks($lchan) nlist
ebbae0a9
IJ
417 if {![info exists oc]} {
418 global marktime_join_startdelay
419 lnick_marktime_start $nl "Welcome." $marktime_join_startdelay
420 }
cf6ea4de
IJ
421 lappend oc $lchan
422 lappend nlist $nl
20087363 423 nick_ishere $n
a056c4bd
IJ
424}
425proc msg_PART {p c chan} {
426 prefix_nick
427 recordlastseen_n $n "leaving $chan" 1
428 process_kickpart $chan $n
429}
430proc msg_QUIT {p c why} {
431 prefix_nick
432 recordlastseen_n $n "leaving ($why)" 0
433 nick_forget $n
434}
422f52e4 435
cc2d31de 436proc msg_PRIVMSG {p c dest text} {
a01859b6
IJ
437 global errorCode
438
cc2d31de 439 prefix_nick
422f52e4 440 if {[ischan $dest]} {
83dd1224 441 recordlastseen_n $n "invoking me in $dest" 1
422f52e4 442 set output $dest
cc2d31de 443 } else {
83dd1224 444 recordlastseen_n $n "talking to me" 1
422f52e4
IJ
445 set output $n
446 }
a4a1f396 447 nick_case $n
422f52e4 448
281f2c0e 449 execute_usercommand $p $c $n $output $dest $text
422f52e4
IJ
450}
451
a056c4bd 452proc msg_INVITE {p c n chan} {
534e26a9 453 after 1000 [list dojoin [irctolower $chan]]
a056c4bd
IJ
454}
455
456proc grep {var predicate list} {
457 set o {}
458 upvar 1 $var v
459 foreach v $list {
460 if {[uplevel 1 [list expr $predicate]]} { lappend o $v }
461 }
462 return $o
463}
464
465proc msg_353 {p c dest type chan nicklist} {
466 global names_chans nick_onchans
534e26a9
IJ
467 set lchan [irctolower $chan]
468 upvar #0 chan_nicks($lchan) nlist
469 lappend names_chans $lchan
470 if {![info exists nlist]} {
471 # We don't think we're on this channel, so ignore it !
472 # Unfortunately, because we don't get a reply to PART,
473 # we have to remember ourselves whether we're on a channel,
474 # and ignore stuff if we're not, to avoid races. Feh.
475 return
476 }
477 set nlist_new {}
478 foreach user [split $nicklist { }] {
479 regsub {^[@+]} $user {} user
480 if {![string length $user]} continue
481 check_nick $user
482 set luser [irctolower $user]
483 upvar #0 nick_onchans($luser) oc
484 lappend oc $lchan
485 lappend nlist_new $luser
486 nick_ishere $user
a056c4bd 487 }
534e26a9 488 set nlist $nlist_new
a056c4bd
IJ
489}
490
491proc msg_366 {p c args} {
492 global names_chans nick_onchans
534e26a9
IJ
493 set lchan [irctolower $c]
494 foreach luser [array names nick_onchans] {
495 upvar #0 nick_onchans($luser) oc
496 if {[llength names_chans] > 1} {
a056c4bd 497 set oc [grep tc {[lsearch -exact $tc $names_chans] >= 0} $oc]
a056c4bd 498 }
534e26a9 499 if {![llength $oc]} { lnick_forget $n }
a056c4bd
IJ
500 }
501 unset names_chans
502}
503
4fd2739c 504proc check_username {target} {
7ce72032
IJ
505 if {
506 [string length $target] > 8 ||
507 [regexp {[^-0-9a-z]} $target] ||
508 ![regexp {^[a-z]} $target]
509 } { error "invalid username" }
4fd2739c
IJ
510}
511
20087363 512proc somedb__head {} {
7a70431a 513 uplevel 1 {
20087363
IJ
514 set idl [irctolower $id]
515 upvar #0 ${nickchan}db($idl) ndbe
516 binary scan $idl H* idh
517 set idfn $fprefix$idh
518 if {![info exists iddbe] && [file exists $idfn]} {
519 set f [open $idfn r]
7a70431a
IJ
520 try_except_finally { set newval [read $f] } {} { close $f }
521 if {[llength $newval] % 2} { error "invalid length" }
20087363 522 set iddbe $newval
7a70431a
IJ
523 }
524 }
525}
526
20087363
IJ
527proc def_somedb {name arglist body} {
528 foreach {nickchan fprefix} {nick users/n chan chans/c} {
529 proc ${nickchan}db_$name $arglist \
534e26a9
IJ
530 "set nickchan $nickchan; set fprefix $fprefix; $body"
531 }
532}
533
534def_somedb list {} {
535 set list {}
536 foreach path [glob -nocomplain -path $fprefix *] {
537 binary scan $path "A[string length $fprefix]A*" afprefix thinghex
538 if {"$afprefix" != "$fprefix"} { error "wrong prefix $path $afprefix" }
539 lappend list [binary format H* $thinghex]
20087363 540 }
534e26a9
IJ
541 return $list
542}
543
544proc def_somedb_id {name arglist body} {
545 def_somedb $name [concat id $arglist] "somedb__head; $body"
7a70431a
IJ
546}
547
534e26a9 548def_somedb_id exists {} {
20087363 549 return [info exists iddbe]
7a70431a
IJ
550}
551
534e26a9 552def_somedb_id delete {} {
20087363
IJ
553 catch { unset iddbe }
554 file delete $idfn
7a70431a
IJ
555}
556
ebbae0a9 557set default_settings_nick {timeformat ks marktime off}
281b5f05
IJ
558set default_settings_chan {
559 autojoin 1
560 mode *
561 userinvite pub
562 topicset {}
563 topicsee {}
564 topictell {}
565}
7a70431a 566
534e26a9 567def_somedb_id set {args} {
20087363
IJ
568 upvar #0 default_settings_$nickchan def
569 if {![info exists iddbe]} { set iddbe $def }
570 foreach {key value} [concat $iddbe $args] { set a($key) $value }
7a70431a
IJ
571 set newval {}
572 foreach {key value} [array get a] { lappend newval $key $value }
20087363 573 set f [open $idfn.new w]
7a70431a
IJ
574 try_except_finally {
575 puts $f $newval
576 close $f
20087363 577 file rename -force $idfn.new $idfn
7a70431a 578 } {
7a70431a 579 } {
d83fb8db 580 catch { close $f }
7a70431a 581 }
20087363 582 set iddbe $newval
7a70431a
IJ
583}
584
534e26a9 585def_somedb_id get {key} {
20087363
IJ
586 upvar #0 default_settings_$nickchan def
587 if {[info exists iddbe]} {
534e26a9 588 set l [concat $iddbe $def]
7a70431a 589 } else {
20087363 590 set l $def
7a70431a
IJ
591 }
592 foreach {tkey value} $l {
593 if {"$tkey" == "$key"} { return $value }
594 }
595 error "unset setting $key"
596}
597
20087363
IJ
598proc opt {key} {
599 global calling_nick
600 if {[info exists calling_nick]} { set n $calling_nick } { set n {} }
601 return [nickdb_get $n $key]
602}
603
7a70431a
IJ
604proc check_notonchan {} {
605 upvar 1 dest dest
a01859b6 606 if {[ischan $dest]} { usererror "That command must be sent privately." }
7a70431a
IJ
607}
608
609proc nick_securitycheck {strict} {
610 upvar 1 n n
a01859b6
IJ
611 if {![nickdb_exists $n]} {
612 usererror "You are unknown to me, use `register'."
613 }
20087363 614 set wantu [nickdb_get $n username]
7a70431a
IJ
615 if {![string length $wantu]} {
616 if {$strict} {
a01859b6 617 usererror "That feature is only available to secure users, sorry."
7a70431a
IJ
618 } else {
619 return
620 }
621 }
534e26a9
IJ
622 set luser [irctolower $n]
623 upvar #0 nick_username($luser) nu
7a70431a 624 if {![info exists nu]} {
a01859b6 625 usererror "Nick $n is secure, you must identify yourself first."
7a70431a
IJ
626 }
627 if {"$wantu" != "$nu"} {
a01859b6
IJ
628 usererror "You are the wrong user -\
629 the nick $n belongs to $wantu, not $nu."
7a70431a
IJ
630 }
631}
632
a01859b6 633proc channel_ismanager {channel n} {
c362e172 634 set mgrs [chandb_get $channel managers]
a01859b6
IJ
635 return [expr {[lsearch -exact [irctolower $mgrs] [irctolower $n]] >= 0}]
636}
637
638proc channel_securitycheck {channel} {
639 upvar n n
640 if {![channel_ismanager $channel $n]} {
641 usererror "You are not a manager of $channel."
20087363 642 }
a01859b6 643 nick_securitycheck 1
20087363
IJ
644}
645
646proc def_chancmd {name body} {
647 proc channel/$name {} \
648 " upvar 1 target chan; upvar 1 n n; upvar 1 text text; $body"
649}
650
281b5f05
IJ
651proc ta_listop {findnow procvalue} {
652 # findnow and procvalue are code fragments which will be executed
653 # in the caller's level. findnow should set ta_listop_ev to
654 # the current list, and procvalue should treat ta_listop_ev as
655 # a proposed value in the list and check and possibly modify
656 # (canonicalise?) it. After ta_listop, ta_listop_ev will
657 # be the new value of the list.
658 upvar 1 ta_listop_ev exchg
659 upvar 1 text text
20087363
IJ
660 set opcode [ta_word]
661 switch -exact _$opcode {
281b5f05 662 _= { }
20087363 663 _+ - _- {
281b5f05
IJ
664 uplevel 1 $findnow
665 foreach item $exchg { set array($item) 1 }
20087363
IJ
666 }
667 default {
281b5f05 668 error "list change opcode must be one of + - ="
20087363
IJ
669 }
670 }
281b5f05
IJ
671 foreach exchg [split $text " "] {
672 if {![string length $exchg]} continue
673 uplevel 1 $procvalue
20087363 674 if {"$opcode" != "-"} {
281b5f05 675 set array($exchg) 1
20087363 676 } else {
281b5f05 677 catch { unset array($exchg) }
20087363
IJ
678 }
679 }
281b5f05
IJ
680 set exchg [lsort [array names array]]
681}
682
683def_chancmd manager {
684 ta_listop {
685 if {[chandb_exists $chan]} {
686 set ta_listop_ev [chandb_get $chan managers]
687 } else {
688 set ta_listop_ev [list [irctolower $n]]
689 }
690 } {
691 check_nick $ta_listop_ev
692 set ta_listop_ev [irctolower $ta_listop_ev]
693 }
694 if {[llength $ta_listop_ev]} {
695 chandb_set $chan managers $ta_listop_ev
696 ucmdr "Managers of $chan: $ta_listop_ev" {}
20087363
IJ
697 } else {
698 chandb_delete $chan
699 ucmdr {} {} "forgets about managing $chan." {}
700 }
701}
702
703def_chancmd autojoin {
704 set yesno [ta_word]
705 switch -exact [string tolower $yesno] {
706 no { set nv 0 }
707 yes { set nv 1 }
708 default { error "channel autojoin must be `yes' or `no' }
709 }
710 chandb_set $chan autojoin $nv
a49f2997
IJ
711 ucmdr [expr {$nv ? "I will join $chan when I'm restarted " : \
712 "I won't join $chan when I'm restarted "}] {}
534e26a9
IJ
713}
714
5e5be903
IJ
715def_chancmd userinvite {
716 set nv [string tolower [ta_word]]
717 switch -exact $nv {
718 pub { set txt "!invite will work for $chan, but it won't work by /msg" }
719 here { set txt "!invite and /msg invite will work, but only for users who are already on $chan." }
720 all { set txt "Any user will be able to invite themselves or anyone else to $chan." }
721 none { set txt "I will not invite anyone to $chan." }
722 default {
723 error "channel userinvite must be `pub', `here', `all' or `none'
724 }
725 }
726 chandb_set $chan userinvite $nv
727 ucmdr $txt {}
728}
729
281b5f05
IJ
730def_chancmd topic {
731 set what [ta_word]
732 switch -exact $what {
733 leave {
734 ta_nomore
735 chandb_set $chan topicset {}
736 ucmdr "I won't ever change the topic of $chan." {}
737 }
738 set {
739 set t [string trim $text]
740 if {![string length $t]} {
741 error "you must specific the topic to set"
742 }
743 chandb_set $chan topicset $t
a49f2997 744 ucmdr "Whenever I'm alone on $chan, I'll set the topic to $t." {}
281b5f05
IJ
745 }
746 see - tell {
747 ta_listop {
748 set ta_listop_ev [chandb_get $chan topic$what]
749 } {
750 if {"$ta_listop_ev" != "*"} {
751 if {![ischan $ta_listop_ev]} {
752 error "bad channel \`$ta_listop_ev' in topic $what"
753 }
754 set ta_listop_ev [irctolower $ta_listop_ev]
755 }
756 }
757 chandb_set $chan topic$what $ta_listop_ev
758 ucmdr "Topic $what list for $chan: $ta_listop_ev" {}
759 }
760 default {
a01859b6 761 usererror "Unknown channel topic subcommand - see help channel."
281b5f05
IJ
762 }
763 }
764}
765
534e26a9
IJ
766def_chancmd mode {
767 set mode [ta_word]
768 if {"$mode" != "*" && ![regexp {^(([-+][imnpst]+)+)$} $mode mode]} {
769 error {channel mode must be * or match ([-+][imnpst]+)+}
770 }
771 chandb_set $chan mode $mode
772 if {"$mode" == "*"} {
281b5f05 773 ucmdr "I won't ever change the mode of $chan." {}
534e26a9 774 } else {
281b5f05 775 ucmdr "Whenever I'm alone on $chan, I'll set the mode to $mode." {}
534e26a9 776 }
20087363
IJ
777}
778
779def_chancmd show {
780 if {[chandb_exists $chan]} {
781 set l "Settings for $chan: autojoin "
782 append l [lindex {no yes} [chandb_get $chan autojoin]]
b60b5a0f
IJ
783 append l ", mode " [chandb_get $chan mode]
784 append l ", userinvite " [chandb_get $chan userinvite] "."
20087363 785 append l "\nManagers: "
8b6ee626 786 append l [join [chandb_get $chan managers] " "]
281b5f05
IJ
787 foreach {ts sep} {see "\n" tell " "} {
788 set t [chandb_get $chan topic$ts]
789 append l $sep
790 if {[llength $t]} {
791 append l "Topic $ts list: $t."
792 } else {
793 append l "Topic $ts list is empty."
794 }
795 }
8b6ee626
IJ
796 append l "\n"
797 set t [chandb_get $chan topicset]
798 if {[string length $t]} {
799 append l "Topic to set: $t"
800 } else {
801 append l "I will not change the topic."
802 }
20087363
IJ
803 ucmdr {} $l
804 } else {
805 ucmdr {} "The channel $chan is not managed."
806 }
807}
808
cf6ea4de
IJ
809proc channelmgr_monoop {} {
810 upvar 1 dest dest
811 upvar 1 text text
812 upvar 1 n n
813 upvar 1 p p
814 upvar 1 target target
815 global chan_nicks
816
3c9af14f
IJ
817 prefix_nick
818
20087363
IJ
819 if {[ischan $dest]} { set target $dest }
820 if {[ta_anymore]} { set target [ta_word] }
821 ta_nomore
cf6ea4de 822 if {![info exists target]} {
a01859b6 823 usererror "You must specify, or invoke me on, the relevant channel."
cf6ea4de
IJ
824 }
825 if {![info exists chan_nicks([irctolower $target])]} {
a01859b6 826 usererror "I am not on $target."
cf6ea4de 827 }
20087363 828 if {![ischan $target]} { error "not a valid channel" }
3c9af14f 829
a01859b6
IJ
830 if {![chandb_exists $target]} {
831 usererror "$target is not a managed channel."
832 }
833 channel_securitycheck $target
cf6ea4de
IJ
834}
835
836def_ucmd op {
837 channelmgr_monoop
20087363
IJ
838 sendout MODE $target +o $n
839}
840
cf6ea4de
IJ
841def_ucmd leave {
842 channelmgr_monoop
843 doleave $target
844}
845
5e5be903 846def_ucmd invite {
a01859b6
IJ
847 global chan_nicks errorCode errorInfo
848 prefix_nick
5e5be903
IJ
849
850 if {[ischan $dest]} {
851 set target $dest
852 set onchan 1
853 } else {
854 set target [ta_word]
855 set onchan 0
856 }
857 set ltarget [irctolower $target]
a01859b6
IJ
858 if {![ischan $target]} { error "$target is not a channel" }
859 if {![info exists chan_nicks($ltarget)]} {
860 usererror "I am not on $target."
5e5be903 861 }
a01859b6
IJ
862 set ui [chandb_get $ltarget userinvite]
863 if {[catch {
864 if {"$ui" == "pub" && !$onchan} {
865 usererror "Invitations to $target must be made there with !invite."
866 }
867 if {"$ui" != "all"} {
868 if {[lsearch -exact $chan_nicks($ltarget) [irctolower $n]] < 0} {
869 usererror "Invitations to $target may only be made\
870 by a user on the channel."
871 }
872 }
873 if {"$ui" == "none"} {
874 usererror "Sorry, I've not been authorised\
875 to invite people to $target."
876 }
877 } emsg]} {
878 if {"$errorCode" == "BLIGHT USER" && [channel_ismanager $target $n]} {
879 if {[catch {
880 nick_securitycheck 1
881 } emsg2]} {
882 if {"$errorCode" == "BLIGHT USER"} {
883 usererror "$emsg2 Therefore you can't use your\
884 channel manager privilege. $emsg"
885 } else {
886 error $error $errorInfo $errorCode
887 }
888 }
889 } else {
890 error $emsg $errorInfo $errorCode
5e5be903 891 }
5e5be903
IJ
892 }
893 if {![ta_anymore]} {
a01859b6 894 usererror "You have to say who to invite."
5e5be903
IJ
895 }
896 set invitees {}
897 while {[ta_anymore]} {
898 set invitee [ta_word]
899 check_nick $invitee
900 lappend invitees $invitee
901 }
902 foreach invitee $invitees {
903 sendout INVITE $invitee $ltarget
904 }
905 set who [lindex $invitees 0]
906 switch -exact llength $invitees {
907 0 { error "zero invitees" }
908 1 { }
909 2 { append who " and [lindex $invitees 1]" }
910 * {
911 set who [join [lreplace $invitees end end] ", "]
912 append who " and [lindex $invitees [llength $invitees]]"
913 }
914 }
33718ee2 915 ucmdr {} {} {} "invites $who to $target."
5e5be903
IJ
916}
917
20087363
IJ
918def_ucmd channel {
919 if {[ischan $dest]} { set target $dest }
920 if {![ta_anymore]} {
921 set subcmd show
922 } else {
923 set subcmd [ta_word]
924 }
925 if {[ischan $subcmd]} {
926 set target $subcmd
927 if {![ta_anymore]} {
928 set subcmd show
929 } else {
930 set subcmd [ta_word]
931 }
932 }
933 if {![info exists target]} { error "privately, you must specify a channel" }
934 set procname channel/$subcmd
935 if {"$subcmd" != "show"} {
a01859b6
IJ
936 if {[catch { info body $procname }]} {
937 usererror "unknown channel setting $subcmd."
938 }
20087363 939 prefix_nick
20087363 940 if {[chandb_exists $target]} {
a01859b6 941 channel_securitycheck $target
20087363 942 } else {
a01859b6 943 nick_securitycheck 1
20087363 944 upvar #0 chan_initialop([irctolower $target]) io
534e26a9 945 upvar #0 nick_unique([irctolower $n]) u
a01859b6
IJ
946 if {![info exists io]} {
947 usererror "$target is not a managed channel."
948 }
949 if {"$io" != "$u"} {
950 usererror "You are not the interim manager of $target."
951 }
952 if {"$subcmd" != "manager"} {
953 usererror "Please use `channel manager' first."
954 }
20087363
IJ
955 }
956 }
957 channel/$subcmd
958}
959
a4a1f396
IJ
960def_ucmd who {
961 if {[ta_anymore]} {
962 set target [ta_word]; ta_nomore
963 set myself 1
964 } else {
965 prefix_nick
966 set target $n
967 set myself [expr {"$target" != "$n"}]
968 }
534e26a9
IJ
969 set ltarget [irctolower $target]
970 upvar #0 nick_case($ltarget) ctarget
a4a1f396 971 set nshow $target
534e26a9
IJ
972 if {[info exists ctarget]} {
973 upvar #0 nick_onchans($ltarget) oc
974 upvar #0 nick_username($ltarget) nu
975 if {[info exists oc]} { set nshow $ctarget }
a4a1f396 976 }
534e26a9 977 if {![nickdb_exists $ltarget]} {
a4a1f396 978 set ol "$nshow is not a registered nick."
20087363 979 } elseif {[string length [set username [nickdb_get $target username]]]} {
a4a1f396
IJ
980 set ol "The nick $nshow belongs to the user $username."
981 } else {
982 set ol "The nick $nshow is registered (but not to a username)."
983 }
534e26a9 984 if {![info exists ctarget] || ![info exists oc]} {
a4a1f396
IJ
985 if {$myself} {
986 append ol "\nI can't see $nshow on anywhere."
987 } else {
988 append ol "\nYou aren't on any channels with me."
989 }
990 } elseif {![info exists nu]} {
991 append ol "\n$nshow has not identified themselves."
992 } elseif {![info exists username]} {
993 append ol "\n$nshow has identified themselves as the user $nu."
994 } elseif {"$nu" != "$username"} {
995 append ol "\nHowever, $nshow is being used by the user $nu."
996 } else {
997 append ol "\n$nshow has identified themselves to me."
998 }
999 ucmdr {} $ol
1000}
1001
7a70431a
IJ
1002def_ucmd register {
1003 prefix_nick
1004 check_notonchan
1005 set old [nickdb_exists $n]
1006 if {$old} { nick_securitycheck 0 }
534e26a9 1007 set luser [irctolower $n]
7a70431a
IJ
1008 switch -exact [string tolower [string trim $text]] {
1009 {} {
534e26a9 1010 upvar #0 nick_username($luser) nu
7a70431a
IJ
1011 if {![info exists nu]} {
1012 ucmdr {} \
a4a1f396 1013 "You must identify yourself before using `register'. See `help identify', or use `register insecure'."
7a70431a
IJ
1014 }
1015 nickdb_set $n username $nu
1016 ucmdr {} {} "makes a note of your username." {}
1017 }
1018 delete {
1019 nickdb_delete $n
1020 ucmdr {} {} "forgets your nickname." {}
1021 }
1022 insecure {
1023 nickdb_set $n username {}
1024 if {$old} {
1025 ucmdr {} "Security is now disabled for your nickname !"
1026 } else {
1027 ucmdr {} "This is fine, but bear in mind that people will be able to mess with your settings. Channel management features need a secure registration." "makes an insecure registration for your nick."
1028 }
1029 }
0d3ea3aa
IJ
1030 default {
1031 error "you mean register / register delete / register insecure"
1032 }
7a70431a 1033 }
11d9bff9
IJ
1034}
1035
1036proc timeformat_desc {tf} {
1037 switch -exact $tf {
86892128 1038 ks { return "Times will be displayed in seconds or kiloseconds." }
11d9bff9
IJ
1039 hms { return "Times will be displayed in hours, minutes, etc." }
1040 default { error "invalid timeformat: $v" }
1041 }
1042}
1043
1044proc def_setting {opt show_body set_body} {
1045 proc set_show/$opt {} "
1046 upvar 1 n n
1047 set opt $opt
1048 $show_body"
1049 if {![string length $set_body]} return
1050 proc set_set/$opt {} "
1051 upvar 1 n n
1052 upvar 1 text text
1053 set opt $opt
1054 $set_body"
1055}
1056
1057def_setting timeformat {
20087363 1058 set tf [nickdb_get $n timeformat]
11d9bff9
IJ
1059 return "$tf: [timeformat_desc $tf]"
1060} {
1061 set tf [string tolower [ta_word]]
1062 ta_nomore
1063 set desc [timeformat_desc $tf]
1064 nickdb_set $n timeformat $tf
1065 ucmdr {} $desc
1066}
1067
ebbae0a9
IJ
1068proc marktime_desc {mt} {
1069 if {"$mt" == "off"} {
1070 return "I will not send you periodic messages."
1071 } elseif {"$mt" == "once"} {
1072 return "I will send you one informational message when I see you."
1073 } else {
ef177383 1074 return "I'll send you a message every [showintervalsecs $mt 0]."
ebbae0a9
IJ
1075 }
1076}
1077
1078def_setting marktime {
1079 set mt [nickdb_get $n marktime]
1080 set p $mt
a4e294d4 1081 if {[string match {[0-9]*} $mt]} { append p s }
ebbae0a9
IJ
1082 append p ": "
1083 append p [marktime_desc $mt]
1084 return $p
1085} {
1086 global marktime_min
1087 set mt [string tolower [ta_word]]
1088 ta_nomore
1089
1090 if {"$mt" == "off" || "$mt" == "once"} {
1091 } elseif {[regexp {^([0-9]+)([a-z]+)$} $mt dummy value unit]} {
1092 switch -exact $unit {
1093 s { set u 1 }
1094 ks { set u 1000 }
1095 m { set u 60 }
1096 h { set u 3600 }
1097 default { error "unknown unit of time $unit" }
1098 }
1099 if {$value > 86400*21/$u} { error "marktime interval too large" }
1100 set mt [expr {$value*$u}]
1101 if {$mt < $marktime_min} { error "marktime interval too small" }
1102 } else {
1103 error "invalid syntax for marktime"
1104 }
1105 nickdb_set $n marktime $mt
1106 lnick_marktime_start [irctolower $n] "So:" 500
1107 ucmdr {} [marktime_desc $mt]
1108}
1109
11d9bff9 1110def_setting security {
20087363 1111 set s [nickdb_get $n username]
11d9bff9
IJ
1112 if {[string length $s]} {
1113 return "Your nick, $n, is controlled by the user $s."
1114 } else {
1115 return "Your nick, $n, is not secure."
1116 }
1117} {}
1118
1119def_ucmd set {
1120 prefix_nick
1121 check_notonchan
1122 if {![nickdb_exists $n]} {
d83fb8db 1123 ucmdr {} "You are unknown to me and so have no settings. (Use `register'.)"
11d9bff9
IJ
1124 }
1125 if {![ta_anymore]} {
1126 set ol {}
1127 foreach proc [lsort [info procs]] {
1128 if {![regexp {^set_show/(.*)$} $proc dummy opt]} continue
1129 lappend ol [format "%-10s %s" $opt [set_show/$opt]]
1130 }
1131 ucmdr {} [join $ol "\n"]
1132 } else {
1133 set opt [ta_word]
1134 if {[catch { info body set_show/$opt }]} {
1135 error "no setting $opt"
1136 }
1137 if {![ta_anymore]} {
1138 ucmdr {} "$opt [set_show/$opt]"
1139 } else {
86892128 1140 nick_securitycheck 0
11d9bff9
IJ
1141 if {[catch { info body set_set/$opt }]} {
1142 error "setting $opt cannot be set with `set'"
1143 }
1144 set_set/$opt
1145 }
1146 }
1147}
7a70431a 1148
4fd2739c
IJ
1149def_ucmd identpass {
1150 set username [ta_word]
d83fb8db 1151 set passmd5 [md5sum "[ta_word]\n"]
4fd2739c
IJ
1152 ta_nomore
1153 prefix_nick
11d9bff9 1154 check_notonchan
534e26a9
IJ
1155 set luser [irctolower $n]
1156 upvar #0 nick_onchans($luser) onchans
4fd2739c 1157 if {![info exists onchans] || ![llength $onchans]} {
7a70431a 1158 ucmdr "You must be on a channel with me to identify yourself." {}
4fd2739c
IJ
1159 }
1160 check_username $username
1161 exec userv --timeout 3 $username << "$passmd5\n" > /dev/null \
1162 irc-identpass $n
534e26a9 1163 upvar #0 nick_username($luser) rec_username
4fd2739c 1164 set rec_username $username
7a70431a 1165 ucmdr "Pleased to see you, $username." {}
4fd2739c
IJ
1166}
1167
1168def_ucmd summon {
1169 set target [ta_word]
1170 ta_nomore
1171 check_username $target
7ce72032
IJ
1172 prefix_nick
1173
1174 upvar #0 lastsummon($target) ls
1175 set now [clock seconds]
1176 if {[info exists ls]} {
1177 set interval [expr {$now - $ls}]
1178 if {$interval < 30} {
1179 ucmdr {} \
1180 "Please be patient; $target was summoned only [showinterval $interval]."
1181 }
1182 }
1183 regsub {^[^!]*!} $p {} path
1184 if {[catch {
1185 exec userv --timeout 3 $target irc-summon $n $path \
1186 [expr {[ischan $dest] ? "$dest" : ""}] \
1187 < /dev/null
1188 } rv]} {
1189 regsub -all "\n" $rv { / } rv
1190 error $rv
1191 }
1192 if {[regexp {^problem (.*)} $rv dummy problem]} {
8a8d337d 1193 ucmdr {} "The user `$target' $problem."
7ce72032
IJ
1194 } elseif {[regexp {^ok ([^ ]+) ([0-9]+)$} $rv dummy tty idlesince]} {
1195 set idletime [expr {$now - $idlesince}]
1196 set ls $now
1197 ucmdr {} {} {} "invites $target ($tty[expr {
ef177383 1198 $idletime > 10 ? ", idle for [showintervalsecs $idletime 0]" : ""
7ce72032
IJ
1199 }]) to [expr {
1200 [ischan $dest] ? "join us here" : "talk to you"
1201 }]."
1202 } else {
1203 error "unexpected response from userv service: $rv"
1204 }
1205}
1206
a69f7d2c
IJ
1207proc md5sum {value} { exec md5sum << $value }
1208
83dd1224 1209def_ucmd seen {
422f52e4 1210 global lastseen nick
83dd1224
IJ
1211 prefix_nick
1212 set ncase [ta_nick]
1213 set nlower [irctolower $ncase]
422f52e4 1214 ta_nomore
83dd1224
IJ
1215 set now [clock seconds]
1216 if {"$nlower" == "[irctolower $nick]"} {
a01859b6 1217 usererror "I am not self-aware."
83dd1224
IJ
1218 } elseif {![info exists lastseen($nlower)]} {
1219 set rstr "I've never seen $ncase."
422f52e4 1220 } else {
83dd1224
IJ
1221 manyset $lastseen($nlower) realnick time what
1222 set howlong [expr {$now - $time}]
1223 set string [showinterval $howlong]
1224 set rstr "I last saw $realnick $string, $what."
1225 }
1226 if {[ischan $dest]} {
1227 set where $dest
1228 } else {
1229 set where {}
1230 }
1231 upvar #0 lookedfor($nlower) lf
1232 if {[info exists lf]} { set oldvalue $lf } else { set oldvalue {} }
1233 set lf [list [list $now $n $where]]
1234 foreach v $oldvalue {
1235 if {"[irctolower [lindex $v 1]]" == "[irctolower $n]"} continue
1236 lappend lf $v
cc2d31de 1237 }
83dd1224 1238 ucmdr {} $rstr
cc2d31de
IJ
1239}
1240
ebbae0a9
IJ
1241proc lnick_marktime_cancel {luser} {
1242 upvar #0 nick_markid($luser) mi
1243 if {![info exists mi]} return
1244 catch { after cancel $mi }
1245 catch { unset mi }
1246}
1247
1248proc lnick_marktime_doafter {luser why ms} {
1249 lnick_marktime_cancel $luser
1250 upvar #0 nick_markid($luser) mi
1251 set mi [after $ms [list lnick_marktime_now $luser $why]]
1252}
1253
1254proc lnick_marktime_reset {luser} {
1255 set mt [nickdb_get $luser marktime]
1256 if {"$mt" == "off" || "$mt" == "once"} return
1257 lnick_marktime_doafter $luser "Time passes." [expr {$mt*1000}]
1258}
1259
1260proc lnick_marktime_start {luser why ms} {
1261 set mt [nickdb_get $luser marktime]
1262 if {"$mt" == "off"} {
1263 lnick_marktime_cancel $luser
1264 } else {
1265 lnick_marktime_doafter $luser $why $ms
1266 }
1267}
1268
1269proc lnick_marktime_now {luser why} {
1270 upvar #0 nick_onchans($luser) oc
93e9bcff
IJ
1271 global calling_nick
1272 set calling_nick $luser
ebbae0a9
IJ
1273 sendprivmsg $luser [lnick_pingstring $why $oc ""]
1274 lnick_marktime_reset $luser
1275}
1276
1277proc lnick_pingstring {why oc apstring} {
1278 global nick_onchans
1279 catch { exec uptime } uptime
1280 set nnicks [llength [array names nick_onchans]]
1281 if {[regexp \
1282 {^ *([0-9:apm]+) +up.*, +(\d+) users, +load average: +([0-9., ]+) *$} \
1283 $uptime dummy time users load]} {
93e9bcff 1284 regsub -all , $load {} load
ebbae0a9
IJ
1285 set uptime "$time $nnicks/$users $load"
1286 } else {
1287 append uptime ", $nnicks nicks"
1288 }
1289 if {[llength $oc]} {
1290 set best_la 0
1291 set activity quiet
1292 foreach ch $oc {
1293 upvar #0 chan_lastactivity($ch) la
1294 if {![info exists la]} continue
1295 if {$la <= $best_la} continue
ef177383
IJ
1296 set since [showintervalsecs [expr {[clock seconds]-$la}] 1]
1297 set activity "$ch $since"
ebbae0a9
IJ
1298 set best_la $la
1299 }
1300 } else {
1301 set activity unseen
1302 }
1303 set str $why
1304 append str " " $uptime " " $activity
1305 if {[string length $apstring]} { append str " " $apstring }
1306 return $str
1307}
1308
1309def_ucmd ping {
1310 if {[ischan $dest]} {
1311 set oc [irctolower $dest]
1312 } else {
1313 global nick_onchans
1314 prefix_nick
1315 set ln [irctolower $n]
1316 if {[info exists nick_onchans($ln)]} {
1317 set oc $nick_onchans($ln)
1318 } else {
1319 set oc {}
1320 }
1321 if {[llength $oc]} { lnick_marktime_reset $ln }
1322 }
1323 ucmdr {} [lnick_pingstring "Pong!" $oc $text]
1324}
1325
28c8ab78
IJ
1326proc ensure_globalsecret {} {
1327 global globalsecret
1328
1329 if {[info exists globalsecret]} return
1330 set gsfile [open /dev/urandom r]
1331 fconfigure $gsfile -translation binary
1332 set globalsecret [read $gsfile 32]
1333 binary scan $globalsecret H* globalsecret
1334 close $gsfile
1335 unset gsfile
1336}
1337
534e26a9 1338proc connected {} {
534e26a9
IJ
1339 foreach chan [chandb_list] {
1340 if {[chandb_get $chan autojoin]} { dojoin $chan }
1341 }
1342}
1343
28c8ab78 1344ensure_globalsecret
e6cc22dc 1345loadhelp
28c8ab78 1346ensure_connecting