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

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

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

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

Paul Mutton

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

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

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








Hack 68 Link Channels on Multiple Networks




How do you encourage a community on one network
to talk to those on another network? Create a bot that can relay
messages between the networks.


Most IRC users are happy
staying connected to just a single IRC network. Joining more networks
inevitably means joining more channels, and, before you know it,
you've run out of space on your screen. The problem
here is that you might end up missing out on discussions with people
who have similar interests on the other networks.


Sometimes there are other reasons for communities to be divided
across two (or more) networks. A firewall or other technical
constraint may prevent some users from connecting to one of the
networks. But as long as someone (or something) can connect to all of
them, it will be possible for messages to be relayed to other users.
This can be done either manually, such as, "Hey
Paul, did you hear about Dave's great idea on the
other network?" or automatically with a bot that
relays all messages between the servers.


Bots that link channels on different IRC networks are often called
relay bots, as
they are used to relay messages from one network to all the other
networks they are connected to. Simple relay bots are straightforward
to implementall they have to do is connect to a few networks,
join the same channel on each network, and read all the messages they
see. Each time a message is received, it must be passed on to all
networks except the one it was received from.


This hack will walk you through the process of creating your own
relay bot.



11.2.1 The Code




This [Hack #35] to connect to each IRC
network. Each connection will be dealt with by
its own PircBot subclass, with a single Controller class being used
to pass on messages to the other connections. The Controller is
essentially a collection of separate IRC bots and is used to link
them together.


Each bot just has to listen for messages and pass them on to the
Controller, so there is not too much code to write. Save the
following as ChannelLinkBot.java:


import org.jibble.pircbot.*;
public class ChannelLinkBot extends PircBot {
// This is the Controller that we will pass messages to.
private Controller controller;
public ChannelLinkBot(Controller c, String server) throws Exception {
controller = c;
setName("MyLinkBot");
setVerbose(true);
connect(server);
}
public void onMessage(String channel, String sender,
String login, String hostname, String message) {
// Pass the message on to the Controller.
controller.shareMessage(this, Colors.BOLD + "<" + sender + "> " +
Colors.NORMAL + message);
}
}


As you can see from this code, whenever one of the bots receives a
message, it shares that message with the Controller. Each bot is
passed a reference to the Controller in its constructor, along with
the name of the server to connect to.


All the
Controller needs to do is maintain a
list of all of its bots and allow each of those bots to send messages
to all the other bots. A LinkedList is used to
store the collection of bots.


Save the following as Controller.java:


import java.util.*;
public class Controller {
private LinkedList bots = new LinkedList( );
private String channel;
public Controller(String channel) {
this.channel = channel;
}
// Add a new bot to the list and make it join the channel.
public synchronized void add(ChannelLinkBot bot) {
bot.joinChannel(channel);
bots.add(bot);
}
// Share a message with all other networks.
public synchronized void shareMessage(ChannelLinkBot from, String message) {
Iterator it = bots.iterator( );
while (it.hasNext( )) {
ChannelLinkBot bot = (ChannelLinkBot) it.next( );
if (bot != from) {
bot.sendMessage(channel, message);
}
}
}
}


The add method in the
Controller is used to add a new bot to the
LinkedList. Each time a new bot is added, it is
told to join the same channel. Note that this method is synchronized,
so it is safe to add new bots while the program is running.


The shareMessage method can be called by each of
the bots and the Controller then iterates through
the list of bots and tells each bot to send that message to its
network. The bot that called this method must not be told to send the
message; otherwise, it will simply end up repeating everything that
is said on its network.


The main method of this program must create a
Controller object and add a bot for each network
it will connect to. You can supply these arguments through the
command line.


Save the following as ChannelLinkBotMain.java:


public class ChannelLinkBotMain {
public static void main(String[] args) {
String channel = args[0];
Controller controller = new Controller(channel);
for (int i = 1; i < args.length; i++) {
try {
ChannelLinkBot bot = new ChannelLinkBot(controller, args[i]);
controller.add(bot);
}
catch (Exception e) {
System.out.println("Could not add a bot: " + e);
}
}
}
}



11.2.2 Running the Hack




Compile the bot like so:


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


When you run the bot, you must specify the channel name as the first
command-line argument. All subsequent arguments will be treated as
server names, and a bot will be created to connect to each of them.


To make your bot connect to irc.freenode.net and an IRC server
running on your own machine, you can run it like this:


C:\java\ChannelLinkBot> java -classpath .;pircbot.jar ChannelLinkBotMain 
#irchacks irc.freenode.net localhost


As soon as the bot has connected to each server, it will join the
channel #irchacks. When it has joined at least two servers, it will
be ready to begin relaying messages.



11.2.3 The Results




When the bot is running, it will relay messages from each network to
all other networks. Each message is prefixed with the nickname of the
sender and is made bold to make it stand out more than the name of
the bot. This gives the impression of everybody being in the same
channel on the same IRC network, as shown in Figure 11-1.




Figure 11-1. Connecting a channel on two different networks


This kind of bot is not really suitable for running in very busy
channels, as the volume of traffic could cause the server to
disconnect it for flooding the server as it relays messages. If
channels are large enough for this to be a problem, then there is
probably not much to be gained by linking them anyway.



/ 175