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.
Running an IRC channel can be
a frustrating experience if you have to manage the ops yourself. Some
networks implement IRC Services such as NickServ [Hack #5] and ChanServ [Hack #9] to bring some order to the
chaos, but there are plenty that don't. A popular
solution is to run an op bot to handle things
for you. It will sit on your channel tirelessly, granting status to
those it trusts and placidly ignoring the cries of
"opme!" from those it
doesn't. Barring ping time outs, server downtime,
and the wrath of the IRC admins, it will save you from the vexation
of discovering that everyone with ops has just quit.
 |
Always check your IRC
network's policy on bots before running one. Some
disallow bots, particularly if they are used for the purpose of
opping and de-opping users.
|
|
There are some ready-made op bots out there, but
let's face it—it's far more
fun to write your own, and you'll learn something
along the way. This hack shows how to make a simple op bot that you
could extend to get something a bit more fully featured. It runs on a
single channel and contains a single list of
trusted
people, who get opped when they join the channel and who can add or
remove other people from the list. For convenience, the bot saves a
copy of the list to a file in its working directory whenever
it's updated, so make sure it runs with the
necessary file permissions.
You can add nicknames to the
bot's list by editing the
trustedlist.txt file before the bot is started.
Create this file and add your own nickname to it. If you are in this
list and you join the same channel as the bot, it will op you. You
can also add nicknames while the bot is running. To add a nickname,
you can send a private message to the bot:
/msg opbot add nickname
Likewise, you can also remove nicknames from the
bot's list at runtime:
/msg opbot remove nickname
 |
The bot will be able to op people only if it has operator status
itself. On IRC servers that do not run any channel services, the bot
can get such status only if it is the first client to join a channel
or if an existing op in the channel ops it.
|
|
12.4.1 The Code
Create a source file called OpBot.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");
}
}
12.4.2 Running the Hack
Compile the bot like so:
C:\java\OpBot> javac -classpath pircbot.jar;. *.java
And run it:
C:\java\OpBot> java -classpath pircbot.jar;. OpBotMain
The bot will start up and get ready to op people in your channel.
12.4.3 Hacking the Hack
This is a pretty simple op bot. There are a few eventualities that it
does not currently deal with. For example, what if you change your
nickname while you are in the channel—should it de-op you?
Worse still, what if someone else tries to use your nickname to get
operator status in your channel? This hack forms the basis of
something better—you could modify it to op only users with the
correct host masks and nickname or even accept passwords through
private messages from users who wish to be opped, as with the
InviteBot [Hack #74] .
—Steve Jolly
|