Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ChannelEventHandler.java 13KB

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