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<"
85 if {[gets $sock line] == -1} { set terminate 1; return }
86 regsub -all "\[^ -\176\240-\376\]" $line ? line
88 if {[regexp -nocase {^:([^ ]+) (.*)} $line dummy prefix remain]} {
90 if {[regexp {^([^!]+)!} $prefix dummy maybenick] &&
91 "[irctolower $maybenick]" == "[irctolower $nick]"} return
95 if {![string length $line]} { return }
96 if {![regexp -nocase {^([0-9a-z]+) *(.*)} $line dummy command line]} {
97 log "bad command: $org"
100 set command [string toupper $command]
102 while {[regexp {^([^ :]+) *(.*)} $line dummy thisword line]} {
103 lappend params $thisword
105 if {[regexp {^:(.*)} $line dummy thisword]} {
106 lappend params $thisword
107 } elseif {[string length $line]} {
108 log "junk at end: $org"
111 if {"$command" == "PRIVMSG" &&
112 [regexp {^[&#+!]} [lindex $params 0]] &&
113 ![regexp {^!} [lindex $params 1]]} {
114 # on-channel message, ignore
116 recordlastseen_p $prefix "talking on [lindex $params 0]" 1
120 log "[clock seconds] <- $org"
121 set procname msg_$command
122 if {[catch { info body $procname }]} { return }
124 eval [list $procname $prefix $command] $params
126 logerror "error: $emsg ($prefix $command $params)"
131 proc sendprivmsg {dest l} {
132 sendout [expr {[ischan $dest] ? "PRIVMSG" : "NOTICE"}] $dest $l
134 proc sendaction {dest what} { sendout PRIVMSG $dest "\001ACTION $what\001" }
135 proc msendprivmsg {dest ll} { foreach l $ll { sendprivmsg $dest $l } }
136 proc msendprivmsg_delayed {delay dest ll} { after $delay [list msendprivmsg $dest $ll] }
138 proc prefix_none {} {
140 if {[string length $p]} { error "prefix specified" }
143 proc msg_PING {p c s1} {
148 proc check_nick {n} {
149 if {[regexp -nocase {[^][\\`_^{|}a-z0-9-]} $n]} { error "bad char in nick" }
150 if {[regexp {^[-0-9]} $n]} { error "bad nick start" }
154 return [regexp {^[&#+!]} $dest]
157 proc irctolower {v} {
158 foreach {from to} [list "\\\[" "{" \
162 regsub -all $from $v $to v
164 return [string tolower $v]
167 proc prefix_nick {} {
171 if {![regexp {^([^!]+)!} $p dummy n]} { error "not from nick" }
173 if {"[irctolower $n]" == "[irctolower $nick]"} {
174 error "from myself" {} {}
178 proc showintervalsecs {howlong} {
179 if {$howlong < 1000} {
182 if {$howlong < 1000000} {
189 set value [expr "$howlong.0 / $scale"]
190 foreach {min format} {100 %.0f 10 %.1f 1 %.2f} {
191 if {$value < $min} continue
192 return [format "$format${pfx}s" $value]
197 proc showinterval {howlong} {
201 return "[showintervalsecs $howlong] ago"
205 proc showtime {when} {
206 return [showinterval [expr {[clock seconds] - $when}]]
209 proc def_msgproc {name argl body} {
210 proc msg_$name "varbase $argl" "\
211 upvar #0 msg/\$varbase/dest d\n\
212 upvar #0 msg/\$varbase/str s\n\
213 upvar #0 msg/\$varbase/accum a\n\
217 def_msgproc begin {dest str} {
223 def_msgproc append {str} {
225 if {[string length $s] && [string length $ns] > 65} {
226 msg__sendout $varbase
227 set s " [string trimleft $str]"
233 def_msgproc finish {} {
234 msg__sendout $varbase
240 def_msgproc _sendout {} {
241 lappend a [string trimright $s]
245 proc looking_whenwhere {when where} {
246 set str [showtime [expr {$when-1}]]
247 if {[string length $where]} { append str " on $where" }
251 proc recordlastseen_n {n how here} {
252 global lastseen lookedfor
253 set lastseen([irctolower $n]) [list $n [clock seconds] $how]
255 upvar #0 lookedfor([irctolower $n]) lf
256 if {[info exists lf]} {
257 switch -exact [llength $lf] {
262 manyset [lindex $lf 0] when who where
264 "FYI, $who was looking for you [looking_whenwhere $when $where]."]
267 msg_begin tosend $n "FYI, people have been looking for you:"
273 msg_append tosend " "
274 } elseif {$i == [llength $lf]} {
275 msg_append tosend " and "
278 msg_append tosend ", "
280 manyset $e when who where
282 "$who ([looking_whenwhere $when $where])$fin"
284 set ml [msg_finish tosend]
288 msendprivmsg_delayed 1000 $n $ml
292 proc recordlastseen_p {p how here} {
294 recordlastseen_n $n $how $here
297 proc chanmode_arg {} {
299 set rv [lindex $cm_args 0]
300 set cm_args [lreplace cm_args 0 0]
304 proc chanmode_o1 {m g p chan} {
307 set who [chanmode_arg]
308 recordlastseen_n $n "being nice to $who" 1
309 if {"[irctolower $who]" == "[irctolower $nick]"} {
310 sendprivmsg $n Thanks.
314 proc chanmode_o0 {m g p chan} {
317 set who [chanmode_arg]
318 recordlastseen_p $p "being mean to $who" 1
319 if {"[irctolower $who]" == "[irctolower $nick]"} {
320 set chandeop($chan) [list [clock seconds] $p]
324 proc msg_MODE {p c dest modelist args} {
325 if {![ischan $dest]} return
326 if {[regexp {^\-(.+)$} $modelist dummy modelist]} {
328 } elseif {[regexp {^\+(.+)$} $modelist dummy modelist]} {
331 error "invalid modelist"
333 foreach m [split $modelist] {
334 set procname chanmode_$m$give
335 if {[catch { info body $procname }]} {
336 recordlastseen_p $p "fiddling with $dest" 1
338 $procname $m $give $p $dest
343 proc process_kickpart {chan user} {
345 if {![ischan $chan]} { error "not a channel" }
347 upvar #0 nick_onchans($user) oc
348 set lc [irctolower $chan]
349 set oc [grep tc {"$tc" != "$lc"} $oc]
352 proc msg_KICK {p c chans users comment} {
353 set chans [split $chans ,]
354 set users [split $users ,]
355 if {[llength $chans] > 1} {
356 foreach chan $chans user $users { process_kickpart $chan $user }
358 foreach user $users { process_kickpart [lindex $chans 0] $user }
362 proc msg_KILL {p c user why} {
366 set nick_arys {onchans username}
368 proc nick_forget {n} {
370 foreach ary $nick_arys {
371 upvar #0 nick_${ary}($n) av
376 proc msg_NICK {p c newnick} {
379 recordlastseen_n $n "changing nicks to $newnick" 0
380 recordlastseen_n $newnick "changing nicks from $n" 1
381 foreach ary $nick_arys {
382 upvar #0 nick_${ary}($n) old
383 upvar #0 nick_${ary}($newnick) new
384 if {[info exists new]} { error "nick collision ?! $ary $n $newnick" }
385 if {[info exists old]} { set new $old; unset old }
389 proc msg_JOIN {p c chan} {
391 recordlastseen_n $n "joining $chan" 1
392 upvar #0 nick_onchans($n) oc
393 lappend oc [irctolower $chan]
395 proc msg_PART {p c chan} {
397 recordlastseen_n $n "leaving $chan" 1
398 process_kickpart $chan $n
400 proc msg_QUIT {p c why} {
402 recordlastseen_n $n "leaving ($why)" 0
406 proc msg_PRIVMSG {p c dest text} {
408 if {[ischan $dest]} {
409 recordlastseen_n $n "invoking me in $dest" 1
412 recordlastseen_n $n "talking to me" 1
417 regsub {^! *} $text {} text
419 set procname ucmd/[string tolower $ucmd]
420 if {[catch { info body $procname }]} {
421 error "unknown command; try help for help"
425 sendprivmsg $n "error: $rv"
427 manyset $rv priv_msgs pub_msgs priv_acts pub_acts
428 foreach {td val} [list $n $priv_acts $output $pub_acts] {
429 foreach l [split $val "\n"] {
433 foreach {td val} [list $n $priv_msgs $output $pub_msgs] {
434 foreach l [split $val "\n"] {
441 proc msg_INVITE {p c n chan} {
442 after 1000 [list sendout JOIN $chan]
445 proc grep {var predicate list} {
449 if {[uplevel 1 [list expr $predicate]]} { lappend o $v }
454 proc msg_353 {p c dest type chan nicklist} {
455 global names_chans nick_onchans
456 if {![info exists names_chans]} { set names_chans {} }
457 set chan [irctolower $chan]
458 lappend names_chans $chan
459 foreach n [array names nick_onchans] {
460 upvar #0 nick_onchans($n) oc
461 set oc [grep tc {"$tc" != "$chan"} $oc]
463 foreach n [split $nicklist { }] {
464 regsub {^[@+]} $n {} n
466 if {![string length $n]} continue
467 upvar #0 nick_onchans($n) oc
472 proc msg_366 {p c args} {
473 global names_chans nick_onchans
474 if {[llength names_chans] > 1} {
475 foreach n [array names nick_onchans] {
476 upvar #0 nick_onchans($n) oc
477 set oc [grep tc {[lsearch -exact $tc $names_chans] >= 0} $oc]
478 if {![llength $oc]} { nick_forget $n }
486 return [expr {!![string length $text]}]
491 if {[string length $text]} { error "too many parameters" }
496 if {![regexp {^([^ ]+) *(.*)} $text dummy firstword text]} {
497 error "too few parameters"
509 proc def_ucmd {cmdname body} {
510 proc ucmd/$cmdname {p dest} " upvar 1 text text\n$body"
513 proc ucmdr {priv pub args} {
514 return -code return [concat [list $priv $pub] $args]
520 catch { unset help_topics }
521 set f [open helpinfos r]
523 while {[gets $f l] >= 0} {
525 if {[regexp {^#.*} $l]} {
526 } elseif {[regexp {^ *$} $l]} {
527 if {[info exists topic]} {
528 set help_topics($topic) [join $lines "\n"]
532 } elseif {[regexp {^!([-+._0-9a-z]*)$} $l dummy newtopic]} {
533 if {[info exists topic]} { error "help $newtopic while in $topic" }
536 } elseif {[regexp {^[^!#]} $l]} {
538 lappend lines [string trimright $l]
540 error "eh ? $lno: $l"
543 if {[info exists topic]} { error "unfinished topic $topic" }
547 upvar #0 help_topics([irctolower [string trim $text]]) info
548 if {![info exists info]} { ucmdr "No help on $text, sorry." {} }
554 ucmdr $help_topics() {}
557 proc manyset {list args} {
558 foreach val $list var $args {
564 proc check_username {target} {
566 [string length $target] > 8 ||
567 [regexp {[^-0-9a-z]} $target] ||
568 ![regexp {^[a-z]} $target]
569 } { error "invalid username" }
572 proc nickdb__head {} {
574 set nl [irctolower $n]
575 upvar #0 nickdb($nl) ndbe
576 binary scan $nl H* nh
578 if {![info exists ndbe] && [file exists $nfn]} {
579 set f [file open $nfn r]
580 try_except_finally { set newval [read $f] } {} { close $f }
581 if {[llength $newval] % 2} { error "invalid length" }
587 proc def_nickdb {name arglist body} {
588 proc nickdb_$name $arglist "nickdb__head; $body"
591 def_nickdb exists {n} {
592 return [info exists ndbe]
595 def_nickdb delete {n} {
600 set default_settings {timeformat ks}
602 def_nickdb set {n args} {
603 global default_settings
604 if {![info exists ndbe]} { set ndbe $default_settings }
605 foreach {key value} [concat $ndbe $args] { set a($key) $value }
607 foreach {key value} [array get a] { lappend newval $key $value }
608 set f [open $nfn.new w]
612 file rename -force $nfn.new $nfn
620 def_nickdb opt {n key} {
621 global default_settings
622 if {[info exists ndbe]} {
625 set l $default_settings
627 foreach {tkey value} $l {
628 if {"$tkey" == "$key"} { return $value }
630 error "unset setting $key"
633 proc check_notonchan {} {
635 if {[ischan $dest]} { error "that command must be sent privately" }
638 proc nick_securitycheck {strict} {
640 if {![nickdb_exists $n]} { error "you are unknown to me, use `register'." }
641 set wantu [nickdb_opt $n username]
642 if {![string length $wantu]} {
644 error "that feature is only available to secure users, sorry."
649 upvar #0 nick_username($n) nu
650 if {![info exists nu]} {
651 error "nick $n is secure, you must identify yourself first."
653 if {"$wantu" != "$nu"} {
654 error "you are the wrong user - the nick $n belongs to $wantu, not $nu"
661 set old [nickdb_exists $n]
662 if {$old} { nick_securitycheck 0 }
663 switch -exact [string tolower [string trim $text]] {
665 upvar #0 nick_username($n) nu
666 if {![info exists nu]} {
668 "You must identify yourself before using `register'. See `help identify'."
670 nickdb_set $n username $nu
671 ucmdr {} {} "makes a note of your username." {}
675 ucmdr {} {} "forgets your nickname." {}
678 nickdb_set $n username {}
680 ucmdr {} "Security is now disabled for your nickname !"
682 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."
688 proc timeformat_desc {tf} {
690 ks { return "Times will be displayed in kiloseconds or seconds." }
691 hms { return "Times will be displayed in hours, minutes, etc." }
692 default { error "invalid timeformat: $v" }
696 proc def_setting {opt show_body set_body} {
697 proc set_show/$opt {} "
701 if {![string length $set_body]} return
702 proc set_set/$opt {} "
709 def_setting timeformat {
710 set tf [nickdb_opt $n timeformat]
711 return "$tf: [timeformat_desc $tf]"
713 set tf [string tolower [ta_word]]
715 set desc [timeformat_desc $tf]
716 nickdb_set $n timeformat $tf
720 def_setting security {
721 set s [nickdb_opt $n username]
722 if {[string length $s]} {
723 return "Your nick, $n, is controlled by the user $s."
725 return "Your nick, $n, is not secure."
732 if {![nickdb_exists $n]} {
733 ucmdr {} "You are unknown to me and so have no settings."
737 foreach proc [lsort [info procs]] {
738 if {![regexp {^set_show/(.*)$} $proc dummy opt]} continue
739 lappend ol [format "%-10s %s" $opt [set_show/$opt]]
741 ucmdr {} [join $ol "\n"]
744 if {[catch { info body set_show/$opt }]} {
745 error "no setting $opt"
748 ucmdr {} "$opt [set_show/$opt]"
750 if {[catch { info body set_set/$opt }]} {
751 error "setting $opt cannot be set with `set'"
759 set username [ta_word]
760 set passmd5 [md5sum [ta_word]]
764 upvar #0 nick_onchans($n) onchans
765 if {![info exists onchans] || ![llength $onchans]} {
766 ucmdr "You must be on a channel with me to identify yourself." {}
768 check_username $username
769 exec userv --timeout 3 $username << "$passmd5\n" > /dev/null \
771 upvar #0 nick_username($n) rec_username
772 set rec_username $username
773 ucmdr "Pleased to see you, $username." {}
779 check_username $target
782 upvar #0 lastsummon($target) ls
783 set now [clock seconds]
784 if {[info exists ls]} {
785 set interval [expr {$now - $ls}]
786 if {$interval < 30} {
788 "Please be patient; $target was summoned only [showinterval $interval]."
791 regsub {^[^!]*!} $p {} path
793 exec userv --timeout 3 $target irc-summon $n $path \
794 [expr {[ischan $dest] ? "$dest" : ""}] \
797 regsub -all "\n" $rv { / } rv
800 if {[regexp {^problem (.*)} $rv dummy problem]} {
801 ucmdr {} "The user `$target' $problem."
802 } elseif {[regexp {^ok ([^ ]+) ([0-9]+)$} $rv dummy tty idlesince]} {
803 set idletime [expr {$now - $idlesince}]
805 ucmdr {} {} {} "invites $target ($tty[expr {
806 $idletime > 10 ? ", idle for [showintervalsecs $idletime]" : ""
808 [ischan $dest] ? "join us here" : "talk to you"
811 error "unexpected response from userv service: $rv"
815 proc md5sum {value} { exec md5sum << $value }
821 set nlower [irctolower $ncase]
823 set now [clock seconds]
824 if {"$nlower" == "[irctolower $nick]"} {
825 error "I am not self-aware."
826 } elseif {![info exists lastseen($nlower)]} {
827 set rstr "I've never seen $ncase."
829 manyset $lastseen($nlower) realnick time what
830 set howlong [expr {$now - $time}]
831 set string [showinterval $howlong]
832 set rstr "I last saw $realnick $string, $what."
834 if {[ischan $dest]} {
839 upvar #0 lookedfor($nlower) lf
840 if {[info exists lf]} { set oldvalue $lf } else { set oldvalue {} }
841 set lf [list [list $now $n $where]]
842 foreach v $oldvalue {
843 if {"[irctolower [lindex $v 1]]" == "[irctolower $n]"} continue
849 if {![info exists sock]} {
850 set sock [socket $host $port]
851 fconfigure $sock -buffering line
852 #fconfigure $sock -translation binary
853 fconfigure $sock -translation crlf
855 sendout USER blight 0 * $ownfullname
857 fileevent $sock readable onread
862 #if {![regexp {tclsh} $argv0]} {