chiark / gitweb /
Improve xfres.
[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 424}
ea8a1bfe 425proc msg_PART {p c chan args} {
a056c4bd 426 prefix_nick
ea8a1bfe
IJ
427 set msg "leaving $chan"
428 if {[llength $args]} {
429 set why [lindex $args 0]
430 if {"[irctolower $why]" != "[irctolower $n]"} { append msg " ($why)" }
431 }
432 recordlastseen_n $n $msg 1
a056c4bd
IJ
433 process_kickpart $chan $n
434}
435proc msg_QUIT {p c why} {
436 prefix_nick
437 recordlastseen_n $n "leaving ($why)" 0
438 nick_forget $n
439}
422f52e4 440
cc2d31de 441proc msg_PRIVMSG {p c dest text} {
a01859b6
IJ
442 global errorCode
443
cc2d31de 444 prefix_nick
422f52e4 445 if {[ischan $dest]} {
83dd1224 446 recordlastseen_n $n "invoking me in $dest" 1
422f52e4 447 set output $dest
cc2d31de 448 } else {
83dd1224 449 recordlastseen_n $n "talking to me" 1
422f52e4
IJ
450 set output $n
451 }
a4a1f396 452 nick_case $n
422f52e4 453
281f2c0e 454 execute_usercommand $p $c $n $output $dest $text
422f52e4
IJ
455}
456
a056c4bd 457proc msg_INVITE {p c n chan} {
534e26a9 458 after 1000 [list dojoin [irctolower $chan]]
a056c4bd
IJ
459}
460
461proc grep {var predicate list} {
462 set o {}
463 upvar 1 $var v
464 foreach v $list {
465 if {[uplevel 1 [list expr $predicate]]} { lappend o $v }
466 }
467 return $o
468}
469
470proc msg_353 {p c dest type chan nicklist} {
471 global names_chans nick_onchans
534e26a9
IJ
472 set lchan [irctolower $chan]
473 upvar #0 chan_nicks($lchan) nlist
474 lappend names_chans $lchan
475 if {![info exists nlist]} {
476 # We don't think we're on this channel, so ignore it !
477 # Unfortunately, because we don't get a reply to PART,
478 # we have to remember ourselves whether we're on a channel,
479 # and ignore stuff if we're not, to avoid races. Feh.
480 return
481 }
482 set nlist_new {}
483 foreach user [split $nicklist { }] {
484 regsub {^[@+]} $user {} user
485 if {![string length $user]} continue
486 check_nick $user
487 set luser [irctolower $user]
488 upvar #0 nick_onchans($luser) oc
489 lappend oc $lchan
490 lappend nlist_new $luser
491 nick_ishere $user
a056c4bd 492 }
534e26a9 493 set nlist $nlist_new
a056c4bd
IJ
494}
495
496proc msg_366 {p c args} {
497 global names_chans nick_onchans
534e26a9
IJ
498 set lchan [irctolower $c]
499 foreach luser [array names nick_onchans] {
500 upvar #0 nick_onchans($luser) oc
501 if {[llength names_chans] > 1} {
a056c4bd 502 set oc [grep tc {[lsearch -exact $tc $names_chans] >= 0} $oc]
a056c4bd 503 }
534e26a9 504 if {![llength $oc]} { lnick_forget $n }
a056c4bd
IJ
505 }
506 unset names_chans
507}
508
4fd2739c 509proc check_username {target} {
7ce72032
IJ
510 if {
511 [string length $target] > 8 ||
512 [regexp {[^-0-9a-z]} $target] ||
513 ![regexp {^[a-z]} $target]
514 } { error "invalid username" }
4fd2739c
IJ
515}
516
20087363 517proc somedb__head {} {
7a70431a 518 uplevel 1 {
20087363
IJ
519 set idl [irctolower $id]
520 upvar #0 ${nickchan}db($idl) ndbe
521 binary scan $idl H* idh
522 set idfn $fprefix$idh
523 if {![info exists iddbe] && [file exists $idfn]} {
524 set f [open $idfn r]
7a70431a
IJ
525 try_except_finally { set newval [read $f] } {} { close $f }
526 if {[llength $newval] % 2} { error "invalid length" }
20087363 527 set iddbe $newval
7a70431a
IJ
528 }
529 }
530}
531
20087363
IJ
532proc def_somedb {name arglist body} {
533 foreach {nickchan fprefix} {nick users/n chan chans/c} {
534 proc ${nickchan}db_$name $arglist \
534e26a9
IJ
535 "set nickchan $nickchan; set fprefix $fprefix; $body"
536 }
537}
538
539def_somedb list {} {
540 set list {}
541 foreach path [glob -nocomplain -path $fprefix *] {
542 binary scan $path "A[string length $fprefix]A*" afprefix thinghex
543 if {"$afprefix" != "$fprefix"} { error "wrong prefix $path $afprefix" }
544 lappend list [binary format H* $thinghex]
20087363 545 }
534e26a9
IJ
546 return $list
547}
548
549proc def_somedb_id {name arglist body} {
550 def_somedb $name [concat id $arglist] "somedb__head; $body"
7a70431a
IJ
551}
552
534e26a9 553def_somedb_id exists {} {
20087363 554 return [info exists iddbe]
7a70431a
IJ
555}
556
534e26a9 557def_somedb_id delete {} {
20087363
IJ
558 catch { unset iddbe }
559 file delete $idfn
7a70431a
IJ
560}
561
ebbae0a9 562set default_settings_nick {timeformat ks marktime off}
281b5f05
IJ
563set default_settings_chan {
564 autojoin 1
565 mode *
566 userinvite pub
567 topicset {}
568 topicsee {}
569 topictell {}
570}
7a70431a 571
534e26a9 572def_somedb_id set {args} {
20087363
IJ
573 upvar #0 default_settings_$nickchan def
574 if {![info exists iddbe]} { set iddbe $def }
575 foreach {key value} [concat $iddbe $args] { set a($key) $value }
7a70431a
IJ
576 set newval {}
577 foreach {key value} [array get a] { lappend newval $key $value }
20087363 578 set f [open $idfn.new w]
7a70431a
IJ
579 try_except_finally {
580 puts $f $newval
581 close $f
20087363 582 file rename -force $idfn.new $idfn
7a70431a 583 } {
7a70431a 584 } {
d83fb8db 585 catch { close $f }
7a70431a 586 }
20087363 587 set iddbe $newval
7a70431a
IJ
588}
589
534e26a9 590def_somedb_id get {key} {
20087363
IJ
591 upvar #0 default_settings_$nickchan def
592 if {[info exists iddbe]} {
534e26a9 593 set l [concat $iddbe $def]
7a70431a 594 } else {
20087363 595 set l $def
7a70431a
IJ
596 }
597 foreach {tkey value} $l {
598 if {"$tkey" == "$key"} { return $value }
599 }
600 error "unset setting $key"
601}
602
20087363
IJ
603proc opt {key} {
604 global calling_nick
605 if {[info exists calling_nick]} { set n $calling_nick } { set n {} }
606 return [nickdb_get $n $key]
607}
608
7a70431a
IJ
609proc check_notonchan {} {
610 upvar 1 dest dest
a01859b6 611 if {[ischan $dest]} { usererror "That command must be sent privately." }
7a70431a
IJ
612}
613
614proc nick_securitycheck {strict} {
615 upvar 1 n n
a01859b6
IJ
616 if {![nickdb_exists $n]} {
617 usererror "You are unknown to me, use `register'."
618 }
20087363 619 set wantu [nickdb_get $n username]
7a70431a
IJ
620 if {![string length $wantu]} {
621 if {$strict} {
a01859b6 622 usererror "That feature is only available to secure users, sorry."
7a70431a
IJ
623 } else {
624 return
625 }
626 }
534e26a9
IJ
627 set luser [irctolower $n]
628 upvar #0 nick_username($luser) nu
7a70431a 629 if {![info exists nu]} {
a01859b6 630 usererror "Nick $n is secure, you must identify yourself first."
7a70431a
IJ
631 }
632 if {"$wantu" != "$nu"} {
a01859b6
IJ
633 usererror "You are the wrong user -\
634 the nick $n belongs to $wantu, not $nu."
7a70431a
IJ
635 }
636}
637
a01859b6 638proc channel_ismanager {channel n} {
c362e172 639 set mgrs [chandb_get $channel managers]
a01859b6
IJ
640 return [expr {[lsearch -exact [irctolower $mgrs] [irctolower $n]] >= 0}]
641}
642
643proc channel_securitycheck {channel} {
644 upvar n n
645 if {![channel_ismanager $channel $n]} {
646 usererror "You are not a manager of $channel."
20087363 647 }
a01859b6 648 nick_securitycheck 1
20087363
IJ
649}
650
651proc def_chancmd {name body} {
652 proc channel/$name {} \
653 " upvar 1 target chan; upvar 1 n n; upvar 1 text text; $body"
654}
655
281b5f05
IJ
656proc ta_listop {findnow procvalue} {
657 # findnow and procvalue are code fragments which will be executed
658 # in the caller's level. findnow should set ta_listop_ev to
659 # the current list, and procvalue should treat ta_listop_ev as
660 # a proposed value in the list and check and possibly modify
661 # (canonicalise?) it. After ta_listop, ta_listop_ev will
662 # be the new value of the list.
663 upvar 1 ta_listop_ev exchg
664 upvar 1 text text
20087363
IJ
665 set opcode [ta_word]
666 switch -exact _$opcode {
281b5f05 667 _= { }
20087363 668 _+ - _- {
281b5f05
IJ
669 uplevel 1 $findnow
670 foreach item $exchg { set array($item) 1 }
20087363
IJ
671 }
672 default {
281b5f05 673 error "list change opcode must be one of + - ="
20087363
IJ
674 }
675 }
281b5f05
IJ
676 foreach exchg [split $text " "] {
677 if {![string length $exchg]} continue
678 uplevel 1 $procvalue
20087363 679 if {"$opcode" != "-"} {
281b5f05 680 set array($exchg) 1
20087363 681 } else {
281b5f05 682 catch { unset array($exchg) }
20087363
IJ
683 }
684 }
281b5f05
IJ
685 set exchg [lsort [array names array]]
686}
687
688def_chancmd manager {
689 ta_listop {
690 if {[chandb_exists $chan]} {
691 set ta_listop_ev [chandb_get $chan managers]
692 } else {
693 set ta_listop_ev [list [irctolower $n]]
694 }
695 } {
696 check_nick $ta_listop_ev
697 set ta_listop_ev [irctolower $ta_listop_ev]
698 }
699 if {[llength $ta_listop_ev]} {
700 chandb_set $chan managers $ta_listop_ev
701 ucmdr "Managers of $chan: $ta_listop_ev" {}
20087363
IJ
702 } else {
703 chandb_delete $chan
704 ucmdr {} {} "forgets about managing $chan." {}
705 }
706}
707
708def_chancmd autojoin {
709 set yesno [ta_word]
710 switch -exact [string tolower $yesno] {
711 no { set nv 0 }
712 yes { set nv 1 }
713 default { error "channel autojoin must be `yes' or `no' }
714 }
715 chandb_set $chan autojoin $nv
a49f2997
IJ
716 ucmdr [expr {$nv ? "I will join $chan when I'm restarted " : \
717 "I won't join $chan when I'm restarted "}] {}
534e26a9
IJ
718}
719
5e5be903
IJ
720def_chancmd userinvite {
721 set nv [string tolower [ta_word]]
722 switch -exact $nv {
723 pub { set txt "!invite will work for $chan, but it won't work by /msg" }
724 here { set txt "!invite and /msg invite will work, but only for users who are already on $chan." }
725 all { set txt "Any user will be able to invite themselves or anyone else to $chan." }
726 none { set txt "I will not invite anyone to $chan." }
727 default {
728 error "channel userinvite must be `pub', `here', `all' or `none'
729 }
730 }
731 chandb_set $chan userinvite $nv
732 ucmdr $txt {}
733}
734
281b5f05
IJ
735def_chancmd topic {
736 set what [ta_word]
737 switch -exact $what {
738 leave {
739 ta_nomore
740 chandb_set $chan topicset {}
741 ucmdr "I won't ever change the topic of $chan." {}
742 }
743 set {
744 set t [string trim $text]
745 if {![string length $t]} {
746 error "you must specific the topic to set"
747 }
748 chandb_set $chan topicset $t
a49f2997 749 ucmdr "Whenever I'm alone on $chan, I'll set the topic to $t." {}
281b5f05
IJ
750 }
751 see - tell {
752 ta_listop {
753 set ta_listop_ev [chandb_get $chan topic$what]
754 } {
755 if {"$ta_listop_ev" != "*"} {
756 if {![ischan $ta_listop_ev]} {
757 error "bad channel \`$ta_listop_ev' in topic $what"
758 }
759 set ta_listop_ev [irctolower $ta_listop_ev]
760 }
761 }
762 chandb_set $chan topic$what $ta_listop_ev
763 ucmdr "Topic $what list for $chan: $ta_listop_ev" {}
764 }
765 default {
a01859b6 766 usererror "Unknown channel topic subcommand - see help channel."
281b5f05
IJ
767 }
768 }
769}
770
534e26a9
IJ
771def_chancmd mode {
772 set mode [ta_word]
773 if {"$mode" != "*" && ![regexp {^(([-+][imnpst]+)+)$} $mode mode]} {
774 error {channel mode must be * or match ([-+][imnpst]+)+}
775 }
776 chandb_set $chan mode $mode
777 if {"$mode" == "*"} {
281b5f05 778 ucmdr "I won't ever change the mode of $chan." {}
534e26a9 779 } else {
281b5f05 780 ucmdr "Whenever I'm alone on $chan, I'll set the mode to $mode." {}
534e26a9 781 }
20087363
IJ
782}
783
784def_chancmd show {
785 if {[chandb_exists $chan]} {
786 set l "Settings for $chan: autojoin "
787 append l [lindex {no yes} [chandb_get $chan autojoin]]
b60b5a0f
IJ
788 append l ", mode " [chandb_get $chan mode]
789 append l ", userinvite " [chandb_get $chan userinvite] "."
20087363 790 append l "\nManagers: "
8b6ee626 791 append l [join [chandb_get $chan managers] " "]
281b5f05
IJ
792 foreach {ts sep} {see "\n" tell " "} {
793 set t [chandb_get $chan topic$ts]
794 append l $sep
795 if {[llength $t]} {
796 append l "Topic $ts list: $t."
797 } else {
798 append l "Topic $ts list is empty."
799 }
800 }
8b6ee626
IJ
801 append l "\n"
802 set t [chandb_get $chan topicset]
803 if {[string length $t]} {
804 append l "Topic to set: $t"
805 } else {
806 append l "I will not change the topic."
807 }
20087363
IJ
808 ucmdr {} $l
809 } else {
810 ucmdr {} "The channel $chan is not managed."
811 }
812}
813
cf6ea4de
IJ
814proc channelmgr_monoop {} {
815 upvar 1 dest dest
816 upvar 1 text text
817 upvar 1 n n
818 upvar 1 p p
819 upvar 1 target target
820 global chan_nicks
821
3c9af14f
IJ
822 prefix_nick
823
20087363
IJ
824 if {[ischan $dest]} { set target $dest }
825 if {[ta_anymore]} { set target [ta_word] }
826 ta_nomore
cf6ea4de 827 if {![info exists target]} {
a01859b6 828 usererror "You must specify, or invoke me on, the relevant channel."
cf6ea4de
IJ
829 }
830 if {![info exists chan_nicks([irctolower $target])]} {
a01859b6 831 usererror "I am not on $target."
cf6ea4de 832 }
20087363 833 if {![ischan $target]} { error "not a valid channel" }
3c9af14f 834
a01859b6
IJ
835 if {![chandb_exists $target]} {
836 usererror "$target is not a managed channel."
837 }
838 channel_securitycheck $target
cf6ea4de
IJ
839}
840
841def_ucmd op {
842 channelmgr_monoop
20087363
IJ
843 sendout MODE $target +o $n
844}
845
cf6ea4de
IJ
846def_ucmd leave {
847 channelmgr_monoop
848 doleave $target
849}
850
5e5be903 851def_ucmd invite {
a01859b6
IJ
852 global chan_nicks errorCode errorInfo
853 prefix_nick
5e5be903
IJ
854
855 if {[ischan $dest]} {
856 set target $dest
857 set onchan 1
858 } else {
859 set target [ta_word]
860 set onchan 0
861 }
862 set ltarget [irctolower $target]
a01859b6
IJ
863 if {![ischan $target]} { error "$target is not a channel" }
864 if {![info exists chan_nicks($ltarget)]} {
865 usererror "I am not on $target."
5e5be903 866 }
a01859b6
IJ
867 set ui [chandb_get $ltarget userinvite]
868 if {[catch {
869 if {"$ui" == "pub" && !$onchan} {
870 usererror "Invitations to $target must be made there with !invite."
871 }
872 if {"$ui" != "all"} {
873 if {[lsearch -exact $chan_nicks($ltarget) [irctolower $n]] < 0} {
874 usererror "Invitations to $target may only be made\
875 by a user on the channel."
876 }
877 }
878 if {"$ui" == "none"} {
879 usererror "Sorry, I've not been authorised\
880 to invite people to $target."
881 }
882 } emsg]} {
883 if {"$errorCode" == "BLIGHT USER" && [channel_ismanager $target $n]} {
884 if {[catch {
885 nick_securitycheck 1
886 } emsg2]} {
887 if {"$errorCode" == "BLIGHT USER"} {
888 usererror "$emsg2 Therefore you can't use your\
889 channel manager privilege. $emsg"
890 } else {
891 error $error $errorInfo $errorCode
892 }
893 }
894 } else {
895 error $emsg $errorInfo $errorCode
5e5be903 896 }
5e5be903
IJ
897 }
898 if {![ta_anymore]} {
a01859b6 899 usererror "You have to say who to invite."
5e5be903
IJ
900 }
901 set invitees {}
902 while {[ta_anymore]} {
903 set invitee [ta_word]
904 check_nick $invitee
905 lappend invitees $invitee
906 }
907 foreach invitee $invitees {
908 sendout INVITE $invitee $ltarget
909 }
910 set who [lindex $invitees 0]
911 switch -exact llength $invitees {
912 0 { error "zero invitees" }
913 1 { }
914 2 { append who " and [lindex $invitees 1]" }
915 * {
916 set who [join [lreplace $invitees end end] ", "]
917 append who " and [lindex $invitees [llength $invitees]]"
918 }
919 }
33718ee2 920 ucmdr {} {} {} "invites $who to $target."
5e5be903
IJ
921}
922
20087363
IJ
923def_ucmd channel {
924 if {[ischan $dest]} { set target $dest }
925 if {![ta_anymore]} {
926 set subcmd show
927 } else {
928 set subcmd [ta_word]
929 }
930 if {[ischan $subcmd]} {
931 set target $subcmd
932 if {![ta_anymore]} {
933 set subcmd show
934 } else {
935 set subcmd [ta_word]
936 }
937 }
938 if {![info exists target]} { error "privately, you must specify a channel" }
939 set procname channel/$subcmd
940 if {"$subcmd" != "show"} {
a01859b6
IJ
941 if {[catch { info body $procname }]} {
942 usererror "unknown channel setting $subcmd."
943 }
20087363 944 prefix_nick
20087363 945 if {[chandb_exists $target]} {
a01859b6 946 channel_securitycheck $target
20087363 947 } else {
a01859b6 948 nick_securitycheck 1
20087363 949 upvar #0 chan_initialop([irctolower $target]) io
534e26a9 950 upvar #0 nick_unique([irctolower $n]) u
a01859b6
IJ
951 if {![info exists io]} {
952 usererror "$target is not a managed channel."
953 }
954 if {"$io" != "$u"} {
955 usererror "You are not the interim manager of $target."
956 }
957 if {"$subcmd" != "manager"} {
958 usererror "Please use `channel manager' first."
959 }
20087363
IJ
960 }
961 }
962 channel/$subcmd
963}
964
a4a1f396
IJ
965def_ucmd who {
966 if {[ta_anymore]} {
967 set target [ta_word]; ta_nomore
968 set myself 1
969 } else {
970 prefix_nick
971 set target $n
972 set myself [expr {"$target" != "$n"}]
973 }
534e26a9
IJ
974 set ltarget [irctolower $target]
975 upvar #0 nick_case($ltarget) ctarget
a4a1f396 976 set nshow $target
534e26a9
IJ
977 if {[info exists ctarget]} {
978 upvar #0 nick_onchans($ltarget) oc
979 upvar #0 nick_username($ltarget) nu
980 if {[info exists oc]} { set nshow $ctarget }
a4a1f396 981 }
534e26a9 982 if {![nickdb_exists $ltarget]} {
a4a1f396 983 set ol "$nshow is not a registered nick."
20087363 984 } elseif {[string length [set username [nickdb_get $target username]]]} {
a4a1f396
IJ
985 set ol "The nick $nshow belongs to the user $username."
986 } else {
987 set ol "The nick $nshow is registered (but not to a username)."
988 }
534e26a9 989 if {![info exists ctarget] || ![info exists oc]} {
a4a1f396
IJ
990 if {$myself} {
991 append ol "\nI can't see $nshow on anywhere."
992 } else {
993 append ol "\nYou aren't on any channels with me."
994 }
995 } elseif {![info exists nu]} {
996 append ol "\n$nshow has not identified themselves."
997 } elseif {![info exists username]} {
998 append ol "\n$nshow has identified themselves as the user $nu."
999 } elseif {"$nu" != "$username"} {
1000 append ol "\nHowever, $nshow is being used by the user $nu."
1001 } else {
1002 append ol "\n$nshow has identified themselves to me."
1003 }
1004 ucmdr {} $ol
1005}
1006
7a70431a
IJ
1007def_ucmd register {
1008 prefix_nick
1009 check_notonchan
1010 set old [nickdb_exists $n]
1011 if {$old} { nick_securitycheck 0 }
534e26a9 1012 set luser [irctolower $n]
7a70431a
IJ
1013 switch -exact [string tolower [string trim $text]] {
1014 {} {
534e26a9 1015 upvar #0 nick_username($luser) nu
7a70431a
IJ
1016 if {![info exists nu]} {
1017 ucmdr {} \
a4a1f396 1018 "You must identify yourself before using `register'. See `help identify', or use `register insecure'."
7a70431a
IJ
1019 }
1020 nickdb_set $n username $nu
1021 ucmdr {} {} "makes a note of your username." {}
1022 }
1023 delete {
1024 nickdb_delete $n
1025 ucmdr {} {} "forgets your nickname." {}
1026 }
1027 insecure {
1028 nickdb_set $n username {}
1029 if {$old} {
1030 ucmdr {} "Security is now disabled for your nickname !"
1031 } else {
1032 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."
1033 }
1034 }
0d3ea3aa
IJ
1035 default {
1036 error "you mean register / register delete / register insecure"
1037 }
7a70431a 1038 }
11d9bff9
IJ
1039}
1040
1041proc timeformat_desc {tf} {
1042 switch -exact $tf {
86892128 1043 ks { return "Times will be displayed in seconds or kiloseconds." }
11d9bff9
IJ
1044 hms { return "Times will be displayed in hours, minutes, etc." }
1045 default { error "invalid timeformat: $v" }
1046 }
1047}
1048
1049proc def_setting {opt show_body set_body} {
1050 proc set_show/$opt {} "
1051 upvar 1 n n
1052 set opt $opt
1053 $show_body"
1054 if {![string length $set_body]} return
1055 proc set_set/$opt {} "
1056 upvar 1 n n
1057 upvar 1 text text
1058 set opt $opt
1059 $set_body"
1060}
1061
1062def_setting timeformat {
20087363 1063 set tf [nickdb_get $n timeformat]
11d9bff9
IJ
1064 return "$tf: [timeformat_desc $tf]"
1065} {
1066 set tf [string tolower [ta_word]]
1067 ta_nomore
1068 set desc [timeformat_desc $tf]
1069 nickdb_set $n timeformat $tf
1070 ucmdr {} $desc
1071}
1072
ebbae0a9
IJ
1073proc marktime_desc {mt} {
1074 if {"$mt" == "off"} {
1075 return "I will not send you periodic messages."
1076 } elseif {"$mt" == "once"} {
1077 return "I will send you one informational message when I see you."
1078 } else {
ef177383 1079 return "I'll send you a message every [showintervalsecs $mt 0]."
ebbae0a9
IJ
1080 }
1081}
1082
1083def_setting marktime {
1084 set mt [nickdb_get $n marktime]
1085 set p $mt
a4e294d4 1086 if {[string match {[0-9]*} $mt]} { append p s }
ebbae0a9
IJ
1087 append p ": "
1088 append p [marktime_desc $mt]
1089 return $p
1090} {
1091 global marktime_min
1092 set mt [string tolower [ta_word]]
1093 ta_nomore
1094
1095 if {"$mt" == "off" || "$mt" == "once"} {
1096 } elseif {[regexp {^([0-9]+)([a-z]+)$} $mt dummy value unit]} {
1097 switch -exact $unit {
1098 s { set u 1 }
1099 ks { set u 1000 }
1100 m { set u 60 }
1101 h { set u 3600 }
1102 default { error "unknown unit of time $unit" }
1103 }
1104 if {$value > 86400*21/$u} { error "marktime interval too large" }
1105 set mt [expr {$value*$u}]
1106 if {$mt < $marktime_min} { error "marktime interval too small" }
1107 } else {
1108 error "invalid syntax for marktime"
1109 }
1110 nickdb_set $n marktime $mt
1111 lnick_marktime_start [irctolower $n] "So:" 500
1112 ucmdr {} [marktime_desc $mt]
1113}
1114
11d9bff9 1115def_setting security {
20087363 1116 set s [nickdb_get $n username]
11d9bff9
IJ
1117 if {[string length $s]} {
1118 return "Your nick, $n, is controlled by the user $s."
1119 } else {
1120 return "Your nick, $n, is not secure."
1121 }
1122} {}
1123
1124def_ucmd set {
1125 prefix_nick
1126 check_notonchan
1127 if {![nickdb_exists $n]} {
d83fb8db 1128 ucmdr {} "You are unknown to me and so have no settings. (Use `register'.)"
11d9bff9
IJ
1129 }
1130 if {![ta_anymore]} {
1131 set ol {}
1132 foreach proc [lsort [info procs]] {
1133 if {![regexp {^set_show/(.*)$} $proc dummy opt]} continue
1134 lappend ol [format "%-10s %s" $opt [set_show/$opt]]
1135 }
1136 ucmdr {} [join $ol "\n"]
1137 } else {
1138 set opt [ta_word]
1139 if {[catch { info body set_show/$opt }]} {
1140 error "no setting $opt"
1141 }
1142 if {![ta_anymore]} {
1143 ucmdr {} "$opt [set_show/$opt]"
1144 } else {
86892128 1145 nick_securitycheck 0
11d9bff9
IJ
1146 if {[catch { info body set_set/$opt }]} {
1147 error "setting $opt cannot be set with `set'"
1148 }
1149 set_set/$opt
1150 }
1151 }
1152}
7a70431a 1153
4fd2739c
IJ
1154def_ucmd identpass {
1155 set username [ta_word]
d83fb8db 1156 set passmd5 [md5sum "[ta_word]\n"]
4fd2739c
IJ
1157 ta_nomore
1158 prefix_nick
11d9bff9 1159 check_notonchan
534e26a9
IJ
1160 set luser [irctolower $n]
1161 upvar #0 nick_onchans($luser) onchans
4fd2739c 1162 if {![info exists onchans] || ![llength $onchans]} {
7a70431a 1163 ucmdr "You must be on a channel with me to identify yourself." {}
4fd2739c
IJ
1164 }
1165 check_username $username
1166 exec userv --timeout 3 $username << "$passmd5\n" > /dev/null \
1167 irc-identpass $n
534e26a9 1168 upvar #0 nick_username($luser) rec_username
4fd2739c 1169 set rec_username $username
7a70431a 1170 ucmdr "Pleased to see you, $username." {}
4fd2739c
IJ
1171}
1172
1173def_ucmd summon {
1174 set target [ta_word]
1175 ta_nomore
1176 check_username $target
7ce72032
IJ
1177 prefix_nick
1178
1179 upvar #0 lastsummon($target) ls
1180 set now [clock seconds]
1181 if {[info exists ls]} {
1182 set interval [expr {$now - $ls}]
1183 if {$interval < 30} {
1184 ucmdr {} \
1185 "Please be patient; $target was summoned only [showinterval $interval]."
1186 }
1187 }
1188 regsub {^[^!]*!} $p {} path
1189 if {[catch {
1190 exec userv --timeout 3 $target irc-summon $n $path \
1191 [expr {[ischan $dest] ? "$dest" : ""}] \
1192 < /dev/null
1193 } rv]} {
1194 regsub -all "\n" $rv { / } rv
1195 error $rv
1196 }
1197 if {[regexp {^problem (.*)} $rv dummy problem]} {
8a8d337d 1198 ucmdr {} "The user `$target' $problem."
7ce72032
IJ
1199 } elseif {[regexp {^ok ([^ ]+) ([0-9]+)$} $rv dummy tty idlesince]} {
1200 set idletime [expr {$now - $idlesince}]
1201 set ls $now
1202 ucmdr {} {} {} "invites $target ($tty[expr {
ef177383 1203 $idletime > 10 ? ", idle for [showintervalsecs $idletime 0]" : ""
7ce72032
IJ
1204 }]) to [expr {
1205 [ischan $dest] ? "join us here" : "talk to you"
1206 }]."
1207 } else {
1208 error "unexpected response from userv service: $rv"
1209 }
1210}
1211
a69f7d2c
IJ
1212proc md5sum {value} { exec md5sum << $value }
1213
83dd1224 1214def_ucmd seen {
422f52e4 1215 global lastseen nick
83dd1224
IJ
1216 prefix_nick
1217 set ncase [ta_nick]
1218 set nlower [irctolower $ncase]
422f52e4 1219 ta_nomore
83dd1224
IJ
1220 set now [clock seconds]
1221 if {"$nlower" == "[irctolower $nick]"} {
a01859b6 1222 usererror "I am not self-aware."
83dd1224
IJ
1223 } elseif {![info exists lastseen($nlower)]} {
1224 set rstr "I've never seen $ncase."
422f52e4 1225 } else {
83dd1224
IJ
1226 manyset $lastseen($nlower) realnick time what
1227 set howlong [expr {$now - $time}]
1228 set string [showinterval $howlong]
1229 set rstr "I last saw $realnick $string, $what."
1230 }
1231 if {[ischan $dest]} {
1232 set where $dest
1233 } else {
1234 set where {}
1235 }
1236 upvar #0 lookedfor($nlower) lf
1237 if {[info exists lf]} { set oldvalue $lf } else { set oldvalue {} }
1238 set lf [list [list $now $n $where]]
1239 foreach v $oldvalue {
1240 if {"[irctolower [lindex $v 1]]" == "[irctolower $n]"} continue
1241 lappend lf $v
cc2d31de 1242 }
83dd1224 1243 ucmdr {} $rstr
cc2d31de
IJ
1244}
1245
ebbae0a9
IJ
1246proc lnick_marktime_cancel {luser} {
1247 upvar #0 nick_markid($luser) mi
1248 if {![info exists mi]} return
1249 catch { after cancel $mi }
1250 catch { unset mi }
1251}
1252
1253proc lnick_marktime_doafter {luser why ms} {
1254 lnick_marktime_cancel $luser
1255 upvar #0 nick_markid($luser) mi
1256 set mi [after $ms [list lnick_marktime_now $luser $why]]
1257}
1258
1259proc lnick_marktime_reset {luser} {
1260 set mt [nickdb_get $luser marktime]
1261 if {"$mt" == "off" || "$mt" == "once"} return
1262 lnick_marktime_doafter $luser "Time passes." [expr {$mt*1000}]
1263}
1264
1265proc lnick_marktime_start {luser why ms} {
1266 set mt [nickdb_get $luser marktime]
1267 if {"$mt" == "off"} {
1268 lnick_marktime_cancel $luser
1269 } else {
1270 lnick_marktime_doafter $luser $why $ms
1271 }
1272}
1273
1274proc lnick_marktime_now {luser why} {
1275 upvar #0 nick_onchans($luser) oc
93e9bcff
IJ
1276 global calling_nick
1277 set calling_nick $luser
ebbae0a9
IJ
1278 sendprivmsg $luser [lnick_pingstring $why $oc ""]
1279 lnick_marktime_reset $luser
1280}
1281
1282proc lnick_pingstring {why oc apstring} {
1283 global nick_onchans
1284 catch { exec uptime } uptime
1285 set nnicks [llength [array names nick_onchans]]
1286 if {[regexp \
ea8a1bfe 1287 {^ *([0-9:apm]+) +up.*, +(\d+) users?, +load average: +([0-9., ]+) *$} \
ebbae0a9 1288 $uptime dummy time users load]} {
93e9bcff 1289 regsub -all , $load {} load
ebbae0a9
IJ
1290 set uptime "$time $nnicks/$users $load"
1291 } else {
1292 append uptime ", $nnicks nicks"
1293 }
1294 if {[llength $oc]} {
1295 set best_la 0
1296 set activity quiet
1297 foreach ch $oc {
1298 upvar #0 chan_lastactivity($ch) la
1299 if {![info exists la]} continue
1300 if {$la <= $best_la} continue
ef177383
IJ
1301 set since [showintervalsecs [expr {[clock seconds]-$la}] 1]
1302 set activity "$ch $since"
ebbae0a9
IJ
1303 set best_la $la
1304 }
1305 } else {
1306 set activity unseen
1307 }
1308 set str $why
1309 append str " " $uptime " " $activity
1310 if {[string length $apstring]} { append str " " $apstring }
1311 return $str
1312}
1313
1314def_ucmd ping {
1315 if {[ischan $dest]} {
1316 set oc [irctolower $dest]
1317 } else {
1318 global nick_onchans
1319 prefix_nick
1320 set ln [irctolower $n]
1321 if {[info exists nick_onchans($ln)]} {
1322 set oc $nick_onchans($ln)
1323 } else {
1324 set oc {}
1325 }
1326 if {[llength $oc]} { lnick_marktime_reset $ln }
1327 }
1328 ucmdr {} [lnick_pingstring "Pong!" $oc $text]
1329}
1330
28c8ab78
IJ
1331proc ensure_globalsecret {} {
1332 global globalsecret
1333
1334 if {[info exists globalsecret]} return
1335 set gsfile [open /dev/urandom r]
1336 fconfigure $gsfile -translation binary
1337 set globalsecret [read $gsfile 32]
1338 binary scan $globalsecret H* globalsecret
1339 close $gsfile
1340 unset gsfile
1341}
1342
534e26a9 1343proc connected {} {
534e26a9
IJ
1344 foreach chan [chandb_list] {
1345 if {[chandb_get $chan autojoin]} { dojoin $chan }
1346 }
1347}
1348
28c8ab78 1349ensure_globalsecret
e6cc22dc 1350loadhelp
28c8ab78 1351ensure_connecting