|MySQL Conference and Expo April 14-17, 2008, Santa Clara, CA|
Pass Notes to Other Users
Author's note: Whether you're using IRC at work or for chatting to friends, it's always useful to run an IRC bot that can pass on short messages to other users. This article shows you how to make your own IRC bot that does just that.
For many people, IRC is more than just a place to chat. Social groups form quite easily in channels; after all, you all share the same interest, right? IRC therefore becomes the natural place to talk to this social group. You may not even know the email addresses of the people you talk to, simply because you don't need to. IRC lets you chat in real time, which is often more convenient than waiting for a response to an email. IRC also lets you transfer files directly from one client to another.
Such dependence on IRC sometimes makes people forget that there are alternative communication mediums. Indeed, some people even become frustrated when they have to resort to alternatives! If the person you want to talk to has left the server for a while (perhaps has gone to bed or to the shops) and you don't know his email address, you could use an IRC bot to pass on your message when he comes back.
A bot that passes on messages doesn't have to be too complicated. All it has to do is allow users to give it new messages, store them, and then pass them on when appropriate:
The next time DeadEd joins the channel, the bot should pass the message to DeadEd:
We can use a HashMap to store a list of messages to pass on, allowing more than one message to be sent to any one user. The HashMap will be indexed by nickname, and each entry will point to an ArrayList that contains messages for that user.
Notice that the HashMap keys must be converted to lowercase. This effectively makes the nicknames case insensitive, so a message that is left for "Paul" can also be received by "paul."
The onMessage method is invoked whenever someone sends a message to the channel. This method checks to see if a user has entered the "tell" command—if so, it adds the message to the HashMap.
When a user joins the channel, the onJoin method is invoked. If there are any messages for this user, they are sent to the channel and then removed from the HashMap.
To instantiate the bot, you will need a main method. Create this in
You can also tell the bot to join more than one channel—simply modify the joinChannel method call so it contains a comma-separated list, for example:
Messages will be accepted from all channels and delivered to the first one the recipient joins.
Running the Hack
Compile the bot like so:
Run the bot with:
Check back to this space next week for more excerpts from IRC Hacks. You'll learn how to perform feats of math; how to announce newsgroup posts; and how to use IRC within a screen.
View catalog information for IRC Hacks
Return to Web DevCenter.
Showing messages 1 through 1 of 1.