####################################################
# by wiebe @ QuakeNet
#
####################################################

####################################################
# clearmode:help:pubm
####################################################
bind pubm mn|mn "% ${botnet-nick} help clearmode" clearmode:help:pubm
proc clearmode:help:pubm { n u h c t } {
  if {![validchan $c]} { return 0 }
  if {[matchattr $h bkZ]} { return 0 }
  lappend o "clearmode: usage clearmode \[<chan>\] <modes>"
  lappend o "clearmode: clears modes on the current or given channel, use '-all' to clear all modes"
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "clearmode: " } }
  putloglev c $c "help: $n $u $h $c clearmode"
  return 1
}


####################################################
# clearmode:help:msgm
####################################################
bind msgm mn|mn "help clearmode" clearmode:help:msgm
proc clearmode:help:msgm { n u h t } {
  if {[matchattr $h bkZ]} { return 0 }
  lappend o "clearmode: usage clearmode <chan> <modes>"
  lappend o "clearmode: clears modes on the given channel, use '-all' to clear all modes"
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "clearmode: " } }
  putcmdlog "($n!$u) !$h! help clearmode"
  return 1
}


####################################################
# clearmode:pubm
####################################################
bind pubm mn|mn "% ${botnet-nick} clearmode" clearmode:pubm
bind pubm mn|mn "% ${botnet-nick} clearmode *" clearmode:pubm
proc clearmode: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 {[string equal $t ""]} {
    lappend o "clearmode: usage clearmode \[<chan>\] <modes>"
  } else {
    set o [clearmode:clearmode $h $t $c]
  }
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "clearmode: " } }
  putloglev c $c "clearmode: $n $u $h $c $t"
  return 1
}


####################################################
# clearmode:msg
####################################################
bind msg mn|mn clearmode clearmode:msg
proc clearmode:msg { n u h t } {
  if {[matchattr $h bkZ]} { return 0 }
  if {[string equal $t ""]} { lappend o "clearmode: usage clearmode <chan> <modes>"
  } else { set o [clearmode:clearmode $h $t] }
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "clearmode: " } }
  return 1
}


####################################################
# clearmode:dcc
####################################################
bind dcc -|- clearmode clearmode:dcc
proc clearmode:dcc { h i t } {
  if {[string equal $t ""]} {
    lappend o "clearmode: usage clearmode \[<chan>\] <modes>"
    lappend o "clearmode: clears modes on the console or given channel, use '-all' to clear all modes"
  } else {
    set c [lindex [split [console $i]] 0]
    if {![validchan $c]} { set c "" }
    set o [clearmode:clearmode $h $t $c]
  }
  foreach l $o { putidx $i $l }
  return 1
}


