Previous Section  < Day Day Up >  Next Section

Hack 74 Invite Users into Channels

figs/moderate.gif figs/hack74.gif

The safest way to keep a channel under control and free from unwanted intruders is to mark it as invite-only. Of course, you need a nice way of letting the regular users in.

Marking a channel as invite-only means that you can join that channel only if an operator in that channel invites you. This is a great way of keeping out unwanted or abusive visitors. To mark a channel as being invite-only, you must be a channel operator to apply the mode +i, for example:

/mode  #irchacks  +i 

If a user now tries to join this channel, she will be told that she cannot do so because it is invite-only. To send an invitation to another user, you can use the /invite command, for example:

/invite  Jibbler #irchacks 

Marking your channel as secret (+s) can also help to avoid unwanted attention. This prevents the channel being listed when users execute the /list command. If people don't even know your channel exists, they can't even begin to contemplate causing any trouble there.

Of course, the whole invite-only solution is not perfect. If you have just connected to the server, the only way you would be able to join the channel is if you get invited. To get invited, you would probably need to send a private message to an operator in the channel and ask to be invited. The operator then invites you into the channel, and you are able to join it. But what if the operator isn't there to invite you? And how can you tell who's an operator in that channel if you can't see who's in it?

12.3.1 Create an InviteBot

One obvious solution is to create an IRC bot that is responsible for handing out invitations. This will be called InviteBot. The bot will sit in the channel and accept invitation requests via private message. To ensure that only valid users are able to use the bot, the invitation request will actually be a password. You can define what this password is and then share it with everybody who is allowed to use the channel. If you send the bot the correct password, it will send you an invitation.

Remember to make sure the bot is a channel operator, otherwise it won't be able to send any invitations.

12.3.2 The Code

Save the following as InviteBot.java:

import org.jibble.pircbot.*;

import java.util.*;



public class InviteBot extends PircBot {

    

    private String channel;

    // The invitation request password.

    private String password = "password";

    

    public InviteBot(String name, String channel) {

        setName(name);

        this.channel = channel;

    }

    

    // Return the channel that this bot lives in.

    public String getChannel( ) {

        return channel;

    }

    

    // Accept private messages.

    public void onPrivateMessage(String sender, String login,

            String hostname, String message) {

        

        // Send an invitation if the password was correct.

        if (message.trim( ).equals(password)) {

            sendInvite(sender, getChannel( ));

        }

    }

    

}

This is quite a simple bot. You will notice that the password is set and stored in the password field. Feel free to change this to whatever you want, but remember not to use a sensitive password, as you will be sharing this with other users!

When the bot receives a private message, the onPrivateMessage method gets called. The bot then checks to see if the message matches the password. If it does, the sender of the message is sent an invitation to join the channel. The sender can then join the channel.

Now you need a main method to launch InviteBot. When you construct InviteBot, you must tell it which channel it is going to live in (#irchacks in this case). Save the following as InviteBotMain.java:

public class InviteBotMain {

    

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

        InviteBot bot = new InviteBot("InviteBot", "#irchacks");

        bot.setVerbose(true);

        bot.connect("irc.freenode.net");

        bot.joinChannel(bot.getChannel( ));

    }

    

}

12.3.3 Running the Hack

Compile the bot like this:

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

You can then run the bot like this:

C:\java\InviteBot> java -classpath .;pircbot.jar InviteBotMain

12.3.4 The Results

In the channel, Paul sets the bot up with operator privileges and sets the channel as invite-only:

* Paul sets mode: +o InviteBot

* Paul sets mode: +i

Jibbler comes along and tries to join the channel, but is told he can't:

/join  #irchacks 

#irchacks unable to join channel (invite only)

Now, rather than pestering a channel operator and asking her to invite him in, Jibbler can now just send the password to InviteBot:

/msg InviteBot  password 

If Jibbler got the right password, he will receive a message similar to this:

* InviteBot (identd@registered.freenode) invites you to join #irchacks

Jibbler is now able to join the channel. If Jibbler's IRC client was set up to automatically join a channel when invited, he won't even need to type /join #irchacks.

    Previous Section  < Day Day Up >  Next Section