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 check_telling_core {nl event} {
149 # event is `talk', `act' or `come'
150 FIXME - delay 750ms when you invoke this, except for `talk'
151 which causes `talk' now and `act' later
152 FIXME - make it be called with come
154 set iml [msgdb_get $nl inbound]
155 if {![llength $iml]} return
157 upvar #0 nick_telling($nl) telling
158 upvar #0 nick_unique($nl) u
160 if {[info exists telling]} {
161 manyset $telling u2 stt telling_when
162 if {"$u2" != "$u"} { unset telling; unset stt; unset telling_when }
165 if {![info exists stt]} {
167 set telling_when $now
170 set ago [expr {$now - $telling_when}]
172 # evstate is string of letters
177 # security level and timing
179 # ss Secure and soon (before interval)
180 # sl Secure and late (after interval)
185 # reliability and timing
187 # rv Remind, very soon (before within-interval)
188 # rs Remind, soon (between)
189 # rl Remind, late (aftr every-interval)
190 # ps Pester, soon (before interval)
191 # pl Pester, late (after interval)
192 # current identification
197 # h Hidden (invisible, no unique)
199 manyset [nickdb_get $n tellsec] sec secwhen
201 insecure { set evstate ii }
202 secure { set evstate [expr {$ago<$secwhen ? "sl" : "ss"}] }
203 default { set evstate "#$sec#" }
206 append evstate [string range $stt 0 0]
208 manyset [nickdb_set $n tellrel] rel relint relwithin
210 unreliable { append evstate uu }
211 remind { append evstate [expr {
212 $ago<$relwithin ? "rv" : $ago<$relint ? "rs" : "rl"
214 pester { append evstate [expr {$ago<$relint ? "ps" : "pl"}] }
215 default { append evstate "#$rel#" }
218 upvar #0 nick_username($nl) nu
219 if {[info exists nu] && "$nu" == "[nickdb_get $nl username]"} {
225 append evstate [expr {[info exists u] ? "v" : "h"}]
227 switch -glob $evstate {
229 # consider delivered:
230 # (very recently passed, and the user talks)
235 # (any other `talk's)
237 ?iin???? - ?iip?l?? - ?ii????? -
238 ?s?n??iv - ?s?m??iv - ?s?p?liv {
240 # (insecure and not passed recently, or just arriving;
241 # secure and not passed recently)
244 ?ssp???? - ???p?s?? - ???p?v?? {
246 # (recently mentioned or passed
247 # immediate `talk' thing)
249 ?s?n???? - ?slm???? - cs?????? {
251 # (secure and not mentioned recently or just arriving,
252 # and should not pass)
254 {You have messages (so identify yourself please).}]
257 error "check_telling_core nl=$nl evstate=$evstate ?"
278 if {![info exists u]} { catch { unset telling } }
279 && [info exists telling]} {
284 if {![info exists telling]} {
287 proc recordlastseen_n {n how here} {
288 global lastseen lookedfor
289 set nl [irctolower $n]
290 set now [clock seconds]
291 set lastseen($nl) [list $n $now $how]
295 check_telling $nl [lindex {x act talk} $here]
297 upvar #0 lookedfor($nl) lf
298 if {[info exists lf]} {
299 switch -exact [llength $lf] {
304 manyset [lindex $lf 0] when who where
306 "FYI, $who was looking for you [looking_whenwhere $when $where]."]
309 msg_begin tosend $n "FYI, people have been looking for you:"
315 msg_append tosend " "
316 } elseif {$i == [llength $lf]} {
317 msg_append tosend " and "
320 msg_append tosend ", "
322 manyset $e when who where
324 "$who ([looking_whenwhere $when $where])$fin"
326 set ml [msg_finish tosend]
330 msendprivmsg_delayed 1000 $n $ml
334 proc note_topic {showoff whoby topic} {
335 set msg "FYI, $whoby has changed the topic on $showoff"
336 if {[string length $topic] < 160} {
337 append msg " to $topic"
339 append msg " but it is too long to reproduce here !"
341 set showoff [irctolower $showoff]
342 set tell [chandb_get $showoff topictell]
343 if {[lsearch -exact $tell *] >= 0} {
344 set tryspies [chandb_list]
348 foreach spy $tryspies {
349 set see [chandb_get $spy topicsee]
350 if {[lsearch -exact $see $showoff] >= 0 || \
351 ([lsearch -exact $see *] >= 0 && \
352 [lsearch -exact $tell $spy] >= 0)} {
353 sendprivmsg $spy $msg
358 proc recordlastseen_p {p how here} {
360 recordlastseen_n $n $how $here
363 proc chanmode_arg {} {
365 set rv [lindex $cm_args 0]
366 set cm_args [lreplace cm_args 0 0]
370 proc chanmode_o1 {m g p chan} {
371 global nick chan_initialop
373 set who [chanmode_arg]
374 recordlastseen_n $n "being nice to $who" 1
375 if {"[irctolower $who]" == "[irctolower $nick]"} {
376 set nlower [irctolower $n]
377 upvar #0 nick_unique($nlower) u
378 if {[chandb_exists $chan]} {
379 sendprivmsg $n Thanks.
380 } elseif {![info exists u]} {
381 sendprivmsg $n {Op me while not on the channel, why don't you ?}
383 set chan_initialop([irctolower $chan]) $u
385 "Thanks. You can use `channel manager ...' to register this channel."
386 if {![nickdb_exists $n] || ![string length [nickdb_get $n username]]} {
388 "(But to do that you must register your nick securely first.)"
394 proc chanmode_o0 {m g p chan} {
397 set who [chanmode_arg]
398 recordlastseen_p $p "being mean to $who" 1
399 if {"[irctolower $who]" == "[irctolower $nick]"} {
400 set chandeop($chan) [list [clock seconds] $p]
404 proc msg_MODE {p c dest modelist args} {
405 if {![ischan $dest]} return
406 if {[regexp {^\-(.+)$} $modelist dummy modelist]} {
408 } elseif {[regexp {^\+(.+)$} $modelist dummy modelist]} {
411 error "invalid modelist"
413 foreach m [split $modelist] {
414 set procname chanmode_$m$give
415 if {[catch { info body $procname }]} {
416 recordlastseen_p $p "fiddling with $dest" 1
418 $procname $m $give $p $dest
423 proc leaving {lchan} {
424 foreach luser [array names nick_onchans] {
425 upvar #0 nick_onchans($luser) oc
426 set oc [grep tc {"$tc" != "$lchan"} $oc]
428 upvar #0 chan_nicks($lchan) nlist
430 upvar #0 chan_lastactivity($lchan) la
434 proc doleave {lchan} {
439 proc dojoin {lchan} {
442 set chan_nicks($lchan) {}
445 proc check_justme {lchan} {
447 upvar #0 chan_nicks($lchan) nlist
448 if {[llength $nlist] != 1} return
449 if {"[lindex $nlist 0]" != "[irctolower $nick]"} return
450 if {[chandb_exists $lchan]} {
451 set mode [chandb_get $lchan mode]
452 if {"$mode" != "*"} {
453 sendout MODE $lchan $mode
455 set topic [chandb_get $lchan topicset]
456 if {[string length $topic]} {
457 sendout TOPIC $lchan $topic
464 proc process_kickpart {chan user} {
467 set luser [irctolower $user]
468 set lchan [irctolower $chan]
469 if {![ischan $chan]} { error "not a channel" }
470 if {"$luser" == "[irctolower $nick]"} {
473 upvar #0 nick_onchans($luser) oc
474 upvar #0 chan_nicks($lchan) nlist
475 set oc [grep tc {"$tc" != "$lchan"} $oc]
476 set nlist [grep tn {"$tn" != "$luser"} $nlist]
478 if {![llength $oc]} {
486 proc msg_TOPIC {p c dest topic} {
488 if {![ischan $dest]} return
489 recordlastseen_n $n "changing the topic on $dest" 1
490 note_topic [irctolower $dest] $n $topic
493 proc msg_KICK {p c chans users comment} {
494 set chans [split $chans ,]
495 set users [split $users ,]
496 if {[llength $chans] > 1} {
497 foreach chan $chans user $users { process_kickpart $chan $user }
499 foreach user $users { process_kickpart [lindex $chans 0] $user }
503 proc msg_KILL {p c user why} {
508 set nick_arys {onchans username unique}
509 # nick_onchans($luser) -> [list ... $lchan ...]
510 # nick_username($luser) -> <securely known local username>
511 # nick_unique($luser) -> <includes-counter>
512 # nick_case($luser) -> $user (valid even if no longer visible)
513 # nick_markid($luser) -> <after id for marktime>
514 # nick_telling($luser) -> <unique> mentioned|passed <when>
516 # chan_nicks($lchan) -> [list ... $luser ...]
517 # chan_lastactivity($lchan) -> [clock seconds]
519 proc lnick_forget {luser} {
520 global nick_arys chan_nicks
521 lnick_marktime_cancel $luser
522 foreach ary $nick_arys {
523 upvar #0 nick_${ary}($luser) av
526 foreach lch [array names chan_nicks] {
527 upvar #0 chan_nicks($lch) nlist
528 set nlist [grep tn {"$tn" != "$luser"} $nlist]
533 proc nick_forget {user} {
534 global nick_arys chan_nicks
535 lnick_forget [irctolower $user]
539 proc nick_case {user} {
541 set nick_case([irctolower $user]) $user
544 proc msg_NICK {p c newnick} {
545 global nick_arys nick_case calling_nick
547 recordlastseen_n $n "changing nicks to $newnick" 0
548 set calling_nick $newnick
549 recordlastseen_n $newnick "changing nicks from $n" 1
550 set luser [irctolower $n]
551 lnick_marktime_cancel $luser
552 set lusernew [irctolower $newnick]
553 foreach ary $nick_arys {
554 upvar #0 nick_${ary}($luser) old
555 upvar #0 nick_${ary}($lusernew) new
556 if {[info exists new]} { error "nick collision ?! $ary $n $newnick" }
557 if {[info exists old]} { set new $old; unset old }
559 upvar #0 nick_onchans($lusernew) oc
561 upvar #0 chan_nicks($ch) nlist
562 set nlist [grep tn {"$tn" != "$luser"} $nlist]
563 lappend nlist $lusernew
565 lnick_marktime_start $lusernew "Hi." 500 1
569 proc nick_ishere {n} {
571 upvar #0 nick_unique([irctolower $n]) u
572 if {![info exists u]} { set u [incr nick_counter].$n.[clock seconds] }
576 proc msg_JOIN {p c chan} {
579 recordlastseen_n $n "joining $chan" 1
580 set nl [irctolower $n]
581 set lchan [irctolower $chan]
582 upvar #0 nick_onchans($nl) oc
583 upvar #0 chan_nicks($lchan) nlist
584 if {![info exists oc]} {
585 global marktime_join_startdelay
586 lnick_marktime_start $nl "Welcome." $marktime_join_startdelay 1
591 proc msg_PART {p c chan args} {
593 set msg "leaving $chan"
594 if {[llength $args]} {
595 set why [lindex $args 0]
596 if {"[irctolower $why]" != "[irctolower $n]"} { append msg " ($why)" }
598 recordlastseen_n $n $msg 1
599 process_kickpart $chan $n
601 proc msg_QUIT {p c why} {
603 recordlastseen_n $n "leaving ($why)" 0
607 proc msg_PRIVMSG {p c dest text} {
611 if {[ischan $dest]} {
612 recordlastseen_n $n "invoking me in $dest" 1
615 recordlastseen_n $n "talking to me" 1
620 execute_usercommand $p $c $n $output $dest $text
623 proc msg_INVITE {p c n chan} {
624 after 1000 [list dojoin [irctolower $chan]]
627 proc grep {var predicate list} {
631 if {[uplevel 1 [list expr $predicate]]} { lappend o $v }
636 proc msg_353 {p c dest type chan nicklist} {
637 global names_chans nick_onchans
638 set lchan [irctolower $chan]
639 upvar #0 chan_nicks($lchan) nlist
640 lappend names_chans $lchan
641 if {![info exists nlist]} {
642 # We don't think we're on this channel, so ignore it !
643 # Unfortunately, because we don't get a reply to PART,
644 # we have to remember ourselves whether we're on a channel,
645 # and ignore stuff if we're not, to avoid races. Feh.
649 foreach user [split $nicklist { }] {
650 regsub {^[@+]} $user {} user
651 if {![string length $user]} continue
653 set luser [irctolower $user]
654 upvar #0 nick_onchans($luser) oc
656 lappend nlist_new $luser
662 proc msg_366 {p c args} {
663 global names_chans nick_onchans
664 set lchan [irctolower $c]
665 foreach luser [array names nick_onchans] {
666 upvar #0 nick_onchans($luser) oc
667 if {[llength names_chans] > 1} {
668 set oc [grep tc {[lsearch -exact $tc $names_chans] >= 0} $oc]
670 if {![llength $oc]} { lnick_forget $n }
675 proc check_username {target} {
677 [string length $target] > 8 ||
678 [regexp {[^-0-9a-z]} $target] ||
679 ![regexp {^[a-z]} $target]
680 } { error "invalid username" }
683 proc somedb__head {} {
685 set idl [irctolower $id]
686 upvar #0 ${nickchan}db($idl) ndbe
687 binary scan $idl H* idh
688 set idfn $fprefix$idh
689 if {![info exists iddbe] && [file exists $idfn]} {
691 try_except_finally { set newval [read $f] } {} { close $f }
692 if {[llength $newval] % 2} { error "invalid length" }
698 proc def_somedb {name arglist body} {
699 foreach {nickchan fprefix} {
704 proc ${nickchan}db_$name $arglist \
705 "set nickchan $nickchan; set fprefix $fprefix; $body"
711 foreach path [glob -nocomplain -path $fprefix *] {
712 binary scan $path "A[string length $fprefix]A*" afprefix thinghex
713 if {"$afprefix" != "$fprefix"} { error "wrong prefix $path $afprefix" }
714 lappend list [binary format H* $thinghex]
719 proc def_somedb_id {name arglist body} {
720 def_somedb $name [concat id $arglist] "somedb__head; $body"
723 def_somedb_id exists {} {
724 return [info exists iddbe]
727 def_somedb_id delete {} {
728 catch { unset iddbe }
732 set default_settings_nick {
736 tellrel {remind 3600 30}
739 set default_settings_chan {
748 set default_settings_msgs {
752 # inbound -> [<nick> <time_t> <message>] ...
753 # outbound -> [<nick> <time_t(earliest)> <count>] ...
755 def_somedb_id set {args} {
756 upvar #0 default_settings_$nickchan def
757 if {![info exists iddbe]} { set iddbe $def }
758 foreach {key value} [concat $iddbe $args] { set a($key) $value }
760 foreach {key value} [array get a] { lappend newval $key $value }
761 set f [open $idfn.new w]
765 file rename -force $idfn.new $idfn
773 def_somedb_id get {key} {
774 upvar #0 default_settings_$nickchan def
775 if {[info exists iddbe]} {
776 set l [concat $iddbe $def]
780 foreach {tkey value} $l {
781 if {"$tkey" == "$key"} { return $value }
783 error "unset setting $key"
788 if {[info exists calling_nick]} { set n $calling_nick } { set n {} }
789 return [nickdb_get $n $key]
792 proc check_notonchan {} {
794 if {[ischan $dest]} { usererror "That command must be sent privately." }
797 proc nick_securitycheck {strict} {
799 if {![nickdb_exists $n]} {
800 usererror "You are unknown to me, use `register'."
802 set wantu [nickdb_get $n username]
803 if {![string length $wantu]} {
805 usererror "That feature is only available to secure users, sorry."
810 set luser [irctolower $n]
811 upvar #0 nick_username($luser) nu
812 if {![info exists nu]} {
813 usererror "Nick $n is secure, you must identify yourself first."
815 if {"$wantu" != "$nu"} {
816 usererror "You are the wrong user -\
817 the nick $n belongs to $wantu, not $nu."
821 proc channel_ismanager {channel n} {
822 set mgrs [chandb_get $channel managers]
823 return [expr {[lsearch -exact [irctolower $mgrs] [irctolower $n]] >= 0}]
826 proc channel_securitycheck {channel} {
828 if {![channel_ismanager $channel $n]} {
829 usererror "You are not a manager of $channel."
834 proc def_chancmd {name body} {
835 proc channel/$name {} \
836 " upvar 1 target chan; upvar 1 n n; upvar 1 text text; $body"
839 proc ta_listop {findnow procvalue} {
840 # findnow and procvalue are code fragments which will be executed
841 # in the caller's level. findnow should set ta_listop_ev to
842 # the current list, and procvalue should treat ta_listop_ev as
843 # a proposed value in the list and check and possibly modify
844 # (canonicalise?) it. After ta_listop, ta_listop_ev will
845 # be the new value of the list.
846 upvar 1 ta_listop_ev exchg
849 switch -exact _$opcode {
853 foreach item $exchg { set array($item) 1 }
856 error "list change opcode must be one of + - ="
859 foreach exchg [split $text " "] {
860 if {![string length $exchg]} continue
862 if {"$opcode" != "-"} {
865 catch { unset array($exchg) }
868 set exchg [lsort [array names array]]
871 def_chancmd manager {
873 if {[chandb_exists $chan]} {
874 set ta_listop_ev [chandb_get $chan managers]
876 set ta_listop_ev [list [irctolower $n]]
879 check_nick $ta_listop_ev
880 set ta_listop_ev [irctolower $ta_listop_ev]
882 if {[llength $ta_listop_ev]} {
883 chandb_set $chan managers $ta_listop_ev
884 ucmdr "Managers of $chan: $ta_listop_ev" {}
887 ucmdr {} {} "forgets about managing $chan." {}
891 def_chancmd autojoin {
893 switch -exact [string tolower $yesno] {
896 default { error "channel autojoin must be `yes' or `no' }
898 chandb_set $chan autojoin $nv
899 ucmdr [expr {$nv ? "I will join $chan when I'm restarted " : \
900 "I won't join $chan when I'm restarted "}] {}
903 def_chancmd userinvite {
904 set nv [string tolower [ta_word]]
906 pub { set txt "!invite will work for $chan, but it won't work by /msg" }
907 here { set txt "!invite and /msg invite will work, but only for users who are already on $chan." }
908 all { set txt "Any user will be able to invite themselves or anyone else to $chan." }
909 none { set txt "I will not invite anyone to $chan." }
911 error "channel userinvite must be `pub', `here', `all' or `none'
914 chandb_set $chan userinvite $nv
920 switch -exact $what {
923 chandb_set $chan topicset {}
924 ucmdr "I won't ever change the topic of $chan." {}
927 set t [string trim $text]
928 if {![string length $t]} {
929 error "you must specific the topic to set"
931 chandb_set $chan topicset $t
932 ucmdr "Whenever I'm alone on $chan, I'll set the topic to $t." {}
936 set ta_listop_ev [chandb_get $chan topic$what]
938 if {"$ta_listop_ev" != "*"} {
939 if {![ischan $ta_listop_ev]} {
940 error "bad channel \`$ta_listop_ev' in topic $what"
942 set ta_listop_ev [irctolower $ta_listop_ev]
945 chandb_set $chan topic$what $ta_listop_ev
946 ucmdr "Topic $what list for $chan: $ta_listop_ev" {}
949 usererror "Unknown channel topic subcommand - see help channel."
956 if {"$mode" != "*" && ![regexp {^(([-+][imnpst]+)+)$} $mode mode]} {
957 error {channel mode must be * or match ([-+][imnpst]+)+}
959 chandb_set $chan mode $mode
960 if {"$mode" == "*"} {
961 ucmdr "I won't ever change the mode of $chan." {}
963 ucmdr "Whenever I'm alone on $chan, I'll set the mode to $mode." {}
968 if {[chandb_exists $chan]} {
969 set l "Settings for $chan: autojoin "
970 append l [lindex {no yes} [chandb_get $chan autojoin]]
971 append l ", mode " [chandb_get $chan mode]
972 append l ", userinvite " [chandb_get $chan userinvite] "."
973 append l "\nManagers: "
974 append l [join [chandb_get $chan managers] " "]
975 foreach {ts sep} {see "\n" tell " "} {
976 set t [chandb_get $chan topic$ts]
979 append l "Topic $ts list: $t."
981 append l "Topic $ts list is empty."
985 set t [chandb_get $chan topicset]
986 if {[string length $t]} {
987 append l "Topic to set: $t"
989 append l "I will not change the topic."
993 ucmdr {} "The channel $chan is not managed."
997 proc channelmgr_monoop {} {
1002 upvar 1 target target
1007 if {[ischan $dest]} { set target $dest }
1008 if {[ta_anymore]} { set target [ta_word] }
1010 if {![info exists target]} {
1011 usererror "You must specify, or invoke me on, the relevant channel."
1013 if {![info exists chan_nicks([irctolower $target])]} {
1014 usererror "I am not on $target."
1016 if {![ischan $target]} { error "not a valid channel" }
1018 if {![chandb_exists $target]} {
1019 usererror "$target is not a managed channel."
1021 channel_securitycheck $target
1026 sendout MODE $target +o $n
1035 global chan_nicks errorCode errorInfo
1038 if {[ischan $dest]} {
1042 set target [ta_word]
1045 set ltarget [irctolower $target]
1046 if {![ischan $target]} { error "$target is not a channel" }
1047 if {![info exists chan_nicks($ltarget)]} {
1048 usererror "I am not on $target."
1050 set ui [chandb_get $ltarget userinvite]
1052 if {"$ui" == "pub" && !$onchan} {
1053 usererror "Invitations to $target must be made there with !invite."
1055 if {"$ui" != "all"} {
1056 if {[lsearch -exact $chan_nicks($ltarget) [irctolower $n]] < 0} {
1057 usererror "Invitations to $target may only be made\
1058 by a user on the channel."
1061 if {"$ui" == "none"} {
1062 usererror "Sorry, I've not been authorised\
1063 to invite people to $target."
1066 if {"$errorCode" == "BLIGHT USER" && [channel_ismanager $target $n]} {
1068 nick_securitycheck 1
1070 if {"$errorCode" == "BLIGHT USER"} {
1071 usererror "$emsg2 Therefore you can't use your\
1072 channel manager privilege. $emsg"
1074 error $error $errorInfo $errorCode
1078 error $emsg $errorInfo $errorCode
1081 if {![ta_anymore]} {
1082 usererror "You have to say who to invite."
1085 while {[ta_anymore]} {
1086 set invitee [ta_word]
1088 lappend invitees $invitee
1090 foreach invitee $invitees {
1091 sendout INVITE $invitee $ltarget
1093 set who [lindex $invitees 0]
1094 switch -exact llength $invitees {
1095 0 { error "zero invitees" }
1097 2 { append who " and [lindex $invitees 1]" }
1099 set who [join [lreplace $invitees end end] ", "]
1100 append who " and [lindex $invitees [llength $invitees]]"
1103 ucmdr {} {} {} "invites $who to $target."
1107 if {[ischan $dest]} { set target $dest }
1108 if {![ta_anymore]} {
1111 set subcmd [ta_word]
1113 if {[ischan $subcmd]} {
1115 if {![ta_anymore]} {
1118 set subcmd [ta_word]
1121 if {![info exists target]} { error "privately, you must specify a channel" }
1122 set procname channel/$subcmd
1123 if {"$subcmd" != "show"} {
1124 if {[catch { info body $procname }]} {
1125 usererror "unknown channel setting $subcmd."
1128 if {[chandb_exists $target]} {
1129 channel_securitycheck $target
1131 nick_securitycheck 1
1132 upvar #0 chan_initialop([irctolower $target]) io
1133 upvar #0 nick_unique([irctolower $n]) u
1134 if {![info exists io]} {
1135 usererror "$target is not a managed channel."
1137 if {"$io" != "$u"} {
1138 usererror "You are not the interim manager of $target."
1140 if {"$subcmd" != "manager"} {
1141 usererror "Please use `channel manager' first."
1149 global nick_case ownmailaddr ownfullname
1152 set target [ta_word]
1153 if {![string length $text]} { error "tell them what?" }
1155 set ltarget [irctolower $target]
1157 if {[info exists nick_case($ltarget)]} { set ctarget $nick_case($ltarget) }
1159 manyset [nickdb_get $target tellsec] sec mailtoint mailwhy
1160 manyset [nickdb_get $target tellrel] rel relint relwithin
1161 switch -exact $sec {
1163 set now [clock seconds]
1164 set inbound [msgsdb_get $ltarget inbound]
1165 lappend inbound $n $now $text
1166 msgsdb_set $ltarget inbound $inbound
1168 set outbound [msgsdb_get $n outbound]
1171 foreach {recip time count} $outbound {
1172 if {"[irctolower $recip]" == "$ltarget"} {
1177 lappend noutbound $recip $time $count
1180 lappend noutbound $ctarget $now 1
1182 msgsdb_set $n outbound $noutbound
1183 set msg "OK, I'll tell $ctarget"
1184 if {$found} { append msg " that too" }
1186 if {"$sec" != "secure"} {
1187 switch -exact $rel {
1188 unreliable { append msg "neither reliably nor securely" }
1189 remind { append msg "pretty reliably, but not securely" }
1190 pester { append msg "reliably but not securely" }
1193 switch -exact $rel {
1194 unreliable { append msg "securely but not reliably" }
1195 remind { append msg "securely and pretty reliably" }
1196 pester { append msg "reliably and securely" }
1203 set fmtmsg [exec fmt << " $text"]
1204 exec /usr/sbin/sendmail -odb -oi -t -oee -f $mailwhy \
1206 "From: $ownmailaddr ($ownfullname)
1208 Subject: IRC tell from $n
1210 $n asked me[expr {[ischan $dest] ? " on $dest" : ""}] to tell you:
1211 [exec fmt << " $text"]
1213 (This message was for your nick $ctarget; your account $mailwhy
1214 arranged for it to be forwarded to $mailtoint.)
1217 "I've mailed $ctarget, which is what they prefer." \
1221 usererror "Sorry, $ctarget does not want me to take messages."
1224 error "bad tellsec $sec"
1231 set target [ta_word]; ta_nomore
1236 set myself [expr {"$target" != "$n"}]
1238 set ltarget [irctolower $target]
1239 upvar #0 nick_case($ltarget) ctarget
1241 if {[info exists ctarget]} {
1242 upvar #0 nick_onchans($ltarget) oc
1243 upvar #0 nick_username($ltarget) nu
1244 if {[info exists oc]} { set nshow $ctarget }
1246 if {![nickdb_exists $ltarget]} {
1247 set ol "$nshow is not a registered nick."
1248 } elseif {[string length [set username [nickdb_get $target username]]]} {
1249 set ol "The nick $nshow belongs to the user $username."
1251 set ol "The nick $nshow is registered (but not to a username)."
1253 if {![info exists ctarget] || ![info exists oc]} {
1255 append ol "\nI can't see $nshow on anywhere."
1257 append ol "\nYou aren't on any channels with me."
1259 } elseif {![info exists nu]} {
1260 append ol "\n$nshow has not identified themselves."
1261 } elseif {![info exists username]} {
1262 append ol "\n$nshow has identified themselves as the user $nu."
1263 } elseif {"$nu" != "$username"} {
1264 append ol "\nHowever, $nshow is being used by the user $nu."
1266 append ol "\n$nshow has identified themselves to me."
1274 set old [nickdb_exists $n]
1275 if {$old} { nick_securitycheck 0 }
1276 set luser [irctolower $n]
1277 switch -exact [string tolower [string trim $text]] {
1279 upvar #0 nick_username($luser) nu
1280 if {![info exists nu]} {
1282 "You must identify yourself before using `register'. See `help identify', or use `register insecure'."
1284 nickdb_set $n username $nu
1285 ucmdr {} {} "makes a note of your username." {}
1289 ucmdr {} {} "forgets your nickname." {}
1292 nickdb_set $n username {}
1294 ucmdr {} "Security is now disabled for your nickname !"
1296 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."
1300 error "you mean register / register delete / register insecure"
1305 proc timeformat_desc {tf} {
1307 ks { return "Times will be displayed in seconds or kiloseconds." }
1308 hms { return "Times will be displayed in hours, minutes, etc." }
1309 default { error "invalid timeformat: $v" }
1314 proc def_setting {opt show_body set_body} {
1316 lappend settings $opt
1317 proc set_show/$opt {} "
1321 if {![string length $set_body]} return
1322 proc set_set/$opt {} "
1329 proc tellme_sec_desc {v} {
1330 manyset $v sec mailtoint
1331 switch -exact $sec {
1333 return "I'll tell you your messages whenever I see you."
1337 "I'll keep the bodies of your messages private until you identify yourself, reminding you every [showintervalsecs $mailtoint 1]."
1340 return "I shan't accept messages for you."
1343 return "I'll forward your messages by email to $mailtoint."
1346 error "bad tellsec $sec"
1351 proc tellme_rel_desc {v} {
1352 manyset $v rel every within
1353 switch -exact $rel {
1355 return "As soon as I've told you, I'll forget the message - note that this means messages can get lost !"
1361 set u ", or talk on channel within [showintervalsecs $within 1] of me having told you"
1364 error "bad tellrel $rel"
1367 return "I'll remind you every [showintervalsecs $every 1] until you say delmsg$u."
1370 def_setting timeformat {
1371 set tf [nickdb_get $n timeformat]
1372 return "$tf: [timeformat_desc $tf]"
1374 set tf [string tolower [ta_word]]
1376 set desc [timeformat_desc $tf]
1377 nickdb_set $n timeformat $tf
1381 proc marktime_desc {mt} {
1382 if {"$mt" == "off"} {
1383 return "I will not send you periodic messages."
1384 } elseif {"$mt" == "once"} {
1385 return "I will send you one informational message when I see you."
1387 return "I'll send you a message every [showintervalsecs $mt 0]."
1391 def_setting marktime {
1392 set mt [nickdb_get $n marktime]
1394 if {[string match {[0-9]*} $mt]} { append p s }
1396 append p [marktime_desc $mt]
1400 set mt [string tolower [ta_word]]
1403 if {"$mt" == "off" || "$mt" == "once"} {
1405 set mt [parse_interval $mt $marktime_min]
1407 nickdb_set $n marktime $mt
1408 lnick_marktime_start [irctolower $n] "So:" 500 0
1409 ucmdr {} [marktime_desc $mt]
1412 def_setting security {
1413 set s [nickdb_get $n username]
1414 if {[string length $s]} {
1415 return "Your nick, $n, is controlled by the user $s."
1417 return "Your nick, $n, is not secure."
1421 def_setting tellme {
1422 set secv [nickdb_get $n tellsec]
1423 set ms [tellme_sec_desc $secv]
1425 switch -exact $sec {
1427 set mr [tellme_rel_desc [nickdb_get $n tellrel]]
1435 set setting [string tolower [ta_word]]
1436 switch -exact $setting {
1437 insecure - secure - refuse {
1439 if {"$setting" == "refuse" && [llength [msgsdb_get $n inbound]]} {
1440 usererror "You must delete the messages you have, first."
1446 set u [nickdb_get $n username]
1447 if {![string length $u]} {
1448 usererror "Sorry, you must register secure to have your messages mailed (to prevent the use of this feature for spamming)."
1451 set v [list mailto [ta_word] $u]
1453 unreliable - pester - remind {
1454 manyset [nickdb_get $n tellsec] sec
1455 switch -exact $sec {
1457 error "can't change message delivery conditions when message disposition prevents messages from being left"
1462 if {"$setting" != "unreliable"} {
1463 set every [parse_interval [ta_word] 300]
1466 if {"$setting" == "remind"} {
1468 set within [parse_interval [ta_word] 5]
1472 if {$within > $every} {
1473 error "remind interval must be at least time to respond"
1480 error "invalid tellme setting $setting"
1483 nickdb_set $n tell$sr $v
1484 ucmdr [tellme_${sr}_desc $v] {}
1487 proc lnick_checktold {luser} {
1488 set ml [msgsdb_get $luser outbound]
1489 if {![llength $ml]} return
1490 set is1 [expr {[llength $ml]==3}]
1491 set m1 "FYI, I haven't yet passed on your"
1493 set now [clock seconds]
1494 while {[llength $ml]} {
1496 set ml [lreplace $ml 0 2]
1497 set td [expr {$now-$t}]
1499 set iv [showinterval $td]
1501 set if1 "message to $r, $iv."
1503 set iv [showintervalsecs $td 0]
1504 set ifo "$r, $n messages, oldest $iv"
1505 set if1 "$n messages to $r, oldest $iv."
1508 sendprivmsg $luser "$m1 $if1"
1511 lappend ol " to $ifo[expr {[llength $ml] ? ";" : "."}]"
1514 sendprivmsg $luser "$m1 messages:"
1515 msendprivmsg $luser $ol
1522 if {![nickdb_exists $n]} {
1523 ucmdr {} "You are unknown to me and so have no settings. (Use `register'.)"
1525 if {![ta_anymore]} {
1527 foreach opt $settings {
1528 lappend ol [format "%-10s %s" $opt [set_show/$opt]]
1530 ucmdr {} [join $ol "\n"]
1533 if {[catch { info body set_show/$opt }]} {
1534 error "no setting $opt"
1536 if {![ta_anymore]} {
1537 ucmdr {} "$opt: [set_show/$opt]"
1539 nick_securitycheck 0
1540 if {[catch { info body set_set/$opt }]} {
1541 error "setting $opt cannot be set with `set'"
1548 def_ucmd identpass {
1549 set username [ta_word]
1550 set passmd5 [md5sum "[ta_word]\n"]
1554 set luser [irctolower $n]
1555 upvar #0 nick_onchans($luser) onchans
1556 if {![info exists onchans] || ![llength $onchans]} {
1557 ucmdr "You must be on a channel with me to identify yourself." {}
1559 check_username $username
1560 exec userv --timeout 3 $username << "$passmd5\n" > /dev/null \
1562 upvar #0 nick_username($luser) rec_username
1563 set rec_username $username
1564 ucmdr "Pleased to see you, $username." {}
1568 set target [ta_word]
1570 check_username $target
1573 upvar #0 lastsummon($target) ls
1574 set now [clock seconds]
1575 if {[info exists ls]} {
1576 set interval [expr {$now - $ls}]
1577 if {$interval < 30} {
1579 "Please be patient; $target was summoned only [showinterval $interval]."
1582 regsub {^[^!]*!} $p {} path
1584 exec userv --timeout 3 $target irc-summon $n $path \
1585 [expr {[ischan $dest] ? "$dest" : ""}] \
1588 regsub -all "\n" $rv { / } rv
1591 if {[regexp {^problem (.*)} $rv dummy problem]} {
1592 ucmdr {} "The user `$target' $problem."
1593 } elseif {[regexp {^ok ([^ ]+) ([0-9]+)$} $rv dummy tty idlesince]} {
1594 set idletime [expr {$now - $idlesince}]
1596 ucmdr {} {} {} "invites $target ($tty[expr {
1597 $idletime > 10 ? ", idle for [showintervalsecs $idletime 0]" : ""
1599 [ischan $dest] ? "join us here" : "talk to you"
1602 error "unexpected response from userv service: $rv"
1606 proc md5sum {value} { exec md5sum << $value }
1609 global lastseen nick
1612 set nlower [irctolower $ncase]
1614 set now [clock seconds]
1615 if {"$nlower" == "[irctolower $nick]"} {
1616 usererror "I am not self-aware."
1617 } elseif {![info exists lastseen($nlower)]} {
1618 set rstr "I've never seen $ncase."
1620 manyset $lastseen($nlower) realnick time what
1621 set howlong [expr {$now - $time}]
1622 set string [showinterval $howlong]
1623 set rstr "I last saw $realnick $string, $what."
1625 if {[ischan $dest]} {
1630 upvar #0 lookedfor($nlower) lf
1631 if {[info exists lf]} { set oldvalue $lf } else { set oldvalue {} }
1632 set lf [list [list $now $n $where]]
1633 foreach v $oldvalue {
1634 if {"[irctolower [lindex $v 1]]" == "[irctolower $n]"} continue
1640 proc lnick_marktime_cancel {luser} {
1641 upvar #0 nick_markid($luser) mi
1642 if {![info exists mi]} return
1643 catch { after cancel $mi }
1647 proc lnick_marktime_doafter {luser why ms mentiontold} {
1648 lnick_marktime_cancel $luser
1649 upvar #0 nick_markid($luser) mi
1650 set mi [after $ms [list lnick_marktime_now $luser $why 0]]
1653 proc lnick_marktime_reset {luser} {
1654 set mt [nickdb_get $luser marktime]
1655 if {"$mt" == "off" || "$mt" == "once"} return
1656 lnick_marktime_doafter $luser "Time passes." [expr {$mt*1000}] 0
1659 proc lnick_marktime_start {luser why ms mentiontold} {
1660 set mt [nickdb_get $luser marktime]
1661 if {"$mt" == "off"} {
1662 lnick_marktime_cancel $luser
1663 after $ms [list lnick_checktold $luser]
1665 lnick_marktime_doafter $luser $why $ms $mentiontold
1669 proc lnick_marktime_now {luser why mentiontold} {
1670 upvar #0 nick_onchans($luser) oc
1672 set calling_nick $luser
1673 sendprivmsg $luser [lnick_pingstring $why $oc ""]
1674 if {$mentiontold} { lnick_checktold $luser }
1675 lnick_marktime_reset $luser
1678 proc lnick_pingstring {why oc apstring} {
1680 catch { exec uptime } uptime
1681 set nnicks [llength [array names nick_onchans]]
1683 {^ *([0-9:apm]+) +up.*, +(\d+) users?, +load average: +([0-9., ]+) *$} \
1684 $uptime dummy time users load]} {
1685 regsub -all , $load {} load
1686 set uptime "$time $nnicks/$users $load"
1688 append uptime ", $nnicks nicks"
1690 if {[llength $oc]} {
1694 upvar #0 chan_lastactivity($ch) la
1695 if {![info exists la]} continue
1696 if {$la <= $best_la} continue
1697 set since [showintervalsecs [expr {[clock seconds]-$la}] 1]
1698 set activity "$ch $since"
1705 append str " " $uptime " " $activity
1706 if {[string length $apstring]} { append str " " $apstring }
1712 set ln [irctolower $n]
1713 if {[ischan $dest]} {
1714 set oc [irctolower $dest]
1717 if {[info exists nick_onchans($ln)]} {
1718 set oc $nick_onchans($ln)
1722 if {[llength $oc]} { lnick_marktime_reset $ln }
1725 ucmdr {} [lnick_pingstring "Pong!" $oc $text]
1728 proc ensure_globalsecret {} {
1731 if {[info exists globalsecret]} return
1732 set gsfile [open /dev/urandom r]
1733 fconfigure $gsfile -translation binary
1734 set globalsecret [read $gsfile 32]
1735 binary scan $globalsecret H* globalsecret
1741 foreach chan [chandb_list] {
1742 if {[chandb_get $chan autojoin]} { dojoin $chan }