Peace & Protection 4.00 — File Browser

PeaceProtection4.00v4.0 / PeaceProtection4.00v4.0 / SCRIPT / PROTECT.MRC

PROTECT.MRC — 27.24 KB — Download this file

; #= P&P -rs
; ########################################
; Peace & Protection
; Protection system (channel)
; ########################################

; new flud

; $_floodc(count,MAX,SEC,var name) (any num words, will be dotted automatically)
; returns totalcount if flood hit (doesn't reset counter)
; > 650 line is a failsafe for if length gets too long, it merges the initial 3 entries
alias _floodc {
  if (($3 <= 0) || ($3 !isnum)) return
  tokenize 32 $1 $2 -u $+ $calc($3 + 2) $calc($ctime - $3) $replace($4-,$chr(32),.) %-- [ $+ [ $replace($4-,$chr(32),.) ] ]
  tokenize 32 1 $1 $2 $3 $4 $5 $6 $+ : $+ $ctime $7 $+ : $+ $1
  :loop | if ($gettok($7,$1,58) < $5) { tokenize 32 $calc($1 + 1) $2 $3 $4 $5 $6 $7 $8 | goto loop }
  tokenize 32 $3 $4 $6 $gettok($7, [ [ $1 ] $+ [ - ] ] ,58) $gettok($8, [ [ $1 ] $+ [ - ] ] ,58)
  if ($len($4) > 650) tokenize 32 $1 $2 $3 $gettok($4,3-,58) $calc($replace($gettok($5,1-3,58),:,+)) $+ : $+ $gettok($5,4-,58)
  set $2 %-- $+ $3 $4 $5
  if ($calc($replace($5,:,+)) >= $1) return $ifmatch
}
; _floodr var name (any num words)
; resets a flood counter
alias _floodr unset %-- $+ $replace($1-,$chr(32),.)

; Word/chan list editing
alias _wcled {
  set %.var $1 | set %.chan $2 | set %.type $3-
  _dialog -am protlist protlist
}
dialog protlist {
  title "Protection"
  size -1 -1 285 235

  text "", 1, 5 5 250 20

  list 2, 5 25 200 150
  list 3, 5 25 200 150, hide disable

  button "&Edit...", 4, 210 25 70 25, default
  button "&Add...", 5, 210 56 70 25
  button "&Delete", 6, 210 87 70 25

  check "U&se global default list", 9, 10 178 200 20

  button "&OK", 10, 5 204 70 25, OK
  button "&Cancel", 11, 135 204 70 25, cancel

  edit "", 12, 1 1 1 1, hide autohs
}
on *:DIALOG:protlist:sclick:10:{
  var %stuff,%num = 1
  if (($did(9).state != 1) || ($gettok($did(12),2,32) == *)) {
    :loop
    if ($did(2,%num)) {
      %stuff = $addtok(%stuff,$remove($ifmatch,$chr(44)),44)
      inc %num | goto loop
    }
  }
  `set $gettok($did(12),1,32) $+ $gettok($did(12),2,32) %stuff
}
on *:DIALOG:protlist:init:*:{
  did -a protlist 12 %.var %.chan %.type
  did -a protlist 1 %.type $+ s to scan for:
  if (%.chan == *) { 
    _loadpl %.var * 2
    did -cb protlist 9
  }
  else {
    _loadpl %.var %.chan 2 | _loadpl %.var * 3
    if (%` [ $+ [ %.var ] $+ [ %.chan ] ] == $null) { _visprotl -h -v -b | did -c protlist 9 }
  }
  unset %.var %.chan %.type
}
alias -l _loadpl {
  var %num = 1,%stuff = %` [ $+ [ $1 ] $+ [ $2 ] ]
  did -r protlist $3
  :loop
  if ($gettok(%stuff,%num,44) != $null) {
    did -a protlist $3 $ifmatch
    inc %num | goto loop
  }
}
alias -l _visprotl did $1 protlist 2 | did $2 protlist 3 | did $3 protlist 4,5,6
on *:DIALOG:protlist:sclick:9:{
  if ($did(9).state) _visprotl -h -v -b
  else _visprotl -v -h -e
}
on *:DIALOG:protlist:dclick:2:_juryrig _pled e
on *:DIALOG:protlist:sclick:4:_juryrig _pled e
on *:DIALOG:protlist:sclick:5:_juryrig _pled i
on *:DIALOG:protlist:sclick:6:if ($did(protlist,2).sel isnum) did -d protlist 2 $ifmatch
alias -l _pled {
  if ($1 == e) {
    var %edit = $$did(protlist,2,$did(protlist,2).sel)
    did -oc protlist 2 $did(protlist,2).sel $_entry(0,%edit,$gettok($did(protlist,12),3-,32),to scan for?Wildcards are allowed- * will match 'anything'.)
  }
  else did -ac protlist 2 $_entry(0,$null,$gettok($did(protlist,12),3-,32),to scan for?Wildcards are allowed- * will match 'anything'.)
}

; Add channel
dialog paddchan {
  title "Add Channel"
  size -1 -1 210 155

  text "Base new channel off of settings for:", 1, 5 5 250 20

  list 2, 5 25 200 100

  text "", 3, 1 1 1 1, hide autohs result

  button "&OK", 10, 15 120 70 25, OK default
  button "&Cancel", 11, 125 120 70 25, cancel
}
on *:DIALOG:paddchan:init:*:{
  var %num = 1
  :loop
  if ($did(chanprot,247,%num) != $null) {
    did -a paddchan 2 $ifmatch
    inc %num | goto loop
  }
}
on *:DIALOG:paddchan:dclick:2:dialog -k paddchan
on *:DIALOG:paddchan:sclick:10:did -a paddchan 3 $did(2,$did(2).sel)

;
; Track channels we are 'opped' in
;

; Updates text/ctcp/other protected channels
alias _upd.prot {
  unset %<protect.*
  var %chan,%num = $chan(0)
  :loop
  %chan = $chan(%num)
  if ($me isop %chan) {
    if ($_getcflag(%chan,71)) %<protect.text = $addtok(%<protect.text,%chan,44)
    if ($_getcflag(%chan,72)) %<protect.ctcp = $addtok(%<protect.ctcp,%chan,44)
    if ($_getcflag(%chan,73)) %<protect.misc = $addtok(%<protect.misc,%chan,44)
  }
  if (%num > 1) { dec %num | goto loop }
  if (%<protect.ctcp == $null) %<protect.ctcp = #none
}

on me:*:PART:#:_upd.prot
raw 366:*:_upd.prot

;
; Track protection flags (%`cflags*)
;

