- From the Editor
- Trivia
- New Look and Plugins
- The Login
- The Mentality
- Staff and Exec News
- Ikioi Interview
- Fun Stuff
- myViral
- Devil's Advocate
- Former Staff Bio: Ker Betterman
- Coders Corner: Intro to Perl IRC Bots
- Legality of The MP3
- Copyright Law
- MP3 effects on Copyright
- Anomalies of Code
- This Months Challenge
- Previous Months Challenge
- The Code
- Restructure
- myMusic
- Comments (3)
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:
-
use strict;
-
use IO::Socket;
-
use DB_File;
-
-
$botnick = ‘PerlBot’; #nickname for the bot
-
$botname = ‘phj33r’; #Realname for bots
-
$botemail = ‘awesome@perl.coder.edu’; #The bots email
-
$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):
-
$irc = IO::Socket::INET->new(
-
PeerAddr => $server,
-
PeerPort => $port,
-
-
#sleep for a little
-
-
#Register with the irc server
-
#Another quick nap
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
-
while ($irc) {
-
}
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:
-
$line = <$irc>;
-
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!
Very nice! Overall looks amazing! Great job Zerhash and all who contributed!
Great job! I enjoyed the articles as well as the interview. Keep up the good work!
Answers for Trivia CAZine: issue 4, October 2009:
1. Cat
2. Pengo
3. Zebulun
4. Prothis
5. 2004