chiark / gitweb /
Do not trash errorcode &c with unset calling_nick
[ircbot] / bot.tcl
diff --git a/bot.tcl b/bot.tcl
index dff9ab30618223234ac6fd18ac3feec6186a341c..09c4ca2caf446eaaeb8bda422ee99fce636ff0c8 100755 (executable)
--- a/bot.tcl
+++ b/bot.tcl
@@ -80,19 +80,26 @@ proc bgerror {msg} {
 }
 
 proc onread {args} {
-    global sock nick calling_nick
+    global sock nick calling_nick errorInfo errorCode
     
     if {[gets $sock line] == -1} { set terminate 1; return }
     regsub -all "\[^ -\176\240-\376\]" $line ? line
     set org $line
+    
+    set ei $errorInfo
+    set ec $errorCode
+    catch { unset calling_nick }
+    set errorInfo $ei
+    set errorCode $ec
+    
     if {[regexp -nocase {^:([^ ]+) (.*)} $line dummy prefix remain]} {
        set line $remain
-       if {[regexp {^([^!]+)!} $prefix dummy maybenick] &&
-           "[irctolower $maybenick]" == "[irctolower $nick]"} return
-       set calling_nick $maybenick
+       if {[regexp {^([^!]+)!} $prefix dummy maybenick]} {
+           set calling_nick $maybenick
+           if {"[irctolower $maybenick]" == "[irctolower $nick]"} return
+       }
     } else {
        set prefix {}
-       catch { unset calling_nick }
     }
     if {![string length $line]} { return }
     if {![regexp -nocase {^([0-9a-z]+) *(.*)} $line dummy command line]} {
@@ -371,14 +378,26 @@ proc msg_MODE {p c dest modelist args} {
     }
 }
 
+proc channel_noone_seen {chan} {
+    global nick_onchans
+    foreach n [array names nick_onchans] {
+       upvar #0 nick_onchans($n) oc
+       set oc [grep tc {"$tc" != "$chan"} $oc]
+    }
+}
+
 proc process_kickpart {chan user} {
+    global nick
     check_nick $user
     if {![ischan $chan]} { error "not a channel" }
-
+    if {"[irctolower $user]" == "[irctolower $nick]"} {
+       channel_noone_seen $chan
+    }
     upvar #0 nick_onchans($user) oc
     set lc [irctolower $chan]
     set oc [grep tc {"$tc" != "$lc"} $oc]
     if {![llength $oc]} { nick_forget $user }
+    nick_case $user
 }    
 
 proc msg_KICK {p c chans users comment} {
@@ -403,10 +422,16 @@ proc nick_forget {n} {
        upvar #0 nick_${ary}($n) av
        catch { unset av }
     }
+    nick_case $n
+}
+
+proc nick_case {n} {
+    global nick_case
+    set nick_case([irctolower $n]) $n
 }
 
 proc msg_NICK {p c newnick} {
-    global nick_arys
+    global nick_arys nick_case
     prefix_nick
     recordlastseen_n $n "changing nicks to $newnick" 0
     recordlastseen_n $newnick "changing nicks from $n" 1
@@ -416,6 +441,7 @@ proc msg_NICK {p c newnick} {
        if {[info exists new]} { error "nick collision ?! $ary $n $newnick" }
        if {[info exists old]} { set new $old; unset old }
     }
+    nick_case $newnick
 }
 
 proc msg_JOIN {p c chan} {
@@ -423,6 +449,7 @@ proc msg_JOIN {p c chan} {
     recordlastseen_n $n "joining $chan" 1
     upvar #0 nick_onchans($n) oc
     lappend oc [irctolower $chan]
+    nick_case $n
 }
 proc msg_PART {p c chan} {
     prefix_nick
@@ -444,6 +471,7 @@ proc msg_PRIVMSG {p c dest text} {
        recordlastseen_n $n "talking to me" 1
        set output $n
     }
+    nick_case $n
 
     if {[catch {
        regsub {^! *} $text {} text
@@ -488,16 +516,14 @@ proc msg_353 {p c dest type chan nicklist} {
     if {![info exists names_chans]} { set names_chans {} }
     set chan [irctolower $chan]
     lappend names_chans $chan
-    foreach n [array names nick_onchans] {
-       upvar #0 nick_onchans($n) oc
-       set oc [grep tc {"$tc" != "$chan"} $oc]
-    }
+    channel_noone_seen $chan
     foreach n [split $nicklist { }] {
        regsub {^[@+]} $n {} n
-       check_nick $n
        if {![string length $n]} continue
+       check_nick $n
        upvar #0 nick_onchans($n) oc
        lappend oc $chan
+       nick_case $n
     }
 }
 
@@ -699,6 +725,47 @@ proc nick_securitycheck {strict} {
     }
 }
 
+def_ucmd who {
+    if {[ta_anymore]} {
+       set target [ta_word]; ta_nomore
+       set myself 1
+    } else {
+       prefix_nick
+       set target $n
+       set myself [expr {"$target" != "$n"}]
+    }
+    upvar #0 nick_case([irctolower $target]) nc
+    set nshow $target
+    if {[info exists nc]} {
+       upvar #0 nick_onchans($nc) oc
+       upvar #0 nick_username($nc) nu
+       if {[info exists oc]} { set nshow $nc }
+    }
+    if {![nickdb_exists $target]} {
+       set ol "$nshow is not a registered nick."
+    } elseif {[string length [set username [nickdb_opt $target username]]]} {
+       set ol "The nick $nshow belongs to the user $username."
+    } else {
+       set ol "The nick $nshow is registered (but not to a username)."
+    }
+    if {![info exists nc] || ![info exists oc]} {
+       if {$myself} {
+           append ol "\nI can't see $nshow on anywhere."
+       } else {
+           append ol "\nYou aren't on any channels with me."
+       }
+    } elseif {![info exists nu]} {
+       append ol "\n$nshow has not identified themselves."
+    } elseif {![info exists username]} {
+       append ol "\n$nshow has identified themselves as the user $nu."
+    } elseif {"$nu" != "$username"} {
+       append ol "\nHowever, $nshow is being used by the user $nu."
+    } else {
+       append ol "\n$nshow has identified themselves to me."
+    }
+    ucmdr {} $ol
+}
+
 def_ucmd register {
     prefix_nick
     check_notonchan
@@ -709,7 +776,7 @@ def_ucmd register {
            upvar #0 nick_username($n) nu
            if {![info exists nu]} {
                ucmdr {} \
- "You must identify yourself before using `register'.  See `help identify'."
+ "You must identify yourself before using `register'.  See `help identify', or use `register insecure'."
            }
            nickdb_set $n username $nu
            ucmdr {} {} "makes a note of your username." {}