#by wiebe @ QuakeNet



#help deop pub
bind pubm omn|omn "% ${botnet-nick} help deop" c_deop:help:pub

proc c_deop:help:pub { nick uhost handle chan text } {
  lappend output "Usage: deop \[<nick1> <nick2> .. <nickN>\]"
  lappend output "Deops you on the channel. Flag +m or +n is needed to deop other users."
  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 deop msg
bind msgm omn|omn "help deop" c_deop:help:msg

proc c_deop:help:msg { nick uhost handle text } {
  lappend output "Usage: deop <channel> \[<nick1> <nick2> .. <nickN>\]"
  lappend output "Deops you on the channel. Flag +m or +n is needed to deop other users."
  lappend output "Usage: deop \[<channel1> <channel2> .. <channelN>\]"
  lappend output "Deops you on all or the given channels."
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  putcmdlog "($nick!$uhost) !$handle! $text"
}



#deop self
bind pubm omn|omn "% ${botnet-nick} deop" c_deop:pub:self

proc c_deop:pub:self { nick uhost handle chan text } {
  if { ![botisop $chan] } {
    lappend output "I am not a channel operator on $chan"
  } elseif { ![isop $nick $chan] } {
    lappend output "You are not opped on $chan"
  } else {

#check flag a|a? +protectops? +autoop?
    lappend output "Deopped you on $chan"
    pushmode $chan -o $nick
  }
  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]]"
}


#deop other
bind pubm omn|omn "% ${botnet-nick} deop *" c_deop:pub:deopother

proc c_deop:pub:deopother { nick uhost handle chan text } {
  set targets [lrange [split $text] 2 end]
  if { ![botisop $chan] } {
    lappend output "I am not a channel operator on $chan"
  } elseif { [matchattr $handle o|o $chan] && ![matchattr $handle mn|mn $chan] } {
    lappend output "No access to deop other users on $chan (need +m or +n flag)"
  } elseif { [string equal $targets ""] } {
    lappend output "Usage: deop \[<nick1> <nick2> .. <nickN>\]"
  } else {
    set output [c_deop:deop:others $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]]"
}


#deop msg
bind msg omn|omn deop c_deop:msg

proc c_deop:msg { nick uhost handle text } {
  set chan [lindex [split $text] 0]
  set targets [lrange [split $text] 1 end]

#nothing giving, or invalid chan, or no access, other check
  if { [string equal $text ""] || ![validchan $chan] || ![matchattr $handle mn|mn $chan] || [c_deop:nickorchan "$text"] } {
    if { [string equal $text ""] } { set text [channels] }
    set output [c_deop:deop:self $nick $handle $text]
  } elseif { ![botisop $chan] } {
     lappend output "I am not a channel operator on $chan"
  } elseif { ![string equal $targets ""] && [matchattr $handle mn|mn $chan] } {
    set output [c_deop:deop:others $chan $targets]
  }
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  return 1
}



#deop user one all or given channels
proc c_deop:deop:self { nick handle channels } {
  set deopchan ""
  foreach chan [split $channels] {
    if { [validchan $chan] && [botisop $chan] && [isop $nick $chan] && [matchattr $handle omn|omn $chan] } {
      lappend deopchan $chan

#check flag a|a? +protectops? +autoop?
      pushmode $chan -o $nick
    }
  }
  if { [string equal $deopchan ""] } {
    lappend output "Found no channels to deop you on."
  } else {
    lappend output "Deopped you on: [join $deopchan]"
  }
  return $output
}



#deop targets
proc c_deop:deop:others { chan targets } {
  foreach t [split $targets] {

#check flag a|a? +protectops? +autoop?
    if { [isop $t $chan] } {
      pushmode $chan -o $t
    }
  }
  lappend output "Deopped other users on $chan"
  return $output
}



#returns if "chan1 chan2 chanN" is used or "chan nick1 nick2 nickN", returns 1 for chans
proc c_deop:nickorchan { targets } {
  if { [string equal $targets ""] } { return 1 }
  if { [string equal [llength $targets] 1] } { return 1 }
  set chanc 0; set nickc 0
  set chan [lindex [split $targets] 0]
  set targets [join [lrange [split $targets] 1 end]]
  foreach t [split $targets] {
    if { [validchan $t] || ![onchan $t $chan] || ![isop $t $chan] || [string match {[#&]*} $t] } {
      incr chanc 1
    } elseif { [isop $t $chan] } { incr nickc 1 }
  }
  if { $chanc >= $nickc } { return 1 } else { return 0 }
}

