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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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.ActionManager;
  24. import com.dmdirc.actions.CoreActionType;
  25. import com.dmdirc.parser.irc.ChannelClientInfo;
  26. import com.dmdirc.parser.irc.ChannelInfo;
  27. import com.dmdirc.parser.irc.ClientInfo;
  28. import com.dmdirc.parser.irc.IRCParser;
  29. import com.dmdirc.parser.irc.callbacks.CallbackManager;
  30. import com.dmdirc.parser.irc.callbacks.CallbackNotFoundException;
  31. import com.dmdirc.parser.irc.callbacks.interfaces.*;
  32. /**
  33. * Handles events for channel objects.
  34. *
  35. * @author chris
  36. */
  37. public final class ChannelEventHandler extends EventHandler implements
  38. IChannelMessage, IChannelGotNames, IChannelTopic, IChannelJoin,
  39. IChannelPart, IChannelKick, IChannelQuit, IChannelAction,
  40. IChannelNickChanged, IChannelModeChanged, IChannelUserModeChanged,
  41. IChannelCTCP, IAwayStateOther, IChannelNotice {
  42. /** The channel that owns this event handler. */
  43. private final Channel owner;
  44. /**
  45. * Creates a new instance of ChannelEventHandler.
  46. *
  47. * @param owner The channel that owns this event handler.
  48. */
  49. public ChannelEventHandler(final Channel owner) {
  50. super();
  51. this.owner = owner;
  52. }
  53. /** {@inheritDoc} */
  54. @Override
  55. protected void addCallback(final CallbackManager cbm, final String name)
  56. throws CallbackNotFoundException {
  57. if ("onAwayStateOther".equals(name)) {
  58. cbm.addCallback(name, this);
  59. } else {
  60. cbm.addCallback(name, this, owner.getChannelInfo().getName());
  61. }
  62. }
  63. /** {@inheritDoc} */
  64. @Override
  65. protected Server getServer() {
  66. return owner.getServer();
  67. }
  68. /**
  69. * Determines if the specified client represents us.
  70. *
  71. * @param client The client to be tested
  72. * @return True if the client is ourself, false otherwise.
  73. */
  74. protected boolean isMyself(final ChannelClientInfo client) {
  75. return client.getClient().equals(owner.getServer().getParser().getMyself());
  76. }
  77. /** {@inheritDoc} */
  78. @Override
  79. public void onChannelMessage(final IRCParser tParser,
  80. final ChannelInfo cChannel, final ChannelClientInfo cChannelClient,
  81. final String sMessage, final String sHost) {
  82. checkParser(tParser);
  83. owner.doNotification(
  84. isMyself(cChannelClient) ? "channelSelfExternalMessage" : "channelMessage",
  85. CoreActionType.CHANNEL_MESSAGE, cChannelClient, sMessage);
  86. }
  87. /** {@inheritDoc} */
  88. @Override
  89. public void onChannelGotNames(final IRCParser tParser, final ChannelInfo cChannel) {
  90. checkParser(tParser);
  91. owner.setClients(cChannel.getChannelClients());
  92. ActionManager.processEvent(CoreActionType.CHANNEL_GOTNAMES, null, owner);
  93. }
  94. /** {@inheritDoc} */
  95. @Override
  96. public void onChannelTopic(final IRCParser tParser,
  97. final ChannelInfo cChannel, final boolean bIsJoinTopic) {
  98. checkParser(tParser);
  99. final Topic newTopic = new Topic(cChannel.getTopic(),
  100. cChannel.getTopicUser(), cChannel.getTopicTime());
  101. if (bIsJoinTopic) {
  102. owner.doNotification("channelTopicDiscovered", CoreActionType.CHANNEL_GOTTOPIC,
  103. newTopic);
  104. } else {
  105. owner.doNotification("channelTopicChanged", CoreActionType.CHANNEL_TOPICCHANGE,
  106. cChannel.getUser(cChannel.getTopicUser(), true), cChannel.getTopic());
  107. }
  108. owner.addTopic(newTopic);
  109. }
  110. /** {@inheritDoc} */
  111. @Override
  112. public void onChannelJoin(final IRCParser tParser, final ChannelInfo cChannel,
  113. final ChannelClientInfo cChannelClient) {
  114. checkParser(tParser);
  115. owner.doNotification("channelJoin", CoreActionType.CHANNEL_JOIN, cChannelClient);
  116. owner.addClient(cChannelClient);
  117. }
  118. /** {@inheritDoc} */
  119. @Override
  120. public void onChannelPart(final IRCParser tParser, final ChannelInfo cChannel,
  121. final ChannelClientInfo cChannelClient, final String sReason) {
  122. checkParser(tParser);
  123. owner.doNotification("channel"
  124. + (isMyself(cChannelClient) ? "Self" : "") + "Part"
  125. + (sReason.isEmpty() ? "" : "Reason"), CoreActionType.CHANNEL_PART,
  126. cChannelClient, sReason);
  127. owner.removeClient(cChannelClient);
  128. }
  129. /** {@inheritDoc} */
  130. @Override
  131. public void onChannelKick(final IRCParser tParser, final ChannelInfo cChannel,
  132. final ChannelClientInfo cKickedClient, final ChannelClientInfo cKickedByClient,
  133. final String sReason, final String sKickedByHost) {
  134. checkParser(tParser);
  135. owner.doNotification("channelKick" + (sReason.isEmpty() ? "" : "Reason"),
  136. CoreActionType.CHANNEL_KICK, cKickedByClient, cKickedClient, sReason);
  137. owner.removeClient(cKickedClient);
  138. }
  139. /** {@inheritDoc} */
  140. @Override
  141. public void onChannelQuit(final IRCParser tParser, final ChannelInfo cChannel,
  142. final ChannelClientInfo cChannelClient, final String sReason) {
  143. checkParser(tParser);
  144. owner.doNotification("channelQuit" + (sReason.isEmpty() ? "" : "Reason"),
  145. CoreActionType.CHANNEL_QUIT, cChannelClient, sReason);
  146. owner.removeClient(cChannelClient);
  147. }
  148. /** {@inheritDoc} */
  149. @Override
  150. public void onChannelAction(final IRCParser tParser, final ChannelInfo cChannel,
  151. final ChannelClientInfo cChannelClient, final String sMessage,
  152. final String sHost) {
  153. checkParser(tParser);
  154. owner.doNotification(
  155. isMyself(cChannelClient) ? "channelSelfExternalAction" : "channelAction",
  156. CoreActionType.CHANNEL_ACTION, cChannelClient, sMessage);
  157. }
  158. /** {@inheritDoc} */
  159. @Override
  160. public void onChannelNickChanged(final IRCParser tParser, final ChannelInfo cChannel,
  161. final ChannelClientInfo cChannelClient, final String sOldNick) {
  162. checkParser(tParser);
  163. owner.doNotification(
  164. isMyself(cChannelClient) ? "channelSelfNickChange" : "channelNickChange",
  165. CoreActionType.CHANNEL_NICKCHANGE, cChannelClient, sOldNick);
  166. owner.renameClient(sOldNick, cChannelClient.getNickname());
  167. }
  168. /** {@inheritDoc} */
  169. @Override
  170. public void onChannelModeChanged(final IRCParser tParser, final ChannelInfo cChannel,
  171. final ChannelClientInfo cChannelClient, final String sHost,
  172. final String sModes) {
  173. checkParser(tParser);
  174. if (sHost.isEmpty()) {
  175. owner.doNotification(sModes.length() <= 1 ? "channelNoModes"
  176. : "channelModeDiscovered", CoreActionType.CHANNEL_MODESDISCOVERED,
  177. sModes.length() <= 1 ? "" : sModes);
  178. } else {
  179. owner.doNotification(isMyself(cChannelClient) ? "channelSelfModeChanged"
  180. : "channelModeChanged", CoreActionType.CHANNEL_MODECHANGE,
  181. cChannelClient, sModes);
  182. }
  183. owner.refreshClients();
  184. }
  185. /** {@inheritDoc} */
  186. @Override
  187. public void onChannelUserModeChanged(final IRCParser tParser,
  188. final ChannelInfo cChannel, final ChannelClientInfo cChangedClient,
  189. final ChannelClientInfo cSetByClient, final String sHost, final String sMode) {
  190. checkParser(tParser);
  191. if (owner.getConfigManager().getOptionBool("channel", "splitusermodes", false)) {
  192. String format = "channelSplitUserMode_" + sMode;
  193. if (!owner.getConfigManager().hasOption("formatter", format)) {
  194. format = "channelSplitUserMode_default";
  195. }
  196. owner.doNotification(format, CoreActionType.CHANNEL_USERMODECHANGE,
  197. cSetByClient, cChangedClient, sMode);
  198. }
  199. }
  200. /** {@inheritDoc} */
  201. @Override
  202. public void onChannelCTCP(final IRCParser tParser, final ChannelInfo cChannel,
  203. final ChannelClientInfo cChannelClient, final String sType,
  204. final String sMessage, final String sHost) {
  205. checkParser(tParser);
  206. owner.doNotification("channelCTCP", CoreActionType.CHANNEL_CTCP,
  207. cChannelClient, sType, sMessage);
  208. owner.getServer().sendCTCPReply(cChannelClient.getNickname(), sType, sMessage);
  209. }
  210. /** {@inheritDoc} */
  211. @Override
  212. public void onAwayStateOther(final IRCParser tParser,
  213. final ClientInfo client, final boolean state) {
  214. checkParser(tParser);
  215. final ChannelClientInfo channelClient = owner.getChannelInfo().getUser(client);
  216. if (channelClient != null) {
  217. owner.doNotification(state ? "channelUserAway" : "channelUserBack",
  218. state ? CoreActionType.CHANNEL_USERAWAY : CoreActionType.CHANNEL_USERBACK,
  219. channelClient);
  220. }
  221. }
  222. /** {@inheritDoc} */
  223. @Override
  224. public void onChannelNotice(final IRCParser tParser,
  225. final ChannelInfo cChannel, final ChannelClientInfo cChannelClient,
  226. final String sMessage, final String sHost) {
  227. checkParser(tParser);
  228. owner.doNotification("channelNotice", CoreActionType.CHANNEL_NOTICE,
  229. cChannelClient, sMessage);
  230. }
  231. }