O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  


 
Buy the book!
IRC Hacks
By Paul Mutton
July 2004
More Info

HACK
#47
Write a Plug-in for PPF
Now that you know how to use the PircBot Plug-in Framework and configure its various plug-ins, let your creative juices flow by making your own plug-in
The Code
[Discuss (0) | Link to this hack]

The Code

The simple plug-in will be called Simple, so create a new directory called Simple under the plugins directory. Then create the directory path for the source code under that (src/net/sourceforge/ppf/plugin/simpleplugin). You should end up with something like .

Figure 1. The directory structure for the Simple plug-in

Now create a file called SimplePlugin.java in the simpleplugin directory:

package net.sourceforge.ppf.plugin.simpleplugin;

import net.sourceforge.ppf.PPF;
import net.sourceforge.ppf.PPFPlugin;
import net.sourceforge.ppf.util.PPFHelp;

public class SimplePlugin extends PPFPlugin {

    static final String PLUGIN_VERSION = "1.0";
    // The commands the plug-in will respond to.
    static final String COMMAND_TIME = "!time";
    static final String COMMAND_HELLO = "!hello";

    public SimplePlugin( ) {
        // Set the plug-in version information.
        setVersion(PLUGIN_VERSION);

        // Set the help responses for this plug-in.
        setHelp(COMMAND_TIME, new PPFHelp(COMMAND_TIME, 
                "Show the time of the server that the bot is running on",
                PPF.AUTH_NONE));
        setHelp(COMMAND_HELLO, new PPFHelp(COMMAND_HELLO,
                "The bot says hello back to you", PPF.AUTH_NONE));
    }

    // Respond to channel messages.
    public void onMessage(String channel, String sender, String login,
            String hostname, String message) {
            
        if(message.equalsIgnoreCase(COMMAND_TIME)) {
            String time = new java.util.Date( ).toString( );
            getBot( ).sendMessage(channel, sender +
                    ": The time where I am is currently: "+ time);
        } else if(message.equalsIgnoreCase(COMMAND_HELLO)) {
            getBot( ).sendMessage(channel, "Hi there " + sender + "!");
        }

    }

}


O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.