You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RelayChannelHandler.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.relaybot;
  23. import com.dmdirc.Channel;
  24. import com.dmdirc.ChannelEventHandler;
  25. import com.dmdirc.config.IdentityManager;
  26. import com.dmdirc.parser.interfaces.ChannelClientInfo;
  27. import com.dmdirc.parser.interfaces.ChannelInfo;
  28. import com.dmdirc.parser.interfaces.Parser;
  29. import com.dmdirc.parser.interfaces.callbacks.ChannelMessageListener;
  30. import com.dmdirc.parser.irc.IRCCallbackManager;
  31. import com.dmdirc.parser.irc.IRCChannelClientInfo;
  32. import com.dmdirc.parser.irc.IRCParser;
  33. import com.dmdirc.plugins.PluginInfo;
  34. import com.dmdirc.plugins.PluginManager;
  35. import com.dmdirc.ui.messages.Styliser;
  36. import java.lang.reflect.Field;
  37. import java.lang.reflect.Method;
  38. import java.util.Date;
  39. import java.util.HashMap;
  40. import java.util.Map;
  41. /**
  42. * This class replaces the ChannelHandler in the core to allow intercepting
  43. * callbacks from the parser.
  44. *
  45. * This allows us to hide them from the core and send faked onullnes instead.
  46. *
  47. * @author shane
  48. */
  49. public class RelayChannelHandler implements ChannelMessageListener {
  50. /** My Channel. */
  51. private final Channel myChannel;
  52. /** Core channel handler. */
  53. private final ChannelEventHandler coreChannelHandler;
  54. /** Plugin that owns this RelayChannelHandler. */
  55. private final RelayBotPlugin myPlugin;
  56. /** Known ChannelClients. */
  57. private final Map<String, IRCChannelClientInfo> channelClients
  58. = new HashMap<String, IRCChannelClientInfo>();
  59. /**
  60. * Create a new RelayChannelHandler.
  61. *
  62. * @param myPlugin Parent plugin
  63. * @param myChannel channel to hax!
  64. */
  65. public RelayChannelHandler(final RelayBotPlugin myPlugin,
  66. final Channel myChannel) {
  67. this.myChannel = myChannel;
  68. this.myPlugin = myPlugin;
  69. ChannelEventHandler ceh;
  70. try {
  71. // Get the core Channel handler.
  72. final Field field = myChannel.getClass().getDeclaredField(
  73. "eventHandler");
  74. field.setAccessible(true);
  75. ceh = (ChannelEventHandler) field.get(myChannel);
  76. } catch (IllegalArgumentException ex) {
  77. ceh = null;
  78. } catch (IllegalAccessException ex) {
  79. ceh = null;
  80. } catch (NoSuchFieldException ex) {
  81. ceh = null;
  82. } catch (SecurityException ex) {
  83. ceh = null;
  84. }
  85. coreChannelHandler = ceh;
  86. if (coreChannelHandler != null) {
  87. final IRCCallbackManager cbm = (IRCCallbackManager) myChannel
  88. .getServer().getParser().getCallbackManager();
  89. cbm.delCallback(ChannelMessageListener.class, coreChannelHandler);
  90. cbm.addCallback(ChannelMessageListener.class, this,
  91. myChannel.getName());
  92. }
  93. }
  94. /**
  95. * Get a ChannelClient object for the given nick@server.
  96. *
  97. * @param channel Channel
  98. * @param date Date for event
  99. * @param nick Nickname to get channel client for.
  100. * @param sendJoinIfNew Send join if new client seen
  101. * @return Requested ChannelClient Info.
  102. */
  103. private IRCChannelClientInfo getChannelClient(final ChannelInfo channel,
  104. final Date date, final String nick, final boolean sendJoinIfNew) {
  105. final Parser parser = channel.getParser();
  106. final String storeName = parser.getStringConverter().toLowerCase(nick);
  107. synchronized (channelClients) {
  108. if (!channelClients.containsKey(storeName)) {
  109. final RelayClientInfo client = new RelayClientInfo(channel
  110. .getParser(), nick);
  111. final IRCChannelClientInfo newChannelClient
  112. = new IRCChannelClientInfo((IRCParser) channel
  113. .getParser(), client, channel);
  114. colourClient(newChannelClient);
  115. channelClients.put(storeName, newChannelClient);
  116. if (sendJoinIfNew && !client.isServer()) {
  117. coreChannelHandler.onChannelJoin(parser, date, channel,
  118. newChannelClient);
  119. // The nickcolour plugin colours the nicknames on join
  120. // and uses nickname@server when colouring rather than
  121. // the setting the user wanted, we can recolour here to
  122. // fix that.
  123. colourClient(newChannelClient);
  124. }
  125. }
  126. return channelClients.get(storeName);
  127. }
  128. }
  129. /**
  130. * Remove a stored ChannelClient.
  131. *
  132. * @param channel Channel
  133. * @param nick Nickname to get channel client for.
  134. */
  135. private void removeChannelClient(final ChannelInfo channel,
  136. final String nick) {
  137. final Parser parser = channel.getParser();
  138. final String storeName = parser.getStringConverter().toLowerCase(nick);
  139. synchronized (channelClients) {
  140. channelClients.remove(storeName);
  141. }
  142. }
  143. /**
  144. * Rename a stored ChannelClient.
  145. *
  146. * @param channelClient ChannelClient
  147. * @param newNick new Nickname
  148. */
  149. private void renameChannelClient(final IRCChannelClientInfo channelClient,
  150. final String newNick) {
  151. final Parser parser = channelClient.getChannel().getParser();
  152. final String storeName = parser.getStringConverter().toLowerCase(
  153. newNick);
  154. synchronized (channelClients) {
  155. channelClients.remove(channelClient.getClient().toString());
  156. ((RelayClientInfo) channelClient.getClient()).changeNickname(
  157. newNick);
  158. channelClients.put(storeName, channelClient);
  159. }
  160. }
  161. /**
  162. * Restore the callback handling to the coreChannelHandler.
  163. */
  164. public void restoreCoreChannelHandler() {
  165. if (coreChannelHandler != null) {
  166. final IRCCallbackManager cbm = (IRCCallbackManager) myChannel
  167. .getServer().getParser().getCallbackManager();
  168. // Force adding this callback to the CBM.
  169. if (cbm instanceof RelayCallbackManager) {
  170. ((RelayCallbackManager) cbm).forceAddCallback(
  171. ChannelMessageListener.class, coreChannelHandler,
  172. myChannel.getName());
  173. } else {
  174. cbm.addCallback(ChannelMessageListener.class,
  175. coreChannelHandler, myChannel.getName());
  176. }
  177. unset();
  178. updateNames();
  179. }
  180. }
  181. /**
  182. * Remove channel message handling from this ChannelHandler.
  183. */
  184. public void unset() {
  185. myChannel.getServer().getParser().getCallbackManager().delCallback(
  186. ChannelMessageListener.class, this);
  187. }
  188. /**
  189. * Colour a client as needed.
  190. *
  191. * @param channelClient Client to colour
  192. */
  193. private void colourClient(final ChannelClientInfo channelClient) {
  194. // Use nick colour plugin to colour the client if available.
  195. final PluginInfo nickColour = PluginManager.getPluginManager()
  196. .getPluginInfoByName("nickcolour");
  197. final boolean fullColour = IdentityManager.getGlobalConfig()
  198. .getOptionBool(myPlugin.getDomain(), "colourFullName");
  199. final RelayClientInfo client = (RelayClientInfo) channelClient
  200. .getClient();
  201. final boolean oldValue = client.getShowFullNickname();
  202. client.setShowFullNickname(fullColour);
  203. if (nickColour != null && nickColour.isLoaded()) {
  204. try {
  205. final Method gpcl = PluginInfo.class.getDeclaredMethod(
  206. "getPluginClassLoader");
  207. gpcl.setAccessible(true);
  208. final Class<?> nc = nickColour.getPlugin().getClass();
  209. final Method colourClient = nc.getDeclaredMethod("colourClient",
  210. new Class<?>[]{String.class, ChannelClientInfo.class});
  211. colourClient.setAccessible(true);
  212. colourClient.invoke(nickColour.getPlugin(), myChannel
  213. .getServer().getNetwork(), channelClient);
  214. } catch (LinkageError e) {
  215. // If it can't colour then oh well.
  216. } catch (Exception t) {
  217. // If it can't colour then oh well.
  218. }
  219. }
  220. client.setShowFullNickname(oldValue);
  221. }
  222. /**
  223. * Handle the IRCParsers incoming message.
  224. *
  225. * @param parser Parser that sent the message
  226. * @param date Date for event
  227. * @param channel Channel the message went to
  228. * @param channelClient Client who send the message
  229. * @param message Message content
  230. * @param host Host of client
  231. */
  232. @Override
  233. public void onChannelMessage(final Parser parser, final Date date,
  234. final ChannelInfo channel, final ChannelClientInfo channelClient,
  235. final String message, final String host) {
  236. final String channelName = parser.getStringConverter().toLowerCase(
  237. channel.getName());
  238. String botName;
  239. try {
  240. botName = IdentityManager.getGlobalConfig().getOption(
  241. myPlugin.getDomain(), channelName);
  242. } catch (IllegalArgumentException iae) {
  243. botName = "";
  244. }
  245. final boolean isBot = parser.getStringConverter().equalsIgnoreCase(
  246. botName, channelClient.getClient().getNickname());
  247. final boolean joinNew = IdentityManager.getGlobalConfig().getOptionBool(
  248. myPlugin.getDomain(), "joinOnDiscover");
  249. // See if we need to modify this message
  250. if (channelClient instanceof IRCChannelClientInfo
  251. && !botName.isEmpty() && isBot) {
  252. final String[] bits = message.split(" ", 2);
  253. final String initial = Styliser.stipControlCodes(bits[0]);
  254. if (initial.charAt(0) == '+') {
  255. // Channel Message
  256. final IRCChannelClientInfo newChannelClient = getChannelClient(
  257. channel, date, bits[0].substring(2, bits[0].length() - 1), joinNew);
  258. coreChannelHandler.onChannelMessage(parser, date, channel,
  259. newChannelClient, bits[1], host);
  260. return;
  261. } else if (initial.equalsIgnoreCase("***")) {
  262. // Some kind of state-changing action
  263. final String[] newBits = bits[1].split(" ");
  264. if (newBits.length > 2) {
  265. final IRCChannelClientInfo newChannelClient
  266. = getChannelClient(channel, date, newBits[0],
  267. joinNew);
  268. if (newBits[2].equalsIgnoreCase("joined")) {
  269. // User joined a relayed channel.
  270. if (!joinNew) {
  271. // If auto join on discover isn't enabled, we will
  272. // need to send this join, else it will already
  273. // have been sent.
  274. coreChannelHandler.onChannelJoin(parser, date,
  275. channel, newChannelClient);
  276. // And recolour to combat the nickcolour plugin
  277. // changing the colour on join.
  278. colourClient(newChannelClient);
  279. }
  280. return;
  281. } else if (newBits[2].equalsIgnoreCase("left")) {
  282. // User left a relayed channel.
  283. String reason = (newBits.length > 4) ? mergeBits(
  284. newBits, 4, newBits.length - 1, " ") : "()";
  285. reason = reason.substring(1, reason.length() - 1);
  286. coreChannelHandler.onChannelPart(parser, date, channel,
  287. newChannelClient, reason);
  288. removeChannelClient(channel, newBits[0]);
  289. return;
  290. } else if (newBits[2].equalsIgnoreCase("quit")) {
  291. // User quit a relayed channel.
  292. String reason = (newBits.length > 4) ? mergeBits(
  293. newBits, 4, newBits.length - 1, " ") : "()";
  294. reason = reason.substring(1, reason.length() - 1);
  295. coreChannelHandler.onChannelQuit(parser, date, channel,
  296. newChannelClient, reason);
  297. removeChannelClient(channel, newBits[0]);
  298. return;
  299. } else if (newBits[2].equalsIgnoreCase("kicked")) {
  300. // User was kicked from a relayed channel.
  301. String reason = (newBits.length > 7) ? mergeBits(
  302. newBits, 7, newBits.length - 1, " ") : "()";
  303. reason = reason.substring(1, reason.length() - 1);
  304. final IRCChannelClientInfo kickingChannelClient
  305. = (newBits.length > 6) ? getChannelClient(
  306. channel, date, newBits[6], joinNew) : null;
  307. coreChannelHandler.onChannelKick(parser, date, channel,
  308. newChannelClient, kickingChannelClient, reason,
  309. "");
  310. removeChannelClient(channel, newBits[0]);
  311. return;
  312. } else if (newBits[2].equalsIgnoreCase("now")
  313. && newBits.length > 3) {
  314. // User changed their nickname in a relayed channel.
  315. renameChannelClient(newChannelClient, newBits[3]);
  316. coreChannelHandler.onChannelNickChanged(parser, date,
  317. channel, newChannelClient, newBits[0]);
  318. return;
  319. }
  320. }
  321. } else if (initial.charAt(0) == '*') {
  322. // Channel Action
  323. final String[] newBits = bits[1].split(" ", 2);
  324. final IRCChannelClientInfo newChannelClient = getChannelClient(
  325. channel, date, newBits[0], joinNew);
  326. coreChannelHandler.onChannelAction(parser, date, channel,
  327. newChannelClient, newBits[1], "");
  328. return;
  329. }
  330. }
  331. // Pass it on unchanged.
  332. coreChannelHandler.onChannelMessage(parser, date, channel,
  333. channelClient, message, host);
  334. }
  335. /**
  336. * Merge the given bits.
  337. *
  338. * @param bits Bits to merge
  339. * @param start Start
  340. * @param end end
  341. * @param joiner What to use to join them
  342. * @return Joined bits.
  343. */
  344. private String mergeBits(final String[] bits, final int start,
  345. final int end, final String joiner) {
  346. final StringBuilder builder = new StringBuilder();
  347. for (int i = start; i <= end; i++) {
  348. if (bits.length < i) { break; }
  349. if (i != start) { builder.append(joiner); }
  350. builder.append(bits[i]);
  351. }
  352. return builder.toString();
  353. }
  354. /**
  355. * Send an onChannelNames() event for this channel.
  356. * This will cause all remote clients to vanish from the nicklist.
  357. */
  358. public void updateNames() {
  359. coreChannelHandler.onChannelGotNames(myChannel.getServer().getParser(),
  360. new Date(), myChannel.getChannelInfo());
  361. }
  362. }