O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  

Buy the book!
IRC Hacks
By Paul Mutton
July 2004
More Info

A Simple Perl IRC Client
Build a simple IRC robot that connects to an IRC server and joins a channel of your choosing
The Code
[Discuss (4) | Link to this hack]

If you have managed to connect directly to an IRC server with Telnet, you are probably ready to use this knowledge and start writing your own programs that connect to IRC automatically. In this example, you will build a simple IRC bot that connects to an IRC server and joins a channel. The term "bot" is commonly used to describe an automated IRC client and is a contraction of "robot." You will get the bot to output information as it receives it from the server.

Many people find that Perl is a very suitable language for making simple IRC bots, as it is easy to use regular expressions to parse the data from the server. A single TCP socket is used to send and receive the text-based data, so you will need to use IO::Socket .

This will be quite a simple bot. All it has to do is connect to the server and join a channel. For this implementation, you must be aware that the IRC RFC states that each command or message must end with a return and new line (i.e., \r\n). To avoid getting disconnected by the server, you will also need to make sure that it responds appropriately to PING messages.

The Results

After the script has connected successfully, you will see it outputting all of the lines it receives from the IRC server, for example:

:simple_bot!identd@82-69-0-0.dsl.in-addr.zen.co.uk JOIN :#irchacks
:calvino.freenode.net 332 simple_bot #irchacks :IRC Hacks channel.
:calvino.freenode.net 333 simple_bot #irchacks Jibbler 1075562584
:calvino.freenode.net 353 simple_bot @ #irchacks :simple_bot DeadEd golbeck
    Jibbler dg Monty Wilmer
:calvino.freenode.net 366 simple_bot #irchacks :End of /NAMES list.
:Jibbler!~pjm2@torax.ukc.ac.uk PRIVMSG #irchacks :Hi simple_bot
:Monty!~monty@myrtle.ukc.ac.uk PRIVMSG #irchacks :It's simple_bot!

This may look quite confusing at first, but you can look at the lines one by one and work out what it means. The first line announces that the script has joined the channel #irchacks. Lines 2-5 contain information about this channel, such as what the channel topic is, who set it and when it was set, and who is in the channel. Lines 6 and 7 show messages being sent by other users in the channel.

You may like to use Perl's regular expression features to process the output into a neater format, as you may not want to display all of this information. Taking a closer look at line 6 again:

:Jibbler!~pjm2@torax.ukc.ac.uk PRIVMSG #irchacks :Hi simple_bot

You can see that the message came from a user named Jibbler, with the login pjm2 and connecting from torax.ukc.ac.uk. The PRIVMSG indicates that this is a normal message being sent to the channel #irchacks. The contents of the message are "Hi simple_bot".

O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.