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_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.
154 if {[info exists calling_nick]} { set save $calling_nick }
155 puts "[list tell_event $nl $event]"
156 switch -exact $event {
159 tell_event_core $nl talk
163 after 750 [list tell_event_core $nl $event]
166 tell_event_core $nl $event
169 error "tell_event $nl $event"
172 if {[info exists save]} { set calling_nick $save }
174 puts "[list tell_event $nl $event] $errorInfo"
177 proc tell_getcstate {} {
178 # uses nl from caller's context
179 # imports telling (as the nick_telling) and u
180 # sets stt, telling_when
182 upvar #0 nick_telling($nl) telling
183 upvar #0 nick_unique($nl) u
185 if {[info exists telling]} {
186 manyset $telling u_last stt telling_when
187 if {![info exists u] || "$u_last" != "$u"} {
188 unset telling; unset stt; unset telling_when
192 if {![info exists stt]} {
194 set telling_when $now
199 proc tell_event_core {nl event} {
200 catch_logged { tell_event_core1 $nl $event }
203 proc tell_event_core1 {nl event} {
204 # event is `talk', `act', `ident' or `msgsarrive'
205 # When user talks we actually get talk now and act later
208 set iml [msgsdb_get $nl inbound]
209 if {![llength $iml]} return
211 set now [clock seconds]
213 set ago [expr {$now - $telling_when}]
215 # Now we have the components of a telling state
216 # u - nick_unique (unset if not visible)
217 # stt - state: norecord, mentioned, passede
218 # ago - how long ago since we did anything
220 # We compute an evstate to dispatch on as follows:
222 # evstate is string of letters
224 # n NORECORD (MESSAGES)
232 # security level and timing
234 # ss Secure and soon (before interval)
235 # sl Secure and late (after interval)
236 # current identification
239 # reliability and timing
241 # rv Remind, very soon (before within-interval)
242 # rs Remind, soon (between)
243 # rl Remind, late (after every-interval)
244 # ps Pester, soon (before interval)
245 # pl Pester, late (after interval)
249 append evstate [string range $stt 0 0]
250 append evstate [string range $event 0 0]
252 manyset [tell_effective_sec $nl] sec secwhen
254 insecure { append evstate ii }
255 secure { append evstate [expr {$ago<$secwhen ? "sl" : "ss"}] }
256 default { append evstate "#$sec#" }
259 upvar #0 nick_username($nl) nu
260 if {[info exists nu] && "$nu" == "[nickdb_get $nl username]"} {
266 manyset [nickdb_get $nl tellrel] rel relint relwithin
268 unreliable { append evstate uu }
269 remind { append evstate [expr {
270 $ago<$relwithin ? "rv" : $ago<$relint ? "rs" : "rl"
272 pester { append evstate [expr {$ago<$relint ? "ps" : "pl"}] }
273 default { append evstate "#$rel#" }
276 switch -glob $evstate {
278 # consider delivered:
279 # (very recently passed, and the user talks)
280 tell_delete_msgs {} $nl
284 # oops, messages passed are now out of date
285 catch_restoreei { unset telling }
289 # ignore new msgs if we haven't passed yet
293 pt???uu - pt???rs - pt???rl - pt???p? {
294 # ignore (any other `talk's) - act handles these
297 ni????? - naii??? - nas?i?? - mi????? - pa????l {
298 # pass and then stuff
299 if {[length $iml] == 3} {
300 manyset $iml sender sentwhen msg
302 "$sender asked me [showintervalsecs [expr {$now-$sentwhen}] 0]\
306 "Here are the [expr {[llength $iml]/3}] messages there are for you:"
307 while {[llength $iml] >= 3} {
308 manyset [lrange $iml 0 2] sender sentwhen msg
309 set iml [lrange $iml 3 end]
311 " [showintervalsecs [expr {$now-$sentwhen}] 1] <$sender> $msg"
314 if {"$rel" == "unreliable"} {
315 tell_delete_msgs {} $nl
321 sendprivmsg $nl {You have messages (so identify yourself please).}
325 sendprivmsg $nl {Don't forget about your messages.}
330 mass??? - pa????v - pa????s {
335 error "tell_event_core nl=$nl evstate=$evstate ?"
338 if {![info exists u]} {
339 catch_restoreei { unset telling }
341 set telling [list $u $stt $now]
345 proc recordlastseen_n {n how here} {
347 # 0 - nick was seen leaving (or changing to another nicks or some such)
348 # 1 - nick was seen doing something else
349 # 2 - nick was seen talking on channel
350 global lastseen lookedfor
351 set nl [irctolower $n]
352 set now [clock seconds]
353 set lastseen($nl) [list $n $now $how]
357 tell_event $nl [lindex {none act talk} $here]
359 upvar #0 lookedfor($nl) lf
360 if {[info exists lf]} {
361 switch -exact [llength $lf] {
366 manyset [lindex $lf 0] when who where
368 "FYI, $who was looking for you [looking_whenwhere $when $where]."]
371 msg_begin tosend $n "FYI, people have been looking for you:"
377 msg_append tosend " "
378 } elseif {$i == [llength $lf]} {
379 msg_append tosend " and "
382 msg_append tosend ", "
384 manyset $e when who where
386 "$who ([looking_whenwhere $when $where])$fin"
388 set ml [msg_finish tosend]
392 msendprivmsg_delayed 1000 $n $ml
396 proc note_topic {showoff whoby topic} {
397 set msg "FYI, $whoby has changed the topic on $showoff"
398 if {[string length $topic] < 160} {
399 append msg " to $topic"
401 append msg " but it is too long to reproduce here !"
403 set showoff [irctolower $showoff]
404 set tell [chandb_get $showoff topictell]
405 if {[lsearch -exact $tell *] >= 0} {
406 set tryspies [chandb_list]
410 foreach spy $tryspies {
411 set see [chandb_get $spy topicsee]
412 if {[lsearch -exact $see $showoff] >= 0 || \
413 ([lsearch -exact $see *] >= 0 && \
414 [lsearch -exact $tell $spy] >= 0)} {
415 sendprivmsg $spy $msg
420 proc recordlastseen_p {p how here} {
422 recordlastseen_n $n $how $here
425 proc chanmode_arg {} {
427 set rv [lindex $cm_args 0]
428 set cm_args [lreplace cm_args 0 0]
432 proc chanmode_o1 {m g p chan} {
433 global nick chan_initialop
435 set who [chanmode_arg]
436 recordlastseen_n $n "being nice to $who" 1
437 if {"[irctolower $who]" == "[irctolower $nick]"} {
438 set nlower [irctolower $n]
439 upvar #0 nick_unique($nlower) u
440 if {[chandb_exists $chan]} {
441 sendprivmsg $n Thanks.
442 } elseif {![info exists u]} {
443 sendprivmsg $n {Op me while not on the channel, why don't you ?}
445 set chan_initialop([irctolower $chan]) $u
447 "Thanks. You can use `channel manager ...' to register this channel."
448 if {![nickdb_exists $n] || ![string length [nickdb_get $n username]]} {
450 "(But to do that you must register your nick securely first.)"
456 proc chanmode_o0 {m g p chan} {
459 set who [chanmode_arg]
460 recordlastseen_p $p "being mean to $who" 1
461 if {"[irctolower $who]" == "[irctolower $nick]"} {
462 set chandeop($chan) [list [clock seconds] $p]
466 proc msg_MODE {p c dest modelist args} {
467 if {![ischan $dest]} return
468 if {[regexp {^\-(.+)$} $modelist dummy modelist]} {
470 } elseif {[regexp {^\+(.+)$} $modelist dummy modelist]} {
473 error "invalid modelist"
475 foreach m [split $modelist] {
476 set procname chanmode_$m$give
477 if {[catch { info body $procname }]} {
478 recordlastseen_p $p "fiddling with $dest" 1
480 $procname $m $give $p $dest
485 proc leaving {lchan} {
486 foreach luser [array names nick_onchans] {
487 upvar #0 nick_onchans($luser) oc
488 set oc [grep tc {"$tc" != "$lchan"} $oc]
490 upvar #0 chan_nicks($lchan) nlist
492 upvar #0 chan_lastactivity($lchan) la
496 proc doleave {lchan} {
501 proc dojoin {lchan} {
504 set chan_nicks($lchan) {}
507 proc check_justme {lchan} {
509 upvar #0 chan_nicks($lchan) nlist
510 if {[llength $nlist] != 1} return
511 if {"[lindex $nlist 0]" != "[irctolower $nick]"} return
512 if {[chandb_exists $lchan]} {
513 set mode [chandb_get $lchan mode]
514 if {"$mode" != "*"} {
515 sendout MODE $lchan $mode
517 set topic [chandb_get $lchan topicset]
518 if {[string length $topic]} {
519 sendout TOPIC $lchan $topic
526 proc process_kickpart {chan user} {
529 set luser [irctolower $user]
530 set lchan [irctolower $chan]
531 if {![ischan $chan]} { error "not a channel" }
532 if {"$luser" == "[irctolower $nick]"} {
535 upvar #0 nick_onchans($luser) oc
536 upvar #0 chan_nicks($lchan) nlist
537 set oc [grep tc {"$tc" != "$lchan"} $oc]
538 set nlist [grep tn {"$tn" != "$luser"} $nlist]
540 if {![llength $oc]} {
548 proc msg_TOPIC {p c dest topic} {
550 if {![ischan $dest]} return
551 recordlastseen_n $n "changing the topic on $dest" 1
552 note_topic [irctolower $dest] $n $topic
555 proc msg_KICK {p c chans users comment} {
556 set chans [split $chans ,]
557 set users [split $users ,]
558 if {[llength $chans] > 1} {
559 foreach chan $chans user $users { process_kickpart $chan $user }
561 foreach user $users { process_kickpart [lindex $chans 0] $user }
565 proc msg_KILL {p c user why} {
570 set nick_arys {onchans username unique}
571 # nick_onchans($luser) -> [list ... $lchan ...]
572 # nick_username($luser) -> <securely known local username>
573 # nick_unique($luser) -> <includes-counter>
574 # nick_case($luser) -> $user (valid even if no longer visible)
575 # nick_markid($luser) -> <after id for marktime>
576 # nick_telling($luser) -> <unique> mentioned|passed <when>
578 # chan_nicks($lchan) -> [list ... $luser ...]
579 # chan_lastactivity($lchan) -> [clock seconds]
581 proc lnick_forget {luser} {
582 global nick_arys chan_nicks
583 lnick_marktime_cancel $luser
584 foreach ary $nick_arys {
585 upvar #0 nick_${ary}($luser) av
588 foreach lch [array names chan_nicks] {
589 upvar #0 chan_nicks($lch) nlist
590 set nlist [grep tn {"$tn" != "$luser"} $nlist]
595 proc nick_forget {user} {
596 global nick_arys chan_nicks
597 lnick_forget [irctolower $user]
601 proc nick_case {user} {
603 set nick_case([irctolower $user]) $user
606 proc msg_NICK {p c newnick} {
607 global nick_arys nick_case calling_nick
609 recordlastseen_n $n "changing nicks to $newnick" 0
610 set calling_nick $newnick
611 recordlastseen_n $newnick "changing nicks from $n" 1
612 set luser [irctolower $n]
613 lnick_marktime_cancel $luser
614 set lusernew [irctolower $newnick]
615 foreach ary $nick_arys {
616 upvar #0 nick_${ary}($luser) old
617 upvar #0 nick_${ary}($lusernew) new
618 if {[info exists new]} { error "nick collision ?! $ary $n $newnick" }
619 if {[info exists old]} { set new $old; unset old }
621 upvar #0 nick_onchans($lusernew) oc
623 upvar #0 chan_nicks($ch) nlist
624 set nlist [grep tn {"$tn" != "$luser"} $nlist]
625 lappend nlist $lusernew
627 lnick_marktime_start $lusernew "Hi." 500 1
631 proc nick_ishere {n} {
633 upvar #0 nick_unique([irctolower $n]) u
634 if {![info exists u]} { set u [incr nick_counter].$n.[clock seconds] }
638 proc msg_JOIN {p c chan} {
641 recordlastseen_n $n "joining $chan" 1
642 set nl [irctolower $n]
643 set lchan [irctolower $chan]
644 upvar #0 nick_onchans($nl) oc
645 upvar #0 chan_nicks($lchan) nlist
646 if {![info exists oc]} {
647 global marktime_join_startdelay
648 lnick_marktime_start $nl "Welcome." $marktime_join_startdelay 1
653 proc msg_PART {p c chan args} {
655 set msg "leaving $chan"
656 if {[llength $args]} {
657 set why [lindex $args 0]
658 if {"[irctolower $why]" != "[irctolower $n]"} { append msg " ($why)" }
660 recordlastseen_n $n $msg 1
661 process_kickpart $chan $n
663 proc msg_QUIT {p c why} {
665 recordlastseen_n $n "leaving ($why)" 0
669 proc msg_PRIVMSG {p c dest text} {
673 if {[ischan $dest]} {
674 recordlastseen_n $n "invoking me in $dest" 1
677 recordlastseen_n $n "talking to me" 1
682 execute_usercommand $p $c $n $output $dest $text
685 proc msg_INVITE {p c n chan} {
686 after 1000 [list dojoin [irctolower $chan]]
689 proc grep {var predicate list} {
693 if {[uplevel 1 [list expr $predicate]]} { lappend o $v }
698 proc msg_353 {p c dest type chan nicklist} {
699 global names_chans nick_onchans
700 set lchan [irctolower $chan]
701 upvar #0 chan_nicks($lchan) nlist
702 lappend names_chans $lchan
703 if {![info exists nlist]} {
704 # We don't think we're on this channel, so ignore it !
705 # Unfortunately, because we don't get a reply to PART,
706 # we have to remember ourselves whether we're on a channel,
707 # and ignore stuff if we're not, to avoid races. Feh.
711 foreach user [split $nicklist { }] {
712 regsub {^[@+]} $user {} user
713 if {![string length $user]} continue
715 set luser [irctolower $user]
716 upvar #0 nick_onchans($luser) oc
718 lappend nlist_new $luser
724 proc msg_366 {p c args} {
725 global names_chans nick_onchans
726 set lchan [irctolower $c]
727 foreach luser [array names nick_onchans] {
728 upvar #0 nick_onchans($luser) oc
729 if {[llength names_chans] > 1} {
730 set oc [grep tc {[lsearch -exact $tc $names_chans] >= 0} $oc]
732 if {![llength $oc]} { lnick_forget $n }
737 proc check_username {target} {
739 [string length $target] > 8 ||
740 [regexp {[^-0-9a-z]} $target] ||
741 ![regexp {^[a-z]} $target]
742 } { error "invalid username" }
745 proc somedb__head {} {
747 set idl [irctolower $id]
748 upvar #0 ${nickchan}db($idl) ndbe
749 binary scan $idl H* idh
750 set idfn $fprefix$idh
751 if {![info exists iddbe] && [file exists $idfn]} {
753 try_except_finally { set newval [read $f] } {} { close $f }
754 if {[llength $newval] % 2} { error "invalid length" }
760 proc def_somedb {name arglist body} {
761 foreach {nickchan fprefix} {
766 proc ${nickchan}db_$name $arglist \
767 "set nickchan $nickchan; set fprefix $fprefix; $body"
773 foreach path [glob -nocomplain -path $fprefix *] {
774 binary scan $path "A[string length $fprefix]A*" afprefix thinghex
775 if {"$afprefix" != "$fprefix"} { error "wrong prefix $path $afprefix" }
776 lappend list [binary format H* $thinghex]
781 proc def_somedb_id {name arglist body} {
782 def_somedb $name [concat id $arglist] "somedb__head; $body"
785 def_somedb_id exists {} {
786 return [info exists iddbe]
789 def_somedb_id delete {} {
790 catch { unset iddbe }
794 set default_settings_nick {
798 tellrel {remind 3600 30}
801 set default_settings_chan {
810 set default_settings_msgs {
814 # inbound -> [<nick> <time_t> <message>] ...
815 # outbound -> [<nick> <time_t(earliest)> <count>] ...
816 # neither are sorted particularly; only one entry per recipient in
817 # output; both sender and recipient are cased
819 def_somedb_id set {args} {
820 upvar #0 default_settings_$nickchan def
821 if {![info exists iddbe]} { set iddbe $def }
822 foreach {key value} [concat $iddbe $args] { set a($key) $value }
824 foreach {key value} [array get a] { lappend newval $key $value }
825 set f [open $idfn.new w]
829 file rename -force $idfn.new $idfn
837 def_somedb_id get {key} {
838 upvar #0 default_settings_$nickchan def
839 if {[info exists iddbe]} {
840 set l [concat $iddbe $def]
844 foreach {tkey value} $l {
845 if {"$tkey" == "$key"} { return $value }
847 error "unset setting $key"
852 if {[info exists calling_nick]} { set n $calling_nick } { set n {} }
853 return [nickdb_get $n $key]
856 proc check_notonchan {} {
858 if {[ischan $dest]} { usererror "That command must be sent privately." }
861 proc nick_securitycheck {strict} {
863 if {![nickdb_exists $n]} {
864 usererror "You are unknown to me, use `register'."
866 set wantu [nickdb_get $n username]
867 if {![string length $wantu]} {
869 usererror "That feature is only available to secure users, sorry."
874 set luser [irctolower $n]
875 upvar #0 nick_username($luser) nu
876 if {![info exists nu]} {
877 usererror "Nick $n is secure, you must identify yourself first."
879 if {"$wantu" != "$nu"} {
880 usererror "You are the wrong user -\
881 the nick $n belongs to $wantu, not $nu."
885 proc channel_ismanager {channel n} {
886 set mgrs [chandb_get $channel managers]
887 return [expr {[lsearch -exact [irctolower $mgrs] [irctolower $n]] >= 0}]
890 proc channel_securitycheck {channel} {
892 if {![channel_ismanager $channel $n]} {
893 usererror "You are not a manager of $channel."
898 proc def_chancmd {name body} {
899 proc channel/$name {} \
900 " upvar 1 target chan; upvar 1 n n; upvar 1 text text; $body"
903 proc ta_listop {findnow procvalue} {
904 # findnow and procvalue are code fragments which will be executed
905 # in the caller's level. findnow should set ta_listop_ev to
906 # the current list, and procvalue should treat ta_listop_ev as
907 # a proposed value in the list and check and possibly modify
908 # (canonicalise?) it. After ta_listop, ta_listop_ev will
909 # be the new value of the list.
910 upvar 1 ta_listop_ev exchg
913 switch -exact _$opcode {
917 foreach item $exchg { set array($item) 1 }
920 error "list change opcode must be one of + - ="
923 foreach exchg [split $text " "] {
924 if {![string length $exchg]} continue
926 if {"$opcode" != "-"} {
929 catch { unset array($exchg) }
932 set exchg [lsort [array names array]]
935 def_chancmd manager {
937 if {[chandb_exists $chan]} {
938 set ta_listop_ev [chandb_get $chan managers]
940 set ta_listop_ev [list [irctolower $n]]
943 check_nick $ta_listop_ev
944 set ta_listop_ev [irctolower $ta_listop_ev]
946 if {[llength $ta_listop_ev]} {
947 chandb_set $chan managers $ta_listop_ev
948 ucmdr "Managers of $chan: $ta_listop_ev" {}
951 ucmdr {} {} "forgets about managing $chan." {}
955 def_chancmd autojoin {
957 switch -exact [string tolower $yesno] {
960 default { error "channel autojoin must be `yes' or `no' }
962 chandb_set $chan autojoin $nv
963 ucmdr [expr {$nv ? "I will join $chan when I'm restarted " : \
964 "I won't join $chan when I'm restarted "}] {}
967 def_chancmd userinvite {
968 set nv [string tolower [ta_word]]
970 pub { set txt "!invite will work for $chan, but it won't work by /msg" }
971 here { set txt "!invite and /msg invite will work, but only for users who are already on $chan." }
972 all { set txt "Any user will be able to invite themselves or anyone else to $chan." }
973 none { set txt "I will not invite anyone to $chan." }
975 error "channel userinvite must be `pub', `here', `all' or `none'
978 chandb_set $chan userinvite $nv
984 switch -exact $what {
987 chandb_set $chan topicset {}
988 ucmdr "I won't ever change the topic of $chan." {}
991 set t [string trim $text]
992 if {![string length $t]} {
993 error "you must specific the topic to set"
995 chandb_set $chan topicset $t
996 ucmdr "Whenever I'm alone on $chan, I'll set the topic to $t." {}
1000 set ta_listop_ev [chandb_get $chan topic$what]
1002 if {"$ta_listop_ev" != "*"} {
1003 if {![ischan $ta_listop_ev]} {
1004 error "bad channel \`$ta_listop_ev' in topic $what"
1006 set ta_listop_ev [irctolower $ta_listop_ev]
1009 chandb_set $chan topic$what $ta_listop_ev
1010 ucmdr "Topic $what list for $chan: $ta_listop_ev" {}
1013 usererror "Unknown channel topic subcommand - see help channel."
1020 if {"$mode" != "*" && ![regexp {^(([-+][imnpst]+)+)$} $mode mode]} {
1021 error {channel mode must be * or match ([-+][imnpst]+)+}
1023 chandb_set $chan mode $mode
1024 if {"$mode" == "*"} {
1025 ucmdr "I won't ever change the mode of $chan." {}
1027 ucmdr "Whenever I'm alone on $chan, I'll set the mode to $mode." {}
1032 if {[chandb_exists $chan]} {
1033 set l "Settings for $chan: autojoin "
1034 append l [lindex {no yes} [chandb_get $chan autojoin]]
1035 append l ", mode " [chandb_get $chan mode]
1036 append l ", userinvite " [chandb_get $chan userinvite] "."
1037 append l "\nManagers: "
1038 append l [join [chandb_get $chan managers] " "]
1039 foreach {ts sep} {see "\n" tell " "} {
1040 set t [chandb_get $chan topic$ts]
1043 append l "Topic $ts list: $t."
1045 append l "Topic $ts list is empty."
1049 set t [chandb_get $chan topicset]
1050 if {[string length $t]} {
1051 append l "Topic to set: $t"
1053 append l "I will not change the topic."
1057 ucmdr {} "The channel $chan is not managed."
1061 proc channelmgr_monoop {} {
1066 upvar 1 target target
1071 if {[ischan $dest]} { set target $dest }
1072 if {[ta_anymore]} { set target [ta_word] }
1074 if {![info exists target]} {
1075 usererror "You must specify, or invoke me on, the relevant channel."
1077 if {![info exists chan_nicks([irctolower $target])]} {
1078 usererror "I am not on $target."
1080 if {![ischan $target]} { error "not a valid channel" }
1082 if {![chandb_exists $target]} {
1083 usererror "$target is not a managed channel."
1085 channel_securitycheck $target
1090 sendout MODE $target +o $n
1099 global chan_nicks errorCode errorInfo
1102 if {[ischan $dest]} {
1106 set target [ta_word]
1109 set ltarget [irctolower $target]
1110 if {![ischan $target]} { error "$target is not a channel" }
1111 if {![info exists chan_nicks($ltarget)]} {
1112 usererror "I am not on $target."
1114 set ui [chandb_get $ltarget userinvite]
1116 if {"$ui" == "pub" && !$onchan} {
1117 usererror "Invitations to $target must be made there with !invite."
1119 if {"$ui" != "all"} {
1120 if {[lsearch -exact $chan_nicks($ltarget) [irctolower $n]] < 0} {
1121 usererror "Invitations to $target may only be made\
1122 by a user on the channel."
1125 if {"$ui" == "none"} {
1126 usererror "Sorry, I've not been authorised\
1127 to invite people to $target."
1130 if {"$errorCode" == "BLIGHT USER" && [channel_ismanager $target $n]} {
1132 nick_securitycheck 1
1134 if {"$errorCode" == "BLIGHT USER"} {
1135 usererror "$emsg2 Therefore you can't use your\
1136 channel manager privilege. $emsg"
1138 error $error $errorInfo $errorCode
1142 error $emsg $errorInfo $errorCode
1145 if {![ta_anymore]} {
1146 usererror "You have to say who to invite."
1149 while {[ta_anymore]} {
1150 set invitee [ta_word]
1152 lappend invitees $invitee
1154 foreach invitee $invitees {
1155 sendout INVITE $invitee $ltarget
1157 set who [lindex $invitees 0]
1158 switch -exact llength $invitees {
1159 0 { error "zero invitees" }
1161 2 { append who " and [lindex $invitees 1]" }
1163 set who [join [lreplace $invitees end end] ", "]
1164 append who " and [lindex $invitees [llength $invitees]]"
1167 ucmdr {} {} {} "invites $who to $target."
1171 if {[ischan $dest]} { set target $dest }
1172 if {![ta_anymore]} {
1175 set subcmd [ta_word]
1177 if {[ischan $subcmd]} {
1179 if {![ta_anymore]} {
1182 set subcmd [ta_word]
1185 if {![info exists target]} { error "privately, you must specify a channel" }
1186 set procname channel/$subcmd
1187 if {"$subcmd" != "show"} {
1188 if {[catch { info body $procname }]} {
1189 usererror "unknown channel setting $subcmd."
1192 if {[chandb_exists $target]} {
1193 channel_securitycheck $target
1195 nick_securitycheck 1
1196 upvar #0 chan_initialop([irctolower $target]) io
1197 upvar #0 nick_unique([irctolower $n]) u
1198 if {![info exists io]} {
1199 usererror "$target is not a managed channel."
1201 if {"$io" != "$u"} {
1202 usererror "You are not the interim manager of $target."
1204 if {"$subcmd" != "manager"} {
1205 usererror "Please use `channel manager' first."
1212 proc tell_effective_sec {n} {
1213 set l [nickdb_get $n tellsec]
1214 set u [nickdb_get $n username]
1215 if {"[lindex $l 0]" == "secure" && ![string length $u]} { set l insecure }
1219 proc tell_peernicks {text} {
1220 set text [irctolower [string trim $text]]
1221 set senders [split $text " "]
1222 foreach sender $senders {
1223 if {[catch { check_nick $sender } emsg]} {
1224 error "invalid sender nick `$sender': $emsg" $errorInfo $errorCode
1230 proc msgsdb_set_maydelete {n key l otherkey} {
1231 msgsdb_set $n $key $l
1232 if {[llength $l]} return
1233 if {[llength [msgsdb_get $n $otherkey]]} return
1237 proc tell_delete_msgs {lsenders lrecip} {
1240 foreach {s t m} [msgsdb_get $recip inbound] {
1241 if {[llength $senders]} {
1242 if {[lsearch -exact $senders [irctolower $s]] == -1} {
1243 lappend ninbound $s $t $m
1250 msgsdb_set_maydelete $recip inbound $ninbound outbound
1251 if {![llength $ninbound]} {
1252 upvar #0 nick_telling($lrecip) telling
1253 catch { unset telling }
1255 foreach s [array names rsenders] {
1257 foreach {r t c} [msgsdb_get $s outbound] {
1258 if {"[irctolower $r]" == "$lrecip"} continue
1259 lappend noutbound $r $t $c
1261 msgsdb_set_maydelete $s outbound $noutbound inbound
1269 nick_securitycheck 0
1270 set recipients [tell_peernicks $text]
1271 if {![llength $recipients]} {
1272 usererror "You must say which recipients' messages from you to forget."
1275 foreach recip $recipients {
1276 incr ndel [tell_delete_msgs [irctolower $n] $recip]
1278 ucmdr "Removed $ndel as yet undelivered message(s)." {}
1282 global errorInfo errorCode
1284 set nl [irctolower $n]
1286 manyset [tell_effective_sec $n] sec secwhen
1287 switch -exact $sec {
1291 "There are no messages to delete\
1292 because your message disposition prevents them from being left."
1295 nick_securitycheck 1
1298 error "delmsg sec $sec"
1302 if {"$stt" != "passed"} {
1304 "There are message(s) you've not yet seen; I'll deliver them to you now.\
1305 If you actually want to delete them, just tell me `delmsg' again."
1307 set senders [tell_peernicks $text]
1308 set ndel [tell_delete_msgs [irctolower $senders] [irctolower $n]]
1310 if {[llength $senders]} {
1311 ucmdr "No relevant incoming messages to delete." {}
1313 ucmdr "No incoming messages to delete." {}
1316 switch -exact [llength $senders] {
1317 0 { ucmdr {} {} "deletes your $ndel message(s)." }
1318 1 { ucmdr {} {} "deletes your $ndel message(s) from $senders." }
1320 ucmdr {} {} "deletes your $ndel message(s) from\
1321 [lreplace $senders end end] and [lindex $senders end]."
1327 global nick_case ownmailaddr ownfullname
1330 set target [ta_word]
1331 if {![string length $text]} { error "tell them what?" }
1332 if {[string length $text] > 400} { error "message too long" }
1334 set ltarget [irctolower $target]
1336 if {[info exists nick_case($ltarget)]} { set ctarget $nick_case($ltarget) }
1338 manyset [tell_effective_sec $target] sec mailtoint mailwhy
1339 manyset [nickdb_get $target tellrel] rel relint relwithin
1340 switch -exact $sec {
1342 set now [clock seconds]
1343 set inbound [msgsdb_get $ltarget inbound]
1344 lappend inbound $n $now $text
1345 msgsdb_set $ltarget inbound $inbound
1347 set outbound [msgsdb_get $n outbound]
1350 foreach {recip time count} $outbound {
1351 if {"[irctolower $recip]" == "$ltarget"} {
1356 lappend noutbound $recip $time $count
1359 lappend noutbound $ctarget $now 1
1361 msgsdb_set $n outbound $noutbound
1362 set msg "OK, I'll tell $ctarget"
1363 if {$found} { append msg " that too" }
1365 if {"$sec" != "secure"} {
1366 switch -exact $rel {
1367 unreliable { append msg "neither reliably nor securely" }
1368 remind { append msg "pretty reliably, but not securely" }
1369 pester { append msg "reliably but not securely" }
1372 switch -exact $rel {
1373 unreliable { append msg "securely but not reliably" }
1374 remind { append msg "securely and pretty reliably" }
1375 pester { append msg "reliably and securely" }
1379 tell_event $ltarget msgsarrive
1383 set fmtmsg [exec fmt << " $text"]
1384 exec /usr/sbin/sendmail -odb -oi -t -oee -f $mailwhy \
1386 "From: $ownmailaddr ($ownfullname)
1388 Subject: IRC tell from $n
1390 $n asked me[expr {[ischan $dest] ? " on $dest" : ""}] to tell you:
1391 [exec fmt << " $text"]
1393 (This message was for your nick $ctarget; your account $mailwhy
1394 arranged for it to be forwarded to $mailtoint.)
1397 "I've mailed $ctarget, which is what they prefer." \
1401 usererror "Sorry, $ctarget does not want me to take messages."
1404 error "bad tellsec $sec"
1411 set target [ta_word]; ta_nomore
1416 set myself [expr {"$target" != "$n"}]
1418 set ltarget [irctolower $target]
1419 upvar #0 nick_case($ltarget) ctarget
1421 if {[info exists ctarget]} {
1422 upvar #0 nick_onchans($ltarget) oc
1423 upvar #0 nick_username($ltarget) nu
1424 if {[info exists oc]} { set nshow $ctarget }
1426 if {![nickdb_exists $ltarget]} {
1427 set ol "$nshow is not a registered nick."
1428 } elseif {[string length [set username [nickdb_get $target username]]]} {
1429 set ol "The nick $nshow belongs to the user $username."
1431 set ol "The nick $nshow is registered (but not to a username)."
1433 if {![info exists ctarget] || ![info exists oc]} {
1435 append ol "\nI can't see $nshow on anywhere."
1437 append ol "\nYou aren't on any channels with me."
1439 } elseif {![info exists nu]} {
1440 append ol "\n$nshow has not identified themselves."
1441 } elseif {![info exists username]} {
1442 append ol "\n$nshow has identified themselves as the user $nu."
1443 } elseif {"$nu" != "$username"} {
1444 append ol "\nHowever, $nshow is being used by the user $nu."
1446 append ol "\n$nshow has identified themselves to me."
1454 set old [nickdb_exists $n]
1455 if {$old} { nick_securitycheck 0 }
1456 set luser [irctolower $n]
1457 switch -exact [string tolower [string trim $text]] {
1459 upvar #0 nick_username($luser) nu
1460 if {![info exists nu]} {
1462 "You must identify yourself before using `register'. See `help identify', or use `register insecure'."
1464 nickdb_set $n username $nu
1465 ucmdr {} {} "makes a note of your username." {}
1469 ucmdr {} {} "forgets your nickname." {}
1472 nickdb_set $n username {}
1474 ucmdr {} "Security is now disabled for your nickname !"
1476 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 error "you mean register / register delete / register insecure"
1485 proc timeformat_desc {tf} {
1487 ks { return "Times will be displayed in seconds or kiloseconds." }
1488 hms { return "Times will be displayed in hours, minutes, etc." }
1489 default { error "invalid timeformat: $v" }
1494 proc def_setting {opt show_body set_body} {
1496 lappend settings $opt
1497 proc set_show/$opt {} "
1501 if {![string length $set_body]} return
1502 proc set_set/$opt {} "
1509 proc tellme_sec_desc {v n} {
1510 manyset $v sec mailtoint
1511 switch -exact $sec {
1513 return "I'll tell you your messages whenever I see you."
1516 if {[string length [nickdb_get $n username]]} {
1518 "I'll keep the bodies of your messages private until you identify yourself, reminding you every [showintervalsecs $mailtoint 1]."
1521 "I'll tell you your messages whenever I see you.\
1522 (Secure message delivery is enabled, but your nick is not registered\
1523 securely. See `help register'.)"
1527 return "I shan't accept messages for you."
1530 return "I'll forward your messages by email to $mailtoint."
1533 error "bad tellsec $sec"
1538 proc tellme_rel_desc {v} {
1539 manyset $v rel every within
1540 switch -exact $rel {
1542 return "As soon as I've told you message(s), I'll forget them\
1543 - note that this means messages can get lost !"
1549 set u ", or talk on channel within [showintervalsecs $within 1] of me having told you"
1552 error "bad tellrel $rel"
1555 return "After delivering messages, I'll remind you every\
1556 [showintervalsecs $every 1] until you say delmsg$u."
1559 def_setting timeformat {
1560 set tf [nickdb_get $n timeformat]
1561 return "$tf: [timeformat_desc $tf]"
1563 set tf [string tolower [ta_word]]
1565 set desc [timeformat_desc $tf]
1566 nickdb_set $n timeformat $tf
1570 proc marktime_desc {mt} {
1571 if {"$mt" == "off"} {
1572 return "I will not send you periodic messages."
1573 } elseif {"$mt" == "once"} {
1574 return "I will send you one informational message when I see you."
1576 return "I'll send you a message every [showintervalsecs $mt 0]."
1580 def_setting marktime {
1581 set mt [nickdb_get $n marktime]
1583 if {[string match {[0-9]*} $mt]} { append p s }
1585 append p [marktime_desc $mt]
1589 set mt [string tolower [ta_word]]
1592 if {"$mt" == "off" || "$mt" == "once"} {
1594 set mt [parse_interval $mt $marktime_min]
1596 nickdb_set $n marktime $mt
1597 lnick_marktime_start [irctolower $n] "So:" 500 0
1598 ucmdr {} [marktime_desc $mt]
1601 def_setting security {
1602 set s [nickdb_get $n username]
1603 if {[string length $s]} {
1604 return "Your nick, $n, is controlled by the user $s."
1606 return "Your nick, $n, is not secure."
1610 proc tellme_setting_sec_simple {} {
1618 proc tellme_setting_neednomsgs {} {
1620 if {[llength [msgsdb_get $n inbound]]} {
1621 usererror "You must delete the messages you have, first."
1626 def_setting tellme {
1627 set secv [nickdb_get $n tellsec]
1628 set ms [tellme_sec_desc $secv $n]
1630 switch -exact $sec {
1632 set mr [tellme_rel_desc [nickdb_get $n tellrel]]
1640 set setting [string tolower [ta_word]]
1641 switch -exact $setting {
1643 tellme_setting_sec_simple
1646 set every [ta_interval_optional 60 600]
1649 set v [list secure $every]
1652 telling_setting_neednomsgs
1653 telling_setting_sec_simple
1656 telling_setting_neednomsgs
1657 set u [nickdb_get $n username]
1658 if {[string length $u]} {
1660 "Sorry, you must register securely to have your messages mailed\
1661 (to prevent the use of this feature for spamming). See `help register'."
1664 set v [list mailto [ta_word] $u]
1666 unreliable - pester - remind {
1667 manyset [nickdb_get $n tellsec] sec
1668 switch -exact $sec {
1671 "You can't change your message delivery conditions when\
1672 your message disposition prevents messages from being left."
1677 if {"$setting" != "unreliable"} {
1678 set every [parse_interval_optional 300 3600]
1681 if {"$setting" == "remind"} {
1682 set within [ta_interval_optional 5 30]
1683 if {$within > $every} {
1684 error "remind interval must be at least time to respond"
1691 error "invalid tellme setting $setting"
1694 nickdb_set $n tell$sr $v
1695 ucmdr [tellme_${sr}_desc $v] {}
1698 proc lnick_checktold {luser} {
1699 set ml [msgsdb_get $luser outbound]
1700 if {![llength $ml]} return
1701 set is1 [expr {[llength $ml]==3}]
1702 set m1 "FYI, I haven't yet passed on your"
1704 set now [clock seconds]
1705 while {[llength $ml]} {
1707 set ml [lreplace $ml 0 2]
1708 set td [expr {$now-$t}]
1710 set iv [showinterval $td]
1712 set if1 "message to $r, $iv."
1714 set iv [showintervalsecs $td 0]
1715 set ifo "$r, $n messages, oldest $iv"
1716 set if1 "$n messages to $r, oldest $iv."
1719 sendprivmsg $luser "$m1 $if1"
1722 lappend ol " to $ifo[expr {[llength $ml] ? ";" : "."}]"
1725 sendprivmsg $luser "$m1 messages:"
1726 msendprivmsg $luser $ol
1733 if {![nickdb_exists $n]} {
1734 ucmdr {} "You are unknown to me and so have no settings. (Use `register'.)"
1736 if {![ta_anymore]} {
1738 foreach opt $settings {
1739 lappend ol [format "%-10s %s" $opt [set_show/$opt]]
1741 ucmdr {} [join $ol "\n"]
1744 if {[catch { info body set_show/$opt }]} {
1745 error "no setting $opt"
1747 if {![ta_anymore]} {
1748 ucmdr {} "$opt: [set_show/$opt]"
1750 nick_securitycheck 0
1751 if {[catch { info body set_set/$opt }]} {
1752 error "setting $opt cannot be set with `set'"
1759 def_ucmd identpass {
1762 set luser [irctolower $n]
1763 set username [ta_word]
1764 set passmd5 [md5sum "[ta_word]\n"]
1766 upvar #0 nick_onchans($luser) onchans
1767 if {![info exists onchans] || ![llength $onchans]} {
1768 ucmdr "You must be on a channel with me to identify yourself." {}
1770 check_username $username
1771 exec userv --timeout 3 $username << "$passmd5\n" > /dev/null \
1773 upvar #0 nick_username($luser) rec_username
1774 set rec_username $username
1775 ucmdr "Pleased to see you, $username." {}
1776 tell_event $luser ident
1780 set target [ta_word]
1782 check_username $target
1785 upvar #0 lastsummon($target) ls
1786 set now [clock seconds]
1787 if {[info exists ls]} {
1788 set interval [expr {$now - $ls}]
1789 if {$interval < 30} {
1791 "Please be patient; $target was summoned only [showinterval $interval]."
1794 regsub {^[^!]*!} $p {} path
1796 exec userv --timeout 3 $target irc-summon $n $path \
1797 [expr {[ischan $dest] ? "$dest" : ""}] \
1800 regsub -all "\n" $rv { / } rv
1803 if {[regexp {^problem (.*)} $rv dummy problem]} {
1804 ucmdr {} "The user `$target' $problem."
1805 } elseif {[regexp {^ok ([^ ]+) ([0-9]+)$} $rv dummy tty idlesince]} {
1806 set idletime [expr {$now - $idlesince}]
1808 ucmdr {} {} {} "invites $target ($tty[expr {
1809 $idletime > 10 ? ", idle for [showintervalsecs $idletime 0]" : ""
1811 [ischan $dest] ? "join us here" : "talk to you"
1814 error "unexpected response from userv service: $rv"
1818 proc md5sum {value} { exec md5sum << $value }
1821 global lastseen nick
1824 set nlower [irctolower $ncase]
1826 set now [clock seconds]
1827 if {"$nlower" == "[irctolower $nick]"} {
1828 usererror "I am not self-aware."
1829 } elseif {![info exists lastseen($nlower)]} {
1830 set rstr "I've never seen $ncase."
1832 manyset $lastseen($nlower) realnick time what
1833 set howlong [expr {$now - $time}]
1834 set string [showinterval $howlong]
1835 set rstr "I last saw $realnick $string, $what."
1837 if {[ischan $dest]} {
1842 upvar #0 lookedfor($nlower) lf
1843 if {[info exists lf]} { set oldvalue $lf } else { set oldvalue {} }
1844 set lf [list [list $now $n $where]]
1845 foreach v $oldvalue {
1846 if {"[irctolower [lindex $v 1]]" == "[irctolower $n]"} continue
1852 proc lnick_marktime_cancel {luser} {
1853 upvar #0 nick_markid($luser) mi
1854 if {![info exists mi]} return
1855 catch { after cancel $mi }
1859 proc lnick_marktime_doafter {luser why ms mentiontold} {
1860 lnick_marktime_cancel $luser
1861 upvar #0 nick_markid($luser) mi
1862 set mi [after $ms [list lnick_marktime_now $luser $why 0]]
1865 proc lnick_marktime_reset {luser} {
1866 set mt [nickdb_get $luser marktime]
1867 if {"$mt" == "off" || "$mt" == "once"} return
1868 lnick_marktime_doafter $luser "Time passes." [expr {$mt*1000}] 0
1871 proc lnick_marktime_start {luser why ms mentiontold} {
1872 set mt [nickdb_get $luser marktime]
1873 if {"$mt" == "off"} {
1874 lnick_marktime_cancel $luser
1875 if {$mentiontold} { after $ms [list lnick_checktold $luser] }
1877 lnick_marktime_doafter $luser $why $ms $mentiontold
1881 proc lnick_marktime_now {luser why mentiontold} {
1882 upvar #0 nick_onchans($luser) oc
1884 set calling_nick $luser
1885 sendprivmsg $luser [lnick_pingstring $why $oc ""]
1886 if {$mentiontold} { lnick_checktold $luser }
1887 lnick_marktime_reset $luser
1890 proc lnick_pingstring {why oc apstring} {
1892 catch { exec uptime } uptime
1893 set nnicks [llength [array names nick_onchans]]
1895 {^ *([0-9:apm]+) +up.*, +(\d+) users?, +load average: +([0-9., ]+) *$} \
1896 $uptime dummy time users load]} {
1897 regsub -all , $load {} load
1898 set uptime "$time $nnicks/$users $load"
1900 append uptime ", $nnicks nicks"
1902 if {[llength $oc]} {
1906 upvar #0 chan_lastactivity($ch) la
1907 if {![info exists la]} continue
1908 if {$la <= $best_la} continue
1909 set since [showintervalsecs [expr {[clock seconds]-$la}] 1]
1910 set activity "$ch $since"
1917 append str " " $uptime " " $activity
1918 if {[string length $apstring]} { append str " " $apstring }
1924 set ln [irctolower $n]
1925 if {[ischan $dest]} {
1926 set oc [irctolower $dest]
1929 if {[info exists nick_onchans($ln)]} {
1930 set oc $nick_onchans($ln)
1934 if {[llength $oc]} { lnick_marktime_reset $ln }
1937 ucmdr {} [lnick_pingstring "Pong!" $oc $text]
1940 proc ensure_globalsecret {} {
1943 if {[info exists globalsecret]} return
1944 set gsfile [open /dev/urandom r]
1945 fconfigure $gsfile -translation binary
1946 set globalsecret [read $gsfile 32]
1947 binary scan $globalsecret H* globalsecret
1953 foreach chan [chandb_list] {
1954 if {[chandb_get $chan autojoin]} { dojoin $chan }