+ if {[ischan $dest]} {
+ recordlastseen_n $n "invoking me in $dest" 1
+ set output $dest
+ } else {
+ recordlastseen_n $n "talking to me" 1
+ set output $n
+ }
+
+ if {[catch {
+ regsub {^! *} $text {} text
+ set ucmd [ta_word]
+ set procname ucmd/[string tolower $ucmd]
+ if {[catch { info body $procname }]} {
+ error "unknown command; try help for help"
+ }
+ $procname $p $dest
+ } rv]} {
+ sendprivmsg $n "error: $rv"
+ } else {
+ manyset $rv priv_msgs pub_msgs priv_acts pub_acts
+ foreach {td val} [list $n $priv_msgs $output $pub_msgs] {
+ foreach l [split $val "\n"] {
+ sendprivmsg $td $l
+ }
+ }
+ foreach {td val} [list $n $priv_acts $output $pub_acts] {
+ foreach l [split $val "\n"] {
+ sendaction $td $l
+ }
+ }
+ }
+}
+
+proc ta_nomore {} {
+ upvar 1 text text
+ if {[string length $text]} { error "too many parameters" }
+}
+
+proc ta_word {} {
+ upvar 1 text text
+ if {![regexp {^([^ ]+) *(.*)} $text dummy firstword text]} {
+ error "too few parameters"
+ }
+ return $firstword
+}
+
+proc ta_nick {} {
+ upvar 1 text text
+ set v [ta_word]
+ check_nick $v
+ return $v
+}
+
+proc def_ucmd {cmdname body} {
+ proc ucmd/$cmdname {p dest} " upvar 1 text text\n$body"
+}
+
+proc ucmdr {priv pub args} {
+ return -code return [concat [list $priv $pub] $args]
+}
+
+proc ucmd_sendhelp {} {
+ ucmdr \
+{Commands currently understood:
+ help get this list of commands
+ seen <nick> ask after someone (I'll tell them you asked)
+ summon <username> invite a logged-on user onto IRC
+Send commands to be by /msg, or say them in channel with ! in front.} {}
+}
+
+def_ucmd help { ta_nomore; ucmd_sendhelp }
+
+def_ucmd ? { ta_nomore; ucmd_sendhelp }
+
+proc manyset {list args} {
+ foreach val $list var $args {
+ upvar 1 $var my
+ set my $val
+ }
+}
+
+def_ucmd summon {
+ set target [ta_word]
+ ta_nomore
+ if {
+ [string length $target] > 8 ||
+ [regexp {[^-0-9a-z]} $target] ||
+ ![regexp {^[a-z]} $target]
+ } { error "invalid username" }
+ prefix_nick
+
+ upvar #0 lastsummon($target) ls
+ set now [clock seconds]
+ if {[info exists ls]} {
+ set interval [expr {$now - $ls}]
+ if {$interval < 30} {
+ ucmdr {} \
+ "Please be patient; $target was summoned only [showinterval $interval]."
+ }
+ }
+ regsub {^[^!]*!} $p {} path
+ if {[catch {
+ exec userv --timeout 3 $target irc-summon $n $path \
+ [expr {[ischan $dest] ? "$dest" : ""}] \
+ < /dev/null
+ } rv]} {
+ regsub -all "\n" $rv { / } rv
+ error $rv
+ }
+ if {[regexp {^problem (.*)} $rv dummy problem]} {
+ ucmdr {} "The user `$target' $problem."
+ } elseif {[regexp {^ok ([^ ]+) ([0-9]+)$} $rv dummy tty idlesince]} {
+ set idletime [expr {$now - $idlesince}]
+ set ls $now
+ ucmdr {} {} {} "invites $target ($tty[expr {
+ $idletime > 10 ? ", idle for [showintervalsecs $idletime]" : ""
+ }]) to [expr {
+ [ischan $dest] ? "join us here" : "talk to you"
+ }]."
+ } else {
+ error "unexpected response from userv service: $rv"
+ }
+}
+
+proc md5sum {value} { exec md5sum << $value }
+
+def_ucmd seen {
+ global lastseen nick
+ prefix_nick
+ set ncase [ta_nick]
+ set nlower [irctolower $ncase]
+ ta_nomore
+ set now [clock seconds]
+ if {"$nlower" == "[irctolower $nick]"} {
+ error "I am not self-aware."
+ } elseif {![info exists lastseen($nlower)]} {
+ set rstr "I've never seen $ncase."