Previous Section  < Day Day Up >  Next Section

Hack 20 Automate mIRC with Scripting

figs/moderate.gif figs/hack20.gif

mIRC is already a friendly and easy-to-use IRC client. Master its scripting capabilities to automate lots of useful tasks.

mIRC is one of the most popular IRC clients available for Windows, and it comes complete with a robust scripting engine. Thousands of ready-to-run scripts are available from sites like http://www.mircscripts.org, although it is often hard to find a script that does exactly what you want, which is why many people decide to write their own scripts, or alter existing ones.

4.5.1 Opening the Scripts Editor

mIRC comes with its own integrated scripts editor (Figure 4-12), which can be accessed by pressing Alt-R while the client is active.

Figure 4-12. The mIRC scripts editor
figs/irch_0412.gif


The five tabs in the scripts editor are Aliases, Popups, Remote, Users, and Variables. Almost all scripts fall into the Remote section. Remote means that the script can respond to remote events, such as people joining a channel or your connecting to an IRC server.

4.5.2 Making a Bad Word Banner

One of the most common types of script is the bad word banner, which bans or kicks users who say a bad word. To do this, the script must be supplied with a list of bad words, and it must "listen" for these words in channel messages.

To make the script nice and flexible, you can use a text file to store the list of bad words. First, you need to make the file, so create a file called badwords.txt in your mIRC directory (for example, c:\Program Files\mIRC\)and place some bad words in it, one per line. If you want, you can also match phrases that contain spaces, as shown in Figure 4-13.

Figure 4-13. The list of bad words
figs/irch_0413.gif


Save the bad words list and return to mIRC. Now open up the script editor (using Alt-R again) and make sure that the Remote tab is selected. Add the following code:

alias matchbad {

  set %i 1

  while (%i <= $lines(badwords.txt)) {

    if ($read(badwords.txt,%i) isin $1-) {

      return $true

    }

    inc %i

  }

  return $false

}

This script will return $true if the given phrase matches a bad word, or $false if it doesn't. The script is explained step-by-step:

4.5.2.1 alias

This tells mIRC that you are creating an alias for a group of commands—instead of the main part of the script doing these commands over and over again itself, you can group them into an alias, which lets us repeat the code contained within it easily. The alias you have made is called matchbad. The word immediately following the alias command is the name given to the alias, which executes all the code contained in the outermost curly braces ({ ... }). If you are used to other programming languages, you can think of an alias as a method or function.

4.5.2.2 set

The set command allows us to set a variable so it contains a value. Variables begin with the % character in mIRC scripts. The set command here sets the %i variable to 1.

4.5.2.3 while

As with other languages, the while loop executes a group of commands while a condition is true. The condition in this case is %i <= $lines(badwords.txt), which roughly translates into English as, "while %i is less than or equal to the number of lines in badwords.txt". $lines is a predefined alias to count the number of lines in a given file.

The code that gets executed while this condition is true is once again included in curly braces and is typically indented for clarity.

4.5.2.4 if

The if statement causes a block of code to be executed only if the condition is true. The read alias used in this condition reads the given line from the specified file, line %i from badwords.txt. The isin operator checks to see if one string is a substring of another. So in this script, it is essentially checking to see if the text in the line read from badwords.txt is in $1-.

$1- is a special variable that returns all the arguments passed to the alias, so if a script calls $matchbad(This is,a test), $1- will be set to "This is a test". There are numerous other variables that reference arguments: $1 and $2 get the first and second arguments, respectively, while suffixing one of these with a - (for example, $1-, $2-, etc.) gives you that argument and everything after it.

4.5.2.5 return

The return function makes the alias return a given value to the caller. In our example, the value $true is returned if the arguments contain a phrase in badwords.txt and $false if it doesn't.

4.5.2.6 inc

The inc command increases a variable by 1 or by an optional specified amount, for example, inc %foo 200.

4.5.3 Putting It All Together

When you put all of this together, you get an alias that scans through a text file and checks to see if any of the phrases within it are contained in the arguments passed. If they are, it returns $true; otherwise it returns $false.

The next part of the script listens to channel messages and bans users who say a phrase that is banned. To do this, use the on TEXT event, which is triggered whenever mIRC receives a private or channel message.

Add the following text to the script using mIRC's editor. Again, let's examine the code bit-by-bit:

on *:TEXT:*:#:{

  if ($matchbad($1-) == $true) {

    mode $chan +b $address($nick,3)

    kick $chan $nick Bad phrase detected!

  }

}

4.5.3.1 on TEXT

This event, as mentioned earlier, is triggered on all private or channel messages. The * before the TEXT means that there are no conditions attached relating to you or the user. You can specify user levels here if you want—see the built-in help system by typing /help user list. The * after it is a wildcard that matches all text. You could use *e* to match only text with an e in it, for example. The # means that only messages sent to a channel will be caught.

4.5.3.2 if

The if statement should be familiar from the alias created earlier. This particular if statement calls the $matchbad alias with the text as a parameter and checks to see if the result equals $true. $1- is the first word of the message and everything after that.

4.5.3.3 mode

This is exactly the same as the /mode command you may have used in mIRC or other IRC clients. The $chan variable contains the name of the channel the message was sent to. The $address alias returns part of a given nickname's address (see /help $address), and the $nick variable contains the user's nickname. The line mode $chan +b $address($nick,3) will place a ban on the user's host.

4.5.3.4 kick

The kick command kicks a given user from the specified channel (with an optional Kick message). This example kicks the user from the channel, with the reason, "Bad phrase detected!" If you want, you can change this to something more fitting.

4.5.4 Running the Hack

Put both the alias and the on TEXT event into your Remote section using the mIRC script editor, and create a badwords.txt file with a few banned phrases in. Now try it out! Note that the on TEXT event will not trigger when you say something, so you will need to use another client or get a friend to help you test the script. Anybody caught saying words on the bad word list will be kick-banned from the channel.

Further information about scripting can be found in the mIRC help system. This is extremely detailed. Simply type /help from mIRC (or press F1) to view it.

Chris Smith

    Previous Section  < Day Day Up >  Next Section