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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * Copyright (c) 2006-2010 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.interfaces.ChannelClientInfo;
  26. import com.dmdirc.parser.interfaces.ChannelInfo;
  27. import com.dmdirc.parser.interfaces.ClientInfo;
  28. import com.dmdirc.parser.interfaces.Parser;
  29. import com.dmdirc.parser.common.CallbackManager;
  30. import com.dmdirc.parser.interfaces.callbacks.*;
  31. /**
  32. * Handles events for channel objects.
  33. *
  34. * @author chris
  35. */
  36. public final class ChannelEventHandler extends EventHandler implements
  37. ChannelMessageListener, ChannelNamesListener, ChannelTopicListener,
  38. ChannelJoinListener, ChannelPartListener, ChannelKickListener,
  39. ChannelQuitListener, ChannelActionListener, ChannelNickChangeListener,
  40. ChannelModeChangeListener, ChannelUserModeChangeListener,
  41. ChannelCtcpListener, OtherAwayStateListener, ChannelNoticeListener,
  42. ChannelNonUserModeChangeListener, ChannelModeNoticeListener {
  43. /** The channel that owns this event handler. */
  44. private final Channel owner;
  45. /**
  46. * Creates a new instance of ChannelEventHandler.
  47. *
  48. * @param owner The channel that owns this event handler.
  49. */
  50. public ChannelEventHandler(final Channel owner) {
  51. super();
  52. this.owner = owner;
  53. }
  54. /** {@inheritDoc} */
  55. @SuppressWarnings("unchecked")
  56. @Override
  57. protected <T extends CallbackInterface> void addCallback(final CallbackManager cbm,
  58. final Class<T> type) {
  59. if (OtherAwayStateListener.class.equals(type)) {
  60. cbm.addCallback(type, (T) this);
  61. } else {
  62. cbm.addCallback(type, (T) this, owner.getChannelInfo().getName());
  63. }
  64. }
  65. /** {@inheritDoc} */
  66. @Override
  67. protected Server getServer() {
  68. return owner.getServer();
  69. }
  70. /**
  71. * Determines if the specified client represents us.
  72. *
  73. * @param client The client to be tested
  74. * @return True if the client is ourself, false otherwise.
  75. */
  76. protected boolean isMyself(final ChannelClientInfo client) {
  77. return client.getClient().equals(owner.getServer().getParser().getLocalClient());
  78. }
  79. /** {@inheritDoc} */
  80. @Override
  81. public void onChannelMessage(final Parser tParser,
  82. final ChannelInfo cChannel, final ChannelClientInfo cChannelClient,
  83. final String sMessage, final String sHost) {
  84. checkParser(tParser);
  85. owner.doNotification(
  86. isMyself(cChannelClient) ? "channelSelfExternalMessage" : "channelMessage",
  87. CoreActionType.CHANNEL_MESSAGE, cChannelClient, sMessage);
  88. }
  89. /** {@inheritDoc} */
  90. @Override
  91. public void onChannelGotNames(final Parser tParser, final ChannelInfo cChannel) {
  92. checkParser(tParser);
  93. owner.setClients(cChannel.getChannelClients());
  94. ActionManager.processEvent(CoreActionType.CHANNEL_GOTNAMES, null, owner);
  95. }
  96. /** {@inheritDoc} */
  97. @Override
  98. public void onChannelTopic(final Parser tParser,
  99. final ChannelInfo cChannel, final boolean bIsJoinTopic) {
  100. checkParser(tParser);
  101. final Topic newTopic = new Topic(cChannel.getTopic(),
  102. cChannel.getTopicSetter(), cChannel.getTopicTime());
  103. if (bIsJoinTopic) {
  104. if (newTopic.getTopic().isEmpty()) {
  105. owner.doNotification("channelNoTopic", CoreActionType.CHANNEL_NOTOPIC);
  106. } else {
  107. owner.doNotification("channelTopicDiscovered", CoreActionType.CHANNEL_GOTTOPIC,
  108. newTopic);
  109. }
  110. } else {
  111. owner.doNotification(cChannel.getTopic().isEmpty()
  112. ? "channelTopicRemoved" : "channelTopicChanged",
  113. CoreActionType.CHANNEL_TOPICCHANGE,
  114. cChannel.getChannelClient(cChannel.getTopicSetter(), true), cChannel.getTopic());
  115. }
  116. if (!bIsJoinTopic || !newTopic.getTopic().isEmpty()) {
  117. owner.addTopic(newTopic);
  118. }
  119. }
  120. /** {@inheritDoc} */
  121. @Override
  122. public void onChannelJoin(final Parser tParser, final ChannelInfo cChannel,
  123. final ChannelClientInfo cChannelClient) {
  124. checkParser(tParser);
  125. owner.doNotification("channelJoin", CoreActionType.CHANNEL_JOIN, cChannelClient);
  126. owner.addClient(cChannelClient);
  127. }
  128. /** {@inheritDoc} */
  129. @Override
  130. public void onChannelPart(final Parser tParser, final ChannelInfo cChannel,
  131. final ChannelClientInfo cChannelClient, final String sReason) {
  132. checkParser(tParser);
  133. owner.doNotification("channel"
  134. + (isMyself(cChannelClient) ? "Self" : "") + "Part"
  135. + (sReason.isEmpty() ? "" : "Reason"), CoreActionType.CHANNEL_PART,
  136. cChannelClient, sReason);
  137. owner.removeClient(cChannelClient);
  138. }
  139. /** {@inheritDoc} */
  140. @Override
  141. public void onChannelKick(final Parser tParser, final ChannelInfo cChannel,
  142. final ChannelClientInfo cKickedClient, final ChannelClientInfo cKickedByClient,
  143. final String sReason, final String sKickedByHost) {
  144. checkParser(tParser);
  145. owner.doNotification("channelKick" + (sReason.isEmpty() ? "" : "Reason"),
  146. CoreActionType.CHANNEL_KICK, cKickedByClient, cKickedClient, sReason);
  147. owner.removeClient(cKickedClient);
  148. }
  149. /** {@inheritDoc} */
  150. @Override
  151. public void onChannelQuit(final Parser tParser, final ChannelInfo cChannel,
  152. final ChannelClientInfo cChannelClient, final String sReason) {
  153. checkParser(tParser);
  154. owner.doNotification("channelQuit" + (sReason.isEmpty() ? "" : "Reason"),
  155. CoreActionType.CHANNEL_QUIT, cChannelClient, sReason);
  156. owner.removeClient(cChannelClient);
  157. }
  158. /** {@inheritDoc} */
  159. @Override
  160. public void onChannelAction(final Parser tParser, final ChannelInfo cChannel,
  161. final ChannelClientInfo cChannelClient, final String sMessage,
  162. final String sHost) {
  163. checkParser(tParser);
  164. owner.doNotification(
  165. isMyself(cChannelClient) ? "channelSelfExternalAction" : "channelAction",
  166. CoreActionType.CHANNEL_ACTION, cChannelClient, sMessage);
  167. }
  168. /** {@inheritDoc} */
  169. @Override
  170. public void onChannelNickChanged(final Parser tParser, final ChannelInfo cChannel,
  171. final ChannelClientInfo cChannelClient, final String sOldNick) {
  172. checkParser(tParser);
  173. owner.doNotification(
  174. isMyself(cChannelClient) ? "channelSelfNickChange" : "channelNickChange",
  175. CoreActionType.CHANNEL_NICKCHANGE, cChannelClient, sOldNick);
  176. owner.renameClient(sOldNick, cChannelClient.getClient().getNickname());
  177. }
  178. /** {@inheritDoc} */
  179. @Override
  180. public void onChannelModeChanged(final Parser tParser, final ChannelInfo cChannel,
  181. final ChannelClientInfo cChannelClient, final String sHost,
  182. final String sModes) {
  183. checkParser(tParser);
  184. if (!owner.getConfigManager().getOptionBool("channel", "splitusermodes")
  185. || !owner.getConfigManager().getOptionBool("channel", "hideduplicatemodes")) {
  186. if (sHost.isEmpty()) {
  187. owner.doNotification(sModes.length() <= 1 ? "channelNoModes"
  188. : "channelModeDiscovered", CoreActionType.CHANNEL_MODESDISCOVERED,
  189. sModes.length() <= 1 ? "" : sModes);
  190. } else {
  191. owner.doNotification(isMyself(cChannelClient) ? "channelSelfModeChanged"
  192. : "channelModeChanged", CoreActionType.CHANNEL_MODECHANGE,
  193. cChannelClient, sModes);
  194. }
  195. }
  196. owner.refreshClients();
  197. }
  198. /** {@inheritDoc} */
  199. @Override
  200. public void onChannelUserModeChanged(final Parser tParser,
  201. final ChannelInfo cChannel, final ChannelClientInfo cChangedClient,
  202. final ChannelClientInfo cSetByClient, final String sHost, final String sMode) {
  203. checkParser(tParser);
  204. if (owner.getConfigManager().getOptionBool("channel", "splitusermodes")) {
  205. String format = "channelSplitUserMode_" + sMode;
  206. if (!owner.getConfigManager().hasOptionString("formatter", format)) {
  207. format = "channelSplitUserMode_default";
  208. }
  209. owner.doNotification(format, CoreActionType.CHANNEL_USERMODECHANGE,
  210. cSetByClient, cChangedClient, sMode);
  211. }
  212. }
  213. /** {@inheritDoc} */
  214. @Override
  215. public void onChannelCTCP(final Parser tParser, final ChannelInfo cChannel,
  216. final ChannelClientInfo cChannelClient, final String sType,
  217. final String sMessage, final String sHost) {
  218. checkParser(tParser);
  219. owner.doNotification("channelCTCP", CoreActionType.CHANNEL_CTCP,
  220. cChannelClient, sType, sMessage);
  221. owner.getServer().sendCTCPReply(cChannelClient.getClient().getNickname(), sType, sMessage);
  222. }
  223. /** {@inheritDoc} */
  224. @Override
  225. public void onAwayStateOther(final Parser tParser,
  226. final ClientInfo client, final boolean state) {
  227. checkParser(tParser);
  228. final ChannelClientInfo channelClient = owner.getChannelInfo().getChannelClient(client);
  229. if (channelClient != null) {
  230. owner.doNotification(state ? "channelUserAway" : "channelUserBack",
  231. state ? CoreActionType.CHANNEL_USERAWAY : CoreActionType.CHANNEL_USERBACK,
  232. channelClient);
  233. }
  234. }
  235. /** {@inheritDoc} */
  236. @Override
  237. public void onChannelNotice(final Parser tParser,
  238. final ChannelInfo cChannel, final ChannelClientInfo cChannelClient,
  239. final String sMessage, final String sHost) {
  240. checkParser(tParser);
  241. owner.doNotification("channelNotice", CoreActionType.CHANNEL_NOTICE,
  242. cChannelClient, sMessage);
  243. }
  244. /** {@inheritDoc} */
  245. @Override
  246. public void onChannelNonUserModeChanged(final Parser tParser,
  247. final ChannelInfo cChannel, final ChannelClientInfo cChannelClient,
  248. final String sHost, final String sModes) {
  249. checkParser(tParser);
  250. if (owner.getConfigManager().getOptionBool("channel", "splitusermodes")
  251. && owner.getConfigManager().getOptionBool("channel", "hideduplicatemodes")) {
  252. if (sHost.isEmpty()) {
  253. owner.doNotification(sModes.length() <= 1 ? "channelNoModes"
  254. : "channelModeDiscovered", CoreActionType.CHANNEL_MODESDISCOVERED,
  255. sModes.length() <= 1 ? "" : sModes);
  256. } else {
  257. owner.doNotification(isMyself(cChannelClient) ? "channelSelfModeChanged"
  258. : "channelModeChanged", CoreActionType.CHANNEL_MODECHANGE,
  259. cChannelClient, sModes);
  260. }
  261. }
  262. owner.refreshClients();
  263. }
  264. /** {@inheritDoc} */
  265. @Override
  266. public void onChannelModeNotice(final Parser tParser,
  267. final ChannelInfo cChannel, final char prefix,
  268. final ChannelClientInfo cChannelClient, final String sMessage,
  269. final String sHost) {
  270. checkParser(tParser);
  271. owner.doNotification("channelModeNotice", CoreActionType.CHANNEL_MODE_NOTICE,
  272. cChannelClient, String.valueOf(prefix), sMessage);
  273. }
  274. }