Friday, July 30, 2004

IRC Bot

I've been playing around IRC lately, and since that is already goofing off in the highest degree, I decided to write a simple IRC bot. My day job is software development, and I thought this would be a fun diversion.

An IRC bot? What's that you ask? Well, assuming you have some background in IRC as a user, a bot is just a program that provides some service. It may fetch sports scores, log all text and produce stats, or grant ops to users that join the channel. Fancy bots do crazy stuff like let people choose music to play, run webcams, and so forth. My goals are far simpler!

My immediate goals for the bot were: keep a channel open, and greet users that join that channel. Since this is a learning experience, I decided to be more "hands on" about writing the bot, and not just take an existing one and modify it slightly.

I started by googling for existing IRC bots, preferably ones I could download and examine to see what I was getting myself into. Google turned up several written in PERL, Java, and C# - any of these languages would be fine. I have two windows boxes and one linux box, and all those languages are available. I also skimmed the IRC specs just for basic information.

In my searches I came across a JAVA API for IRC bots! I couldn't resist starting with such a great foundation, so I began reading the documentation for PircBot.

At risk of peeling back the curtain and piercing the aura of difficulty surrounding software development ;) all it took to make the bot greet users was this code:


public void onJoin( String channel, String sender, String login, String hostname )
{
sendMessage( channel, "Hail " + sender + "! Welcome to the harem." );
}


In addition to this, I just had to fill out the correct server and channel to join.

To be honest, it took me longer to update my Java SDK to the latest version and remember how to build and run Java apps than it took me to get a bot up, running, and greeting users. I also enhanced the bot code so it takes the bot name on the command line. Thus, once the bot is ready, I can easily launch several copies and give each one a different name.

Now, this implementation isn't very sophisticated. For example, the bot greets itself when it joins the channel. I decided that looked really stupid so I made a minor enhancement:


public void onJoin( String channel, String sender, String login, String hostname )
{
if ( getNick().equals( "harembot" ) && !sender.equals( "harembot" ) )
sendMessage( channel, "Hail " + sender + "! Welcome to the harem." );
}


The extra code does this: only a bot named "harembot" will greet users, and the bot named "harembot" will not greet itself.

That wasn't so hard, now was it?!

I left the bot running over night and found it had been disconnected somehow. So, I added in two different methods for reconnecting. The first cycles through the various servers until the bot connects successfully, and the second just keeps trying to reconnect to the same server. Right now I'm running both methods to see which one results in a more reliable presence.

The other thing I added was ops granting. This was interesting as I had to learn about various modes and the messages involved when a user gets ops or has ops removed. I may cover this later, but right now I also have my various bots recognizing each other and granting ops when they join. I need to take it a step further and implement a system where I can add people to a list for the same treatment.

So this is what I do sometimes to relax sometimes, rather then veg in front of the TV (which hasn't even been turned on in 5 days). However tonight I'll probably watch something in my Netflix queue.

No comments: