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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * Copyright (c) 2006-2014 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.events.ChannelActionEvent;
  24. import com.dmdirc.events.ChannelCtcpEvent;
  25. import com.dmdirc.events.ChannelGotnamesEvent;
  26. import com.dmdirc.events.ChannelGottopicEvent;
  27. import com.dmdirc.events.ChannelJoinEvent;
  28. import com.dmdirc.events.ChannelKickEvent;
  29. import com.dmdirc.events.ChannelListmodesretrievedEvent;
  30. import com.dmdirc.events.ChannelMessageEvent;
  31. import com.dmdirc.events.ChannelModeNoticeEvent;
  32. import com.dmdirc.events.ChannelModechangeEvent;
  33. import com.dmdirc.events.ChannelModesdiscoveredEvent;
  34. import com.dmdirc.events.ChannelNickchangeEvent;
  35. import com.dmdirc.events.ChannelNoticeEvent;
  36. import com.dmdirc.events.ChannelNotopicEvent;
  37. import com.dmdirc.events.ChannelPartEvent;
  38. import com.dmdirc.events.ChannelQuitEvent;
  39. import com.dmdirc.events.ChannelTopicChangeEvent;
  40. import com.dmdirc.events.ChannelUserAwayEvent;
  41. import com.dmdirc.events.ChannelUserBackEvent;
  42. import com.dmdirc.events.ChannelUsermodechangeEvent;
  43. import com.dmdirc.events.DisplayableEvent;
  44. import com.dmdirc.events.EventUtils;
  45. import com.dmdirc.interfaces.Connection;
  46. import com.dmdirc.parser.common.AwayState;
  47. import com.dmdirc.parser.common.CallbackManager;
  48. import com.dmdirc.parser.interfaces.ChannelClientInfo;
  49. import com.dmdirc.parser.interfaces.ChannelInfo;
  50. import com.dmdirc.parser.interfaces.ClientInfo;
  51. import com.dmdirc.parser.interfaces.Parser;
  52. import com.dmdirc.parser.interfaces.callbacks.CallbackInterface;
  53. import com.dmdirc.parser.interfaces.callbacks.ChannelActionListener;
  54. import com.dmdirc.parser.interfaces.callbacks.ChannelCtcpListener;
  55. import com.dmdirc.parser.interfaces.callbacks.ChannelJoinListener;
  56. import com.dmdirc.parser.interfaces.callbacks.ChannelKickListener;
  57. import com.dmdirc.parser.interfaces.callbacks.ChannelListModeListener;
  58. import com.dmdirc.parser.interfaces.callbacks.ChannelMessageListener;
  59. import com.dmdirc.parser.interfaces.callbacks.ChannelModeChangeListener;
  60. import com.dmdirc.parser.interfaces.callbacks.ChannelModeNoticeListener;
  61. import com.dmdirc.parser.interfaces.callbacks.ChannelNamesListener;
  62. import com.dmdirc.parser.interfaces.callbacks.ChannelNickChangeListener;
  63. import com.dmdirc.parser.interfaces.callbacks.ChannelNonUserModeChangeListener;
  64. import com.dmdirc.parser.interfaces.callbacks.ChannelNoticeListener;
  65. import com.dmdirc.parser.interfaces.callbacks.ChannelPartListener;
  66. import com.dmdirc.parser.interfaces.callbacks.ChannelQuitListener;
  67. import com.dmdirc.parser.interfaces.callbacks.ChannelTopicListener;
  68. import com.dmdirc.parser.interfaces.callbacks.ChannelUserModeChangeListener;
  69. import com.dmdirc.parser.interfaces.callbacks.OtherAwayStateListener;
  70. import com.google.common.base.Optional;
  71. import com.google.common.base.Strings;
  72. import com.google.common.eventbus.EventBus;
  73. import java.util.Date;
  74. /**
  75. * Handles events for channel objects.
  76. */
  77. public class ChannelEventHandler extends EventHandler implements
  78. ChannelMessageListener, ChannelNamesListener, ChannelTopicListener,
  79. ChannelJoinListener, ChannelPartListener, ChannelKickListener,
  80. ChannelQuitListener, ChannelActionListener, ChannelNickChangeListener,
  81. ChannelModeChangeListener, ChannelUserModeChangeListener,
  82. ChannelCtcpListener, OtherAwayStateListener, ChannelNoticeListener,
  83. ChannelNonUserModeChangeListener, ChannelModeNoticeListener,
  84. ChannelListModeListener {
  85. /** The channel that owns this event handler. */
  86. private final Channel owner;
  87. /** Event bus to send events on. */
  88. private final EventBus eventBus;
  89. public ChannelEventHandler(final Channel owner, final EventBus eventBus) {
  90. this.owner = owner;
  91. this.eventBus = eventBus;
  92. }
  93. @SuppressWarnings("unchecked")
  94. @Override
  95. protected <T extends CallbackInterface> void addCallback(final CallbackManager cbm,
  96. final Class<T> type) {
  97. if (OtherAwayStateListener.class.equals(type)) {
  98. cbm.addCallback(type, (T) this);
  99. } else {
  100. cbm.addCallback(type, (T) this, owner.getChannelInfo().getName());
  101. }
  102. }
  103. @Override
  104. protected Connection getConnection() {
  105. return owner.getConnection();
  106. }
  107. /**
  108. * Determines if the specified client represents us.
  109. *
  110. * @param client The client to be tested
  111. *
  112. * @return True if the client is ourself, false otherwise.
  113. */
  114. protected boolean isMyself(final ChannelClientInfo client) {
  115. return client.getClient().equals(owner.getConnection().getParser().getLocalClient());
  116. }
  117. @Override
  118. public void onChannelMessage(final Parser parser, final Date date,
  119. final ChannelInfo channel, final ChannelClientInfo client,
  120. final String message, final String host) {
  121. checkParser(parser);
  122. final DisplayableEvent event = new ChannelMessageEvent(owner, client, message);
  123. final String format = EventUtils.postDisplayable(eventBus, event,
  124. isMyself(client) ? "channelSelfExternalMessage" : "channelMessage");
  125. owner.doNotification(date, format, client, message);
  126. }
  127. @Override
  128. public void onChannelGotNames(final Parser parser, final Date date, final ChannelInfo channel) {
  129. checkParser(parser);
  130. owner.setClients(channel.getChannelClients());
  131. eventBus.post(new ChannelGotnamesEvent(owner));
  132. }
  133. @Override
  134. public void onChannelTopic(final Parser parser, final Date date,
  135. final ChannelInfo channel, final boolean isJoinTopic) {
  136. checkParser(parser);
  137. if (isJoinTopic) {
  138. if (Strings.isNullOrEmpty(channel.getTopic())) {
  139. final DisplayableEvent event = new ChannelNotopicEvent(owner);
  140. final String format = EventUtils.postDisplayable(eventBus, event, "channelNoTopic");
  141. owner.doNotification(date, format);
  142. } else {
  143. final Topic newTopic = new Topic(channel.getTopic(), channel.getTopicSetter(),
  144. channel.getTopicTime());
  145. final DisplayableEvent event = new ChannelGottopicEvent(owner, newTopic);
  146. final String format = EventUtils.postDisplayable(eventBus, event,
  147. "channelTopicDiscovered");
  148. owner.doNotification(date, format, newTopic);
  149. }
  150. } else {
  151. final DisplayableEvent event = new ChannelTopicChangeEvent(owner,
  152. channel.getChannelClient(channel.getTopicSetter(), true),
  153. channel.getTopic());
  154. final String format = EventUtils.postDisplayable(eventBus, event,
  155. Strings.isNullOrEmpty(channel.getTopic())
  156. ? "channelTopicRemoved" : "channelTopicChanged");
  157. eventBus.post(event);
  158. owner.doNotification(date, format, channel.getChannelClient(channel.getTopicSetter(),
  159. true), channel.getTopic());
  160. }
  161. final Optional<Topic> currentTopic = owner.getCurrentTopic();
  162. final boolean hasNewTopic = !Strings.isNullOrEmpty(channel.getTopic());
  163. if (!isJoinTopic
  164. || (!currentTopic.isPresent() && hasNewTopic)
  165. || (currentTopic.isPresent() && !channel.getTopic().equals(
  166. owner.getCurrentTopic().get().getTopic()))) {
  167. // Only add the topic if:
  168. // - It's being set while we're in the channel (rather than discovered on join), or
  169. // - We think the current topic is empty and are discovering a new one, or
  170. // - The newly discovered topic is different to what we thought the current topic was.
  171. owner.addTopic(new Topic(channel.getTopic(), channel.getTopicSetter(),
  172. channel.getTopicTime()));
  173. }
  174. }
  175. @Override
  176. public void onChannelJoin(final Parser parser, final Date date, final ChannelInfo channel,
  177. final ChannelClientInfo client) {
  178. checkParser(parser);
  179. final DisplayableEvent event = new ChannelJoinEvent(owner, client);
  180. final String format = EventUtils.postDisplayable(eventBus, event, "channelJoin");
  181. owner.doNotification(date, format, client);
  182. owner.addClient(client);
  183. }
  184. @Override
  185. public void onChannelPart(final Parser parser, final Date date, final ChannelInfo channel,
  186. final ChannelClientInfo client, final String reason) {
  187. checkParser(parser);
  188. final DisplayableEvent event = new ChannelPartEvent(owner, client, reason);
  189. final String format = EventUtils.postDisplayable(eventBus, event,
  190. "channel"
  191. + (isMyself(client) ? "Self" : "") + "Part"
  192. + (reason.isEmpty() ? "" : "Reason"));
  193. owner.doNotification(date, format, client, reason);
  194. owner.removeClient(client);
  195. }
  196. @Override
  197. public void onChannelKick(final Parser parser, final Date date, final ChannelInfo channel,
  198. final ChannelClientInfo kickedClient, final ChannelClientInfo client,
  199. final String reason, final String host) {
  200. checkParser(parser);
  201. final DisplayableEvent event = new ChannelKickEvent(owner, client, kickedClient, reason);
  202. final String format = EventUtils.postDisplayable(eventBus, event,
  203. "channelKick" + (reason.isEmpty() ? "" : "Reason"));
  204. owner.doNotification(date, format, client, kickedClient, reason);
  205. owner.removeClient(kickedClient);
  206. }
  207. @Override
  208. public void onChannelQuit(final Parser parser, final Date date, final ChannelInfo channel,
  209. final ChannelClientInfo client, final String reason) {
  210. checkParser(parser);
  211. final DisplayableEvent event = new ChannelQuitEvent(owner, client, reason);
  212. final String format = EventUtils.postDisplayable(eventBus, event,
  213. "channelQuit" + (reason.isEmpty() ? "" : "Reason"));
  214. owner.doNotification(date, format, client, reason);
  215. owner.removeClient(client);
  216. }
  217. @Override
  218. public void onChannelAction(final Parser parser, final Date date, final ChannelInfo channel,
  219. final ChannelClientInfo client, final String message,
  220. final String host) {
  221. checkParser(parser);
  222. final DisplayableEvent event = new ChannelActionEvent(owner, client, message);
  223. final String format = EventUtils.postDisplayable(eventBus, event,
  224. isMyself(client) ? "channelSelfExternalAction" : "channelAction");
  225. owner.doNotification(date, format, client, message);
  226. }
  227. @Override
  228. public void onChannelNickChanged(final Parser parser, final Date date,
  229. final ChannelInfo channel, final ChannelClientInfo client, final String oldNick) {
  230. checkParser(parser);
  231. final DisplayableEvent event = new ChannelNickchangeEvent(owner, client, oldNick);
  232. final String format = EventUtils.postDisplayable(eventBus, event,
  233. isMyself(client) ? "channelSelfNickChange" : "channelNickChange");
  234. owner.doNotification(date, format, client, oldNick);
  235. owner.renameClient(oldNick, client.getClient().getNickname());
  236. }
  237. @Override
  238. public void onChannelModeChanged(final Parser parser, final Date date,
  239. final ChannelInfo channel, final ChannelClientInfo client, final String host,
  240. final String modes) {
  241. checkParser(parser);
  242. if (!owner.getConfigManager().getOptionBool("channel", "splitusermodes")
  243. || !owner.getConfigManager().getOptionBool("channel", "hideduplicatemodes")) {
  244. if (host.isEmpty()) {
  245. final DisplayableEvent event = new ChannelModesdiscoveredEvent(owner,
  246. modes.length() <= 1 ? "" : modes);
  247. final String format = EventUtils.postDisplayable(eventBus, event,
  248. modes.length() <= 1 ? "channelNoModes" : "channelModeDiscovered");
  249. owner.doNotification(date, format, modes.length() <= 1 ? "" : modes);
  250. } else {
  251. final DisplayableEvent event = new ChannelModechangeEvent(owner, client, modes);
  252. final String format = EventUtils.postDisplayable(eventBus, event,
  253. isMyself(client) ? "channelSelfModeChanged" : "channelModeChanged");
  254. owner.doNotification(date, format, client, modes);
  255. }
  256. }
  257. owner.refreshClients();
  258. }
  259. @Override
  260. public void onChannelUserModeChanged(final Parser parser, final Date date,
  261. final ChannelInfo channel, final ChannelClientInfo targetClient,
  262. final ChannelClientInfo client, final String host, final String mode) {
  263. checkParser(parser);
  264. if (owner.getConfigManager().getOptionBool("channel", "splitusermodes")) {
  265. String format = "channelSplitUserMode_" + mode;
  266. if (!owner.getConfigManager().hasOptionString("formatter", format)) {
  267. format = "channelSplitUserMode_default";
  268. }
  269. final DisplayableEvent event = new ChannelUsermodechangeEvent(owner, client,
  270. targetClient, mode);
  271. final String result = EventUtils.postDisplayable(eventBus, event, format);
  272. owner.doNotification(date, result, client, targetClient, mode);
  273. }
  274. }
  275. @Override
  276. public void onChannelCTCP(final Parser parser, final Date date,
  277. final ChannelInfo channel, final ChannelClientInfo client,
  278. final String type, final String message, final String host) {
  279. checkParser(parser);
  280. final ChannelCtcpEvent event = new ChannelCtcpEvent(owner, client, type, message);
  281. final String format = EventUtils.postDisplayable(eventBus, event, "channelCTCP");
  282. if (!event.isHandled()) {
  283. owner.getConnection().sendCTCPReply(client.getClient().getNickname(), type, message);
  284. }
  285. owner.doNotification(date, format, client, type, message);
  286. }
  287. @Override
  288. public void onAwayStateOther(final Parser parser, final Date date,
  289. final ClientInfo client, final AwayState oldState, final AwayState state) {
  290. checkParser(parser);
  291. final ChannelClientInfo channelClient = owner.getChannelInfo().getChannelClient(client);
  292. if (channelClient != null) {
  293. final boolean away = state == AwayState.AWAY;
  294. final boolean discovered = oldState == AwayState.UNKNOWN;
  295. final DisplayableEvent event = away
  296. ? new ChannelUserAwayEvent(date.getTime(), owner, channelClient)
  297. : new ChannelUserBackEvent(date.getTime(), owner, channelClient);
  298. final String format = EventUtils.postDisplayable(eventBus, event,
  299. (away ? "channelUserAway" : "channelUserBack")
  300. + (discovered ? "Discovered" : ""));
  301. owner.doNotification(date, format, channelClient);
  302. }
  303. }
  304. @Override
  305. public void onChannelNotice(final Parser parser, final Date date,
  306. final ChannelInfo channel, final ChannelClientInfo client,
  307. final String message, final String host) {
  308. checkParser(parser);
  309. final DisplayableEvent event = new ChannelNoticeEvent(owner, client, message);
  310. final String format = EventUtils.postDisplayable(eventBus, event, "channelNotice");
  311. owner.doNotification(date, format, client, message);
  312. }
  313. @Override
  314. public void onChannelNonUserModeChanged(final Parser parser, final Date date,
  315. final ChannelInfo channel, final ChannelClientInfo client,
  316. final String host, final String modes) {
  317. checkParser(parser);
  318. if (owner.getConfigManager().getOptionBool("channel", "splitusermodes")
  319. && owner.getConfigManager().getOptionBool("channel", "hideduplicatemodes")) {
  320. if (host.isEmpty()) {
  321. final DisplayableEvent event = new ChannelModesdiscoveredEvent(owner,
  322. modes.length() <= 1 ? "" : modes);
  323. final String format = EventUtils.postDisplayable(eventBus, event,
  324. modes.length() <= 1 ? "channelNoModes" : "channelModeDiscovered");
  325. owner.doNotification(date, format, modes.length() <= 1 ? "" : modes);
  326. } else {
  327. final DisplayableEvent event = new ChannelModechangeEvent(owner, client, modes);
  328. final String format = EventUtils.postDisplayable(eventBus, event,
  329. isMyself(client) ? "channelSelfModeChanged" : "channelModeChanged");
  330. owner.doNotification(date, format, client, modes);
  331. }
  332. }
  333. owner.refreshClients();
  334. }
  335. @Override
  336. public void onChannelModeNotice(final Parser parser, final Date date,
  337. final ChannelInfo channel, final char prefix,
  338. final ChannelClientInfo client, final String message,
  339. final String host) {
  340. checkParser(parser);
  341. final DisplayableEvent event = new ChannelModeNoticeEvent(owner, client,
  342. String.valueOf(prefix), message);
  343. final String format = EventUtils.postDisplayable(eventBus, event, "channelModeNotice");
  344. owner.doNotification(date, format, client, String.valueOf(prefix), message);
  345. }
  346. @Override
  347. public void onChannelGotListModes(final Parser parser, final Date date,
  348. final ChannelInfo channel, final char mode) {
  349. checkParser(parser);
  350. final DisplayableEvent event = new ChannelListmodesretrievedEvent(owner, mode);
  351. final String format = EventUtils.postDisplayable(eventBus, event,
  352. "channelListModeRetrieved");
  353. owner.doNotification(date, format, mode);
  354. }
  355. }