Undo Modechanges

From Scriptwiki
Jump to: navigation, search

This script is supposed to undo all modechanges in a channel. It may not work 100%, but it will be updated in the future. You can find explanations in the script.

Note that you cant just replace + with - and - with plus as e.g. +l needs a parameter for setting (the limit that is), but you can unset it without parameter (just -l). Read more about it here.

on @*:RAWMODE:#: {
 ; check if bitchmode is globally on
 if ($bitchmode(global) != on) { halt }

 ; check if bitchmode for this channel is on
 if ($bitchmode($chan) != on) { halt }

 ; set some variable. 
 ; we use %cmode to save if it is currenty a "+" or "-" what we are looking at (in $1-)
 ; %+modes is for modes we want to set, %-modes for modes we want to unset
 ; %+param are the params for the modes we want to set, %-param for the one we want to unset
 ; %npara is the number of the parameter we are currently looking at
 var %i = 1, %cmode, %+modes, %-modes, %modes, %+param, %-param, %npara = 1

 ; lets loop through the string
 while (%i <= $len($1)) {
   ; we want to know if a mode is + or -, so lets set it to a variable
   if ($mid($1,%i,1) == +) { %cmode = + }
   elseif ($mid($1,%i,1) == -) { %cmode = - }

   ; if its in the third field of $chanmodes, it needs a parameter to set and none to unset. Look at raw 005 to get
   ; more info about it
   elseif ($mid($1,%i,1) isincs $gettok($chanmodes,3,44)) {

     ; if we are currently at the "+" 
     if (%cmode == +) {

       ; check if we set something, if yes, we need to set it as "default". Add it to the hashtable
       if ($nick == $me) { hadd bitchmode chan. [ $+ [ $chan ] $+ [ $chr(124) ] $+ [ $mid($1,%i,1) ] ] $gettok($1-,$calc(%npara + 1),32) }

       ; if there is something as default in our hashtable, we want to set this again
       if ($bitchmode($chan $+ $chr(124) $+ $mid($1,%i,1))) { 

         ; put it to the variable
         %+modes = %+modes $+ $mid($1,%i,1)

         ; increase the parameter-variable so that we look at the next one
         inc %npara
         %+param = %+param $bitchmode($chan $+ $chr(124) $+ $mid($1,%i,1))
       }

       ; if there is no default, just unset it
       else {
         %-modes = %-modes $+ $mid($1,%i,1)

         ; increase the parameter-variable so that we look at the next one
         inc %npara
       }
     }
     ; if its "-" 
     else {
       ; just reset it with the info we have in our hashtable
       %+modes = %+modes $+ $mid($1,%i,1)
       %+param = %+param $bitchmode($chan $+ $chr(124) $+ $mid($1,%i,1))
     }
   }

   ; if its in the field 1 or 2, it needs a parameter to set AND unset
   elseif ($mid($1,%i,1) isincs $gettok($chanmodes,1-2,44)) { 

     ; if its +, we just want to unset it. You dont need to look if this set "oversets" another setting, as this is
     ; not possible with modes in these fields of $chanmodes
     if (%cmode == +) { 
       %-modes = %-modes $+ $mid($1,%i,1) 
       %-param = $addtok(%-param,$gettok($1-,$calc(%npara + 1),32),32)
     }
     ; if someone unsets it, we just set it again
     else { 
       %+modes = %+modes $+ $mid($1,%i,1) 
       %+param = $addtok(%+param,$gettok($1-,$calc(%npara + 1),32),32)
     } 
     ; we need to increase the parameter counter again
     inc %npara
   }
   ; if its in the last field, its just a simple mode without parameter, so we will just undo it
   elseif ($mid($1,%i,1) isin $gettok($chanmodes,4,44)) { 
     if (%cmode == +) { %-modes = %-modes $+ $mid($1,%i,1) }
     else { %+modes = %+modes $+ $mid($1,%i,1) } 
   }
   ; here we increase the parameter counter if its a nickmode (e.g. +o)
   elseif ($mid($1,%i,1) isincs $nickmodes) { inc %npara }

   ; increase the looping variable
   inc %i
 }

 ; put all stuff together
 if ($len(%+modes) > 0) { %modes = %modes + $+ %+modes }
 if ($len(%-modes) > 0) { %modes = %modes $+ - $+ %-modes }

 ; if we did do all this stuff, we dont want to undo it of course. We still needed to loop through it to set new
 ; default settings
 if ($nick != $me) { mode $chan %modes %+param %-param }
}

