9 defset marktime_min 300
10 defset marktime_join_startdelay 5000
12 proc privmsg_unlogged {prefix ischan params} {
14 [regexp {^![a-z][-a-z]*[a-z]( .*)?$} [lindex $params 1]]} {
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 }
25 proc showintervalsecs {howlong abbrev} {
26 return [showintervalsecs/[opt timeformat] $howlong $abbrev]
29 proc showintervalsecs/ks {howlong abbrev} {
30 if {$howlong < 1000} {
33 if {$howlong < 1000000} {
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]
48 proc format_qty {qty unit abbrev} {
51 append o [string range $unit 0 0]
55 if {$qty != 1} { append o s }
60 proc showintervalsecs/hms {qty abbrev} {
61 set ul {second 60 minute 60 hour 24 day 7 week}
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]
69 set o [format_qty $qty [lindex $ul 0] $abbrev]
71 if {!$abbrev} { append o " " }
72 append o [format_qty $remainv $remainu $abbrev]
77 proc showinterval {howlong} {
81 return "[showintervalsecs $howlong 0] ago"
85 proc showtime {when} {
86 return [showinterval [expr {[clock seconds] - $when}]]
89 proc parse_interval {specified min} {
90 if {![regexp {^([0-9]+)([a-z]+)$} $specified dummy value unit]} {
91 error "invalid syntax for interval"
98 default { error "unknown unit of time $unit" }
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)" }
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\
114 def_msgproc begin {dest str} {
120 def_msgproc append {str} {
122 if {[string length $s] && [string length $ns] > 65} {
123 msg__sendout $varbase
124 set s " [string trimleft $str]"
130 def_msgproc finish {} {
131 msg__sendout $varbase
137 def_msgproc _sendout {} {
138 lappend a [string trimright $s]
142 proc looking_whenwhere {when where} {
143 set str [showtime [expr {$when-1}]]
144 if {[string length $where]} { append str " on $where" }
148 proc tell_getcstate {} {
149 # uses nl from caller's context
150 # imports telling (as the nick_telling) and u
151 # sets stt, telling_when
153 upvar #0 nick_telling($nl) telling
154 upvar #0 nick_unique($nl) u
156 if {[info exists telling]} {
157 manyset $telling u_last stt telling_when
158 if {![info exists u] || "$u_last" != "$u"} {
168 proc tell_event {nl event} {
169 # For `act' we *haven't* yet done the 750ms delay; we implement
170 # that here. Also, here we turn `talk' into `talk' now and `act'
171 # later. We also support the psuedo-event `none'. The del msg
172 # and new msg events are handled by the command procedures, not here.
174 if {[info exists calling_nick]} { set save $calling_nick }
176 switch -exact $event {
179 tell_event_core $nl talk
183 after 750 [list tell_event_core $nl $event]
186 tell_event_core $nl $event
189 set r [tell_event_core $nl TELLME]
192 error "tell_event $nl $event"
195 if {[info exists save]} { set calling_nick $save }
199 proc tell_event_core {nl event} {
200 global tell_event_teventi errorInfo
201 set tell_event_teventi "*$event* $nl"
203 tell_event_core1 $nl $event
205 log_intern "tell event error" "$emsg >$errorInfo<"
208 log_intern "tell event" "done $tell_event_teventi $emsg"
213 proc tell_event_core1 {nl event} {
214 # event is `talk', `act', `ident', `msgsarrive' or `TELLME'
215 # When user talks we actually get talk now and act later
218 set iml [msgsdb_get $nl inbound]
219 if {![llength $iml]} { return nomsgs }
221 set now [clock seconds]
223 set ago [expr {$now - $telling_when}]
225 # Now we have the components of a telling state
226 # u - nick_unique (unset if not visible)
227 # stt - state: undelivered, mentioned, passede
228 # ago - how long ago since we did anything
230 # We compute an evstate to dispatch on as follows:
232 # evstate is string of letters
234 # u UNDELIVERED (MESSAGES)
242 # T tellme (user command)
243 # security level and timing
245 # ss Secure and soon (before interval)
246 # sl Secure and late (after interval)
247 # current identification
250 # reliability and timing
252 # rv Remind, very soon (before within-interval)
253 # rs Remind, soon (between)
254 # rl Remind, late (after every-interval)
255 # ps Pester, soon (before interval)
256 # pl Pester, late (after interval)
260 append evstate [string range $stt 0 0]
261 append evstate [string range $event 0 0]
263 manyset [nickdb_get_sec_effective $nl] sec secwhen
265 insecure { append evstate ii }
266 secure { append evstate [expr {$ago<$secwhen ? "ss" : "sl"}] }
267 default { append evstate "#$sec#" }
270 upvar #0 nick_username($nl) nu
271 if {[info exists nu] && "$nu" == "[nickdb_get_username $nl]"} {
277 manyset [nickdb_get $nl tellrel] rel relint relwithin
279 unreliable { append evstate uu }
280 remind { append evstate [expr {
281 $ago<$relwithin ? "rv" : $ago<$relint ? "rs" : "rl"
283 pester { append evstate [expr {$ago<$relint ? "ps" : "pl"}] }
284 default { append evstate "#$rel#" }
287 global tell_event_teventi
288 set tell_event_teventi "$evstate $ago $nl"
289 switch -glob $evstate {
291 # consider delivered:
292 # (very recently passed, and the user talks)
293 tell_delete_msgs {} $nl
297 # oops, messages passed are now out of date
298 catch_restoreei { unset telling }
302 # ignore new msgs if we haven't passed yet
306 pt???uu - pt???rs - pt???rl - pt???p? {
307 # ignore (any other `talk's) - act handles these
311 uaii?uu - uaii??l - uas?i?l -
314 # pass and then stuff
315 if {[llength $iml] == 3} {
316 manyset $iml sender sentwhen msg
318 "$sender asked me, [showinterval [expr {$now-$sentwhen}]],\
322 "I have [expr {[llength $iml]/3}] messages for you:"
323 while {[llength $iml] >= 3} {
324 manyset [lrange $iml 0 2] sender sentwhen msg
325 set iml [lrange $iml 3 end]
327 " [showintervalsecs [expr {$now-$sentwhen}] 1] <$sender> $msg"
330 if {"$rel" == "unreliable"} {
331 tell_delete_msgs {} $nl
332 return toldunreliable
338 sendprivmsg $nl {You have messages (so identify yourself please).}
343 sendprivmsg $nl {You must identify yourself to see your messages.}
347 sendprivmsg $nl {Don't forget about your messages.}
353 mass??? - pa????v - pa????s -
361 error "tell_event_core nl=$nl evstate=$evstate ?"
364 if {![info exists u]} {
365 set telling [list {} undelivered $now]
367 set telling [list $u $stt $now]
372 proc recordlastseen_n {n how here} {
374 # 0 - nick was seen leaving (or changing to another nicks or some such)
375 # 1 - nick was seen doing something else
376 # 2 - nick was seen talking on channel
377 global lastseen lookedfor
378 set nl [irctolower $n]
379 set now [clock seconds]
380 set lastseen($nl) [list $n $now $how]
384 tell_event $nl [lindex {none act talk} $here]
386 upvar #0 lookedfor($nl) lf
387 if {[info exists lf]} {
388 switch -exact [llength $lf] {
393 manyset [lindex $lf 0] when who where
395 "FYI, $who was looking for you [looking_whenwhere $when $where]."]
398 msg_begin tosend $n "FYI, people have been looking for you:"
404 msg_append tosend " "
405 } elseif {$i == [llength $lf]} {
406 msg_append tosend " and "
409 msg_append tosend ", "
411 manyset $e when who where
413 "$who ([looking_whenwhere $when $where])$fin"
415 set ml [msg_finish tosend]
419 msendprivmsg_delayed 1000 $n $ml
423 proc note_topic {showoff whoby topic} {
424 set msg "FYI, $whoby has changed the topic on $showoff"
425 if {[string length $topic] < 160} {
426 append msg " to $topic"
428 append msg " but it is too long to reproduce here !"
430 set showoff [irctolower $showoff]
431 set tell [chandb_get $showoff topictell]
432 if {[lsearch -exact $tell *] >= 0} {
433 set tryspies [chandb_list]
437 foreach spy $tryspies {
438 set see [chandb_get $spy topicsee]
439 if {[lsearch -exact $see $showoff] >= 0 || \
440 ([lsearch -exact $see *] >= 0 && \
441 [lsearch -exact $tell $spy] >= 0)} {
442 sendprivmsg $spy $msg
447 proc recordlastseen_p {p how here} {
449 recordlastseen_n $n $how $here
452 proc chanmode_arg {} {
454 set rv [lindex $cm_args 0]
455 set cm_args [lreplace cm_args 0 0]
459 proc chanmode_o1 {m g p chan} {
460 global nick chan_initialop
462 set who [chanmode_arg]
463 recordlastseen_n $n "being nice to $who" 1
464 if {"[irctolower $who]" == "[irctolower $nick]"} {
465 set nlower [irctolower $n]
466 upvar #0 nick_unique($nlower) u
467 if {[chandb_exists $chan]} {
468 sendprivmsg $n Thanks.
469 } elseif {![info exists u]} {
470 sendprivmsg $n {Op me while not on the channel, why don't you ?}
472 set chan_initialop([irctolower $chan]) $u
474 "Thanks. You can use `channel manager ...' to register this channel."
475 if {![string length [nickdb_get_username $n username]]} {
477 "(But to do that you must register your nick securely first.)"
483 proc chanmode_o0 {m g p chan} {
486 set who [chanmode_arg]
487 recordlastseen_p $p "being mean to $who" 1
488 if {"[irctolower $who]" == "[irctolower $nick]"} {
489 set chandeop($chan) [list [clock seconds] $p]
493 proc msg_MODE {p c dest modelist args} {
494 if {![ischan $dest]} return
495 if {[regexp {^\-(.+)$} $modelist dummy modelist]} {
497 } elseif {[regexp {^\+(.+)$} $modelist dummy modelist]} {
500 error "invalid modelist"
502 foreach m [split $modelist] {
503 set procname chanmode_$m$give
504 if {[catch { info body $procname }]} {
505 recordlastseen_p $p "fiddling with $dest" 1
507 $procname $m $give $p $dest
512 proc leaving {lchan} {
513 foreach luser [array names nick_onchans] {
514 upvar #0 nick_onchans($luser) oc
515 set oc [grep tc {"$tc" != "$lchan"} $oc]
517 upvar #0 chan_nicks($lchan) nlist
519 upvar #0 chan_lastactivity($lchan) la
523 proc doleave {lchan} {
528 proc dojoin {lchan} {
531 set chan_nicks($lchan) {}
534 proc check_justme {lchan} {
536 upvar #0 chan_nicks($lchan) nlist
537 if {[llength $nlist] != 1} return
538 if {"[lindex $nlist 0]" != "[irctolower $nick]"} return
539 if {[chandb_exists $lchan]} {
540 set mode [chandb_get $lchan mode]
541 if {"$mode" != "*"} {
542 sendout MODE $lchan $mode
544 set topic [chandb_get $lchan topicset]
545 if {[string length $topic]} {
546 sendout TOPIC $lchan $topic
553 proc process_kickpart {chan user} {
556 set luser [irctolower $user]
557 set lchan [irctolower $chan]
558 if {![ischan $chan]} { error "not a channel" }
559 if {"$luser" == "[irctolower $nick]"} {
562 upvar #0 nick_onchans($luser) oc
563 upvar #0 chan_nicks($lchan) nlist
564 set oc [grep tc {"$tc" != "$lchan"} $oc]
565 set nlist [grep tn {"$tn" != "$luser"} $nlist]
567 if {![llength $oc]} {
575 proc msg_TOPIC {p c dest topic} {
577 if {![ischan $dest]} return
578 recordlastseen_n $n "changing the topic on $dest" 1
579 note_topic [irctolower $dest] $n $topic
582 proc msg_KICK {p c chans users comment} {
583 set chans [split $chans ,]
584 set users [split $users ,]
585 if {[llength $chans] > 1} {
586 foreach chan $chans user $users { process_kickpart $chan $user }
588 foreach user $users { process_kickpart [lindex $chans 0] $user }
592 proc msg_KILL {p c user why} {
597 set nick_arys {onchans username unique}
598 # nick_onchans($luser) -> [list ... $lchan ...]
599 # nick_username($luser) -> <securely known local username>
600 # nick_unique($luser) -> <includes-counter>
601 # nick_case($luser) -> $user (valid even if no longer visible)
602 # nick_markid($luser) -> <after id for marktime>
603 # nick_telling($luser) -> <unique> mentioned|passed <when>
605 # chan_nicks($lchan) -> [list ... $luser ...]
606 # chan_lastactivity($lchan) -> [clock seconds]
608 proc lnick_forget {luser} {
609 global nick_arys chan_nicks
610 lnick_marktime_cancel $luser
611 foreach ary $nick_arys {
612 upvar #0 nick_${ary}($luser) av
615 foreach lch [array names chan_nicks] {
616 upvar #0 chan_nicks($lch) nlist
617 set nlist [grep tn {"$tn" != "$luser"} $nlist]
622 proc nick_forget {user} {
623 global nick_arys chan_nicks
624 lnick_forget [irctolower $user]
628 proc nick_case {user} {
630 set nick_case([irctolower $user]) $user
633 proc msg_NICK {p c newnick} {
634 global nick_arys nick_case calling_nick
636 recordlastseen_n $n "changing nicks to $newnick" 0
637 set calling_nick $newnick
638 recordlastseen_n $newnick "changing nicks from $n" 1
639 set luser [irctolower $n]
640 lnick_marktime_cancel $luser
641 set lusernew [irctolower $newnick]
642 foreach ary $nick_arys {
643 upvar #0 nick_${ary}($luser) old
644 upvar #0 nick_${ary}($lusernew) new
645 if {[info exists new]} { error "nick collision ?! $ary $n $newnick" }
646 if {[info exists old]} { set new $old; unset old }
648 upvar #0 nick_onchans($lusernew) oc
650 upvar #0 chan_nicks($ch) nlist
651 set nlist [grep tn {"$tn" != "$luser"} $nlist]
652 lappend nlist $lusernew
654 lnick_marktime_start $lusernew "Hi." 500 1
658 proc nick_ishere {n} {
660 upvar #0 nick_unique([irctolower $n]) u
661 if {![info exists u]} { set u [incr nick_counter].$n.[clock seconds] }
665 proc msg_JOIN {p c chan} {
668 recordlastseen_n $n "joining $chan" 1
669 set nl [irctolower $n]
670 set lchan [irctolower $chan]
671 upvar #0 nick_onchans($nl) oc
672 upvar #0 chan_nicks($lchan) nlist
673 if {![info exists oc]} {
674 global marktime_join_startdelay
675 lnick_marktime_start $nl "Welcome." $marktime_join_startdelay 1
680 proc msg_PART {p c chan args} {
682 set msg "leaving $chan"
683 if {[llength $args]} {
684 set why [lindex $args 0]
685 if {"[irctolower $why]" != "[irctolower $n]"} { append msg " ($why)" }
687 recordlastseen_n $n $msg 1
688 process_kickpart $chan $n
690 proc msg_QUIT {p c why} {
692 recordlastseen_n $n "leaving ($why)" 0
696 proc msg_PRIVMSG {p c dest text} {
700 if {[ischan $dest]} {
701 recordlastseen_n $n "invoking me in $dest" 1
704 recordlastseen_n $n "talking to me" 1
709 execute_usercommand $p $c $n $output $dest $text
712 proc msg_INVITE {p c n chan} {
713 after 1000 [list dojoin [irctolower $chan]]
716 proc grep {var predicate list} {
720 if {[uplevel 1 [list expr $predicate]]} { lappend o $v }
725 proc msg_353 {p c dest type chan nicklist} {
726 global names_chans nick_onchans
727 set lchan [irctolower $chan]
728 upvar #0 chan_nicks($lchan) nlist
729 lappend names_chans $lchan
730 if {![info exists nlist]} {
731 # We don't think we're on this channel, so ignore it !
732 # Unfortunately, because we don't get a reply to PART,
733 # we have to remember ourselves whether we're on a channel,
734 # and ignore stuff if we're not, to avoid races. Feh.
738 foreach user [split $nicklist { }] {
739 regsub {^[@+]} $user {} user
740 if {![string length $user]} continue
742 set luser [irctolower $user]
743 upvar #0 nick_onchans($luser) oc
745 lappend nlist_new $luser
751 proc msg_366 {p c args} {
752 global names_chans nick_onchans
753 set lchan [irctolower $c]
754 foreach luser [array names nick_onchans] {
755 upvar #0 nick_onchans($luser) oc
756 if {[llength names_chans] > 1} {
757 set oc [grep tc {[lsearch -exact $tc $names_chans] >= 0} $oc]
759 if {![llength $oc]} { lnick_forget $n }
764 proc check_username {target} {
766 [string length $target] > 8 ||
767 [regexp {[^-0-9a-z]} $target] ||
768 ![regexp {^[a-z]} $target]
769 } { error "invalid username" }
772 proc somedb__head {} {
774 set idl [irctolower $id]
775 upvar #0 ${nickchan}db($idl) ndbe
776 binary scan $idl H* idh
777 set idfn $fprefix$idh
778 if {![info exists iddbe] && [file exists $idfn]} {
780 try_except_finally { set newval [read $f] } {} { close $f }
781 if {[llength $newval] % 2} { error "invalid length" }
787 proc def_somedb {name arglist body} {
788 foreach {nickchan fprefix} {
793 proc ${nickchan}db_$name $arglist \
794 "set nickchan $nickchan; set fprefix $fprefix; $body"
800 foreach path [glob -nocomplain -path $fprefix *] {
801 binary scan $path "A[string length $fprefix]A*" afprefix thinghex
802 if {"$afprefix" != "$fprefix"} { error "wrong prefix $path $afprefix" }
803 lappend list [binary format H* $thinghex]
808 proc def_somedb_id {name arglist body} {
809 def_somedb $name [concat id $arglist] "somedb__head; $body"
812 def_somedb_id exists {} {
813 return [info exists iddbe]
816 def_somedb_id delete {} {
817 catch { unset iddbe }
821 set default_settings_nick {
825 tellrel {remind 3600 30}
828 set default_settings_chan {
837 set default_settings_msgs {
841 # inbound -> [<nick> <time_t> <message>] ...
842 # outbound -> [<nick> <time_t(earliest)> <count>] ...
843 # neither are sorted particularly; only one entry per recipient in
844 # output; both sender and recipient are cased
846 def_somedb_id set {args} {
847 upvar #0 default_settings_$nickchan def
848 if {![info exists iddbe]} { set iddbe $def }
849 foreach {key value} [concat $iddbe $args] { set a($key) $value }
851 foreach {key value} [array get a] { lappend newval $key $value }
852 set f [open $idfn.new w]
856 file rename -force $idfn.new $idfn
864 def_somedb_id get {key} {
865 upvar #0 default_settings_$nickchan def
866 if {[info exists iddbe]} {
867 set l [concat $iddbe $def]
871 foreach {tkey value} $l {
872 if {"$tkey" == "$key"} { return $value }
874 error "unset setting $key"
879 if {[info exists calling_nick]} { set n $calling_nick } { set n {} }
880 return [nickdb_get $n $key]
883 proc check_notonchan {} {
885 if {[ischan $dest]} { usererror "That command must be sent privately." }
888 proc nick_securitycheck {strict} {
890 if {![nickdb_exists $n]} {
891 usererror "You are unknown to me, use `register'."
893 set wantu [nickdb_get $n username]
894 if {![string length $wantu]} {
896 usererror "That feature is only available to secure users, sorry."
901 set luser [irctolower $n]
902 upvar #0 nick_username($luser) nu
903 if {![info exists nu]} {
904 usererror "Nick $n is secure, you must identify yourself first."
906 if {"$wantu" != "$nu"} {
907 usererror "You are the wrong user -\
908 the nick $n belongs to $wantu, not $nu."
912 proc channel_ismanager {channel n} {
913 set mgrs [chandb_get $channel managers]
914 return [expr {[lsearch -exact [irctolower $mgrs] [irctolower $n]] >= 0}]
917 proc channel_securitycheck {channel} {
919 if {![channel_ismanager $channel $n]} {
920 usererror "You are not a manager of $channel."
925 proc def_chancmd {name body} {
926 proc channel/$name {} \
927 " upvar 1 target chan; upvar 1 n n; upvar 1 text text; $body"
930 proc ta_listop {findnow procvalue} {
931 # findnow and procvalue are code fragments which will be executed
932 # in the caller's level. findnow should set ta_listop_ev to
933 # the current list, and procvalue should treat ta_listop_ev as
934 # a proposed value in the list and check and possibly modify
935 # (canonicalise?) it. After ta_listop, ta_listop_ev will
936 # be the new value of the list.
937 upvar 1 ta_listop_ev exchg
940 switch -exact _$opcode {
944 foreach item $exchg { set array($item) 1 }
947 error "list change opcode must be one of + - ="
950 foreach exchg [split $text " "] {
951 if {![string length $exchg]} continue
953 if {"$opcode" != "-"} {
956 catch { unset array($exchg) }
959 set exchg [lsort [array names array]]
962 def_chancmd manager {
964 if {[chandb_exists $chan]} {
965 set ta_listop_ev [chandb_get $chan managers]
967 set ta_listop_ev [list [irctolower $n]]
970 check_nick $ta_listop_ev
971 set ta_listop_ev [irctolower $ta_listop_ev]
973 if {[llength $ta_listop_ev]} {
974 chandb_set $chan managers $ta_listop_ev
975 ucmdr "Managers of $chan: $ta_listop_ev" {}
978 ucmdr {} {} "forgets about managing $chan." {}
982 def_chancmd autojoin {
984 switch -exact [string tolower $yesno] {
987 default { error "channel autojoin must be `yes' or `no' }
989 chandb_set $chan autojoin $nv
990 ucmdr [expr {$nv ? "I will join $chan when I'm restarted " : \
991 "I won't join $chan when I'm restarted "}] {}
994 def_chancmd userinvite {
995 set nv [string tolower [ta_word]]
997 pub { set txt "!invite will work for $chan, but it won't work by /msg" }
998 here { set txt "!invite and /msg invite will work, but only for users who are already on $chan." }
999 all { set txt "Any user will be able to invite themselves or anyone else to $chan." }
1000 none { set txt "I will not invite anyone to $chan." }
1002 error "channel userinvite must be `pub', `here', `all' or `none'
1005 chandb_set $chan userinvite $nv
1011 switch -exact $what {
1014 chandb_set $chan topicset {}
1015 ucmdr "I won't ever change the topic of $chan." {}
1018 set t [string trim $text]
1019 if {![string length $t]} {
1020 error "you must specific the topic to set"
1022 chandb_set $chan topicset $t
1023 ucmdr "Whenever I'm alone on $chan, I'll set the topic to $t." {}
1027 set ta_listop_ev [chandb_get $chan topic$what]
1029 if {"$ta_listop_ev" != "*"} {
1030 if {![ischan $ta_listop_ev]} {
1031 error "bad channel \`$ta_listop_ev' in topic $what"
1033 set ta_listop_ev [irctolower $ta_listop_ev]
1036 chandb_set $chan topic$what $ta_listop_ev
1037 ucmdr "Topic $what list for $chan: $ta_listop_ev" {}
1040 usererror "Unknown channel topic subcommand - see help channel."
1047 if {"$mode" != "*" && ![regexp {^(([-+][imnpst]+)+)$} $mode mode]} {
1048 error {channel mode must be * or match ([-+][imnpst]+)+}
1050 chandb_set $chan mode $mode
1051 if {"$mode" == "*"} {
1052 ucmdr "I won't ever change the mode of $chan." {}
1054 ucmdr "Whenever I'm alone on $chan, I'll set the mode to $mode." {}
1059 if {[chandb_exists $chan]} {
1060 set l "Settings for $chan: autojoin "
1061 append l [lindex {no yes} [chandb_get $chan autojoin]]
1062 append l ", mode " [chandb_get $chan mode]
1063 append l ", userinvite " [chandb_get $chan userinvite] "."
1064 append l "\nManagers: "
1065 append l [join [chandb_get $chan managers] " "]
1066 foreach {ts sep} {see "\n" tell " "} {
1067 set t [chandb_get $chan topic$ts]
1070 append l "Topic $ts list: $t."
1072 append l "Topic $ts list is empty."
1076 set t [chandb_get $chan topicset]
1077 if {[string length $t]} {
1078 append l "Topic to set: $t"
1080 append l "I will not change the topic."
1084 ucmdr {} "The channel $chan is not managed."
1088 proc channelmgr_monoop {} {
1093 upvar 1 target target
1098 if {[ischan $dest]} { set target $dest }
1099 if {[ta_anymore]} { set target [ta_word] }
1101 if {![info exists target]} {
1102 usererror "You must specify, or invoke me on, the relevant channel."
1104 if {![info exists chan_nicks([irctolower $target])]} {
1105 usererror "I am not on $target."
1107 if {![ischan $target]} { error "not a valid channel" }
1109 if {![chandb_exists $target]} {
1110 usererror "$target is not a managed channel."
1112 channel_securitycheck $target
1117 sendout MODE $target +o $n
1126 global chan_nicks errorCode errorInfo
1129 if {[ischan $dest]} {
1133 set target [ta_word]
1136 set ltarget [irctolower $target]
1137 if {![ischan $target]} { error "$target is not a channel" }
1138 if {![info exists chan_nicks($ltarget)]} {
1139 usererror "I am not on $target."
1141 set ui [chandb_get $ltarget userinvite]
1143 if {"$ui" == "pub" && !$onchan} {
1144 usererror "Invitations to $target must be made there with !invite."
1146 if {"$ui" != "all"} {
1147 if {[lsearch -exact $chan_nicks($ltarget) [irctolower $n]] < 0} {
1148 usererror "Invitations to $target may only be made\
1149 by a user on the channel."
1152 if {"$ui" == "none"} {
1153 usererror "Sorry, I've not been authorised\
1154 to invite people to $target."
1157 if {"$errorCode" == "BLIGHT USER" && [channel_ismanager $target $n]} {
1159 nick_securitycheck 1
1161 if {"$errorCode" == "BLIGHT USER"} {
1162 usererror "$emsg2 Therefore you can't use your\
1163 channel manager privilege. $emsg"
1165 error $error $errorInfo $errorCode
1169 error $emsg $errorInfo $errorCode
1172 if {![ta_anymore]} {
1173 usererror "You have to say who to invite."
1176 while {[ta_anymore]} {
1177 set invitee [ta_word]
1179 lappend invitees $invitee
1181 foreach invitee $invitees {
1182 sendout INVITE $invitee $ltarget
1184 set who [lindex $invitees 0]
1185 switch -exact llength $invitees {
1186 0 { error "zero invitees" }
1188 2 { append who " and [lindex $invitees 1]" }
1190 set who [join [lreplace $invitees end end] ", "]
1191 append who " and [lindex $invitees [llength $invitees]]"
1194 ucmdr {} {} {} "invites $who to $target."
1198 if {[ischan $dest]} { set target $dest }
1199 if {![ta_anymore]} {
1202 set subcmd [ta_word]
1204 if {[ischan $subcmd]} {
1206 if {![ta_anymore]} {
1209 set subcmd [ta_word]
1212 if {![info exists target]} { error "privately, you must specify a channel" }
1213 set procname channel/$subcmd
1214 if {"$subcmd" != "show"} {
1215 if {[catch { info body $procname }]} {
1216 usererror "unknown channel setting $subcmd."
1219 if {[chandb_exists $target]} {
1220 channel_securitycheck $target
1222 nick_securitycheck 1
1223 upvar #0 chan_initialop([irctolower $target]) io
1224 upvar #0 nick_unique([irctolower $n]) u
1225 if {![info exists io]} {
1226 usererror "$target is not a managed channel."
1228 if {"$io" != "$u"} {
1229 usererror "You are not the interim manager of $target."
1231 if {"$subcmd" != "manager"} {
1232 usererror "Please use `channel manager' first."
1239 proc nickdb_get_username {n} {
1240 if {![nickdb_exists $n]} { return "" }
1241 return [nickdb_get $n username]
1244 proc nickdb_get_sec_effective {n} {
1245 set l [nickdb_get $n tellsec]
1246 set u [nickdb_get_username $n]
1247 if {"[lindex $l 0]" == "secure" && ![string length $u]} { set l insecure }
1251 proc tell_peernicks {text} {
1252 set text [irctolower [string trim $text]]
1253 set senders [split $text " "]
1254 foreach sender $senders {
1255 if {[catch { check_nick $sender } emsg]} {
1256 error "invalid sender nick `$sender': $emsg" $errorInfo $errorCode
1262 proc msgsdb_set_maydelete {n key l otherkey} {
1263 msgsdb_set $n $key $l
1264 if {[llength $l]} return
1265 if {[llength [msgsdb_get $n $otherkey]]} return
1269 proc tell_delete_msgs {lsenders lrecip} {
1272 foreach {s t m} [msgsdb_get $lrecip inbound] {
1273 if {[llength $lsenders]} {
1274 if {[lsearch -exact $lsenders [irctolower $s]] == -1} {
1275 lappend ninbound $s $t $m
1282 msgsdb_set_maydelete $lrecip inbound $ninbound outbound
1283 if {![llength $ninbound]} {
1284 upvar #0 nick_telling($lrecip) telling
1285 catch { unset telling }
1287 foreach s [array names rsenders] {
1289 foreach {r t c} [msgsdb_get $s outbound] {
1290 if {"[irctolower $r]" == "$lrecip"} continue
1291 lappend noutbound $r $t $c
1293 msgsdb_set_maydelete $s outbound $noutbound inbound
1301 nick_securitycheck 0
1302 set recipients [tell_peernicks $text]
1303 if {![llength $recipients]} {
1304 usererror "You must say which recipients' messages from you to forget."
1307 foreach recip $recipients {
1308 incr ndel [tell_delete_msgs [irctolower $n] $recip]
1310 ucmdr "Removed $ndel as yet undelivered message(s)." {}
1314 global errorInfo errorCode
1316 set nl [irctolower $n]
1318 manyset [nickdb_get_sec_effective $n] sec secwhen
1319 switch -exact $sec {
1323 "There are no messages to delete\
1324 because your message disposition prevents them from being left."
1327 nick_securitycheck 1
1330 error "delmsg sec $sec"
1334 if {"$stt" != "passed"} {
1336 "There are message(s) you've not yet seen; I'll deliver them to you now.\
1337 If you actually want to delete them, just tell me `delmsg' again."
1339 set senders [tell_peernicks $text]
1340 set ndel [tell_delete_msgs [irctolower $senders] [irctolower $n]]
1342 if {[llength $senders]} {
1343 ucmdr "No relevant incoming messages to delete." {}
1345 ucmdr "No incoming messages to delete." {}
1348 switch -exact [llength $senders] {
1349 0 { ucmdr {} {} "deletes your $ndel message(s)." }
1350 1 { ucmdr {} {} "deletes your $ndel message(s) from $senders." }
1352 ucmdr {} {} "deletes your $ndel message(s) from\
1353 [lreplace $senders end end] and [lindex $senders end]."
1362 switch -exact [tell_event [irctolower $n] tellme] {
1363 ERROR - INVALID { ucmdr {} {is ill. Help!} }
1364 nomsgs { ucmdr {You have no messages.} {} }
1370 global nick_case ownmailaddr ownfullname
1373 set target [ta_word]
1374 if {![string length $text]} { error "tell them what?" }
1375 if {[string length $text] > 400} { error "message too long" }
1377 set ltarget [irctolower $target]
1379 if {[info exists nick_case($ltarget)]} { set ctarget $nick_case($ltarget) }
1381 manyset [nickdb_get_sec_effective $target] sec mailtoint mailwhy
1382 manyset [nickdb_get $target tellrel] rel relint relwithin
1383 switch -exact $sec {
1385 set now [clock seconds]
1386 set inbound [msgsdb_get $ltarget inbound]
1387 lappend inbound $n $now $text
1388 msgsdb_set $ltarget inbound $inbound
1390 set outbound [msgsdb_get $n outbound]
1393 foreach {recip time count} $outbound {
1394 if {"[irctolower $recip]" == "$ltarget"} {
1399 lappend noutbound $recip $time $count
1402 lappend noutbound $ctarget $now 1
1404 msgsdb_set $n outbound $noutbound
1405 set msg "OK, I'll tell $ctarget"
1406 if {$found} { append msg " that too" }
1408 if {"$sec" != "secure"} {
1409 switch -exact $rel {
1410 unreliable { append msg "neither reliably nor securely" }
1411 remind { append msg "pretty reliably, but not securely" }
1412 pester { append msg "reliably but not securely" }
1415 switch -exact $rel {
1416 unreliable { append msg "securely but not reliably" }
1417 remind { append msg "securely and pretty reliably" }
1418 pester { append msg "reliably and securely" }
1422 tell_event $ltarget msgsarrive
1426 set fmtmsg [exec fmt << " $text"]
1427 exec /usr/sbin/sendmail -odb -oi -t -oee -f $mailwhy \
1429 "From: $ownmailaddr ($ownfullname)
1431 Subject: IRC tell from $n
1433 $n asked me[expr {[ischan $dest] ? " on $dest" : ""}] to tell you:
1434 [exec fmt << " $text"]
1436 (This message was for your nick $ctarget; your account $mailwhy
1437 arranged for it to be forwarded to $mailtoint.)
1440 "I've mailed $ctarget, which is what they prefer." \
1444 usererror "Sorry, $ctarget does not want me to take messages."
1447 error "bad tellsec $sec"
1454 set target [ta_word]; ta_nomore
1459 set myself [expr {"$target" != "$n"}]
1461 set ltarget [irctolower $target]
1462 upvar #0 nick_case($ltarget) ctarget
1464 if {[info exists ctarget]} {
1465 upvar #0 nick_onchans($ltarget) oc
1466 upvar #0 nick_username($ltarget) nu
1467 if {[info exists oc]} { set nshow $ctarget }
1469 if {![nickdb_exists $ltarget]} {
1470 set ol "$nshow is not a registered nick."
1471 } elseif {[string length [set username [nickdb_get $target username]]]} {
1472 set ol "The nick $nshow belongs to the user $username."
1474 set ol "The nick $nshow is registered (but not to a username)."
1476 if {![info exists ctarget] || ![info exists oc]} {
1478 append ol "\nI can't see $nshow on anywhere."
1480 append ol "\nYou aren't on any channels with me."
1482 } elseif {![info exists nu]} {
1483 append ol "\n$nshow has not identified themselves."
1484 } elseif {![info exists username]} {
1485 append ol "\n$nshow has identified themselves as the user $nu."
1486 } elseif {"$nu" != "$username"} {
1487 append ol "\nHowever, $nshow is being used by the user $nu."
1489 append ol "\n$nshow has identified themselves to me."
1497 set old [nickdb_exists $n]
1498 if {$old} { nick_securitycheck 0 }
1499 set luser [irctolower $n]
1500 switch -exact [string tolower [string trim $text]] {
1502 upvar #0 nick_username($luser) nu
1503 if {![info exists nu]} {
1505 "You must identify yourself before using `register'. See `help identify', or use `register insecure'."
1507 nickdb_set $n username $nu
1508 ucmdr {} {} "makes a note of your username." {}
1512 ucmdr {} {} "forgets your nickname." {}
1515 nickdb_set $n username {}
1517 ucmdr {} "Security is now disabled for your nickname !"
1519 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."
1523 error "you mean register / register delete / register insecure"
1528 proc timeformat_desc {tf} {
1530 ks { return "Times will be displayed in seconds or kiloseconds." }
1531 hms { return "Times will be displayed in hours, minutes, etc." }
1532 default { error "invalid timeformat: $v" }
1537 proc def_setting {opt show_body set_body} {
1539 lappend settings $opt
1540 proc set_show/$opt {} "
1544 if {![string length $set_body]} return
1545 proc set_set/$opt {} "
1552 proc tellme_sec_desc {v n} {
1553 manyset $v sec mailtoint
1554 switch -exact $sec {
1556 return "I'll tell you your messages whenever I see you."
1559 if {[string length [nickdb_get_username $n]]} {
1561 "I'll keep the bodies of your messages private until you identify yourself, reminding you every [showintervalsecs $mailtoint 1]."
1564 "I'll tell you your messages whenever I see you.\
1565 (Secure message delivery is enabled, but your nick is not registered\
1566 securely. See `help register'.)"
1570 return "I shan't accept messages for you."
1573 return "I'll forward your messages by email to $mailtoint."
1576 error "bad tellsec $sec"
1581 proc tellme_rel_desc {v} {
1582 manyset $v rel every within
1583 switch -exact $rel {
1585 return "As soon as I've told you message(s), I'll forget them\
1586 - note that this means messages can get lost !"
1592 set u ", or talk on channel within [showintervalsecs $within 1] of me having told you"
1595 error "bad tellrel $rel"
1598 return "After delivering messages, I'll remind you every\
1599 [showintervalsecs $every 1] until you say delmsg$u."
1602 def_setting timeformat {
1603 set tf [nickdb_get $n timeformat]
1604 return "$tf: [timeformat_desc $tf]"
1606 set tf [string tolower [ta_word]]
1608 set desc [timeformat_desc $tf]
1609 nickdb_set $n timeformat $tf
1613 proc marktime_desc {mt} {
1614 if {"$mt" == "off"} {
1615 return "I will not send you periodic messages."
1616 } elseif {"$mt" == "once"} {
1617 return "I will send you one informational message when I see you."
1619 return "I'll send you a message every [showintervalsecs $mt 0]."
1623 def_setting marktime {
1624 set mt [nickdb_get $n marktime]
1626 if {[string match {[0-9]*} $mt]} { append p s }
1628 append p [marktime_desc $mt]
1632 set mt [string tolower [ta_word]]
1635 if {"$mt" == "off" || "$mt" == "once"} {
1637 set mt [parse_interval $mt $marktime_min]
1639 nickdb_set $n marktime $mt
1640 lnick_marktime_start [irctolower $n] "So:" 500 0
1641 ucmdr {} [marktime_desc $mt]
1644 def_setting security {
1645 set s [nickdb_get $n username]
1646 if {[string length $s]} {
1647 return "Your nick, $n, is controlled by the user $s."
1649 return "Your nick, $n, is not secure."
1653 proc tellme_setting_sec_simple {} {
1661 proc tellme_setting_neednomsgs {} {
1663 if {[llength [msgsdb_get $n inbound]]} {
1664 usererror "You must delete the messages you have, first."
1669 def_setting tellme {
1670 set secv [nickdb_get $n tellsec]
1671 set ms [tellme_sec_desc $secv $n]
1673 switch -exact $sec {
1675 set mr [tellme_rel_desc [nickdb_get $n tellrel]]
1683 set setting [string tolower [ta_word]]
1684 switch -exact $setting {
1686 tellme_setting_sec_simple
1689 set every [ta_interval_optional 60 600]
1692 set v [list secure $every]
1695 telling_setting_neednomsgs
1696 telling_setting_sec_simple
1699 telling_setting_neednomsgs
1701 if {[string length [set u [nickdb_get_username $n]]]} {
1703 "Sorry, you must register securely to have your messages mailed\
1704 (to prevent the use of this feature for spamming). See `help register'."
1707 set v [list mailto [ta_word] $u]
1709 unreliable - pester - remind {
1710 manyset [nickdb_get $n tellsec] sec
1711 switch -exact $sec {
1714 "You can't change your message delivery conditions when\
1715 your message disposition prevents messages from being left."
1720 if {"$setting" != "unreliable"} {
1721 set every [parse_interval_optional 300 3600]
1724 if {"$setting" == "remind"} {
1725 set within [ta_interval_optional 5 30]
1726 if {$within > $every} {
1727 error "remind interval must be at least time to respond"
1734 error "invalid tellme setting $setting"
1737 nickdb_set $n tell$sr $v
1738 ucmdr [tellme_${sr}_desc $v] {}
1741 proc lnick_checktold {luser} {
1742 set ml [msgsdb_get $luser outbound]
1743 if {![llength $ml]} return
1744 set is1 [expr {[llength $ml]==3}]
1745 set m1 "FYI, I haven't yet passed on your"
1747 set now [clock seconds]
1748 while {[llength $ml]} {
1750 set ml [lreplace $ml 0 2]
1751 set td [expr {$now-$t}]
1753 set iv [showinterval $td]
1755 set if1 "message to $r, $iv."
1757 set iv [showintervalsecs $td 0]
1758 set ifo "$r, $n messages, oldest $iv"
1759 set if1 "$n messages to $r, oldest $iv."
1762 sendprivmsg $luser "$m1 $if1"
1765 lappend ol " to $ifo[expr {[llength $ml] ? ";" : "."}]"
1768 sendprivmsg $luser "$m1 messages:"
1769 msendprivmsg $luser $ol
1776 if {![nickdb_exists $n]} {
1777 ucmdr {} "You are unknown to me and so have no settings. (Use `register'.)"
1779 if {![ta_anymore]} {
1781 foreach opt $settings {
1782 lappend ol [format "%-10s %s" $opt [set_show/$opt]]
1784 ucmdr {} [join $ol "\n"]
1787 if {[catch { info body set_show/$opt }]} {
1788 error "no setting $opt"
1790 if {![ta_anymore]} {
1791 ucmdr {} "$opt: [set_show/$opt]"
1793 nick_securitycheck 0
1794 if {[catch { info body set_set/$opt }]} {
1795 error "setting $opt cannot be set with `set'"
1802 def_ucmd identpass {
1805 set luser [irctolower $n]
1806 set username [ta_word]
1807 set passmd5 [md5sum "[ta_word]\n"]
1809 upvar #0 nick_onchans($luser) onchans
1810 if {![info exists onchans] || ![llength $onchans]} {
1811 ucmdr "You must be on a channel with me to identify yourself." {}
1813 check_username $username
1814 exec userv --timeout 3 $username << "$passmd5\n" > /dev/null \
1816 upvar #0 nick_username($luser) rec_username
1817 set rec_username $username
1818 tell_event $luser ident
1819 ucmdr "Pleased to see you, $username." {}
1823 set target [ta_word]
1825 check_username $target
1828 upvar #0 lastsummon($target) ls
1829 set now [clock seconds]
1830 if {[info exists ls]} {
1831 set interval [expr {$now - $ls}]
1832 if {$interval < 30} {
1834 "Please be patient; $target was summoned only [showinterval $interval]."
1837 regsub {^[^!]*!} $p {} path
1839 exec userv --timeout 3 $target irc-summon $n $path \
1840 [expr {[ischan $dest] ? "$dest" : ""}] \
1843 regsub -all "\n" $rv { / } rv
1846 if {[regexp {^problem (.*)} $rv dummy problem]} {
1847 ucmdr {} "The user `$target' $problem."
1848 } elseif {[regexp {^ok ([^ ]+) ([0-9]+)$} $rv dummy tty idlesince]} {
1849 set idletime [expr {$now - $idlesince}]
1851 ucmdr {} {} {} "invites $target ($tty[expr {
1852 $idletime > 10 ? ", idle for [showintervalsecs $idletime 0]" : ""
1854 [ischan $dest] ? "join us here" : "talk to you"
1857 error "unexpected response from userv service: $rv"
1861 proc md5sum {value} { exec md5sum << $value }
1864 global lastseen nick
1867 set nlower [irctolower $ncase]
1869 set now [clock seconds]
1870 if {"$nlower" == "[irctolower $nick]"} {
1871 usererror "I am not self-aware."
1872 } elseif {![info exists lastseen($nlower)]} {
1873 set rstr "I've never seen $ncase."
1875 manyset $lastseen($nlower) realnick time what
1876 set howlong [expr {$now - $time}]
1877 set string [showinterval $howlong]
1878 set rstr "I last saw $realnick $string, $what."
1880 if {[ischan $dest]} {
1885 upvar #0 lookedfor($nlower) lf
1886 if {[info exists lf]} { set oldvalue $lf } else { set oldvalue {} }
1887 set lf [list [list $now $n $where]]
1888 foreach v $oldvalue {
1889 if {"[irctolower [lindex $v 1]]" == "[irctolower $n]"} continue
1895 proc lnick_marktime_cancel {luser} {
1896 upvar #0 nick_markid($luser) mi
1897 if {![info exists mi]} return
1898 catch { after cancel $mi }
1902 proc lnick_marktime_doafter {luser why ms mentiontold} {
1903 lnick_marktime_cancel $luser
1904 upvar #0 nick_markid($luser) mi
1905 set mi [after $ms [list lnick_marktime_now $luser $why 0]]
1908 proc lnick_marktime_reset {luser} {
1909 set mt [nickdb_get $luser marktime]
1910 if {"$mt" == "off" || "$mt" == "once"} return
1911 lnick_marktime_doafter $luser "Time passes." [expr {$mt*1000}] 0
1914 proc lnick_marktime_start {luser why ms mentiontold} {
1915 set mt [nickdb_get $luser marktime]
1916 if {"$mt" == "off"} {
1917 lnick_marktime_cancel $luser
1918 if {$mentiontold} { after $ms [list lnick_checktold $luser] }
1920 lnick_marktime_doafter $luser $why $ms $mentiontold
1924 proc lnick_marktime_now {luser why mentiontold} {
1925 upvar #0 nick_onchans($luser) oc
1927 set calling_nick $luser
1928 sendprivmsg $luser [lnick_pingstring $why $oc ""]
1929 if {$mentiontold} { after 150 [list lnick_checktold $luser] }
1930 lnick_marktime_reset $luser
1933 proc lnick_pingstring {why oc apstring} {
1935 catch { exec uptime } uptime
1936 set nnicks [llength [array names nick_onchans]]
1938 {^ *([0-9:apm]+) +up.*, +(\d+) users?, +load average: +([0-9., ]+) *$} \
1939 $uptime dummy time users load]} {
1940 regsub -all , $load {} load
1941 set uptime "$time $nnicks/$users $load"
1943 append uptime ", $nnicks nicks"
1945 if {[llength $oc]} {
1949 upvar #0 chan_lastactivity($ch) la
1950 if {![info exists la]} continue
1951 if {$la <= $best_la} continue
1952 set since [showintervalsecs [expr {[clock seconds]-$la}] 1]
1953 set activity "$ch $since"
1960 append str " " $uptime " " $activity
1961 if {[string length $apstring]} { append str " " $apstring }
1967 set ln [irctolower $n]
1968 if {[ischan $dest]} {
1969 set oc [irctolower $dest]
1972 if {[info exists nick_onchans($ln)]} {
1973 set oc $nick_onchans($ln)
1977 if {[llength $oc]} { lnick_marktime_reset $ln }
1979 after 150 [list lnick_checktold $ln]
1980 ucmdr {} [lnick_pingstring "Pong!" $oc $text]
1983 proc ensure_globalsecret {} {
1986 if {[info exists globalsecret]} return
1987 set gsfile [open /dev/urandom r]
1988 fconfigure $gsfile -translation binary
1989 set globalsecret [read $gsfile 32]
1990 binary scan $globalsecret H* globalsecret
1996 foreach chan [chandb_list] {
1997 if {[chandb_get $chan autojoin]} { dojoin $chan }