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";
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";
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";
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
}
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 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";
}
}

