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.

RelayCallbackManager.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright (c) 2006-2013 DMDirc Developers
  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.parser.common.CallbackManager;
  26. import com.dmdirc.parser.common.CallbackNotFoundException;
  27. import com.dmdirc.parser.common.CallbackObject;
  28. import com.dmdirc.parser.common.ChildImplementations;
  29. import com.dmdirc.parser.interfaces.Parser;
  30. import com.dmdirc.parser.interfaces.callbacks.CallbackInterface;
  31. import com.dmdirc.parser.interfaces.callbacks.ChannelMessageListener;
  32. import com.dmdirc.parser.interfaces.callbacks.SocketCloseListener;
  33. import com.dmdirc.parser.irc.IRCParser;
  34. import java.lang.reflect.Field;
  35. import java.util.Date;
  36. import java.util.HashMap;
  37. import java.util.Map;
  38. /**
  39. * This is a callback manager that when created will replace the callback
  40. * manager of the given parser with itself.
  41. *
  42. * When a new callback is added it will be allowed unless the RelayBotPlugin
  43. * says otherwise.
  44. *
  45. * When the parser disconnects, the original CBM is restored.
  46. *
  47. * @author shane
  48. */
  49. public class RelayCallbackManager extends CallbackManager implements SocketCloseListener {
  50. /** Pluign that created this callback manager. */
  51. private final RelayBotPlugin myPlugin;
  52. /** Original CallbackManager */
  53. private final CallbackManager originalCBM;
  54. /**
  55. * Create a new RelayCallbackManager and replace
  56. *
  57. * @param myPlugin
  58. * @param parser
  59. */
  60. public RelayCallbackManager(final RelayBotPlugin myPlugin, final IRCParser parser) {
  61. super(parser, getImplementations());
  62. this.myPlugin = myPlugin;
  63. this.originalCBM = parser.getCallbackManager();
  64. setCallbackManager(parser, this);
  65. addCallback(SocketCloseListener.class, this);
  66. }
  67. private static Map<Class<?>, Class<?>> getImplementations() {
  68. final Map<Class<?>, Class<?>> implementations
  69. = new HashMap<Class<?>, Class<?>>();
  70. for (Class<?> child : IRCParser.class.getAnnotation(ChildImplementations.class).value()) {
  71. for (Class<?> iface : child.getInterfaces()) {
  72. implementations.put(iface, child);
  73. }
  74. }
  75. return implementations;
  76. }
  77. /** {@inheritDoc} */
  78. @Override
  79. public <S extends CallbackInterface> void addCallback(final Class<S> callback, final S o, final String target) throws CallbackNotFoundException {
  80. // Don't allow the core to give itself a ChannelMessageListener if we
  81. // already have a listener for this channel.
  82. if (o instanceof ChannelEventHandler && callback == ChannelMessageListener.class) {
  83. try {
  84. // Get the old callback manager
  85. final Field field = o.getClass().getDeclaredField("owner");
  86. field.setAccessible(true);
  87. final Channel channel = (Channel) field.get(o);
  88. if (myPlugin.isListening(channel)) {
  89. return;
  90. }
  91. } catch (NoSuchFieldException ex) {
  92. ex.printStackTrace();
  93. } catch (SecurityException ex) {
  94. ex.printStackTrace();
  95. } catch (IllegalArgumentException ex) {
  96. ex.printStackTrace();
  97. } catch (IllegalAccessException ex) {
  98. ex.printStackTrace();
  99. }
  100. }
  101. // Unless the plugin says we are listening instead of this channel, then
  102. // we can add the callback.
  103. forceAddCallback(callback, o, target);
  104. }
  105. /**
  106. * Add a callback with a specific target.
  107. * This method will throw a CallbackNotFoundException if the callback does not exist.
  108. *
  109. * @param <T> The type of callback
  110. * @param callback Type of callback object.
  111. * @param o instance of ICallbackInterface to add.
  112. * @param target Parameter to specify that a callback should only fire for specific things
  113. * @throws CallbackNotFoundException If callback is not found.
  114. * @throws NullPointerException If 'o' is null
  115. */
  116. public <T extends CallbackInterface> void forceAddCallback(final Class<T> callback, final T o, final String target) throws CallbackNotFoundException {
  117. super.addCallback(callback, o, target);
  118. }
  119. /**
  120. * Set the Callback Manager of a given parser.
  121. *
  122. * @param parser
  123. * @param cbm
  124. */
  125. private void setCallbackManager(final IRCParser parser, final CallbackManager cbm) {
  126. try {
  127. // Get the old callback manager
  128. final Field field = parser.getClass().getSuperclass().getDeclaredField("callbackManager");
  129. field.setAccessible(true);
  130. @SuppressWarnings("unchecked")
  131. final CallbackManager oldCBM = (CallbackManager) field.get(parser);
  132. // Clone the known CallbackObjects list (horrible code ahoy!)
  133. // First get the old map of callbacks
  134. final Field cbField = CallbackManager.class.getDeclaredField("callbackHash");
  135. cbField.setAccessible(true);
  136. @SuppressWarnings("unchecked")
  137. final Map<Class<? extends CallbackInterface>, CallbackObject> oldCallbackHash = (Map<Class<? extends CallbackInterface>, CallbackObject>) cbField.get(oldCBM);
  138. // Clear my map of callbacks
  139. @SuppressWarnings("unchecked")
  140. final Map<Class<? extends CallbackInterface>, CallbackObject> myCallbackHash = (Map<Class<? extends CallbackInterface>, CallbackObject>) cbField.get(cbm);
  141. myCallbackHash.clear();
  142. // Now add them all to the new cbm.
  143. for (CallbackObject callback : oldCallbackHash.values()) {
  144. // Change their manager to the new one.
  145. final Field ownerField = CallbackObject.class.getDeclaredField("myManager");
  146. ownerField.setAccessible(true);
  147. ownerField.set(callback, cbm);
  148. // And add them to the CBM
  149. cbm.addCallbackType(callback);
  150. }
  151. // Replace the old one with the new one.
  152. field.set(parser, cbm);
  153. } catch (IllegalArgumentException ex) {
  154. ex.printStackTrace();
  155. } catch (IllegalAccessException ex) {
  156. ex.printStackTrace();
  157. } catch (NoSuchFieldException ex) {
  158. ex.printStackTrace();
  159. } catch (SecurityException ex) {
  160. ex.printStackTrace();
  161. }
  162. }
  163. /**
  164. * When the socket closes, reset the callback manager.
  165. *
  166. * @param parser
  167. */
  168. @Override
  169. public void onSocketClosed(final Parser parser, final Date date) {
  170. if (parser.getCallbackManager() instanceof RelayCallbackManager) {
  171. setCallbackManager((IRCParser)parser, originalCBM);
  172. }
  173. }
  174. }