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.

ChannelEventHandler.java 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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;
  23. import com.dmdirc.actions.CoreActionType;
  24. import com.dmdirc.parser.ChannelClientInfo;
  25. import com.dmdirc.parser.ChannelInfo;
  26. import com.dmdirc.parser.ClientInfo;
  27. import com.dmdirc.parser.IRCParser;
  28. import com.dmdirc.parser.callbacks.CallbackManager;
  29. import com.dmdirc.parser.callbacks.CallbackNotFoundException;
  30. import com.dmdirc.parser.callbacks.interfaces.*;
  31. /**
  32. * Handles events for channel objects.
  33. *
  34. * @author chris
  35. */
  36. public final class ChannelEventHandler extends EventHandler implements
  37. IChannelMessage, IChannelGotNames, IChannelTopic, IChannelJoin,
  38. IChannelPart, IChannelKick, IChannelQuit, IChannelAction,
  39. IChannelNickChanged, IChannelModeChanged, IChannelUserModeChanged,
  40. IChannelCTCP, IAwayStateOther {
  41. /** The channel that owns this event handler. */
  42. private final Channel owner;
  43. /**
  44. * Creates a new instance of ChannelEventHandler.
  45. *
  46. * @param owner The channel that owns this event handler.
  47. */
  48. public ChannelEventHandler(final Channel owner) {
  49. this.owner = owner;
  50. }
  51. /** {@inheritDoc} */
  52. @Override
  53. protected void addCallback(final CallbackManager cbm, final String name)
  54. throws CallbackNotFoundException {
  55. if (name.equals("OnAwayStateOther")) {
  56. cbm.addCallback(name, this);
  57. } else {
  58. cbm.addCallback(name, this, owner.getChannelInfo().getName());
  59. }
  60. }
  61. /** {@inheritDoc} */
  62. @Override
  63. protected IRCParser getParser() {
  64. return owner.getServer().getParser();
  65. }
  66. /**
  67. * Determines if the specified client represents us.
  68. *
  69. * @param client The client to be tested
  70. * @return True if the client is ourself, false otherwise.
  71. */
  72. protected boolean isMyself(final ChannelClientInfo client) {
  73. return client.getClient().equals(owner.getServer().getParser().getMyself());
  74. }
  75. /** {@inheritDoc} */
  76. @Override
  77. public void onChannelMessage(final IRCParser tParser,
  78. final ChannelInfo cChannel, final ChannelClientInfo cChannelClient,
  79. final String sMessage, final String sHost) {
  80. checkParser(tParser);
  81. owner.doNotification(
  82. isMyself(cChannelClient) ? "channelSelfExternalMessage" : "channelMessage",
  83. CoreActionType.CHANNEL_MESSAGE, cChannelClient, sMessage);
  84. }
  85. /** {@inheritDoc} */
  86. @Override
  87. public void onChannelGotNames(final IRCParser tParser, final ChannelInfo cChannel) {
  88. checkParser(tParser);
  89. owner.onChannelGotNames();
  90. }
  91. /** {@inheritDoc} */
  92. @Override
  93. public void onChannelTopic(final IRCParser tParser, final ChannelInfo cChannel,
  94. final boolean bIsJoinTopic) {
  95. checkParser(tParser);
  96. owner.onChannelTopic(bIsJoinTopic);
  97. }
  98. /** {@inheritDoc} */
  99. @Override
  100. public void onChannelJoin(final IRCParser tParser, final ChannelInfo cChannel,
  101. final ChannelClientInfo cChannelClient) {
  102. checkParser(tParser);
  103. owner.doNotification("channelJoin", CoreActionType.CHANNEL_JOIN, cChannelClient);
  104. owner.addClient(cChannelClient);
  105. }
  106. /** {@inheritDoc} */
  107. @Override
  108. public void onChannelPart(final IRCParser tParser, final ChannelInfo cChannel,
  109. final ChannelClientInfo cChannelClient, final String sReason) {
  110. checkParser(tParser);
  111. owner.doNotification("channel"
  112. + (isMyself(cChannelClient) ? "Self" : "") + "Part"
  113. + (sReason.isEmpty() ? "" : "Reason"), CoreActionType.CHANNEL_PART,
  114. cChannelClient, sReason);
  115. owner.removeClient(cChannelClient);
  116. }
  117. /** {@inheritDoc} */
  118. @Override
  119. public void onChannelKick(final IRCParser tParser, final ChannelInfo cChannel,
  120. final ChannelClientInfo cKickedClient, final ChannelClientInfo cKickedByClient,
  121. final String sReason, final String sKickedByHost) {
  122. checkParser(tParser);
  123. owner.doNotification("channelKick" + (sReason.isEmpty() ? "" : "Reason"),
  124. CoreActionType.CHANNEL_KICK, cKickedByClient, cKickedClient, sReason);
  125. owner.removeClient(cKickedClient);
  126. }
  127. /** {@inheritDoc} */
  128. @Override
  129. public void onChannelQuit(final IRCParser tParser, final ChannelInfo cChannel,
  130. final ChannelClientInfo cChannelClient, final String sReason) {
  131. checkParser(tParser);
  132. owner.doNotification("channelQuit" + (sReason.isEmpty() ? "" : "Reason"),
  133. CoreActionType.CHANNEL_QUIT, cChannelClient, sReason);
  134. owner.removeClient(cChannelClient);
  135. }
  136. /** {@inheritDoc} */
  137. @Override
  138. public void onChannelAction(final IRCParser tParser, final ChannelInfo cChannel,
  139. final ChannelClientInfo cChannelClient, final String sMessage, final String sHost) {
  140. checkParser(tParser);
  141. owner.doNotification(
  142. isMyself(cChannelClient) ? "channelSelfExternalAction" : "channelAction",
  143. CoreActionType.CHANNEL_ACTION, cChannelClient, sMessage);
  144. }
  145. /** {@inheritDoc} */
  146. @Override
  147. public void onChannelNickChanged(final IRCParser tParser, final ChannelInfo cChannel,
  148. final ChannelClientInfo cChannelClient, final String sOldNick) {
  149. checkParser(tParser);
  150. owner.doNotification(
  151. isMyself(cChannelClient) ? "channelSelfNickChange" : "channelNickChange",
  152. CoreActionType.CHANNEL_NICKCHANGE, cChannelClient, sOldNick);
  153. owner.renameClient(sOldNick, cChannelClient.getNickname());
  154. }
  155. /** {@inheritDoc} */
  156. @Override
  157. public void onChannelModeChanged(final IRCParser tParser, final ChannelInfo cChannel,
  158. final ChannelClientInfo cChannelClient, final String sHost, final String sModes) {
  159. checkParser(tParser);
  160. owner.onChannelModeChanged(cChannelClient, sHost, sModes);
  161. }
  162. /** {@inheritDoc} */
  163. @Override
  164. public void onChannelUserModeChanged(final IRCParser tParser, final ChannelInfo cChannel,
  165. final ChannelClientInfo cChangedClient, final ChannelClientInfo cSetByClient,
  166. final String sHost, final String sMode) {
  167. checkParser(tParser);
  168. owner.onChannelUserModeChanged(cChangedClient, cSetByClient, sMode);
  169. }
  170. /** {@inheritDoc} */
  171. @Override
  172. public void onChannelCTCP(final IRCParser tParser, final ChannelInfo cChannel,
  173. final ChannelClientInfo cChannelClient, final String sType,
  174. final String sMessage, final String sHost) {
  175. checkParser(tParser);
  176. owner.doNotification("channelCTCP", CoreActionType.CHANNEL_CTCP,
  177. cChannelClient, sType, sMessage);
  178. owner.getServer().sendCTCPReply(cChannelClient.getNickname(), sType, sMessage);
  179. }
  180. /** {@inheritDoc} */
  181. @Override
  182. public void onAwayStateOther(final IRCParser tParser, final ClientInfo client,
  183. final boolean state) {
  184. checkParser(tParser);
  185. owner.onAwayStateOther(client, state);
  186. }
  187. }