Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

CallbackObjectSpecific.java 6.5KB

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