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

ChannelEventHandler.java 13KB

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