; $_getcflag(#chan,pos) #chan of * for global
alias _getcflag {
  if (%`cflags. [ $+ [ $1 ] ] != $null) if ($gettok($ifmatch,$2,32) != ?) return $ifmatch
  return $gettok(%`cflags,$2,32)
}
; _setcflag #chan pos value  (#chan of * for global)
alias _setcflag {
  if ($1 == *) var %old = %`cflags | else var %old = %`cflags. [ $+ [ $1 ] ]
  if (%old == $null) %old = $_p2s($str(?,80))

  ;;; beta fix remove later
  :loop80 | if ($numtok(%old,32) < 80) { %old = $instok(%old,?,-1,32) | goto loop80 }

  %old = $puttok(%old,$3,$2,32)
  if ($remove(%old,?) == $null) var %old
  if ($1 == *) `set cflags %old | else `set cflags. $+ $1 %old
  if ($2 isnum 71-73) _upd.prot
}
; _clrcflag #chan
alias _clrcflag `set cflags. $+ $1

;
; Generic code
;

; 'protect' user due to op/voice/level ?
; $1 nonnull to only check userlevel
; if due to level, returns level
alias -l _protect {
  if ($_level($chan,$level($fulladdress)) >= 50) return $ifmatch
  if ($1) return 0
  if (($nick isop $chan) && ($_getcflag($chan,69))) return 1
  if (($nick isvo $chan) && ($_getcflag($chan,70))) return 1
  return 0
}

; $_action(type,n,chan)
alias -l _action {
  var %file = $_cfg(actions.dat)
  if ($read -tns [ $+ [ $1 ] $+ [ $2 ] $+ [ $3 ] ] %file != $null) return $ifmatch
  if ($read -tns [ $+ [ $1 ] $+ [ 1 ] $+ [ $3 ] ] %file != $null) return
  return $read -tns [ $+ [ $1 ] $+ [ $2* ] ] %file
}

; _punish type $chan target &nick& $site &addr& &fulladdr& &A& &B& [x]
; target is used for measurements; $chan/$site used for code as well as &these&
; target must be NICK or WILDSITE
; if x is present, then no waiting is done even for rapid punishments
; returns 0 for waited on punishment or no punishment
; returns 1 for warning punishment
; returns 2 for other punishment
alias -l _punish {
  if (($10 == $null) && (%--wait. [ $+ [ $1. ] $+ [ $3 ] $+ [ $2 ] ] )) return 0
  set -u %.punishwait $_getcflag($2,75)
  inc -u [ $+ [ %.punishwait ] ] %--wait. $+ $1. $+ $3 $+ $2
  inc -u [ $+ [ $calc($_getcflag($2,76) + %.punishwait) ] ] %--act. $+ $1. $+ $3 $+ $2
  var %action = $_action($1,%--act. [ $+ [ $1. ] $+ [ $3 ] $+ [ $2 ] ] ,$2)
  if ((%action == $null) && (%--act. [ $+ [ $1. ] $+ [ $3 ] $+ [ $2 ] ] > 1)) {
    dec %--act. $+ $1. $+ $3 $+ $2
    %action = $_action($1,%--act. [ $+ [ $1. ] $+ [ $3 ] $+ [ $2 ] ] ,$2)
  }
  if (%action != $null) {
    if ($gettok(%action,1,32) > 1) {
      var %cmd = $gettok(* .mode+i .mode+m .mode+im warn warn-chan warn-self warn-ops warn-msg,$ifmatch,32)
      if (warn* iswm %cmd) %cmd $2 $3 $replace($_protwarnmsg($mid(*...nwzw,$gettok(%action,1,32),1),$1),&chan&,$2,&nick&,$4,&A&,$_p2s($8),&B&,$_p2s($9))
      else %cmd $2
    }
    else var %cmd
    if ($gettok(%action,2,32) > 1) var %do = $gettok(* .bk .bk0 .bk2 .cb .deop .fk .kick .tempban warn warn-chan warn-self warn-ops warn-msg,$ifmatch,32) &chan& &target& $gettok(%action,3-,32)
    else var %do = $gettok(%action,3-,32)
    $_finalmsg($replace($_premsg(%do),&chan&,$2,&target&,$3,&nick&,$4,&site&,$5,&addr&,$6,&fulladdr&,$7,&A&,$_p2s($8),&B&,$_p2s($9)))
    if ((%`show.punish) && ((%cmd != warn-self) && (warn* !iswm %do))) dispr $2 $:cl($2) Punishing $:t($3) $iif($3 != $4,( [ $+ [ $4) ] ] ,-) $replace( [ $read -ts [ $+ [ $1 ] ] script\punish.dat ] ,&A&,$_p2s($8),&B&,$_p2s($9)) Offense %--act. [ $+ [ $1. ] $+ [ $3 ] $+ [ $2 ] ] ( $+ $remove($gettok(%do,1,32),.) $+ )
    _ssplay protection
    _recseen offend $+ $2 $7 $1 $+ $chr(9) $+ $8 $+ $chr(9) $+ $9 7
    if (warn isin $gettok(%do,1,32)) return 1
    return 2
  }
  return 0
}
; _protwarnmsg s|w|n|k|c code
alias _protwarnmsg {
  goto $1
  :z | return $:t(&nick&) triggered protection- $read -ts [ $+ [ $2 ] ] script\punish.dat (F8 to punish)
  :s | return <$:t$&nick&>$:t$ triggered protection- $read -ts [ $+ [ $2 ] ] script\punish.dat (F8 to punish)
  :w | return &nick& triggered protection- $read -ts [ $+ [ $2 ] ] script\punish2.dat
  :n | return Warning: Protection on &chan& triggered- $read -ts [ $+ [ $2 ] ] script\punish2.dat
  :k | return $read -ts [ $+ [ $2 ] ] script\punish2.dat
  :c | return
}

