mIRC Scripting

mIRC: Conditional Statements

Scripts, just like in real life, need to evaluate things to make decisions. In mIRC, that command is called the /IF command.
The basic if statement would look like this:

if (<condition>) {

}

The /if command works by evaluating the condition. If the condition(s) was found to be $TRUE, mIRC will execute the commands. The condition is usually made up of two values and a comparison operator. For example:

2 > 1

That condition will evaluate to $TRUE because 2 is greater than 1.

5 > 10

That condition will evaluate to $FALSE because 5 is less than 10, hence, the commands will not get executed.

Operators

Operators Explanation
== equal to
=== equal to*
!= not equal to
< less then
<= less then or equal to
> more then
>= more then or equal to
// is multiple of
\\ is not multiple of
& bitwise
 
isin %var is in %varB
isincs %var is in %varB*
iswm wildcard *text* match %VarB
iswmcs wildcard *text* match %VarB*
isnum num1 is in num2(-num3)
isletter %var is letter
isalnum %var contains letters and nums
isalpha %var has only letters
islower %var has only lowcase
isupper %var has only uppercase
 
ison nick is on channel #Channel
isop nick is OP in channel #channel
ishop nick is Help in channel #channel
isvoice nick is voice in channel #channel
isreg nick is regular in channel #channel
ischan if u are in that channel
isban nick is baned on channel #channel
 
isaop nick is in your auto-op list
isavoice nick is in your auto-voice list
isignore nick is in your ignore list
isprotect nick is in your protect list
isnotify nick is in your notify list

Looking at the operators list you probably have noticed some different types of operators that are not found in other scripting languages, these operators are used specially for mIRC and IRC. For Example:

isop (nick is OP in channel #channel) is used to check if a user is OP in a given channel. For example, in our following script, we will check if the user is op in a given channel in order to let him HELP other people.

on 1:TEXT:!h *:#MyChan:{
  ;if the user is an operator in #MyChan
  if ($nick isop $chan) {
    mode $chan +h $2
  }
}

 

Else

Although the /if is very helpful, lots of times you need to execute something in case it was $FALSE. The /else command lets you do just that. The basic format will look like this:

if (<condition>) {
  /*
  Code here will be executed if
  the condition is $TRUE
  */

}
else {
  /*
  Apparently the condition was $FALSE,
  thus going to the else section.
  */

}

Going back to the !h command from above, we can use the /else command to tell a non-op user that he needs to be an op to use that command.

on 1:TEXT:!h *:#MyChan:{
  ;if the user is an operator in #MyChan
  if ($nick isop $chan) {
    mode $chan +h $2
  }
  else {
    notice $nick Sorry, But only Channel OP can use this command.
  }
}

 

ElseIf

The elseif is an /if statement and an /else combined. The elseif gets to evaluate like the /if statement if the original /if statement return $FALSE. For example:

if (<condition>) {
  /*
  condition was $TRUE
  */

}
elseif (<condition>) {
  /*
  The original /if was $FALSE but /elseif is $TRUE
  */

}
else {
  /*
  Both the /if and the /elseif conditions evaluated to $FALSE.
  */

}

There are no limitations to the amount of elseifs you can have in a given script.

 

Complex Conditions

A complex condition is a statement that is made up of multiple conditions. Every condition eventually comes down to whether the condition is $FALSE or $TRUE. Logical Operators are used to combine those conditions.

&& - AND
|| - OR

The ‘&&’ (AND) Operator is used when you want BOTH conditions to return $TRUE. The ‘||’ (OR) Operator is used when you want at least one of the condition to return $TRUE.For example:

;Will Execute
if
($true) && ($true) { ... }
;Will NOT Execute
if ($true) && ($false) { ... }
;Will NOT Execute
if
($false) && ($false) { ... }
;Will Execute
if
($true) || ($true) { ... }
;Will Executee
if
($true) || ($false) { ... }
;Will NOT Execute
if
($false) || ($false) { ... }
;Will Execute
if
($true) || ($false) && ($true) { ... }
;Will NOT Execute
if
($true) || ($false) && ($false) { ... }
;Will Execute
if
($false) || ($true) || ($false) { ... }
;Will NOT Execute
if
($true) && ($false) && ($false) { ... }

 

Negation

The ! operator reverses the truth of a boolean expression. if applied to a true condition, the operator returns false. If it is applied to an expression that is false, the operator returns true.

For example, instead of saying:

var %foo = $null
if (%foo == $null) { echo works! }

You can say

var %foo = $null
if (!%foo) { echo works! }

In the statement above, mIRC will check if the variable is $null (or 0).

 

References

mIRC offers two identifiers to retrieve the first or second parameter of the if statement. Please note, the identifiers will return the first and second parameter or the $TRUE condition.

$v1 and $v2

if (I !isin team) {
  echo -a There is no $v1 in $v2 $+ !
}

if (1 == 2) || (I !isin team) {
  echo -a There is no $v1 in $v2 $+ !
  ;Since the first condition was evaluated to $FALSE, the $v1 and $v2 were taken from the second condition which was $TRUE.
}

 

IIF Identifier

iif is a built in identifier that evaluates a condition, similar to /if statement, and returns one of two values if the condition was true or false.

Syntax:

$iif(<condition>,<true>)
$iif(<condition>,<true>,<false>)

Example:

Alias Example {
  echo -a $iif($calc(1 + 1) == 2,1+1 Equals 2)
  echo -a $iif(2 == 5,I Guess 2 Does Equals 5 After All,Nope 2 != 5)
}