Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Perl (http://www.go4expert.com/articles/perl-tutorials/)
-   -   IRC bot in Perl (http://www.go4expert.com/articles/irc-bot-perl-t11831/)

XXxxImmortalxxXX 4Jul2008 12:21

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:

Code:

use IO::Socket;

$con = IO::Socket::INET->new(PeerAddr=>'irc.ircwebaddress.com',
                            PeerPort=>'6667',
                            Proto=>'tcp',
                            Timeout=>'30') || print "Error! $!\n";

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:

Code:

print $con "USER helloiamabot do you love me\r\n";
print $con "NICK RandomBot\r\n";

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 :).
Code:

use IO::Socket;

$con = IO::Socket::INET->new(PeerAddr=>'irc.ircwebaddress.com',
                            PeerPort=>'6667',
                            Proto=>'tcp',
                            Timeout=>'30') || print "Error! $!\n";

print $con "USER helloiamabot do you like me\r\n";
print $con "NICK RandomBot\r\n";
print $con "JOIN #bot_channel\r\n";
while($answer = <$con>)
{
        print $answer; #Show server reply
        if(($answer =~ /PRIVMSG/) && ($answer =~ /hello/i))        #Regex to see if it was a message, way too simple so needs tweaking!
        {
                print $con "PRIVMSG #bot_channel :Hello\r\n"; #Reply to the message, don't forget about the space between #channel and :
        }
        if($answer =~ m/^PING (.*?)$/gi) #Answer ping requests
        {
                print $con "PONG ".$1."\r\n";
        }
}

Hope this helps anyone just got bored and decided to post this

pradeep 7Jul2008 12:33

Re: IRC bot in Perl
 
Nice one.

XXxxImmortalxxXX 7Jul2008 12:35

Re: IRC bot in Perl
 
Thankyou very much

Maledikt 20Dec2008 05:29

Re: IRC bot in Perl
 
Very informative post. It is clear you know a lot about writing code. I would recommend using a spell checker whilst typing in your post though, as I noticed a couple of typos in your post.

With respect where it is due,
Maledikt.

Fatboy12345236 19Sep2009 10:58

Re: IRC bot in Perl
 
Just what I needed, for my IRC Channel! Thanks for the help. :D


All times are GMT +5.5. The time now is 03:29.