Java IRC bot
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

CallbackObjectSpecific.java 6.0KB

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