; This is the alias to turn the script on or off, either globally or for channels
; Its /bitchmode [#channel] [on|off]
; You can do:
; /bitchmode #channel <on|off> - turns bitchmode for this channel on or off
; /bitchmode #channel - shows current option
; /bitchmode <on> - turns bitchmode on / off. If on, its just activated for the previosly 
;                   activated channel
; /bitchmode <off>  - turns bitchmode off
; /bitchmode - shows all options

alias bitchmode {
 ; we want to check if its used as identifier ($bitchmode())
 if ($isid) {

   ; lets return the global setting
   if ($1 == global) { return $hget(bitchmode,global) }

   ; if its a channel in the first parameter
   if ($left($1,1) == $chr(35)) {
     ; return whether bitchmode is on or off for this channel
     if ($numtok($1-,32) == 1) { 
       return $iif($hget(bitchmode,chan. [ $+ [ $1 ] ]),$hget(bitchmode,chan. [ $+ [ $1 ] ]))
     }

     ; return information we have saved about a certain channel (modes that are set)
     else {
       return $iif($hget(bitchmode,chan. [ $+ [ $1 ] ]),$hget(bitchmode,chan. [ $+ [ $1 ] ]));
     }
   }
 }
 ; its no identifier, so its for changing options
 else {
   ; we want to give everything out
   if (!$1) {
     ; lets loop through all items in the hash
     var %i = 1

     while (%i <= $hfind(bitchmode,chan.*,0,w)) { 
       ; if it is really a channel option    
       if ($chr(124) !isin $hfind(bitchmode,chan*,%i,w)) {
         echo -a $gettok($hfind(bitchmode,chan*,%i,w),2,46) => $hget(bitchmode,$hfind(bitchmode,chan*,%i,w))
       }
       ; increase looping variable
       inc %i
     }
     ; echo the global options
     echo -a GLOBAL: $hget(bitchmode,global)
   }
   ; we want to turn it on
   if ($1 == on) { hadd bitchmode global on | echo -a bitchmode turned on for: global. }

   ; we want to turn it off
   if ($1 == off) { hadd bitchmode global off | echo -a bitchmode turned off for: global. }

   ; we want to change a channel setting, so lets do it
   if ($left($1,1) == $chr(35)) {
     if (!$istok(on off, $2, 32)) { halt }
     hadd bitchmode chan. [ $+ [ $chan ] ] on
     echo -a bitchmode turned $2 for: $1 $+ .
   }  
 }
}

; adds everything to the hash
raw 324:*: {
 ; we need to get all stuff in field #3 @ raw 005 ($chanmodes)
 var %i = 1, %npara = 1 

 ; lets loop through the information the raw gives us
 while (%i <= $len($3)) {
   ; if it is in the field 3, we need to save it (as we dont get the information when it is unset)
   if ($mid($3,%i,1) isincs $gettok($chanmodes,3,44)) { 
     ; add it to the hashtable
     hadd bitchmode chan. [ $+ [ $2 ] $+ [ $chr(124) ] $+ [ $mid($3,%i,1) ] ] $gettok($1-,$calc($3 + %npara),32) 

     ; increase parameter counter
     inc %npara 
   }
   ; if its in the first or second field, lets increase the counter parameter again
   if ($mid($3,%i,1) isincs $gettok($chanmodes,1-2,44)) { inc %npara }

   ; increase looping variable
   inc %i
 }
}

; we want to load everything on start again
on *:START: {
 ; lets make a new hashtable if it doesnt exist
 if (!$hget(bitchmode)) { hmake bitchmode 10 }

 ; load the "old" stuff in it
 hload bitchmode bitchmode.dat

 ; delete everything but channels and global
 var %i = 1

 ; loop through the entire table
 while (%i <= $hget(bitchmode,0).item) {
   ; delete everything thats no channel or global option
   if ($chr(124) isin $hget(bitchmode,%i).item) && (global != $hget(bitchmode,%i).item) { hdel bitchmode $hget(bitchmode,%i).item }

   ; increase looping variable
   inc %i
 }
}

; lets save everything when we exit to bitchmode.dat
on *:EXIT: {
 hsave bitchmode bitchmode.dat
}