#by wiebe @ QuakeNet



#help whois msg
bind msgm fvlomn|fvlomn "help whois" c_whois:help:msg

proc c_whois:help:msg { nick uhost handle text } {
  lappend output "Usage: whois <nick>|<#account>"
  lappend output "Shows account info, global access level, channel list and date/time the user last joined. Use a nick or prefix the account name with a #. Channels are only shown to you if you have flags on it or are a global master or owner."
  lappend output "Where \002a\002=auto-op \002b\002=bot \002d\002=deny-op \002f\002=friend \002g\002=auto-voice \002k\002=auto-kickban \002l\002=halfop \002m\002=master \002n\002=owner \002o\002=op \002p\002=party-line \002q\002=deny-voice \002r\002=deny-halfop \002u\002=unshared \002v\002=voice \002y\002=auto-halfop"
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  putcmdlog "($nick!$uhost) !$handle! $text"
}



#whois msg
bind msg fvlomn|fvlomn whois c_whois:msg

proc c_whois:msg { nick uhost handle text } {
  set target [lindex [split $text] 0]
  set account [string range $target 1 end]

#wrong use
  if { [string equal $target \#] || [string equal $target ""] } {
    lappend output "Usage: whois <nick>|<#account>"

#unknown account
  } elseif { ![validuser $account] && [string match \#* $target] } {
    lappend output "Account $account is not known."

#nick is not known, we dont see him
  } elseif { (![onchan $target] || ![validuser [nick2hand $target]]) && ![validuser $account] } {
    lappend output "User $target is not known."

#we got through, and can begin the whois
  } else {
    if { ![string match \#* $target] } {
      set account [nick2hand $target]
      lappend output "User $target is $account and is known on the following channels:"
    } else {
      lappend output "Account $account is known on the following channels:"
    }
    set flags [chattr $account]
    set space [string repeat " " 24]
    if { ![string equal $flags -] } { lappend output "$account has global flags: $flags" }
    set result 0
    foreach chan [lsort [channels]] {
      if { [haschanrec $account $chan] } {

#show only if sharing fvlomn chan flags, whois'ing yourself, or having global mn flags
        if { [string equal -nocase $account $handle] || [matchattr $handle mn|fvlomn $chan] } {
          set flags [lindex [split [chattr $account $chan] |] 1]
          if { ![string equal $flags -] } {
            set result 1
            set space1 [string repeat " " [expr 30 - [string length $chan]]]
            set laston [getuser $account LASTON $chan]
            if { [string equal $laston 0] } { set laston "" }
            set lastondate ""
            if { ![string equal $laston ""] } { set lastondate [clock format $laston -format %d/%m/%y] }
            set date [clock format [unixtime] -format %d/%m/%y]
            if { [string equal $lastondate $date] } {
              set laston [clock format $laston -format %H:%M]
            } elseif { ![string equal $laston ""] } {
              set laston [clock format $laston -format %d/%m/%y]
            } else {
              set laston never
            }
            set space2 [string repeat " " [expr 15 - [string length $flags]]]
            lappend output "$chan$space1$flags$space2$laston"
          } else { delchanrec $account $chan }
        }
      }
    }
    if { !$result } {
      lappend output "Account $account is not known on any channels."
    } else {
      lappend output "End of list."
    }
  }
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  return 1
}

