3 proc defset {varname val} {
5 if {![info exists var]} { set var $val }
12 defset ownfullname "testing bot"
13 defset ownmailaddr test-irc-bot@example.com
15 defset musthaveping_ms 10000
17 defset out_interval 2100
18 defset out_lag_lag 5000
19 defset out_lag_very 25000
21 defset marktime_min 300
22 defset marktime_join_startdelay 5000
24 proc manyset {list args} {
25 foreach val $list var $args {
31 proc try_except_finally {try except finally} {
32 global errorInfo errorCode
33 set er [catch { uplevel 1 $try } emsg]
37 if {[catch { uplevel 1 $except } emsg3]} {
38 append ei "\nALSO ERROR HANDLING ERROR:\n$emsg3"
41 set er2 [catch { uplevel 1 $finally } emsg2]
44 append ei "\nALSO ERROR CLEANING UP:\n$emsg2"
46 return -code $er -errorinfo $ei -errorcode $ec $emsg
48 return -code $er2 -errorinfo $errorInfo -errorcode $errorCode $emsg2
54 proc usererror {emsg} { error $emsg {} {BLIGHT USER} }
58 global out_queue out_creditms out_creditat out_interval out_maxburst
59 global out_lag_lag out_lag_very
60 #set pr [lindex [info level 0] 0]
61 #puts $pr>[clock seconds]|$out_creditat|$out_creditms|[llength $out_queue]<
67 if {[llength $out_queue]*$out_interval > $out_lag_very} {
69 } elseif {[llength $out_queue]*$out_interval > $out_lag_lag} {
79 set now [clock seconds]
80 incr out_creditms [expr {($now - $out_creditat) * 1000}]
82 if {$out_creditms > $out_maxburst*$out_interval} {
83 set out_creditms [expr {$out_maxburst*$out_interval}]
88 proc out_runqueue {now} {
92 while {[llength $out_queue] && $out_creditms >= $out_interval} {
93 #puts rq>$now|$out_creditat|$out_creditms|[llength $out_queue]<
94 manyset [lindex $out_queue 0] orgwhen msg
95 set out_queue [lrange $out_queue 1 end]
96 if {[llength $out_queue]} {
97 append orgwhen "+[expr {$now - $orgwhen}]"
98 append orgwhen "([llength $out_queue])"
100 puts "$orgwhen -> $msg"
102 incr out_creditms -$out_interval
104 if {[llength $out_queue]} {
105 after $out_interval out_nextmessage
109 proc out_nextmessage {} {
111 set now [clock seconds]
112 incr out_creditms $out_interval
113 set out_creditat $now
117 proc sendout_priority {priority command args} {
118 global sock out_queue
119 if {[llength $args]} {
120 set la [lindex $args end]
121 set args [lreplace $args end end]
123 if {[regexp {[: ]} $i]} {
124 error "bad argument in output $i ($command $args)"
129 set args [lreplace $args 0 -1 $command]
130 set string [join $args { }]
131 set now [clock seconds]
132 set newe [list $now $string]
134 set out_queue [concat [list $newe] $out_queue]
136 lappend out_queue $newe
138 if {[llength $out_queue] == 1} {
143 proc sendout {command args} { eval sendout_priority [list 0 $command] $args }
149 proc logerror {data} {
154 global saveei saveec errorInfo errorCode
156 set saveei $errorInfo
157 set saveec $errorCode
159 puts ">$saveec|$saveei<"
169 global sock nick calling_nick errorInfo errorCode
171 if {[gets $sock line] == -1} { fail "EOF/error on input" }
172 regsub -all "\[^ -\176\240-\376\]" $line ? line
177 catch { unset calling_nick }
181 if {[regexp -nocase {^:([^ ]+) (.*)} $line dummy prefix remain]} {
183 if {[regexp {^([^!]+)!} $prefix dummy maybenick]} {
184 set calling_nick $maybenick
185 if {"[irctolower $maybenick]" == "[irctolower $nick]"} return
190 if {![string length $line]} { return }
191 if {![regexp -nocase {^([0-9a-z]+) *(.*)} $line dummy command line]} {
192 log "bad command: $org"
195 set command [string toupper $command]
197 while {[regexp {^([^ :]+) *(.*)} $line dummy thisword line]} {
198 lappend params $thisword
200 if {[regexp {^:(.*)} $line dummy thisword]} {
201 lappend params $thisword
202 } elseif {[string length $line]} {
203 log "junk at end: $org"
206 if {"$command" == "PRIVMSG" &&
207 [regexp {^[&#+!]} [lindex $params 0]] &&
208 ![regexp {^![a-z][-a-z]*[a-z]( .*)?$} [lindex $params 1]]} {
209 # on-channel message, ignore
210 set chan [lindex $params 0]
211 upvar #0 chan_lastactivity([irctolower $chan]) la
212 set la [clock seconds]
213 catch { recordlastseen_p $prefix "talking on $chan" 1 }
216 log "[clock seconds] <- $org"
217 set procname msg_$command
218 if {[catch { info body $procname }]} { return }
220 eval [list $procname $prefix $command] $params
222 logerror "error: $emsg ($prefix $command $params)"
227 proc sendprivmsg {dest l} {
228 foreach v [split $l "\n"] {
229 sendout [expr {[ischan $dest] ? "PRIVMSG" : "NOTICE"}] $dest $v
232 proc sendaction_priority {priority dest what} {
233 sendout_priority $priority PRIVMSG $dest "\001ACTION $what\001"
235 proc msendprivmsg {dest ll} { foreach l $ll { sendprivmsg $dest $l } }
236 proc msendprivmsg_delayed {delay dest ll} { after $delay [list msendprivmsg $dest $ll] }
238 proc prefix_none {} {
240 if {[string length $p]} { error "prefix specified" }
243 proc msg_PING {p c s1} {
244 global musthaveping_after
247 if {[info exists musthaveping_after]} connected
250 proc check_nick {n} {
251 if {[regexp -nocase {[^][\\`_^{|}a-z0-9-]} $n]} { error "bad char in nick" }
252 if {[regexp {^[-0-9]} $n]} { error "bad nick start" }
256 return [regexp {^[&#+!]} $dest]
259 proc irctolower {v} {
260 foreach {from to} [list "\\\[" "{" \
264 regsub -all $from $v $to v
266 return [string tolower $v]
269 proc prefix_nick {} {
273 if {![regexp {^([^!]+)!} $p dummy n]} { error "not from nick" }
275 if {"[irctolower $n]" == "[irctolower $nick]"} {
276 error "from myself" {} {}
280 proc showintervalsecs {howlong abbrev} {
281 return [showintervalsecs/[opt timeformat] $howlong $abbrev]
284 proc showintervalsecs/ks {howlong abbrev} {
285 if {$howlong < 1000} {
288 if {$howlong < 1000000} {
295 set value [expr "$howlong.0 / $scale"]
296 foreach {min format} {100 %.0f 10 %.1f 1 %.2f} {
297 if {$value < $min} continue
298 return [format "$format${pfx}s" $value]
303 proc format_qty {qty unit abbrev} {
306 append o [string range $unit 0 0]
310 if {$qty != 1} { append o s }
315 proc showintervalsecs/hms {qty abbrev} {
316 set ul {second 60 minute 60 hour 24 day 7 week}
318 while {[llength $ul] > 1 && $qty >= [set uv [lindex $ul 1]]} {
319 set remainu [lindex $ul 0]
320 set remainv [expr {$qty % $uv}]
321 set qty [expr {($qty-$remainv)/$uv}]
322 set ul [lreplace $ul 0 1]
324 set o [format_qty $qty [lindex $ul 0] $abbrev]
326 if {!$abbrev} { append o " " }
327 append o [format_qty $remainv $remainu $abbrev]
332 proc showinterval {howlong} {
336 return "[showintervalsecs $howlong 0] ago"
340 proc showtime {when} {
341 return [showinterval [expr {[clock seconds] - $when}]]
344 proc def_msgproc {name argl body} {
345 proc msg_$name "varbase $argl" "\
346 upvar #0 msg/\$varbase/dest d\n\
347 upvar #0 msg/\$varbase/str s\n\
348 upvar #0 msg/\$varbase/accum a\n\
352 def_msgproc begin {dest str} {
358 def_msgproc append {str} {
360 if {[string length $s] && [string length $ns] > 65} {
361 msg__sendout $varbase
362 set s " [string trimleft $str]"
368 def_msgproc finish {} {
369 msg__sendout $varbase
375 def_msgproc _sendout {} {
376 lappend a [string trimright $s]
380 proc looking_whenwhere {when where} {
381 set str [showtime [expr {$when-1}]]
382 if {[string length $where]} { append str " on $where" }
386 proc recordlastseen_n {n how here} {
387 global lastseen lookedfor
388 set lastseen([irctolower $n]) [list $n [clock seconds] $how]
390 upvar #0 lookedfor([irctolower $n]) lf
391 if {[info exists lf]} {
392 switch -exact [llength $lf] {
397 manyset [lindex $lf 0] when who where
399 "FYI, $who was looking for you [looking_whenwhere $when $where]."]
402 msg_begin tosend $n "FYI, people have been looking for you:"
408 msg_append tosend " "
409 } elseif {$i == [llength $lf]} {
410 msg_append tosend " and "
413 msg_append tosend ", "
415 manyset $e when who where
417 "$who ([looking_whenwhere $when $where])$fin"
419 set ml [msg_finish tosend]
423 msendprivmsg_delayed 1000 $n $ml
427 proc note_topic {showoff whoby topic} {
428 set msg "FYI, $whoby has changed the topic on $showoff"
429 if {[string length $topic] < 160} {
430 append msg " to $topic"
432 append msg " but it is too long to reproduce here !"
434 set showoff [irctolower $showoff]
435 set tell [chandb_get $showoff topictell]
436 if {[lsearch -exact $tell *] >= 0} {
437 set tryspies [chandb_list]
441 foreach spy $tryspies {
442 set see [chandb_get $spy topicsee]
443 if {[lsearch -exact $see $showoff] >= 0 || \
444 ([lsearch -exact $see *] >= 0 && \
445 [lsearch -exact $tell $spy] >= 0)} {
446 sendprivmsg $spy $msg
451 proc recordlastseen_p {p how here} {
453 recordlastseen_n $n $how $here
456 proc chanmode_arg {} {
458 set rv [lindex $cm_args 0]
459 set cm_args [lreplace cm_args 0 0]
463 proc chanmode_o1 {m g p chan} {
464 global nick chan_initialop
466 set who [chanmode_arg]
467 recordlastseen_n $n "being nice to $who" 1
468 if {"[irctolower $who]" == "[irctolower $nick]"} {
469 set nlower [irctolower $n]
470 upvar #0 nick_unique($nlower) u
471 if {[chandb_exists $chan]} {
472 sendprivmsg $n Thanks.
473 } elseif {![info exists u]} {
474 sendprivmsg $n {Op me while not on the channel, why don't you ?}
476 set chan_initialop([irctolower $chan]) $u
478 "Thanks. You can use `channel manager ...' to register this channel."
479 if {![nickdb_exists $n] || ![string length [nickdb_get $n username]]} {
481 "(But to do that you must register your nick securely first.)"
487 proc chanmode_o0 {m g p chan} {
490 set who [chanmode_arg]
491 recordlastseen_p $p "being mean to $who" 1
492 if {"[irctolower $who]" == "[irctolower $nick]"} {
493 set chandeop($chan) [list [clock seconds] $p]
497 proc msg_MODE {p c dest modelist args} {
498 if {![ischan $dest]} return
499 if {[regexp {^\-(.+)$} $modelist dummy modelist]} {
501 } elseif {[regexp {^\+(.+)$} $modelist dummy modelist]} {
504 error "invalid modelist"
506 foreach m [split $modelist] {
507 set procname chanmode_$m$give
508 if {[catch { info body $procname }]} {
509 recordlastseen_p $p "fiddling with $dest" 1
511 $procname $m $give $p $dest
516 proc leaving {lchan} {
517 foreach luser [array names nick_onchans] {
518 upvar #0 nick_onchans($luser) oc
519 set oc [grep tc {"$tc" != "$lchan"} $oc]
521 upvar #0 chan_nicks($lchan) nlist
523 upvar #0 chan_lastactivity($lchan) la
527 proc doleave {lchan} {
532 proc dojoin {lchan} {
535 set chan_nicks($lchan) {}
538 proc check_justme {lchan} {
540 upvar #0 chan_nicks($lchan) nlist
541 if {[llength $nlist] != 1} return
542 if {"[lindex $nlist 0]" != "[irctolower $nick]"} return
543 if {[chandb_exists $lchan]} {
544 set mode [chandb_get $lchan mode]
545 if {"$mode" != "*"} {
546 sendout MODE $lchan $mode
548 set topic [chandb_get $lchan topicset]
549 if {[string length $topic]} {
550 sendout TOPIC $lchan $topic
557 proc process_kickpart {chan user} {
560 set luser [irctolower $user]
561 set lchan [irctolower $chan]
562 if {![ischan $chan]} { error "not a channel" }
563 if {"$luser" == "[irctolower $nick]"} {
566 upvar #0 nick_onchans($luser) oc
567 upvar #0 chan_nicks($lchan) nlist
568 set oc [grep tc {"$tc" != "$lchan"} $oc]
569 set nlist [grep tn {"$tn" != "$luser"} $nlist]
571 if {![llength $oc]} {
579 proc msg_TOPIC {p c dest topic} {
581 if {![ischan $dest]} return
582 recordlastseen_n $n "changing the topic on $dest" 1
583 note_topic [irctolower $dest] $n $topic
586 proc msg_KICK {p c chans users comment} {
587 set chans [split $chans ,]
588 set users [split $users ,]
589 if {[llength $chans] > 1} {
590 foreach chan $chans user $users { process_kickpart $chan $user }
592 foreach user $users { process_kickpart [lindex $chans 0] $user }
596 proc msg_KILL {p c user why} {
601 set nick_arys {onchans username unique}
602 # nick_onchans($luser) -> [list ... $lchan ...]
603 # nick_username($luser) -> <securely known local username>
604 # nick_unique($luser) -> <counter>
605 # nick_case($luser) -> $user (valid even if no longer visible)
606 # nick_markid($luser) -> <after id for marktime>
608 # chan_nicks($lchan) -> [list ... $luser ...]
609 # chan_lastactivity($lchan) -> [clock seconds]
611 proc lnick_forget {luser} {
612 global nick_arys chan_nicks
613 lnick_marktime_cancel $luser
614 foreach ary $nick_arys {
615 upvar #0 nick_${ary}($luser) av
618 foreach lch [array names chan_nicks] {
619 upvar #0 chan_nicks($lch) nlist
620 set nlist [grep tn {"$tn" != "$luser"} $nlist]
625 proc nick_forget {user} {
626 global nick_arys chan_nicks
627 lnick_forget [irctolower $user]
631 proc nick_case {user} {
633 set nick_case([irctolower $user]) $user
636 proc msg_NICK {p c newnick} {
637 global nick_arys nick_case
639 recordlastseen_n $n "changing nicks to $newnick" 0
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
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} {
669 recordlastseen_n $n "joining $chan" 1
670 set nl [irctolower $n]
671 set lchan [irctolower $chan]
672 upvar #0 nick_onchans($nl) oc
673 upvar #0 chan_nicks($lchan) nlist
674 if {![info exists oc]} {
675 global marktime_join_startdelay
676 lnick_marktime_start $nl "Welcome." $marktime_join_startdelay
682 proc msg_PART {p c chan} {
684 recordlastseen_n $n "leaving $chan" 1
685 process_kickpart $chan $n
687 proc msg_QUIT {p c why} {
689 recordlastseen_n $n "leaving ($why)" 0
693 proc msg_PRIVMSG {p c dest text} {
697 if {[ischan $dest]} {
698 recordlastseen_n $n "invoking me in $dest" 1
701 recordlastseen_n $n "talking to me" 1
707 regsub {^! *} $text {} text
709 set procname ucmd/[string tolower $ucmd]
710 if {[catch { info body $procname }]} {
711 usererror "Unknown command; try help for help."
715 if {"$errorCode" != "BLIGHT USER"} { set rv "error: $rv" }
718 manyset $rv priv_msgs pub_msgs priv_acts pub_acts
719 foreach {td val} [list $n $priv_acts $output $pub_acts] {
720 foreach l [split $val "\n"] {
721 sendaction_priority 0 $td $l
724 foreach {td val} [list $n $priv_msgs $output $pub_msgs] {
725 foreach l [split $val "\n"] {
732 proc msg_INVITE {p c n chan} {
733 after 1000 [list dojoin [irctolower $chan]]
736 proc grep {var predicate list} {
740 if {[uplevel 1 [list expr $predicate]]} { lappend o $v }
745 proc msg_353 {p c dest type chan nicklist} {
746 global names_chans nick_onchans
747 set lchan [irctolower $chan]
748 upvar #0 chan_nicks($lchan) nlist
749 lappend names_chans $lchan
750 if {![info exists nlist]} {
751 # We don't think we're on this channel, so ignore it !
752 # Unfortunately, because we don't get a reply to PART,
753 # we have to remember ourselves whether we're on a channel,
754 # and ignore stuff if we're not, to avoid races. Feh.
758 foreach user [split $nicklist { }] {
759 regsub {^[@+]} $user {} user
760 if {![string length $user]} continue
762 set luser [irctolower $user]
763 upvar #0 nick_onchans($luser) oc
765 lappend nlist_new $luser
771 proc msg_366 {p c args} {
772 global names_chans nick_onchans
773 set lchan [irctolower $c]
774 foreach luser [array names nick_onchans] {
775 upvar #0 nick_onchans($luser) oc
776 if {[llength names_chans] > 1} {
777 set oc [grep tc {[lsearch -exact $tc $names_chans] >= 0} $oc]
779 if {![llength $oc]} { lnick_forget $n }
786 return [expr {!![string length $text]}]
791 if {[string length $text]} { error "too many parameters" }
796 if {![regexp {^([^ ]+) *(.*)} $text dummy firstword text]} {
797 error "too few parameters"
809 proc def_ucmd {cmdname body} {
810 proc ucmd/$cmdname {p dest} " upvar 1 text text\n$body"
813 proc ucmdr {priv pub args} {
814 return -code return [concat [list $priv $pub] $args]
818 global help_topics errorInfo
820 catch { unset help_topics }
821 set f [open helpinfos r]
824 while {[gets $f l] >= 0} {
826 if {[regexp {^#.*} $l]} {
827 } elseif {[regexp {^ *$} $l]} {
828 if {[info exists topic]} {
829 set help_topics($topic) [join $lines "\n"]
833 } elseif {[regexp {^\:\:} $l]} {
834 } elseif {[regexp {^\:([-+._0-9a-z]*)$} $l dummy newtopic]} {
835 if {[info exists topic]} {
836 error "help $newtopic while in $topic"
840 } elseif {[regexp {^[^:#]} $l]} {
842 regsub -all {([^\\])\!\$?} _$l {\1} l
843 regsub -all {\\(.)} $l {\1} l
845 lappend lines [string trimright $l]
847 error "eh ? $lno: $l"
850 if {[info exists topic]} { error "unfinished topic $topic" }
852 set errorInfo "in helpinfos line $lno\n$errorInfo"
861 set topic [irctolower [string trim $text]]
862 if {[string length $topic]} {
863 set ontopic " on `$topic'"
867 if {[set lag [out_lagged]]} {
868 if {[ischan $dest]} { set replyto $dest } else { set replyto $n }
870 sendaction_priority 1 $replyto \
871 "is very lagged. Please ask for help$ontopic again later."
874 sendaction_priority 1 $replyto \
875 "is lagged. Your help$ontopic will arrive shortly ..."
879 upvar #0 help_topics($topic) info
880 if {![info exists info]} { ucmdr "No help on $topic, sorry." {} }
886 ucmdr $help_topics() {}
889 proc check_username {target} {
891 [string length $target] > 8 ||
892 [regexp {[^-0-9a-z]} $target] ||
893 ![regexp {^[a-z]} $target]
894 } { error "invalid username" }
897 proc somedb__head {} {
899 set idl [irctolower $id]
900 upvar #0 ${nickchan}db($idl) ndbe
901 binary scan $idl H* idh
902 set idfn $fprefix$idh
903 if {![info exists iddbe] && [file exists $idfn]} {
905 try_except_finally { set newval [read $f] } {} { close $f }
906 if {[llength $newval] % 2} { error "invalid length" }
912 proc def_somedb {name arglist body} {
913 foreach {nickchan fprefix} {nick users/n chan chans/c} {
914 proc ${nickchan}db_$name $arglist \
915 "set nickchan $nickchan; set fprefix $fprefix; $body"
921 foreach path [glob -nocomplain -path $fprefix *] {
922 binary scan $path "A[string length $fprefix]A*" afprefix thinghex
923 if {"$afprefix" != "$fprefix"} { error "wrong prefix $path $afprefix" }
924 lappend list [binary format H* $thinghex]
929 proc def_somedb_id {name arglist body} {
930 def_somedb $name [concat id $arglist] "somedb__head; $body"
933 def_somedb_id exists {} {
934 return [info exists iddbe]
937 def_somedb_id delete {} {
938 catch { unset iddbe }
942 set default_settings_nick {timeformat ks marktime off}
943 set default_settings_chan {
952 def_somedb_id set {args} {
953 upvar #0 default_settings_$nickchan def
954 if {![info exists iddbe]} { set iddbe $def }
955 foreach {key value} [concat $iddbe $args] { set a($key) $value }
957 foreach {key value} [array get a] { lappend newval $key $value }
958 set f [open $idfn.new w]
962 file rename -force $idfn.new $idfn
970 def_somedb_id get {key} {
971 upvar #0 default_settings_$nickchan def
972 if {[info exists iddbe]} {
973 set l [concat $iddbe $def]
977 foreach {tkey value} $l {
978 if {"$tkey" == "$key"} { return $value }
980 error "unset setting $key"
985 if {[info exists calling_nick]} { set n $calling_nick } { set n {} }
986 return [nickdb_get $n $key]
989 proc check_notonchan {} {
991 if {[ischan $dest]} { usererror "That command must be sent privately." }
994 proc nick_securitycheck {strict} {
996 if {![nickdb_exists $n]} {
997 usererror "You are unknown to me, use `register'."
999 set wantu [nickdb_get $n username]
1000 if {![string length $wantu]} {
1002 usererror "That feature is only available to secure users, sorry."
1007 set luser [irctolower $n]
1008 upvar #0 nick_username($luser) nu
1009 if {![info exists nu]} {
1010 usererror "Nick $n is secure, you must identify yourself first."
1012 if {"$wantu" != "$nu"} {
1013 usererror "You are the wrong user -\
1014 the nick $n belongs to $wantu, not $nu."
1018 proc channel_ismanager {channel n} {
1019 set mgrs [chandb_get $channel managers]
1020 return [expr {[lsearch -exact [irctolower $mgrs] [irctolower $n]] >= 0}]
1023 proc channel_securitycheck {channel} {
1025 if {![channel_ismanager $channel $n]} {
1026 usererror "You are not a manager of $channel."
1028 nick_securitycheck 1
1031 proc def_chancmd {name body} {
1032 proc channel/$name {} \
1033 " upvar 1 target chan; upvar 1 n n; upvar 1 text text; $body"
1036 proc ta_listop {findnow procvalue} {
1037 # findnow and procvalue are code fragments which will be executed
1038 # in the caller's level. findnow should set ta_listop_ev to
1039 # the current list, and procvalue should treat ta_listop_ev as
1040 # a proposed value in the list and check and possibly modify
1041 # (canonicalise?) it. After ta_listop, ta_listop_ev will
1042 # be the new value of the list.
1043 upvar 1 ta_listop_ev exchg
1045 set opcode [ta_word]
1046 switch -exact _$opcode {
1050 foreach item $exchg { set array($item) 1 }
1053 error "list change opcode must be one of + - ="
1056 foreach exchg [split $text " "] {
1057 if {![string length $exchg]} continue
1058 uplevel 1 $procvalue
1059 if {"$opcode" != "-"} {
1062 catch { unset array($exchg) }
1065 set exchg [lsort [array names array]]
1068 def_chancmd manager {
1070 if {[chandb_exists $chan]} {
1071 set ta_listop_ev [chandb_get $chan managers]
1073 set ta_listop_ev [list [irctolower $n]]
1076 check_nick $ta_listop_ev
1077 set ta_listop_ev [irctolower $ta_listop_ev]
1079 if {[llength $ta_listop_ev]} {
1080 chandb_set $chan managers $ta_listop_ev
1081 ucmdr "Managers of $chan: $ta_listop_ev" {}
1084 ucmdr {} {} "forgets about managing $chan." {}
1088 def_chancmd autojoin {
1090 switch -exact [string tolower $yesno] {
1093 default { error "channel autojoin must be `yes' or `no' }
1095 chandb_set $chan autojoin $nv
1096 ucmdr [expr {$nv ? "I will join $chan when I'm restarted " : \
1097 "I won't join $chan when I'm restarted "}] {}
1100 def_chancmd userinvite {
1101 set nv [string tolower [ta_word]]
1103 pub { set txt "!invite will work for $chan, but it won't work by /msg" }
1104 here { set txt "!invite and /msg invite will work, but only for users who are already on $chan." }
1105 all { set txt "Any user will be able to invite themselves or anyone else to $chan." }
1106 none { set txt "I will not invite anyone to $chan." }
1108 error "channel userinvite must be `pub', `here', `all' or `none'
1111 chandb_set $chan userinvite $nv
1117 switch -exact $what {
1120 chandb_set $chan topicset {}
1121 ucmdr "I won't ever change the topic of $chan." {}
1124 set t [string trim $text]
1125 if {![string length $t]} {
1126 error "you must specific the topic to set"
1128 chandb_set $chan topicset $t
1129 ucmdr "Whenever I'm alone on $chan, I'll set the topic to $t." {}
1133 set ta_listop_ev [chandb_get $chan topic$what]
1135 if {"$ta_listop_ev" != "*"} {
1136 if {![ischan $ta_listop_ev]} {
1137 error "bad channel \`$ta_listop_ev' in topic $what"
1139 set ta_listop_ev [irctolower $ta_listop_ev]
1142 chandb_set $chan topic$what $ta_listop_ev
1143 ucmdr "Topic $what list for $chan: $ta_listop_ev" {}
1146 usererror "Unknown channel topic subcommand - see help channel."
1153 if {"$mode" != "*" && ![regexp {^(([-+][imnpst]+)+)$} $mode mode]} {
1154 error {channel mode must be * or match ([-+][imnpst]+)+}
1156 chandb_set $chan mode $mode
1157 if {"$mode" == "*"} {
1158 ucmdr "I won't ever change the mode of $chan." {}
1160 ucmdr "Whenever I'm alone on $chan, I'll set the mode to $mode." {}
1165 if {[chandb_exists $chan]} {
1166 set l "Settings for $chan: autojoin "
1167 append l [lindex {no yes} [chandb_get $chan autojoin]]
1168 append l ", mode " [chandb_get $chan mode]
1169 append l ", userinvite " [chandb_get $chan userinvite] "."
1170 append l "\nManagers: "
1171 append l [join [chandb_get $chan managers] " "]
1172 foreach {ts sep} {see "\n" tell " "} {
1173 set t [chandb_get $chan topic$ts]
1176 append l "Topic $ts list: $t."
1178 append l "Topic $ts list is empty."
1182 set t [chandb_get $chan topicset]
1183 if {[string length $t]} {
1184 append l "Topic to set: $t"
1186 append l "I will not change the topic."
1190 ucmdr {} "The channel $chan is not managed."
1194 proc channelmgr_monoop {} {
1199 upvar 1 target target
1204 if {[ischan $dest]} { set target $dest }
1205 if {[ta_anymore]} { set target [ta_word] }
1207 if {![info exists target]} {
1208 usererror "You must specify, or invoke me on, the relevant channel."
1210 if {![info exists chan_nicks([irctolower $target])]} {
1211 usererror "I am not on $target."
1213 if {![ischan $target]} { error "not a valid channel" }
1215 if {![chandb_exists $target]} {
1216 usererror "$target is not a managed channel."
1218 channel_securitycheck $target
1223 sendout MODE $target +o $n
1232 global chan_nicks errorCode errorInfo
1235 if {[ischan $dest]} {
1239 set target [ta_word]
1242 set ltarget [irctolower $target]
1243 if {![ischan $target]} { error "$target is not a channel" }
1244 if {![info exists chan_nicks($ltarget)]} {
1245 usererror "I am not on $target."
1247 set ui [chandb_get $ltarget userinvite]
1249 if {"$ui" == "pub" && !$onchan} {
1250 usererror "Invitations to $target must be made there with !invite."
1252 if {"$ui" != "all"} {
1253 if {[lsearch -exact $chan_nicks($ltarget) [irctolower $n]] < 0} {
1254 usererror "Invitations to $target may only be made\
1255 by a user on the channel."
1258 if {"$ui" == "none"} {
1259 usererror "Sorry, I've not been authorised\
1260 to invite people to $target."
1263 if {"$errorCode" == "BLIGHT USER" && [channel_ismanager $target $n]} {
1265 nick_securitycheck 1
1267 if {"$errorCode" == "BLIGHT USER"} {
1268 usererror "$emsg2 Therefore you can't use your\
1269 channel manager privilege. $emsg"
1271 error $error $errorInfo $errorCode
1275 error $emsg $errorInfo $errorCode
1278 if {![ta_anymore]} {
1279 usererror "You have to say who to invite."
1282 while {[ta_anymore]} {
1283 set invitee [ta_word]
1285 lappend invitees $invitee
1287 foreach invitee $invitees {
1288 sendout INVITE $invitee $ltarget
1290 set who [lindex $invitees 0]
1291 switch -exact llength $invitees {
1292 0 { error "zero invitees" }
1294 2 { append who " and [lindex $invitees 1]" }
1296 set who [join [lreplace $invitees end end] ", "]
1297 append who " and [lindex $invitees [llength $invitees]]"
1300 ucmdr {} {} {} "invites $who to $target."
1304 if {[ischan $dest]} { set target $dest }
1305 if {![ta_anymore]} {
1308 set subcmd [ta_word]
1310 if {[ischan $subcmd]} {
1312 if {![ta_anymore]} {
1315 set subcmd [ta_word]
1318 if {![info exists target]} { error "privately, you must specify a channel" }
1319 set procname channel/$subcmd
1320 if {"$subcmd" != "show"} {
1321 if {[catch { info body $procname }]} {
1322 usererror "unknown channel setting $subcmd."
1325 if {[chandb_exists $target]} {
1326 channel_securitycheck $target
1328 nick_securitycheck 1
1329 upvar #0 chan_initialop([irctolower $target]) io
1330 upvar #0 nick_unique([irctolower $n]) u
1331 if {![info exists io]} {
1332 usererror "$target is not a managed channel."
1334 if {"$io" != "$u"} {
1335 usererror "You are not the interim manager of $target."
1337 if {"$subcmd" != "manager"} {
1338 usererror "Please use `channel manager' first."
1347 set target [ta_word]; ta_nomore
1352 set myself [expr {"$target" != "$n"}]
1354 set ltarget [irctolower $target]
1355 upvar #0 nick_case($ltarget) ctarget
1357 if {[info exists ctarget]} {
1358 upvar #0 nick_onchans($ltarget) oc
1359 upvar #0 nick_username($ltarget) nu
1360 if {[info exists oc]} { set nshow $ctarget }
1362 if {![nickdb_exists $ltarget]} {
1363 set ol "$nshow is not a registered nick."
1364 } elseif {[string length [set username [nickdb_get $target username]]]} {
1365 set ol "The nick $nshow belongs to the user $username."
1367 set ol "The nick $nshow is registered (but not to a username)."
1369 if {![info exists ctarget] || ![info exists oc]} {
1371 append ol "\nI can't see $nshow on anywhere."
1373 append ol "\nYou aren't on any channels with me."
1375 } elseif {![info exists nu]} {
1376 append ol "\n$nshow has not identified themselves."
1377 } elseif {![info exists username]} {
1378 append ol "\n$nshow has identified themselves as the user $nu."
1379 } elseif {"$nu" != "$username"} {
1380 append ol "\nHowever, $nshow is being used by the user $nu."
1382 append ol "\n$nshow has identified themselves to me."
1390 set old [nickdb_exists $n]
1391 if {$old} { nick_securitycheck 0 }
1392 set luser [irctolower $n]
1393 switch -exact [string tolower [string trim $text]] {
1395 upvar #0 nick_username($luser) nu
1396 if {![info exists nu]} {
1398 "You must identify yourself before using `register'. See `help identify', or use `register insecure'."
1400 nickdb_set $n username $nu
1401 ucmdr {} {} "makes a note of your username." {}
1405 ucmdr {} {} "forgets your nickname." {}
1408 nickdb_set $n username {}
1410 ucmdr {} "Security is now disabled for your nickname !"
1412 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."
1418 proc timeformat_desc {tf} {
1420 ks { return "Times will be displayed in seconds or kiloseconds." }
1421 hms { return "Times will be displayed in hours, minutes, etc." }
1422 default { error "invalid timeformat: $v" }
1426 proc def_setting {opt show_body set_body} {
1427 proc set_show/$opt {} "
1431 if {![string length $set_body]} return
1432 proc set_set/$opt {} "
1439 def_setting timeformat {
1440 set tf [nickdb_get $n timeformat]
1441 return "$tf: [timeformat_desc $tf]"
1443 set tf [string tolower [ta_word]]
1445 set desc [timeformat_desc $tf]
1446 nickdb_set $n timeformat $tf
1450 proc marktime_desc {mt} {
1451 if {"$mt" == "off"} {
1452 return "I will not send you periodic messages."
1453 } elseif {"$mt" == "once"} {
1454 return "I will send you one informational message when I see you."
1456 return "I'll send you a message every [showintervalsecs $mt 0]."
1460 def_setting marktime {
1461 set mt [nickdb_get $n marktime]
1463 if {[string match {[0-9]*} $mt]} { append p s }
1465 append p [marktime_desc $mt]
1469 set mt [string tolower [ta_word]]
1472 if {"$mt" == "off" || "$mt" == "once"} {
1473 } elseif {[regexp {^([0-9]+)([a-z]+)$} $mt dummy value unit]} {
1474 switch -exact $unit {
1479 default { error "unknown unit of time $unit" }
1481 if {$value > 86400*21/$u} { error "marktime interval too large" }
1482 set mt [expr {$value*$u}]
1483 if {$mt < $marktime_min} { error "marktime interval too small" }
1485 error "invalid syntax for marktime"
1487 nickdb_set $n marktime $mt
1488 lnick_marktime_start [irctolower $n] "So:" 500
1489 ucmdr {} [marktime_desc $mt]
1492 def_setting security {
1493 set s [nickdb_get $n username]
1494 if {[string length $s]} {
1495 return "Your nick, $n, is controlled by the user $s."
1497 return "Your nick, $n, is not secure."
1504 if {![nickdb_exists $n]} {
1505 ucmdr {} "You are unknown to me and so have no settings. (Use `register'.)"
1507 if {![ta_anymore]} {
1509 foreach proc [lsort [info procs]] {
1510 if {![regexp {^set_show/(.*)$} $proc dummy opt]} continue
1511 lappend ol [format "%-10s %s" $opt [set_show/$opt]]
1513 ucmdr {} [join $ol "\n"]
1516 if {[catch { info body set_show/$opt }]} {
1517 error "no setting $opt"
1519 if {![ta_anymore]} {
1520 ucmdr {} "$opt [set_show/$opt]"
1522 nick_securitycheck 0
1523 if {[catch { info body set_set/$opt }]} {
1524 error "setting $opt cannot be set with `set'"
1531 def_ucmd identpass {
1532 set username [ta_word]
1533 set passmd5 [md5sum "[ta_word]\n"]
1537 set luser [irctolower $n]
1538 upvar #0 nick_onchans($luser) onchans
1539 if {![info exists onchans] || ![llength $onchans]} {
1540 ucmdr "You must be on a channel with me to identify yourself." {}
1542 check_username $username
1543 exec userv --timeout 3 $username << "$passmd5\n" > /dev/null \
1545 upvar #0 nick_username($luser) rec_username
1546 set rec_username $username
1547 ucmdr "Pleased to see you, $username." {}
1551 set target [ta_word]
1553 check_username $target
1556 upvar #0 lastsummon($target) ls
1557 set now [clock seconds]
1558 if {[info exists ls]} {
1559 set interval [expr {$now - $ls}]
1560 if {$interval < 30} {
1562 "Please be patient; $target was summoned only [showinterval $interval]."
1565 regsub {^[^!]*!} $p {} path
1567 exec userv --timeout 3 $target irc-summon $n $path \
1568 [expr {[ischan $dest] ? "$dest" : ""}] \
1571 regsub -all "\n" $rv { / } rv
1574 if {[regexp {^problem (.*)} $rv dummy problem]} {
1575 ucmdr {} "The user `$target' $problem."
1576 } elseif {[regexp {^ok ([^ ]+) ([0-9]+)$} $rv dummy tty idlesince]} {
1577 set idletime [expr {$now - $idlesince}]
1579 ucmdr {} {} {} "invites $target ($tty[expr {
1580 $idletime > 10 ? ", idle for [showintervalsecs $idletime 0]" : ""
1582 [ischan $dest] ? "join us here" : "talk to you"
1585 error "unexpected response from userv service: $rv"
1589 proc md5sum {value} { exec md5sum << $value }
1592 global lastseen nick
1595 set nlower [irctolower $ncase]
1597 set now [clock seconds]
1598 if {"$nlower" == "[irctolower $nick]"} {
1599 usererror "I am not self-aware."
1600 } elseif {![info exists lastseen($nlower)]} {
1601 set rstr "I've never seen $ncase."
1603 manyset $lastseen($nlower) realnick time what
1604 set howlong [expr {$now - $time}]
1605 set string [showinterval $howlong]
1606 set rstr "I last saw $realnick $string, $what."
1608 if {[ischan $dest]} {
1613 upvar #0 lookedfor($nlower) lf
1614 if {[info exists lf]} { set oldvalue $lf } else { set oldvalue {} }
1615 set lf [list [list $now $n $where]]
1616 foreach v $oldvalue {
1617 if {"[irctolower [lindex $v 1]]" == "[irctolower $n]"} continue
1623 proc lnick_marktime_cancel {luser} {
1624 upvar #0 nick_markid($luser) mi
1625 if {![info exists mi]} return
1626 catch { after cancel $mi }
1630 proc lnick_marktime_doafter {luser why ms} {
1631 lnick_marktime_cancel $luser
1632 upvar #0 nick_markid($luser) mi
1633 set mi [after $ms [list lnick_marktime_now $luser $why]]
1636 proc lnick_marktime_reset {luser} {
1637 set mt [nickdb_get $luser marktime]
1638 if {"$mt" == "off" || "$mt" == "once"} return
1639 lnick_marktime_doafter $luser "Time passes." [expr {$mt*1000}]
1642 proc lnick_marktime_start {luser why ms} {
1643 set mt [nickdb_get $luser marktime]
1644 if {"$mt" == "off"} {
1645 lnick_marktime_cancel $luser
1647 lnick_marktime_doafter $luser $why $ms
1651 proc lnick_marktime_now {luser why} {
1652 upvar #0 nick_onchans($luser) oc
1654 set calling_nick $luser
1655 sendprivmsg $luser [lnick_pingstring $why $oc ""]
1656 lnick_marktime_reset $luser
1659 proc lnick_pingstring {why oc apstring} {
1661 catch { exec uptime } uptime
1662 set nnicks [llength [array names nick_onchans]]
1664 {^ *([0-9:apm]+) +up.*, +(\d+) users, +load average: +([0-9., ]+) *$} \
1665 $uptime dummy time users load]} {
1666 regsub -all , $load {} load
1667 set uptime "$time $nnicks/$users $load"
1669 append uptime ", $nnicks nicks"
1671 if {[llength $oc]} {
1675 upvar #0 chan_lastactivity($ch) la
1676 if {![info exists la]} continue
1677 if {$la <= $best_la} continue
1678 set since [showintervalsecs [expr {[clock seconds]-$la}] 1]
1679 set activity "$ch $since"
1686 append str " " $uptime " " $activity
1687 if {[string length $apstring]} { append str " " $apstring }
1692 if {[ischan $dest]} {
1693 set oc [irctolower $dest]
1697 set ln [irctolower $n]
1698 if {[info exists nick_onchans($ln)]} {
1699 set oc $nick_onchans($ln)
1703 if {[llength $oc]} { lnick_marktime_reset $ln }
1705 ucmdr {} [lnick_pingstring "Pong!" $oc $text]
1708 proc ensure_globalsecret {} {
1711 if {[info exists globalsecret]} return
1712 set gsfile [open /dev/urandom r]
1713 fconfigure $gsfile -translation binary
1714 set globalsecret [read $gsfile 32]
1715 binary scan $globalsecret H* globalsecret
1720 proc ensure_outqueue {} {
1722 if {[info exists out_queue]} return
1724 set out_creditat [clock seconds]
1726 set out_lag_reported 0
1727 set out_lag_reportwhen $out_creditat
1731 logerror "failing: $msg"
1735 proc ensure_connecting {} {
1736 global sock ownfullname host port nick socketargs
1737 global musthaveping_ms musthaveping_after
1739 if {[info exists sock]} return
1740 set sock [eval socket $socketargs [list $host $port]]
1741 fconfigure $sock -buffering line
1742 fconfigure $sock -translation crlf
1744 sendout USER blight 0 * $ownfullname
1746 fileevent $sock readable onread
1748 set musthaveping_after [after $musthaveping_ms \
1749 {fail "no ping within timeout"}]
1753 global musthaveping_after
1755 after cancel $musthaveping_after
1756 unset musthaveping_after
1758 foreach chan [chandb_list] {
1759 if {[chandb_get $chan autojoin]} { dojoin $chan }