####################################################
# by wiebe @ QuakeNet
#
# can use chase.tcl when available
# can use term.tcl when available
####################################################

setudef flag public

####################################################
# kick:help:pubm
####################################################
bind pubm lomn|lomn "% ${botnet-nick} help kick" kick:help:pubm
proc kick:help:pubm { n u h c t } {
  if {[matchattr $h bkZ]} { return 0 }
  lappend o "kick: usage kick \[<chan>\] <nick> \[<nick2> .. <nick6> :\]<reason>"
  lappend o "kick: kicks users from the channel, the reason can be a custom reason, one predefined from the rule command or a term from the term database (use ?? \[<chan>\] <term>). the ':' prefixing the reason is required when using multiple nicks."
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "kick: " } }
  putloglev c $c "help: $n $u $h $c kick"
  return 1
}


####################################################
# kick:help:msgm
####################################################
bind msgm lomn|lomn "help kick" kick:help:msgm
proc kick:help:msgm { n u h t } {
  if {[matchattr $h bkZ]} { return 0 }
  lappend o "kick: usage kick <chan> <nick> \[<nick2> .. <nick6> :\]<reason>"
  lappend o "kick: kicks users from the channel, the reason can be a custom reason, one predefined from the rule command or a term from the term database (use ?? \[<chan>\] <term>). the ':' prefixing the reason is required when using multiple nicks."
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "kick: " } }
  putcmdlog "($n!$u) !$h! help kick"
  return 1
}


####################################################
# kick:pubm
####################################################
bind pubm lomn|lomn "% ${botnet-nick} kick" kick:pubm
bind pubm lomn|lomn "% ${botnet-nick} kick *" kick:pubm
proc kick:pubm { n u h c t } {
  if {![validchan $c]} { return 0 }
  if {[matchattr $h bkZ]} { return 0 }
  set t [join [lrange [split $t] 2 end]]
  set o [kick:kick $h $t $n $c]
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "kick: " } }
  putloglev c $c "kick: $n $u $h $c $t"
  return 1
}


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


####################################################
# kick:dcc
####################################################
bind dcc -|- kick kick:dcc
proc kick:dcc { h i t } {
  if {![valididx $i]} { return 0 }
  set c [lindex [split [console $i]] 0]
  if {![validchan $c]} { set c "" }
  if {[string equal $t ""]} {
    lappend o "kick: usage kick \[<chan>\] <nick> \[<nick2> .. <nick6> :\]<reason>"
    lappend o "kick: kicks users from the channel, the reason can be a custom reason, one predefined from the rule command or a term from the term database (use ?? \[<chan>\] <term>). the ':' prefixing the reason is required when using multiple nicks."
  } else { set o [kick:kick $h $t "" $c] }
  foreach l $o { putidx $i $l }
  return 1
}


####################################################
# kick:kick
####################################################
proc kick:kick { h t m c } {
  set t [split $t]; set d [lindex $t 0]
  if {[string match {[#&]*} $d]} { set c $d; set t [lrange $t 1 end] }
  if {![validchan $c] && ![string equal [info procs whichchan] ""]} { set c [whichchan $h $c] }
  set p [lsearch -glob $t ":*"]
  if {![string equal $p "-1"]} {
    set r [string range [join [lrange $t $p end]] 1 end]
    set t [lrange $t 0 [expr $p -1]]
  } else { set r [join [lrange $t 1 end]]; set t [split [lindex $t 0]] }

  if {[string equal $c ""]} {
    set d ""
    foreach c [channels] { if {[channel get $c public]} { lappend d $c } }
    if {[string equal [llength $d] 1]} { set c [lindex $d 0] } else { set c "" }
  }

  if {[string equal $c ""]} {
    lappend o "kick: usage kick \[<chan>\] <nick> \[<nick2> .. <nick6> :\]<reason>"
  } elseif {![validchan $c] || ![matchattr $h lomn|lomn $c]} {
    lappend o "kick: no access or unknown channel $c"
  } elseif {[string equal $t ""] && [string equal $r ""]} {
    lappend o "kick: usage kick \[<chan>\] <nick> \[<nick2> .. <nick6> :\]<reason>"
  } elseif {[string equal $t ""]} {
    lappend o "kick: no nicks specified, only a reason"
  } elseif {![botonchan $c]} {
    lappend o "kick: I am not on $c"
  } elseif {![botisop $c] && ![botishalfop $c]} {
    lappend o "kick: I am not opped on $c"
  } else {
    if {[string match "\\?\\? ?*" $r] && ![string equal [info procs term:export] ""]} { 
      set r [term:export $c [join [lrange [split $r] 1 end]]]
    }
    if {![string equal [info procs rule:get] ""]} { set r [rule:get $r] }
    if {[string equal $r ""]} { set r "out" }
    if {[llength $t] > 6} { set t [lrange $t 0 5]; lappend o "kick: maximum is 6 nicks" }
    set s ""; set f ""; set b ""
    foreach n $t {
      if {[string equal $n ""]} { continue }
      if {![onchan $n $c]} {
        if {![string equal [info procs chase] ""]} { set d [chase $n] }
        if {![string equal $d ""]} { set n $d }
      }
      if {![onchan $n] || [onchansplit $n $c]} { lappend f "'$n not found'"; continue }
      if {[isop $n $c] || [ishalfop $n $c]} { lappend f "'$n is op'"; continue }
      set g [nick2hand $n]
      if {[matchattr $g bfvlomn|fvlomn $c] && ![matchattr $g kZ|k $c]} {
        lappend f "'$n is friendly'"; continue
      }
      putkick $c $n $r; lappend s $n
    }
    if {![string equal $s ""]} {
      set s [join $s "   "]; set g [md5 "kick $s"]
      lappend o "kick: kicked from $c '$s' :$r"
      if {![string equal [info procs ircconsole] ""]} {
        ircconsole $c x KICK "$h '$s' :$r" $g
      }
    }
    if {![string equal $f ""]} { lappend o "kick: failed to kick from $c [join $f "   "]" }
  }
  return $o
}


####################################################
# kick:chan
####################################################
# not used
proc kick:chan { h m t } {
  set r ""
  foreach c [channels] {
    if {![channel get $c public]} { continue }
    if {![botisop $c] && ![botishalfop $c]} { continue }
    if {![matchattr $h lomn|lomn $c]} { continue }
    if {![string equal $m ""] && (![onchan $m $c] || [onchansplit $m $c])} { continue }
    set x 0
    foreach n $t {
      if {![onchan $n $c]} {
        if {![string equal [info procs chase] ""]} { set d [chase $n] }
        if {![string equal $d ""]} { set n $d }
      }
      if {![onchan $n $c] || [onchansplit $n $c]} { continue }
      incr x
    }
    lappend r "$x $c"
  }
  set r [lsort -decreasing -dictionary $r]
  set c [lindex $r 0 1]
  return $c
}


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

