Web DevCenter    
 Published on Web DevCenter (http://www.oreillynet.com/javascript/)
 See this if you're having trouble printing code examples


O'Reilly Book Excerpts: IRC Hacks

Hacking IRC

by Paul Mutton

Author's note: Many of my colleagues use IRC, and a few of us also have webcams so we can see what each other is up to. By combining an IRC client and a webcam, we can let people know what we're doing, even if it's not readily apparent by looking at the webcam.

Moderate Hack #30 Add Your IRC Nickname to Your Webcam


Webcams are the best way to let people know exactly what you're doing—after all, a picture is worth a thousand words, or so they say. Adding extra text to your webcam image can reveal a lot more, particularly if you are not there to be seen. This hack shows you how to display your current IRC nickname on your webcam image.

The Windows webcam software used in this hack is Dorgem. This unusual name doesn't actually mean anything; it is a combination of letters that could sound like a word and didn't return any results on search engines at the time of its creation. So the chances are that if you Google this, you'll be able to find it easily. If not, you can download it from http://dorgem.sourceforge.net.

One useful feature of Dorgem is that it allows you to overlay captions on your webcam image. These captions can be either bitmap images or plain text. If you go for the plain text option, you can choose to overlay a string that you type in, or tell it to read the contents of a file. The latter choice allows you to include whatever text is in the file, so you can easily create a script for your IRC client that updates the file with your current nickname.

Writing Your Nickname to a File

As this hack is to run on a Windows machine, it will use mIRC as the IRC client. To update the contents of the file every time you change your nickname, it's going to be necessary to trap that event. The easiest way of doing this is to override mIRC's /nick command so that it saves the new nickname in a file.

Open up the mIRC Scripts Editor (Tools → Scripts Editor...) and select the Aliases tab. Now create the alias for the /nick command shown in .

Modifying the behavior of mIRC's /nick command
Figure 1. Modifying the behavior of mIRC's /nick command

This overrides the /nick command and causes a sequence of three other commands to be executed instead. The first of these passes the $1 variable to the /nick command. $1 is the first argument that is supplied to your alias, so this has the effect of changing your nickname as usual.

The second line echoes a string to your active window so you can receive a confirmation of what your webcam message will say. $2- is a variable that refers to the second argument, including everything after it. This means you can add some more details after your nickname, and they will also be included in this message.

The third line writes the same message to a file. The -c means that the file will be cleared before it is written to. In this example, the message will be written to c:\temp\nick.txt.

The alias lets you add extra parameters to the /nick command in case you want to add more detail to your webcam message. For example, if Paul wants to change his nickname to Paul|desk-less, he can do so in the usual way by entering:

/nick Paul|desk-less

This would cause nick.txt to contain:

Current IRC nick: Paul|desk-less ...

If he wanted to include some more information about his nickname, he could simply add some more details after the /nick command:

/nick Paul|desk-less they came and stole my desk :(

This would cause nick.txt to contain:

Current IRC nick: Paul|desk-less ... they came and stole my desk :(

Now that you've configured your IRC client to keep this file up-to-date, you just have to set up Dorgem to display its contents on your webcam image.

Displaying File Contents in Dorgem

Assuming Dorgem is up and running properly, you can add a caption to the webcam image by clicking on the Caption Settings button, as shown in .


Figure 2. The main Dorgem window

In the Caption Settings dialog (), click on Add to add a new caption overlay. When prompted, select a Text caption and click on OK.


Figure 3. Adding a new type of caption

In the Text Caption Settings dialog, shown in , give the caption a meaningful name, such as "IRC nick." This will not appear on the webcam image, but it will help you work out what this caption is used for if you end up adding any others.

Make sure the Enable checkbox is checked and that the length is set to 0 (unlimited). Enter the filename in the File box. For best results, you should make the text transparent, otherwise it will be printed on an opaque rectangle. If you want to make it stand out better, you could add a full shadow and use a contrasting color for background and foreground.


Figure 4. Setting up the caption to read from a file

Clicking on the Position button reveals the Caption Position dialog, as shown in . This example places the caption at the bottom right of the webcam image. If you know the dimensions of your webcam image, you can even experiment with absolute positioning.


Figure 5. Setting the position of the caption

The Results

shows the final results: a webcam image with the user's current IRC nickname and personalized message in the bottom right.


Figure 6. The webcam image with IRC nickname and message

When people look at your webcam now, they will no longer be left guessing what you're up to when you're not there.

IRC Hacks

Related Reading

IRC Hacks
100 Industrial-Strength Tips & Tools
By Paul Mutton

Moderate Hack #50Pass 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:

<Jibbler> tell DeadEd to look at http://www.jibble.org/comicbot/
<TellBot> Okay, Jibbler

The next time DeadEd joins the channel, the bot should pass the message to DeadEd:

* DeadEd has joined #irchacks
<TellBot> DeadEd, Jibbler asked me to tell you to look at
    http://www.jibble.org/comicbot/

The Code

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.

Create a file called TellBot.java:
import org.jibble.pircbot.*;
import java.util.*;

public class TellBot extends PircBot {
    
    // A map of String (nickname) to ArrayList (message Strings).
    private HashMap messages = new HashMap( );
    
    public TellBot(String name) {
        setName(name);
    }
    
    public void onMessage(String channel, String sender, String login,
            String hostname, String message) {
        
        String[] tokens = message.split("\\s+");

        // Check for the "tell" command.
        if (tokens.length > 2 && tokens[0].equalsIgnoreCase("tell")) {
            String nick = tokens[1];
            message = message.substring(message.indexOf(nick) + nick.length( ) + 1);
            
            // Convert the nickname to lowercase for use in the HashMap.
            String key = nick.toLowerCase( );
            ArrayList list = (ArrayList) messages.get(key);
            if (list == null) {
                // Create a new ArrayList if the HashMap entry is empty.
                list = new ArrayList( );
                messages.put(key, list);
            }
            
            // Add the message to the list for the target nickname.
            list.add(sender + " asked me to tell you " + message);
            sendMessage(channel, "Okay, " + sender);
        }
        
    }
    
    public void onJoin(String channel, String sender,
            String login, String hostname) {
        
        // Convert the nickname to lowercase to get the HashMap key.
        String key = sender.toLowerCase( );
        ArrayList list = (ArrayList) messages.get(key);
        if (list != null) {
            // Send all messages to the user.
            for (int i = 0; i < list.size( ); i++) {
                String message = (String) list.get(i);
                sendMessage(channel, sender + ", " + message);
            }
            // Now erase all messages for this user.
            messages.put(key, null);
        }
    }
    
}

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 TellBotMain.java:

public class TellBotMain {
    
    public static void main(String[] args) throws Exception {
        TellBot bot = new TellBot("TellBot");
        bot.setVerbose(true);
        bot.connect("irc.freenode.net");
        bot.joinChannel("#irchacks");
    }
    
}

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:

bot.joinChannel("#irchacks,#jibble,#pircbot");

Messages will be accepted from all channels and delivered to the first one the recipient joins.

Running the Hack

Compile the bot like so:

javac -classpath .;pircbot.jar *.java

Run the bot with:

java -classpath .;pircbot.jar TellBotMain

The Results

shows the bot in action. As soon as DeadEd joins the channel, TellBot passes my message on to him.

TellBot passing on a message from Jibbler to DeadEd
Figure 7-7. TellBot passing on a message from Jibbler to DeadEd

This type of bot is very popular among communities. You'll find yourself sending fewer emails once you start using this.

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.

Paul Mutton is the author of the PircBot IRC framework and several other Java programs that can be found on his web site.


View catalog information for IRC Hacks

Return to Web DevCenter.

Copyright © 2009 O'Reilly Media, Inc.