123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406 |
- /*
- * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- package com.dmdirc.addons.relaybot;
-
- import com.dmdirc.Channel;
- import com.dmdirc.ChannelEventHandler;
- import com.dmdirc.config.IdentityManager;
- import com.dmdirc.parser.interfaces.ChannelClientInfo;
- import com.dmdirc.parser.interfaces.ChannelInfo;
- import com.dmdirc.parser.interfaces.Parser;
- import com.dmdirc.parser.interfaces.callbacks.ChannelMessageListener;
- import com.dmdirc.parser.irc.IRCCallbackManager;
- import com.dmdirc.parser.irc.IRCChannelClientInfo;
- import com.dmdirc.parser.irc.IRCParser;
- import com.dmdirc.plugins.PluginInfo;
- import com.dmdirc.plugins.PluginManager;
- import com.dmdirc.ui.messages.Styliser;
-
- import java.lang.reflect.Field;
- import java.lang.reflect.Method;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.Map;
-
- /**
- * This class replaces the ChannelHandler in the core to allow intercepting
- * callbacks from the parser.
- *
- * This allows us to hide them from the core and send faked onullnes instead.
- *
- * @author shane
- */
- public class RelayChannelHandler implements ChannelMessageListener {
- /** My Channel. */
- private final Channel myChannel;
-
- /** Core channel handler. */
- private final ChannelEventHandler coreChannelHandler;
-
- /** Plugin that owns this RelayChannelHandler. */
- private final RelayBotPlugin myPlugin;
-
- /** Known ChannelClients. */
- private final Map<String, IRCChannelClientInfo> channelClients
- = new HashMap<String, IRCChannelClientInfo>();
-
- /**
- * Create a new RelayChannelHandler.
- *
- * @param myPlugin Parent plugin
- * @param myChannel channel to hax!
- */
- public RelayChannelHandler(final RelayBotPlugin myPlugin,
- final Channel myChannel) {
- this.myChannel = myChannel;
- this.myPlugin = myPlugin;
- ChannelEventHandler ceh;
- try {
- // Get the core Channel handler.
- final Field field = myChannel.getClass().getDeclaredField(
- "eventHandler");
- field.setAccessible(true);
- ceh = (ChannelEventHandler) field.get(myChannel);
- } catch (IllegalArgumentException ex) {
- ceh = null;
- } catch (IllegalAccessException ex) {
- ceh = null;
- } catch (NoSuchFieldException ex) {
- ceh = null;
- } catch (SecurityException ex) {
- ceh = null;
- }
-
- coreChannelHandler = ceh;
-
- if (coreChannelHandler != null) {
- final IRCCallbackManager cbm = (IRCCallbackManager) myChannel
- .getServer().getParser().getCallbackManager();
- cbm.delCallback(ChannelMessageListener.class, coreChannelHandler);
- cbm.addCallback(ChannelMessageListener.class, this,
- myChannel.getName());
- }
- }
-
-
- /**
- * Get a ChannelClient object for the given nick@server.
- *
- * @param channel Channel
- * @param date Date for event
- * @param nick Nickname to get channel client for.
- * @param sendJoinIfNew Send join if new client seen
- * @return Requested ChannelClient Info.
- */
- private IRCChannelClientInfo getChannelClient(final ChannelInfo channel,
- final Date date, final String nick, final boolean sendJoinIfNew) {
- final Parser parser = channel.getParser();
- final String storeName = parser.getStringConverter().toLowerCase(nick);
- synchronized (channelClients) {
- if (!channelClients.containsKey(storeName)) {
- final RelayClientInfo client = new RelayClientInfo(channel
- .getParser(), nick);
- final IRCChannelClientInfo newChannelClient
- = new IRCChannelClientInfo((IRCParser) channel
- .getParser(), client, channel);
- colourClient(newChannelClient);
-
- channelClients.put(storeName, newChannelClient);
- if (sendJoinIfNew && !client.isServer()) {
- coreChannelHandler.onChannelJoin(parser, date, channel,
- newChannelClient);
- // The nickcolour plugin colours the nicknames on join
- // and uses nickname@server when colouring rather than
- // the setting the user wanted, we can recolour here to
- // fix that.
- colourClient(newChannelClient);
- }
- }
-
- return channelClients.get(storeName);
- }
- }
-
- /**
- * Remove a stored ChannelClient.
- *
- * @param channel Channel
- * @param nick Nickname to get channel client for.
- */
- private void removeChannelClient(final ChannelInfo channel,
- final String nick) {
- final Parser parser = channel.getParser();
- final String storeName = parser.getStringConverter().toLowerCase(nick);
- synchronized (channelClients) {
- channelClients.remove(storeName);
- }
- }
-
- /**
- * Rename a stored ChannelClient.
- *
- * @param channelClient ChannelClient
- * @param newNick new Nickname
- */
- private void renameChannelClient(final IRCChannelClientInfo channelClient,
- final String newNick) {
- final Parser parser = channelClient.getChannel().getParser();
- final String storeName = parser.getStringConverter().toLowerCase(
- newNick);
-
- synchronized (channelClients) {
- channelClients.remove(channelClient.getClient().toString());
- ((RelayClientInfo) channelClient.getClient()).changeNickname(
- newNick);
- channelClients.put(storeName, channelClient);
- }
- }
-
- /**
- * Restore the callback handling to the coreChannelHandler.
- */
- public void restoreCoreChannelHandler() {
- if (coreChannelHandler != null) {
- final IRCCallbackManager cbm = (IRCCallbackManager) myChannel
- .getServer().getParser().getCallbackManager();
-
- // Force adding this callback to the CBM.
- if (cbm instanceof RelayCallbackManager) {
- ((RelayCallbackManager) cbm).forceAddCallback(
- ChannelMessageListener.class, coreChannelHandler,
- myChannel.getName());
- } else {
- cbm.addCallback(ChannelMessageListener.class,
- coreChannelHandler, myChannel.getName());
- }
- unset();
- updateNames();
- }
- }
-
- /**
- * Remove channel message handling from this ChannelHandler.
- */
- public void unset() {
- myChannel.getServer().getParser().getCallbackManager().delCallback(
- ChannelMessageListener.class, this);
- }
-
- /**
- * Colour a client as needed.
- *
- * @param channelClient Client to colour
- */
- private void colourClient(final ChannelClientInfo channelClient) {
- // Use nick colour plugin to colour the client if available.
- final PluginInfo nickColour = PluginManager.getPluginManager()
- .getPluginInfoByName("nickcolour");
-
- final boolean fullColour = IdentityManager.getGlobalConfig()
- .getOptionBool(myPlugin.getDomain(), "colourFullName");
- final RelayClientInfo client = (RelayClientInfo) channelClient
- .getClient();
- final boolean oldValue = client.getShowFullNickname();
- client.setShowFullNickname(fullColour);
-
- if (nickColour != null && nickColour.isLoaded()) {
- try {
- final Method gpcl = PluginInfo.class.getDeclaredMethod(
- "getPluginClassLoader");
- gpcl.setAccessible(true);
-
- final Class<?> nc = nickColour.getPlugin().getClass();
- final Method colourClient = nc.getDeclaredMethod("colourClient",
- new Class<?>[]{String.class, ChannelClientInfo.class});
- colourClient.setAccessible(true);
- colourClient.invoke(nickColour.getPlugin(), myChannel
- .getServer().getNetwork(), channelClient);
- } catch (LinkageError e) {
- // If it can't colour then oh well.
- } catch (Exception t) {
- // If it can't colour then oh well.
- }
- }
-
- client.setShowFullNickname(oldValue);
- }
-
-
- /**
- * Handle the IRCParsers incoming message.
- *
- * @param parser Parser that sent the message
- * @param date Date for event
- * @param channel Channel the message went to
- * @param channelClient Client who send the message
- * @param message Message content
- * @param host Host of client
- */
- @Override
- public void onChannelMessage(final Parser parser, final Date date,
- final ChannelInfo channel, final ChannelClientInfo channelClient,
- final String message, final String host) {
- final String channelName = parser.getStringConverter().toLowerCase(
- channel.getName());
- String botName;
- try {
- botName = IdentityManager.getGlobalConfig().getOption(
- myPlugin.getDomain(), channelName);
- } catch (IllegalArgumentException iae) {
- botName = "";
- }
- final boolean isBot = parser.getStringConverter().equalsIgnoreCase(
- botName, channelClient.getClient().getNickname());
-
- final boolean joinNew = IdentityManager.getGlobalConfig().getOptionBool(
- myPlugin.getDomain(), "joinOnDiscover");
-
- // See if we need to modify this message
- if (channelClient instanceof IRCChannelClientInfo
- && !botName.isEmpty() && isBot) {
- final String[] bits = message.split(" ", 2);
- final String initial = Styliser.stipControlCodes(bits[0]);
-
- if (initial.charAt(0) == '+') {
- // Channel Message
- final IRCChannelClientInfo newChannelClient = getChannelClient(
- channel, date, bits[0].substring(2, bits[0].length() - 1), joinNew);
-
- coreChannelHandler.onChannelMessage(parser, date, channel,
- newChannelClient, bits[1], host);
- return;
- } else if (initial.equalsIgnoreCase("***")) {
- // Some kind of state-changing action
- final String[] newBits = bits[1].split(" ");
-
- if (newBits.length > 2) {
- final IRCChannelClientInfo newChannelClient
- = getChannelClient(channel, date, newBits[0],
- joinNew);
-
- if (newBits[2].equalsIgnoreCase("joined")) {
- // User joined a relayed channel.
- if (!joinNew) {
- // If auto join on discover isn't enabled, we will
- // need to send this join, else it will already
- // have been sent.
- coreChannelHandler.onChannelJoin(parser, date,
- channel, newChannelClient);
- // And recolour to combat the nickcolour plugin
- // changing the colour on join.
- colourClient(newChannelClient);
- }
- return;
- } else if (newBits[2].equalsIgnoreCase("left")) {
- // User left a relayed channel.
- String reason = (newBits.length > 4) ? mergeBits(
- newBits, 4, newBits.length - 1, " ") : "()";
- reason = reason.substring(1, reason.length() - 1);
-
- coreChannelHandler.onChannelPart(parser, date, channel,
- newChannelClient, reason);
- removeChannelClient(channel, newBits[0]);
- return;
- } else if (newBits[2].equalsIgnoreCase("quit")) {
- // User quit a relayed channel.
- String reason = (newBits.length > 4) ? mergeBits(
- newBits, 4, newBits.length - 1, " ") : "()";
- reason = reason.substring(1, reason.length() - 1);
-
- coreChannelHandler.onChannelQuit(parser, date, channel,
- newChannelClient, reason);
- removeChannelClient(channel, newBits[0]);
- return;
- } else if (newBits[2].equalsIgnoreCase("kicked")) {
- // User was kicked from a relayed channel.
- String reason = (newBits.length > 7) ? mergeBits(
- newBits, 7, newBits.length - 1, " ") : "()";
- reason = reason.substring(1, reason.length() - 1);
-
- final IRCChannelClientInfo kickingChannelClient
- = (newBits.length > 6) ? getChannelClient(
- channel, date, newBits[6], joinNew) : null;
-
- coreChannelHandler.onChannelKick(parser, date, channel,
- newChannelClient, kickingChannelClient, reason,
- "");
- removeChannelClient(channel, newBits[0]);
- return;
- } else if (newBits[2].equalsIgnoreCase("now")
- && newBits.length > 3) {
- // User changed their nickname in a relayed channel.
- renameChannelClient(newChannelClient, newBits[3]);
-
- coreChannelHandler.onChannelNickChanged(parser, date,
- channel, newChannelClient, newBits[0]);
- return;
- }
- }
- } else if (initial.charAt(0) == '*') {
- // Channel Action
- final String[] newBits = bits[1].split(" ", 2);
-
- final IRCChannelClientInfo newChannelClient = getChannelClient(
- channel, date, newBits[0], joinNew);
-
- coreChannelHandler.onChannelAction(parser, date, channel,
- newChannelClient, newBits[1], "");
- return;
- }
- }
-
- // Pass it on unchanged.
- coreChannelHandler.onChannelMessage(parser, date, channel,
- channelClient, message, host);
- }
-
- /**
- * Merge the given bits.
- *
- * @param bits Bits to merge
- * @param start Start
- * @param end end
- * @param joiner What to use to join them
- * @return Joined bits.
- */
- private String mergeBits(final String[] bits, final int start,
- final int end, final String joiner) {
- final StringBuilder builder = new StringBuilder();
- for (int i = start; i <= end; i++) {
- if (bits.length < i) { break; }
- if (i != start) { builder.append(joiner); }
- builder.append(bits[i]);
- }
-
- return builder.toString();
- }
-
- /**
- * Send an onChannelNames() event for this channel.
- * This will cause all remote clients to vanish from the nicklist.
- */
- public void updateNames() {
- coreChannelHandler.onChannelGotNames(myChannel.getServer().getParser(),
- new Date(), myChannel.getChannelInfo());
- }
- }
|