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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * Copyright (c) 2006-2011 DMDirc Developers
  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.common.CallbackManager;
  27. import com.dmdirc.parser.interfaces.ChannelClientInfo;
  28. import com.dmdirc.parser.interfaces.ChannelInfo;
  29. import com.dmdirc.parser.interfaces.ClientInfo;
  30. import com.dmdirc.parser.interfaces.Parser;
  31. import com.dmdirc.parser.interfaces.callbacks.*; //NOPMD
  32. import java.util.Date;
  33. /**
  34. * Handles events for channel objects.
  35. */
  36. public 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. ChannelListModeListener {
  44. /** The channel that owns this event handler. */
  45. private final Channel owner;
  46. /**
  47. * Creates a new instance of ChannelEventHandler.
  48. *
  49. * @param owner The channel that owns this event handler.
  50. */
  51. public ChannelEventHandler(final Channel owner) {
  52. super();
  53. this.owner = owner;
  54. }
  55. /** {@inheritDoc} */
  56. @SuppressWarnings("unchecked")
  57. @Override
  58. protected <T extends CallbackInterface> void addCallback(final CallbackManager cbm,
  59. final Class<T> type) {
  60. if (OtherAwayStateListener.class.equals(type)) {
  61. cbm.addCallback(type, (T) this);
  62. } else {
  63. cbm.addCallback(type, (T) this, owner.getChannelInfo().getName());
  64. }
  65. }
  66. /** {@inheritDoc} */
  67. @Override
  68. protected Server getServer() {
  69. return owner.getServer();
  70. }
  71. /**
  72. * Determines if the specified client represents us.
  73. *
  74. * @param client The client to be tested
  75. * @return True if the client is ourself, false otherwise.
  76. */
  77. protected boolean isMyself(final ChannelClientInfo client) {
  78. return client.getClient().equals(owner.getServer().getParser().getLocalClient());
  79. }
  80. /** {@inheritDoc} */
  81. @Override
  82. public void onChannelMessage(final Parser parser, final Date date,
  83. final ChannelInfo channel, final ChannelClientInfo client,
  84. final String message, final String host) {
  85. checkParser(parser);
  86. owner.doNotification(date,
  87. isMyself(client) ? "channelSelfExternalMessage" : "channelMessage",
  88. CoreActionType.CHANNEL_MESSAGE, client, message);
  89. }
  90. /** {@inheritDoc} */
  91. @Override
  92. public void onChannelGotNames(final Parser parser, final Date date, final ChannelInfo channel) {
  93. checkParser(parser);
  94. owner.setClients(channel.getChannelClients());
  95. ActionManager.getActionManager().triggerEvent(
  96. CoreActionType.CHANNEL_GOTNAMES, null, owner);
  97. }
  98. /** {@inheritDoc} */
  99. @Override
  100. public void onChannelTopic(final Parser parser, final Date date,
  101. final ChannelInfo channel, final boolean isJoinTopic) {
  102. checkParser(parser);
  103. final Topic newTopic = new Topic(channel.getTopic(),
  104. channel.getTopicSetter(), channel.getTopicTime());
  105. if (isJoinTopic) {
  106. if (newTopic.getTopic().isEmpty()) {
  107. owner.doNotification(date, "channelNoTopic", CoreActionType.CHANNEL_NOTOPIC);
  108. } else {
  109. owner.doNotification(date, "channelTopicDiscovered", CoreActionType.CHANNEL_GOTTOPIC,
  110. newTopic);
  111. }
  112. } else {
  113. owner.doNotification(date, channel.getTopic().isEmpty()
  114. ? "channelTopicRemoved" : "channelTopicChanged",
  115. CoreActionType.CHANNEL_TOPICCHANGE,
  116. channel.getChannelClient(channel.getTopicSetter(), true), channel.getTopic());
  117. }
  118. if (!isJoinTopic
  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 parser, final Date date, final ChannelInfo channel,
  131. final ChannelClientInfo client) {
  132. checkParser(parser);
  133. owner.doNotification(date, "channelJoin", CoreActionType.CHANNEL_JOIN, client);
  134. owner.addClient(client);
  135. }
  136. /** {@inheritDoc} */
  137. @Override
  138. public void onChannelPart(final Parser parser, final Date date, final ChannelInfo channel,
  139. final ChannelClientInfo client, final String reason) {
  140. checkParser(parser);
  141. owner.doNotification(date, "channel"
  142. + (isMyself(client) ? "Self" : "") + "Part"
  143. + (reason.isEmpty() ? "" : "Reason"), CoreActionType.CHANNEL_PART,
  144. client, reason);
  145. owner.removeClient(client);
  146. }
  147. /** {@inheritDoc} */
  148. @Override
  149. public void onChannelKick(final Parser parser, final Date date, final ChannelInfo channel,
  150. final ChannelClientInfo kickedClient, final ChannelClientInfo client,
  151. final String reason, final String host) {
  152. checkParser(parser);
  153. owner.doNotification(date, "channelKick" + (reason.isEmpty() ? "" : "Reason"),
  154. CoreActionType.CHANNEL_KICK, client, kickedClient, reason);
  155. owner.removeClient(kickedClient);
  156. }
  157. /** {@inheritDoc} */
  158. @Override
  159. public void onChannelQuit(final Parser parser, final Date date, final ChannelInfo channel,
  160. final ChannelClientInfo client, final String reason) {
  161. checkParser(parser);
  162. owner.doNotification(date, "channelQuit" + (reason.isEmpty() ? "" : "Reason"),
  163. CoreActionType.CHANNEL_QUIT, client, reason);
  164. owner.removeClient(client);
  165. }
  166. /** {@inheritDoc} */
  167. @Override
  168. public void onChannelAction(final Parser parser, final Date date, final ChannelInfo channel,
  169. final ChannelClientInfo client, final String message,
  170. final String host) {
  171. checkParser(parser);
  172. owner.doNotification(date,
  173. isMyself(client) ? "channelSelfExternalAction" : "channelAction",
  174. CoreActionType.CHANNEL_ACTION, client, message);
  175. }
  176. /** {@inheritDoc} */
  177. @Override
  178. public void onChannelNickChanged(final Parser parser, final Date date,
  179. final ChannelInfo channel, final ChannelClientInfo client, final String oldNick) {
  180. checkParser(parser);
  181. owner.doNotification(date,
  182. isMyself(client) ? "channelSelfNickChange" : "channelNickChange",
  183. CoreActionType.CHANNEL_NICKCHANGE, client, oldNick);
  184. owner.renameClient(oldNick, client.getClient().getNickname());
  185. }
  186. /** {@inheritDoc} */
  187. @Override
  188. public void onChannelModeChanged(final Parser parser, final Date date,
  189. final ChannelInfo channel, final ChannelClientInfo client, final String host,
  190. final String modes) {
  191. checkParser(parser);
  192. if (!owner.getConfigManager().getOptionBool("channel", "splitusermodes")
  193. || !owner.getConfigManager().getOptionBool("channel", "hideduplicatemodes")) {
  194. if (host.isEmpty()) {
  195. owner.doNotification(date, modes.length() <= 1 ? "channelNoModes"
  196. : "channelModeDiscovered", CoreActionType.CHANNEL_MODESDISCOVERED,
  197. modes.length() <= 1 ? "" : modes);
  198. } else {
  199. owner.doNotification(date, isMyself(client) ? "channelSelfModeChanged"
  200. : "channelModeChanged", CoreActionType.CHANNEL_MODECHANGE,
  201. client, modes);
  202. }
  203. }
  204. owner.refreshClients();
  205. }
  206. /** {@inheritDoc} */
  207. @Override
  208. public void onChannelUserModeChanged(final Parser parser, final Date date,
  209. final ChannelInfo channel, final ChannelClientInfo targetClient,
  210. final ChannelClientInfo client, final String host, final String mode) {
  211. checkParser(parser);
  212. if (owner.getConfigManager().getOptionBool("channel", "splitusermodes")) {
  213. String format = "channelSplitUserMode_" + mode;
  214. if (!owner.getConfigManager().hasOptionString("formatter", format)) {
  215. format = "channelSplitUserMode_default";
  216. }
  217. owner.doNotification(date, format, CoreActionType.CHANNEL_USERMODECHANGE,
  218. client, targetClient, mode);
  219. }
  220. }
  221. /** {@inheritDoc} */
  222. @Override
  223. public void onChannelCTCP(final Parser parser, final Date date,
  224. final ChannelInfo channel, final ChannelClientInfo client,
  225. final String type, final String message, final String host) {
  226. checkParser(parser);
  227. if (owner.doNotification(date, "channelCTCP", CoreActionType.CHANNEL_CTCP,
  228. client, type, message)) {
  229. owner.getServer().sendCTCPReply(client.getClient().getNickname(),
  230. type, message);
  231. }
  232. }
  233. /** {@inheritDoc} */
  234. @Override
  235. public void onAwayStateOther(final Parser parser, final Date date,
  236. final ClientInfo client, final AwayState oldState, final AwayState state) {
  237. checkParser(parser);
  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(date, (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 parser, final Date date,
  251. final ChannelInfo channel, final ChannelClientInfo client,
  252. final String message, final String host) {
  253. checkParser(parser);
  254. owner.doNotification(date, "channelNotice", CoreActionType.CHANNEL_NOTICE,
  255. client, message);
  256. }
  257. /** {@inheritDoc} */
  258. @Override
  259. public void onChannelNonUserModeChanged(final Parser parser, final Date date,
  260. final ChannelInfo channel, final ChannelClientInfo client,
  261. final String host, final String modes) {
  262. checkParser(parser);
  263. if (owner.getConfigManager().getOptionBool("channel", "splitusermodes")
  264. && owner.getConfigManager().getOptionBool("channel", "hideduplicatemodes")) {
  265. if (host.isEmpty()) {
  266. owner.doNotification(date, modes.length() <= 1 ? "channelNoModes"
  267. : "channelModeDiscovered", CoreActionType.CHANNEL_MODESDISCOVERED,
  268. modes.length() <= 1 ? "" : modes);
  269. } else {
  270. owner.doNotification(date, isMyself(client) ? "channelSelfModeChanged"
  271. : "channelModeChanged", CoreActionType.CHANNEL_MODECHANGE,
  272. client, modes);
  273. }
  274. }
  275. owner.refreshClients();
  276. }
  277. /** {@inheritDoc} */
  278. @Override
  279. public void onChannelModeNotice(final Parser parser, final Date date,
  280. final ChannelInfo channel, final char prefix,
  281. final ChannelClientInfo client, final String message,
  282. final String host) {
  283. checkParser(parser);
  284. owner.doNotification(date, "channelModeNotice", CoreActionType.CHANNEL_MODE_NOTICE,
  285. client, String.valueOf(prefix), message);
  286. }
  287. /** {@inheritDoc} */
  288. @Override
  289. public void onChannelGotListModes(final Parser parser, final Date date,
  290. final ChannelInfo channel, final char mode) {
  291. checkParser(parser);
  292. owner.doNotification(date, "channelListModeRetrieved",
  293. CoreActionType.CHANNEL_LISTMODERETRIEVED, Character.valueOf(mode));
  294. }
  295. }