#by wiebe @ QuakeNet
#requires c_rule.tcl
#requires chase.tcl


#help kick pub
bind pubm lomn|lomn "% ${botnet-nick} help kick" c_kick:help:pub

proc c_kick:help:pub { nick uhost handle chan text } {
  lappend output "Usage: kick <nick1> \[<nick2> .. <nick6>\] \[:<reason>\]"
  lappend output "Kicks 6 users at most, custom reason or one predefined from the rule command, the : prefixing the reason is required when using multiple nicks."
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  putloglev c $chan "[lindex [split $text] 1]: $nick $uhost $handle $chan [join [lrange [split $text] 2 end]]"
}



#help kick msg
bind msgm lomn|lomn "help kick" c_kick:help:msg

proc c_kick:help:msg { nick uhost handle text } {
  lappend output "Usage: kick <channel> <nick1> \[<nick2> .. <nick6>\] \[:<reason>\]"
  lappend output "Kicks 6 users at most, custom reason or one predefined from the rule command, the : prefixing the reason is required when using multiple nicks."
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  putcmdlog "($nick!$uhost) !$handle! $text"
}



#kick pub
bind pubm lomn|lomn "% ${botnet-nick} kick" c_kick:pub
bind pubm lomn|lomn "% ${botnet-nick} kick *" c_kick:pub

proc c_kick:pub { nick uhost handle chan text } {
  set targets [join [lrange [split $text] 2 end]]
  if { ![botisop $chan] && ![botishalfop $chan] } {
    lappend output "I am not a channel operator on $chan"    
  } elseif { [string equal $targets ""] } {
    lappend output "Usage: kick <nick1> \[<nick2> .. <nick6>\] \[:<reason>\]"
  } else {
    set output [c_kick:kick $nick $chan $targets]
  }
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  putloglev c $chan "[lindex [split $text] 1]: $nick $uhost $handle $chan [join [lrange [split $text] 2 end]]"
}



#kick msg
bind msg lomn|lomn kick c_kick:msg

proc c_kick:msg { nick uhost handle text } {
  set chan [lindex [split $text] 0]
  set targets [join [lrange [split $text] 1 end]]
  if { ![validchan $chan] && ![string equal $chan ""] } {
    lappend output "No access or unknown channel $chan"
  } elseif { [string equal $chan ""] || [string equal $targets ""] } {
    lappend output "Usage: kick <channel> <nick1> \[<nick2> .. <nick6>\] \[:<reason>\]"
  } elseif { ![matchattr $handle lomn|lomn $chan] } {
    lappend output "No access or unknown channel $chan"
  } elseif { ![botisop $chan] && ![botishalfop $chan] } {
    lappend output "I am not a channel operator on $chan"
  } else {
    set output [c_kick:kick $nick $chan $targets]
  }
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  return 1
}



#kick
proc c_kick:kick { nick chan targets } {
  set reason ""; set onchan ""; set offchan ""; set output ""
  set p [lsearch -glob [split $targets] :*]

#found reason, adjust targets
  if { ![string equal $p -1] } {
    set reason [string range [join [lrange [split $targets] $p end]] 1 end]
    set targets [join [lrange [split $targets] 0 [expr $p -1]]]

#allow max 6 targets
    set targets [join [lrange [split $targets] 0 5]]
  } else {
    set reason [join [lrange [split $targets] 1 end]]
    set targets [lindex [split $targets] 0]
  }

  foreach t [split $targets] {


#chasenick
    if { ![onchan $t $chan] } {
      if { [catch {set ct [chase:chasenick $t]} error] } {
        putlog "ERROR using chase.tcl, perhaps you have not loaded it?"
      }
      if { ![string equal $ct -1] && [onchan $ct $chan] && ![onchansplit $ct] } { set t $ct }
    }

#dont kick myself or a bot or a target not on the channel
    if { [isbotnick $t] || [matchattr [nick2hand $t] b] || ![onchan $t $chan] || [onchansplit $t] } {
      if { [string equal [lsearch -exact [string tolower $offchan] [string tolower $t]] -1] } {
        lappend offchan $t
      }

#on the channel
    } elseif { [onchan $t $chan] && ![onchansplit $t] } {
      if { [string equal [lsearch -exact [string tolower $onchan] [string tolower $t]] -1] } {
        lappend onchan $t
      }

#is not a valid nick, asume this is the reason, find it and save
    } elseif { ![regexp {^[a-zA-Z\^\[\]\{\}\\\|\_][a-zA-Z0-9\^\[\]\{\}\\\|\-\_]+$} $t] &&
 [string equal $p -1] } {
      set p [lsearch -exact [split $targets] $t]
      set reason [join [lrange [split $targets] $p end]]
      break
    }
  }

#set reason
  set temp 0
  if { [catch {set temp [c_rule:rule $reason]} error] } {
    putlog "ERROR using c_rule.tcl, perhaps you have not loaded it?"
  }
  if { ![string equal $temp 0] } { set reason $temp }

#use default
  if { [string equal $reason ""] } {
    if { [catch {set reason [c_rule:rule \#0]} error] } {
      putlog "ERROR using c_rule.tcl, perhaps you have not loaded it?"
      set reason "You are violating channel rules."
    }    
  }

  if { ![string equal $onchan ""] } {
    lappend output "Kicked from $chan [join $onchan] ($reason)"
    foreach t $onchan { putkick $chan $t $reason }
  }
  if { ![string equal $offchan ""] } { lappend output "Failed to kick from $chan [join $offchan]" }
  if { [string equal $offchan$onchan ""] } { lappend output "Failed to find any valid targets to kick from $chan" }
  return $output
}

