Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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