####################################################
# by wiebe @ QuakeNet
#
# can use chase.tcl when available
####################################################

####################################################
# devoice:help:pubm
####################################################
bind pubm vlomn|vlomn "% ${botnet-nick} help devoice" devoice:help:pubm
proc devoice:help:pubm { n u h c t } {
  if {[matchattr $h bkZ]} { return 0 }
  lappend o "devoice: usage devoice \[<chan> .. <chan>\]"
  lappend o "devoice: devoices you on the current or given channels"
  lappend o "devoice: usage devoice \[<chan>\] <nick> \[<nick> .. <nick>\]"
  lappend o "devoice: devoices users on the current or given channel"
  lappend o "devoice: usage devoice \[<chan>\] -host <nick!user@host>"
  lappend o "devoice: devoices users matching mask on the current or given channel"
  lappend o "devoice: usage devoice \[<chan>\] -flag \[<globalflags>\[&|<chanflags>\]\]"
  lappend o "devoice: devoices users with matching flags on the current or given channel"
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "devoice: " } }
  putloglev c $c "help: $n $u $h $c devoice"
  return 1
}


####################################################
# devoice:help:msgm
####################################################
bind msgm vlomn|vlomn "help devoice" devoice:help:msgm
proc devoice:help:msgm { n u h t } {
  if {[matchattr $h bkZ]} { return 0 }
  lappend o "devoice: usage devoice \[<chan> .. <chan>\]"
  lappend o "devoice: devoices you on all or the given channels"
  lappend o "devoice: usage devoice <chan> <nick> \[<nick> .. <nick>\]"
  lappend o "devoice: devoices users on the given channel"
  lappend o "devoice: usage devoice <chan> -host <nick!user@host>"
  lappend o "devoice: devoices users matching mask on the given channel"
  lappend o "devoice: usage devoice <chan> -flag \[<globalflags>\[&|<chanflags>\]\]"
  lappend o "devoice: devoices users with matching flags on the given channel"
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "devoice: " } }
  putcmdlog "($n!$u) !$h! help devoice"
  return 1
}


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


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


####################################################
# devoice:dcc
####################################################
bind dcc -|- devoice2 devoice:dcc
proc devoice:dcc { h i t } {
  set o [devoice:devoice $h $t]
  foreach l $o { putidx $i $l }
  return 1
}


