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.

CallbackObjectSpecific.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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.parser.common;
  23. import com.dmdirc.parser.interfaces.SpecificCallback;
  24. import com.dmdirc.parser.interfaces.ChannelInfo;
  25. import com.dmdirc.parser.interfaces.ClientInfo;
  26. import com.dmdirc.parser.interfaces.Parser;
  27. import com.dmdirc.parser.interfaces.callbacks.CallbackInterface;
  28. import java.util.ArrayList;
  29. import java.util.Hashtable;
  30. /**
  31. * CallbackObjectSpecific.
  32. * Superclass for all callback types that have a "specific" target.
  33. *
  34. * @author Shane Mc Cormack
  35. */
  36. public abstract class CallbackObjectSpecific extends CallbackObject {
  37. /** Hashtable for storing specific information for callback. */
  38. protected volatile Hashtable<CallbackInterface, String> specificData
  39. = new Hashtable<CallbackInterface, String>();
  40. /**
  41. * Create a new instance of the Callback Object.
  42. *
  43. * @param parser Parser That owns this callback
  44. * @param manager CallbackManager that is in charge of this callback
  45. * @param type The type of callback to use
  46. * @since 0.6.3m1
  47. */
  48. public CallbackObjectSpecific(final Parser parser, final CallbackManager<?> manager,
  49. final Class<? extends CallbackInterface> type) {
  50. super(parser, manager, type);
  51. }
  52. /**
  53. * Used to check if a channel matches the specificData.
  54. *
  55. * @param eMethod Object that is being called back to
  56. * @param cChannel ChannelInfo object for the channel to test
  57. * @return true if channel given matches the specifics for the method given
  58. */
  59. protected boolean isValidChan(final CallbackInterface eMethod, final ChannelInfo cChannel) {
  60. if (specificData.containsKey(eMethod)) {
  61. if (!myParser.getStringConverter().equalsIgnoreCase(cChannel.getName(),
  62. specificData.get(eMethod))) {
  63. return false;
  64. }
  65. }
  66. return true;
  67. }
  68. /**
  69. * Used to check if a hostname matches the specificData.
  70. *
  71. * @param eMethod Object that is being called back to
  72. * @param sHost Hostname of user that sent the query
  73. * @return true if host given matches the specifics for the method given
  74. */
  75. protected boolean isValidUser(final CallbackInterface eMethod, final String sHost) {
  76. final String nickname = translateHostname(sHost);
  77. if (specificData.containsKey(eMethod)) {
  78. if (!myParser.getStringConverter().equalsIgnoreCase(nickname,
  79. specificData.get(eMethod))) {
  80. return false;
  81. }
  82. }
  83. return true;
  84. }
  85. /**
  86. * Translates the specified hostname into the form which is used to
  87. * register specific callbacks (e.g. nickname).
  88. *
  89. * @param hostname The hostname to be parsed
  90. * @return The translated hostname
  91. */
  92. protected abstract String translateHostname(final String hostname);
  93. // We override the default add method to make sure that any add with no
  94. // specifics will have the specific data removed.
  95. /**
  96. * Add a new callback.
  97. *
  98. * @param eMethod Object to callback to.
  99. */
  100. @Override
  101. public void add(final CallbackInterface eMethod) {
  102. addCallback(eMethod);
  103. if (specificData.containsKey(eMethod)) {
  104. specificData.remove(eMethod);
  105. }
  106. }
  107. /**
  108. * Add a new callback with a specific target.
  109. *
  110. * @param eMethod Object to callback to.
  111. * @param specificTarget Target that must match for callback to be called.
  112. */
  113. public void add(final CallbackInterface eMethod, final String specificTarget) {
  114. add(eMethod);
  115. if (!specificTarget.isEmpty()) {
  116. specificData.put(eMethod, specificTarget);
  117. }
  118. }
  119. /**
  120. * Remove a callback.
  121. *
  122. * @param eMethod Object to remove callback to.
  123. */
  124. @Override
  125. public void del(final CallbackInterface eMethod) {
  126. delCallback(eMethod);
  127. if (specificData.containsKey(eMethod)) {
  128. specificData.remove(eMethod);
  129. }
  130. }
  131. /**
  132. * Actually calls this callback. The specified arguments must match those
  133. * specified in the callback's interface, or an error will be raised.
  134. *
  135. * @param args The arguments to pass to the callback implementation
  136. * @return True if a method was called, false otherwise
  137. */
  138. @Override
  139. public boolean call(final Object... args) {
  140. boolean bResult = false;
  141. final Object[] newArgs = new Object[args.length + 1];
  142. System.arraycopy(args, 0, newArgs, 1, args.length);
  143. newArgs[0] = myParser;
  144. createFakeArgs(newArgs);
  145. for (CallbackInterface iface : new ArrayList<CallbackInterface>(callbackInfo)) {
  146. if (type.isAnnotationPresent(SpecificCallback.class) &&
  147. ((args[0] instanceof ClientInfo
  148. && !isValidUser(iface, ((ClientInfo) args[0]).getHostname()))
  149. || (args[0] instanceof ChannelInfo
  150. && !isValidChan(iface, (ChannelInfo) args[0]))
  151. || (!(args[0] instanceof ClientInfo
  152. || args[0] instanceof ChannelInfo) &&
  153. args[args.length - 1] instanceof String
  154. && !isValidUser(iface, (String) args[args.length - 1])))) {
  155. continue;
  156. }
  157. try {
  158. type.getMethods()[0].invoke(iface, newArgs);
  159. } catch (Exception e) {
  160. final ParserError ei = new ParserError(ParserError.ERROR_ERROR,
  161. "Exception in callback (" + e.getMessage() + ")",
  162. myParser.getLastLine());
  163. ei.setException(e);
  164. callErrorInfo(ei);
  165. }
  166. bResult = true;
  167. }
  168. return bResult;
  169. }
  170. }