####################################################
# by wiebe @ QuakeNet
#
# bans users from channels with chanout command
#
# this script *REQUIRES* bannick.tcl
#
# script can use: irc.tcl ircconsole.tcl rule.tcl term.tcl whichchan.tcl
# script provides info for: script.tcl
#
####################################################

####################################################
# chanout:help:pubm
####################################################
bind pubm lomn|lomn "% ${botnet-nick} help chanout" chanout:help:pubm
proc chanout:help:pubm { n u h c t } {
  if {[matchattr $h bkZ]} { return 0 }
  lappend o "chanout: usage chanout \[<chan>\] <nick> \[<nick2> .. <nick6> :\]<reason>"
  lappend o "chanout: bans 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. prefix nicks with a # to ban *!*@<account>.users.quakenet.org, to avoid confusion with #chan and #nick, use - for chan when it is supposed to be the current channel."
  if {[info procs cnotice] == ""} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "chanout: " } }
  putloglev c $c "help: $n $u $h $c chanout"
  return 1
}


####################################################
# chanout:help:msgm
####################################################
bind msgm lomn|lomn "help chanout" chanout:help:msgm
proc chanout:help:msgm { n u h t } {
  if {[matchattr $h bkZ]} { return 0 }
  lappend o "chanout: usage chanout <chan> <nick> \[<nick2> .. <nick6> :\]<reason>"
  lappend o "chanout: bans 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. prefix nicks with a # to ban *!*@<account>.users.quakenet.org"
  if {[info procs cnotice] == ""} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "chanout: " } }
  putcmdlog "($n!$u) !$h! help chanout"
  return 1
}


####################################################
# chanout:pubm
####################################################
bind pubm lomn|lomn "% ${botnet-nick} chanout" chanout:pubm
bind pubm lomn|lomn "% ${botnet-nick} chanout *" chanout:pubm
proc chanout: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 [chanout:chanout $h $t $c]
  if {[info procs cnotice] == ""} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "chanout: " } }
  putloglev c $c "chanout: $n $u $h $c $t"
  return 1
}


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


####################################################
# chanout:dcc
####################################################
bind dcc -|- chanout chanout:dcc
proc chanout:dcc { h i t } {
  if {![valididx $i]} { return 0 }
  set c [lindex [split [console $i]] 0]
  if {![validchan $c]} { set c "" }
  if {$t == ""} {
    lappend o "chanout: usage chanout \[<chan>\] <nick> \[<nick2> .. <nick6> :\]<reason>"
    lappend o "chanout: bans 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. prefix nicks with a # to ban *!*@<account>.users.quakenet.org"
  } else { set o [chanout:chanout $h $t $c] }
  foreach l $o { putidx $i $l }
  return 1
}


####################################################
# chanout:chanout
####################################################
proc chanout:chanout { h t {c ""} } {
  set t [split $t]; set d [lindex $t 0]; set l "240"
  if {$c == "" || [string match {[#&]*} $d]} { set c $d; set t [lrange $t 1 end] }
  if {[lindex $t 0] == "-"} { set t [lrange 1 end] }
  if {![validchan $c] && [info procs whichchan] != ""} { set c [whichchan $h $c] }
  set p [lsearch -glob $t ":*"]
  if {$p != "-1"} {
    set r [string range [join [lrange $t $p end]] 1 end]; set t [join [lrange $t 0 [expr $p -1]]]
  } else { set r [join [lrange $t 1 end]]; set t [lindex $t 0] }
  if {[info procs bannick:ban] == ""} {
    lappend o "chanout: this script requires bannick.tcl"
  } elseif {$c == ""} {
    lappend o "chanout: usage chanout \[<chan>\] <nick> \[<nick2> .. <nick6> :\]<reason>"
  } elseif {![validchan $c] || ![matchattr $h lomn|lomn $c]} {
    lappend o "chanout: no access or unknown channel $c"
  } elseif {$t == "" && $r == ""} {
    lappend o "chanout: usage chanout \[<chan>\] <nick> \[<nick2> .. <nick6> :\]<reason>"
  } elseif {$t == ""} {
    lappend o "chanout: no nicks specified, only a reason"
  } else {
    if {[string match "\\?\\? ?*" $r] && [info procs term:export] != ""} { 
      set r [term:export $c [join [lrange [split $r] 1 end]]]
    }
    if {[info procs rule:get] != ""} { set r [rule:get $r] }
    if {$r == ""} { set r "Banned" }
    if {[llength $t] > 6} { set t [lrange $t 0 5]; lappend o "chanout: maximum is 6 nicks" }
    bannick:ban [join $t ,] $c $h $r $l; set t [join $t "  "]
    lappend o "chanout: banned from $c '$t' :$r"
    if {[info procs ircconsole] != ""} { ircconsole $c o CHANOUT "$h BAN '$t' :$r" }
  }
  return $o
}


set ::scriptdb(chanout) {
  "provides chanout command"
}