####################################################
# devoice:devoice
####################################################
proc devoice:devoice { h t {n ""} {c ""} } {
  set t [split $t]
# pub
  if {![string equal $c ""]} {
    set v [lindex $t 0]
# devoice user on chan $c 3
    if {[string equal $v ""]} {
      set o [devoice:devoice3 $h $n $c]
# devoice host on chan $c 4
    } elseif {[string equal -nocase $v "-host"]} {
      set o [devoice:devoice4 $h $c [lrange $t 1 end]]
# devoice flag on chan $c 5
    } elseif {[string equal -nocase $v "-flag"]} {
      set o [devoice:devoice5 $h $c [lrange $t 1 end]]
# change chan
    } elseif {[string match {[#&]*} $v]} {
      set c $v; set v [lindex $t 1]
# devoice user on given chans 2
      if {[string equal $v ""] || [string match {[#&]*} $v]} {
        set o [devoice:devoice2 $h $t $n]
# devoice host on chan $c 4
      } elseif {[string equal -nocase $v "-host"]} {
        set o [devoice:devoice4 $h $c [lrange $t 2 end]]
# devoice flag on chan $c 5
      } elseif {[string equal -nocase $v "-flag"]} {
        set o [devoice:devoice5 $h $c [lrange $t 2 end]]
# devoice users on chan $c 1
      } else { set o [devoice:devoice1 $h $c [lrange $t 1 end]] }
# devoice users on chan $c 1
    } else { set o [devoice:devoice1 $h $c $t] }

# msg
  } elseif {![string equal $n ""]} {
    set c [lindex $t 0]; set v [lindex $t 1]
# devoice user on all chans 3
    if {[string equal $c ""]} {
      set o [devoice:devoice3 $h $n]
# devoice user on given chans 2
    } elseif {[string equal $v ""] || [string match {[#&]*} $v]} {
      set o [devoice:devoice2 $h $t $n]
# devoice host on chan $c 4
    } elseif {[string equal -nocase $v "-host"]} {
      set o [devoice:devoice4 $h $c [lrange $t 2 end]]
# devoice flag on chan $c 5
    } elseif {[string equal -nocase $v "-flag"]} {
      set o [devoice:devoice5 $h $c [lrange $t 2 end]]
# devoice users on chan $c 1
    } else { set o [devoice:devoice1 $h $c [lrange $t 1 end]] }

# dcc
  } else {
    set c [lindex $t 0]; set v [lindex $t 1]
# devoice handle on all chans 3
    if {[string equal $c ""]} {
      set o [devoice:devoice3 $h]
# devoice handle on given chans 2
    } elseif {[string equal $v ""] || [string match {[#&]*} $v]} {
      set o [devoice:devoice2 $h $t]
# devoice host on chan $c 4
    } elseif {[string equal -nocase $v "-host"]} {
      set o [devoice:devoice4 $h $c [lrange $t 2 end]]
# devoice flag on chan $c 5
    } elseif {[string equal -nocase $v "-flag"]} {
      set o [devoice:devoice5 $h $c [lrange $t 2 end]]
# devoice users on chan $c 1
    } else { set o [devoice:devoice1 $h $c [lrange $t 1 end]] }
  }
  return $o
}


####################################################
# devoice:devoice1 devoice other users
####################################################
proc devoice:devoice1 { h c t } {
  if {![validchan $c] && ![string equal [info procs whichchan] ""]} { set c [whichchan $h $c] }
  if {![validchan $c] || ![matchattr $h vlomn|vlomn $c]} {
    lappend o "devoice: no access or unknown channel $c"
  } elseif {![botonchan $c]} {
    lappend o "devoice: unable to devoice on $c (I am not there)"
  } elseif {![botisop $c] && ![botishalfop $c]} {
    lappend o "devoice: unable to devoice on $c (I am not opped)"
  } elseif {![matchattr $h mn|mn $c]} {
    set f ""; set s ""
    foreach n $t {
      if {![onchan $n $c]} {
        if {![string equal [info procs chase] ""]} { set d [chase $n] }
        if {![string equal $d ""]} { set n $d }
      }
      if {![onchan $n $c]} { lappend f "'$n not onchan'"; continue }
      if {[onchansplit $n $c]} { lappend f "'$n split'"; continue }
      if {![isvoice $n $c]} { lappend f "'$n not voiced'"; continue }
      if {![string equal -nocase $h [nick2hand $n $c]]} { lappend f "'$n not you'"; continue }
      pushmode $c -v $n; lappend s $n
    }
    if {![string equal $s ""]} { lappend o "devoice: devoiced on $c '[join $s "   "]'" }
    if {![string equal $f ""]} { lappend o "devoice: failed to devoice on $c [join $f "   "]" }
  } else {
    set f ""; set s ""
    foreach n $t {
      if {[string equal $n ""]} { continue }
      if {![onchan $n $c]} {
        if {![string equal [info procs chase] ""]} { set d [chase $n] }
        if {![string equal $d ""]} { set n $d }
      }
      if {![onchan $n $c]} { lappend f "'$n not onchan'"; continue }
      if {[onchansplit $n $c]} { lappend f "'$n split'"; continue }
      if {![isvoice $n $c]} { lappend f "'$n not voiced'"; continue }
      pushmode $c -v $n; lappend s $n
    }
    if {![string equal $s ""]} { lappend o "devoice: devoiced on $c '[join $s "   "]'" }
    if {![string equal $f ""]} { lappend o "devoice: failed to devoice on $c [join $f "   "]" }
  }
  return $o
}


####################################################
# devoice:devoice2 devoice user on given channels
####################################################
proc devoice:devoice2 { h t {n ""} } {
  set f ""; set s ""
  if {[string equal $n ""]} {
    foreach c $t {
      if {![validchan $c] && ![string equal [info procs whichchan] ""]} { set c [whichchan $h $c] }
      if {![validchan $c] || ![matchattr $h vlomn|vlomn $c]} {
        lappend f "'$c no access'"; continue
      }
      if {![botonchan $c]} { lappend f "'$c I am not there'"; continue }
      if {![botisop $c] && ![botishalfop $c]} { lappend f "'$c I am not opped'"; continue }
      if {![handonchan $h $c]} { lappend f "'$c you are not there'"; continue }
      lappend s $c
      foreach n [chanlist $c] {
        if {![string equal -nocase $h [nick2hand $n $c]]} { continue }
        if {[onchansplit $n $c]} { continue }
        if {![isvoice $n $c]} { continue }
        pushmode $c -v $n
      }
    }
    if {![string equal $s ""]} { lappend o "devoice: devoiced you on '[join $s "   "]'" }
    if {![string equal $f ""]} { lappend o "devoice: failed to devoice you on [join $f "   "]" }
  } else {
    foreach c $t {
      if {![validchan $c] && ![string equal [info procs whichchan] ""]} { set c [whichchan $h $c] }
      if {[string equal $c ""]} { continue }
      if {![validchan $c] || ![matchattr $h vlomn|vlomn $c]} {
        lappend f "'$c no access'"; continue
      }
      if {![botonchan $c]} { lappend f "'$c I am not there'"; continue }
      if {![botisop $c] && ![botishalfop $c]} { lappend f "'$c I am not opped'"; continue }
      set m [lindex [split [getchanmode $c]] 0]
      if {![onchan $n $c] || [onchansplit $n $c]} {
        if {[string match "*d*" $m]} { puthelp "MODE $c -v $n"; lappend s $c
        } else { lappend f "'$c you are not there'" }
        continue
      }
      pushmode $c -v $n; lappend s $c
    }
    if {![string equal $s ""]} { lappend o "devoice: devoiced you on [join $s "   "]" }
    if {![string equal $f ""]} { lappend o "devoice: failed to devoice you on [join $f "   "]" }
  }
  return $o
}


####################################################
# devoice:devoice3 devoice user on all channels
####################################################
proc devoice:devoice3 { h {n ""} {c ""} } {
  if {![validchan $c] && ![string equal [info procs whichchan] ""]} { set c [whichchan $h $c] }
  set o ""
# pub
  if {![string equal $c ""] && ![string equal $n ""]} {
    if {![validchan $c] || ![matchattr $h vlomn|vlomn $c]} {
      lappend o "devoice: no access or unknown channel $c"
    } elseif {![botonchan $c]} {
      lappend o "devoice: unable to devoice $c (I am not there)"
    } elseif {![botisop $c] && ![botishalfop $c]} {
      lappend o "devoice: unable to devoice $c (I am not opped)"
    } elseif {![onchan $n $c]} {
      lappend o "devoice: unable to devoice $c (you are not there)"
    } elseif {[onchansplit $n $c]} {
      lappend o "devoice: unable to devoice $c (you are split)"
    } elseif {![isvoice $n $c]} {
      lappend o "devoice: unable to devoice $c (you are devoiced)"
    } else { pushmode $c -v $n }
# msg
  } elseif {![string equal $n ""]} {
    set s ""
    foreach c [channels] {
      if {![botisop $c] && ![botishalfop $c]} { continue }
      if {![isvoice $n $c]} { continue }
      if {![onchan $n $c]} { continue }
      if {[onchansplit $n $c]} { continue }
      if {![matchattr $h vlomn|vlomn $c]} { continue }
      if {[matchattr $h Zkq|kq $c]} { continue }
      pushmode $c -v $n; lappend s $c
    }
    if {[string equal $s ""]} { lappend o "devoice: no channels found to devoice you on"
    } else { lappend o "devoice: devoiced you on [join $s "   "]" }
# dcc
  } else {
    set s ""
    foreach c [channels] {
      if {![botisop $c] && ![botishalfop $c]} { continue }
      if {![handonchan $h $c]} { continue }
      if {![matchattr $h vlomn|vlomn $c]} { continue }
      if {[matchattr $h Zkq|kq $c]} { continue }
      lappend s $c
      foreach n [chanlist $c] {
        if {![string equal -nocase $h [nick2hand $n $c]]} { continue }
        if {![isvoice $n $c]} { continue }
        if {[onchansplit $n $c]} { continue }
        pushmode $c -v $n
      }
    }
    if {[string equal $s ""]} { lappend o "devoice: no channels found to devoice you on"
    } else { lappend o "devoice: devoiced you on [join $s "   "]" }
  }
  return $o
}


####################################################
# devoice:devoice4 devoice host t on c
####################################################
# 4 t = #chan -host mask (devoice users matching mask on given chan)
proc devoice:devoice4 { h c t } {
  if {![validchan $c] && ![string equal [info procs whichchan] ""]} { set c [whichchan $h $c] }
  set m [lindex $t 0]; set z 0; regsub -all {[][\\]} $m {\\\0} m
  if {![validchan $c] || ![matchattr $h vlomn|vlomn $c]} {
    lappend o "devoice: no access or unknown channel $c"
  } elseif {![botonchan $c]} {
    lappend o "devoice: unable to devoice on $c (I am not there)"
  } elseif {![botisop $c] && ![botishalfop $c]} {
    lappend o "devoice: unable to devoice on $c (I am not opped)"
  } elseif {![matchattr $h mn|mn $c]} {
    lappend o "devoice: need to be master or owner to devoice based on host"
  } elseif {[string equal $m ""]} {
    lappend o "devoice: usage -host <nick!user@host>"
  } else {
    foreach n [chanlist $c] {
      if {![isvoice $n $c]} { continue }
      if {[onchansplit $n $c]} { continue }
      if {![string match -nocase $m $n![getchanhost $n $c]]} { continue }
      pushmode $c -v $n; incr z
    }
    set m [lindex $t 0]
    if {!$z} {
      lappend o "devoice: no users to devoice matching '$m' on $c with at least +f"
    } else { lappend o "devoice: devoiced $z users matching '$m' on $c with at least +f" }
  }
  return $o
}


####################################################
# devoice:devoice5 devoice flag t on c
####################################################
# 5 t -|- (devoice user with the given flags)
proc devoice:devoice5 { h c t } {
  if {![validchan $c] && ![string equal [info procs whichchan] ""]} { set c [whichchan $h $c] }
  set x [lindex $t 0]; set z 0
  if {![validchan $c] || ![matchattr $h vlomn|vlomn $c]} {
    lappend o "devoice: no access or unknown channel $c"
  } elseif {![botonchan $c]} {
    lappend o "devoice: unable to devoice on $c (I am not there)"
  } elseif {![botisop $c] && ![botishalfop $c]} {
    lappend o "devoice: unable to devoice on $c (I am not opped)"
  } elseif {![matchattr $h mn|mn $c]} {
    lappend o "devoice: need to be master or owner to devoice based on flags"
  } elseif {[string equal $x ""]} {
    lappend o "devoice: usage -flag \[<globalflags>\[&|<chanflags>\]\]"
    lappend o "devoice: match users the given flags, the & means AND, the | means OR, - and + can also be used."
  } else {
    foreach n [chanlist $c $x] {
      if {![isvoice $n $c]} { continue }
      if {[onchansplit $n $c]} { continue }
      pushmode $c -v $n; incr z
    }
    if {!$z} {
      lappend o "devoice: no users to devoice with flags matching '$x' on $c"
    } else { lappend o "devoice: devoiced $z users with flags matching '$x' on $c" }
  }
  return $o
}


set scriptdb(devoice) {
  "provides devoice command"
}

