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 set ndel [tell_delete_msgs {} $nl]
295 "I'm assuming you got the $ndel message(s) I just passed on."
299 # oops, messages passed are now out of date
300 catch_restoreei { unset telling }
304 # ignore new msgs if we haven't passed yet
308 pt???uu - pt???rs - pt???rl - pt???p? {
309 # ignore (any other `talk's) - act handles these
313 uaii?uu - uaii??l - uas?iuu - uas?i?l -
316 # pass and then stuff
317 if {[llength $iml] == 3} {
318 manyset $iml sender sentwhen msg
320 "$sender asked me, [showinterval [expr {$now-$sentwhen}]],\
324 "I have [expr {[llength $iml]/3}] messages for you:"
325 while {[llength $iml] >= 3} {
326 manyset [lrange $iml 0 2] sender sentwhen msg
327 set iml [lrange $iml 3 end]
329 " [showintervalsecs [expr {$now-$sentwhen}] 1] <$sender> $msg"
332 if {"$rel" == "unreliable"} {
333 tell_delete_msgs {} $nl
334 return toldunreliable
340 sendprivmsg $nl {You have messages (so identify yourself please).}
345 sendprivmsg $nl {You must identify yourself to see your messages.}
349 sendprivmsg $nl {Don't forget about your messages.}
355 mass??? - pa????v - pa????s -
363 error "tell_event_core nl=$nl evstate=$evstate ?"
366 if {![info exists u]} {
367 set telling [list {} undelivered $now]
369 set telling [list $u $stt $now]
374 proc recordlastseen_n {n how here} {
376 # 0 - nick was seen leaving (or changing to another nicks or some such)
377 # 1 - nick was seen doing something else
378 # 2 - nick was seen talking on channel
379 global lastseen lookedfor
380 set nl [irctolower $n]
381 set now [clock seconds]
382 set lastseen($nl) [list $n $now $how]
386 tell_event $nl [lindex {none act talk} $here]
388 upvar #0 lookedfor($nl) lf
389 if {[info exists lf]} {
390 switch -exact [llength $lf] {
395 manyset [lindex $lf 0] when who where
397 "FYI, $who was looking for you [looking_whenwhere $when $where]."]
400 msg_begin tosend $n "FYI, people have been looking for you:"
406 msg_append tosend " "
407 } elseif {$i == [llength $lf]} {
408 msg_append tosend " and "
411 msg_append tosend ", "
413 manyset $e when who where
415 "$who ([looking_whenwhere $when $where])$fin"
417 set ml [msg_finish tosend]
421 msendprivmsg_delayed 1000 $n $ml
425 proc note_topic {showoff whoby topic} {
426 set msg "FYI, $whoby has changed the topic on $showoff"
427 if {[string length $topic] < 160} {
428 append msg " to $topic"
430 append msg " but it is too long to reproduce here !"
432 set showoff [irctolower $showoff]
433 set tell [chandb_get $showoff topictell]
434 if {[lsearch -exact $tell *] >= 0} {
435 set tryspies [chandb_list]
439 foreach spy $tryspies {
440 set see [chandb_get $spy topicsee]
441 if {[lsearch -exact $see $showoff] >= 0 || \
442 ([lsearch -exact $see *] >= 0 && \
443 [lsearch -exact $tell $spy] >= 0)} {
444 sendprivmsg $spy $msg
449 proc recordlastseen_p {p how here} {
451 recordlastseen_n $n $how $here
454 proc chanmode_arg {} {
456 set rv [lindex $cm_args 0]
457 set cm_args [lreplace cm_args 0 0]
461 proc chanmode_o1 {m g p chan} {
462 global nick chan_initialop
464 set who [chanmode_arg]
465 recordlastseen_n $n "being nice to $who" 1
466 if {"[irctolower $who]" == "[irctolower $nick]"} {
467 set nlower [irctolower $n]
468 upvar #0 nick_unique($nlower) u
469 if {[chandb_exists $chan]} {
470 sendprivmsg $n Thanks.
471 } elseif {![info exists u]} {
472 sendprivmsg $n {Op me while not on the channel, why don't you ?}
474 set chan_initialop([irctolower $chan]) $u
476 "Thanks. You can use `channel manager ...' to register this channel."
477 if {![string length [nickdb_get_username $n username]]} {
479 "(But to do that you must register your nick securely first.)"
485 proc chanmode_o0 {m g p chan} {
488 set who [chanmode_arg]
489 recordlastseen_p $p "being mean to $who" 1
490 if {"[irctolower $who]" == "[irctolower $nick]"} {
491 set chandeop($chan) [list [clock seconds] $p]
495 proc msg_MODE {p c dest modelist args} {
496 if {![ischan $dest]} return
497 if {[regexp {^\-(.+)$} $modelist dummy modelist]} {
499 } elseif {[regexp {^\+(.+)$} $modelist dummy modelist]} {
502 error "invalid modelist"
504 foreach m [split $modelist] {
505 set procname chanmode_$m$give
506 if {[catch { info body $procname }]} {
507 recordlastseen_p $p "fiddling with $dest" 1
509 $procname $m $give $p $dest
514 proc leaving {lchan} {
515 foreach luser [array names nick_onchans] {
516 upvar #0 nick_onchans($luser) oc
517 set oc [grep tc {"$tc" != "$lchan"} $oc]
519 upvar #0 chan_nicks($lchan) nlist
521 upvar #0 chan_lastactivity($lchan) la
525 proc doleave {lchan} {
530 proc dojoin {lchan} {
533 set chan_nicks($lchan) {}
536 proc check_justme {lchan} {
538 upvar #0 chan_nicks($lchan) nlist
539 if {[llength $nlist] != 1} return
540 if {"[lindex $nlist 0]" != "[irctolower $nick]"} return
541 if {[chandb_exists $lchan]} {
542 set mode [chandb_get $lchan mode]
543 if {"$mode" != "*"} {
544 sendout MODE $lchan $mode
546 set topic [chandb_get $lchan topicset]
547 if {[string length $topic]} {
548 sendout TOPIC $lchan $topic
555 proc process_kickpart {chan user} {
558 set luser [irctolower $user]
559 set lchan [irctolower $chan]
560 if {![ischan $chan]} { error "not a channel" }
561 if {"$luser" == "[irctolower $nick]"} {
564 upvar #0 nick_onchans($luser) oc
565 upvar #0 chan_nicks($lchan) nlist
566 set oc [grep tc {"$tc" != "$lchan"} $oc]
567 set nlist [grep tn {"$tn" != "$luser"} $nlist]
569 if {![llength $oc]} {
577 proc msg_TOPIC {p c dest topic} {
579 if {![ischan $dest]} return
580 recordlastseen_n $n "changing the topic on $dest" 1
581 note_topic [irctolower $dest] $n $topic
584 proc msg_KICK {p c chans users comment} {
585 set chans [split $chans ,]
586 set users [split $users ,]
587 if {[llength $chans] > 1} {
588 foreach chan $chans user $users { process_kickpart $chan $user }
590 foreach user $users { process_kickpart [lindex $chans 0] $user }
594 proc msg_KILL {p c user why} {
599 set nick_arys {onchans username unique}
600 # nick_onchans($luser) -> [list ... $lchan ...]
601 # nick_username($luser) -> <securely known local username>
602 # nick_unique($luser) -> <includes-counter>
603 # nick_case($luser) -> $user (valid even if no longer visible)
604 # nick_markid($luser) -> <after id for marktime>
605 # nick_telling($luser) -> <unique> mentioned|passed <when>
607 # chan_nicks($lchan) -> [list ... $luser ...]
608 # chan_lastactivity($lchan) -> [clock seconds]
610 proc lnick_forget {luser} {
611 global nick_arys chan_nicks
612 lnick_marktime_cancel $luser
613 foreach ary $nick_arys {
614 upvar #0 nick_${ary}($luser) av
617 foreach lch [array names chan_nicks] {
618 upvar #0 chan_nicks($lch) nlist
619 set nlist [grep tn {"$tn" != "$luser"} $nlist]
624 proc nick_forget {user} {
625 global nick_arys chan_nicks
626 lnick_forget [irctolower $user]
630 proc nick_case {user} {
632 set nick_case([irctolower $user]) $user
635 proc msg_NICK {p c newnick} {
636 global nick_arys nick_case calling_nick
638 recordlastseen_n $n "changing nicks to $newnick" 0
639 set calling_nick $newnick
640 recordlastseen_n $newnick "changing nicks from $n" 1
641 set luser [irctolower $n]
642 lnick_marktime_cancel $luser
643 set lusernew [irctolower $newnick]
644 foreach ary $nick_arys {
645 upvar #0 nick_${ary}($luser) old
646 upvar #0 nick_${ary}($lusernew) new
647 if {[info exists new]} { error "nick collision ?! $ary $n $newnick" }
648 if {[info exists old]} { set new $old; unset old }
650 upvar #0 nick_onchans($lusernew) oc
652 upvar #0 chan_nicks($ch) nlist
653 set nlist [grep tn {"$tn" != "$luser"} $nlist]
654 lappend nlist $lusernew
656 lnick_marktime_start $lusernew "Hi." 500 1
660 proc nick_ishere {n} {
662 upvar #0 nick_unique([irctolower $n]) u
663 if {![info exists u]} { set u [incr nick_counter].$n.[clock seconds] }
667 proc msg_JOIN {p c chan} {
670 recordlastseen_n $n "joining $chan" 1
671 set nl [irctolower $n]
672 set lchan [irctolower $chan]
673 upvar #0 nick_onchans($nl) oc
674 upvar #0 chan_nicks($lchan) nlist
675 if {![info exists oc]} {
676 global marktime_join_startdelay
677 lnick_marktime_start $nl "Welcome." $marktime_join_startdelay 1
682 proc msg_PART {p c chan args} {
684 set msg "leaving $chan"
685 if {[llength $args]} {
686 set why [lindex $args 0]
687 if {"[irctolower $why]" != "[irctolower $n]"} { append msg " ($why)" }
689 recordlastseen_n $n $msg 1
690 process_kickpart $chan $n
692 proc msg_QUIT {p c why} {
694 recordlastseen_n $n "leaving ($why)" 0
698 proc msg_PRIVMSG {p c dest text} {
702 if {[ischan $dest]} {
703 recordlastseen_n $n "invoking me in $dest" 1
706 recordlastseen_n $n "talking to me" 1
711 execute_usercommand $p $c $n $output $dest $text
714 proc msg_INVITE {p c n chan} {
715 after 1000 [list dojoin [irctolower $chan]]
718 proc grep {var predicate list} {
722 if {[uplevel 1 [list expr $predicate]]} { lappend o $v }
727 proc msg_353 {p c dest type chan nicklist} {
728 global names_chans nick_onchans
729 set lchan [irctolower $chan]
730 upvar #0 chan_nicks($lchan) nlist
731 lappend names_chans $lchan
732 if {![info exists nlist]} {
733 # We don't think we're on this channel, so ignore it !
734 # Unfortunately, because we don't get a reply to PART,
735 # we have to remember ourselves whether we're on a channel,
736 # and ignore stuff if we're not, to avoid races. Feh.
740 foreach user [split $nicklist { }] {
741 regsub {^[@+]} $user {} user
742 if {![string length $user]} continue
744 set luser [irctolower $user]
745 upvar #0 nick_onchans($luser) oc
747 lappend nlist_new $luser
753 proc msg_366 {p c args} {
754 global names_chans nick_onchans
755 set lchan [irctolower $c]
756 foreach luser [array names nick_onchans] {
757 upvar #0 nick_onchans($luser) oc
758 if {[llength names_chans] > 1} {
759 set oc [grep tc {[lsearch -exact $tc $names_chans] >= 0} $oc]
761 if {![llength $oc]} { lnick_forget $n }
766 proc check_username {target} {
768 [string length $target] > 8 ||
769 [regexp {[^-0-9a-z]} $target] ||
770 ![regexp {^[a-z]} $target]
771 } { error "invalid username" }
774 proc somedb__head {} {
776 set idl [irctolower $id]
777 upvar #0 ${nickchan}db($idl) ndbe
778 binary scan $idl H* idh
779 set idfn $fprefix$idh
780 if {![info exists iddbe] && [file exists $idfn]} {
782 try_except_finally { set newval [read $f] } {} { close $f }
783 if {[llength $newval] % 2} { error "invalid length" }
789 proc def_somedb {name arglist body} {
790 foreach {nickchan fprefix} {
795 proc ${nickchan}db_$name $arglist \
796 "set nickchan $nickchan; set fprefix $fprefix; $body"
802 foreach path [glob -nocomplain -path $fprefix *] {
803 binary scan $path "A[string length $fprefix]A*" afprefix thinghex
804 if {"$afprefix" != "$fprefix"} { error "wrong prefix $path $afprefix" }
805 lappend list [binary format H* $thinghex]
810 proc def_somedb_id {name arglist body} {
811 def_somedb $name [concat id $arglist] "somedb__head; $body"
814 def_somedb_id exists {} {
815 return [info exists iddbe]
818 def_somedb_id delete {} {
819 catch { unset iddbe }
823 set default_settings_nick {
827 tellrel {remind 3600 30}
830 set default_settings_chan {
839 set default_settings_msgs {
843 # inbound -> [<nick> <time_t> <message>] ...
844 # outbound -> [<nick> <time_t(earliest)> <count>] ...
845 # neither are sorted particularly; only one entry per recipient in
846 # output; both sender and recipient are cased
848 def_somedb_id set {args} {
849 upvar #0 default_settings_$nickchan def
850 if {![info exists iddbe]} { set iddbe $def }
851 foreach {key value} [concat $iddbe $args] { set a($key) $value }
853 foreach {key value} [array get a] { lappend newval $key $value }
854 set f [open $idfn.new w]
858 file rename -force $idfn.new $idfn
866 def_somedb_id get {key} {
867 upvar #0 default_settings_$nickchan def
868 if {[info exists iddbe]} {
869 set l [concat $iddbe $def]
873 foreach {tkey value} $l {
874 if {"$tkey" == "$key"} { return $value }
876 error "unset setting $key"
881 if {[info exists calling_nick]} { set n $calling_nick } { set n {} }
882 return [nickdb_get $n $key]
885 proc check_notonchan {} {
887 if {[ischan $dest]} { usererror "That command must be sent privately." }
890 proc nick_securitycheck {strict} {
892 if {![nickdb_exists $n]} {
893 usererror "You are unknown to me, use `register'."
895 set wantu [nickdb_get $n username]
896 if {![string length $wantu]} {
898 usererror "That feature is only available to secure users, sorry."
903 set luser [irctolower $n]
904 upvar #0 nick_username($luser) nu
905 if {![info exists nu]} {
906 usererror "Nick $n is secure, you must identify yourself first."
908 if {"$wantu" != "$nu"} {
909 usererror "You are the wrong user -\
910 the nick $n belongs to $wantu, not $nu."
914 proc channel_ismanager {channel n} {
915 set mgrs [chandb_get $channel managers]
916 return [expr {[lsearch -exact [irctolower $mgrs] [irctolower $n]] >= 0}]
919 proc channel_securitycheck {channel} {
921 if {![channel_ismanager $channel $n]} {
922 usererror "You are not a manager of $channel."
927 proc def_chancmd {name body} {
928 proc channel/$name {} \
929 " upvar 1 target chan; upvar 1 n n; upvar 1 text text; $body"
932 proc ta_listop {findnow procvalue} {
933 # findnow and procvalue are code fragments which will be executed
934 # in the caller's level. findnow should set ta_listop_ev to
935 # the current list, and procvalue should treat ta_listop_ev as
936 # a proposed value in the list and check and possibly modify
937 # (canonicalise?) it. After ta_listop, ta_listop_ev will
938 # be the new value of the list.
939 upvar 1 ta_listop_ev exchg
942 switch -exact _$opcode {
946 foreach item $exchg { set array($item) 1 }
949 error "list change opcode must be one of + - ="
952 foreach exchg [split $text " "] {
953 if {![string length $exchg]} continue
955 if {"$opcode" != "-"} {
958 catch { unset array($exchg) }
961 set exchg [lsort [array names array]]
964 def_chancmd manager {
966 if {[chandb_exists $chan]} {
967 set ta_listop_ev [chandb_get $chan managers]
969 set ta_listop_ev [list [irctolower $n]]
972 check_nick $ta_listop_ev
973 set ta_listop_ev [irctolower $ta_listop_ev]
975 if {[llength $ta_listop_ev]} {
976 chandb_set $chan managers $ta_listop_ev
977 ucmdr "Managers of $chan: $ta_listop_ev" {}
980 ucmdr {} {} "forgets about managing $chan." {}
984 def_chancmd autojoin {
986 switch -exact [string tolower $yesno] {
989 default { error "channel autojoin must be `yes' or `no' }
991 chandb_set $chan autojoin $nv
992 ucmdr [expr {$nv ? "I will join $chan when I'm restarted " : \
993 "I won't join $chan when I'm restarted "}] {}
996 def_chancmd userinvite {
997 set nv [string tolower [ta_word]]
999 pub { set txt "!invite will work for $chan, but it won't work by /msg" }
1000 here { set txt "!invite and /msg invite will work, but only for users who are already on $chan." }
1001 all { set txt "Any user will be able to invite themselves or anyone else to $chan." }
1002 none { set txt "I will not invite anyone to $chan." }
1004 error "channel userinvite must be `pub', `here', `all' or `none'
1007 chandb_set $chan userinvite $nv
1013 switch -exact $what {
1016 chandb_set $chan topicset {}
1017 ucmdr "I won't ever change the topic of $chan." {}
1020 set t [string trim $text]
1021 if {![string length $t]} {
1022 error "you must specific the topic to set"
1024 chandb_set $chan topicset $t
1025 ucmdr "Whenever I'm alone on $chan, I'll set the topic to $t." {}
1029 set ta_listop_ev [chandb_get $chan topic$what]
1031 if {"$ta_listop_ev" != "*"} {
1032 if {![ischan $ta_listop_ev]} {
1033 error "bad channel \`$ta_listop_ev' in topic $what"
1035 set ta_listop_ev [irctolower $ta_listop_ev]
1038 chandb_set $chan topic$what $ta_listop_ev
1039 ucmdr "Topic $what list for $chan: $ta_listop_ev" {}
1042 usererror "Unknown channel topic subcommand - see help channel."
1049 if {"$mode" != "*" && ![regexp {^(([-+][imnpst]+)+)$} $mode mode]} {
1050 error {channel mode must be * or match ([-+][imnpst]+)+}
1052 chandb_set $chan mode $mode
1053 if {"$mode" == "*"} {
1054 ucmdr "I won't ever change the mode of $chan." {}
1056 ucmdr "Whenever I'm alone on $chan, I'll set the mode to $mode." {}
1061 if {[chandb_exists $chan]} {
1062 set l "Settings for $chan: autojoin "
1063 append l [lindex {no yes} [chandb_get $chan autojoin]]
1064 append l ", mode " [chandb_get $chan mode]
1065 append l ", userinvite " [chandb_get $chan userinvite] "."
1066 append l "\nManagers: "
1067 append l [join [chandb_get $chan managers] " "]
1068 foreach {ts sep} {see "\n" tell " "} {
1069 set t [chandb_get $chan topic$ts]
1072 append l "Topic $ts list: $t."
1074 append l "Topic $ts list is empty."
1078 set t [chandb_get $chan topicset]
1079 if {[string length $t]} {
1080 append l "Topic to set: $t"
1082 append l "I will not change the topic."
1086 ucmdr {} "The channel $chan is not managed."
1090 proc channelmgr_monoop {} {
1095 upvar 1 target target
1100 if {[ischan $dest]} { set target $dest }
1101 if {[ta_anymore]} { set target [ta_word] }
1103 if {![info exists target]} {
1104 usererror "You must specify, or invoke me on, the relevant channel."
1106 if {![info exists chan_nicks([irctolower $target])]} {
1107 usererror "I am not on $target."
1109 if {![ischan $target]} { error "not a valid channel" }
1111 if {![chandb_exists $target]} {
1112 usererror "$target is not a managed channel."
1114 channel_securitycheck $target
1119 sendout MODE $target +o $n
1128 global chan_nicks errorCode errorInfo
1131 if {[ischan $dest]} {
1135 set target [ta_word]
1138 set ltarget [irctolower $target]
1139 if {![ischan $target]} { error "$target is not a channel" }
1140 if {![info exists chan_nicks($ltarget)]} {
1141 usererror "I am not on $target."
1143 set ui [chandb_get $ltarget userinvite]
1145 if {"$ui" == "pub" && !$onchan} {
1146 usererror "Invitations to $target must be made there with !invite."
1148 if {"$ui" != "all"} {
1149 if {[lsearch -exact $chan_nicks($ltarget) [irctolower $n]] < 0} {
1150 usererror "Invitations to $target may only be made\
1151 by a user on the channel."
1154 if {"$ui" == "none"} {
1155 usererror "Sorry, I've not been authorised\
1156 to invite people to $target."
1159 if {"$errorCode" == "BLIGHT USER" && [channel_ismanager $target $n]} {
1161 nick_securitycheck 1
1163 if {"$errorCode" == "BLIGHT USER"} {
1164 usererror "$emsg2 Therefore you can't use your\
1165 channel manager privilege. $emsg"
1167 error $error $errorInfo $errorCode
1171 error $emsg $errorInfo $errorCode
1174 if {![ta_anymore]} {
1175 usererror "You have to say who to invite."
1178 while {[ta_anymore]} {
1179 set invitee [ta_nick]
1180 lappend invitees $invitee
1182 foreach invitee $invitees {
1183 sendout INVITE $invitee $ltarget
1185 set who [lindex $invitees 0]
1186 switch -exact llength $invitees {
1187 0 { error "zero invitees" }
1189 2 { append who " and [lindex $invitees 1]" }
1191 set who [join [lreplace $invitees end end] ", "]
1192 append who " and [lindex $invitees [llength $invitees]]"
1195 ucmdr {} {} {} "invites $who to $target."
1199 if {[ischan $dest]} { set target $dest }
1200 if {![ta_anymore]} {
1203 set subcmd [ta_word]
1205 if {[ischan $subcmd]} {
1207 if {![ta_anymore]} {
1210 set subcmd [ta_word]
1213 if {![info exists target]} { error "privately, you must specify a channel" }
1214 set procname channel/$subcmd
1215 if {"$subcmd" != "show"} {
1216 if {[catch { info body $procname }]} {
1217 usererror "unknown channel setting $subcmd."
1220 if {[chandb_exists $target]} {
1221 channel_securitycheck $target
1223 nick_securitycheck 1
1224 upvar #0 chan_initialop([irctolower $target]) io
1225 upvar #0 nick_unique([irctolower $n]) u
1226 if {![info exists io]} {
1227 usererror "$target is not a managed channel."
1229 if {"$io" != "$u"} {
1230 usererror "You are not the interim manager of $target."
1232 if {"$subcmd" != "manager"} {
1233 usererror "Please use `channel manager' first."
1240 proc nickdb_get_username {n} {
1241 if {![nickdb_exists $n]} { return "" }
1242 return [nickdb_get $n username]
1245 proc nickdb_get_sec_effective {n} {
1246 set l [nickdb_get $n tellsec]
1247 set u [nickdb_get_username $n]
1248 if {"[lindex $l 0]" == "secure" && ![string length $u]} { set l insecure }
1252 proc tell_peernicks {text} {
1253 global errorInfo errorCode
1254 set text [irctolower [string trim $text]]
1255 set senders [split $text " "]
1256 foreach sender $senders {
1257 if {[catch { check_nick $sender } emsg]} {
1258 error "invalid nick `$sender': $emsg" $errorInfo $errorCode
1264 proc msgsdb_set_maydelete {n key l otherkey} {
1265 msgsdb_set $n $key $l
1266 if {[llength $l]} return
1267 if {[llength [msgsdb_get $n $otherkey]]} return
1271 proc tell_delete_msgs {lsenders lrecip} {
1274 foreach {s t m} [msgsdb_get $lrecip inbound] {
1275 if {[llength $lsenders]} {
1276 if {[lsearch -exact $lsenders [irctolower $s]] == -1} {
1277 lappend ninbound $s $t $m
1284 msgsdb_set_maydelete $lrecip inbound $ninbound outbound
1285 if {![llength $ninbound]} {
1286 upvar #0 nick_telling($lrecip) telling
1287 catch { unset telling }
1289 foreach s [array names rsenders] {
1291 foreach {r t c} [msgsdb_get $s outbound] {
1292 if {"[irctolower $r]" == "$lrecip"} continue
1293 lappend noutbound $r $t $c
1295 msgsdb_set_maydelete $s outbound $noutbound inbound
1303 nick_securitycheck 0
1304 set recipients [tell_peernicks $text]
1305 if {![llength $recipients]} {
1306 usererror "You must say which recipients' messages from you to forget."
1309 foreach recip $recipients {
1310 incr ndel [tell_delete_msgs [irctolower $n] $recip]
1312 ucmdr "Removed $ndel as yet undelivered message(s)." {}
1316 global errorInfo errorCode
1318 set nl [irctolower $n]
1320 manyset [nickdb_get_sec_effective $n] sec secwhen
1321 switch -exact $sec {
1325 "There are no messages to delete\
1326 because your message disposition prevents them from being left."
1329 nick_securitycheck 1
1332 error "delmsg sec $sec"
1335 if {![llength [msgsdb_get $n inbound]]} {
1336 ucmdr "No incoming messages to delete." {}
1339 if {![info exists u]} {
1341 "I can't delete your messages unless I can see you on a channel with me.\
1342 Otherwise I might delete a message I hadn't told you about yet."
1344 if {"$stt" != "passed"} {
1345 set telling [list $u undelivered 0]
1347 "There are message(s) you may not yet have seen;\
1348 I'll deliver them to you now.\
1349 If you actually want to delete them, just tell me `delmsg' again."
1351 set senders [tell_peernicks $text]
1352 set ndel [tell_delete_msgs [irctolower $senders] [irctolower $n]]
1354 if {[llength $senders]} {
1355 ucmdr "No relevant incoming messages to delete." {}
1358 switch -exact [llength $senders] {
1359 0 { ucmdr {} {} "deletes your $ndel message(s)." }
1360 1 { ucmdr {} {} "deletes your $ndel message(s) from $senders." }
1362 ucmdr {} {} "deletes your $ndel message(s) from\
1363 [lreplace $senders end end] and [lindex $senders end]."
1372 switch -exact [tell_event [irctolower $n] tellme] {
1373 ERROR - INVALID { ucmdr {} {is ill. Help!} }
1374 nomsgs { ucmdr {You have no messages.} {} }
1380 global nick_case ownmailaddr ownfullname
1383 set target [ta_nick]
1384 if {![string length $text]} { error "tell them what?" }
1385 if {[string length $text] > 400} { error "message too long" }
1387 set ltarget [irctolower $target]
1389 if {[info exists nick_case($ltarget)]} { set ctarget $nick_case($ltarget) }
1391 manyset [nickdb_get_sec_effective $target] sec mailtoint mailwhy
1392 manyset [nickdb_get $target tellrel] rel relint relwithin
1393 switch -exact $sec {
1395 set now [clock seconds]
1396 set inbound [msgsdb_get $ltarget inbound]
1397 lappend inbound $n $now $text
1398 msgsdb_set $ltarget inbound $inbound
1400 set outbound [msgsdb_get $n outbound]
1403 foreach {recip time count} $outbound {
1404 if {"[irctolower $recip]" == "$ltarget"} {
1409 lappend noutbound $recip $time $count
1412 lappend noutbound $ctarget $now 1
1414 msgsdb_set $n outbound $noutbound
1415 set msg "OK, I'll tell $ctarget"
1416 if {$found} { append msg " that too" }
1418 if {"$sec" != "secure"} {
1419 switch -exact $rel {
1420 unreliable { append msg "neither reliably nor securely" }
1421 remind { append msg "pretty reliably, but not securely" }
1422 pester { append msg "reliably but not securely" }
1425 switch -exact $rel {
1426 unreliable { append msg "securely but not reliably" }
1427 remind { append msg "securely and pretty reliably" }
1428 pester { append msg "reliably and securely" }
1432 tell_event $ltarget msgsarrive
1436 set fmtmsg [exec fmt << " $text"]
1437 exec /usr/sbin/sendmail -odb -oi -t -oee -f $mailwhy \
1439 "From: $ownmailaddr ($ownfullname)
1441 Subject: IRC tell from $n
1443 $n asked me[expr {[ischan $dest] ? " on $dest" : ""}] to tell you:
1444 [exec fmt << " $text"]
1446 (This message was for your nick $ctarget; your account $mailwhy
1447 arranged for it to be forwarded to $mailtoint.)
1450 "I've mailed $ctarget, which is what they prefer." \
1454 usererror "Sorry, $ctarget does not want me to take messages."
1457 error "bad tellsec $sec"
1464 set target [ta_nick]; ta_nomore
1469 set myself [expr {"$target" != "$n"}]
1471 set ltarget [irctolower $target]
1472 upvar #0 nick_case($ltarget) ctarget
1474 if {[info exists ctarget]} {
1475 upvar #0 nick_onchans($ltarget) oc
1476 upvar #0 nick_username($ltarget) nu
1477 if {[info exists oc]} { set nshow $ctarget }
1479 if {![nickdb_exists $ltarget]} {
1480 set ol "$nshow is not a registered nick."
1481 } elseif {[string length [set username [nickdb_get $target username]]]} {
1482 set ol "The nick $nshow belongs to the user $username."
1484 set ol "The nick $nshow is registered (but not to a username)."
1486 if {![info exists ctarget] || ![info exists oc]} {
1488 append ol "\nI can't see $nshow on anywhere."
1490 append ol "\nYou aren't on any channels with me."
1492 } elseif {![info exists nu]} {
1493 append ol "\n$nshow has not identified themselves."
1494 } elseif {![info exists username]} {
1495 append ol "\n$nshow has identified themselves as the user $nu."
1496 } elseif {"$nu" != "$username"} {
1497 append ol "\nHowever, $nshow is being used by the user $nu."
1499 append ol "\n$nshow has identified themselves to me."
1507 set old [nickdb_exists $n]
1508 if {$old} { nick_securitycheck 0 }
1509 set luser [irctolower $n]
1510 switch -exact [string tolower [string trim $text]] {
1512 upvar #0 nick_username($luser) nu
1513 if {![info exists nu]} {
1515 "You must identify yourself before using `register'. See `help identify', or use `register insecure'."
1517 nickdb_set $n username $nu
1518 ucmdr {} {} "makes a note of your username." {}
1522 ucmdr {} {} "forgets your nickname." {}
1525 nickdb_set $n username {}
1527 ucmdr {} "Security is now disabled for your nickname !"
1529 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."
1533 error "you mean register / register delete / register insecure"
1538 proc timeformat_desc {tf} {
1540 ks { return "Times will be displayed in seconds or kiloseconds." }
1541 hms { return "Times will be displayed in hours, minutes, etc." }
1542 default { error "invalid timeformat: $v" }
1547 proc def_setting {opt show_body set_body} {
1549 lappend settings $opt
1550 proc set_show/$opt {} "
1554 if {![string length $set_body]} return
1555 proc set_set/$opt {} "
1562 proc tellme_sec_desc {v n} {
1563 manyset $v sec mailtoint
1564 switch -exact $sec {
1566 return "I'll tell you your messages whenever I see you."
1569 if {[string length [nickdb_get_username $n]]} {
1571 "I'll keep the bodies of your messages private until you identify yourself, reminding you every [showintervalsecs $mailtoint 1]."
1574 "I'll tell you your messages whenever I see you.\
1575 (Secure message delivery is enabled, but your nick is not registered\
1576 securely. See `help register'.)"
1580 return "I shan't accept messages for you."
1583 return "I'll forward your messages by email to $mailtoint."
1586 error "bad tellsec $sec"
1591 proc tellme_rel_desc {v n} {
1592 manyset $v rel every within
1593 switch -exact $rel {
1595 return "As soon as I've told you message(s), I'll forget them\
1596 - note that this means messages can get lost !"
1602 set u ", or talk on channel within [showintervalsecs $within 1] of me having told you"
1605 error "bad tellrel $rel"
1608 return "After delivering messages, I'll remind you every\
1609 [showintervalsecs $every 1] until you say delmsg$u."
1612 def_setting timeformat {
1613 set tf [nickdb_get $n timeformat]
1614 return "$tf: [timeformat_desc $tf]"
1616 set tf [string tolower [ta_word]]
1618 set desc [timeformat_desc $tf]
1619 nickdb_set $n timeformat $tf
1623 proc marktime_desc {mt} {
1624 if {"$mt" == "off"} {
1625 return "I will not send you periodic messages."
1626 } elseif {"$mt" == "once"} {
1627 return "I will send you one informational message when I see you."
1629 return "I'll send you a message every [showintervalsecs $mt 0]."
1633 def_setting marktime {
1634 set mt [nickdb_get $n marktime]
1636 if {[string match {[0-9]*} $mt]} { append p s }
1638 append p [marktime_desc $mt]
1642 set mt [string tolower [ta_word]]
1645 if {"$mt" == "off" || "$mt" == "once"} {
1647 set mt [parse_interval $mt $marktime_min]
1649 nickdb_set $n marktime $mt
1650 lnick_marktime_start [irctolower $n] "So:" 500 0
1651 ucmdr {} [marktime_desc $mt]
1654 def_setting security {
1655 set s [nickdb_get $n username]
1656 if {[string length $s]} {
1657 return "Your nick, $n, is controlled by the user $s."
1659 return "Your nick, $n, is not secure."
1663 proc tellme_setting_sec_simple {} {
1671 proc tellme_setting_neednomsgs {} {
1673 if {[llength [msgsdb_get $n inbound]]} {
1674 usererror "You must delete the incoming messages you have, first."
1679 def_setting tellme {
1680 set secv [nickdb_get $n tellsec]
1681 set ms [tellme_sec_desc $secv $n]
1683 switch -exact $sec {
1685 set mr [tellme_rel_desc [nickdb_get $n tellrel] $n]
1693 set setting [string tolower [ta_word]]
1694 switch -exact $setting {
1696 tellme_setting_sec_simple
1699 set every [ta_interval_optional 60 600]
1702 set v [list secure $every]
1705 tellme_setting_neednomsgs
1706 tellme_setting_sec_simple
1709 tellme_setting_neednomsgs
1711 if {![string length [set u [nickdb_get_username $n]]]} {
1713 "Sorry, you must register securely to have your messages mailed\
1714 (to prevent the use of this feature for spamming). See `help register'."
1717 set v [list mailto [ta_word] $u]
1719 unreliable - pester - remind {
1720 manyset [nickdb_get $n tellsec] sec
1721 switch -exact $sec {
1724 "You can't change your message delivery conditions when\
1725 your message disposition prevents messages from being left."
1730 if {"$setting" != "unreliable"} {
1731 set every [ta_interval_optional 300 3600]
1734 if {"$setting" == "remind"} {
1735 set within [ta_interval_optional 5 30]
1736 if {$within > $every} {
1737 error "remind interval must be at least time to respond"
1744 error "invalid tellme setting $setting"
1747 nickdb_set $n tell$sr $v
1748 upvar #0 nick_telling([irctolower $n]) telling
1749 catch { unset telling }
1750 ucmdr [tellme_${sr}_desc $v $n] {}
1753 proc lnick_checktold {luser} {
1754 set ml [msgsdb_get $luser outbound]
1755 if {![llength $ml]} return
1756 set is1 [expr {[llength $ml]==3}]
1757 set m1 "FYI, I haven't yet delivered your"
1759 set now [clock seconds]
1760 while {[llength $ml]} {
1762 set ml [lreplace $ml 0 2]
1763 set td [expr {$now-$t}]
1765 set iv [showinterval $td]
1767 set if1 "message to $r, $iv."
1769 set iv [showintervalsecs $td 0]
1770 set ifo "$r, $n messages, oldest $iv"
1771 set if1 "$n messages to $r, oldest $iv."
1774 sendprivmsg $luser "$m1 $if1"
1777 lappend ol " to $ifo[expr {[llength $ml] ? ";" : "."}]"
1780 sendprivmsg $luser "$m1 messages:"
1781 msendprivmsg $luser $ol
1788 if {![nickdb_exists $n]} {
1789 ucmdr {} "You are unknown to me and so have no settings. (Use `register'.)"
1791 if {![ta_anymore]} {
1793 foreach opt $settings {
1794 lappend ol [format "%-10s %s" $opt [set_show/$opt]]
1796 ucmdr {} [join $ol "\n"]
1799 if {[catch { info body set_show/$opt }]} {
1800 error "no setting $opt"
1802 if {![ta_anymore]} {
1803 ucmdr {} "$opt: [set_show/$opt]"
1805 nick_securitycheck 0
1806 if {[catch { info body set_set/$opt }]} {
1807 error "setting $opt cannot be set with `set'"
1814 def_ucmd identpass {
1817 set luser [irctolower $n]
1818 set username [ta_word]
1819 set passmd5 [md5sum "[ta_word]\n"]
1821 upvar #0 nick_onchans($luser) onchans
1822 if {![info exists onchans] || ![llength $onchans]} {
1823 ucmdr "You must be on a channel with me to identify yourself." {}
1825 check_username $username
1826 exec userv --timeout 3 $username << "$passmd5\n" > /dev/null \
1828 upvar #0 nick_username($luser) rec_username
1829 set rec_username $username
1830 after 50 [list tell_event $luser ident]
1831 ucmdr "Pleased to see you, $username." {}
1835 set target [ta_word]
1837 check_username $target
1840 upvar #0 lastsummon($target) ls
1841 set now [clock seconds]
1842 if {[info exists ls]} {
1843 set interval [expr {$now - $ls}]
1844 if {$interval < 30} {
1846 "Please be patient; $target was summoned only [showinterval $interval]."
1849 regsub {^[^!]*!} $p {} path
1851 exec userv --timeout 3 $target irc-summon $n $path \
1852 [expr {[ischan $dest] ? "$dest" : ""}] \
1855 regsub -all "\n" $rv { / } rv
1858 if {[regexp {^problem (.*)} $rv dummy problem]} {
1859 ucmdr {} "The user `$target' $problem."
1860 } elseif {[regexp {^ok ([^ ]+) ([0-9]+)$} $rv dummy tty idlesince]} {
1861 set idletime [expr {$now - $idlesince}]
1863 ucmdr {} {} {} "invites $target ($tty[expr {
1864 $idletime > 10 ? ", idle for [showintervalsecs $idletime 0]" : ""
1866 [ischan $dest] ? "join us here" : "talk to you"
1869 error "unexpected response from userv service: $rv"
1873 proc md5sum {value} { exec md5sum << $value }
1876 global lastseen nick
1879 set nlower [irctolower $ncase]
1881 set now [clock seconds]
1882 if {"$nlower" == "[irctolower $nick]"} {
1883 usererror "I am not self-aware."
1884 } elseif {![info exists lastseen($nlower)]} {
1885 set rstr "I've never seen $ncase."
1887 manyset $lastseen($nlower) realnick time what
1888 set howlong [expr {$now - $time}]
1889 set string [showinterval $howlong]
1890 set rstr "I last saw $realnick $string, $what."
1892 if {[ischan $dest]} {
1897 upvar #0 lookedfor($nlower) lf
1898 if {[info exists lf]} { set oldvalue $lf } else { set oldvalue {} }
1899 set lf [list [list $now $n $where]]
1900 foreach v $oldvalue {
1901 if {"[irctolower [lindex $v 1]]" == "[irctolower $n]"} continue
1907 proc lnick_marktime_cancel {luser} {
1908 upvar #0 nick_markid($luser) mi
1909 if {![info exists mi]} return
1910 catch { after cancel $mi }
1914 proc lnick_marktime_doafter {luser why ms mentiontold} {
1915 lnick_marktime_cancel $luser
1916 upvar #0 nick_markid($luser) mi
1917 set mi [after $ms [list lnick_marktime_now $luser $why 0]]
1920 proc lnick_marktime_reset {luser} {
1921 set mt [nickdb_get $luser marktime]
1922 if {"$mt" == "off" || "$mt" == "once"} return
1923 lnick_marktime_doafter $luser "Time passes." [expr {$mt*1000}] 0
1926 proc lnick_marktime_start {luser why ms mentiontold} {
1927 set mt [nickdb_get $luser marktime]
1928 if {"$mt" == "off"} {
1929 lnick_marktime_cancel $luser
1930 if {$mentiontold} { after $ms [list lnick_checktold $luser] }
1932 lnick_marktime_doafter $luser $why $ms $mentiontold
1936 proc lnick_marktime_now {luser why mentiontold} {
1937 upvar #0 nick_onchans($luser) oc
1939 set calling_nick $luser
1940 sendprivmsg $luser [lnick_pingstring $why $oc ""]
1941 if {$mentiontold} { after 150 [list lnick_checktold $luser] }
1942 lnick_marktime_reset $luser
1945 proc lnick_pingstring {why oc apstring} {
1947 catch { exec uptime } uptime
1948 set nnicks [llength [array names nick_onchans]]
1950 {^ *([0-9:apm]+) +up.*, +(\d+) users?, +load average: +([0-9., ]+) *$} \
1951 $uptime dummy time users load]} {
1952 regsub -all , $load {} load
1953 set uptime "$time $nnicks/$users $load"
1955 append uptime ", $nnicks nicks"
1957 if {[llength $oc]} {
1961 upvar #0 chan_lastactivity($ch) la
1962 if {![info exists la]} continue
1963 if {$la <= $best_la} continue
1964 set since [showintervalsecs [expr {[clock seconds]-$la}] 1]
1965 set activity "$ch $since"
1972 append str " " $uptime " " $activity
1973 if {[string length $apstring]} { append str " " $apstring }
1979 set ln [irctolower $n]
1980 if {[ischan $dest]} {
1981 set oc [irctolower $dest]
1984 if {[info exists nick_onchans($ln)]} {
1985 set oc $nick_onchans($ln)
1989 if {[llength $oc]} { lnick_marktime_reset $ln }
1991 after 150 [list lnick_checktold $ln]
1992 ucmdr {} [lnick_pingstring "Pong!" $oc $text]
1995 proc ensure_globalsecret {} {
1998 if {[info exists globalsecret]} return
1999 set gsfile [open /dev/urandom r]
2000 fconfigure $gsfile -translation binary
2001 set globalsecret [read $gsfile 32]
2002 binary scan $globalsecret H* globalsecret
2008 foreach chan [chandb_list] {
2009 if {[chandb_get $chan autojoin]} { dojoin $chan }