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.2KB

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