; standard protection; assumes $3 (max) is above 0
; _lamercheck type inc max secs nick/address chan / $_lamercheck(type,inc,max,secs,nick/address,chan)
; use nick for plain protections, address for join/nick/etc
; returns 1 if protection enabled and should skip further protections
; skips protections if over 15 sec self lag
alias -l _lamercheck {
  if ($_floodc($2,$3,$4,$1,$5,$6)) {
    if ((min isin %-self.lag) || ($calc($gettok(%-self.lag,1,46)) > 15)) return 1
    _punish $1 $6 $nick $nick $site $address $fulladdress $3 $4
    if ($result) { _floodr $1 $5 $6 | if ($result > 1) return 1 }
  }
}

;
; CTCP protections
;

ctcp *:DCC:%<protect.ctcp:{
  _ccprot 41
  if ($_getcflag($chan,74)) {
    if (%.dcc.invalid) _punish invdcc $chan $nick $nick $site $address $fulladdress $2 $_s2p(%.dcc.invalid)
    elseif ($istok(ACCEPT RESUME,$2,32)) _punish invdcc $chan $nick $nick $site $address $fulladdress $2 DCCACCEPT/RESUMEtoachannel
  }
}
ctcp *:PING:%<protect.ctcp:_ccprot $iif($len($1-) < 26,33,34)
ctcp *:?DCC:%<protect.ctcp:_ccprot 40
ctcp *:SOUND:%<protect.ctcp:{
  _ccprot 39
  var %sound = $_getcflag($chan,21)
  if (%sound) if ($_getcflag($chan,8) > 0) _lamercheck scroll %sound $ifmatch $_getcflag($chan,9) $nick $chan
}
ctcp *:*:%<protect.ctcp:if ($findtok(ECHO TIME VERSION USERINFO CLIENTINFO SOUND XDCC DCC FINGER,$1,1,32)) _ccprot $calc($ifmatch + 33) | else _ccprot 43
alias -l _ccprot {
  _recseen offend $+ $chan $fulladdress ctcp $+ 	x	x 7
  if ($_protect) $$$

  var %pts = 1
  if ($_getcflag($chan,28)) {
    %pts = $_getcflag($chan,$1)
    if (%pts < 1) return
  }

  if ($_getcflag($chan,29) > 0) {
    if ($_floodc(%pts,$ifmatch,$_getcflag($chan,30),ctcp,$site,$chan)) {
      _punish ctcp $chan $wildsite $nick $site $address $fulladdress $_getcflag($chan,29) $_getcflag($chan,30) x
      if ($result) { _floodr ctcp $site $chan | if ($result > 1) $$$ }
    }
  }

  if ($_getcflag($chan,31) > 0) {
    if ($_floodc(%pts,$ifmatch,$_getcflag($chan,32),ctcp,$chan)) {
      _punish allc $chan $wildsite $nick $site $address $fulladdress $_getcflag($chan,31) $_getcflag($chan,32)
      if ($result > 1) $$$
    }
  }
}

