5 if {![info exists nick]} { set nick Blight }
6 if {![info exists ownfullname]} { set ownfullname "here to Help" }
7 set ownmailaddr blight@chiark.greenend.org.uk
9 if {![info exists globalsecret]} {
10 set gsfile [open /dev/urandom r]
11 fconfigure $gsfile -translation binary
12 set globalsecret [read $gsfile 32]
13 binary scan $globalsecret H* globalsecret
18 proc try_except_finally {try except finally} {
19 global errorInfo errorCode
20 set er [catch { uplevel 1 $try } emsg]
24 if {[catch { uplevel 1 $except } emsg3]} {
25 append ei "\nALSO ERROR HANDLING ERROR:\n$emsg3"
28 set er2 [catch { uplevel 1 $finally } emsg2]
31 append ei "\nALSO ERROR CLEANING UP:\n$emsg2"
33 return -code $er -errorinfo $ei -errorcode $ec $emsg
35 return -code $er2 -errorinfo $errorInfo -errorcode $errorCode $emsg2
41 proc sendout {command args} {
43 if {[llength $args]} {
44 set la [lindex $args end]
45 set args [lreplace $args end end]
47 if {[regexp {[: ]} $i]} {
48 error "bad argument in output $i ($command $args)"
53 set args [lreplace $args 0 -1 $command]
54 set string [join $args { }]
55 puts "[clock seconds] -> $string"
63 proc logerror {data} {
68 global saveei saveec errorInfo errorCode
73 puts ">$saveec|$saveei<"
83 global sock nick calling_nick errorInfo errorCode
85 if {[gets $sock line] == -1} { set terminate 1; return }
86 regsub -all "\[^ -\176\240-\376\]" $line ? line
91 catch { unset calling_nick }
95 if {[regexp -nocase {^:([^ ]+) (.*)} $line dummy prefix remain]} {
97 if {[regexp {^([^!]+)!} $prefix dummy maybenick]} {
98 set calling_nick $maybenick
99 if {"[irctolower $maybenick]" == "[irctolower $nick]"} return
104 if {![string length $line]} { return }
105 if {![regexp -nocase {^([0-9a-z]+) *(.*)} $line dummy command line]} {
106 log "bad command: $org"
109 set command [string toupper $command]
111 while {[regexp {^([^ :]+) *(.*)} $line dummy thisword line]} {
112 lappend params $thisword
114 if {[regexp {^:(.*)} $line dummy thisword]} {
115 lappend params $thisword
116 } elseif {[string length $line]} {
117 log "junk at end: $org"
120 if {"$command" == "PRIVMSG" &&
121 [regexp {^[&#+!]} [lindex $params 0]] &&
122 ![regexp {^!} [lindex $params 1]]} {
123 # on-channel message, ignore
125 recordlastseen_p $prefix "talking on [lindex $params 0]" 1
129 log "[clock seconds] <- $org"
130 set procname msg_$command
131 if {[catch { info body $procname }]} { return }
133 eval [list $procname $prefix $command] $params
135 logerror "error: $emsg ($prefix $command $params)"
140 proc sendprivmsg {dest l} {
141 sendout [expr {[ischan $dest] ? "PRIVMSG" : "NOTICE"}] $dest $l
143 proc sendaction {dest what} { sendout PRIVMSG $dest "\001ACTION $what\001" }
144 proc msendprivmsg {dest ll} { foreach l $ll { sendprivmsg $dest $l } }
145 proc msendprivmsg_delayed {delay dest ll} { after $delay [list msendprivmsg $dest $ll] }
147 proc prefix_none {} {
149 if {[string length $p]} { error "prefix specified" }
152 proc msg_PING {p c s1} {
157 proc check_nick {n} {
158 if {[regexp -nocase {[^][\\`_^{|}a-z0-9-]} $n]} { error "bad char in nick" }
159 if {[regexp {^[-0-9]} $n]} { error "bad nick start" }
163 return [regexp {^[&#+!]} $dest]
166 proc irctolower {v} {
167 foreach {from to} [list "\\\[" "{" \
171 regsub -all $from $v $to v
173 return [string tolower $v]
176 proc prefix_nick {} {
180 if {![regexp {^([^!]+)!} $p dummy n]} { error "not from nick" }
182 if {"[irctolower $n]" == "[irctolower $nick]"} {
183 error "from myself" {} {}
187 proc showintervalsecs {howlong} {
188 return [showintervalsecs/[opt timeformat] $howlong]
191 proc showintervalsecs/ks {howlong} {
192 if {$howlong < 1000} {
195 if {$howlong < 1000000} {
202 set value [expr "$howlong.0 / $scale"]
203 foreach {min format} {100 %.0f 10 %.1f 1 %.2f} {
204 if {$value < $min} continue
205 return [format "$format${pfx}s" $value]
210 proc format_qty {qty unit} {
214 if {$qty != 1} { append o s }
218 proc showintervalsecs/hms {qty} {
219 set ul {second 60 minute 60 hour 24 day 7 week}
221 while {[llength $ul] > 1 && $qty >= [set uv [lindex $ul 1]]} {
222 set remainu [lindex $ul 0]
223 set remainv [expr {$qty % $uv}]
224 set qty [expr {($qty-$remainv)/$uv}]
225 set ul [lreplace $ul 0 1]
227 set o [format_qty $qty [lindex $ul 0]]
230 append o [format_qty $remainv $remainu]
235 proc showinterval {howlong} {
239 return "[showintervalsecs $howlong] ago"
243 proc showtime {when} {
244 return [showinterval [expr {[clock seconds] - $when}]]
247 proc def_msgproc {name argl body} {
248 proc msg_$name "varbase $argl" "\
249 upvar #0 msg/\$varbase/dest d\n\
250 upvar #0 msg/\$varbase/str s\n\
251 upvar #0 msg/\$varbase/accum a\n\
255 def_msgproc begin {dest str} {
261 def_msgproc append {str} {
263 if {[string length $s] && [string length $ns] > 65} {
264 msg__sendout $varbase
265 set s " [string trimleft $str]"
271 def_msgproc finish {} {
272 msg__sendout $varbase
278 def_msgproc _sendout {} {
279 lappend a [string trimright $s]
283 proc looking_whenwhere {when where} {
284 set str [showtime [expr {$when-1}]]
285 if {[string length $where]} { append str " on $where" }
289 proc recordlastseen_n {n how here} {
290 global lastseen lookedfor
291 set lastseen([irctolower $n]) [list $n [clock seconds] $how]
293 upvar #0 lookedfor([irctolower $n]) lf
294 if {[info exists lf]} {
295 switch -exact [llength $lf] {
300 manyset [lindex $lf 0] when who where
302 "FYI, $who was looking for you [looking_whenwhere $when $where]."]
305 msg_begin tosend $n "FYI, people have been looking for you:"
311 msg_append tosend " "
312 } elseif {$i == [llength $lf]} {
313 msg_append tosend " and "
316 msg_append tosend ", "
318 manyset $e when who where
320 "$who ([looking_whenwhere $when $where])$fin"
322 set ml [msg_finish tosend]
326 msendprivmsg_delayed 1000 $n $ml
330 proc recordlastseen_p {p how here} {
332 recordlastseen_n $n $how $here
335 proc chanmode_arg {} {
337 set rv [lindex $cm_args 0]
338 set cm_args [lreplace cm_args 0 0]
342 proc chanmode_o1 {m g p chan} {
345 set who [chanmode_arg]
346 recordlastseen_n $n "being nice to $who" 1
347 if {"[irctolower $who]" == "[irctolower $nick]"} {
348 sendprivmsg $n Thanks.
352 proc chanmode_o0 {m g p chan} {
355 set who [chanmode_arg]
356 recordlastseen_p $p "being mean to $who" 1
357 if {"[irctolower $who]" == "[irctolower $nick]"} {
358 set chandeop($chan) [list [clock seconds] $p]
362 proc msg_MODE {p c dest modelist args} {
363 if {![ischan $dest]} return
364 if {[regexp {^\-(.+)$} $modelist dummy modelist]} {
366 } elseif {[regexp {^\+(.+)$} $modelist dummy modelist]} {
369 error "invalid modelist"
371 foreach m [split $modelist] {
372 set procname chanmode_$m$give
373 if {[catch { info body $procname }]} {
374 recordlastseen_p $p "fiddling with $dest" 1
376 $procname $m $give $p $dest
381 proc channel_noone_seen {chan} {
383 foreach n [array names nick_onchans] {
384 upvar #0 nick_onchans($n) oc
385 set oc [grep tc {"$tc" != "$chan"} $oc]
389 proc process_kickpart {chan user} {
392 if {![ischan $chan]} { error "not a channel" }
393 if {"[irctolower $user]" == "[irctolower $nick]"} {
394 channel_noone_seen $chan
396 upvar #0 nick_onchans($user) oc
397 set lc [irctolower $chan]
398 set oc [grep tc {"$tc" != "$lc"} $oc]
399 if {![llength $oc]} { nick_forget $user }
403 proc msg_KICK {p c chans users comment} {
404 set chans [split $chans ,]
405 set users [split $users ,]
406 if {[llength $chans] > 1} {
407 foreach chan $chans user $users { process_kickpart $chan $user }
409 foreach user $users { process_kickpart [lindex $chans 0] $user }
413 proc msg_KILL {p c user why} {
417 set nick_arys {onchans username}
419 proc nick_forget {n} {
421 foreach ary $nick_arys {
422 upvar #0 nick_${ary}($n) av
430 set nick_case([irctolower $n]) $n
433 proc msg_NICK {p c newnick} {
434 global nick_arys nick_case
436 recordlastseen_n $n "changing nicks to $newnick" 0
437 recordlastseen_n $newnick "changing nicks from $n" 1
438 foreach ary $nick_arys {
439 upvar #0 nick_${ary}($n) old
440 upvar #0 nick_${ary}($newnick) new
441 if {[info exists new]} { error "nick collision ?! $ary $n $newnick" }
442 if {[info exists old]} { set new $old; unset old }
447 proc msg_JOIN {p c chan} {
449 recordlastseen_n $n "joining $chan" 1
450 upvar #0 nick_onchans($n) oc
451 lappend oc [irctolower $chan]
454 proc msg_PART {p c chan} {
456 recordlastseen_n $n "leaving $chan" 1
457 process_kickpart $chan $n
459 proc msg_QUIT {p c why} {
461 recordlastseen_n $n "leaving ($why)" 0
465 proc msg_PRIVMSG {p c dest text} {
467 if {[ischan $dest]} {
468 recordlastseen_n $n "invoking me in $dest" 1
471 recordlastseen_n $n "talking to me" 1
477 regsub {^! *} $text {} text
479 set procname ucmd/[string tolower $ucmd]
480 if {[catch { info body $procname }]} {
481 error "unknown command; try help for help"
485 sendprivmsg $n "error: $rv"
487 manyset $rv priv_msgs pub_msgs priv_acts pub_acts
488 foreach {td val} [list $n $priv_acts $output $pub_acts] {
489 foreach l [split $val "\n"] {
493 foreach {td val} [list $n $priv_msgs $output $pub_msgs] {
494 foreach l [split $val "\n"] {
501 proc msg_INVITE {p c n chan} {
502 after 1000 [list sendout JOIN $chan]
505 proc grep {var predicate list} {
509 if {[uplevel 1 [list expr $predicate]]} { lappend o $v }
514 proc msg_353 {p c dest type chan nicklist} {
515 global names_chans nick_onchans
516 if {![info exists names_chans]} { set names_chans {} }
517 set chan [irctolower $chan]
518 lappend names_chans $chan
519 channel_noone_seen $chan
520 foreach n [split $nicklist { }] {
521 regsub {^[@+]} $n {} n
522 if {![string length $n]} continue
524 upvar #0 nick_onchans($n) oc
530 proc msg_366 {p c args} {
531 global names_chans nick_onchans
532 if {[llength names_chans] > 1} {
533 foreach n [array names nick_onchans] {
534 upvar #0 nick_onchans($n) oc
535 set oc [grep tc {[lsearch -exact $tc $names_chans] >= 0} $oc]
536 if {![llength $oc]} { nick_forget $n }
544 return [expr {!![string length $text]}]
549 if {[string length $text]} { error "too many parameters" }
554 if {![regexp {^([^ ]+) *(.*)} $text dummy firstword text]} {
555 error "too few parameters"
567 proc def_ucmd {cmdname body} {
568 proc ucmd/$cmdname {p dest} " upvar 1 text text\n$body"
571 proc ucmdr {priv pub args} {
572 return -code return [concat [list $priv $pub] $args]
578 catch { unset help_topics }
579 set f [open helpinfos r]
582 while {[gets $f l] >= 0} {
584 if {[regexp {^#.*} $l]} {
585 } elseif {[regexp {^ *$} $l]} {
586 if {[info exists topic]} {
587 set help_topics($topic) [join $lines "\n"]
591 } elseif {[regexp {^!([-+._0-9a-z]*)$} $l dummy newtopic]} {
592 if {[info exists topic]} {
593 error "help $newtopic while in $topic"
597 } elseif {[regexp {^[^!#]} $l]} {
599 lappend lines [string trimright $l]
601 error "eh ? $lno: $l"
604 if {[info exists topic]} { error "unfinished topic $topic" }
611 upvar #0 help_topics([irctolower [string trim $text]]) info
612 if {![info exists info]} { ucmdr "No help on $text, sorry." {} }
618 ucmdr $help_topics() {}
621 proc manyset {list args} {
622 foreach val $list var $args {
628 proc check_username {target} {
630 [string length $target] > 8 ||
631 [regexp {[^-0-9a-z]} $target] ||
632 ![regexp {^[a-z]} $target]
633 } { error "invalid username" }
636 proc nickdb__head {} {
638 set nl [irctolower $n]
639 upvar #0 nickdb($nl) ndbe
640 binary scan $nl H* nh
642 if {![info exists ndbe] && [file exists $nfn]} {
644 try_except_finally { set newval [read $f] } {} { close $f }
645 if {[llength $newval] % 2} { error "invalid length" }
651 proc def_nickdb {name arglist body} {
652 proc nickdb_$name $arglist "nickdb__head; $body"
655 def_nickdb exists {n} {
656 return [info exists ndbe]
659 def_nickdb delete {n} {
664 set default_settings {timeformat ks}
666 def_nickdb set {n args} {
667 global default_settings
668 if {![info exists ndbe]} { set ndbe $default_settings }
669 foreach {key value} [concat $ndbe $args] { set a($key) $value }
671 foreach {key value} [array get a] { lappend newval $key $value }
672 set f [open $nfn.new w]
676 file rename -force $nfn.new $nfn
686 if {[info exists calling_nick]} { set n $calling_nick } { set n {} }
687 return [nickdb_opt $n $key]
690 def_nickdb opt {n key} {
691 global default_settings
692 if {[info exists ndbe]} {
695 set l $default_settings
697 foreach {tkey value} $l {
698 if {"$tkey" == "$key"} { return $value }
700 error "unset setting $key"
703 proc check_notonchan {} {
705 if {[ischan $dest]} { error "that command must be sent privately" }
708 proc nick_securitycheck {strict} {
710 if {![nickdb_exists $n]} { error "you are unknown to me, use `register'." }
711 set wantu [nickdb_opt $n username]
712 if {![string length $wantu]} {
714 error "that feature is only available to secure users, sorry."
719 upvar #0 nick_username($n) nu
720 if {![info exists nu]} {
721 error "nick $n is secure, you must identify yourself first."
723 if {"$wantu" != "$nu"} {
724 error "you are the wrong user - the nick $n belongs to $wantu, not $nu"
730 set target [ta_word]; ta_nomore
735 set myself [expr {"$target" != "$n"}]
737 upvar #0 nick_case([irctolower $target]) nc
739 if {[info exists nc]} {
740 upvar #0 nick_onchans($nc) oc
741 upvar #0 nick_username($nc) nu
742 if {[info exists oc]} { set nshow $nc }
744 if {![nickdb_exists $target]} {
745 set ol "$nshow is not a registered nick."
746 } elseif {[string length [set username [nickdb_opt $target username]]]} {
747 set ol "The nick $nshow belongs to the user $username."
749 set ol "The nick $nshow is registered (but not to a username)."
751 if {![info exists nc] || ![info exists oc]} {
753 append ol "\nI can't see $nshow on anywhere."
755 append ol "\nYou aren't on any channels with me."
757 } elseif {![info exists nu]} {
758 append ol "\n$nshow has not identified themselves."
759 } elseif {![info exists username]} {
760 append ol "\n$nshow has identified themselves as the user $nu."
761 } elseif {"$nu" != "$username"} {
762 append ol "\nHowever, $nshow is being used by the user $nu."
764 append ol "\n$nshow has identified themselves to me."
772 set old [nickdb_exists $n]
773 if {$old} { nick_securitycheck 0 }
774 switch -exact [string tolower [string trim $text]] {
776 upvar #0 nick_username($n) nu
777 if {![info exists nu]} {
779 "You must identify yourself before using `register'. See `help identify', or use `register insecure'."
781 nickdb_set $n username $nu
782 ucmdr {} {} "makes a note of your username." {}
786 ucmdr {} {} "forgets your nickname." {}
789 nickdb_set $n username {}
791 ucmdr {} "Security is now disabled for your nickname !"
793 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."
799 proc timeformat_desc {tf} {
801 ks { return "Times will be displayed in seconds or kiloseconds." }
802 hms { return "Times will be displayed in hours, minutes, etc." }
803 default { error "invalid timeformat: $v" }
807 proc def_setting {opt show_body set_body} {
808 proc set_show/$opt {} "
812 if {![string length $set_body]} return
813 proc set_set/$opt {} "
820 def_setting timeformat {
821 set tf [nickdb_opt $n timeformat]
822 return "$tf: [timeformat_desc $tf]"
824 set tf [string tolower [ta_word]]
826 set desc [timeformat_desc $tf]
827 nickdb_set $n timeformat $tf
831 def_setting security {
832 set s [nickdb_opt $n username]
833 if {[string length $s]} {
834 return "Your nick, $n, is controlled by the user $s."
836 return "Your nick, $n, is not secure."
843 if {![nickdb_exists $n]} {
844 ucmdr {} "You are unknown to me and so have no settings. (Use `register'.)"
848 foreach proc [lsort [info procs]] {
849 if {![regexp {^set_show/(.*)$} $proc dummy opt]} continue
850 lappend ol [format "%-10s %s" $opt [set_show/$opt]]
852 ucmdr {} [join $ol "\n"]
855 if {[catch { info body set_show/$opt }]} {
856 error "no setting $opt"
859 ucmdr {} "$opt [set_show/$opt]"
862 if {[catch { info body set_set/$opt }]} {
863 error "setting $opt cannot be set with `set'"
871 set username [ta_word]
872 set passmd5 [md5sum "[ta_word]\n"]
876 upvar #0 nick_onchans($n) onchans
877 if {![info exists onchans] || ![llength $onchans]} {
878 ucmdr "You must be on a channel with me to identify yourself." {}
880 check_username $username
881 exec userv --timeout 3 $username << "$passmd5\n" > /dev/null \
883 upvar #0 nick_username($n) rec_username
884 set rec_username $username
885 ucmdr "Pleased to see you, $username." {}
891 check_username $target
894 upvar #0 lastsummon($target) ls
895 set now [clock seconds]
896 if {[info exists ls]} {
897 set interval [expr {$now - $ls}]
898 if {$interval < 30} {
900 "Please be patient; $target was summoned only [showinterval $interval]."
903 regsub {^[^!]*!} $p {} path
905 exec userv --timeout 3 $target irc-summon $n $path \
906 [expr {[ischan $dest] ? "$dest" : ""}] \
909 regsub -all "\n" $rv { / } rv
912 if {[regexp {^problem (.*)} $rv dummy problem]} {
913 ucmdr {} "The user `$target' $problem."
914 } elseif {[regexp {^ok ([^ ]+) ([0-9]+)$} $rv dummy tty idlesince]} {
915 set idletime [expr {$now - $idlesince}]
917 ucmdr {} {} {} "invites $target ($tty[expr {
918 $idletime > 10 ? ", idle for [showintervalsecs $idletime]" : ""
920 [ischan $dest] ? "join us here" : "talk to you"
923 error "unexpected response from userv service: $rv"
927 proc md5sum {value} { exec md5sum << $value }
933 set nlower [irctolower $ncase]
935 set now [clock seconds]
936 if {"$nlower" == "[irctolower $nick]"} {
937 error "I am not self-aware."
938 } elseif {![info exists lastseen($nlower)]} {
939 set rstr "I've never seen $ncase."
941 manyset $lastseen($nlower) realnick time what
942 set howlong [expr {$now - $time}]
943 set string [showinterval $howlong]
944 set rstr "I last saw $realnick $string, $what."
946 if {[ischan $dest]} {
951 upvar #0 lookedfor($nlower) lf
952 if {[info exists lf]} { set oldvalue $lf } else { set oldvalue {} }
953 set lf [list [list $now $n $where]]
954 foreach v $oldvalue {
955 if {"[irctolower [lindex $v 1]]" == "[irctolower $n]"} continue
961 if {![info exists sock]} {
962 set sock [socket $host $port]
963 fconfigure $sock -buffering line
964 #fconfigure $sock -translation binary
965 fconfigure $sock -translation crlf
967 sendout USER blight 0 * $ownfullname
969 fileevent $sock readable onread
974 #if {![regexp {tclsh} $argv0]} {