IRC Hacks [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

IRC Hacks [Electronic resources] - نسخه متنی

Paul Mutton

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید







Hack 50 Pass Notes to Other Users

Not everybody can afford to be permanently
connected to IRC, but it is still often a preferred medium for
communication. Pass messages on to other users when they next join
the channel with this simple IRC bot.

For many people, IRC is more than just a
place to chat. Social groups form quite easily in channels; after
all, you all share the same interest, right? IRC therefore becomes
the natural place to talk to this social group. You may not even know
the email addresses of the people you talk to, simply because you
don't need to. IRC lets you chat in real time, which
is often more convenient than waiting for a response to an email. IRC
also lets you transfer files directly from one client to another.

Such dependence on IRC sometimes makes people forget that there are
alternative communication mediums. Indeed, some people even become
frustrated when they have to resort to alternatives! If the person
you want to talk to has left the server for a while (perhaps has gone
to bed or to the shops) and you don't know his email
address, you could use an IRC bot to pass on your message when he
comes back.

A bot that passes on messages doesn't have to be too
complicated. All it has to do is allow users to give it new messages,
store them, and then pass them on when appropriate:

<Jibbler> tell DeadEd to look at http://www.jibble.org/comicbot/
<TellBot> Okay, Jibbler

The next time DeadEd joins the channel, the bot should pass the
message to DeadEd:

* DeadEd has joined #irchacks
<TellBot> DeadEd, Jibbler asked me to tell you to look at
http://www.jibble.org/comicbot/


7.8.1 The Code


We can use a HashMap to store a list of messages to pass
on, allowing more than one message to be sent to any one user. The
HashMap will be indexed by nickname, and each entry will point to an
ArrayList that contains messages for that
user.

Create a file called TellBot.java:

import org.jibble.pircbot.*;
import java.util.*;
public class TellBot extends PircBot {
// A map of String (nickname) to ArrayList (message Strings).
private HashMap messages = new HashMap( );
public TellBot(String name) {
setName(name);
}
public void onMessage(String channel, String sender, String login,
String hostname, String message) {
String[] tokens = message.split("\\s+");
// Check for the "tell" command.
if (tokens.length > 2 && tokens[0].equalsIgnoreCase("tell")) {
String nick = tokens[1];
message = message.substring(message.indexOf(nick) + nick.length( ) + 1);
// Convert the nickname to lowercase for use in the HashMap.
String key = nick.toLowerCase( );
ArrayList list = (ArrayList) messages.get(key);
if (list == null) {
// Create a new ArrayList if the HashMap entry is empty.
list = new ArrayList( );
messages.put(key, list);
}
// Add the message to the list for the target nickname.
list.add(sender + " asked me to tell you " + message);
sendMessage(channel, "Okay, " + sender);
}
}
public void onJoin(String channel, String sender,
String login, String hostname) {
// Convert the nickname to lowercase to get the HashMap key.
String key = sender.toLowerCase( );
ArrayList list = (ArrayList) messages.get(key);
if (list != null) {
// Send all messages to the user.
for (int i = 0; i < list.size( ); i++) {
String message = (String) list.get(i);
sendMessage(channel, sender + ", " + message);
}
// Now erase all messages for this user.
messages.put(key, null);
}
}
}

Notice that the HashMap keys must be converted to lowercase. This
effectively makes the nicknames case insensitive, so a message that
is left for "Paul" can also be
received by "paul."

The onMessage method is invoked whenever someone
sends a message to the channel. This method checks to see if a user
has entered the "tell"
commandif so, it adds the message to the HashMap.

When a user joins the channel, the onJoin method
is invoked. If there are any messages for this user, they are sent to
the channel and then removed from the HashMap.

To instantiate the bot, you will need a main method. Create this in
TellBotMain.java
:

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

You can also tell the bot to join more than one channelsimply
modify the joinChannel method call so it contains
a comma-separated list, for example:

        bot.joinChannel("#irchacks,#jibble,#pircbot");

Messages will be accepted from all channels and delivered to the
first one the recipient joins.


7.8.2 Running the Hack


Compile the bot like so:

javac -classpath .;pircbot.jar *.java

Run the bot with:

java -classpath .;pircbot.jar TellBotMain


7.8.3 The Results


Figure 7-7 shows the bot in action. As soon as
DeadEd joins the channel, TellBot passes my message on to him.


Figure 7-7. TellBot passing on a message from Jibbler to DeadEd

This type of bot is very popular among communities.
You'll find yourself sending fewer emails once you
start using this.


/ 175