#by wiebe @ QuakeNet
#requires chase.tcl
#requires dnsdb.tcl
#requires whox.tcl



#help chase msg
bind msgm lomn|lomn "help chase" c_chase:help:msg

proc c_chase:help:msg { nick uhost handle text } {
  lappend output "Usage: chase <channel> <nick>"
  lappend output "Shows stored info for the given nick."
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  putcmdlog "($nick!$uhost) !$handle! $text"
}



#chase msg
bind msg lomn|lomn chase c_chase:msg

proc c_chase:msg { nick uhost handle text } {
  set chan [lindex [split $text] 0]
  set cnick [lindex [split $text] 1]
  if { ![validchan $chan] && ![string equal $chan ""] } {
    lappend output "No access or unknown channel $chan"
  } elseif { [string equal $chan ""] || [string equal $cnick ""] } {
    lappend output "Usage: chase <channel> <nick>"
  } elseif { ![matchattr $handle lomn|lomn $chan] } {
    lappend output "No access or unknown channel $chan"  } else {

#get host from chase
    set chost ""
    if { [onchan $cnick] } {
      set ts [getchanjoin $cnick $chan]
      if { [string equal $ts 0] } {
        set ts [getchanjoin $::botnick $chan]
        set chost "$ts [getchanhost $cnick] o bot join"
      } else {
        set chost "$ts [getchanhost $cnick] o user join"
      }
    } elseif { [catch {set chost [chase:chaseuhost $cnick $chan 1]} error] } {
      putlog "ERROR using chase.tcl, perhaps you have not loaded it?"
      error $error
    } elseif { [string equal $chost -1] } { set chost "" }
#get host from dnsdb
    set dnsdbhost ""
    if { [onchan $cnick] } {
      if { [catch {set dnsdbhost [dnsdb:host $cnick]} error] } {
        putlog "ERROR using dnsdb.tcl, perhaps you have not loaded it?"
        error $error
      }
    } elseif { [catch {set dnsdbhost [dnsdbchase:host $cnick]} error] } {
      putlog "ERROR using dnsdb.tcl, perhaps you have not loaded it?"
      error $error
    }
    if { [string equal $dnsdbhost -1] } { set dnsdbhost "" }

#get ip from dnsdb
    set dnsdbip ""
    if { [onchan $cnick] } {
      if { [catch {set dnsdbip [dnsdb:ip $cnick]} error] } {
        putlog "ERROR using dnsdb.tcl, perhaps you have not loaded it?"
        error $error
      }
    } elseif { [catch {set dnsdbip [dnsdbchase:ip $cnick]} error] } {
      putlog "ERROR using dnsdb.tcl, perhaps you have not loaded it?"
      error $error
    }
    if { [string equal $dnsdbip -1] } { set dnsdbip "" }

#get whox info
    set whox ""
    if { [onchan $cnick] } {
      if { [catch {set whox [whox $cnick all]} error] } {
        putlog "ERROR using dnsdb.tcl, perhaps you have not loaded it?"
        error $error
      }
    } elseif { [catch {set whox [whox $cnick all]} error] } {
      putlog "ERROR using dnsdb.tcl, perhaps you have not loaded it?"
      error $error
    }

#show
    if { ![string equal $chost ""] } {
      set ts [lindex [split $chost] 0]
      set ts "[ctime $ts] ([c_chase:ts $ts] ago)"
      set userhost [lindex [split $chost] 1]
      set how [lindex [split $chost] 2]
      if { [string equal $how p] } {
        set reason [join [lrange [split $chost] 3 end]]
        set how "parted at $ts ($reason)"
      } elseif { [string equal $how k] } {
        set by "[lindex [split $chost] 3]"
        if { [string match *!*@* $by] } {
          set by "[lindex [split $by !] 0] ([lindex [split $by !] 1])"
        } else {
          set by [lindex [split $by !] 1]
        }
        set reason [join [lrange [split $chost] 4 end]]
        set how "kicked from $chan at $ts by $by ($reason)"
      } elseif { [string equal $how q] } {
        set reason [join [lrange [split $chost] 3 end]]
        set how "quit $chan at $ts ($reason)"
      } elseif { [string equal $how s] } {
        set how "quit $chan at $ts (*.net *.split)"
      } elseif { [string equal $how o] } {
        set reason [join [lrange [split $chost] 3 end]]
        set how "on $chan since $ts ($reason)"
      } else {
        set reason [join [lrange [split $chost] 3 end]]
        if { ![string equal $reason ""] } { set reason ($reason) }
        set how "vanished from $chan at $ts $reason"
      }
      lappend output "$cnick user@host: $userhost"
      lappend output "$cnick $how"
    }
    if { ![string equal $dnsdbhost ""] } {
      if { ![string equal -nocase [lindex [split $chost @] 1] $dnsdbhost] } {
        lappend output "$cnick host: $dnsdbhost"
      }
    }
    if { ![string equal $dnsdbip ""] } {
      if { ![string equal -nocase [lindex [split $chost @] 1] $dnsdbip] } {
        lappend output "$cnick IP: $dnsdbip"
      }
    }
    if { ![string equal $whox ""] } {
      set whox [split $whox]
      set i ip=[lindex $whox 0]
      set s server=[lindex $whox 1]
      set f flags=[lindex $whox 2]
      set d distance=[lindex $whox 3]
      set l idle=[lindex $whox 4]
      set a account=[lindex $whox 5]
      set r realname=[join [lrange $whox 6 end]]
      if { [string match *x* $f] } { set i ip=127.0.0.1 }
      set whox "$i   $s   $f   $d   $l   $a   $r"
      lappend output "$cnick WHOX: $whox"
    }
    if { [string equal $chost$dnsdbhost$dnsdbip$whox ""] } {
      lappend output "No chase info was found for $cnick on $chan"
    } else {
      lappend output "End of chase info for $cnick on $chan"
    }

  }
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  return 1
}



#give timestamp, returns duration since/to in Xy Xw Xd Xh Xm Xs format
proc c_chase:ts { ts } {
  if { ![string is digit $ts] } { return 0 }
  if { $ts > [unixtime] } {
    set ts [duration [expr $ts - [unixtime]]]
  } else {
    set ts [duration [expr [unixtime] - $ts]]
  }
  set ts [string map [list " seconds" "s" "search" "replace" "search" "replace"] $ts]
  set ts [string map [list " second" "s" "search" "replace" "search" "replace"] $ts]
  set ts [string map [list " minutes" "m" "search" "replace" "search" "replace"] $ts]
  set ts [string map [list " minute" "m" "search" "replace" "search" "replace"] $ts]
  set ts [string map [list " hours" "h" "search" "replace" "search" "replace"] $ts]
  set ts [string map [list " hour" "h" "search" "replace" "search" "replace"] $ts]
  set ts [string map [list " days" "d" "search" "replace" "search" "replace"] $ts]
  set ts [string map [list " day" "d" "search" "replace" "search" "replace"] $ts]
  set ts [string map [list " weeks" "w" "search" "replace" "search" "replace"] $ts]
  set ts [string map [list " week" "w" "search" "replace" "search" "replace"] $ts]
  set ts [string map [list " years" "y" "search" "replace" "search" "replace"] $ts]
  set ts [string map [list " year" "y" "search" "replace" "search" "replace"] $ts]
  return [join [lrange [split $ts] 0 1]]
}

