IRC bot in Perl

Discussion in 'Perl' started by XXxxImmortalxxXX, Jul 4, 2008.

  1. XXxxImmortalxxXX

    XXxxImmortalxxXX New Member

    Joined:
    Jun 27, 2007
    Messages:
    561
    Likes Received:
    19
    Trophy Points:
    0

    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
     
    Last edited by a moderator: Jul 4, 2008
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Nice one.
     
  3. XXxxImmortalxxXX

    XXxxImmortalxxXX New Member

    Joined:
    Jun 27, 2007
    Messages:
    561
    Likes Received:
    19
    Trophy Points:
    0
    Thankyou very much
     
  4. Maledikt

    Maledikt New Member

    Joined:
    Dec 19, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  5. Fatboy12345236

    Fatboy12345236 New Member

    Joined:
    Sep 12, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Just what I needed, for my IRC Channel! Thanks for the help. :D
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice