Improve helpinfos for tell. Working on check_telling. Also need to make it be calle...
[ircbot] / bot.tcl
1 # Actual IRC bot code
2
3 set helpfile helpinfos
4
5 source irccore.tcl
6 source parsecmd.tcl
7 source stdhelp.tcl
8
9 defset marktime_min 300
10 defset marktime_join_startdelay 5000
11
12 proc privmsg_unlogged {prefix ischan params} {
13 if {!$ischan ||
14 [regexp {^![a-z][-a-z]*[a-z]( .*)?$} [lindex $params 1]]} {
15 return 0
16 }
17 # on-channel message, ignore
18 set chan [lindex $params 0]
19 upvar #0 chan_lastactivity([irctolower $chan]) la
20 set la [clock seconds]
21 catch_logged { recordlastseen_p $prefix "talking on $chan" 2 }
22 return 1
23 }
24
25 proc showintervalsecs {howlong abbrev} {
26 return [showintervalsecs/[opt timeformat] $howlong $abbrev]
27 }
28
29 proc showintervalsecs/ks {howlong abbrev} {
30 if {$howlong < 1000} {
31 return "${howlong}s"
32 } else {
33 if {$howlong < 1000000} {
34 set pfx k
35 set scale 1000
36 } else {
37 set pfx M
38 set scale 1000000
39 }
40 set value [expr "$howlong.0 / $scale"]
41 foreach {min format} {100 %.0f 10 %.1f 1 %.2f} {
42 if {$value < $min} continue
43 return [format "$format${pfx}s" $value]
44 }
45 }
46 }
47
48 proc format_qty {qty unit abbrev} {
49 set o $qty
50 if {$abbrev} {
51 append o [string range $unit 0 0]
52 } else {
53 append o " "
54 append o $unit
55 if {$qty != 1} { append o s }
56 }
57 return $o
58 }
59
60 proc showintervalsecs/hms {qty abbrev} {
61 set ul {second 60 minute 60 hour 24 day 7 week}
62 set remainv 0
63 while {[llength $ul] > 1 && $qty >= [set uv [lindex $ul 1]]} {
64 set remainu [lindex $ul 0]
65 set remainv [expr {$qty % $uv}]
66 set qty [expr {($qty-$remainv)/$uv}]
67 set ul [lreplace $ul 0 1]
68 }
69 set o [format_qty $qty [lindex $ul 0] $abbrev]
70 if {$remainv} {
71 if {!$abbrev} { append o " " }
72 append o [format_qty $remainv $remainu $abbrev]
73 }
74 return $o
75 }
76
77 proc showinterval {howlong} {
78 if {$howlong <= 0} {
79 return {just now}
80 } else {
81 return "[showintervalsecs $howlong 0] ago"
82 }
83 }
84
85 proc showtime {when} {
86 return [showinterval [expr {[clock seconds] - $when}]]
87 }
88
89 proc parse_interval {specified min} {
90 if {![regexp {^([0-9]+)([a-z]+)$} $specified dummy value unit]} {
91 error "invalid syntax for interval"
92 }
93 switch -exact $unit {
94 s { set u 1 }
95 ks { set u 1000 }
96 m { set u 60 }
97 h { set u 3600 }
98 default { error "unknown unit of time $unit" }
99 }
100 if {$value > 86400*21/$u} { error "interval too large" }
101 set result [expr {$value*$u}]
102 if {$result < $min} { error "interval too small (<${min}s)" }
103 return $result
104 }
105
106 proc def_msgproc {name argl body} {
107 proc msg_$name "varbase $argl" "\
108 upvar #0 msg/\$varbase/dest d\n\
109 upvar #0 msg/\$varbase/str s\n\
110 upvar #0 msg/\$varbase/accum a\n\
111 $body"
112 }
113
114 def_msgproc begin {dest str} {
115 set d $dest
116 set s $str
117 set a {}
118 }
119
120 def_msgproc append {str} {
121 set ns "$s$str"
122 if {[string length $s] && [string length $ns] > 65} {
123 msg__sendout $varbase
124 set s " [string trimleft $str]"
125 } else {
126 set s $ns
127 }
128 }
129
130 def_msgproc finish {} {
131 msg__sendout $varbase
132 unset s
133 unset d
134 return $a
135 }
136
137 def_msgproc _sendout {} {
138 lappend a [string trimright $s]
139 set s {}
140 }
141
142 proc looking_whenwhere {when where} {
143 set str [showtime [expr {$when-1}]]
144 if {[string length $where]} { append str " on $where" }
145 return $str
146 }
147
148 proc check_telling {nl event} {
149 # event is `talk', `act' or `come'
150
151 set iml [msgdb_get $nl inbound]
152 if {![llength $iml]} return
153
154 upvar #0 nick_telling($nl) telling
155 upvar #0 nick_unique($nl) u
156
157 if {[info exists telling]} {
158 manyset $telling u2 stt telling_when
159 if {"$u2" != "$u"} { unset telling; unset stt; unset telling_when }
160 }
161
162 if {![info exists stt]} {
163 set stt norecord
164 set telling_when $now
165 }
166
167 set ago [expr {$now - $telling_when}]
168
169 # evstate is string of letters
170 # event
171 # t talk
172 # a act
173 # c come
174 # current state
175 # n NORECORD
176 # m MENTIONED
177 # p PASSED
178 # security level and timing
179 # ii Insecure
180 # ss Secure and soon (before interval)
181 # sl Secure and late (after interval)
182 # reliability and timing
183 # uu Unreliable
184 # rv Remind, very soon (before within-interval)
185 # rs Remind, soon (between)
186 # rl Remind, late (aftr every-interval)
187 # ps Pester, soon (before interval)
188 # pl Pester, late (after interval)
189 # current identification
190 # i Identified
191 # u Unidentified
192 # current visibility
193 # v Visible
194 # h Hidden (invisible, no unique)
195
196 set evstate [string range $stt 0 0]
197
198 manyset [nickdb_get $n tellsec] sec secwhen
199 switch -exact $sec {
200 insecure { append evstate ii }
201 secure { append evstate [expr {$ago<$secwhen ? "sl" : "ss"}] }
202 default { append evstate "#$sec#" }
203 }
204
205 manyset [nickdb_set $n tellrel] rel relint relwithin
206 switch -exact $rel {
207 unreliable { append evstate uu }
208 remind { append evstate [expr {
209 $ago<$relwithin ? "rv" : $ago<$relint ? "rs" : "rl"
210 }]}
211 pester { append evstate [expr {$ago<$relint ? "ps" : "pl"}] }
212 default { append evstate "#$rel#" }
213 }
214
215 upvar #0 nick_username($nl) nu
216 if {[info exists nu] && "$nu" == "[nickdb_get $nl username]"} {
217 append evstate i
218 } else {
219 append evstate u
220 }
221
222 append evstate [expr {[info exists u] ? "v" : "h"}]
223
224 switch -glob $evstate {
225
226
227
228
229 if {[
230 set security
231
232 switch -exact
233
234 if {![info exists u]} { catch { unset telling } }
235 && [info exists telling]} {
236
237
238 }
239
240 if {![info exists telling]} {
241
242
243 proc recordlastseen_n {n how here} {
244 global lastseen lookedfor
245 set nl [irctolower $n]
246 set now [clock seconds]
247 set lastseen($nl) [list $n $now $how]
248
249 if {!$here} return
250
251 check_telling $nl [lindex {x act talk} $here]
252
253 upvar #0 lookedfor($nl) lf
254 if {[info exists lf]} {
255 switch -exact [llength $lf] {
256 0 {
257 set ml {}
258 }
259 1 {
260 manyset [lindex $lf 0] when who where
261 set ml [list \
262 "FYI, $who was looking for you [looking_whenwhere $when $where]."]
263 }
264 default {
265 msg_begin tosend $n "FYI, people have been looking for you:"
266 set i 0
267 set fin ""
268 foreach e $lf {
269 incr i
270 if {$i == 1} {
271 msg_append tosend " "
272 } elseif {$i == [llength $lf]} {
273 msg_append tosend " and "
274 set fin .
275 } else {
276 msg_append tosend ", "
277 }
278 manyset $e when who where
279 msg_append tosend \
280 "$who ([looking_whenwhere $when $where])$fin"
281 }
282 set ml [msg_finish tosend]
283 }
284 }
285 unset lf
286 msendprivmsg_delayed 1000 $n $ml
287 }
288 }
289
290 proc note_topic {showoff whoby topic} {
291 set msg "FYI, $whoby has changed the topic on $showoff"
292 if {[string length $topic] < 160} {
293 append msg " to $topic"
294 } else {
295 append msg " but it is too long to reproduce here !"
296 }
297 set showoff [irctolower $showoff]
298 set tell [chandb_get $showoff topictell]
299 if {[lsearch -exact $tell *] >= 0} {
300 set tryspies [chandb_list]
301 } else {
302 set tryspies $tell
303 }
304 foreach spy $tryspies {
305 set see [chandb_get $spy topicsee]
306 if {[lsearch -exact $see $showoff] >= 0 || \
307 ([lsearch -exact $see *] >= 0 && \
308 [lsearch -exact $tell $spy] >= 0)} {
309 sendprivmsg $spy $msg
310 }
311 }
312 }
313
314 proc recordlastseen_p {p how here} {
315 prefix_nick
316 recordlastseen_n $n $how $here
317 }
318
319 proc chanmode_arg {} {
320 upvar 2 args cm_args
321 set rv [lindex $cm_args 0]
322 set cm_args [lreplace cm_args 0 0]
323 return $rv
324 }
325
326 proc chanmode_o1 {m g p chan} {
327 global nick chan_initialop
328 prefix_nick
329 set who [chanmode_arg]
330 recordlastseen_n $n "being nice to $who" 1
331 if {"[irctolower $who]" == "[irctolower $nick]"} {
332 set nlower [irctolower $n]
333 upvar #0 nick_unique($nlower) u
334 if {[chandb_exists $chan]} {
335 sendprivmsg $n Thanks.
336 } elseif {![info exists u]} {
337 sendprivmsg $n {Op me while not on the channel, why don't you ?}
338 } else {
339 set chan_initialop([irctolower $chan]) $u
340 sendprivmsg $n \
341 "Thanks. You can use `channel manager ...' to register this channel."
342 if {![nickdb_exists $n] || ![string length [nickdb_get $n username]]} {
343 sendprivmsg $n \
344 "(But to do that you must register your nick securely first.)"
345 }
346 }
347 }
348 }
349
350 proc chanmode_o0 {m g p chan} {
351 global nick chandeop
352 prefix_nick
353 set who [chanmode_arg]
354 recordlastseen_p $p "being mean to $who" 1
355 if {"[irctolower $who]" == "[irctolower $nick]"} {
356 set chandeop($chan) [list [clock seconds] $p]
357 }
358 }
359
360 proc msg_MODE {p c dest modelist args} {
361 if {![ischan $dest]} return
362 if {[regexp {^\-(.+)$} $modelist dummy modelist]} {
363 set give 0
364 } elseif {[regexp {^\+(.+)$} $modelist dummy modelist]} {
365 set give 1
366 } else {
367 error "invalid modelist"
368 }
369 foreach m [split $modelist] {
370 set procname chanmode_$m$give
371 if {[catch { info body $procname }]} {
372 recordlastseen_p $p "fiddling with $dest" 1
373 } else {
374 $procname $m $give $p $dest
375 }
376 }
377 }
378
379 proc leaving {lchan} {
380 foreach luser [array names nick_onchans] {
381 upvar #0 nick_onchans($luser) oc
382 set oc [grep tc {"$tc" != "$lchan"} $oc]
383 }
384 upvar #0 chan_nicks($lchan) nlist
385 unset nlist
386 upvar #0 chan_lastactivity($lchan) la
387 catch { unset la }
388 }
389
390 proc doleave {lchan} {
391 sendout PART $lchan
392 leaving $lchan
393 }
394
395 proc dojoin {lchan} {
396 global chan_nicks
397 sendout JOIN $lchan
398 set chan_nicks($lchan) {}
399 }
400
401 proc check_justme {lchan} {
402 global nick
403 upvar #0 chan_nicks($lchan) nlist
404 if {[llength $nlist] != 1} return
405 if {"[lindex $nlist 0]" != "[irctolower $nick]"} return
406 if {[chandb_exists $lchan]} {
407 set mode [chandb_get $lchan mode]
408 if {"$mode" != "*"} {
409 sendout MODE $lchan $mode
410 }
411 set topic [chandb_get $lchan topicset]
412 if {[string length $topic]} {
413 sendout TOPIC $lchan $topic
414 }
415 } else {
416 doleave $lchan
417 }
418 }
419
420 proc process_kickpart {chan user} {
421 global nick
422 check_nick $user
423 set luser [irctolower $user]
424 set lchan [irctolower $chan]
425 if {![ischan $chan]} { error "not a channel" }
426 if {"$luser" == "[irctolower $nick]"} {
427 leaving $lchan
428 } else {
429 upvar #0 nick_onchans($luser) oc
430 upvar #0 chan_nicks($lchan) nlist
431 set oc [grep tc {"$tc" != "$lchan"} $oc]
432 set nlist [grep tn {"$tn" != "$luser"} $nlist]
433 nick_case $user
434 if {![llength $oc]} {
435 nick_forget $luser
436 } else {
437 check_justme $lchan
438 }
439 }
440 }
441
442 proc msg_TOPIC {p c dest topic} {
443 prefix_nick
444 if {![ischan $dest]} return
445 recordlastseen_n $n "changing the topic on $dest" 1
446 note_topic [irctolower $dest] $n $topic
447 }
448
449 proc msg_KICK {p c chans users comment} {
450 set chans [split $chans ,]
451 set users [split $users ,]
452 if {[llength $chans] > 1} {
453 foreach chan $chans user $users { process_kickpart $chan $user }
454 } else {
455 foreach user $users { process_kickpart [lindex $chans 0] $user }
456 }
457 }
458
459 proc msg_KILL {p c user why} {
460 nick_forget $user
461 }
462
463 set nick_counter 0
464 set nick_arys {onchans username unique}
465 # nick_onchans($luser) -> [list ... $lchan ...]
466 # nick_username($luser) -> <securely known local username>
467 # nick_unique($luser) -> <includes-counter>
468 # nick_case($luser) -> $user (valid even if no longer visible)
469 # nick_markid($luser) -> <after id for marktime>
470 # nick_telling($luser) -> <unique> mentioned|passed <when>
471
472 # chan_nicks($lchan) -> [list ... $luser ...]
473 # chan_lastactivity($lchan) -> [clock seconds]
474
475 proc lnick_forget {luser} {
476 global nick_arys chan_nicks
477 lnick_marktime_cancel $luser
478 foreach ary $nick_arys {
479 upvar #0 nick_${ary}($luser) av
480 catch { unset av }
481 }
482 foreach lch [array names chan_nicks] {
483 upvar #0 chan_nicks($lch) nlist
484 set nlist [grep tn {"$tn" != "$luser"} $nlist]
485 check_justme $lch
486 }
487 }
488
489 proc nick_forget {user} {
490 global nick_arys chan_nicks
491 lnick_forget [irctolower $user]
492 nick_case $user
493 }
494
495 proc nick_case {user} {
496 global nick_case
497 set nick_case([irctolower $user]) $user
498 }
499
500 proc msg_NICK {p c newnick} {
501 global nick_arys nick_case calling_nick
502 prefix_nick
503 recordlastseen_n $n "changing nicks to $newnick" 0
504 set calling_nick $newnick
505 recordlastseen_n $newnick "changing nicks from $n" 1
506 set luser [irctolower $n]
507 lnick_marktime_cancel $luser
508 set lusernew [irctolower $newnick]
509 foreach ary $nick_arys {
510 upvar #0 nick_${ary}($luser) old
511 upvar #0 nick_${ary}($lusernew) new
512 if {[info exists new]} { error "nick collision ?! $ary $n $newnick" }
513 if {[info exists old]} { set new $old; unset old }
514 }
515 upvar #0 nick_onchans($lusernew) oc
516 foreach ch $oc {
517 upvar #0 chan_nicks($ch) nlist
518 set nlist [grep tn {"$tn" != "$luser"} $nlist]
519 lappend nlist $lusernew
520 }
521 lnick_marktime_start $lusernew "Hi." 500 1
522 nick_case $newnick
523 }
524
525 proc nick_ishere {n} {
526 global nick_counter
527 upvar #0 nick_unique([irctolower $n]) u
528 if {![info exists u]} { set u [incr nick_counter].$n.[clock seconds] }
529 nick_case $n
530 }
531
532 proc msg_JOIN {p c chan} {
533 prefix_nick
534 nick_ishere $n
535 recordlastseen_n $n "joining $chan" 1
536 set nl [irctolower $n]
537 set lchan [irctolower $chan]
538 upvar #0 nick_onchans($nl) oc
539 upvar #0 chan_nicks($lchan) nlist
540 if {![info exists oc]} {
541 global marktime_join_startdelay
542 lnick_marktime_start $nl "Welcome." $marktime_join_startdelay 1
543 }
544 lappend oc $lchan
545 lappend nlist $nl
546 }
547 proc msg_PART {p c chan args} {
548 prefix_nick
549 set msg "leaving $chan"
550 if {[llength $args]} {
551 set why [lindex $args 0]
552 if {"[irctolower $why]" != "[irctolower $n]"} { append msg " ($why)" }
553 }
554 recordlastseen_n $n $msg 1
555 process_kickpart $chan $n
556 }
557 proc msg_QUIT {p c why} {
558 prefix_nick
559 recordlastseen_n $n "leaving ($why)" 0
560 nick_forget $n
561 }
562
563 proc msg_PRIVMSG {p c dest text} {
564 global errorCode
565
566 prefix_nick
567 if {[ischan $dest]} {
568 recordlastseen_n $n "invoking me in $dest" 1
569 set output $dest
570 } else {
571 recordlastseen_n $n "talking to me" 1
572 set output $n
573 }
574 nick_case $n
575
576 execute_usercommand $p $c $n $output $dest $text
577 }
578
579 proc msg_INVITE {p c n chan} {
580 after 1000 [list dojoin [irctolower $chan]]
581 }
582
583 proc grep {var predicate list} {
584 set o {}
585 upvar 1 $var v
586 foreach v $list {
587 if {[uplevel 1 [list expr $predicate]]} { lappend o $v }
588 }
589 return $o
590 }
591
592 proc msg_353 {p c dest type chan nicklist} {
593 global names_chans nick_onchans
594 set lchan [irctolower $chan]
595 upvar #0 chan_nicks($lchan) nlist
596 lappend names_chans $lchan
597 if {![info exists nlist]} {
598 # We don't think we're on this channel, so ignore it !
599 # Unfortunately, because we don't get a reply to PART,
600 # we have to remember ourselves whether we're on a channel,
601 # and ignore stuff if we're not, to avoid races. Feh.
602 return
603 }
604 set nlist_new {}
605 foreach user [split $nicklist { }] {
606 regsub {^[@+]} $user {} user
607 if {![string length $user]} continue
608 check_nick $user
609 set luser [irctolower $user]
610 upvar #0 nick_onchans($luser) oc
611 lappend oc $lchan
612 lappend nlist_new $luser
613 nick_ishere $user
614 }
615 set nlist $nlist_new
616 }
617
618 proc msg_366 {p c args} {
619 global names_chans nick_onchans
620 set lchan [irctolower $c]
621 foreach luser [array names nick_onchans] {
622 upvar #0 nick_onchans($luser) oc
623 if {[llength names_chans] > 1} {
624 set oc [grep tc {[lsearch -exact $tc $names_chans] >= 0} $oc]
625 }
626 if {![llength $oc]} { lnick_forget $n }
627 }
628 unset names_chans
629 }
630
631 proc check_username {target} {
632 if {
633 [string length $target] > 8 ||
634 [regexp {[^-0-9a-z]} $target] ||
635 ![regexp {^[a-z]} $target]
636 } { error "invalid username" }
637 }
638
639 proc somedb__head {} {
640 uplevel 1 {
641 set idl [irctolower $id]
642 upvar #0 ${nickchan}db($idl) ndbe
643 binary scan $idl H* idh
644 set idfn $fprefix$idh
645 if {![info exists iddbe] && [file exists $idfn]} {
646 set f [open $idfn r]
647 try_except_finally { set newval [read $f] } {} { close $f }
648 if {[llength $newval] % 2} { error "invalid length" }
649 set iddbe $newval
650 }
651 }
652 }
653
654 proc def_somedb {name arglist body} {
655 foreach {nickchan fprefix} {
656 nick users/n
657 chan chans/c
658 msgs users/m
659 } {
660 proc ${nickchan}db_$name $arglist \
661 "set nickchan $nickchan; set fprefix $fprefix; $body"
662 }
663 }
664
665 def_somedb list {} {
666 set list {}
667 foreach path [glob -nocomplain -path $fprefix *] {
668 binary scan $path "A[string length $fprefix]A*" afprefix thinghex
669 if {"$afprefix" != "$fprefix"} { error "wrong prefix $path $afprefix" }
670 lappend list [binary format H* $thinghex]
671 }
672 return $list
673 }
674
675 proc def_somedb_id {name arglist body} {
676 def_somedb $name [concat id $arglist] "somedb__head; $body"
677 }
678
679 def_somedb_id exists {} {
680 return [info exists iddbe]
681 }
682
683 def_somedb_id delete {} {
684 catch { unset iddbe }
685 file delete $idfn
686 }
687
688 set default_settings_nick {
689 timeformat ks
690 marktime off
691 tellsec insecure
692 tellrel {remind 3600 30}
693 }
694
695 set default_settings_chan {
696 autojoin 1
697 mode *
698 userinvite pub
699 topicset {}
700 topicsee {}
701 topictell {}
702 }
703
704 set default_settings_msgs {
705 inbound {}
706 outbound {}
707 }
708 # inbound -> [<nick> <time_t> <message>] ...
709 # outbound -> [<nick> <time_t(earliest)> <count>] ...
710
711 def_somedb_id set {args} {
712 upvar #0 default_settings_$nickchan def
713 if {![info exists iddbe]} { set iddbe $def }
714 foreach {key value} [concat $iddbe $args] { set a($key) $value }
715 set newval {}
716 foreach {key value} [array get a] { lappend newval $key $value }
717 set f [open $idfn.new w]
718 try_except_finally {
719 puts $f $newval
720 close $f
721 file rename -force $idfn.new $idfn
722 } {
723 } {
724 catch { close $f }
725 }
726 set iddbe $newval
727 }
728
729 def_somedb_id get {key} {
730 upvar #0 default_settings_$nickchan def
731 if {[info exists iddbe]} {
732 set l [concat $iddbe $def]
733 } else {
734 set l $def
735 }
736 foreach {tkey value} $l {
737 if {"$tkey" == "$key"} { return $value }
738 }
739 error "unset setting $key"
740 }
741
742 proc opt {key} {
743 global calling_nick
744 if {[info exists calling_nick]} { set n $calling_nick } { set n {} }
745 return [nickdb_get $n $key]
746 }
747
748 proc check_notonchan {} {
749 upvar 1 dest dest
750 if {[ischan $dest]} { usererror "That command must be sent privately." }
751 }
752
753 proc nick_securitycheck {strict} {
754 upvar 1 n n
755 if {![nickdb_exists $n]} {
756 usererror "You are unknown to me, use `register'."
757 }
758 set wantu [nickdb_get $n username]
759 if {![string length $wantu]} {
760 if {$strict} {
761 usererror "That feature is only available to secure users, sorry."
762 } else {
763 return
764 }
765 }
766 set luser [irctolower $n]
767 upvar #0 nick_username($luser) nu
768 if {![info exists nu]} {
769 usererror "Nick $n is secure, you must identify yourself first."
770 }
771 if {"$wantu" != "$nu"} {
772 usererror "You are the wrong user -\
773 the nick $n belongs to $wantu, not $nu."
774 }
775 }
776
777 proc channel_ismanager {channel n} {
778 set mgrs [chandb_get $channel managers]
779 return [expr {[lsearch -exact [irctolower $mgrs] [irctolower $n]] >= 0}]
780 }
781
782 proc channel_securitycheck {channel} {
783 upvar n n
784 if {![channel_ismanager $channel $n]} {
785 usererror "You are not a manager of $channel."
786 }
787 nick_securitycheck 1
788 }
789
790 proc def_chancmd {name body} {
791 proc channel/$name {} \
792 " upvar 1 target chan; upvar 1 n n; upvar 1 text text; $body"
793 }
794
795 proc ta_listop {findnow procvalue} {
796 # findnow and procvalue are code fragments which will be executed
797 # in the caller's level. findnow should set ta_listop_ev to
798 # the current list, and procvalue should treat ta_listop_ev as
799 # a proposed value in the list and check and possibly modify
800 # (canonicalise?) it. After ta_listop, ta_listop_ev will
801 # be the new value of the list.
802 upvar 1 ta_listop_ev exchg
803 upvar 1 text text
804 set opcode [ta_word]
805 switch -exact _$opcode {
806 _= { }
807 _+ - _- {
808 uplevel 1 $findnow
809 foreach item $exchg { set array($item) 1 }
810 }
811 default {
812 error "list change opcode must be one of + - ="
813 }
814 }
815 foreach exchg [split $text " "] {
816 if {![string length $exchg]} continue
817 uplevel 1 $procvalue
818 if {"$opcode" != "-"} {
819 set array($exchg) 1
820 } else {
821 catch { unset array($exchg) }
822 }
823 }
824 set exchg [lsort [array names array]]
825 }
826
827 def_chancmd manager {
828 ta_listop {
829 if {[chandb_exists $chan]} {
830 set ta_listop_ev [chandb_get $chan managers]
831 } else {
832 set ta_listop_ev [list [irctolower $n]]
833 }
834 } {
835 check_nick $ta_listop_ev
836 set ta_listop_ev [irctolower $ta_listop_ev]
837 }
838 if {[llength $ta_listop_ev]} {
839 chandb_set $chan managers $ta_listop_ev
840 ucmdr "Managers of $chan: $ta_listop_ev" {}
841 } else {
842 chandb_delete $chan
843 ucmdr {} {} "forgets about managing $chan." {}
844 }
845 }
846
847 def_chancmd autojoin {
848 set yesno [ta_word]
849 switch -exact [string tolower $yesno] {
850 no { set nv 0 }
851 yes { set nv 1 }
852 default { error "channel autojoin must be `yes' or `no' }
853 }
854 chandb_set $chan autojoin $nv
855 ucmdr [expr {$nv ? "I will join $chan when I'm restarted " : \
856 "I won't join $chan when I'm restarted "}] {}
857 }
858
859 def_chancmd userinvite {
860 set nv [string tolower [ta_word]]
861 switch -exact $nv {
862 pub { set txt "!invite will work for $chan, but it won't work by /msg" }
863 here { set txt "!invite and /msg invite will work, but only for users who are already on $chan." }
864 all { set txt "Any user will be able to invite themselves or anyone else to $chan." }
865 none { set txt "I will not invite anyone to $chan." }
866 default {
867 error "channel userinvite must be `pub', `here', `all' or `none'
868 }
869 }
870 chandb_set $chan userinvite $nv
871 ucmdr $txt {}
872 }
873
874 def_chancmd topic {
875 set what [ta_word]
876 switch -exact $what {
877 leave {
878 ta_nomore
879 chandb_set $chan topicset {}
880 ucmdr "I won't ever change the topic of $chan." {}
881 }
882 set {
883 set t [string trim $text]
884 if {![string length $t]} {
885 error "you must specific the topic to set"
886 }
887 chandb_set $chan topicset $t
888 ucmdr "Whenever I'm alone on $chan, I'll set the topic to $t." {}
889 }
890 see - tell {
891 ta_listop {
892 set ta_listop_ev [chandb_get $chan topic$what]
893 } {
894 if {"$ta_listop_ev" != "*"} {
895 if {![ischan $ta_listop_ev]} {
896 error "bad channel \`$ta_listop_ev' in topic $what"
897 }
898 set ta_listop_ev [irctolower $ta_listop_ev]
899 }
900 }
901 chandb_set $chan topic$what $ta_listop_ev
902 ucmdr "Topic $what list for $chan: $ta_listop_ev" {}
903 }
904 default {
905 usererror "Unknown channel topic subcommand - see help channel."
906 }
907 }
908 }
909
910 def_chancmd mode {
911 set mode [ta_word]
912 if {"$mode" != "*" && ![regexp {^(([-+][imnpst]+)+)$} $mode mode]} {
913 error {channel mode must be * or match ([-+][imnpst]+)+}
914 }
915 chandb_set $chan mode $mode
916 if {"$mode" == "*"} {
917 ucmdr "I won't ever change the mode of $chan." {}
918 } else {
919 ucmdr "Whenever I'm alone on $chan, I'll set the mode to $mode." {}
920 }
921 }
922
923 def_chancmd show {
924 if {[chandb_exists $chan]} {
925 set l "Settings for $chan: autojoin "
926 append l [lindex {no yes} [chandb_get $chan autojoin]]
927 append l ", mode " [chandb_get $chan mode]
928 append l ", userinvite " [chandb_get $chan userinvite] "."
929 append l "\nManagers: "
930 append l [join [chandb_get $chan managers] " "]
931 foreach {ts sep} {see "\n" tell " "} {
932 set t [chandb_get $chan topic$ts]
933 append l $sep
934 if {[llength $t]} {
935 append l "Topic $ts list: $t."
936 } else {
937 append l "Topic $ts list is empty."
938 }
939 }
940 append l "\n"
941 set t [chandb_get $chan topicset]
942 if {[string length $t]} {
943 append l "Topic to set: $t"
944 } else {
945 append l "I will not change the topic."
946 }
947 ucmdr {} $l
948 } else {
949 ucmdr {} "The channel $chan is not managed."
950 }
951 }
952
953 proc channelmgr_monoop {} {
954 upvar 1 dest dest
955 upvar 1 text text
956 upvar 1 n n
957 upvar 1 p p
958 upvar 1 target target
959 global chan_nicks
960
961 prefix_nick
962
963 if {[ischan $dest]} { set target $dest }
964 if {[ta_anymore]} { set target [ta_word] }
965 ta_nomore
966 if {![info exists target]} {
967 usererror "You must specify, or invoke me on, the relevant channel."
968 }
969 if {![info exists chan_nicks([irctolower $target])]} {
970 usererror "I am not on $target."
971 }
972 if {![ischan $target]} { error "not a valid channel" }
973
974 if {![chandb_exists $target]} {
975 usererror "$target is not a managed channel."
976 }
977 channel_securitycheck $target
978 }
979
980 def_ucmd op {
981 channelmgr_monoop
982 sendout MODE $target +o $n
983 }
984
985 def_ucmd leave {
986 channelmgr_monoop
987 doleave $target
988 }
989
990 def_ucmd invite {
991 global chan_nicks errorCode errorInfo
992 prefix_nick
993
994 if {[ischan $dest]} {
995 set target $dest
996 set onchan 1
997 } else {
998 set target [ta_word]
999 set onchan 0
1000 }
1001 set ltarget [irctolower $target]
1002 if {![ischan $target]} { error "$target is not a channel" }
1003 if {![info exists chan_nicks($ltarget)]} {
1004 usererror "I am not on $target."
1005 }
1006 set ui [chandb_get $ltarget userinvite]
1007 if {[catch {
1008 if {"$ui" == "pub" && !$onchan} {
1009 usererror "Invitations to $target must be made there with !invite."
1010 }
1011 if {"$ui" != "all"} {
1012 if {[lsearch -exact $chan_nicks($ltarget) [irctolower $n]] < 0} {
1013 usererror "Invitations to $target may only be made\
1014 by a user on the channel."
1015 }
1016 }
1017 if {"$ui" == "none"} {
1018 usererror "Sorry, I've not been authorised\
1019 to invite people to $target."
1020 }
1021 } emsg]} {
1022 if {"$errorCode" == "BLIGHT USER" && [channel_ismanager $target $n]} {
1023 if {[catch {
1024 nick_securitycheck 1
1025 } emsg2]} {
1026 if {"$errorCode" == "BLIGHT USER"} {
1027 usererror "$emsg2 Therefore you can't use your\
1028 channel manager privilege. $emsg"
1029 } else {
1030 error $error $errorInfo $errorCode
1031 }
1032 }
1033 } else {
1034 error $emsg $errorInfo $errorCode
1035 }
1036 }
1037 if {![ta_anymore]} {
1038 usererror "You have to say who to invite."
1039 }
1040 set invitees {}
1041 while {[ta_anymore]} {
1042 set invitee [ta_word]
1043 check_nick $invitee
1044 lappend invitees $invitee
1045 }
1046 foreach invitee $invitees {
1047 sendout INVITE $invitee $ltarget
1048 }
1049 set who [lindex $invitees 0]
1050 switch -exact llength $invitees {
1051 0 { error "zero invitees" }
1052 1 { }
1053 2 { append who " and [lindex $invitees 1]" }
1054 * {
1055 set who [join [lreplace $invitees end end] ", "]
1056 append who " and [lindex $invitees [llength $invitees]]"
1057 }
1058 }
1059 ucmdr {} {} {} "invites $who to $target."
1060 }
1061
1062 def_ucmd channel {
1063 if {[ischan $dest]} { set target $dest }
1064 if {![ta_anymore]} {
1065 set subcmd show
1066 } else {
1067 set subcmd [ta_word]
1068 }
1069 if {[ischan $subcmd]} {
1070 set target $subcmd
1071 if {![ta_anymore]} {
1072 set subcmd show
1073 } else {
1074 set subcmd [ta_word]
1075 }
1076 }
1077 if {![info exists target]} { error "privately, you must specify a channel" }
1078 set procname channel/$subcmd
1079 if {"$subcmd" != "show"} {
1080 if {[catch { info body $procname }]} {
1081 usererror "unknown channel setting $subcmd."
1082 }
1083 prefix_nick
1084 if {[chandb_exists $target]} {
1085 channel_securitycheck $target
1086 } else {
1087 nick_securitycheck 1
1088 upvar #0 chan_initialop([irctolower $target]) io
1089 upvar #0 nick_unique([irctolower $n]) u
1090 if {![info exists io]} {
1091 usererror "$target is not a managed channel."
1092 }
1093 if {"$io" != "$u"} {
1094 usererror "You are not the interim manager of $target."
1095 }
1096 if {"$subcmd" != "manager"} {
1097 usererror "Please use `channel manager' first."
1098 }
1099 }
1100 }
1101 channel/$subcmd
1102 }
1103
1104 def_ucmd tell {
1105 global nick_case ownmailaddr ownfullname
1106
1107 prefix_nick
1108 set target [ta_word]
1109 if {![string length $text]} { error "tell them what?" }
1110
1111 set ltarget [irctolower $target]
1112 set ctarget $target
1113 if {[info exists nick_case($ltarget)]} { set ctarget $nick_case($ltarget) }
1114
1115 manyset [nickdb_get $target tellsec] sec mailtoint mailwhy
1116 manyset [nickdb_get $target tellrel] rel relint relwithin
1117 switch -exact $sec {
1118 insecure - secure {
1119 set now [clock seconds]
1120 set inbound [msgsdb_get $ltarget inbound]
1121 lappend inbound $n $now $text
1122 msgsdb_set $ltarget inbound $inbound
1123
1124 set outbound [msgsdb_get $n outbound]
1125 set noutbound {}
1126 set found 0
1127 foreach {recip time count} $outbound {
1128 if {"[irctolower $recip]" == "$ltarget"} {
1129 incr count
1130 set recip $ctarget
1131 set found 1
1132 }
1133 lappend noutbound $recip $time $count
1134 }
1135 if {!$found} {
1136 lappend noutbound $ctarget $now 1
1137 }
1138 msgsdb_set $n outbound $noutbound
1139 set msg "OK, I'll tell $ctarget"
1140 if {$found} { append msg " that too" }
1141 append msg ", "
1142 if {"$sec" != "secure"} {
1143 switch -exact $rel {
1144 unreliable { append msg "neither reliably nor securely" }
1145 remind { append msg "pretty reliably, but not securely" }
1146 pester { append msg "reliably but not securely" }
1147 }
1148 } else {
1149 switch -exact $rel {
1150 unreliable { append msg "securely but not reliably" }
1151 remind { append msg "securely and pretty reliably" }
1152 pester { append msg "reliably and securely" }
1153 }
1154 }
1155 append msg .
1156 ucmdr $msg {}
1157 }
1158 mailto {
1159 set fmtmsg [exec fmt << " $text"]
1160 exec /usr/sbin/sendmail -odb -oi -t -oee -f $mailwhy \
1161 > /dev/null << \
1162 "From: $ownmailaddr ($ownfullname)
1163 To: $mailtoint
1164 Subject: IRC tell from $n
1165
1166 $n asked me[expr {[ischan $dest] ? " on $dest" : ""}] to tell you:
1167 [exec fmt << " $text"]
1168
1169 (This message was for your nick $ctarget; your account $mailwhy
1170 arranged for it to be forwarded to $mailtoint.)
1171 "
1172 ucmdr \
1173 "I've mailed $ctarget, which is what they prefer." \
1174 {}
1175 }
1176 refuse {
1177 usererror "Sorry, $ctarget does not want me to take messages."
1178 }
1179 default {
1180 error "bad tellsec $sec"
1181 }
1182 }
1183 }
1184
1185 def_ucmd who {
1186 if {[ta_anymore]} {
1187 set target [ta_word]; ta_nomore
1188 set myself 1
1189 } else {
1190 prefix_nick
1191 set target $n
1192 set myself [expr {"$target" != "$n"}]
1193 }
1194 set ltarget [irctolower $target]
1195 upvar #0 nick_case($ltarget) ctarget
1196 set nshow $target
1197 if {[info exists ctarget]} {
1198 upvar #0 nick_onchans($ltarget) oc
1199 upvar #0 nick_username($ltarget) nu
1200 if {[info exists oc]} { set nshow $ctarget }
1201 }
1202 if {![nickdb_exists $ltarget]} {
1203 set ol "$nshow is not a registered nick."
1204 } elseif {[string length [set username [nickdb_get $target username]]]} {
1205 set ol "The nick $nshow belongs to the user $username."
1206 } else {
1207 set ol "The nick $nshow is registered (but not to a username)."
1208 }
1209 if {![info exists ctarget] || ![info exists oc]} {
1210 if {$myself} {
1211 append ol "\nI can't see $nshow on anywhere."
1212 } else {
1213 append ol "\nYou aren't on any channels with me."
1214 }
1215 } elseif {![info exists nu]} {
1216 append ol "\n$nshow has not identified themselves."
1217 } elseif {![info exists username]} {
1218 append ol "\n$nshow has identified themselves as the user $nu."
1219 } elseif {"$nu" != "$username"} {
1220 append ol "\nHowever, $nshow is being used by the user $nu."
1221 } else {
1222 append ol "\n$nshow has identified themselves to me."
1223 }
1224 ucmdr {} $ol
1225 }
1226
1227 def_ucmd register {
1228 prefix_nick
1229 check_notonchan
1230 set old [nickdb_exists $n]
1231 if {$old} { nick_securitycheck 0 }
1232 set luser [irctolower $n]
1233 switch -exact [string tolower [string trim $text]] {
1234 {} {
1235 upvar #0 nick_username($luser) nu
1236 if {![info exists nu]} {
1237 ucmdr {} \
1238 "You must identify yourself before using `register'. See `help identify', or use `register insecure'."
1239 }
1240 nickdb_set $n username $nu
1241 ucmdr {} {} "makes a note of your username." {}
1242 }
1243 delete {
1244 nickdb_delete $n
1245 ucmdr {} {} "forgets your nickname." {}
1246 }
1247 insecure {
1248 nickdb_set $n username {}
1249 if {$old} {
1250 ucmdr {} "Security is now disabled for your nickname !"
1251 } else {
1252 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."
1253 }
1254 }
1255 default {
1256 error "you mean register / register delete / register insecure"
1257 }
1258 }
1259 }
1260
1261 proc timeformat_desc {tf} {
1262 switch -exact $tf {
1263 ks { return "Times will be displayed in seconds or kiloseconds." }
1264 hms { return "Times will be displayed in hours, minutes, etc." }
1265 default { error "invalid timeformat: $v" }
1266 }
1267 }
1268
1269 set settings {}
1270 proc def_setting {opt show_body set_body} {
1271 global settings
1272 lappend settings $opt
1273 proc set_show/$opt {} "
1274 upvar 1 n n
1275 set opt $opt
1276 $show_body"
1277 if {![string length $set_body]} return
1278 proc set_set/$opt {} "
1279 upvar 1 n n
1280 upvar 1 text text
1281 set opt $opt
1282 $set_body"
1283 }
1284
1285 proc tellme_sec_desc {v} {
1286 manyset $v sec mailtoint
1287 switch -exact $sec {
1288 insecure {
1289 return "I'll tell you your messages whenever I see you."
1290 }
1291 secure {
1292 return \
1293 "I'll keep the bodies of your messages private until you identify yourself, reminding you every [showintervalsecs $mailtoint 1]."
1294 }
1295 refuse {
1296 return "I shan't accept messages for you."
1297 }
1298 mailto {
1299 return "I'll forward your messages by email to $mailtoint."
1300 }
1301 default {
1302 error "bad tellsec $sec"
1303 }
1304 }
1305 }
1306
1307 proc tellme_rel_desc {v} {
1308 manyset $v rel every within
1309 switch -exact $rel {
1310 unreliable {
1311 return "As soon as I've told you, I'll forget the message - note that this means messages can get lost !"
1312 }
1313 pester {
1314 set u {}
1315 }
1316 remind {
1317 set u ", or talk on channel within [showintervalsecs $within 1] of me having told you"
1318 }
1319 default {
1320 error "bad tellrel $rel"
1321 }
1322 }
1323 return "I'll remind you every [showintervalsecs $every 1] until you say delmsg$u."
1324 }
1325
1326 def_setting timeformat {
1327 set tf [nickdb_get $n timeformat]
1328 return "$tf: [timeformat_desc $tf]"
1329 } {
1330 set tf [string tolower [ta_word]]
1331 ta_nomore
1332 set desc [timeformat_desc $tf]
1333 nickdb_set $n timeformat $tf
1334 ucmdr {} $desc
1335 }
1336
1337 proc marktime_desc {mt} {
1338 if {"$mt" == "off"} {
1339 return "I will not send you periodic messages."
1340 } elseif {"$mt" == "once"} {
1341 return "I will send you one informational message when I see you."
1342 } else {
1343 return "I'll send you a message every [showintervalsecs $mt 0]."
1344 }
1345 }
1346
1347 def_setting marktime {
1348 set mt [nickdb_get $n marktime]
1349 set p $mt
1350 if {[string match {[0-9]*} $mt]} { append p s }
1351 append p ": "
1352 append p [marktime_desc $mt]
1353 return $p
1354 } {
1355 global marktime_min
1356 set mt [string tolower [ta_word]]
1357 ta_nomore
1358
1359 if {"$mt" == "off" || "$mt" == "once"} {
1360 } else {
1361 set mt [parse_interval $mt $marktime_min]
1362 }
1363 nickdb_set $n marktime $mt
1364 lnick_marktime_start [irctolower $n] "So:" 500 0
1365 ucmdr {} [marktime_desc $mt]
1366 }
1367
1368 def_setting security {
1369 set s [nickdb_get $n username]
1370 if {[string length $s]} {
1371 return "Your nick, $n, is controlled by the user $s."
1372 } else {
1373 return "Your nick, $n, is not secure."
1374 }
1375 } {}
1376
1377 def_setting tellme {
1378 set secv [nickdb_get $n tellsec]
1379 set ms [tellme_sec_desc $secv]
1380 manyset $secv sec
1381 switch -exact $sec {
1382 insecure - secure {
1383 set mr [tellme_rel_desc [nickdb_get $n tellrel]]
1384 return "$ms $mr"
1385 }
1386 refuse - mailto {
1387 return $ms
1388 }
1389 }
1390 } {
1391 set setting [string tolower [ta_word]]
1392 switch -exact $setting {
1393 insecure - secure - refuse {
1394 ta_nomore
1395 if {"$setting" == "refuse" && [llength [msgsdb_get $n inbound]]} {
1396 usererror "You must delete the messages you have, first."
1397 }
1398 set sr sec
1399 set v $setting
1400 }
1401 mailto {
1402 set u [nickdb_get $n username]
1403 if {![string length $u]} {
1404 usererror "Sorry, you must register secure to have your messages mailed (to prevent the use of this feature for spamming)."
1405 }
1406 set sr sec
1407 set v [list mailto [ta_word] $u]
1408 }
1409 unreliable - pester - remind {
1410 manyset [nickdb_get $n tellsec] sec
1411 switch -exact $sec {
1412 refuse - mailto {
1413 error "can't change message delivery conditions when message disposition prevents messages from being left"
1414 }
1415 }
1416 set sr rel
1417 set v $setting
1418 if {"$setting" != "unreliable"} {
1419 set every [parse_interval [ta_word] 300]
1420 lappend v $every
1421 }
1422 if {"$setting" == "remind"} {
1423 if {[ta_anymore]} {
1424 set within [parse_interval [ta_word] 5]
1425 } else {
1426 set within 30
1427 }
1428 if {$within > $every} {
1429 error "remind interval must be at least time to respond"
1430 }
1431 lappend v $within
1432 }
1433 ta_nomore
1434 }
1435 default {
1436 error "invalid tellme setting $setting"
1437 }
1438 }
1439 nickdb_set $n tell$sr $v
1440 ucmdr [tellme_${sr}_desc $v] {}
1441 }
1442
1443 proc lnick_checktold {luser} {
1444 set ml [msgsdb_get $luser outbound]
1445 if {![llength $ml]} return
1446 set is1 [expr {[llength $ml]==3}]
1447 set m1 "FYI, I haven't yet passed on your"
1448 set ol {}
1449 set now [clock seconds]
1450 while {[llength $ml]} {
1451 manyset $ml r t n
1452 set ml [lreplace $ml 0 2]
1453 set td [expr {$now-$t}]
1454 if {$n == 1} {
1455 set iv [showinterval $td]
1456 set ifo "$r, $iv"
1457 set if1 "message to $r, $iv."
1458 } else {
1459 set iv [showintervalsecs $td 0]
1460 set ifo "$r, $n messages, oldest $iv"
1461 set if1 "$n messages to $r, oldest $iv."
1462 }
1463 if {$is1} {
1464 sendprivmsg $luser "$m1 $if1"
1465 return
1466 } else {
1467 lappend ol " to $ifo[expr {[llength $ml] ? ";" : "."}]"
1468 }
1469 }
1470 sendprivmsg $luser "$m1 messages:"
1471 msendprivmsg $luser $ol
1472 }
1473
1474 def_ucmd set {
1475 global settings
1476 prefix_nick
1477 check_notonchan
1478 if {![nickdb_exists $n]} {
1479 ucmdr {} "You are unknown to me and so have no settings. (Use `register'.)"
1480 }
1481 if {![ta_anymore]} {
1482 set ol {}
1483 foreach opt $settings {
1484 lappend ol [format "%-10s %s" $opt [set_show/$opt]]
1485 }
1486 ucmdr {} [join $ol "\n"]
1487 } else {
1488 set opt [ta_word]
1489 if {[catch { info body set_show/$opt }]} {
1490 error "no setting $opt"
1491 }
1492 if {![ta_anymore]} {
1493 ucmdr {} "$opt: [set_show/$opt]"
1494 } else {
1495 nick_securitycheck 0
1496 if {[catch { info body set_set/$opt }]} {
1497 error "setting $opt cannot be set with `set'"
1498 }
1499 set_set/$opt
1500 }
1501 }
1502 }
1503
1504 def_ucmd identpass {
1505 set username [ta_word]
1506 set passmd5 [md5sum "[ta_word]\n"]
1507 ta_nomore
1508 prefix_nick
1509 check_notonchan
1510 set luser [irctolower $n]
1511 upvar #0 nick_onchans($luser) onchans
1512 if {![info exists onchans] || ![llength $onchans]} {
1513 ucmdr "You must be on a channel with me to identify yourself." {}
1514 }
1515 check_username $username
1516 exec userv --timeout 3 $username << "$passmd5\n" > /dev/null \
1517 irc-identpass $n
1518 upvar #0 nick_username($luser) rec_username
1519 set rec_username $username
1520 ucmdr "Pleased to see you, $username." {}
1521 }
1522
1523 def_ucmd summon {
1524 set target [ta_word]
1525 ta_nomore
1526 check_username $target
1527 prefix_nick
1528
1529 upvar #0 lastsummon($target) ls
1530 set now [clock seconds]
1531 if {[info exists ls]} {
1532 set interval [expr {$now - $ls}]
1533 if {$interval < 30} {
1534 ucmdr {} \
1535 "Please be patient; $target was summoned only [showinterval $interval]."
1536 }
1537 }
1538 regsub {^[^!]*!} $p {} path
1539 if {[catch {
1540 exec userv --timeout 3 $target irc-summon $n $path \
1541 [expr {[ischan $dest] ? "$dest" : ""}] \
1542 < /dev/null
1543 } rv]} {
1544 regsub -all "\n" $rv { / } rv
1545 error $rv
1546 }
1547 if {[regexp {^problem (.*)} $rv dummy problem]} {
1548 ucmdr {} "The user `$target' $problem."
1549 } elseif {[regexp {^ok ([^ ]+) ([0-9]+)$} $rv dummy tty idlesince]} {
1550 set idletime [expr {$now - $idlesince}]
1551 set ls $now
1552 ucmdr {} {} {} "invites $target ($tty[expr {
1553 $idletime > 10 ? ", idle for [showintervalsecs $idletime 0]" : ""
1554 }]) to [expr {
1555 [ischan $dest] ? "join us here" : "talk to you"
1556 }]."
1557 } else {
1558 error "unexpected response from userv service: $rv"
1559 }
1560 }
1561
1562 proc md5sum {value} { exec md5sum << $value }
1563
1564 def_ucmd seen {
1565 global lastseen nick
1566 prefix_nick
1567 set ncase [ta_nick]
1568 set nlower [irctolower $ncase]
1569 ta_nomore
1570 set now [clock seconds]
1571 if {"$nlower" == "[irctolower $nick]"} {
1572 usererror "I am not self-aware."
1573 } elseif {![info exists lastseen($nlower)]} {
1574 set rstr "I've never seen $ncase."
1575 } else {
1576 manyset $lastseen($nlower) realnick time what
1577 set howlong [expr {$now - $time}]
1578 set string [showinterval $howlong]
1579 set rstr "I last saw $realnick $string, $what."
1580 }
1581 if {[ischan $dest]} {
1582 set where $dest
1583 } else {
1584 set where {}
1585 }
1586 upvar #0 lookedfor($nlower) lf
1587 if {[info exists lf]} { set oldvalue $lf } else { set oldvalue {} }
1588 set lf [list [list $now $n $where]]
1589 foreach v $oldvalue {
1590 if {"[irctolower [lindex $v 1]]" == "[irctolower $n]"} continue
1591 lappend lf $v
1592 }
1593 ucmdr {} $rstr
1594 }
1595
1596 proc lnick_marktime_cancel {luser} {
1597 upvar #0 nick_markid($luser) mi
1598 if {![info exists mi]} return
1599 catch { after cancel $mi }
1600 catch { unset mi }
1601 }
1602
1603 proc lnick_marktime_doafter {luser why ms mentiontold} {
1604 lnick_marktime_cancel $luser
1605 upvar #0 nick_markid($luser) mi
1606 set mi [after $ms [list lnick_marktime_now $luser $why 0]]
1607 }
1608
1609 proc lnick_marktime_reset {luser} {
1610 set mt [nickdb_get $luser marktime]
1611 if {"$mt" == "off" || "$mt" == "once"} return
1612 lnick_marktime_doafter $luser "Time passes." [expr {$mt*1000}] 0
1613 }
1614
1615 proc lnick_marktime_start {luser why ms mentiontold} {
1616 set mt [nickdb_get $luser marktime]
1617 if {"$mt" == "off"} {
1618 lnick_marktime_cancel $luser
1619 after $ms [list lnick_checktold $luser]
1620 } else {
1621 lnick_marktime_doafter $luser $why $ms $mentiontold
1622 }
1623 }
1624
1625 proc lnick_marktime_now {luser why mentiontold} {
1626 upvar #0 nick_onchans($luser) oc
1627 global calling_nick
1628 set calling_nick $luser
1629 sendprivmsg $luser [lnick_pingstring $why $oc ""]
1630 if {$mentiontold} { lnick_checktold $luser }
1631 lnick_marktime_reset $luser
1632 }
1633
1634 proc lnick_pingstring {why oc apstring} {
1635 global nick_onchans
1636 catch { exec uptime } uptime
1637 set nnicks [llength [array names nick_onchans]]
1638 if {[regexp \
1639 {^ *([0-9:apm]+) +up.*, +(\d+) users?, +load average: +([0-9., ]+) *$} \
1640 $uptime dummy time users load]} {
1641 regsub -all , $load {} load
1642 set uptime "$time $nnicks/$users $load"
1643 } else {
1644 append uptime ", $nnicks nicks"
1645 }
1646 if {[llength $oc]} {
1647 set best_la 0
1648 set activity quiet
1649 foreach ch $oc {
1650 upvar #0 chan_lastactivity($ch) la
1651 if {![info exists la]} continue
1652 if {$la <= $best_la} continue
1653 set since [showintervalsecs [expr {[clock seconds]-$la}] 1]
1654 set activity "$ch $since"
1655 set best_la $la
1656 }
1657 } else {
1658 set activity unseen
1659 }
1660 set str $why
1661 append str " " $uptime " " $activity
1662 if {[string length $apstring]} { append str " " $apstring }
1663 return $str
1664 }
1665
1666 def_ucmd ping {
1667 prefix_nick
1668 set ln [irctolower $n]
1669 if {[ischan $dest]} {
1670 set oc [irctolower $dest]
1671 } else {
1672 global nick_onchans
1673 if {[info exists nick_onchans($ln)]} {
1674 set oc $nick_onchans($ln)
1675 } else {
1676 set oc {}
1677 }
1678 if {[llength $oc]} { lnick_marktime_reset $ln }
1679 }
1680 lnick_checktold $ln
1681 ucmdr {} [lnick_pingstring "Pong!" $oc $text]
1682 }
1683
1684 proc ensure_globalsecret {} {
1685 global globalsecret
1686
1687 if {[info exists globalsecret]} return
1688 set gsfile [open /dev/urandom r]
1689 fconfigure $gsfile -translation binary
1690 set globalsecret [read $gsfile 32]
1691 binary scan $globalsecret H* globalsecret
1692 close $gsfile
1693 unset gsfile
1694 }
1695
1696 proc connected {} {
1697 foreach chan [chandb_list] {
1698 if {[chandb_get $chan autojoin]} { dojoin $chan }
1699 }
1700 }
1701
1702 ensure_globalsecret
1703 loadhelp
1704 ensure_connecting