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
85 if {[gets $sock line] == -1} { set terminate 1; return }
86 regsub -all "\[^ -\176\240-\376\]" $line ? line
88 catch { unset calling_nick }
89 if {[regexp -nocase {^:([^ ]+) (.*)} $line dummy prefix remain]} {
91 if {[regexp {^([^!]+)!} $prefix dummy maybenick]} {
92 set calling_nick $maybenick
93 if {"[irctolower $maybenick]" == "[irctolower $nick]"} return
98 if {![string length $line]} { return }
99 if {![regexp -nocase {^([0-9a-z]+) *(.*)} $line dummy command line]} {
100 log "bad command: $org"
103 set command [string toupper $command]
105 while {[regexp {^([^ :]+) *(.*)} $line dummy thisword line]} {
106 lappend params $thisword
108 if {[regexp {^:(.*)} $line dummy thisword]} {
109 lappend params $thisword
110 } elseif {[string length $line]} {
111 log "junk at end: $org"
114 if {"$command" == "PRIVMSG" &&
115 [regexp {^[&#+!]} [lindex $params 0]] &&
116 ![regexp {^!} [lindex $params 1]]} {
117 # on-channel message, ignore
119 recordlastseen_p $prefix "talking on [lindex $params 0]" 1
123 log "[clock seconds] <- $org"
124 set procname msg_$command
125 if {[catch { info body $procname }]} { return }
127 eval [list $procname $prefix $command] $params
129 logerror "error: $emsg ($prefix $command $params)"
134 proc sendprivmsg {dest l} {
135 sendout [expr {[ischan $dest] ? "PRIVMSG" : "NOTICE"}] $dest $l
137 proc sendaction {dest what} { sendout PRIVMSG $dest "\001ACTION $what\001" }
138 proc msendprivmsg {dest ll} { foreach l $ll { sendprivmsg $dest $l } }
139 proc msendprivmsg_delayed {delay dest ll} { after $delay [list msendprivmsg $dest $ll] }
141 proc prefix_none {} {
143 if {[string length $p]} { error "prefix specified" }
146 proc msg_PING {p c s1} {
151 proc check_nick {n} {
152 if {[regexp -nocase {[^][\\`_^{|}a-z0-9-]} $n]} { error "bad char in nick" }
153 if {[regexp {^[-0-9]} $n]} { error "bad nick start" }
157 return [regexp {^[&#+!]} $dest]
160 proc irctolower {v} {
161 foreach {from to} [list "\\\[" "{" \
165 regsub -all $from $v $to v
167 return [string tolower $v]
170 proc prefix_nick {} {
174 if {![regexp {^([^!]+)!} $p dummy n]} { error "not from nick" }
176 if {"[irctolower $n]" == "[irctolower $nick]"} {
177 error "from myself" {} {}
181 proc showintervalsecs {howlong} {
182 return [showintervalsecs/[opt timeformat] $howlong]
185 proc showintervalsecs/ks {howlong} {
186 if {$howlong < 1000} {
189 if {$howlong < 1000000} {
196 set value [expr "$howlong.0 / $scale"]
197 foreach {min format} {100 %.0f 10 %.1f 1 %.2f} {
198 if {$value < $min} continue
199 return [format "$format${pfx}s" $value]
204 proc format_qty {qty unit} {
208 if {$qty != 1} { append o s }
212 proc showintervalsecs/hms {qty} {
213 set ul {second 60 minute 60 hour 24 day 7 week}
215 while {[llength $ul] > 1 && $qty >= [set uv [lindex $ul 1]]} {
216 set remainu [lindex $ul 0]
217 set remainv [expr {$qty % $uv}]
218 set qty [expr {($qty-$remainv)/$uv}]
219 set ul [lreplace $ul 0 1]
221 set o [format_qty $qty [lindex $ul 0]]
224 append o [format_qty $remainv $remainu]
229 proc showinterval {howlong} {
233 return "[showintervalsecs $howlong] ago"
237 proc showtime {when} {
238 return [showinterval [expr {[clock seconds] - $when}]]
241 proc def_msgproc {name argl body} {
242 proc msg_$name "varbase $argl" "\
243 upvar #0 msg/\$varbase/dest d\n\
244 upvar #0 msg/\$varbase/str s\n\
245 upvar #0 msg/\$varbase/accum a\n\
249 def_msgproc begin {dest str} {
255 def_msgproc append {str} {
257 if {[string length $s] && [string length $ns] > 65} {
258 msg__sendout $varbase
259 set s " [string trimleft $str]"
265 def_msgproc finish {} {
266 msg__sendout $varbase
272 def_msgproc _sendout {} {
273 lappend a [string trimright $s]
277 proc looking_whenwhere {when where} {
278 set str [showtime [expr {$when-1}]]
279 if {[string length $where]} { append str " on $where" }
283 proc recordlastseen_n {n how here} {
284 global lastseen lookedfor
285 set lastseen([irctolower $n]) [list $n [clock seconds] $how]
287 upvar #0 lookedfor([irctolower $n]) lf
288 if {[info exists lf]} {
289 switch -exact [llength $lf] {
294 manyset [lindex $lf 0] when who where
296 "FYI, $who was looking for you [looking_whenwhere $when $where]."]
299 msg_begin tosend $n "FYI, people have been looking for you:"
305 msg_append tosend " "
306 } elseif {$i == [llength $lf]} {
307 msg_append tosend " and "
310 msg_append tosend ", "
312 manyset $e when who where
314 "$who ([looking_whenwhere $when $where])$fin"
316 set ml [msg_finish tosend]
320 msendprivmsg_delayed 1000 $n $ml
324 proc recordlastseen_p {p how here} {
326 recordlastseen_n $n $how $here
329 proc chanmode_arg {} {
331 set rv [lindex $cm_args 0]
332 set cm_args [lreplace cm_args 0 0]
336 proc chanmode_o1 {m g p chan} {
339 set who [chanmode_arg]
340 recordlastseen_n $n "being nice to $who" 1
341 if {"[irctolower $who]" == "[irctolower $nick]"} {
342 sendprivmsg $n Thanks.
346 proc chanmode_o0 {m g p chan} {
349 set who [chanmode_arg]
350 recordlastseen_p $p "being mean to $who" 1
351 if {"[irctolower $who]" == "[irctolower $nick]"} {
352 set chandeop($chan) [list [clock seconds] $p]
356 proc msg_MODE {p c dest modelist args} {
357 if {![ischan $dest]} return
358 if {[regexp {^\-(.+)$} $modelist dummy modelist]} {
360 } elseif {[regexp {^\+(.+)$} $modelist dummy modelist]} {
363 error "invalid modelist"
365 foreach m [split $modelist] {
366 set procname chanmode_$m$give
367 if {[catch { info body $procname }]} {
368 recordlastseen_p $p "fiddling with $dest" 1
370 $procname $m $give $p $dest
375 proc channel_noone_seen {chan} {
377 foreach n [array names nick_onchans] {
378 upvar #0 nick_onchans($n) oc
379 set oc [grep tc {"$tc" != "$chan"} $oc]
383 proc process_kickpart {chan user} {
386 if {![ischan $chan]} { error "not a channel" }
387 if {"[irctolower $user]" == "[irctolower $nick]"} {
388 channel_noone_seen $chan
390 upvar #0 nick_onchans($user) oc
391 set lc [irctolower $chan]
392 set oc [grep tc {"$tc" != "$lc"} $oc]
393 if {![llength $oc]} { nick_forget $user }
397 proc msg_KICK {p c chans users comment} {
398 set chans [split $chans ,]
399 set users [split $users ,]
400 if {[llength $chans] > 1} {
401 foreach chan $chans user $users { process_kickpart $chan $user }
403 foreach user $users { process_kickpart [lindex $chans 0] $user }
407 proc msg_KILL {p c user why} {
411 set nick_arys {onchans username}
413 proc nick_forget {n} {
415 foreach ary $nick_arys {
416 upvar #0 nick_${ary}($n) av
424 set nick_case([irctolower $n]) $n
427 proc msg_NICK {p c newnick} {
428 global nick_arys nick_case
430 recordlastseen_n $n "changing nicks to $newnick" 0
431 recordlastseen_n $newnick "changing nicks from $n" 1
432 foreach ary $nick_arys {
433 upvar #0 nick_${ary}($n) old
434 upvar #0 nick_${ary}($newnick) new
435 if {[info exists new]} { error "nick collision ?! $ary $n $newnick" }
436 if {[info exists old]} { set new $old; unset old }
441 proc msg_JOIN {p c chan} {
443 recordlastseen_n $n "joining $chan" 1
444 upvar #0 nick_onchans($n) oc
445 lappend oc [irctolower $chan]
448 proc msg_PART {p c chan} {
450 recordlastseen_n $n "leaving $chan" 1
451 process_kickpart $chan $n
453 proc msg_QUIT {p c why} {
455 recordlastseen_n $n "leaving ($why)" 0
459 proc msg_PRIVMSG {p c dest text} {
461 if {[ischan $dest]} {
462 recordlastseen_n $n "invoking me in $dest" 1
465 recordlastseen_n $n "talking to me" 1
471 regsub {^! *} $text {} text
473 set procname ucmd/[string tolower $ucmd]
474 if {[catch { info body $procname }]} {
475 error "unknown command; try help for help"
479 sendprivmsg $n "error: $rv"
481 manyset $rv priv_msgs pub_msgs priv_acts pub_acts
482 foreach {td val} [list $n $priv_acts $output $pub_acts] {
483 foreach l [split $val "\n"] {
487 foreach {td val} [list $n $priv_msgs $output $pub_msgs] {
488 foreach l [split $val "\n"] {
495 proc msg_INVITE {p c n chan} {
496 after 1000 [list sendout JOIN $chan]
499 proc grep {var predicate list} {
503 if {[uplevel 1 [list expr $predicate]]} { lappend o $v }
508 proc msg_353 {p c dest type chan nicklist} {
509 global names_chans nick_onchans
510 if {![info exists names_chans]} { set names_chans {} }
511 set chan [irctolower $chan]
512 lappend names_chans $chan
513 channel_noone_seen $chan
514 foreach n [split $nicklist { }] {
515 regsub {^[@+]} $n {} n
516 if {![string length $n]} continue
518 upvar #0 nick_onchans($n) oc
524 proc msg_366 {p c args} {
525 global names_chans nick_onchans
526 if {[llength names_chans] > 1} {
527 foreach n [array names nick_onchans] {
528 upvar #0 nick_onchans($n) oc
529 set oc [grep tc {[lsearch -exact $tc $names_chans] >= 0} $oc]
530 if {![llength $oc]} { nick_forget $n }
538 return [expr {!![string length $text]}]
543 if {[string length $text]} { error "too many parameters" }
548 if {![regexp {^([^ ]+) *(.*)} $text dummy firstword text]} {
549 error "too few parameters"
561 proc def_ucmd {cmdname body} {
562 proc ucmd/$cmdname {p dest} " upvar 1 text text\n$body"
565 proc ucmdr {priv pub args} {
566 return -code return [concat [list $priv $pub] $args]
572 catch { unset help_topics }
573 set f [open helpinfos r]
576 while {[gets $f l] >= 0} {
578 if {[regexp {^#.*} $l]} {
579 } elseif {[regexp {^ *$} $l]} {
580 if {[info exists topic]} {
581 set help_topics($topic) [join $lines "\n"]
585 } elseif {[regexp {^!([-+._0-9a-z]*)$} $l dummy newtopic]} {
586 if {[info exists topic]} {
587 error "help $newtopic while in $topic"
591 } elseif {[regexp {^[^!#]} $l]} {
593 lappend lines [string trimright $l]
595 error "eh ? $lno: $l"
598 if {[info exists topic]} { error "unfinished topic $topic" }
605 upvar #0 help_topics([irctolower [string trim $text]]) info
606 if {![info exists info]} { ucmdr "No help on $text, sorry." {} }
612 ucmdr $help_topics() {}
615 proc manyset {list args} {
616 foreach val $list var $args {
622 proc check_username {target} {
624 [string length $target] > 8 ||
625 [regexp {[^-0-9a-z]} $target] ||
626 ![regexp {^[a-z]} $target]
627 } { error "invalid username" }
630 proc nickdb__head {} {
632 set nl [irctolower $n]
633 upvar #0 nickdb($nl) ndbe
634 binary scan $nl H* nh
636 if {![info exists ndbe] && [file exists $nfn]} {
638 try_except_finally { set newval [read $f] } {} { close $f }
639 if {[llength $newval] % 2} { error "invalid length" }
645 proc def_nickdb {name arglist body} {
646 proc nickdb_$name $arglist "nickdb__head; $body"
649 def_nickdb exists {n} {
650 return [info exists ndbe]
653 def_nickdb delete {n} {
658 set default_settings {timeformat ks}
660 def_nickdb set {n args} {
661 global default_settings
662 if {![info exists ndbe]} { set ndbe $default_settings }
663 foreach {key value} [concat $ndbe $args] { set a($key) $value }
665 foreach {key value} [array get a] { lappend newval $key $value }
666 set f [open $nfn.new w]
670 file rename -force $nfn.new $nfn
680 if {[info exists calling_nick]} { set n $calling_nick } { set n {} }
681 return [nickdb_opt $n $key]
684 def_nickdb opt {n key} {
685 global default_settings
686 if {[info exists ndbe]} {
689 set l $default_settings
691 foreach {tkey value} $l {
692 if {"$tkey" == "$key"} { return $value }
694 error "unset setting $key"
697 proc check_notonchan {} {
699 if {[ischan $dest]} { error "that command must be sent privately" }
702 proc nick_securitycheck {strict} {
704 if {![nickdb_exists $n]} { error "you are unknown to me, use `register'." }
705 set wantu [nickdb_opt $n username]
706 if {![string length $wantu]} {
708 error "that feature is only available to secure users, sorry."
713 upvar #0 nick_username($n) nu
714 if {![info exists nu]} {
715 error "nick $n is secure, you must identify yourself first."
717 if {"$wantu" != "$nu"} {
718 error "you are the wrong user - the nick $n belongs to $wantu, not $nu"
724 set target [ta_word]; ta_nomore
729 set myself [expr {"$target" != "$n"}]
731 upvar #0 nick_case([irctolower $target]) nc
733 if {[info exists nc]} {
734 upvar #0 nick_onchans($nc) oc
735 upvar #0 nick_username($nc) nu
736 if {[info exists oc]} { set nshow $nc }
738 if {![nickdb_exists $target]} {
739 set ol "$nshow is not a registered nick."
740 } elseif {[string length [set username [nickdb_opt $target username]]]} {
741 set ol "The nick $nshow belongs to the user $username."
743 set ol "The nick $nshow is registered (but not to a username)."
745 if {![info exists nc] || ![info exists oc]} {
747 append ol "\nI can't see $nshow on anywhere."
749 append ol "\nYou aren't on any channels with me."
751 } elseif {![info exists nu]} {
752 append ol "\n$nshow has not identified themselves."
753 } elseif {![info exists username]} {
754 append ol "\n$nshow has identified themselves as the user $nu."
755 } elseif {"$nu" != "$username"} {
756 append ol "\nHowever, $nshow is being used by the user $nu."
758 append ol "\n$nshow has identified themselves to me."
766 set old [nickdb_exists $n]
767 if {$old} { nick_securitycheck 0 }
768 switch -exact [string tolower [string trim $text]] {
770 upvar #0 nick_username($n) nu
771 if {![info exists nu]} {
773 "You must identify yourself before using `register'. See `help identify', or use `register insecure'."
775 nickdb_set $n username $nu
776 ucmdr {} {} "makes a note of your username." {}
780 ucmdr {} {} "forgets your nickname." {}
783 nickdb_set $n username {}
785 ucmdr {} "Security is now disabled for your nickname !"
787 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."
793 proc timeformat_desc {tf} {
795 ks { return "Times will be displayed in seconds or kiloseconds." }
796 hms { return "Times will be displayed in hours, minutes, etc." }
797 default { error "invalid timeformat: $v" }
801 proc def_setting {opt show_body set_body} {
802 proc set_show/$opt {} "
806 if {![string length $set_body]} return
807 proc set_set/$opt {} "
814 def_setting timeformat {
815 set tf [nickdb_opt $n timeformat]
816 return "$tf: [timeformat_desc $tf]"
818 set tf [string tolower [ta_word]]
820 set desc [timeformat_desc $tf]
821 nickdb_set $n timeformat $tf
825 def_setting security {
826 set s [nickdb_opt $n username]
827 if {[string length $s]} {
828 return "Your nick, $n, is controlled by the user $s."
830 return "Your nick, $n, is not secure."
837 if {![nickdb_exists $n]} {
838 ucmdr {} "You are unknown to me and so have no settings. (Use `register'.)"
842 foreach proc [lsort [info procs]] {
843 if {![regexp {^set_show/(.*)$} $proc dummy opt]} continue
844 lappend ol [format "%-10s %s" $opt [set_show/$opt]]
846 ucmdr {} [join $ol "\n"]
849 if {[catch { info body set_show/$opt }]} {
850 error "no setting $opt"
853 ucmdr {} "$opt [set_show/$opt]"
856 if {[catch { info body set_set/$opt }]} {
857 error "setting $opt cannot be set with `set'"
865 set username [ta_word]
866 set passmd5 [md5sum "[ta_word]\n"]
870 upvar #0 nick_onchans($n) onchans
871 if {![info exists onchans] || ![llength $onchans]} {
872 ucmdr "You must be on a channel with me to identify yourself." {}
874 check_username $username
875 exec userv --timeout 3 $username << "$passmd5\n" > /dev/null \
877 upvar #0 nick_username($n) rec_username
878 set rec_username $username
879 ucmdr "Pleased to see you, $username." {}
885 check_username $target
888 upvar #0 lastsummon($target) ls
889 set now [clock seconds]
890 if {[info exists ls]} {
891 set interval [expr {$now - $ls}]
892 if {$interval < 30} {
894 "Please be patient; $target was summoned only [showinterval $interval]."
897 regsub {^[^!]*!} $p {} path
899 exec userv --timeout 3 $target irc-summon $n $path \
900 [expr {[ischan $dest] ? "$dest" : ""}] \
903 regsub -all "\n" $rv { / } rv
906 if {[regexp {^problem (.*)} $rv dummy problem]} {
907 ucmdr {} "The user `$target' $problem."
908 } elseif {[regexp {^ok ([^ ]+) ([0-9]+)$} $rv dummy tty idlesince]} {
909 set idletime [expr {$now - $idlesince}]
911 ucmdr {} {} {} "invites $target ($tty[expr {
912 $idletime > 10 ? ", idle for [showintervalsecs $idletime]" : ""
914 [ischan $dest] ? "join us here" : "talk to you"
917 error "unexpected response from userv service: $rv"
921 proc md5sum {value} { exec md5sum << $value }
927 set nlower [irctolower $ncase]
929 set now [clock seconds]
930 if {"$nlower" == "[irctolower $nick]"} {
931 error "I am not self-aware."
932 } elseif {![info exists lastseen($nlower)]} {
933 set rstr "I've never seen $ncase."
935 manyset $lastseen($nlower) realnick time what
936 set howlong [expr {$now - $time}]
937 set string [showinterval $howlong]
938 set rstr "I last saw $realnick $string, $what."
940 if {[ischan $dest]} {
945 upvar #0 lookedfor($nlower) lf
946 if {[info exists lf]} { set oldvalue $lf } else { set oldvalue {} }
947 set lf [list [list $now $n $where]]
948 foreach v $oldvalue {
949 if {"[irctolower [lindex $v 1]]" == "[irctolower $n]"} continue
955 if {![info exists sock]} {
956 set sock [socket $host $port]
957 fconfigure $sock -buffering line
958 #fconfigure $sock -translation binary
959 fconfigure $sock -translation crlf
961 sendout USER blight 0 * $ownfullname
963 fileevent $sock readable onread
968 #if {![regexp {tclsh} $argv0]} {