Previous Section  < Day Day Up >  Next Section

Hack 71 Bridge Two Infobots

figs/moderate.gif figs/hack71.gif

Combining the knowledge of infobots running on different servers is a useful way of sharing information between two communities.

Infobots are one of the more popular "fun" IRC bots out there. Usually, they are set up so that they create their own database of information nuggets—known as factoids—by monitoring conversations in channels. You can also add, delete, and update factoids manually. An infobot configured like this quickly turns into a "community memory," storing hard-to-remember facts like birthdays, email addresses, and references to past embarrassments concerning the members of the IRC community it serves. In this hack, we'll look at how to allow two communities to access each other's memories, by bridging two infobots on different servers.

Let's say that there are two infobots, called Norfolk and Dipsy. Norfolk and Dipsy are connected to different IRC servers. Imagine that the owners of Norfolk want Dipsy to appear on their channel too. What they need is a "bridge bot" on their channel that will be called "Dipsy" on their server, but that will connect to the second server under a different name and talk to the real Dipsy off-channel. Of course, there's no reason that different name can't be "Norfolk," assuming the name is free on the second server. In this case, the bridge bot can be completely symmetrical, sitting on-channel next to Dipsy on the second server and querying the real Norfolk off-channel on the original server. Both channels now appear to have both bots sitting on them.

Note that regardless of how the real infobots are set up, the "dummy" bots will respond only if directly addressed in a channel, like so.

dipsy: what is the meaning of life?

The format of infobot responses to on-channel and off-channel messages is slightly different, so the bridging isn't always going to be perfect. There's nothing in this hack that is infobot-specific, so it should work with any two bots that respond to off-channel messages in the same way as on-channel ones, or even with real people.

Don't forget to tell the two infobots to ignore their new companions, or they will end up flooding the channels!


The bridge bot can be implemented in a particularly elegant fashion with Java by creating two instances of the same PircBot-based class and making each instance a member class of the other.

11.5.1 The Code

The bridge bot lives in BridgeBot.java and makes use of the PircBot package to connect to IRC servers. Note the getName() accessor method is used to get the current name of a bot.

import org.jibble.pircbot.*;



public class BridgeBot extends PircBot {



    public BridgeBot otherBot;

    public String channelName;



    public BridgeBot(String name, String channelName) {

        this.setName(name);

        otherBot = this;

        this.channelName = channelName;

    }



    // Handle on-channel messages.

    public void onMessage(String channel, String sender,

            String login, String hostname, String message) {

        if (!sender.equals(getName( )) && message.startsWith(getName( ))) {

            // Pass the message on to the real version of this bot.

            otherBot.sendMessage(getName( ), message);

        }

    }



    // Handle off-channel messages

    public void onPrivateMessage(String sender, String login,

            String hostname, String message)  {

        if (!sender.equals(getName( )) && sender.equals(otherBot.getName( ))) {

            // Make the otherBot send a message to its channel.

            otherBot.sendMessage(otherBot.channelName, message);

        }

    }

   

}

To kick the bridge bot into life, you will need to create a main method that creates two instances of the BridgeBot class. You could place this main method in a new file, BridgeBotMain.java. After each bridge bot is instantiated, it is supplied with a reference to the other BridgeBot object to allow messages to be passed on.

public class BridgeBotMain {



    // Configuration settings for each BridgeBot.

    public static final String firstServerName = "ircserver.domain.com";

    public static final String firstBotName = "Norfolk";

    public static final String firstChannel = "#channel1";



    public static final String secondServerName = "ircserver.anotherdomain.com";

    public static final String secondBotName = "Dipsy";

    public static final String secondChannel = "#channel2";



    public static void main(String[] args) throws Exception {



    BridgeBot firstBot = new BridgeBot(firstBotName, firstChannel);

        BridgeBot secondBot = new BridgeBot(secondBotName, secondChannel);        



        // Supply each BridgeBot with a reference to its partner.

        firstBot.otherBot = secondBot;

        secondBot.otherBot = firstBot;



        // Make the bots join servers and channels.

        firstBot.connect(firstServerName);

        firstBot.joinChannel(firstChannel);

        firstBot.channelName = firstChannel;

        

        secondBot.connect(secondServerName);

        secondBot.joinChannel(secondChannel);

        secondBot.channelName = secondChannel;

    }

}

11.5.2 Running the Hack

Compile the classes with the javac command:

C:\java\BridgeBot> javac -classpath .;pircbot.jar *.java

Launch the two bots by running the main method in the BridgeBotMain class:

C:\java\BridgeBot> java -classpath .;pircbot.jar BridgeBotMain

Steve Jolly

    Previous Section  < Day Day Up >  Next Section