####################################################
# by wiebe @ QuakeNet
#
# bans users from all +public channels where one has access on with out command
#
# this script *REQUIRES* bannick.tcl
#
# script can use: irc.tcl ircconsole.tcl rule.tcl term.tcl script.tcl
####################################################

setudef flag public

####################################################
# out:help:pubm
####################################################
bind pubm lomn|lomn "% ${botnet-nick} help out" out:help:pubm
proc out:help:pubm { n u h c t } {
  if {[matchattr $h bkZ]} { return 0 }
  lappend o "out: usage out <nick> \[<nick2> .. <nick6> :\]<reason>"
  lappend o "out: bans users, 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 "out: " } }
  putloglev c $c "help: $n $u $h $c out"
  return 1
}


####################################################
# out:help:msgm
####################################################
bind msgm lomn|lomn "help out" out:help:msgm
proc out:help:msgm { n u h t } {
  if {[matchattr $h bkZ]} { return 0 }
  lappend o "out: usage out <nick> \[<nick2> .. <nick6> :\]<reason>"
  lappend o "out: bans users 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 "out: " } }
  putcmdlog "($n!$u) !$h! help out"
  return 1
}


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


####################################################
# out:msg
####################################################
bind msg lomn|lomn out out:msg
proc out:msg { n u h t } {
  if {[matchattr $h bkZ]} { return 0 }
  if {$t == ""} {
    lappend o "out: usage out <nick> \[<nick2> .. <nick6> :\]<reason>"
  } else { set o [out:out $h $t] }
  if {[info procs cnotice] == ""} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "out: " } }
  return 1
}


####################################################
# out:dcc
####################################################
bind dcc -|- out out:dcc
proc out:dcc { h i t } {
  set p 0
  if {![matchattr $h lomn]} {
    foreach c [channels] { if {[matchattr $h lomn|lomn $c]} { set p 1; break } }
  } else { set p 1 }
  set c [lindex [split [console $i]] 0]
  if {![validchan $c]} { set c "" }
  if {!$p} {
    lappend o "out: no access"
  } elseif {$t == ""} {
    lappend o "out: usage out <nick> \[<nick2> .. <nick6> :\]<reason>"
    lappend o "out: 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 [out:out $h $t $c] }
  foreach l $o { putidx $i $l }
  return 1
}


####################################################
# out:out
####################################################
proc out:out { h t {c ""} } {
  set t [split $t]; set p [lsearch -glob $t ":*"]; set l "240"
  if {[info procs bannick:ban] == ""} {
    lappend o "out: this script requires bannick.tcl"
  } else {
    if {$p == 0} {
      set t ""; set r ""
      lappend o "out: no nicks were specified, only a reason"
    } elseif {$p != "-1"} {
      set r [string range [join [lrange $t $p end]] 1 end]
      if {$p > 6} { lappend o "out: maximum is 6 nicks"; set p 6 }
      set t [join [lrange $t 0 [expr $p -1]]]
    } else { set r [join [lrange $t 1 end]]; set t [lindex $t 0] }
    if {[string match "\\?\\? ?*" $r]} {
      if {[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" }
    foreach c [channels] {
      if {![channel get $c public]} { continue }
      if {![matchattr $h lomn|lomn $c] || [matchattr $h kZ|k $c]} { continue }
      lappend z $c
    }
    if {$z != ""} {
      lappend o "out: banned '$t' :$r"
      bannick:ban [join [split $t] ,] [join $z ,] $h $r $l
      set g [md5 "ban $t"]
      if {[info procs ircconsole] != ""} { ircconsole * o OUT "$h BAN '$t' :$r" $g }
    } else { lappend o "out: no +public channels found where you have access to ban on." }
  }
  return $o
}


####################################################
# set info for script.tcl
####################################################
set ::scriptdb(out) {
  "provides out command, bans users from all +public channels"
}

