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 {nl event} {
149 # for all except `talk' we delay 750ms
150 switch -exact $event {
154 check_telling_core $nl talk
155 check_telling_core $nl act
158 after 750 [list check_telling_core $nl $event]
161 error "check_telling $nl $event"
166 proc check_telling_core {nl event} {
167 # event is `talk', `act' or `come'
168 # When user talks we actually get talk now and act later
169 FIXME - make it be called with come
170 FIXME - implement all cmds
171 FIXME - implement tells_deliver set stt [list $u passed $now]
172 FIXME - implement tells_delete catch { unset stt } ?
173 set iml [msgdb_get $nl inbound]
174 if {![llength $iml]} return
176 upvar #0 nick_telling($nl) telling
177 upvar #0 nick_unique($nl) u
179 if {[info exists telling]} {
180 manyset $telling u2 stt telling_when
181 if {"$u2" != "$u"} { unset telling; unset stt; unset telling_when }
184 if {![info exists stt]} {
186 set telling_when $now
189 set ago [expr {$now - $telling_when}]
191 # evstate is string of letters
196 # security level and timing
198 # ss Secure and soon (before interval)
199 # sl Secure and late (after interval)
204 # reliability and timing
206 # rv Remind, very soon (before within-interval)
207 # rs Remind, soon (between)
208 # rl Remind, late (aftr every-interval)
209 # ps Pester, soon (before interval)
210 # pl Pester, late (after interval)
211 # current identification
216 # h Hidden (invisible, no unique)
218 manyset [nickdb_get $n tellsec] sec secwhen
220 insecure { set evstate ii }
221 secure { set evstate [expr {$ago<$secwhen ? "sl" : "ss"}] }
222 default { set evstate "#$sec#" }
225 append evstate [string range $stt 0 0]
227 manyset [nickdb_set $n tellrel] rel relint relwithin
229 unreliable { append evstate uu }
230 remind { append evstate [expr {
231 $ago<$relwithin ? "rv" : $ago<$relint ? "rs" : "rl"
233 pester { append evstate [expr {$ago<$relint ? "ps" : "pl"}] }
234 default { append evstate "#$rel#" }
237 upvar #0 nick_username($nl) nu
238 if {[info exists nu] && "$nu" == "[nickdb_get $nl username]"} {
244 append evstate [expr {[info exists u] ? "v" : "h"}]
246 switch -glob $evstate {
248 # consider delivered:
249 # (very recently passed, and the user talks)
254 # (any other `talk's)
256 ?iin???? - ?iip?l?? - ?ii????? -
257 ?s?n??iv - ?s?m??iv - ?s?p?liv {
259 # (insecure and not passed recently, or just arriving;
260 # secure and not passed recently)
263 ?ssp???? - ???p?s?? - ???p?v?? {
265 # (recently mentioned or passed
266 # immediate `talk' thing)
268 ?s?n???? - ?slm???? - cs?????? {
270 # (secure and not mentioned recently or just arriving,
271 # and should not pass)
273 {You have messages (so identify yourself please).}]
274 set stt [list $u mentioned $now]
277 error "check_telling_core nl=$nl evstate=$evstate ?"
282 proc recordlastseen_n {n how here} {
283 global lastseen lookedfor
284 set nl [irctolower $n]
285 set now [clock seconds]
286 set lastseen($nl) [list $n $now $how]
290 check_telling $nl [lindex {none act talk} $here]
292 upvar #0 lookedfor($nl) lf
293 if {[info exists lf]} {
294 switch -exact [llength $lf] {
299 manyset [lindex $lf 0] when who where
301 "FYI, $who was looking for you [looking_whenwhere $when $where]."]
304 msg_begin tosend $n "FYI, people have been looking for you:"
310 msg_append tosend " "
311 } elseif {$i == [llength $lf]} {
312 msg_append tosend " and "
315 msg_append tosend ", "
317 manyset $e when who where
319 "$who ([looking_whenwhere $when $where])$fin"
321 set ml [msg_finish tosend]
325 msendprivmsg_delayed 1000 $n $ml
329 proc note_topic {showoff whoby topic} {
330 set msg "FYI, $whoby has changed the topic on $showoff"
331 if {[string length $topic] < 160} {
332 append msg " to $topic"
334 append msg " but it is too long to reproduce here !"
336 set showoff [irctolower $showoff]
337 set tell [chandb_get $showoff topictell]
338 if {[lsearch -exact $tell *] >= 0} {
339 set tryspies [chandb_list]
343 foreach spy $tryspies {
344 set see [chandb_get $spy topicsee]
345 if {[lsearch -exact $see $showoff] >= 0 || \
346 ([lsearch -exact $see *] >= 0 && \
347 [lsearch -exact $tell $spy] >= 0)} {
348 sendprivmsg $spy $msg
353 proc recordlastseen_p {p how here} {
355 recordlastseen_n $n $how $here
358 proc chanmode_arg {} {
360 set rv [lindex $cm_args 0]
361 set cm_args [lreplace cm_args 0 0]
365 proc chanmode_o1 {m g p chan} {
366 global nick chan_initialop
368 set who [chanmode_arg]
369 recordlastseen_n $n "being nice to $who" 1
370 if {"[irctolower $who]" == "[irctolower $nick]"} {
371 set nlower [irctolower $n]
372 upvar #0 nick_unique($nlower) u
373 if {[chandb_exists $chan]} {
374 sendprivmsg $n Thanks.
375 } elseif {![info exists u]} {
376 sendprivmsg $n {Op me while not on the channel, why don't you ?}
378 set chan_initialop([irctolower $chan]) $u
380 "Thanks. You can use `channel manager ...' to register this channel."
381 if {![nickdb_exists $n] || ![string length [nickdb_get $n username]]} {
383 "(But to do that you must register your nick securely first.)"
389 proc chanmode_o0 {m g p chan} {
392 set who [chanmode_arg]
393 recordlastseen_p $p "being mean to $who" 1
394 if {"[irctolower $who]" == "[irctolower $nick]"} {
395 set chandeop($chan) [list [clock seconds] $p]
399 proc msg_MODE {p c dest modelist args} {
400 if {![ischan $dest]} return
401 if {[regexp {^\-(.+)$} $modelist dummy modelist]} {
403 } elseif {[regexp {^\+(.+)$} $modelist dummy modelist]} {
406 error "invalid modelist"
408 foreach m [split $modelist] {
409 set procname chanmode_$m$give
410 if {[catch { info body $procname }]} {
411 recordlastseen_p $p "fiddling with $dest" 1
413 $procname $m $give $p $dest
418 proc leaving {lchan} {
419 foreach luser [array names nick_onchans] {
420 upvar #0 nick_onchans($luser) oc
421 set oc [grep tc {"$tc" != "$lchan"} $oc]
423 upvar #0 chan_nicks($lchan) nlist
425 upvar #0 chan_lastactivity($lchan) la
429 proc doleave {lchan} {
434 proc dojoin {lchan} {
437 set chan_nicks($lchan) {}
440 proc check_justme {lchan} {
442 upvar #0 chan_nicks($lchan) nlist
443 if {[llength $nlist] != 1} return
444 if {"[lindex $nlist 0]" != "[irctolower $nick]"} return
445 if {[chandb_exists $lchan]} {
446 set mode [chandb_get $lchan mode]
447 if {"$mode" != "*"} {
448 sendout MODE $lchan $mode
450 set topic [chandb_get $lchan topicset]
451 if {[string length $topic]} {
452 sendout TOPIC $lchan $topic
459 proc process_kickpart {chan user} {
462 set luser [irctolower $user]
463 set lchan [irctolower $chan]
464 if {![ischan $chan]} { error "not a channel" }
465 if {"$luser" == "[irctolower $nick]"} {
468 upvar #0 nick_onchans($luser) oc
469 upvar #0 chan_nicks($lchan) nlist
470 set oc [grep tc {"$tc" != "$lchan"} $oc]
471 set nlist [grep tn {"$tn" != "$luser"} $nlist]
473 if {![llength $oc]} {
481 proc msg_TOPIC {p c dest topic} {
483 if {![ischan $dest]} return
484 recordlastseen_n $n "changing the topic on $dest" 1
485 note_topic [irctolower $dest] $n $topic
488 proc msg_KICK {p c chans users comment} {
489 set chans [split $chans ,]
490 set users [split $users ,]
491 if {[llength $chans] > 1} {
492 foreach chan $chans user $users { process_kickpart $chan $user }
494 foreach user $users { process_kickpart [lindex $chans 0] $user }
498 proc msg_KILL {p c user why} {
503 set nick_arys {onchans username unique}
504 # nick_onchans($luser) -> [list ... $lchan ...]
505 # nick_username($luser) -> <securely known local username>
506 # nick_unique($luser) -> <includes-counter>
507 # nick_case($luser) -> $user (valid even if no longer visible)
508 # nick_markid($luser) -> <after id for marktime>
509 # nick_telling($luser) -> <unique> mentioned|passed <when>
511 # chan_nicks($lchan) -> [list ... $luser ...]
512 # chan_lastactivity($lchan) -> [clock seconds]
514 proc lnick_forget {luser} {
515 global nick_arys chan_nicks
516 lnick_marktime_cancel $luser
517 foreach ary $nick_arys {
518 upvar #0 nick_${ary}($luser) av
521 foreach lch [array names chan_nicks] {
522 upvar #0 chan_nicks($lch) nlist
523 set nlist [grep tn {"$tn" != "$luser"} $nlist]
528 proc nick_forget {user} {
529 global nick_arys chan_nicks
530 lnick_forget [irctolower $user]
534 proc nick_case {user} {
536 set nick_case([irctolower $user]) $user
539 proc msg_NICK {p c newnick} {
540 global nick_arys nick_case calling_nick
542 recordlastseen_n $n "changing nicks to $newnick" 0
543 set calling_nick $newnick
544 recordlastseen_n $newnick "changing nicks from $n" 1
545 set luser [irctolower $n]
546 lnick_marktime_cancel $luser
547 set lusernew [irctolower $newnick]
548 foreach ary $nick_arys {
549 upvar #0 nick_${ary}($luser) old
550 upvar #0 nick_${ary}($lusernew) new
551 if {[info exists new]} { error "nick collision ?! $ary $n $newnick" }
552 if {[info exists old]} { set new $old; unset old }
554 upvar #0 nick_onchans($lusernew) oc
556 upvar #0 chan_nicks($ch) nlist
557 set nlist [grep tn {"$tn" != "$luser"} $nlist]
558 lappend nlist $lusernew
560 lnick_marktime_start $lusernew "Hi." 500 1
564 proc nick_ishere {n} {
566 upvar #0 nick_unique([irctolower $n]) u
567 if {![info exists u]} { set u [incr nick_counter].$n.[clock seconds] }
571 proc msg_JOIN {p c chan} {
574 recordlastseen_n $n "joining $chan" 1
575 set nl [irctolower $n]
576 set lchan [irctolower $chan]
577 upvar #0 nick_onchans($nl) oc
578 upvar #0 chan_nicks($lchan) nlist
579 if {![info exists oc]} {
580 global marktime_join_startdelay
581 lnick_marktime_start $nl "Welcome." $marktime_join_startdelay 1
586 proc msg_PART {p c chan args} {
588 set msg "leaving $chan"
589 if {[llength $args]} {
590 set why [lindex $args 0]
591 if {"[irctolower $why]" != "[irctolower $n]"} { append msg " ($why)" }
593 recordlastseen_n $n $msg 1
594 process_kickpart $chan $n
596 proc msg_QUIT {p c why} {
598 recordlastseen_n $n "leaving ($why)" 0
602 proc msg_PRIVMSG {p c dest text} {
606 if {[ischan $dest]} {
607 recordlastseen_n $n "invoking me in $dest" 1
610 recordlastseen_n $n "talking to me" 1
615 execute_usercommand $p $c $n $output $dest $text
618 proc msg_INVITE {p c n chan} {
619 after 1000 [list dojoin [irctolower $chan]]
622 proc grep {var predicate list} {
626 if {[uplevel 1 [list expr $predicate]]} { lappend o $v }
631 proc msg_353 {p c dest type chan nicklist} {
632 global names_chans nick_onchans
633 set lchan [irctolower $chan]
634 upvar #0 chan_nicks($lchan) nlist
635 lappend names_chans $lchan
636 if {![info exists nlist]} {
637 # We don't think we're on this channel, so ignore it !
638 # Unfortunately, because we don't get a reply to PART,
639 # we have to remember ourselves whether we're on a channel,
640 # and ignore stuff if we're not, to avoid races. Feh.
644 foreach user [split $nicklist { }] {
645 regsub {^[@+]} $user {} user
646 if {![string length $user]} continue
648 set luser [irctolower $user]
649 upvar #0 nick_onchans($luser) oc
651 lappend nlist_new $luser
657 proc msg_366 {p c args} {
658 global names_chans nick_onchans
659 set lchan [irctolower $c]
660 foreach luser [array names nick_onchans] {
661 upvar #0 nick_onchans($luser) oc
662 if {[llength names_chans] > 1} {
663 set oc [grep tc {[lsearch -exact $tc $names_chans] >= 0} $oc]
665 if {![llength $oc]} { lnick_forget $n }
670 proc check_username {target} {
672 [string length $target] > 8 ||
673 [regexp {[^-0-9a-z]} $target] ||
674 ![regexp {^[a-z]} $target]
675 } { error "invalid username" }
678 proc somedb__head {} {
680 set idl [irctolower $id]
681 upvar #0 ${nickchan}db($idl) ndbe
682 binary scan $idl H* idh
683 set idfn $fprefix$idh
684 if {![info exists iddbe] && [file exists $idfn]} {
686 try_except_finally { set newval [read $f] } {} { close $f }
687 if {[llength $newval] % 2} { error "invalid length" }
693 proc def_somedb {name arglist body} {
694 foreach {nickchan fprefix} {
699 proc ${nickchan}db_$name $arglist \
700 "set nickchan $nickchan; set fprefix $fprefix; $body"
706 foreach path [glob -nocomplain -path $fprefix *] {
707 binary scan $path "A[string length $fprefix]A*" afprefix thinghex
708 if {"$afprefix" != "$fprefix"} { error "wrong prefix $path $afprefix" }
709 lappend list [binary format H* $thinghex]
714 proc def_somedb_id {name arglist body} {
715 def_somedb $name [concat id $arglist] "somedb__head; $body"
718 def_somedb_id exists {} {
719 return [info exists iddbe]
722 def_somedb_id delete {} {
723 catch { unset iddbe }
727 set default_settings_nick {
731 tellrel {remind 3600 30}
734 set default_settings_chan {
743 set default_settings_msgs {
747 # inbound -> [<nick> <time_t> <message>] ...
748 # outbound -> [<nick> <time_t(earliest)> <count>] ...
750 def_somedb_id set {args} {
751 upvar #0 default_settings_$nickchan def
752 if {![info exists iddbe]} { set iddbe $def }
753 foreach {key value} [concat $iddbe $args] { set a($key) $value }
755 foreach {key value} [array get a] { lappend newval $key $value }
756 set f [open $idfn.new w]
760 file rename -force $idfn.new $idfn
768 def_somedb_id get {key} {
769 upvar #0 default_settings_$nickchan def
770 if {[info exists iddbe]} {
771 set l [concat $iddbe $def]
775 foreach {tkey value} $l {
776 if {"$tkey" == "$key"} { return $value }
778 error "unset setting $key"
783 if {[info exists calling_nick]} { set n $calling_nick } { set n {} }
784 return [nickdb_get $n $key]
787 proc check_notonchan {} {
789 if {[ischan $dest]} { usererror "That command must be sent privately." }
792 proc nick_securitycheck {strict} {
794 if {![nickdb_exists $n]} {
795 usererror "You are unknown to me, use `register'."
797 set wantu [nickdb_get $n username]
798 if {![string length $wantu]} {
800 usererror "That feature is only available to secure users, sorry."
805 set luser [irctolower $n]
806 upvar #0 nick_username($luser) nu
807 if {![info exists nu]} {
808 usererror "Nick $n is secure, you must identify yourself first."
810 if {"$wantu" != "$nu"} {
811 usererror "You are the wrong user -\
812 the nick $n belongs to $wantu, not $nu."
816 proc channel_ismanager {channel n} {
817 set mgrs [chandb_get $channel managers]
818 return [expr {[lsearch -exact [irctolower $mgrs] [irctolower $n]] >= 0}]
821 proc channel_securitycheck {channel} {
823 if {![channel_ismanager $channel $n]} {
824 usererror "You are not a manager of $channel."
829 proc def_chancmd {name body} {
830 proc channel/$name {} \
831 " upvar 1 target chan; upvar 1 n n; upvar 1 text text; $body"
834 proc ta_listop {findnow procvalue} {
835 # findnow and procvalue are code fragments which will be executed
836 # in the caller's level. findnow should set ta_listop_ev to
837 # the current list, and procvalue should treat ta_listop_ev as
838 # a proposed value in the list and check and possibly modify
839 # (canonicalise?) it. After ta_listop, ta_listop_ev will
840 # be the new value of the list.
841 upvar 1 ta_listop_ev exchg
844 switch -exact _$opcode {
848 foreach item $exchg { set array($item) 1 }
851 error "list change opcode must be one of + - ="
854 foreach exchg [split $text " "] {
855 if {![string length $exchg]} continue
857 if {"$opcode" != "-"} {
860 catch { unset array($exchg) }
863 set exchg [lsort [array names array]]
866 def_chancmd manager {
868 if {[chandb_exists $chan]} {
869 set ta_listop_ev [chandb_get $chan managers]
871 set ta_listop_ev [list [irctolower $n]]
874 check_nick $ta_listop_ev
875 set ta_listop_ev [irctolower $ta_listop_ev]
877 if {[llength $ta_listop_ev]} {
878 chandb_set $chan managers $ta_listop_ev
879 ucmdr "Managers of $chan: $ta_listop_ev" {}
882 ucmdr {} {} "forgets about managing $chan." {}
886 def_chancmd autojoin {
888 switch -exact [string tolower $yesno] {
891 default { error "channel autojoin must be `yes' or `no' }
893 chandb_set $chan autojoin $nv
894 ucmdr [expr {$nv ? "I will join $chan when I'm restarted " : \
895 "I won't join $chan when I'm restarted "}] {}
898 def_chancmd userinvite {
899 set nv [string tolower [ta_word]]
901 pub { set txt "!invite will work for $chan, but it won't work by /msg" }
902 here { set txt "!invite and /msg invite will work, but only for users who are already on $chan." }
903 all { set txt "Any user will be able to invite themselves or anyone else to $chan." }
904 none { set txt "I will not invite anyone to $chan." }
906 error "channel userinvite must be `pub', `here', `all' or `none'
909 chandb_set $chan userinvite $nv
915 switch -exact $what {
918 chandb_set $chan topicset {}
919 ucmdr "I won't ever change the topic of $chan." {}
922 set t [string trim $text]
923 if {![string length $t]} {
924 error "you must specific the topic to set"
926 chandb_set $chan topicset $t
927 ucmdr "Whenever I'm alone on $chan, I'll set the topic to $t." {}
931 set ta_listop_ev [chandb_get $chan topic$what]
933 if {"$ta_listop_ev" != "*"} {
934 if {![ischan $ta_listop_ev]} {
935 error "bad channel \`$ta_listop_ev' in topic $what"
937 set ta_listop_ev [irctolower $ta_listop_ev]
940 chandb_set $chan topic$what $ta_listop_ev
941 ucmdr "Topic $what list for $chan: $ta_listop_ev" {}
944 usererror "Unknown channel topic subcommand - see help channel."
951 if {"$mode" != "*" && ![regexp {^(([-+][imnpst]+)+)$} $mode mode]} {
952 error {channel mode must be * or match ([-+][imnpst]+)+}
954 chandb_set $chan mode $mode
955 if {"$mode" == "*"} {
956 ucmdr "I won't ever change the mode of $chan." {}
958 ucmdr "Whenever I'm alone on $chan, I'll set the mode to $mode." {}
963 if {[chandb_exists $chan]} {
964 set l "Settings for $chan: autojoin "
965 append l [lindex {no yes} [chandb_get $chan autojoin]]
966 append l ", mode " [chandb_get $chan mode]
967 append l ", userinvite " [chandb_get $chan userinvite] "."
968 append l "\nManagers: "
969 append l [join [chandb_get $chan managers] " "]
970 foreach {ts sep} {see "\n" tell " "} {
971 set t [chandb_get $chan topic$ts]
974 append l "Topic $ts list: $t."
976 append l "Topic $ts list is empty."
980 set t [chandb_get $chan topicset]
981 if {[string length $t]} {
982 append l "Topic to set: $t"
984 append l "I will not change the topic."
988 ucmdr {} "The channel $chan is not managed."
992 proc channelmgr_monoop {} {
997 upvar 1 target target
1002 if {[ischan $dest]} { set target $dest }
1003 if {[ta_anymore]} { set target [ta_word] }
1005 if {![info exists target]} {
1006 usererror "You must specify, or invoke me on, the relevant channel."
1008 if {![info exists chan_nicks([irctolower $target])]} {
1009 usererror "I am not on $target."
1011 if {![ischan $target]} { error "not a valid channel" }
1013 if {![chandb_exists $target]} {
1014 usererror "$target is not a managed channel."
1016 channel_securitycheck $target
1021 sendout MODE $target +o $n
1030 global chan_nicks errorCode errorInfo
1033 if {[ischan $dest]} {
1037 set target [ta_word]
1040 set ltarget [irctolower $target]
1041 if {![ischan $target]} { error "$target is not a channel" }
1042 if {![info exists chan_nicks($ltarget)]} {
1043 usererror "I am not on $target."
1045 set ui [chandb_get $ltarget userinvite]
1047 if {"$ui" == "pub" && !$onchan} {
1048 usererror "Invitations to $target must be made there with !invite."
1050 if {"$ui" != "all"} {
1051 if {[lsearch -exact $chan_nicks($ltarget) [irctolower $n]] < 0} {
1052 usererror "Invitations to $target may only be made\
1053 by a user on the channel."
1056 if {"$ui" == "none"} {
1057 usererror "Sorry, I've not been authorised\
1058 to invite people to $target."
1061 if {"$errorCode" == "BLIGHT USER" && [channel_ismanager $target $n]} {
1063 nick_securitycheck 1
1065 if {"$errorCode" == "BLIGHT USER"} {
1066 usererror "$emsg2 Therefore you can't use your\
1067 channel manager privilege. $emsg"
1069 error $error $errorInfo $errorCode
1073 error $emsg $errorInfo $errorCode
1076 if {![ta_anymore]} {
1077 usererror "You have to say who to invite."
1080 while {[ta_anymore]} {
1081 set invitee [ta_word]
1083 lappend invitees $invitee
1085 foreach invitee $invitees {
1086 sendout INVITE $invitee $ltarget
1088 set who [lindex $invitees 0]
1089 switch -exact llength $invitees {
1090 0 { error "zero invitees" }
1092 2 { append who " and [lindex $invitees 1]" }
1094 set who [join [lreplace $invitees end end] ", "]
1095 append who " and [lindex $invitees [llength $invitees]]"
1098 ucmdr {} {} {} "invites $who to $target."
1102 if {[ischan $dest]} { set target $dest }
1103 if {![ta_anymore]} {
1106 set subcmd [ta_word]
1108 if {[ischan $subcmd]} {
1110 if {![ta_anymore]} {
1113 set subcmd [ta_word]
1116 if {![info exists target]} { error "privately, you must specify a channel" }
1117 set procname channel/$subcmd
1118 if {"$subcmd" != "show"} {
1119 if {[catch { info body $procname }]} {
1120 usererror "unknown channel setting $subcmd."
1123 if {[chandb_exists $target]} {
1124 channel_securitycheck $target
1126 nick_securitycheck 1
1127 upvar #0 chan_initialop([irctolower $target]) io
1128 upvar #0 nick_unique([irctolower $n]) u
1129 if {![info exists io]} {
1130 usererror "$target is not a managed channel."
1132 if {"$io" != "$u"} {
1133 usererror "You are not the interim manager of $target."
1135 if {"$subcmd" != "manager"} {
1136 usererror "Please use `channel manager' first."
1144 global nick_case ownmailaddr ownfullname
1147 set target [ta_word]
1148 if {![string length $text]} { error "tell them what?" }
1150 set ltarget [irctolower $target]
1152 if {[info exists nick_case($ltarget)]} { set ctarget $nick_case($ltarget) }
1154 manyset [nickdb_get $target tellsec] sec mailtoint mailwhy
1155 manyset [nickdb_get $target tellrel] rel relint relwithin
1156 switch -exact $sec {
1158 set now [clock seconds]
1159 set inbound [msgsdb_get $ltarget inbound]
1160 lappend inbound $n $now $text
1161 msgsdb_set $ltarget inbound $inbound
1163 set outbound [msgsdb_get $n outbound]
1166 foreach {recip time count} $outbound {
1167 if {"[irctolower $recip]" == "$ltarget"} {
1172 lappend noutbound $recip $time $count
1175 lappend noutbound $ctarget $now 1
1177 msgsdb_set $n outbound $noutbound
1178 set msg "OK, I'll tell $ctarget"
1179 if {$found} { append msg " that too" }
1181 if {"$sec" != "secure"} {
1182 switch -exact $rel {
1183 unreliable { append msg "neither reliably nor securely" }
1184 remind { append msg "pretty reliably, but not securely" }
1185 pester { append msg "reliably but not securely" }
1188 switch -exact $rel {
1189 unreliable { append msg "securely but not reliably" }
1190 remind { append msg "securely and pretty reliably" }
1191 pester { append msg "reliably and securely" }
1198 set fmtmsg [exec fmt << " $text"]
1199 exec /usr/sbin/sendmail -odb -oi -t -oee -f $mailwhy \
1201 "From: $ownmailaddr ($ownfullname)
1203 Subject: IRC tell from $n
1205 $n asked me[expr {[ischan $dest] ? " on $dest" : ""}] to tell you:
1206 [exec fmt << " $text"]
1208 (This message was for your nick $ctarget; your account $mailwhy
1209 arranged for it to be forwarded to $mailtoint.)
1212 "I've mailed $ctarget, which is what they prefer." \
1216 usererror "Sorry, $ctarget does not want me to take messages."
1219 error "bad tellsec $sec"
1226 set target [ta_word]; ta_nomore
1231 set myself [expr {"$target" != "$n"}]
1233 set ltarget [irctolower $target]
1234 upvar #0 nick_case($ltarget) ctarget
1236 if {[info exists ctarget]} {
1237 upvar #0 nick_onchans($ltarget) oc
1238 upvar #0 nick_username($ltarget) nu
1239 if {[info exists oc]} { set nshow $ctarget }
1241 if {![nickdb_exists $ltarget]} {
1242 set ol "$nshow is not a registered nick."
1243 } elseif {[string length [set username [nickdb_get $target username]]]} {
1244 set ol "The nick $nshow belongs to the user $username."
1246 set ol "The nick $nshow is registered (but not to a username)."
1248 if {![info exists ctarget] || ![info exists oc]} {
1250 append ol "\nI can't see $nshow on anywhere."
1252 append ol "\nYou aren't on any channels with me."
1254 } elseif {![info exists nu]} {
1255 append ol "\n$nshow has not identified themselves."
1256 } elseif {![info exists username]} {
1257 append ol "\n$nshow has identified themselves as the user $nu."
1258 } elseif {"$nu" != "$username"} {
1259 append ol "\nHowever, $nshow is being used by the user $nu."
1261 append ol "\n$nshow has identified themselves to me."
1269 set old [nickdb_exists $n]
1270 if {$old} { nick_securitycheck 0 }
1271 set luser [irctolower $n]
1272 switch -exact [string tolower [string trim $text]] {
1274 upvar #0 nick_username($luser) nu
1275 if {![info exists nu]} {
1277 "You must identify yourself before using `register'. See `help identify', or use `register insecure'."
1279 nickdb_set $n username $nu
1280 ucmdr {} {} "makes a note of your username." {}
1284 ucmdr {} {} "forgets your nickname." {}
1287 nickdb_set $n username {}
1289 ucmdr {} "Security is now disabled for your nickname !"
1291 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."
1295 error "you mean register / register delete / register insecure"
1300 proc timeformat_desc {tf} {
1302 ks { return "Times will be displayed in seconds or kiloseconds." }
1303 hms { return "Times will be displayed in hours, minutes, etc." }
1304 default { error "invalid timeformat: $v" }
1309 proc def_setting {opt show_body set_body} {
1311 lappend settings $opt
1312 proc set_show/$opt {} "
1316 if {![string length $set_body]} return
1317 proc set_set/$opt {} "
1324 proc tellme_sec_desc {v} {
1325 manyset $v sec mailtoint
1326 switch -exact $sec {
1328 return "I'll tell you your messages whenever I see you."
1332 "I'll keep the bodies of your messages private until you identify yourself, reminding you every [showintervalsecs $mailtoint 1]."
1335 return "I shan't accept messages for you."
1338 return "I'll forward your messages by email to $mailtoint."
1341 error "bad tellsec $sec"
1346 proc tellme_rel_desc {v} {
1347 manyset $v rel every within
1348 switch -exact $rel {
1350 return "As soon as I've told you, I'll forget the message - note that this means messages can get lost !"
1356 set u ", or talk on channel within [showintervalsecs $within 1] of me having told you"
1359 error "bad tellrel $rel"
1362 return "I'll remind you every [showintervalsecs $every 1] until you say delmsg$u."
1365 def_setting timeformat {
1366 set tf [nickdb_get $n timeformat]
1367 return "$tf: [timeformat_desc $tf]"
1369 set tf [string tolower [ta_word]]
1371 set desc [timeformat_desc $tf]
1372 nickdb_set $n timeformat $tf
1376 proc marktime_desc {mt} {
1377 if {"$mt" == "off"} {
1378 return "I will not send you periodic messages."
1379 } elseif {"$mt" == "once"} {
1380 return "I will send you one informational message when I see you."
1382 return "I'll send you a message every [showintervalsecs $mt 0]."
1386 def_setting marktime {
1387 set mt [nickdb_get $n marktime]
1389 if {[string match {[0-9]*} $mt]} { append p s }
1391 append p [marktime_desc $mt]
1395 set mt [string tolower [ta_word]]
1398 if {"$mt" == "off" || "$mt" == "once"} {
1400 set mt [parse_interval $mt $marktime_min]
1402 nickdb_set $n marktime $mt
1403 lnick_marktime_start [irctolower $n] "So:" 500 0
1404 ucmdr {} [marktime_desc $mt]
1407 def_setting security {
1408 set s [nickdb_get $n username]
1409 if {[string length $s]} {
1410 return "Your nick, $n, is controlled by the user $s."
1412 return "Your nick, $n, is not secure."
1416 def_setting tellme {
1417 set secv [nickdb_get $n tellsec]
1418 set ms [tellme_sec_desc $secv]
1420 switch -exact $sec {
1422 set mr [tellme_rel_desc [nickdb_get $n tellrel]]
1430 set setting [string tolower [ta_word]]
1431 switch -exact $setting {
1432 insecure - secure - refuse {
1434 if {"$setting" == "refuse" && [llength [msgsdb_get $n inbound]]} {
1435 usererror "You must delete the messages you have, first."
1441 set u [nickdb_get $n username]
1442 if {![string length $u]} {
1443 usererror "Sorry, you must register secure to have your messages mailed (to prevent the use of this feature for spamming)."
1446 set v [list mailto [ta_word] $u]
1448 unreliable - pester - remind {
1449 manyset [nickdb_get $n tellsec] sec
1450 switch -exact $sec {
1452 error "can't change message delivery conditions when message disposition prevents messages from being left"
1457 if {"$setting" != "unreliable"} {
1458 set every [parse_interval [ta_word] 300]
1461 if {"$setting" == "remind"} {
1463 set within [parse_interval [ta_word] 5]
1467 if {$within > $every} {
1468 error "remind interval must be at least time to respond"
1475 error "invalid tellme setting $setting"
1478 nickdb_set $n tell$sr $v
1479 ucmdr [tellme_${sr}_desc $v] {}
1482 proc lnick_checktold {luser} {
1483 set ml [msgsdb_get $luser outbound]
1484 if {![llength $ml]} return
1485 set is1 [expr {[llength $ml]==3}]
1486 set m1 "FYI, I haven't yet passed on your"
1488 set now [clock seconds]
1489 while {[llength $ml]} {
1491 set ml [lreplace $ml 0 2]
1492 set td [expr {$now-$t}]
1494 set iv [showinterval $td]
1496 set if1 "message to $r, $iv."
1498 set iv [showintervalsecs $td 0]
1499 set ifo "$r, $n messages, oldest $iv"
1500 set if1 "$n messages to $r, oldest $iv."
1503 sendprivmsg $luser "$m1 $if1"
1506 lappend ol " to $ifo[expr {[llength $ml] ? ";" : "."}]"
1509 sendprivmsg $luser "$m1 messages:"
1510 msendprivmsg $luser $ol
1517 if {![nickdb_exists $n]} {
1518 ucmdr {} "You are unknown to me and so have no settings. (Use `register'.)"
1520 if {![ta_anymore]} {
1522 foreach opt $settings {
1523 lappend ol [format "%-10s %s" $opt [set_show/$opt]]
1525 ucmdr {} [join $ol "\n"]
1528 if {[catch { info body set_show/$opt }]} {
1529 error "no setting $opt"
1531 if {![ta_anymore]} {
1532 ucmdr {} "$opt: [set_show/$opt]"
1534 nick_securitycheck 0
1535 if {[catch { info body set_set/$opt }]} {
1536 error "setting $opt cannot be set with `set'"
1543 def_ucmd identpass {
1544 set username [ta_word]
1545 set passmd5 [md5sum "[ta_word]\n"]
1549 set luser [irctolower $n]
1550 upvar #0 nick_onchans($luser) onchans
1551 if {![info exists onchans] || ![llength $onchans]} {
1552 ucmdr "You must be on a channel with me to identify yourself." {}
1554 check_username $username
1555 exec userv --timeout 3 $username << "$passmd5\n" > /dev/null \
1557 upvar #0 nick_username($luser) rec_username
1558 set rec_username $username
1559 ucmdr "Pleased to see you, $username." {}
1563 set target [ta_word]
1565 check_username $target
1568 upvar #0 lastsummon($target) ls
1569 set now [clock seconds]
1570 if {[info exists ls]} {
1571 set interval [expr {$now - $ls}]
1572 if {$interval < 30} {
1574 "Please be patient; $target was summoned only [showinterval $interval]."
1577 regsub {^[^!]*!} $p {} path
1579 exec userv --timeout 3 $target irc-summon $n $path \
1580 [expr {[ischan $dest] ? "$dest" : ""}] \
1583 regsub -all "\n" $rv { / } rv
1586 if {[regexp {^problem (.*)} $rv dummy problem]} {
1587 ucmdr {} "The user `$target' $problem."
1588 } elseif {[regexp {^ok ([^ ]+) ([0-9]+)$} $rv dummy tty idlesince]} {
1589 set idletime [expr {$now - $idlesince}]
1591 ucmdr {} {} {} "invites $target ($tty[expr {
1592 $idletime > 10 ? ", idle for [showintervalsecs $idletime 0]" : ""
1594 [ischan $dest] ? "join us here" : "talk to you"
1597 error "unexpected response from userv service: $rv"
1601 proc md5sum {value} { exec md5sum << $value }
1604 global lastseen nick
1607 set nlower [irctolower $ncase]
1609 set now [clock seconds]
1610 if {"$nlower" == "[irctolower $nick]"} {
1611 usererror "I am not self-aware."
1612 } elseif {![info exists lastseen($nlower)]} {
1613 set rstr "I've never seen $ncase."
1615 manyset $lastseen($nlower) realnick time what
1616 set howlong [expr {$now - $time}]
1617 set string [showinterval $howlong]
1618 set rstr "I last saw $realnick $string, $what."
1620 if {[ischan $dest]} {
1625 upvar #0 lookedfor($nlower) lf
1626 if {[info exists lf]} { set oldvalue $lf } else { set oldvalue {} }
1627 set lf [list [list $now $n $where]]
1628 foreach v $oldvalue {
1629 if {"[irctolower [lindex $v 1]]" == "[irctolower $n]"} continue
1635 proc lnick_marktime_cancel {luser} {
1636 upvar #0 nick_markid($luser) mi
1637 if {![info exists mi]} return
1638 catch { after cancel $mi }
1642 proc lnick_marktime_doafter {luser why ms mentiontold} {
1643 lnick_marktime_cancel $luser
1644 upvar #0 nick_markid($luser) mi
1645 set mi [after $ms [list lnick_marktime_now $luser $why 0]]
1648 proc lnick_marktime_reset {luser} {
1649 set mt [nickdb_get $luser marktime]
1650 if {"$mt" == "off" || "$mt" == "once"} return
1651 lnick_marktime_doafter $luser "Time passes." [expr {$mt*1000}] 0
1654 proc lnick_marktime_start {luser why ms mentiontold} {
1655 set mt [nickdb_get $luser marktime]
1656 if {"$mt" == "off"} {
1657 lnick_marktime_cancel $luser
1658 after $ms [list lnick_checktold $luser]
1660 lnick_marktime_doafter $luser $why $ms $mentiontold
1664 proc lnick_marktime_now {luser why mentiontold} {
1665 upvar #0 nick_onchans($luser) oc
1667 set calling_nick $luser
1668 sendprivmsg $luser [lnick_pingstring $why $oc ""]
1669 if {$mentiontold} { lnick_checktold $luser }
1670 lnick_marktime_reset $luser
1673 proc lnick_pingstring {why oc apstring} {
1675 catch { exec uptime } uptime
1676 set nnicks [llength [array names nick_onchans]]
1678 {^ *([0-9:apm]+) +up.*, +(\d+) users?, +load average: +([0-9., ]+) *$} \
1679 $uptime dummy time users load]} {
1680 regsub -all , $load {} load
1681 set uptime "$time $nnicks/$users $load"
1683 append uptime ", $nnicks nicks"
1685 if {[llength $oc]} {
1689 upvar #0 chan_lastactivity($ch) la
1690 if {![info exists la]} continue
1691 if {$la <= $best_la} continue
1692 set since [showintervalsecs [expr {[clock seconds]-$la}] 1]
1693 set activity "$ch $since"
1700 append str " " $uptime " " $activity
1701 if {[string length $apstring]} { append str " " $apstring }
1707 set ln [irctolower $n]
1708 if {[ischan $dest]} {
1709 set oc [irctolower $dest]
1712 if {[info exists nick_onchans($ln)]} {
1713 set oc $nick_onchans($ln)
1717 if {[llength $oc]} { lnick_marktime_reset $ln }
1720 ucmdr {} [lnick_pingstring "Pong!" $oc $text]
1723 proc ensure_globalsecret {} {
1726 if {[info exists globalsecret]} return
1727 set gsfile [open /dev/urandom r]
1728 fconfigure $gsfile -translation binary
1729 set globalsecret [read $gsfile 32]
1730 binary scan $globalsecret H* globalsecret
1736 foreach chan [chandb_list] {
1737 if {[chandb_get $chan autojoin]} { dojoin $chan }