####################################################
# clearmode:clearmode
####################################################
proc clearmode:clearmode { h t {c ""} } {
  set t [split $t]; set d [lindex $t 0]; set m [lindex $t 1]
  if {[string match {[#&]*} $d]} { set c $d } else { set m $d }
  if {![validchan $c] && ![string equal [info procs whichchan] ""]} { set c [whichchan $h $c] }
  if {[string equal $c ""]} {
    lappend o "clearmode: usage clearmode \[<chan>\] <modes>"
  } elseif {![validchan $c] || ![matchattr $h mn|mn $c]} {
    lappend o "clearmode: no access or unknown channel $c"
    lappend o "clearmode: usage clearmode \[<chan>\] <modes>"
  } elseif {[string equal $c ""] || [string equal $m ""]} {
    lappend o "clearmode: usage clearmode \[<chan>\] <modes>"
# need priv OPMODE, command needs to be enabled
# need to use isupport.tcl
  } elseif {![string equal [info procs umode] ""] && [string match *o* [umode]]} {
    if {[string equal -nocase $m "-all"]} {
      putserv "CLEARMODE $c bovklmirspcCNMT"; lappend o "clearmode: cleared all modes on $c"
      if {![string equal [info procs ircconsole] ""]} { ircconsole $c m CLEARMODE "$h clears all modes" }
    } else {
      putserv "CLEARMODE $c $m"; lappend o "clearmode: cleared modes on $c '$m'"
      if {![string equal [info procs ircconsole] ""]} { ircconsole $c m CLEARMODE "$h clears modes '$m'" }
    }
  } elseif {![botonchan $c]} {
    lappend o "clearmode: unable to clear modes on $c (I am not there)"
  } elseif {![botisop $c] && ![botishalfop $c]} {
    lappend o "clearmode: unable to clear modes on $c (I am not opped)"
  } else {
    set d ""; set f ""; set s ""; set x 0
    set z [string range [lindex [split [getchanmode $c]] 0] 1 end]
    if {[string equal -nocase $m "-all"]} { set x 1; set m "ohvb$z" }
    foreach a [split $m ""] {
      if {![string equal [lsearch -exact $d $a] "-1"]} { continue }
      lappend d $a
      if {![botisop $c] && [string match {[oh]} $a]} {
        lappend f "'$a I am not opped'"
      } elseif {[string equal $a "d"]} { lappend f "'$a cannot clear this mode'"
# op
      } elseif {[string equal $a "o"]} {
        foreach n [chanlist $c] {
          if {![isop $n $c]} { continue }
# let's not trigger all sorts of anti takeover protection
          if {[matchattr [nick2hand $n $c] bomn|omn $c]} { continue }
          if {[isbotnick $n]} { continue }
          pushmode $c -o $n
        }
        lappend s "ops"
# halfop
      } elseif {[string equal $a "h"]} {
        foreach n [chanlist $c] { if {[ishalfop $n $c]} { pushmode $c -h $n } }
        lappend s "halfops"
# voice
      } elseif {[string equal $a "v"]} {
        foreach n [chanlist $c] { if {[isvoice $n $c]} { pushmode $c -v $n } }
        lappend s "voices"
# bans
      } elseif {[string equal $a "b"]} {
        set y [chanbans $c]; set l [llength $y]
        if {[string equal $l "0"]} { lappend f "'$a no bans set'"
        } elseif {$l > 6} { putserv "MODE $c +b-b * *"; lappend s "bans"
        } else { foreach b $y { pushmode $c -b [lindex $b 0] }; lappend s "bans" }
# rest, not set
      } elseif {![string match "*$a*" $z]} { lappend f "'$a mode not set'"
# key
      } elseif {[string equal $a "k"]} {
        set k [clearmode:key $c]
        if {[string equal $k ""]} { lappend f "'$a unable to clear, key not found'"
        } else { pushmode $c -k $k; lappend s "key" }
# rest
      } else { pushmode $c -$a; lappend s "$a" }
    }
    if {$x} { lappend o "clearmode: cleared all modes on $c"
      if {![string equal [info procs ircconsole] ""]} {
        ircconsole $c m CLEARMODE "$h clears all modes"
      }
    } else {
      if {![string equal $s ""]} {
        lappend o "clearmode: cleared modes on $c '[join $s]'"
        if {![string equal [info procs ircconsole] ""]} {
          ircconsole $c m CLEARMODE "$h clears modes [join $s]"
        }
      }
      if {![string equal $f ""]} {
        lappend o "clearmode: failed to clear modes on $c [join $f "   "]"
      }
    }
  }
  return $o
}


####################################################
# clearmode:key
####################################################
proc clearmode:key { c } {
  set m [split [getchanmode $c]]; set p [lrange $m 1 end]; set m [lindex $m 0]
  if {![string match "*k*" $m]} { return "" }
  if {[string equal [llength $p] "0"]} { return "" }
  if {[string equal [llength $p] "1"]} { return [lindex $p 0] }
  set x 0; set m [split $m ""]
  foreach n $m {
    if {[string equal $n "k"]} { break }
    if {[string equal $n "l"]} { incr x 1 }
  }
  set k [lindex $p $x]
  return $k
}


set scriptdb(clearmode) {
  "provides clearmode command to clear given or all modes on a channel"
}

