CAZine: issue 5, November 2009

CAZine: issue 5, November 2009

Coders Corner: Intro to Perl IRC Bots

By: Gen Gothicfreak

There are many ways to code IRC bots and lots of modules to use while doing this.
My preferred method is using the basics. IO::Socket. But there are a few ways of doing this.
Some of the modules that could be used are:

IO::Socket (my preferred)
IRC::Chat (will handle most connections/msgs etc)

I have never tested but feel you could use IO::Socket::SSL for encrypted connections (again never tested just a thought)

There are other modules you could use like DBI for databases or colour coding modules, although I would as with any other
perl script recomend using strict. Makes things simpler for the larger scripts.

Now let me explain the basics before I show any code. Let me just say and I CANNOT stress this enough. COMMENT YOUR CODE
it makes it easier going back through to mod, and also if you decide to distribute. COMMENT THE CODE!

Set basic variables that you might need (botname, email addy, realname, owner, database file(s), log file(s) etc)

Once you get the basic modules loaded and variables set we move to the interesting part.
So far you should get something like this:

  1. use strict;
  2. use IO::Socket;
  3. use DB_File;
  4.  
  5. $botnick = ‘PerlBot’; #nickname for the bot
  6. $botname = ‘phj33r’; #Realname for bots
  7. $botemail = ‘awesome@perl.coder.edu’; #The bots email
  8. $botmodes = ‘+B’; #Modes the bot needs to set WARNING!!! some networks require certain modes to be set on bots like ‘+B’

Keep in mind if you use DB_File i use that for the Berkley Database,
you might be using something else or nothing if you don’t use

a database or if you are using a flat file.

Next comes the connection and basic loop (I’ll explain after i show):

  1. $irc = IO::Socket::INET->new(
  2.                      PeerAddr => $server,
  3.                      PeerPort => $port,
  4.                      Proto => ‘tcp’ ) or die "Could Not Make Connection\n";
  5.  
  6. #sleep for a little
  7. sleep(2);
  8.  
  9. #Register with the irc server
  10. print $irc "USER $botname $botname $botname :$botname\n";
  11. print $irc "NICK $botnick\n";
  12. #Another quick nap
  13. sleep(2);

Ok Basically decide on a variable (i used $irc) to set the socket to. give the basic IO::Socket connection info (see above)

After that i have it sleep for a second, not really needed but i find it easier on cpu usage.

At that point you send the USER and NICK info. You can get all that
info by reading RFC1459 (load of information thats great for making
bots there.

Then i have it sleep a little longer (again just for cpu usage)

After that you need a way to make sure the socket is active and you can still move on. I use something simple

  1. while ($irc) {
  2. }

Quick simple easy eh?
Also doing it this way if its disconnected you can put a reconnect call in there after the {} And allow the bot to reconnect if its disconnected by ping peer etc.
!!WARNING!!
You might want to code a check in there, if you put a die or disconnect command into the bot and it has a reconnect feature it will reconnect.

Other then that i put a:

  1. $line = <$irc>;
  2. print "$line\n";

That way you can watch the syntax if you don’t want to go through and learn all of the RFC. It helps with the entire coding process for testing purposes.

Coming next issue, basic commands and how to turn the bot into a background process!

VN:F [1.7.9_1023]
Rating: 10.0/10 (1 vote cast)
CAZine: issue 5, November 200910.0101

Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13

About the Author