選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

CallbackObjectSpecific.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (c) 2006-2008 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.callbacks.interfaces.ICallbackInterface;
  28. /**
  29. * CallbackObjectSpecific.
  30. * Superclass for all callback types that have a "specific" target.
  31. *
  32. * @author Shane Mc Cormack
  33. */
  34. public abstract class CallbackObjectSpecific extends CallbackObject {
  35. /** Hashtable for storing specific information for callback. */
  36. protected volatile Hashtable<ICallbackInterface, String> specificData = new Hashtable<ICallbackInterface, String>();
  37. /**
  38. * Create a new instance of the Callback Object.
  39. *
  40. * @param parser IRCParser That owns this callback
  41. * @param manager CallbackManager that is in charge of this callback
  42. */
  43. public CallbackObjectSpecific(final IRCParser parser, final CallbackManager manager) { super(parser, manager); }
  44. /**
  45. * Used to check if a channel matches the specificData.
  46. *
  47. * @param eMethod Object that is being called back to
  48. * @param cChannel ChannelInfo object for the channel to test
  49. * @return true if channel given matches the specifics for the method given
  50. */
  51. protected boolean isValidChan(final ICallbackInterface eMethod, final ChannelInfo cChannel) {
  52. if (specificData.containsKey(eMethod)) {
  53. if (!myParser.getIRCStringConverter().equalsIgnoreCase(cChannel.getName(), specificData.get(eMethod))) { return false; }
  54. }
  55. return true;
  56. }
  57. /**
  58. * Used to check if a hostname matches the specificData.
  59. *
  60. * @param eMethod Object that is being called back to
  61. * @param sHost Hostname of user that sent the query
  62. * @return true if host given matches the specifics for the method given
  63. */
  64. protected boolean isValidUser(final ICallbackInterface eMethod, final String sHost) {
  65. final String nickname = ClientInfo.parseHost(sHost);
  66. if (specificData.containsKey(eMethod)) {
  67. if (!myParser.getIRCStringConverter().equalsIgnoreCase(nickname, specificData.get(eMethod))) { return false; }
  68. }
  69. return true;
  70. }
  71. // We override the default add method to make sure that any add with no
  72. // specifics will have the specific data removed.
  73. /**
  74. * Add a new callback.
  75. *
  76. * @param eMethod Object to callback to.
  77. */
  78. public void add(final ICallbackInterface eMethod) {
  79. addCallback(eMethod);
  80. if (specificData.containsKey(eMethod)) { specificData.remove(eMethod); }
  81. }
  82. /**
  83. * Add a new callback with a specific target.
  84. *
  85. * @param eMethod Object to callback to.
  86. * @param specificTarget Target that must match for callback to be called.
  87. */
  88. public void add(final ICallbackInterface eMethod, final String specificTarget) {
  89. add(eMethod);
  90. if (!specificTarget.isEmpty()) {
  91. specificData.put(eMethod, specificTarget);
  92. }
  93. }
  94. /**
  95. * Remove a callback.
  96. *
  97. * @param eMethod Object to remove callback to.
  98. */
  99. public void del(final ICallbackInterface eMethod) {
  100. delCallback(eMethod);
  101. if (specificData.containsKey(eMethod)) { specificData.remove(eMethod); }
  102. }
  103. }