IRC bot in Perl

Introduction

IRC is a very easy protocol to communicate with once you get the basics. I'll be showing how to create a 'bot' (=robot) in the well-known scripting language Perl. All the commands sent to the server can also be ported to any other programming or scripting language.

I will not go too deep into detail in every function used, but use small snippets to illustrate.[/COLOR]

Prerequisites

Before you can start 'botting', you need a Perl package named IO::Socket. Most Perl distributions come with this package already so you will probally just ignore this step.

Connecting

To get started with your bot, you should open a connection with the irc server first. This can be done easily with this piece of code:

Explanation: the first line is just to include the connection package, the other lines tell the package to connect to a server named irc.ircwebaddress.com on port 6667, using the tcp method. It will timeout after 30 seconds without response. If there is an error, the script will print Error! [error message]. Remember $con, because all data sent to the server will have something to do with it.

Authenticating

Assuming you have a successful connection with the IRC server, it will start asking nasty questions about who you are and stuff. If you do not send this data, you'll get disconnected. When authenticating, you should send two things. Your 'user' which can be about anything, some people put a quote here, don't make it too short or too long, few words will do. And your 'nick', which is the nickname that'll appear in channels. You can send this in a very easy way, like this:

There isn't much explanation to this, however you shouldn't forget about ending *all* your messages to the server with \r\n, this means your message has ENDED. If you don't, the server will interprete it as one big line of gibberish and say nasty things about you.

Joining a channel

Now you're not gonna sit and wait until Jesus comes from to the sky,(lol) you want to do something and let's say we want to join a channel. This can be done very easily with a very difficult command (hehe): JOIN #channel

Code:

print $con "JOIN #bot_channel\r\n";

Now you're in a channel, if it didn't exist it will be created. Now it's time to enter the endless loop...

Opening the loop

A bot needs some way of loop to keep running, in this loop it can answer replies from the server or send new ones. Now you need to create it too! Luckily, this isn't too hard either, this is a good example of such a loop:

Code:

while($answer = <$con>)
{
#cool actions come here
}

As long as you are in this while(), you are connected! There is one more thing to do to prevent disconnecting, and that is..

Answering PING requests

Every X seconds, an irc server will send a PING to you, to check if you're still alive. You should answer this as soon as possible because otherwise you will get disconnected with a timeout error. The answer to this should be added in the connection loop you have created before.

Code:

if($answer =~ m/^PING (.*?)$/gi)
{
print $con "PONG ".$1."\r\n";
}

What does this do? It uses a regular expression to see if the server has sent a PING request containing some random data after that. If it has sent this, it will send 'PONG random_data' back, this is enough for the IRC server to keep you on its alive list.

What can I do after connecting?

So much! I will list the most used commands with a quick example.

Sending a message
If you want to sent a message to a channel or a user, you should use PRIVMSG. If you don't add # in front of the target, it will try to send to a single user in a private message.

Code:

print $con "PRIVMSG #bot_channel :Hello from XXxxImmortalxxXX!\r\n";

Leaving a channel

Don't like the channel you are currently in? Use PART.

Code:

print $con "PART #bot_channel:Goodbye from XXxxImmortalxxXX!\r\n";

Making a user operator

'opping' someone is only possible when you're an operator yourself, of course.

Code:

print $con "MODE #bot_channel +o Nickname_of_target\r\n";

Complete code

This code is just enough to connect to a server, join a channel, and say 'hello' back .