#by wiebe @ quakenet

#add dcc cmd



#bind pub fvlomn|fvlomn !showwho showwho:pub

proc showwho:pub { nick uhost handle chan text } {
  global showwho
  if { ![validchan $chan] } { return 0 }
  set chan [string tolower $chan]
  set n [string tolower [lindex [split [lindex [split $text] 0] ,] 0]]
  set r ^(\[a-z\]|\[A-Z\]|\\^|\\\[|\\]|\\{|\\}|\\|`|_)(\[a-z\]|\[A-Z\]|\[0-9\]|-|\\^|\\\[|\\]|\\{|\\}|\\||`|_)\{0,[expr $::nicklen -1]\}\$
  if { [info exists showwho($chan)] } {
    lappend output "A request is already in progress."
  } elseif { $n == "" } {
    lappend output "Usage: who <nick>"
  } elseif { ![regexp $r $n] } {
    lappend output "$n is an invalid nick."
  } else {
    set showwho($chan) $n
    puthelp "WHO $n n%tcuihsnfdlar,456"
    lappend output "Retrieving who info for $n"
  }
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  return 1
}



#raw
bind raw -|- "354" showwho:raw
bind raw -|- "315" showwho:raw

proc showwho:raw { server numeric text } {
  global showwho
  set msg ""

#354 <target> [querytype] [channel] [user] [ip] [host] [server] [nick] [flags] [distance] [idle-time] [account] [:realname..]
  if { $numeric == "354" } {
    set q [lindex [split $text] 1]
    if { $q != "456" } { return 0 }
    set c [lindex [split $text] 2]
    set u [lindex [split $text] 3]
    set i [lindex [split $text] 4]
    set h [lindex [split $text] 5]
    set s [lindex [split $text] 6]
    set n [lindex [split $text] 7]
    if { [isbotnick $n] } { set i 127.0.0.1 }
    set nl [showwho:lower $n]
    set f [lindex [split $text] 8]
    set d [lindex [split $text] 9]
    set l [lindex [split $text] 10]
    set a [lindex [split $text] 11]
    set r [string trimleft [join [lrange [split $text] 12 end]] :]
    foreach chan [channels] {
      set chan [string tolower $chan]
      if { [info exists showwho($chan)] && [botonchan $chan] && [showwho:lower $showwho($chan)] == $nl } {
        set msg "WHO: nick: $n    channel: $c    username: $u    ip: $i    host: $h    server: $s    flags: $f    distance: $d    idle: $l    account: $a    realname: $r"
        if { [string match *c* [lindex [split [getchanmode $chan]] 0]] } { set msg [stripcodes bcru $msg] }
        puthelp "PRIVMSG $chan :$msg"
        unset showwho($chan)
      }
    }

#315 me mask :End of /WHO list.
  } elseif { $numeric == "315" } {
    set n [lindex [split $text] 1]
    set nl [showwho:lower $n]
    foreach chan [channels] {
      set chan [string tolower $chan]
      if { [info exists showwho($chan)] && [botonchan $chan] && [showwho:lower [lindex [split $showwho($chan)] 0]] == $nl } {
        set msg "WHO: nick $n does not exist."
        if { [string match *c* [lindex [split [getchanmode $chan]] 0]] } { set msg [stripcodes bcru $msg] }
        puthelp "PRIVMSG $chan :$msg"
        unset showwho($chan)
      }
    }
  }
  return 0
}



#connect
bind evnt -|- init-server showwho:connect

proc showwho:connect { type } {
  global showwho
  if { [info exists showwho] } {
    unset showwho
  }
}



#join
bind join -|- * showwho:join

proc showwho:join { nick uhost handle chan } {
  global showwho
  if { [isbotnick $nick] && [info exists showwho($chan)] } {
    unset showwho($chan)
  }
}



#rfc1459 compare
proc showwho:lower { text } {
  set text [string map [list "\{" "\[" "search" "replace" "search" "replace"] $text]
  set text [string map [list "\}" "\]" "search" "replace" "search" "replace"] $text]
  set text [string map [list "\~" "\^" "search" "replace" "search" "replace"] $text]
  set text [string map [list "\\" "\|" "search" "replace" "search" "replace"] $text]
  set text [string tolower $text]
  return $text
}



