#by wiebe @ QuakeNet



#help unstick pub

bind pubm mn|mn "% ${botnet-nick} help unstick" c_unstick:help:pub

proc c_unstick:help:pub { nick uhost handle chan text } {
  lappend output "Usage: unstick <ban1> \[<ban2> .. <banN>\]"
  lappend output "Makes the specified sticky channel bans normal."
  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 unstick msg

bind msgm mn|mn "help unstick" c_unstick:help:msg

proc c_unstick:help:msg { nick uhost handle text } {
  lappend output "Usage: unstick <channel> <ban1> \[<ban2> .. <banN>\]"
  lappend output "Makes the specified sticky channel bans normal."
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  putcmdlog "($nick!$uhost) !$handle! $text"
}



#unstick pub
bind pubm mn|mn "% ${botnet-nick} unstick" c_unstick:pub
bind pubm mn|mn "% ${botnet-nick} unstick *" c_unstick:pub

proc c_unstick:pub { nick uhost handle chan text } {
  set masks [join [lrange [split $text] 2 end]]
  if { [string equal $masks ""] } {
    lappend output "Usage: unstick <ban1> \[<ban2> .. <banN>\]"
  } else {
    set globalb ""
    set noban ""
    set done ""
    set notsticky ""
    foreach mask [split $masks] {
      if { [string match ?*@?* $mask] && ![string match ?*!?*@?* $mask] } {
        set mask *!$mask
      } elseif { [string match *.* $mask] && ![string match ?*!?*@?* $mask] } {
        set mask *!*@$mask
      } elseif { [string match ?*!?* $mask] && ![string match ?*!?*@?* $mask] } {
        set mask $mask@*
      } elseif { ![string match ?*!?*@?* $mask] } {
        set mask $mask!*@*
      }

#thanks to [isban ..], it is not possible to see
#if a ban is both on the chan & global banlist OR only on the global banlist
#so we use this 'little' trick

      if { [isban $mask $chan] } {

#ban is (also?) global
        if { [isban $mask] } {

#channel ban is not sticky, global ban is sticky
          if { ![isbansticky $mask $chan] && [isbansticky $mask] } {
            lappend notsticky $mask

#channel ban is sticky, global ban is not sticky
          } elseif { [isbansticky $mask $chan] && ![isbansticky $mask] } {
            unstick $mask $chan
            lappend done $mask

#both are sticky, unstick the channel ban
          } elseif { [isbansticky $mask $chan] && [isbansticky $mask] } {
            unstick $mask $chan

#check if the global ban is not sticky now, if so, remove it
            if { ![isbansticky $mask] } {
              stick $mask
              lappend globalb $mask

#global ban did not become not sticky, so all is ok
            } else {
              lappend done $mask
            }

#channel ban is already sticky
          } else {
            lappend notsticky $mask
          }

#channel ban only, sticky
        } elseif { [isbansticky $mask $chan] } {
          unstick $mask $chan
          lappend done $mask

#channel ban is already not sticky
        } else {
          lappend notsticky $mask
        }

#no such ban
      } else {
        lappend noban $mask
      }
    }
    if { ![string equal $noban ""] } {
      lappend output "No such ban on the channel ban list on $chan: $noban"
    }
    if { ![string equal $notsticky ""] } {
      lappend output "Not sticky on $chan: $notsticky"
    }
    if { ![string equal $globalb ""] } {
      lappend output "Can not unstick global bans: $globalb"
    }
    if { ![string equal $done ""] } {
      lappend output "Unstick bans on $chan: $done"
    }
  }
  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]]"
}



#unstick msg
bind msg mn|mn unstick c_unstick:msg

proc c_unstick:msg { nick uhost handle text } {
  set chan [lindex [split $text] 0]
  set masks [lrange [split $text] 1 end]
  if { [string equal $chan ""] || [string equal $masks ""] } {
    lappend output "Usage: unstick <channel> <ban1> \[<ban2> .. <banN>\]"
  } elseif { ![validchan $chan] || ![matchattr $handle mn|mn $chan] } {
    lappend output "No access or unknown channel $chan"
  } else {
    set globalb ""
    set noban ""
    set done ""
    set notsticky ""
    foreach mask [split $masks] {
      if { [string match ?*@?* $mask] && ![string match ?*!?*@?* $mask] } {
        set mask *!$mask
      } elseif { [string match *.* $mask] && ![string match ?*!?*@?* $mask] } {
        set mask *!*@$mask
      } elseif { [string match ?*!?* $mask] && ![string match ?*!?*@?* $mask] } {
        set mask $mask@*
      } elseif { ![string match ?*!?*@?* $mask] } {
        set mask $mask!*@*
      }

#thanks to [isban ..], it is not possible to see
#if a ban is both on the chan & global banlist OR only on the global banlist
#so we use this 'little' trick

      if { [isban $mask $chan] } {

#ban is (also?) global
        if { [isban $mask] } {

#channel ban is not sticky, global ban is sticky
          if { ![isbansticky $mask $chan] && [isbansticky $mask] } {
            lappend notsticky $mask

#channel ban is sticky, global ban is not sticky
          } elseif { [isbansticky $mask $chan] && ![isbansticky $mask] } {
            unstick $mask $chan
            lappend done $mask

#both are sticky, unstick the channel ban
          } elseif { [isbansticky $mask $chan] && [isbansticky $mask] } {
            unstick $mask $chan

#check if the global ban is not sticky now, if so, remove it
            if { ![isbansticky $mask] } {
              stick $mask
              lappend globalb $mask

#global ban did not become not sticky, so all is ok
            } else {
              lappend done $mask
            }

#channel ban is already sticky
          } else {
            lappend notsticky $mask
          }

#channel ban only, sticky
        } elseif { [isbansticky $mask $chan] } {
          unstick $mask $chan
          lappend done $mask

#channel ban is already not sticky
        } else {
          lappend notsticky $mask
        }

#no such ban
      } else {
        lappend noban $mask
      }
    }
    if { ![string equal $noban ""] } {
      lappend output "No such ban on the channel ban list on $chan: $noban"
    }
    if { ![string equal $notsticky ""] } {
      lappend output "Not sticky on $chan: $notsticky"
    }
    if { ![string equal $globalb ""] } {
      lappend output "Can not unstick global bans: $globalb"
    }
    if { ![string equal $done ""] } {
      lappend output "Unstick bans on $chan: $done"
    }
  }
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  return 1
}

