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