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

####################################################
# deop:help:pubm
####################################################
bind pubm omn|omn "% ${botnet-nick} help deop" deop:help:pubm
proc deop:help:pubm { n u h c t } {
  if {[matchattr $h bkZ]} { return 0 }
  lappend o "deop: usage deop \[<chan> .. <chan>\]"
  lappend o "deop: deops you on the current or given channels"
  lappend o "deop: usage deop \[<chan>\] <nick> \[<nick> .. <nick>\]"
  lappend o "deop: deops users on the current or given channel"
  lappend o "deop: usage deop \[<chan>\] -host <nick!user@host>"
  lappend o "deop: deops users matching mask on the current or given channel"
  lappend o "deop: usage deop \[<chan>\] -flag \[<globalflags>\[&|<chanflags>\]\]"
  lappend o "deop: deops 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 "deop: " } }
  putloglev c $c "help: $n $u $h $c deop"
  return 1
}


####################################################
# deop:help:msgm
####################################################
bind msgm omn|omn "help deop" deop:help:msgm
proc deop:help:msgm { n u h t } {
  if {[matchattr $h bkZ]} { return 0 }
  lappend o "deop: usage deop \[<chan> .. <chan>\]"
  lappend o "deop: deops you on all or the given channels"
  lappend o "deop: usage deop <chan> <nick> \[<nick> .. <nick>\]"
  lappend o "deop: deops users on the given channel"
  lappend o "deop: usage deop <chan> -host <nick!user@host>"
  lappend o "deop: deops users matching mask on the given channel"
  lappend o "deop: usage deop <chan> -flag \[<globalflags>\[&|<chanflags>\]\]"
  lappend o "deop: deops 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 "deop: " } }
  putcmdlog "($n!$u) !$h! help deop"
  return 1
}


####################################################
# deop:pubm
####################################################
bind pubm omn|omn "% ${botnet-nick} deop" deop:pubm
bind pubm omn|omn "% ${botnet-nick} deop *" deop:pubm
proc deop: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 [deop:deop $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 "deop: " } }
  putloglev c $c "deop: $n $u $h $c $t"
  return 1
}


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


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


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

