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 switch -exact $event {
158 tell_event_core $nl talk
162 after 750 [list tell_event_core $nl $event]
165 tell_event_core $nl $event
168 error "tell_event $nl $event"
171 if {[info exists save]} { set calling_nick $save }
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
179 upvar #0 nick_telling($nl) telling
180 upvar #0 nick_unique($nl) u
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
189 if {![info exists stt]} {
191 set telling_when $now
196 proc tell_event_core {nl event} {
197 catch_logged { tell_event_core1 $nl $event }
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
205 set iml [msgsdb_get $nl inbound]
206 if {![llength $iml]} return
208 set now [clock seconds]
210 set ago [expr {$now - $telling_when}]
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
217 # We compute an evstate to dispatch on as follows:
219 # evstate is string of letters
221 # n NORECORD (MESSAGES)
229 # security level and timing
231 # ss Secure and soon (before interval)
232 # sl Secure and late (after interval)
233 # current identification
236 # reliability and timing
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)
246 append evstate [string range $stt 0 0]
247 append evstate [string range $event 0 0]
249 manyset [nickdb_get_sec_effective $nl] sec secwhen
251 insecure { append evstate ii }
252 secure { append evstate [expr {$ago<$secwhen ? "sl" : "ss"}] }
253 default { append evstate "#$sec#" }
256 upvar #0 nick_username($nl) nu
257 if {[info exists nu] && "$nu" == "[nickdb_get_username $nl]"} {
263 manyset [nickdb_get $nl tellrel] rel relint relwithin
265 unreliable { append evstate uu }
266 remind { append evstate [expr {
267 $ago<$relwithin ? "rv" : $ago<$relint ? "rs" : "rl"
269 pester { append evstate [expr {$ago<$relint ? "ps" : "pl"}] }
270 default { append evstate "#$rel#" }
273 log "tell_event_core1 core evstate=$evstate (ago=$ago)"
274 switch -glob $evstate {
276 # consider delivered:
277 # (very recently passed, and the user talks)
278 tell_delete_msgs {} $nl
282 # oops, messages passed are now out of date
283 catch_restoreei { unset telling }
287 # ignore new msgs if we haven't passed yet
291 pt???uu - pt???rs - pt???rl - pt???p? {
292 # ignore (any other `talk's) - act handles these
295 ni????? - naii??? - nas?i?? - mi????? - pa????l {
296 # pass and then stuff
297 if {[llength $iml] == 3} {
298 manyset $iml sender sentwhen msg
300 "$sender asked me [showinterval [expr {$now-$sentwhen}]]\
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]
309 " [showintervalsecs [expr {$now-$sentwhen}] 1] <$sender> $msg"
312 if {"$rel" == "unreliable"} {
313 tell_delete_msgs {} $nl
319 sendprivmsg $nl {You have messages (so identify yourself please).}
323 sendprivmsg $nl {Don't forget about your messages.}
328 mass??? - pa????v - pa????s {
333 error "tell_event_core nl=$nl evstate=$evstate ?"
336 if {![info exists u]} {
337 catch_restoreei { unset telling }
339 set telling [list $u $stt $now]
343 proc recordlastseen_n {n how here} {
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]
355 tell_event $nl [lindex {none act talk} $here]
357 upvar #0 lookedfor($nl) lf
358 if {[info exists lf]} {
359 switch -exact [llength $lf] {
364 manyset [lindex $lf 0] when who where
366 "FYI, $who was looking for you [looking_whenwhere $when $where]."]
369 msg_begin tosend $n "FYI, people have been looking for you:"
375 msg_append tosend " "
376 } elseif {$i == [llength $lf]} {
377 msg_append tosend " and "
380 msg_append tosend ", "
382 manyset $e when who where
384 "$who ([looking_whenwhere $when $where])$fin"
386 set ml [msg_finish tosend]
390 msendprivmsg_delayed 1000 $n $ml
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"
399 append msg " but it is too long to reproduce here !"
401 set showoff [irctolower $showoff]
402 set tell [chandb_get $showoff topictell]
403 if {[lsearch -exact $tell *] >= 0} {
404 set tryspies [chandb_list]
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
418 proc recordlastseen_p {p how here} {
420 recordlastseen_n $n $how $here
423 proc chanmode_arg {} {
425 set rv [lindex $cm_args 0]
426 set cm_args [lreplace cm_args 0 0]
430 proc chanmode_o1 {m g p chan} {
431 global nick chan_initialop
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 ?}
443 set chan_initialop([irctolower $chan]) $u
445 "Thanks. You can use `channel manager ...' to register this channel."
446 if {![string length [nickdb_get_username $n username]]} {
448 "(But to do that you must register your nick securely first.)"
454 proc chanmode_o0 {m g p chan} {
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]
464 proc msg_MODE {p c dest modelist args} {
465 if {![ischan $dest]} return
466 if {[regexp {^\-(.+)$} $modelist dummy modelist]} {
468 } elseif {[regexp {^\+(.+)$} $modelist dummy modelist]} {
471 error "invalid modelist"
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
478 $procname $m $give $p $dest
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]
488 upvar #0 chan_nicks($lchan) nlist
490 upvar #0 chan_lastactivity($lchan) la
494 proc doleave {lchan} {
499 proc dojoin {lchan} {
502 set chan_nicks($lchan) {}
505 proc check_justme {lchan} {
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
515 set topic [chandb_get $lchan topicset]
516 if {[string length $topic]} {
517 sendout TOPIC $lchan $topic
524 proc process_kickpart {chan user} {
527 set luser [irctolower $user]
528 set lchan [irctolower $chan]
529 if {![ischan $chan]} { error "not a channel" }
530 if {"$luser" == "[irctolower $nick]"} {
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]
538 if {![llength $oc]} {
546 proc msg_TOPIC {p c dest topic} {
548 if {![ischan $dest]} return
549 recordlastseen_n $n "changing the topic on $dest" 1
550 note_topic [irctolower $dest] $n $topic
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 }
559 foreach user $users { process_kickpart [lindex $chans 0] $user }
563 proc msg_KILL {p c user why} {
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>
576 # chan_nicks($lchan) -> [list ... $luser ...]
577 # chan_lastactivity($lchan) -> [clock seconds]
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
586 foreach lch [array names chan_nicks] {
587 upvar #0 chan_nicks($lch) nlist
588 set nlist [grep tn {"$tn" != "$luser"} $nlist]
593 proc nick_forget {user} {
594 global nick_arys chan_nicks
595 lnick_forget [irctolower $user]
599 proc nick_case {user} {
601 set nick_case([irctolower $user]) $user
604 proc msg_NICK {p c newnick} {
605 global nick_arys nick_case calling_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 }
619 upvar #0 nick_onchans($lusernew) oc
621 upvar #0 chan_nicks($ch) nlist
622 set nlist [grep tn {"$tn" != "$luser"} $nlist]
623 lappend nlist $lusernew
625 lnick_marktime_start $lusernew "Hi." 500 1
629 proc nick_ishere {n} {
631 upvar #0 nick_unique([irctolower $n]) u
632 if {![info exists u]} { set u [incr nick_counter].$n.[clock seconds] }
636 proc msg_JOIN {p c chan} {
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
651 proc msg_PART {p c chan args} {
653 set msg "leaving $chan"
654 if {[llength $args]} {
655 set why [lindex $args 0]
656 if {"[irctolower $why]" != "[irctolower $n]"} { append msg " ($why)" }
658 recordlastseen_n $n $msg 1
659 process_kickpart $chan $n
661 proc msg_QUIT {p c why} {
663 recordlastseen_n $n "leaving ($why)" 0
667 proc msg_PRIVMSG {p c dest text} {
671 if {[ischan $dest]} {
672 recordlastseen_n $n "invoking me in $dest" 1
675 recordlastseen_n $n "talking to me" 1
680 execute_usercommand $p $c $n $output $dest $text
683 proc msg_INVITE {p c n chan} {
684 after 1000 [list dojoin [irctolower $chan]]
687 proc grep {var predicate list} {
691 if {[uplevel 1 [list expr $predicate]]} { lappend o $v }
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.
709 foreach user [split $nicklist { }] {
710 regsub {^[@+]} $user {} user
711 if {![string length $user]} continue
713 set luser [irctolower $user]
714 upvar #0 nick_onchans($luser) oc
716 lappend nlist_new $luser
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]
730 if {![llength $oc]} { lnick_forget $n }
735 proc check_username {target} {
737 [string length $target] > 8 ||
738 [regexp {[^-0-9a-z]} $target] ||
739 ![regexp {^[a-z]} $target]
740 } { error "invalid username" }
743 proc somedb__head {} {
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]} {
751 try_except_finally { set newval [read $f] } {} { close $f }
752 if {[llength $newval] % 2} { error "invalid length" }
758 proc def_somedb {name arglist body} {
759 foreach {nickchan fprefix} {
764 proc ${nickchan}db_$name $arglist \
765 "set nickchan $nickchan; set fprefix $fprefix; $body"
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]
779 proc def_somedb_id {name arglist body} {
780 def_somedb $name [concat id $arglist] "somedb__head; $body"
783 def_somedb_id exists {} {
784 return [info exists iddbe]
787 def_somedb_id delete {} {
788 catch { unset iddbe }
792 set default_settings_nick {
796 tellrel {remind 3600 30}
799 set default_settings_chan {
808 set default_settings_msgs {
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
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 }
822 foreach {key value} [array get a] { lappend newval $key $value }
823 set f [open $idfn.new w]
827 file rename -force $idfn.new $idfn
835 def_somedb_id get {key} {
836 upvar #0 default_settings_$nickchan def
837 if {[info exists iddbe]} {
838 set l [concat $iddbe $def]
842 foreach {tkey value} $l {
843 if {"$tkey" == "$key"} { return $value }
845 error "unset setting $key"
850 if {[info exists calling_nick]} { set n $calling_nick } { set n {} }
851 return [nickdb_get $n $key]
854 proc check_notonchan {} {
856 if {[ischan $dest]} { usererror "That command must be sent privately." }
859 proc nick_securitycheck {strict} {
861 if {![nickdb_exists $n]} {
862 usererror "You are unknown to me, use `register'."
864 set wantu [nickdb_get $n username]
865 if {![string length $wantu]} {
867 usererror "That feature is only available to secure users, sorry."
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."
877 if {"$wantu" != "$nu"} {
878 usererror "You are the wrong user -\
879 the nick $n belongs to $wantu, not $nu."
883 proc channel_ismanager {channel n} {
884 set mgrs [chandb_get $channel managers]
885 return [expr {[lsearch -exact [irctolower $mgrs] [irctolower $n]] >= 0}]
888 proc channel_securitycheck {channel} {
890 if {![channel_ismanager $channel $n]} {
891 usererror "You are not a manager of $channel."
896 proc def_chancmd {name body} {
897 proc channel/$name {} \
898 " upvar 1 target chan; upvar 1 n n; upvar 1 text text; $body"
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
911 switch -exact _$opcode {
915 foreach item $exchg { set array($item) 1 }
918 error "list change opcode must be one of + - ="
921 foreach exchg [split $text " "] {
922 if {![string length $exchg]} continue
924 if {"$opcode" != "-"} {
927 catch { unset array($exchg) }
930 set exchg [lsort [array names array]]
933 def_chancmd manager {
935 if {[chandb_exists $chan]} {
936 set ta_listop_ev [chandb_get $chan managers]
938 set ta_listop_ev [list [irctolower $n]]
941 check_nick $ta_listop_ev
942 set ta_listop_ev [irctolower $ta_listop_ev]
944 if {[llength $ta_listop_ev]} {
945 chandb_set $chan managers $ta_listop_ev
946 ucmdr "Managers of $chan: $ta_listop_ev" {}
949 ucmdr {} {} "forgets about managing $chan." {}
953 def_chancmd autojoin {
955 switch -exact [string tolower $yesno] {
958 default { error "channel autojoin must be `yes' or `no' }
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 "}] {}
965 def_chancmd userinvite {
966 set nv [string tolower [ta_word]]
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." }
973 error "channel userinvite must be `pub', `here', `all' or `none'
976 chandb_set $chan userinvite $nv
982 switch -exact $what {
985 chandb_set $chan topicset {}
986 ucmdr "I won't ever change the topic of $chan." {}
989 set t [string trim $text]
990 if {![string length $t]} {
991 error "you must specific the topic to set"
993 chandb_set $chan topicset $t
994 ucmdr "Whenever I'm alone on $chan, I'll set the topic to $t." {}
998 set ta_listop_ev [chandb_get $chan topic$what]
1000 if {"$ta_listop_ev" != "*"} {
1001 if {![ischan $ta_listop_ev]} {
1002 error "bad channel \`$ta_listop_ev' in topic $what"
1004 set ta_listop_ev [irctolower $ta_listop_ev]
1007 chandb_set $chan topic$what $ta_listop_ev
1008 ucmdr "Topic $what list for $chan: $ta_listop_ev" {}
1011 usererror "Unknown channel topic subcommand - see help channel."
1018 if {"$mode" != "*" && ![regexp {^(([-+][imnpst]+)+)$} $mode mode]} {
1019 error {channel mode must be * or match ([-+][imnpst]+)+}
1021 chandb_set $chan mode $mode
1022 if {"$mode" == "*"} {
1023 ucmdr "I won't ever change the mode of $chan." {}
1025 ucmdr "Whenever I'm alone on $chan, I'll set the mode to $mode." {}
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]
1041 append l "Topic $ts list: $t."
1043 append l "Topic $ts list is empty."
1047 set t [chandb_get $chan topicset]
1048 if {[string length $t]} {
1049 append l "Topic to set: $t"
1051 append l "I will not change the topic."
1055 ucmdr {} "The channel $chan is not managed."
1059 proc channelmgr_monoop {} {
1064 upvar 1 target target
1069 if {[ischan $dest]} { set target $dest }
1070 if {[ta_anymore]} { set target [ta_word] }
1072 if {![info exists target]} {
1073 usererror "You must specify, or invoke me on, the relevant channel."
1075 if {![info exists chan_nicks([irctolower $target])]} {
1076 usererror "I am not on $target."
1078 if {![ischan $target]} { error "not a valid channel" }
1080 if {![chandb_exists $target]} {
1081 usererror "$target is not a managed channel."
1083 channel_securitycheck $target
1088 sendout MODE $target +o $n
1097 global chan_nicks errorCode errorInfo
1100 if {[ischan $dest]} {
1104 set target [ta_word]
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."
1112 set ui [chandb_get $ltarget userinvite]
1114 if {"$ui" == "pub" && !$onchan} {
1115 usererror "Invitations to $target must be made there with !invite."
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."
1123 if {"$ui" == "none"} {
1124 usererror "Sorry, I've not been authorised\
1125 to invite people to $target."
1128 if {"$errorCode" == "BLIGHT USER" && [channel_ismanager $target $n]} {
1130 nick_securitycheck 1
1132 if {"$errorCode" == "BLIGHT USER"} {
1133 usererror "$emsg2 Therefore you can't use your\
1134 channel manager privilege. $emsg"
1136 error $error $errorInfo $errorCode
1140 error $emsg $errorInfo $errorCode
1143 if {![ta_anymore]} {
1144 usererror "You have to say who to invite."
1147 while {[ta_anymore]} {
1148 set invitee [ta_word]
1150 lappend invitees $invitee
1152 foreach invitee $invitees {
1153 sendout INVITE $invitee $ltarget
1155 set who [lindex $invitees 0]
1156 switch -exact llength $invitees {
1157 0 { error "zero invitees" }
1159 2 { append who " and [lindex $invitees 1]" }
1161 set who [join [lreplace $invitees end end] ", "]
1162 append who " and [lindex $invitees [llength $invitees]]"
1165 ucmdr {} {} {} "invites $who to $target."
1169 if {[ischan $dest]} { set target $dest }
1170 if {![ta_anymore]} {
1173 set subcmd [ta_word]
1175 if {[ischan $subcmd]} {
1177 if {![ta_anymore]} {
1180 set subcmd [ta_word]
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."
1190 if {[chandb_exists $target]} {
1191 channel_securitycheck $target
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."
1199 if {"$io" != "$u"} {
1200 usererror "You are not the interim manager of $target."
1202 if {"$subcmd" != "manager"} {
1203 usererror "Please use `channel manager' first."
1210 proc nickdb_get_username {n} {
1211 if {![nickdb_exists $n]} { return "" }
1212 return [nickdb_get $n username]
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 }
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
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
1240 proc tell_delete_msgs {lsenders lrecip} {
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
1253 msgsdb_set_maydelete $recip inbound $ninbound outbound
1254 if {![llength $ninbound]} {
1255 upvar #0 nick_telling($lrecip) telling
1256 catch { unset telling }
1258 foreach s [array names rsenders] {
1260 foreach {r t c} [msgsdb_get $s outbound] {
1261 if {"[irctolower $r]" == "$lrecip"} continue
1262 lappend noutbound $r $t $c
1264 msgsdb_set_maydelete $s outbound $noutbound inbound
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."
1278 foreach recip $recipients {
1279 incr ndel [tell_delete_msgs [irctolower $n] $recip]
1281 ucmdr "Removed $ndel as yet undelivered message(s)." {}
1285 global errorInfo errorCode
1287 set nl [irctolower $n]
1289 manyset [nickdb_get_sec_effective $n] sec secwhen
1290 switch -exact $sec {
1294 "There are no messages to delete\
1295 because your message disposition prevents them from being left."
1298 nick_securitycheck 1
1301 error "delmsg sec $sec"
1305 if {"$stt" != "passed"} {
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."
1310 set senders [tell_peernicks $text]
1311 set ndel [tell_delete_msgs [irctolower $senders] [irctolower $n]]
1313 if {[llength $senders]} {
1314 ucmdr "No relevant incoming messages to delete." {}
1316 ucmdr "No incoming messages to delete." {}
1319 switch -exact [llength $senders] {
1320 0 { ucmdr {} {} "deletes your $ndel message(s)." }
1321 1 { ucmdr {} {} "deletes your $ndel message(s) from $senders." }
1323 ucmdr {} {} "deletes your $ndel message(s) from\
1324 [lreplace $senders end end] and [lindex $senders end]."
1330 global nick_case ownmailaddr ownfullname
1333 set target [ta_word]
1334 if {![string length $text]} { error "tell them what?" }
1335 if {[string length $text] > 400} { error "message too long" }
1337 set ltarget [irctolower $target]
1339 if {[info exists nick_case($ltarget)]} { set ctarget $nick_case($ltarget) }
1341 manyset [nickdb_get_sec_effective $target] sec mailtoint mailwhy
1342 manyset [nickdb_get $target tellrel] rel relint relwithin
1343 switch -exact $sec {
1345 set now [clock seconds]
1346 set inbound [msgsdb_get $ltarget inbound]
1347 lappend inbound $n $now $text
1348 msgsdb_set $ltarget inbound $inbound
1350 set outbound [msgsdb_get $n outbound]
1353 foreach {recip time count} $outbound {
1354 if {"[irctolower $recip]" == "$ltarget"} {
1359 lappend noutbound $recip $time $count
1362 lappend noutbound $ctarget $now 1
1364 msgsdb_set $n outbound $noutbound
1365 set msg "OK, I'll tell $ctarget"
1366 if {$found} { append msg " that too" }
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" }
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" }
1382 tell_event $ltarget msgsarrive
1386 set fmtmsg [exec fmt << " $text"]
1387 exec /usr/sbin/sendmail -odb -oi -t -oee -f $mailwhy \
1389 "From: $ownmailaddr ($ownfullname)
1391 Subject: IRC tell from $n
1393 $n asked me[expr {[ischan $dest] ? " on $dest" : ""}] to tell you:
1394 [exec fmt << " $text"]
1396 (This message was for your nick $ctarget; your account $mailwhy
1397 arranged for it to be forwarded to $mailtoint.)
1400 "I've mailed $ctarget, which is what they prefer." \
1404 usererror "Sorry, $ctarget does not want me to take messages."
1407 error "bad tellsec $sec"
1414 set target [ta_word]; ta_nomore
1419 set myself [expr {"$target" != "$n"}]
1421 set ltarget [irctolower $target]
1422 upvar #0 nick_case($ltarget) ctarget
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 }
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."
1434 set ol "The nick $nshow is registered (but not to a username)."
1436 if {![info exists ctarget] || ![info exists oc]} {
1438 append ol "\nI can't see $nshow on anywhere."
1440 append ol "\nYou aren't on any channels with me."
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."
1449 append ol "\n$nshow has identified themselves to me."
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]] {
1462 upvar #0 nick_username($luser) nu
1463 if {![info exists nu]} {
1465 "You must identify yourself before using `register'. See `help identify', or use `register insecure'."
1467 nickdb_set $n username $nu
1468 ucmdr {} {} "makes a note of your username." {}
1472 ucmdr {} {} "forgets your nickname." {}
1475 nickdb_set $n username {}
1477 ucmdr {} "Security is now disabled for your nickname !"
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."
1483 error "you mean register / register delete / register insecure"
1488 proc timeformat_desc {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" }
1497 proc def_setting {opt show_body set_body} {
1499 lappend settings $opt
1500 proc set_show/$opt {} "
1504 if {![string length $set_body]} return
1505 proc set_set/$opt {} "
1512 proc tellme_sec_desc {v n} {
1513 manyset $v sec mailtoint
1514 switch -exact $sec {
1516 return "I'll tell you your messages whenever I see you."
1519 if {[string length [nickdb_get_username $n]]} {
1521 "I'll keep the bodies of your messages private until you identify yourself, reminding you every [showintervalsecs $mailtoint 1]."
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'.)"
1530 return "I shan't accept messages for you."
1533 return "I'll forward your messages by email to $mailtoint."
1536 error "bad tellsec $sec"
1541 proc tellme_rel_desc {v} {
1542 manyset $v rel every within
1543 switch -exact $rel {
1545 return "As soon as I've told you message(s), I'll forget them\
1546 - note that this means messages can get lost !"
1552 set u ", or talk on channel within [showintervalsecs $within 1] of me having told you"
1555 error "bad tellrel $rel"
1558 return "After delivering messages, I'll remind you every\
1559 [showintervalsecs $every 1] until you say delmsg$u."
1562 def_setting timeformat {
1563 set tf [nickdb_get $n timeformat]
1564 return "$tf: [timeformat_desc $tf]"
1566 set tf [string tolower [ta_word]]
1568 set desc [timeformat_desc $tf]
1569 nickdb_set $n timeformat $tf
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."
1579 return "I'll send you a message every [showintervalsecs $mt 0]."
1583 def_setting marktime {
1584 set mt [nickdb_get $n marktime]
1586 if {[string match {[0-9]*} $mt]} { append p s }
1588 append p [marktime_desc $mt]
1592 set mt [string tolower [ta_word]]
1595 if {"$mt" == "off" || "$mt" == "once"} {
1597 set mt [parse_interval $mt $marktime_min]
1599 nickdb_set $n marktime $mt
1600 lnick_marktime_start [irctolower $n] "So:" 500 0
1601 ucmdr {} [marktime_desc $mt]
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."
1609 return "Your nick, $n, is not secure."
1613 proc tellme_setting_sec_simple {} {
1621 proc tellme_setting_neednomsgs {} {
1623 if {[llength [msgsdb_get $n inbound]]} {
1624 usererror "You must delete the messages you have, first."
1629 def_setting tellme {
1630 set secv [nickdb_get $n tellsec]
1631 set ms [tellme_sec_desc $secv $n]
1633 switch -exact $sec {
1635 set mr [tellme_rel_desc [nickdb_get $n tellrel]]
1643 set setting [string tolower [ta_word]]
1644 switch -exact $setting {
1646 tellme_setting_sec_simple
1649 set every [ta_interval_optional 60 600]
1652 set v [list secure $every]
1655 telling_setting_neednomsgs
1656 telling_setting_sec_simple
1659 telling_setting_neednomsgs
1661 if {[string length [set u [nickdb_get_username $n]]]} {
1663 "Sorry, you must register securely to have your messages mailed\
1664 (to prevent the use of this feature for spamming). See `help register'."
1667 set v [list mailto [ta_word] $u]
1669 unreliable - pester - remind {
1670 manyset [nickdb_get $n tellsec] sec
1671 switch -exact $sec {
1674 "You can't change your message delivery conditions when\
1675 your message disposition prevents messages from being left."
1680 if {"$setting" != "unreliable"} {
1681 set every [parse_interval_optional 300 3600]
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"
1694 error "invalid tellme setting $setting"
1697 nickdb_set $n tell$sr $v
1698 ucmdr [tellme_${sr}_desc $v] {}
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"
1707 set now [clock seconds]
1708 while {[llength $ml]} {
1710 set ml [lreplace $ml 0 2]
1711 set td [expr {$now-$t}]
1713 set iv [showinterval $td]
1715 set if1 "message to $r, $iv."
1717 set iv [showintervalsecs $td 0]
1718 set ifo "$r, $n messages, oldest $iv"
1719 set if1 "$n messages to $r, oldest $iv."
1722 sendprivmsg $luser "$m1 $if1"
1725 lappend ol " to $ifo[expr {[llength $ml] ? ";" : "."}]"
1728 sendprivmsg $luser "$m1 messages:"
1729 msendprivmsg $luser $ol
1736 if {![nickdb_exists $n]} {
1737 ucmdr {} "You are unknown to me and so have no settings. (Use `register'.)"
1739 if {![ta_anymore]} {
1741 foreach opt $settings {
1742 lappend ol [format "%-10s %s" $opt [set_show/$opt]]
1744 ucmdr {} [join $ol "\n"]
1747 if {[catch { info body set_show/$opt }]} {
1748 error "no setting $opt"
1750 if {![ta_anymore]} {
1751 ucmdr {} "$opt: [set_show/$opt]"
1753 nick_securitycheck 0
1754 if {[catch { info body set_set/$opt }]} {
1755 error "setting $opt cannot be set with `set'"
1762 def_ucmd identpass {
1765 set luser [irctolower $n]
1766 set username [ta_word]
1767 set passmd5 [md5sum "[ta_word]\n"]
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." {}
1773 check_username $username
1774 exec userv --timeout 3 $username << "$passmd5\n" > /dev/null \
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
1783 set target [ta_word]
1785 check_username $target
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} {
1794 "Please be patient; $target was summoned only [showinterval $interval]."
1797 regsub {^[^!]*!} $p {} path
1799 exec userv --timeout 3 $target irc-summon $n $path \
1800 [expr {[ischan $dest] ? "$dest" : ""}] \
1803 regsub -all "\n" $rv { / } rv
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}]
1811 ucmdr {} {} {} "invites $target ($tty[expr {
1812 $idletime > 10 ? ", idle for [showintervalsecs $idletime 0]" : ""
1814 [ischan $dest] ? "join us here" : "talk to you"
1817 error "unexpected response from userv service: $rv"
1821 proc md5sum {value} { exec md5sum << $value }
1824 global lastseen nick
1827 set nlower [irctolower $ncase]
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."
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."
1840 if {[ischan $dest]} {
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
1855 proc lnick_marktime_cancel {luser} {
1856 upvar #0 nick_markid($luser) mi
1857 if {![info exists mi]} return
1858 catch { after cancel $mi }
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]]
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
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] }
1880 lnick_marktime_doafter $luser $why $ms $mentiontold
1884 proc lnick_marktime_now {luser why mentiontold} {
1885 upvar #0 nick_onchans($luser) oc
1887 set calling_nick $luser
1888 sendprivmsg $luser [lnick_pingstring $why $oc ""]
1889 if {$mentiontold} { lnick_checktold $luser }
1890 lnick_marktime_reset $luser
1893 proc lnick_pingstring {why oc apstring} {
1895 catch { exec uptime } uptime
1896 set nnicks [llength [array names nick_onchans]]
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"
1903 append uptime ", $nnicks nicks"
1905 if {[llength $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"
1920 append str " " $uptime " " $activity
1921 if {[string length $apstring]} { append str " " $apstring }
1927 set ln [irctolower $n]
1928 if {[ischan $dest]} {
1929 set oc [irctolower $dest]
1932 if {[info exists nick_onchans($ln)]} {
1933 set oc $nick_onchans($ln)
1937 if {[llength $oc]} { lnick_marktime_reset $ln }
1940 ucmdr {} [lnick_pingstring "Pong!" $oc $text]
1943 proc ensure_globalsecret {} {
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
1956 foreach chan [chandb_list] {
1957 if {[chandb_get $chan autojoin]} { dojoin $chan }