If-then-else statements

 

The If-then-else statement allows you to compare values and execute different parts of a script based on that comparison.

 

Basic format

 

if (v1 operator v2) { commands }

elseif (v1 operator v2) { commands }

else { commands }

 

The ( ) brackets enclose comparisons, whereas the { } brackets enclose the commands you want to be performed if a comparison is true. You must make sure that the number of ( ) and { } brackets match to make sure that the correct comparisons are made, and that the correct commands are executed.

 

Using brackets speeds up processing. If an alias uses too few brackets then the statement might be ambiguous and the alias will take longer to parse, might be parsed incorrectly, or might not be parsed at all.

 

You can nest as many if-then-else statements as you want inside each other.

 

The Operators

 

       ==        equal to

       ===        equal to (case-sensitive)

       !=        not equal to

       <        less than

       <=        less than or equal to

       >        greater than

       >=        greater than or equal to

       //        v2 is a multiple of v1

       \\        v2 is not a multiple of v1

       &        bitwise comparison

 

       isin        string v1 is in string v2

       isincs        string v1 is in string v2 (case sensitive)

       iswm        wildcard string v1 matches string v2

       iswmcs        wildcard string v1 matches string v2 (case sensitive)

       isnum        number v1 is a number in the range v2 which is in the form n1-n2 (v2 optional)

       isletter        letter v1 is a letter in the list of letters in v2 (v2 optional)

       isalnum        text contains only letters and numbers

       isalpha        text contains only letters

       islower        text contains only lower case letters

       isupper        text contains only upper case letters

 

       ison        nickname v1 is on channel v2

       isop        nickname v1 is an op on channel v2

       ishop        nickname v1 is a halfop on channel v2

       isvoice        nickname v1 has a voice on channel v2

       isreg        nickname v1 is a normal nick on channel v2

       ischan        if v1 is a channel which you are on.

       isban        if v1 is a banned address in internal ban list on channel v2

       isinvite        if v1 is on the invite list of channel v2

       isexcept        if v1 is on the except list of channel v2

 

       isaop        if v1 is a user in your auto-op list for channel v2 (v2 optional)

       isavoice        if v1 is a user in your auto-voice list for channel v2 (v2 optional)

       isignore        if v1 is a user in your ignore list with the ignore switch v2 (v2 optional)

       isprotect        if v1 is a user in your protect list for channel v2 (v2 optional)

       isnotify        if v1 is a user in your notify list.

 

To negate an operator you can prefix it with an ! exclamation mark.

 

$v1 & $v2

Returns the first and second parameters of an if-then-else comparison. So, in the case of this comparison:

 

if (text isin sometext) { ... }

 

$v1 will return "text" and $v2 will return "sometext".

 

Combining comparisons

You can combine comparisons by using the && for AND and || for OR characters.

 

number {

 if (($1 > 0) && ($1 < 10)) {

   if ($1 < 5) echo Number is less than five

   else echo Number is greater than five

 }

 else echo Number is out of bounds

}

 

This alias checks if the number you specify, when you type /number <value>, lies within the required range.

 

The ! not prefix

You can use the ! prefix in front of variables and identifiers in an if-then-else statement to negate a value. The following statements are equivalent.

 

if (%x == $null) echo x has no value

 

if (!%x) echo x has no value

 

Examples

 

listops {

 echo 4 * Listing Ops on #

 set %i 1

 :next

 set %nick $nick(#,%i)

 if %nick == $null goto done

 if %nick isop # echo 3 %nick is an Op!

 inc %i

 goto next

 :done

 echo 4 * End of Ops list

}

 

This alias lists the Ops on the current channel. It does this the hard way since we could just use $opnick() instead but using $nick() serves as an example of how isop can be used and how $null is returned once we reach the end of the list.

 

GiveOps {

 %i = 0

 %nicks = ""

 :nextnick

 inc %i

 if ($snick(#,%i) == $null) { if ($len(%nicks) > 0) mode # +oooo %nicks | halt }

 %nicks = %nicks $snick(#,%i)

 if (4 // %i) { mode # +oooo %nicks | %nicks = "" }

 goto nextnick

}

 

This is a popup definition which Ops the nicknames which are selected in the current channel nicknames listbox.

 

on 1:ctcpreply:PING* {

 if ($2 == $null) halt

 else {

   %pt = $ctime - $2

   if (%pt < 0) set %pt 0

   if (%pt < 5) echo 4 [PING reply] $nick is too close for comfort

   elseif (%pt < 20) echo 4 [PING reply] $nick is at just about the right distance

   else echo 4 [PING reply] Earth to $nick earth to $nick

 }

 halt

}

 

This intercepts a ping reply and prints out a message based on how far away the person is.