#by wiebe @ QuakeNet



#help stick pub

bind pubm mn|mn "% ${botnet-nick} help stick" c_stick:help:pub

proc c_stick:help:pub { nick uhost handle chan text } {
  lappend output "Usage: stick <ban1> \[<ban2> .. <banN>\]"
  lappend output "Makes the specified channel bans 'sticky', meaning the bans will always be active on the channel, until they expire or set back to normal with unstick."
  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 stick msg

bind msgm mn|mn "help stick" c_stick:help:msg

proc c_stick:help:msg { nick uhost handle text } {
  lappend output "Usage: stick <channel> <ban1> \[<ban2> .. <banN>\]"
  lappend output "Makes the specified channel bans 'sticky', meaning the bans will always be active on the channel, until they expire or set back to normal with unstick."
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  putcmdlog "($nick!$uhost) !$handle! $text"
}



#stick pub
bind pubm mn|mn "% ${botnet-nick} stick" c_stick:pub
bind pubm mn|mn "% ${botnet-nick} stick *" c_stick:pub

proc c_stick:pub { nick uhost handle chan text } {
  set masks [join [lrange [split $text] 2 end]]
  if { [string equal $masks ""] } {
    lappend output "Usage: stick <ban1> \[<ban2> .. <banN>\]"
  } else {
    set globalb ""
    set noban ""
    set done ""
    set already ""
    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] } {
            stick $mask $chan
            lappend done $mask

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

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

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

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

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

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

#channel ban is already sticky
        } else {
          lappend already $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 $already ""] } {
      lappend output "Already sticky on $chan: $already"
    }
    if { ![string equal $globalb ""] } {
      lappend output "Can not make global bans sticky: $globalb"
    }
    if { ![string equal $done ""] } {
      lappend output "Stick 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]]"
}



#stick msg
bind msg mn|mn stick c_stick:msg

proc c_stick: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: stick <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 already ""
    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] } {
            stick $mask $chan
            lappend done $mask

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

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

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

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

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

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

#channel ban is already sticky
        } else {
          lappend already $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 $already ""] } {
      lappend output "Already sticky on $chan: $already"
    }
    if { ![string equal $globalb ""] } {
      lappend output "Can not make global bans sticky: $globalb"
    }
    if { ![string equal $done ""] } {
      lappend output "Stick bans on $chan: $done"
    }
  }
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  return 1
}

