# !who.tcl 1.1 by MC_8@undernet <mc8@home.com>
#  made for AZ0R0S@undernet
# ###
# Script will do a public 'who' lookup on someone.
# This script requires moretools.tcl version 1.0.1 or greater.
# ###
# Set this to what flag you want ppl to be able to access this command
# set it to - for everyone (anyone can access it with -)
set mc_az(flags) o|o

# Set this to the pub trigger you want it to be at, defaulf it !who ie !who 209.223.39.7
set mc_az(trigger) !who

# Set this to the results limit, let's say someone does !who *.*
# well, we don't want it to flood the bot tring to show ALL those results....
set mc_az(limit) 2

# [0=MSG to channel/1=MSG to person/2=NOTICE to person], default 0
# how do you want the output to be sent?
set mc_az(meth) 0

#### Script starts here ####
if {([info exists moretools]) && ($moretools >= 010001)} {
 set mc_az(version) v1.1
 bind pub $mc_az(flags) $mc_az(trigger) mc:az:trigger 
 set mc_az(going) 0
 proc mc:az:trigger {nick uhost hand chan args} {
  global mc_az
  if {$mc_az(going)} {
   puthelp "NOTICE $nick :I am currently doing a request, try again later..."
   return 0
  }
  set mc_az(args) [lindex $args 0] ; bind raw - 315 mc:az:who ; bind raw - 352 mc:az:who
  set mc_az(going) 1 ; set mc_az(chan) $chan ; set mc_az(nick) $nick
  switch -- $mc_az(meth) {
   1 {puthelp "PRIVMSG $nick :Looking up $mc_az(args)..."}
   2 {puthelp "NOTICE $nick :Looking up $mc_az(args)..."}
   default {puthelp "PRIVMSG $chan :Looking up $mc_az(args)..."}
  }
  puthelp "WHO $mc_az(args)"
 }
 set lookuprun 0
 set mc_az(limit_buffer) 0
 proc mc:az:who {server num info} { 
  global mc_az lookuprun
  if {$num == 352} {
   if {$mc_az(limit_buffer) == $mc_az(limit)} {
    switch -- $mc_az(meth) {
     1 {
      puthelp "PRIVMSG $mc_az(nick) :End of /WHO list."
      puthelp "PRIVMSG $mc_az(nick) :Too many lines in the output, restrict your query"
     }
     2 {
      puthelp "NOTICE $mc_az(nick) :End of /WHO list."
      puthelp "NOTICE $mc_az(nick) :Too many lines in the output, restrict your query"
     }
     default {
      puthelp "PRIVMSG $mc_az(chan) :End of /WHO list."
      puthelp "PRIVMSG $mc_az(chan) :Too many lines in the output, restrict your query"
     }
    }
    set mc_az(limit_buffer) 0 ; unset mc_az(chan) ; set mc_az(going) 0 ; set lookuprun 0
    unbind raw - 315 mc:az:who ; unbind raw - 352 mc:az:who ; return 0
   }
   if {[info exists mc_az(status)]} {unset mc_az(status)} ; set info [filt $info]
   switch -- $mc_az(meth) {
    1 {
     puthelp "PRIVMSG $mc_az(nick) : "
     puthelp "PRIVMSG $mc_az(nick) :Nick!ident@host: [lindex $info 5]![lindex $info 2]@[lindex $info 3]"
    }
    2 {
     puthelp "NOTICE $mc_az(nick) : "
     puthelp "NOTICE $mc_az(nick) :Nick!ident@host: [lindex $info 5]![lindex $info 2]@[lindex $info 3]"
    }
    default {
     puthelp "PRIVMSG $mc_az(chan) : "
     puthelp "PRIVMSG $mc_az(chan) :Nick!ident@host: [lindex $info 5]![lindex $info 2]@[lindex $info 3]"
    }
   }
   if {[string index [lindex $info 6] 0] == "H"} {set mc_az(status) "-Not Away-"}
   if {[string index [lindex $info 6] 0] == "G"} {set mc_az(status) "-Away-"}
   if {[string index [lindex $info 6] 1] == "*"} {
    if {[info exists mc_az(status)]} {set mc_az(status) "$mc_az(status) -IRCop`r-"} {
     set mc_az(status) "-IRCop`r-"
    }
   }
   if {![info exists mc_az(status)]} {set mc_az(status) "-=none=-"}
   switch -- $mc_az(meth) {
    1 {puthelp "PRIVMSG $mc_az(nick) :-Status:         $mc_az(status)"}
    2 {puthelp "NOTICE $mc_az(nick) :-Status:         $mc_az(status)"}
    default {puthelp "PRIVMSG $mc_az(chan) :-Status:         $mc_az(status)"}
   }
   incr mc_az(limit_buffer) 1
  } {
   if {($mc_az(limit_buffer) == 0) && (!$lookuprun)} {
    set lookuprun 1
    if {[dnslookup $mc_az(args)] == 0} {
     switch -- $mc_az(meth) {
      1 {puthelp "PRIVMSG $mc_az(nick) :No matches found, $mc_az(args) unresolvable."}
      2 {puthelp "NOTICE $mc_az(nick) :No matches found, $mc_az(args) unresolvable."}
      default {puthelp "PRIVMSG $mc_az(chan) :No matches found, $mc_az(args) unresolvable."}
     }
    } {
     switch -- $mc_az(meth) {
      1 {puthelp "PRIVMSG $mc_az(nick) :No match found, switching to resolved host -=\[$mc_az(args) <-=-> [dnslookup $mc_az(args)]\]=-"}
      2 {puthelp "NOTICE $mc_az(nick) :No match found, switching to resolved host -=\[$mc_az(args) <-=-> [dnslookup $mc_az(args)]\]=-"}
      default {puthelp "PRIVMSG $mc_az(chan) :No match found, switching to resolved host -=\[$mc_az(args) <-=-> [dnslookup $mc_az(args)]\]=-"}
     }
     puthelp "WHO [dnslookup $mc_az(args)]" ; return 2
    }
   }
   switch -- $mc_az(meth) {
    1 {puthelp "PRIVMSG $mc_az(nick) :End of /WHO list."}
    2 {puthelp "NOTICE $mc_az(nick) :End of /WHO list."}
    default {puthelp "PRIVMSG $mc_az(chan) :End of /WHO list."}
   }
   set mc_az(limit_buffer) 0
   unset mc_az(chan) ; set mc_az(going) 0 ; set lookuprun 0 ; unbind raw - 315 mc:az:who
   unbind raw - 352 mc:az:who
  }
 }
 switch -- $mc_az(meth) {
  1 {set buffer "MSG'n to nick."}
  2 {set buffer "NOTICE'n to nick."}
  default {set buffer "MSG'n to channel."}
 }
 putlog "***WHO \[$mc_az(trigger)\] $mc_az(version) loaded, by MC_8 for AZ0R0S <Method: $buffer>"
} {
 putlog "***WHO \[$mc_az(trigger)\] NOT loaded, please load moretools.tcl v1.0.1+ (before this one)"
}