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

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