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
#75
Maintain Operator Status
Losing operator status in your own channel can be a nuisance. Create an IRC bot that automatically grants operator status to those who deserve it
The Code
[Discuss (0) | Link to this hack]

The Code

Create a source file calledOpBot.java:

import org.jibble.pircbot.*;
import java.io.*;
import java.util.*;

public class OpBot extends PircBot {

    private File file = new File("trustedlist.txt");
    private Vector trustedlist = new Vector( );

    public OpBot(String botName) {
        setName(botName);
        setLogin(botName);
        // Try to read in the trusted list from the file.
        try {
            FileReader fileReader = new FileReader(file);
            BufferedReader reader = new BufferedReader(fileReader);
            String nickname = null;
            while ((nickname = reader.readLine( )) != null) {
                trustedlist.add(nickname);
            }
            reader.close( );
        }
        catch (Exception e) {
            System.err.println("Problem reading from " + file + ": " + e);
        }
    }

    public void onPrivateMessage(String sender, String login,
            String hostname, String message) {
        String nickname;
        
        // Only authorized users can send commands to the bot.
        if (trustedlist.contains(sender)) {
            if (message.startsWith("add ")) {
                nickname = message.substring(4);
                // Add the user to the trusted list and save.
                trustedlist.add(nickname);
                sendMessage(sender,"added " + nickname);
                saveTrustedList( );
            }
            else if (message.startsWith("remove ")) {
                nickname = message.substring(7);
                if (trustedlist.contains(nickname)) {
                    // Remove the user from the trusted list and save.
                    trustedlist.remove(nickname);
                    sendMessage(sender,"removed " + nickname);
                    saveTrustedList( );
                } else {
                    sendMessage(sender, "nickname not in list");
                }
            }
            else {
                sendMessage(sender, "invalid command");
            }
        }
    }

    public void onJoin(String channel, String sender,
            String login, String hostname) {
        if (trustedlist.contains(sender)) {
            // Op the user if he is on the trusted list.
            op(channel, sender);
        }
    }

    private void saveTrustedList( ) {
        try {
            FileWriter writer = new FileWriter(file);
            Iterator it = trustedlist.iterator( );
            while (it.hasNext( )) {
                writer.write(it.next( ) + "\n");
            }
            writer.flush( );
            writer.close( );
        }
        catch (Exception e){
            System.out.println("Error saving trustedlist: " + e);
        }
    }
}

You'll need a main method to instantiate the bot and tell it which server to connect to and which channels to join. You can save this as OpBotMain.java:

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


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.