# msg
  } elseif {![string equal $n ""]} {
    set c [lindex $t 0]; set v [lindex $t 1]
# deop user on all chans 3
    if {[string equal $c ""]} {
      set o [deop:deop3 $h $n]
# deop user on given chans 2
    } elseif {[string equal $v ""] || [string match {[#&]*} $v]} {
      set o [deop:deop2 $h $t $n]
# deop host on chan $c 4
    } elseif {[string equal -nocase $v "-host"]} {
      set o [deop:deop4 $h $c [lrange $t 2 end]]
# deop flag on chan $c 5
    } elseif {[string equal -nocase $v "-flag"]} {
      set o [deop:deop5 $h $c [lrange $t 2 end]]
# deop users on chan $c 1
    } else { set o [deop:deop1 $h $c [lrange $t 1 end]] }

# dcc
  } else {
    set c [lindex $t 0]; set v [lindex $t 1]
# deop handle on all chans 3
    if {[string equal $c ""]} {
      set o [deop:deop3 $h]
# deop handle on given chans 2
    } elseif {[string equal $v ""] || [string match {[#&]*} $v]} {
      set o [deop:deop2 $h $t]
# deop host on chan $c 4
    } elseif {[string equal -nocase $v "-host"]} {
      set o [deop:deop4 $h $c [lrange $t 2 end]]
# deop flag on chan $c 5
    } elseif {[string equal -nocase $v "-flag"]} {
      set o [deop:deop5 $h $c [lrange $t 2 end]]
# deop users on chan $c 1
    } else { set o [deop:deop1 $h $c [lrange $t 1 end]] }
  }
  return $o
}


####################################################
# deop:deop1 deop other users
####################################################
proc deop:deop1 { h c t } {
  if {![validchan $c] && ![string equal [info procs whichchan] ""]} { set c [whichchan $h $c] }
  if {![validchan $c] || ![matchattr $h omn|omn $c]} {
    lappend o "deop: no access or unknown channel $c"
  } elseif {![botonchan $c]} {
    lappend o "deop: unable to deop on $c (I am not there)"
  } elseif {![botisop $c]} {
    lappend o "deop: unable to deop 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 {![isop $n $c]} { lappend f "'$n is not opped'"; continue }
      if {![string equal -nocase $h [nick2hand $n $c]]} { lappend f "'$n not you'"; continue }
      pushmode $c -o $n; lappend s $n
    }
    if {![string equal $s ""]} { lappend o "deop: deopped on $c '[join $s "   "]'" }
    if {![string equal $f ""]} { lappend o "deop: failed to deop 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 {![isop $n $c]} { lappend f "'$n is not opped'"; continue }
      if {[isbotnick $n]} { lappend f "'$n is me'"; continue }
      set u [nick2hand $n $c]
      if {[matchattr $u b] && [matchattr $u o|o $c]} { lappend f "'$n is a bot'"; continue }
      pushmode $c -o $n; lappend s $n
    }
    if {![string equal $s ""]} { lappend o "deop: deopped on $c '[join $s "   "]'" }
    if {![string equal $f ""]} { lappend o "deop: failed to deop on $c [join $f "   "]" }
  }
  return $o
}


####################################################
# deop:deop2 deop user on given channels
####################################################
proc deop:deop2 { 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 omn|omn $c]} {
        lappend f "'$c no access'"; continue
      }
      if {![botonchan $c]} { lappend f "'$c I am not there'"; continue }
      if {![botisop $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 {![isop $n $c]} { continue }
        pushmode $c -o $n
      }
    }
    if {![string equal $s ""]} { lappend o "deop: deopped you on '[join $s "   "]'" }
    if {![string equal $f ""]} { lappend o "deop: failed to deop 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 omn|omn $c]} {
        lappend f "'$c no access'"; continue
      }
      if {![botonchan $c]} { lappend f "'$c I am not there'"; continue }
      if {![botisop $c]} { lappend f "'$c I am not opped'"; continue }
      if {![onchan $n $c] || [onchansplit $n $c]} {
        lappend f "'$c you are not there'"; continue
      }
      if {![isop $n $c]} { lappend f "'$c you are not opped'"; continue }
      pushmode $c -o $n; lappend s $c
    }
    if {![string equal $s ""]} { lappend o "deop: deopped you on [join $s "   "]" }
    if {![string equal $f ""]} { lappend o "deop: failed to deop you on [join $f "   "]" }
  }
  return $o
}


####################################################
# deop:deop3 deop user on all channels
####################################################
proc deop:deop3 { 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 omn|omn $c]} {
      lappend o "deop: no access or unknown channel $c"
    } elseif {![botonchan $c]} {
      lappend o "deop: unable to deop $c (I am not there)"
    } elseif {![botisop $c]} {
      lappend o "deop: unable to deop $c (I am not opped)"
    } elseif {![onchan $n $c]} {
      lappend o "deop: unable to deop $c (you are not there)"
    } elseif {[onchansplit $n $c]} {
      lappend o "deop: unable to deop $c (you are split)"
    } elseif {![isop $n $c]} {
      lappend o "deop: unable to deop $c (you are not opped)"
    } else { pushmode $c -o $n }
# msg
  } elseif {![string equal $n ""]} {
    set s ""
    foreach c [channels] {
      if {![botisop $c]} { continue }
      if {![isop $n $c]} { continue }
      if {[onchansplit $n $c]} { continue }
      if {![matchattr $h omn|omn $c]} { continue }
      if {[matchattr $h Zkd|kd $c]} { continue }
      pushmode $c -o $n; lappend s $c
    }
    if {[string equal $s ""]} { lappend o "deop: no channels found to deop you on"
    } else { lappend o "deop: deopped you on [join $s "   "]" }
# dcc
  } else {
    set s ""
    foreach c [channels] {
      if {![botisop $c]} { continue }
      if {![handonchan $h $c]} { continue }
      if {![matchattr $h omn|omn $c]} { continue }
      lappend s $c
      foreach n [chanlist $c] {
        if {![string equal -nocase $h [nick2hand $n $c]]} { continue }
        if {![isop $n $c]} { continue }
        if {[onchansplit $n $c]} { continue }
        pushmode $c -o $n
      }
    }
    if {[string equal $s ""]} { lappend o "deop: no channels found to deop you on"
    } else { lappend o "deop: deopped you on [join $s "   "]" }
  }
  return $o
}


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


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


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

