Simple IRC Bot in Perl Tutorial
Once again, by request – here is a tutorial on how to make a simple IRC bot in perl.
Wikipedia: An IRC bot is a set of scripts or an independent program that connects to Internet Relay Chat as a client, and so appears to other IRC users as another user. It differs from a regular client in that instead of providing interactive access to IRC for a human user, it performs automated functions.
Well, I really, really enjoy coding said bots – and it seriously is a simple task, if you know some simple IRC commands – and some simple programming skills are needed as well, to make the more complicated bots.
However, for this ‘hello world’ bot we will be coding, we will be using Perl – which is SO SIMPLE to use for creating IRC bots, and if you already know some perl, you could do really neat stuff with it.
Now, before we begin – you will need a perl interpreter – I would recommend ActiveState ActivePerl. Also – you will need a perl package called ‘IO::Socket’ – Most perl distributions come with it, so it might just be a good idea to ignore that bit, Now for the actual coding – open up notepad and get coding.
Connecting to the Network:
Well, using some common sense – you need your IRC bot to connect to a network of some sort, using the following code:
use IO::Socket;
$talk = IO::Socket::INET->new(PeerAddr=>’irc.9unkz0r.com,
PeerPort=>’6667′,
Proto=>’tcp’,
Timeout=>’30′) || print “Something Blew Up! $!\n”;
Now to explain this code…
The first line is just telling Perl to load the IO::Socket package.
The remaining lines are telling the bot to connect to an IRC server located at ‘irc.9unkz0r.com’ on port 6667, using the TCP meshed, and without a response from the server in a 30 second period – it will assume a ‘time-out’ and quit.
Authenticating:
You now have to get your bot to authenticate to the network, this is simple work – just use:
print $talk “USER PwnBot IRC Bot\r\n”;
print $talk “NICK PwnBot\r\n”;
Well, it looks pretty obvious now – anything that’s passed through $talk will be sent to the server,
Just a few rules you have to follow with this bit:
-USER = The bots Username, this can pretty much be anything you want, (note: this is not the bots Nickname)
-NICK = The bots Nickname, this is the name that the bot wil be identified by.
Also – Just remember to end your lines using ‘\r\n’ if you don’t the server won’t understand your commands and your bot will pretty much be useless.
Joining a Channel
Now, What use is a bot when it doesn’t join a channel? – Very…Very little.
So, next up in the script – write
print $talk “JOIN #9unkz0r\r\n”;
As you can probably see, you are using the IRC command ‘JOIN #9unkz0r’, which will make it join a channel called #9unkz0r – Simple…eh?
Starting a Loop
Now, You have to get the bot to enter a ‘loop’ to keep it running, this is a little more complicated – but still simple, I find its easy to just do:
while($answer = <$talk>)
{
#I’m looping 
}
Playing PING PONG
So, you got the script to stay alive, that’s good – but the IRC server periodically ‘pings’ its clients to see if they are still alive and connected – think of it as a game of ping pong – the ball comes to you, and if you don’t hit it back….you fail
.
So to automatically answer to PING commands pointed at you – we will be using a regular expression:
if($answer =~ m/^PING (.*?)$/gi)
{
print $talk “PONG “.$1.”\r\n”;
}
Once a PING has been detected, which is in the form of ‘PING randomized_data’, it will send ‘PONG randomized_data’ back to the server – thus keeping you on its online list.
Congratulations, You have just completed an IRC bot! – Now save the file as ‘something.pl’, now run the file in ActiveState ActivePerl – to do this, read the ActivePerl documentation.
Now check if it runs and joins said channel – if it didn’t…you suck.
Other things you can put in:
I hope by now you have noticed a pattern in what’s going on now, basically you can use normal core IRC commands like this:
print $talk “<Command>\r\n”;
So you can do stuff like
Identify to NickServ: print $talk “MSG NickServ identify <password>\r\n”;
Talk in a Channel: print $talk “PRIVMSG #9unkz0r :Fruit Salad\r\n”;
Join a Channel: print $talk “JOIN #SecondChannel\r\n”;
Leaving a Channel: print $talk “PART #SecondChannel\r\n”;
Giving a User [+O]: print $talk “MODE #9unkz0r +o Cbhacker\r\n”;





























Writing an IRC bot in Perl - Linoleum said
am July 12 2008 @ 9:07 am
[...] channels alive, logging conversation when you’re not around or just for general notifications. Cbhacker shows how a simple IRC bot can be created in perl, using just print statements and the IO::Socket [...]
Find newspaper printers in Missouri said
am July 22 2008 @ 1:01 am
Find newspaper printers in Missouri…
[...] Curtain Written by David Sullivan, a full-time journalist since 1975, involved in copy editing for most of that time. A newspaper journalist in Philadelphia, a native of Indiana, a resident of New Jersey, an executive board member of ACES,[...]…