;
; Text protections
;

on *:NOTICE:*:%<protect.text:{
  if ($target !ischan) return
  if ($_protect) return

  if ($_getcflag($chan,12) > 0) if ($_lamercheck(notice,1,$ifmatch,$_getcflag($chan,13),$nick,$chan)) return

  _txprot $1-
}

on *:TEXT:*:%<protect.text:if ($_protect) return | _txprot $1-
on *:ACTION:*:%<protect.text:if ($_protect) return | _txprot $1-
alias -l _txprot {
  if ($_getcflag($chan,8) > 0) if ($_lamercheck(scroll,1,$ifmatch,$_getcflag($chan,9),$nick,$chan)) return
  if ($_getcflag($chan,6) > 0) if ($_lamercheck(text,$len($1-),$ifmatch,$_getcflag($chan,7),$nick,$chan)) return

  if ($_getcflag($chan,10) > 0) {
    if (%--last. [ $+ [ $nick ] $+ [ $chan ] ] == $strip($1-)) { if ($_lamercheck(repeat,1,$_getcflag($chan,10),$_getcflag($chan,11),$nick,$chan)) return }
    else set -u [ $+ [ $calc($_getcflag($chan,11) + 1) ] ] %--last. $+ $nick $+ $chan $strip($1-)
  }
  if ($_getcflag($chan,19) > 0) if ($calc($count($1-,) + $count($1-,) + $count($1-,) + $count($1-,)) > $ifmatch) { _punish attrab $chan $nick $nick $site $address $fulladdress $ifmatch $_getcflag($chan,19) | if ($result > 1) return }
  if ($_getcflag($chan,17) > 0) if ($_lamercheck(attrfl,$calc($count($1-,) + $count($1-,) + $count($1-,) + $count($1-,)),$ifmatch,$_getcflag($chan,18),$nick,$chan)) return

  if ($_getcflag($chan,14) > 0) {
    var %letter = $calc($len($1-) - $len($removecs($upper($1-),A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z)))
    if (%letter >= $_getcflag($chan,15)) {
      var %cap = $calc($len($1-) - $len($removecs($1-,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z)))
      if ($calc(%cap / %letter * 100) >= $_getcflag($chan,14)) _punish caps $chan $nick $nick $site $address $fulladdress $int($ifmatch) $_getcflag($chan,14)
    }
  }

  if ($_getcflag($chan,24)) _wordkick 1 , $1- ,
  if ($_getcflag($chan,25)) _wordkick 2 , $1- ,
}
alias -l _wordkick {
  var %words = %`wordk [ $+ [ $1. ] $+ [ $chan ] ]
  if (%words == $null) %words = %`wordk [ $+ [ $1.* ] ]
  if (%words) {
    var %match,%stripped,%num = $numtok(%words,44)
    %stripped = , $replace($strip($3-),!,.,$chr(44),.,-,.,$chr(40),.,$chr(41),.,?,.,",.,.,$chr(32)) ,
    :loop
    %match = * $gettok(%words,%num,44) *
    if (%match iswm $2-) { _punish wordk $+ $1 $chan $nick $nick $site $address $fulladdress $_s2p($gettok(%words,%num,44)) $wildtok($2-,$gettok(%words,%num,44),1,32) | if ($result < 2) return | $$$ }
    if (%match iswm %stripped) { _punish wordk $+ $1 $chan $nick $nick $site $address $fulladdress $_s2p($gettok(%words,%num,44)) $wildtok(%stripped,$gettok(%words,%num,44),1,32) | if ($result < 2) return | $$$ }
    if (%num > 1) { dec %num | goto loop }
  }
}

on !*:NICK:{
  var %chan,%fulladdress = $newnick $+ ! $+ $address,%num = $comchan($newnick,0)
  :loop
  if (%num) {
    %chan = $comchan($newnick,%num)
    if ($istok(%<protect.misc,%chan,44)) {
      if ($istok($level(%fulladdress),=black,44)) _runblack %chan $_fuser(%fulladdress)
      if ($_level(%chan,$level(%fulladdress)) >= 50) goto skip
      if (($nick isop %chan) && ($_getcflag(%chan,69))) goto skip
      if (($nick isvo %chan) && ($_getcflag(%chan,70))) goto skip
      if ($_getcflag(%chan,4) > 0) {
        if ($_floodc(1,$ifmatch,$_getcflag(%chan,5),nick,$address,%chan)) {
          _punish nick %chan $newnick $newnick $site $address %fulladdress $ifmatch $_getcflag(%chan,5)
          if ($result) _floodr nick $address %chan
        }
      }
    }
    :skip
    dec %num | goto loop
  }
}

on !*:TOPIC:%<protect.text:{
  if ($_protect(1)) return
  if ($_getcflag($chan,20) > 0) if ($_getcflag($chan,8) > 0) _lamercheck scroll $_getcflag($chan,20) $ifmatch $_getcflag($chan,9) $nick $chan
}

on !*:QUIT:{
  unset %--wait.*. $+ $nick $+ * %--wait.*. $+ $replace($wildsite,*,?) $+ *
  var %chan,%num = $comchan($nick,0)
  :loop
  if (%num) {
    %chan = $comchan($nick,%num)
    if ($istok(%<protect.misc,%chan,44)) _dojplim %chan
    dec %num | goto loop
  }
  if (($count($1,.) > 1) && ($count($2,.) > 1) && ($3 == $null)) set -u180 %_split.quit. [ $+ [ $nick ] ] 1
}
on !*:PART:%<protect.misc:{
  unset %--wait.*. $+ $nick $+ $chan %--wait.*. $+ $replace($wildsite,*,?) $+ $chan
  _dojplim $chan
  if ($_protect) return
  if ($_getcflag($chan,57) > 0) _lamercheck joinp 1 $ifmatch $_getcflag($chan,58) $address $chan
}
alias -l _runblack {
  var %file = $_cfg(userinfo.ini),%chan = $readini -n %file $2 chan
  if ((%chan == $null) || ($istok(%chan,$1,44))) {
    set -u %.punishwait 1
    disprs $1 $:cl($1) $:s($2) is $:b(blacklisted) $+ - Kickbanning...
    var %msg = $_msg(black)
    if (&reason !isin %msg) %msg = %msg &reason&
    var %why = $readini -n %file $2 note
    kb $1 $2 $replace(%msg,&reason&,$_readprep(%why),&addr&,$2)
  }
}
on !*:JOIN:#:{
  var %lvl = $level($fulladdress),%level = $_level($chan,%lvl)
  if ($me isop $chan) {
    if ($istok($level($fulladdress),=black,44)) _runblack $chan $maddress
    elseif ((@ isin %level) || (+ isin %level)) {
      var %scan = $chr(44) $+ = $+ $chan $+ ¬
      if (%scan isin %lvl) var %chan = $chan
      else var %chan = *
      if (*@ iswm %level) {
        if ($null == $readini -n $_cfg(userinfo.ini) $maddress %chan) { _init.mass $chan 2 | _add.mass + o $nick }
      }
      if (*+ iswm %level) {
        if ($null == $readini -n $_cfg(userinfo.ini) $maddress %chan) { _init.mass $chan 2 | _add.mass + v $nick }
      }
    }
  }
  if (($_getcflag($chan,26)) && (($me isop $chan) || ($_getcflag($chan,27) == 0)) && ((%!away == $null) || ($_getcflag($chan,79) == 0))) %.joinwhois.show = $calc(1 + $_getcflag($chan,80))
  if ($_getcflag($chan,16)) %.joinwhois.do = $addtok(%.joinwhois.do,_ircopchk,32)
  if ($_getcflag($chan,22)) %.joinwhois.do = $addtok(%.joinwhois.do,_pervscan,32)
  if ($istok(%<protect.misc,$chan,44) == $false) return
  _dojplim $chan
  if ($_protect(1)) return
  if (($_getcflag($chan,59)) && (%_split.quit. [ $+ [ $nick ] ] != 1)) {
    var %req = $round($calc($nick($chan,0) * $_getcflag($chan,60) / 100),0)
    if ($_getcflag($chan,61) > %req) %req = $ifmatch
    if ($_getcflag($chan,62) < %req) %req = $ifmatch
    if ($_floodc(1,%req,$_getcflag($chan,63),massjoin,$chan)) {
      disprs $chan $:cl($chan) Mass join detected ( $+ $:s($ifmatch) joins) Temp. setting mode...
      _floodr massjoin $chan
      tempmode 60 $chan $_p2s($_getcflag($chan,3))
    }
  }
  if (($_getcflag($chan,1)) && (%level < 25)) {
    if ($_getcflag($chan,2)) var %mask = *!* $+ $address
    else var %mask = $wildsite
    if ($ialchan(%mask,$chan,0) > $_getcflag($chan,1)) {
      _punish clones $chan $wildsite $nick $site $address $fulladdress $ifmatch %mask
      if ($result > 1) return
    }
  }
  if ($_getcflag($chan,57) > 0) _lamercheck joinp 1 $ifmatch $_getcflag($chan,58) $address $chan
}
alias -l _dojplim if ($_getcflag($1,64)) .timer.limitfresh. [ $+ [ $1 ] ] 1 $_getcflag($1,68) _limitfresh $1
alias -l _limitfresh {
  var %limit = $calc($nick($1,0) * $_getcflag($1,65) / 100)
  if ($_getcflag($1,66) > %limit) %limit = $ifmatch
  if ($_getcflag($1,67) < %limit) %limit = $ifmatch
  if ($calc($nick($1,0) + %limit) != $chan($1).limit) .mode $1 +l $ifmatch
}
alias _ircopchk {
  if ($3) {
    if ($4 == 0) return
    <warn.ircop> $1 $2 is an IRCop
  }
  else {
    if (%-mwho.ircop == $null) return
    <warn.ircop> $1 $2 %-mwho.ircop
  }
  upd.nick.comchan $2
}
alias _pervscan {
  var %chans = %`pervscan. [ $+ [ $1 ] ]
  if (%chans == $null) %chans = %`pervscan.*
  if (%chans) {
    var %on = $remtok(%-mwho.chanbare,$1,1,32)
    if (%on) {
      var %num = $numtok(%chans,44)
      :loop2
      if ($wildtok(%on,$gettok(%chans,%num,44),1,32)) _punish pervscan $1 $2 $2 %-mwho.host %-mwho.ident $+ @ $+ %-mwho.host $2 $+ ! $+ %-mwho.ident $+ @ $+ %-mwho.host $ifmatch $gettok(%chans,%num,44)
      elseif (%num > 1) { dec %num | goto loop2 }
    }
  }
}

