#by wiebe @ QuakeNet
#provides nick chasing and user@host chasing for other scripts



#join
bind join -|- * chase:join
bind rejn -|- * chase:join

proc chase:join { nick uhost handle chan } {
  global chasedb
  set nick [string tolower $nick]
  set chan [string tolower $chan]
  if { [info exists chasedb(uhost,$chan,$nick)] } {
    unset chasedb(uhost,$chan,$nick)
  }
  if { [info exists chasedb(uhost,$nick)] } {
    unset chasedb(uhost,$nick)
  }
}



#part
bind part -|- * chase:part

proc chase:part { nick uhost handle chan {msg ""} } {
  global chasedb
  if { ![isbotnick $nick] && [validchan $chan] } {
    set nick [string tolower $nick]
    set chan [string tolower $chan]
    set chasedb(uhost,$chan,$nick) "[unixtime] $uhost p"
    set chasedb(uhost,$nick) "[unixtime] $uhost"
  }
}



#kick
bind kick -|- * chase:kick

proc chase:kick { nick uhost handle chan target msg } {
  global chasedb
  if { ![isbotnick $target] && [validchan $chan] } {
    set target [string tolower $target]
    set chan [string tolower $chan]
    set chasedb(uhost,$chan,$target) "[unixtime] [getchanhost $target] k $nick!$uhost $msg"
    set chasedb(uhost,$target) "[unixtime] [getchanhost $target]"
  }
}



#quit
bind sign -|- * chase:quit

proc chase:quit { nick uhost handle chan msg } {
  global chasedb
  if { [string equal -nocase $msg "registered"] } { return }
  if { [string equal -nocase $msg "host change"] } { return }
  set nick [string tolower $nick]
  set chan [string tolower $chan]
  set chasedb(uhost,$chan,$nick) "[unixtime] $uhost q $msg"
  set chasedb(uhost,$nick) "[unixtime] $uhost"
}



#split
bind splt -|- * chase:splt

proc chase:splt { nick uhost handle chan } {
  global chasedb
  set nick [string tolower $nick]
  set chan [string tolower $chan]
  set chasedb(uhost,$chan,$nick) "[unixtime] $uhost s"
  set chasedb(uhost,$nick) "[unixtime] $uhost"
}


#nick
bind nick -|- * chase:nick

proc chase:nick { nick uhost handle chan newnick } {
  global chasedb
  if { [string equal -nocase $nick $newnick] } { return }
  if { [string equal $chan *] } { return }
  set nick [string tolower $nick]
  set newnick [string tolower $newnick]
  set chasedb(nick,$nick) "[unixtime] $newnick"
}



#time
bind time -|- * chase:time

proc chase:time { mi ho da mo ye } {
  global chasedb
  foreach name [array names chasedb] {
    if { [string match nick,* $name] && [expr [unixtime] - [lindex [split $chasedb($name)] 0]] > "60" } {
      unset chasedb($name)
    } elseif { [string match uhost,* $name] && [expr [unixtime] - [lindex [split $chasedb($name)] 0]] > "86400" } {
      unset chasedb($name)
    }
  }
}



#chasenick, chases the given nick to the current nick, -1 if failed
proc chase:chasenick { nick } {
  global chasedb
  set nick [string tolower $nick]
#does not exist
  if { ![info exists chasedb(nick,$nick)] } { return -1 }
#exists but is older than 60 seconds
  if { [expr [unixtime] - [lindex [split $chasedb(nick,$nick)] 0]] > "60" } {
    unset chasedb(nick,$nick)
    return -1
  }
#exists, trace back to current nick
  set nick [lindex [split $chasedb(nick,$nick)] 1]
  while { [info exists chasedb(nick,$nick)] && ![onchan $nick] } {
    set nick [lindex [split $chasedb(nick,$nick)] 1]
  }
  return $nick
}



#chaseuhost, returns last known user@host for nick on chan, -1 if failed
#without chan param, global chasing
#info param specified, then "ts user@host p/k/q/s by message" is returned
proc chase:chaseuhost { nick {chan ""} {info ""}} {
  global chasedb
  set nick [string tolower $nick]
  set name "uhost,$nick"
  if { ![string equal $chan ""] } {
    set chan [string tolower $chan]
    set name "uhost,$chan,$nick"
  }
#does not exist
  if { ![info exists chasedb($name)] } { return -1 }
#exists but is older than 86400 seconds (1 day)
  if { [expr [unixtime] - [lindex [split $chasedb($name)] 0]] > "86400" } {
    unset chasedb($name)
    return -1
  }
  if { [string equal $info ""] } {
    return [lindex [split $chasedb($name)] 1]
  } else {
    return $chasedb($name)
  }
}

