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

####################################################
# whowas:get
####################################################
proc whowas:get { nick setting {chan ""} } {
  if {$nick == ""} { return "" }
  if {$setting == ""} { return "" }
  global whowasdb
  set n [string tolower $nick]; set s [string tolower $setting]; set c [string tolower $chan]
  if {$c != ""} { set c ",$c" }
  set e $s$c,$n
  if {![info exists whowasdb($e)]} { return "" }
  set d [split [lindex $whowasdb($e) end]]; set t [lindex $d 0]
  if {[expr [clock seconds] - $t] > "86400"} { unset whowasdb($e); return "" }
  return [join [lrange $d 1 end]]
}


####################################################
# whowas:set
####################################################
proc whowas:set { nick setting value {chan ""} } {
  if {$nick == ""} { return 0 }
  if {$setting == ""} { return 0 }
  if {$value == ""} { return 0 }
  global whowasdb
  set n [string tolower $nick]; set s [string tolower $setting]; set c [string tolower $chan]
  set t [clock seconds]; set v $value; if {$c != ""} { set c ",$c" }
  set e $s$c,$n
  if {[info exists whowasdb($e)]} {
    regsub -all {[][\\]} $v {\\\0} w
    set p [lsearch -glob $whowasdb($e) "* $w"]
    if {$p > -1} { set whowasdb($e) [lreplace $whowasdb($e) $p $p] }
    lappend whowasdb($e) "$t $v"; set whowasdb($e) [lrange $whowasdb($e) end-4 end]
  } else { lappend whowasdb($e) "$t $v" }
  return 1
}


####################################################
# whowas:part
####################################################
bind part -|- * whowas:part
proc whowas:part { n u h c {m ""} } { whowas:leave $c $n $u $h }

####################################################
# whowas:kick
####################################################
bind kick -|- * whowas:kick
proc whowas:kick { n u h c t m } { whowas:leave $c $t [getchanhost $t $c] [nick2hand $t $c] }


####################################################
# whowas:sign
####################################################
bind sign -|- * whowas:sign; bind splt -|- * whowas:sign
proc whowas:sign { n u h c {m ""} } {
  if {[string equal -nocase $m "registered"]} { return 0 }
  if {[string equal -nocase $m "host change"]} { return 0 }
  whowas:leave $c $n $u $h
}


####################################################
# whowas:leave
####################################################
proc whowas:leave { c n u h } {
  whowas:set $n uhost $u $c; whowas:set $n uhost $u
  whowas:set $n hand $h $c; whowas:set $n hand $h
}


####################################################
# whowas:time
####################################################
bind time -|- "14 * * * *" whowas:time
proc whowas:time { mi ho da mo ye } {
  global whowasdb
  set n [clock seconds]
  foreach e [array names whowasdb] {
    set d $whowasdb($e); set m [expr [llength $d] -1]
    for {set f 0} {$f <= $m} {incr f} {
      set t [lindex [split [lindex $d end-$f]] 0]
      if {[expr $n - $t] < "86400"} { continue }
      if {$f == 0} { unset whowasdb($e); break }
      set whowasdb($e) [lrange $d end-[expr $f -1] end]; break
    }
  }
}


####################################################
# whowas:help:msg
####################################################
bind msgm lomn|lomn "help whowas" whowas:help:msgm
proc whowas:help:msgm { n u h t } {
  if {[matchattr $h bkZ]} { return 0 }
  lappend o "whowas: usage whowas \[<chan>\] <nick> \[<limit>\]"
  lappend o "whowas: shows cached info for the given nick (handle, user@host, hostname, IP, whox). if channel is omitted global info is shown, need to be a global op, master or owner to access. limit specifies the maximum results per type, default is 0 (no limit)."
  if {[info procs cnotice] == ""} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "whowas: " } }
  putcmdlog "($n!$u) !$h! help whowas"
  return 1
}


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


####################################################
# whowas:dcc
####################################################
bind dcc -|- whowas whowas:dcc
proc whowas:dcc { h i t } {
  if {$t == ""} {
    lappend o "whowas: usage whowas \[<chan>\] <nick> \[<limit>\]"
    lappend o "whowas: shows cached info for the given nick (user@host, hostname, IP, whox). if channel is omitted global info is shown, need to be a global op, master or owner to access. limit specifies the maximum results per type, default is 0 (no limit)."
  } else { set o [whowas:whowas $h $t] }
  foreach l $o { putidx $i $l }
  return 1
}


####################################################
# whowas:whowas
####################################################
proc whowas:whowas { h t } {
  set t [split $t]; set c [lindex $t 0]; set n $c; set m [lindex $t 1]
  if {[string match "\#*" $c]} { set n $m; set m [lindex $t 2] } else { set c "" }
  if {![validchan $c] && [info procs whichchan] != ""} { set c [whichchan $h $c] }
  if {$m == ""} { set m 0 }
  if {$c == "" && ![matchattr $h omn]} {
    lappend o "whowas: no channel specified"
    lappend o "whowas: usage whowas \[<chan>\] <nick> \[<limit>\]"
  } elseif {$c != "" && (![validchan $c] || ![matchattr $h lomn|lomn $c])} {
    lappend o "whowas: no access or unknown channel $c"
  } elseif {$n == ""} {
    lappend o "whowas: no nick specified"
    lappend o "whowas: usage whowas \[<chan>\] <nick> \[<limit>\]"
  } elseif {![regexp {^\d+$} $m]} {
    lappend o "whowas: invalid limit specified (must be a number)"
    lappend o "whowas: usage whowas \[<chan>\] <nick> \[<limit>\]"
  } else {
    global whowasdb; set f ""; set g 0
    foreach s "hand uhost dnsdbh dnsdbi whox" {
      if {$c == ""} { set e "$s,$n"; set d $n
      } else { set e "$s,$c,$n"; set d "$n on $c" }
      set e [string tolower $e]
      set s [string map "hand handle uhost user@host dnsdbh hostname dnsdbi IP" $s]
      if {![info exists whowasdb($e)]} { lappend f $s; continue }
      set y $whowasdb($e); set z [expr [llength $y] -1]; set u ""
      for {set x 0} {$x <= $z && ($x < $m || $m == 0)} { incr x} {
        set w [split [lindex $y end-$x]]; set v [join [lrange $w 1 end]]; set w [lindex $w 0]
        set w [whowas:ts $w];
        if {!$g} { lappend o "whowas: showing info for $d:" }
        lappend u "$v ($w)"; set g 1
      }
      if {$u != ""} { lappend o "$s: [join $u "   "]" }
    }
    if {$f != ""} { lappend o "whowas: no info available for $d: [join $f "   "]" }
  }
  return $o
}


####################################################
# whowas:ts
####################################################
proc whowas: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(whowas) {
  "keeps info from users that left the bot's channels (think of user@host, ip, and any other info forwarded by other scripts. provides whowas command to show cached info."
}