on *:BAN:%<protect.misc:{
  if (($remove($banmask,*,!,?,.,@,~) == $null) && ($_getcflag($chan,56))) return
  var %skipen
  if (($banmask iswm %!myself) && ($_getcflag($chan,45))) {
    if ($nick == $me) { disprs $chan $:cl($chan) You banned yourself.. removing ban. | .mode $chan -b $banmask }
    else {
      disprs $chan $:cl($chan) You were banned by $:t($nick) - attempting to unban
      unban $chan $banmask
      if ($_protect(1)) return
      _punish banyou $chan $nick $nick $site $address $fulladdress $banmask %!myself
      if ($result > 1) return
      %skipen = 1
    }
  }
  elseif ((%`show.banned) && (%.ban.count)) <show.banned> $chan %.ban.count %.ban.who
  if ($nick == $me) return
  var %whom,%num = %.ban.count
  :loop
  %whom = $gettok(%.ban.who,%num,32)
  if ((%whom != $nick) && ($_level($chan,$level($address(%whom,5))) >= 60)) {
    if (($ifmatch >= 75) && ($_level($chan,$level($fulladdress)) < 50)) .raw kick $chan $nick : $+ $_finalmsg($replace($_msg(protectb),&chan&,$chan,&nick&,%whom,&addr&,$maddress,&ban&,$banmask,&you&,$nick))
    _init.mass $chan | _add.mass - b $banmask
  }
  else {
    if (%num > 1) { dec %num | goto loop }
    if ($banmask !iswm $fulladdress) {
      var %user = $_fuser2($banmask)
      if ($_level($chan,$level(%user)) >= 60) {
        if (($ifmatch >= 75) && ($_level($chan,$level($fulladdress)) < 50)) { _init.mass $chan | _add.mass - o $nick }
        else _init.mass $chan
        _add.mass - b $banmask
        if ($_finalmsg($replace($_msg(protectbn),&chan&,$chan,&nick&,%user,&addr&,%user,&ban&,$banmask,&you&,$nick))) _linedance notice $nick $ifmatch
      }
    }
  }
  if ($_protect(1)) goto enforce
  if (%.ban.count) {
    if ($_getcflag($chan,50)) {
      if ($_floodc(%.ban.count,$ifmatch,$_getcflag($chan,51),usersban,$site,$chan) > %.ban.count) {
        _punish mbanu $chan $wildsite $nick $site $address $fulladdress $ifmatch $_getcflag($chan,51)
        if ($result) { _floodr usersban $site $chan | if ($result > 1) return | %skipen = 1 }
      }
    }
    if ($_getcflag($chan,52)) {
      if ($_floodc(1,$ifmatch,$_getcflag($chan,53),totalban,$site,$chan)) {
        _punish mbanb $chan $wildsite $nick $site $address $fulladdress $ifmatch $_getcflag($chan,53)
        if ($result) { _floodr totalban $site $chan | return }
      }
    }
  }
  :enforce
  if (%skipen) return
  %.punishwait = 1
  if (($_getcflag($chan,23)) && ($nick isop $chan) && (%.ban.count < 4)) .fk -nvu $chan $banmask $replace($_msg(banned),&banner&,$nick)
}

on !*:MODE:%<protect.misc:{
  if ($_protect(1)) return
  var %modes = $_fixmode($1)
  if (($calc($count(%modes,+l) + $count(%modes,+k) + $count(%modes,+i)) > 1) && ($_getcflag($chan,55))) {
    _init.mass $chan
    var %mode,%pos = 1
    :loop
    %mode = $mid(%modes,%pos,2)
    if ($len(%mode) == 2) {
      if (-* iswm %mode) { if (%mode !== -k) _add.mass + $mid(%mode,2,1) }
      else _add.mass - $mid(%mode,2,1) $iif(%mode === +k,$chan($chan).key)
      inc %pos 2 | goto loop
    }
    _punish tover $chan $nick $nick $site $address $fulladdress $1 $_s2p($1-)
    if ($result > 1) return
  }
  if ((+l isin %modes) && ($chan($chan).limit == 1) && ($_getcflag($chan,54))) {
    _init.mass $chan
    _add.mass - l
    _punish model $chan $nick $nick $site $address $fulladdress $1 $_s2p($1-)
  }
}

on *:KICK:#:{
  if ($knick == $me) { %<op = $remtok(%<op,$chan,32) | unset %<op. $+ $chan | _upd.prot }
  else {
    unset %--wait.*. $+ $knick $+ $chan %--wait.*. $+ $replace($address($knick,2),*,?) $+ $chan
    if ($istok(%<protect.misc,$chan,44)) {
      _dojplim $chan
      if (($nick != $me) && ($nick != $knick) && ($_level($chan,$level($address($knick,5))) >= 60)) {
        if (($ifmatch >= 75) && ($_level($chan,$level($fulladdress)) < 50)) .raw kick $chan $nick : $+ $_finalmsg($replace($_msg(protectk),&chan&,$chan,&nick&,$knick,&addr&,$maddress,&you&,$nick))
        invite $knick $chan
      }
      if (($nick == $me) || ($_protect(1))) return
      if (($knick isop $chan) && ($_getcflag($chan,46))) {
        if ($_floodc(1,$ifmatch,$_getcflag($chan,47),userdeop,$site,$chan)) {
          _punish deop $chan $wildsite $nick $site $address $fulladdress $ifmatch $_getcflag($chan,47)
          if ($result) { _floodr userdeop $site $chan | if ($result > 1) return }
        }
      }
      if ($_getcflag($chan,48)) {
        if ($_floodc(1,$ifmatch,$_getcflag($chan,49),userkick,$site,$chan)) {
          _punish kick $chan $wildsite $nick $site $address $fulladdress $ifmatch $_getcflag($chan,49)
          if ($result) _floodr userkick $site $chan
        }
      }
    }
  }
}
on *:DEOP:#:{
  if ($opnick == $me) _upd.prot
  if ($_isbot($nick)) return
  if ($istok(%<protect.misc,$chan,44)) {
    if (($nick != $me) && ($nick != $opnick) && ($_level($chan,$level($address($opnick,5))) >= 60)) {
      if (($ifmatch >= 75) && ($_level($chan,$level($fulladdress)) < 50)) { _init.mass $chan | _add.mass - o $nick }
      else _init.mass $chan
      _add.mass + o $opnick
      if ($_finalmsg($replace($_msg(protectd),&chan&,$chan,&nick&,$knick,&addr&,$maddress,&you&,$nick))) _linedance notice $nick $ifmatch
    }
    if (($nick == $me) || ($_protect(1))) return
    if ($_getcflag($chan,46)) {
      if ($_floodc(1,$ifmatch,$_getcflag($chan,47),userdeop,$site,$chan)) {
        _punish deop $chan $wildsite $nick $site $address $fulladdress $ifmatch $_getcflag($chan,47)
        if ($result) _floodr userdeop $site $chan
      }
    }
  }
}
on *:OP:#:_doop
on *:SERVEROP:#:_doop
alias -l _doop {
  if ($opnick == $me) {
    _upd.prot
    ; Blacklist scan
    var %addr,%num = $ulist(*,black,0)
    :loop
    if (%num) {
      %addr = $ulist(*,black,%num)
      if ($ialchan(%addr,$chan,1)) _runblack $chan %addr
      dec %num | goto loop
    }
  }
  elseif (($nick != $me) && ($me isop $chan)) {
    if ($right($_level($chan,$level($address($opnick,5))),1) == -) .deop $chan $opnick
    elseif (($istok(%<protect.misc,$chan,44)) && ($_getcflag($chan,$iif(. isin $nick,78,77)))) {
      if (($notify($opnick) == $null) && ($remtok($level($address($opnick,5)),=black,1,44) == $dlevel)) .deop $chan $opnick
    }
  }
}

;
; Protect aliases
;
; /tempmode n chan modes
alias tempmode {
  _init.mass $2
  if ($left($3,1) isin +-) _add.mass $ifmatch $right($3,-1) $4-
  else _add.mass + $3-
  .timer 1 $1 .raw mode $2 - $+ $replace($3,+,/,-,+,/,-) $4-
}
alias mode+i tempmode 60 $1 +i
alias mode+m tempmode 60 $1 +m
alias mode+im tempmode 60 $1 +im
alias warn {
  if ($_ismask($2)) _linedance fnotice $1-
  else _linedance notice $2-
}
alias warn-msg {
  ;;; leave as fnotice?
  if ($_ismask($2)) _linedance fnotice $1-
  else _linedance msg $2-
}
alias warn-chan _linedance notice $1 $3-
alias warn-self disprs $1 $:cl($1) $3-
alias warn-ops _linedance onotice $1 $3-

;
; Quick toggles
;
; /prot [#channel] [+|-]ircop|text|ctcp|misc|all|op|voc|enforce|selfban|whois|whoisaway|whoischan|whoisop ...
alias prot {
  var %name,%flag,%bit,%work,%chan,%todo,%num = 1
  if (($left($1,1) isin &#) || ($1 == *)) { %chan = $1 | %todo = $2- } | elseif (#) { %chan = # | %todo = $1- } | else _error You must use /prot in a channel(or specify a target channel)
  if (%todo == $null) {
    dispa Syntax: /prot [#channel] [+|-]option ...
    dispa Options: ircop, text, ctcp, misc, all, op, voc, enforce, selfban, whois, whoisaway, whoischan, whoisop
    return
  }
  :loop
  %work = $gettok(%todo,%num,32)
  if ($left(%work,1) isin 0?+-*) { %bit = $left(%work,1) | %work = $right(%work,-1) }
  else %bit = 0
  if (%work == $null) return
  if ($findtok(ircop text ctcp misc op voc enforce selfban whois whoisaway whoischan whoisop,%work,1,32)) {
    %name = $gettok(IRCop check on join is.Text protections are.CTCP protections are.Other protections are.Ops immunity is.Voiced user immunity is.Ban enforcement is.Personal ban protection is.Whois on join is.Don't whois on join if away is.Show whois on join in channel is.Only whois on join if opped is,$ifmatch,46)
    %flag = $gettok(16 71 72 73 69 70 23 45 26 79 80 27,$ifmatch,32)
    if (%bit == *) _setcflag %chan %flag $iif($_getcflag(%chan,%flag),0,1)
    elseif (%bit == +) _setcflag %chan %flag 1
    elseif (%bit == -) _setcflag %chan %flag 0
    elseif ((%bit == ?) && (%chan != *)) _setcflag %chan %flag ?
    dispa %name $:s($_tf2o($_getcflag(%chan,%flag))) (for %chan $+ )
  }
  elseif (%work == all) %todo = %todo %bit $+ text %bit $+ ctcp %bit $+ misc
  else dispa Unknown option ' $+ %work $+ '
  inc %num | goto loop
}