RandomBase.com logo
Announcement:
World of Warcraft Bots, need some help in choosing the best bot?

News Archives

Statistics

  • There are 17 users online.
  • Most users ever online was 424 on 03/16/08.
  • Our poor server had to serve 34 pages in the last 15 minutes.
  • And yet he managed to generate this page in 0.003 seconds.

Affiliates & friends

RandomBase.com isn't just one website - it is more.

IRC bot in Perl

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.



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:

use IO::Socket;

$con = IO::Socket::INET->new(PeerAddr=>'irc.randombase.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.randombase.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:

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, 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

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:

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.

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.

print $con "PRIVMSG #bot_channel :Hello from randombase.com!\r\n";


Leaving a channel

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

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


Making a user operator

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

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

use IO::Socket;

$con = IO::Socket::INET->new(PeerAddr=>'irc.randombase.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";
	}
}