####################################################
# by wiebe @ QuakeNet
#
####################################################

####################################################
# whois:help:msg
####################################################
bind msgm vlomn|vlomn "help whois" whois:help:msgm
proc whois:help:msgm { n u h t } {
  if {[matchattr $h bkZ]} { return 0 }
  lappend o "whois: usage whois <nick>|#<account>"
  lappend o "whois: 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 any of 'v l o m n' flags on it or have any of 'o m n' global flags."
  lappend o "whois: where   a=auto-op   b=bot   d=deny-op   f=friend   g=auto-voice   k=auto-ban   l=halfop   m=master   n=owner   o=op   p=party-line   q=deny-voice   r=deny-halfop   u=unshared   v=voice   y=auto-halfop"
  if {![string equal [info procs userflag:get] ""]} {
    set r [userflag:get]; if {![string equal $r ""]} { lappend o "whois: $r" }
  }
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "whois: " } }
  putcmdlog "($n!$u) !$h! help whois"
  return 1
}


####################################################
# whois:msg
####################################################
bind msg vlomn|vlomn whois whois:msg
proc whois:msg { n u h t } {
  if {[matchattr $h bkZ]} { return 0 }
  set o [whois:whois $h $t]
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "whois: " } }
  return 1
}


####################################################
# whois:dcc
####################################################
bind dcc -|- whois2 whois:dcc
proc whois:dcc { h i t } {
  set o [whois:whois $h $t]
  lappend o "whois: where   a=auto-op   b=bot   d=deny-op   f=friend   g=auto-voice   k=auto-ban   l=halfop   m=master   n=owner   o=op   p=party-line   q=deny-voice   r=deny-halfop   u=unshared   v=voice   y=auto-halfop"
  if {![string equal [info procs userflag:get] ""]} {
    set r [userflag:get]; if {![string equal $r ""]} { lappend o "whois: $r" }
  }
  foreach l $o { putidx $i $l }
  return 1
}


####################################################
# whois:whois
####################################################
proc whois:whois { h t } {
  set t [lindex [split $t] 0]
  if {[string match "#*" $t]} {
    set a [string range $t 1 end]; if {[validuser $a]} { set s "account '[getuser $a HANDLE]'" }
  } else { set a [nick2hand $t]; set s "user $t (account $a)" }
  if {[string equal $a "*"]} { set a "" }
  if {[string equal $t ""]} {
    lappend o "whois: usage whois <nick>|#<account>"
  } elseif {[string match "#*" $t] && ![validuser $a]} {
    lappend o "whois: unknown account $a"
  } elseif {![string match "#*" $t] && [string equal $a ""]} {
    lappend o "whois: unknown user $t"
  } else {
    if {[string equal -nocase $h $a]} {
      lappend o "whois: you are $h and known on the following channels:"
    } else { lappend o "whois: $s is known to you on the following channels:" }
    if {[matchattr $h mn]} {
      set c [getuser $a XTRA created]
      if {[string equal $c ""]} { set c "???" } else { set c "[ctime $c] ([whois:ts $c] ago)" }
      set b [getuser $a XTRA by]
      if {[string equal $b ""]} { set b "???" }
      lappend o "whois: $s added by $b on $c"
    }
    set f [chattr $a]; set r 0; set c "global flags"; set b 0
    set x [string repeat " " [expr 30 - [string length $c]]]
    if {![string equal $f "-"]} { lappend o "$c$x$f"  }
    foreach c [channels] {
      if {![haschanrec $a $c]} { continue }
      set f [lindex [split [chattr $a $c] |] 1]
      if {[string equal $f "-"]} { delchanrec $a $c; continue }
      if {![matchattr $h omn|vlomn $c]} { continue }
      set x [string repeat " " [expr 30 - [string length $c]]]
      set y [string repeat " " [expr 15 - [string length $f]]]
      set l [getuser $a LASTON $c]; set k ""; set r 1; incr b
      if {[string equal $l 0]} { set l "" }
      if {![string equal $l ""]} { set k [clock format $l -format %d/%m/%y] }
      set d [clock format [clock seconds] -format %d/%m/%y]
      if {[handonchan $a $c]} { set l now
      } elseif {[string equal $k $d]} { set l [clock format $l -format %H:%M]
      } elseif {![string equal $l ""]} { set l [clock format $l -format %d/%m/%y]
      } else { set l never }
      lappend o "$c$x$f$y$l"
    }
    if {!$r} {
      if {[string equal -nocase $h $a]} { lappend o "whois: you are not known on any channels."
      } else { lappend o "whois: $s is not known to you on any channels." }
    } else {
      lappend o "whois: $s is known to you on $b channels."
      lappend o "whois: end of list."
    }
  }
  return $o
}


####################################################
# whois:ts
####################################################
proc whois:ts { t } {
  if {![string is digit $t]} { return 0 }
  set n [clock seconds]; set t [expr $t - $n]; if {$t < 0} { set t [expr $t * -1] }
  set t [duration $t]
  set t [string map "seconds s second s minutes m minute m hours h hour h" $t]
  set t [string map "days d day d weeks w week w years y year y" $t]
  return [join [lrange [split $t] 0 3] ""]
}


set scriptdb(whois) {
  "provides whois command"
}

