Hack 64 Welcome Visitors Automagically


your IRC channels."
Can I ask a
question?" Users of busy channels will often observe
newcomers popping in and asking this. You may wonder why they
don't just ask their question, allowing people to
choose whether or not to answer it, rather than having to answer the
redundant question as well.
10.2.1 Channel Rules
Many IRC channels have a basic set of
ground
rules associated with them.
The regular inhabitants of the channel generally know these rules and
abide by them. A lot of rules are common sense and are put in place
to avoid wasting people's time. Busy IRC channels
invariably have lots of first-time visitors. Some of these visitors
tend to be unaware of the rules, including the ones that you may
consider to be common sense! Consider including the rules in a
welcome message that is sent as soon as newcomers join the channel.
10.2.2 Welcome Options
Welcome messages can be sent to people
who have just arrived in a channel in a number of ways. Sending a
message to the entire channel is definitely one method to avoid, as
only the newcomer will find the message relevantnobody else in
the channel will want to keep seeing it again and again. Sending a
private message to the newcomer is a better option, as it
won't annoy other users. However, most clients
display private messages in a different window or tab, so some people
opt to send a notice to the user instead. Many clients display
notices in the active window that are easily distinguished from
normal messages.Sometimes people aren't around to personally deliver
welcome messages in a timely fashion, so a bot comes into play.
10.2.3 The Code
WelcomeBot.java
joins multiple channels and uses a unique welcome message for each
channel:
import org.jibble.pircbot.*;The bot will welcome users to all of the channels into which it has
import java.util.HashMap;
public class WelcomeBot extends PircBot {
// This maps channel names to welcome messages.
private HashMap welcomes = new HashMap( );
private static final String defaultWelcome = "Welcome to this channel.";
public WelcomeBot(String name) {
setName(name);
}
// Adds a new welcome message to the bot.
public void addWelcome(String channel, String welcome) {
welcomes.put(channel.toLowerCase( ), welcome);
}
// Returns a welcome message for the specified channel.
private String getWelcome(String channel, String nickname) {
String welcome = (String) welcomes.get(channel.toLowerCase( ));
// If there is no welcome message for this channel, use the default.
if (welcome == null) {
welcome = defaultWelcome;
}
// Replace $channel and $nickname if they are found.
welcome = welcome.replaceAll("(?i:\\$channel)", channel);
welcome = welcome.replaceAll("(?i:\\$nickname)", nickname);
return welcome;
}
// This method gets called whenever someone joins a channel.
public void onJoin(String channel, String sender,
String login, String hostname) {
if (!sender.equalsIgnoreCase(getNick( ))) {
// Send a welcome notice to the visitor.
sendNotice(sender, getWelcome(channel, sender));
}
}
}
been sent. In your main method, you can add specific welcome messages
for each channel. If you do not specify these, the default welcome
message will be used.Set a custom welcome message for a channel by
calling the addWelcome method:
bot.addWelcome("#channel", "Welcome to our channel... behave nicely!");Your welcome message can also make use of the
$channel and $nickname
variables, which will be automatically replaced by the bot, for
example:
bot.addWelcome("#channel", "Welcome to $channel, $nickname.");Create a file called
WelcomeBotMain.java
. You will use this to specify your
welcome messages and tell the bot which channels to join:
public class WelcomeBotMain {
public static void main(String[] args) throws Exception {
WelcomeBot bot = new WelcomeBot("WelcomeBot");
bot.setVerbose(true);
bot.addWelcome("#irchacks", "Welcome to $channel, $nickname");
bot.connect("irc.freenode.net");
bot.joinChannel("#irchacks");
bot.joinChannel("#test");
}
}
10.2.4 Running the Hack
Compile the bot like so:
C:\java\NntpBot> javac -classpath .;pircbot.jar *.javaYou can then run the bot by entering:
C:\java\NntpBot> java -classpath .;pircbot.jar WelcomeBotMain
10.2.5 The Results
Figure 10-1 shows the WelcomeBot in action,
delivering welcome notices to two channels. In the top half, you can
see the default welcome notice being sent as I join the channel
#test. In the bottom half, you can see the custom welcome notice that
was specified in WelcomeBotMain.java.
Figure 10-1. WelcomeBot delivering welcome notices to two channels

is to use ChanServ [Hack #9] to
send welcome notices to users.