#by wiebe @ QuakeNet



#help devoice pub
bind pubm vlomn|vlomn "% ${botnet-nick} help devoice" c_devoice:help:pub

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

proc c_devoice:help:msg { nick uhost handle text } {
  lappend output "Usage: devoice <channel> \[<nick1> <nick2> .. <nickN>\]"
  lappend output "Devoices you on the channel. Flag +m or +n is needed to devoice other users."
  lappend output "Usage: devoice \[<channel1> <channel2> .. <channelN>\]"
  lappend output "Devoices 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"
}



#devoice self
bind pubm vlomn|vlomn "% ${botnet-nick} devoice" c_devoice:pub:self

proc c_devoice:pub:self { nick uhost handle chan text } {
  if { ![botisop $chan] } {
    lappend output "I am not a channel operator on $chan"
  } elseif { ![isvoice $nick $chan] } {
    lappend output "You are not voiced on $chan"
  } else {
    lappend output "Devoiced you on $chan"
    pushmode $chan -v $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]]"
}


#devoice other
bind pubm vlomn|vlomn "% ${botnet-nick} devoice *" c_devoice:pub:devoiceother

proc c_devoice:pub:devoiceother { 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 vlo|vlo $chan] && ![matchattr $handle mn|mn $chan] } {
    lappend output "No access to devoice other users on $chan (need +m or +n flag)"
  } elseif { [string equal $targets ""] } {
    lappend output "Usage: devoice \[<nick1> <nick2> .. <nickN>\]"
  } else {
    set output [c_devoice:devoice: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]]"
}


#devoice msg
bind msg vlomn|vlomn devoice c_devoice:msg

proc c_devoice: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_devoice:nickorchan "$text"] } {
    if { [string equal $text ""] } { set text [channels] }
    set output [c_devoice:devoice: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_devoice:devoice:others $chan $targets]
  }
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  return 1
}



#devoice user one all or given channels
proc c_devoice:devoice:self { nick handle channels } {
  set devoicechan ""
  foreach chan [split $channels] {
    if { [validchan $chan] && [botisop $chan] && [isvoice $nick $chan] && ![ishalfop $nick $chan] && ![isop $nick $chan] && [matchattr $handle vlomn|vlomn $chan] } {
      lappend devoicechan $chan
      pushmode $chan -v $nick
    }
  }
  if { [string equal $devoicechan ""] } {
    lappend output "Found no channels to devoice you on."
  } else {
    lappend output "Devoiced you on: [join $devoicechan]"
  }
  return $output
}



#devoice targets
proc c_devoice:devoice:others { chan targets } {
  foreach t [split $targets] {
    if { [isvoice $t $chan] && ![ishalfop $t $chan] && ![isop $t $chan] } {

#check +autovoice? +g flags?
      pushmode $chan -v $t
    }
  }
  lappend output "Devoiced other users on $chan"
  return $output
}



#returns if "chan1 chan2 chanN" is used or "chan nick1 nick2 nickN", returns 1 for chans
proc c_devoice: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] || ![isvoice $t $chan] || [ishalfop $t $chan] || [isop $t $chan] || [string match {[#&]*} $t] } {
      incr chanc 1
    } elseif { [isvoice $t $chan] } { incr nickc 1 }
  }
  if { $chanc >= $nickc } { return 1 } else { return 0 }
}

