Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ServerEventHandler.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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.logger.ErrorLevel;
  26. import com.dmdirc.logger.Logger;
  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.ParserError;
  31. import com.dmdirc.parser.common.CallbackManager;
  32. import com.dmdirc.parser.common.CallbackNotFoundException;
  33. import com.dmdirc.parser.interfaces.callbacks.*;
  34. /**
  35. * Handles parser events for a Server object.
  36. *
  37. * @author chris
  38. */
  39. public final class ServerEventHandler extends EventHandler
  40. implements ChannelSelfJoinListener, PrivateMessageListener, PrivateActionListener,
  41. ErrorInfoListener, PrivateCtcpListener, PrivateCtcpReplyListener, SocketCloseListener,
  42. PrivateNoticeListener, MotdStartListener, MotdLineListener, MotdEndListener, NumericListener, PingFailureListener,
  43. PingSuccessListener, AwayStateListener, ConnectErrorListener, NickInUseListener, Post005Listener,
  44. AuthNoticeListener, UnknownNoticeListener, UserModeChangeListener, InviteListener, WallopListener,
  45. WalluserListener, WallDesyncListener, NickChangeListener, ServerErrorListener, PingSentListener,
  46. UserModeDiscoveryListener {
  47. /** The server instance that owns this event handler. */
  48. private final Server owner;
  49. /**
  50. * Creates a new instance of ServerEventHandler.
  51. *
  52. * @param owner The Server instance that we're handling events for
  53. */
  54. public ServerEventHandler(final Server owner) {
  55. super();
  56. this.owner = owner;
  57. }
  58. /** {@inheritDoc} */
  59. @Override
  60. @SuppressWarnings("unchecked")
  61. protected <T extends CallbackInterface> void addCallback(
  62. final CallbackManager cbm, final Class<T> type) throws CallbackNotFoundException {
  63. cbm.addCallback(type, (T) this);
  64. }
  65. /** {@inheritDoc} */
  66. @Override
  67. protected Server getServer() {
  68. return owner;
  69. }
  70. /** {@inheritDoc} */
  71. @Override
  72. public void onChannelSelfJoin(final Parser tParser, final ChannelInfo cChannel) {
  73. checkParser(tParser);
  74. owner.addChannel(cChannel);
  75. }
  76. /** {@inheritDoc} */
  77. @Override
  78. public void onPrivateMessage(final Parser tParser, final String sMessage,
  79. final String sHost) {
  80. checkParser(tParser);
  81. if (!owner.hasQuery(sHost)) {
  82. owner.addQuery(sHost);
  83. owner.getQuery(sHost).onPrivateMessage(tParser, sMessage, sHost);
  84. }
  85. }
  86. /** {@inheritDoc} */
  87. @Override
  88. public void onPrivateAction(final Parser tParser, final String sMessage,
  89. final String sHost) {
  90. checkParser(tParser);
  91. if (!owner.hasQuery(sHost)) {
  92. owner.addQuery(sHost);
  93. owner.getQuery(sHost).onPrivateAction(tParser, sMessage, sHost);
  94. }
  95. }
  96. /** {@inheritDoc} */
  97. @Override
  98. public void onErrorInfo(final Parser tParser, final ParserError errorInfo) {
  99. final ErrorLevel errorLevel = ErrorLevel.UNKNOWN;
  100. final Exception ex = (errorInfo.isException()) ? errorInfo.getException()
  101. : new Exception("Parser exception.\n\n\tLast line:\t" //NOPMD
  102. + errorInfo.getLastLine() + "\n\tServer:\t" + owner.getName() + "\n");
  103. if (errorInfo.isUserError()) {
  104. Logger.userError(errorLevel, errorInfo.getData(), ex);
  105. } else {
  106. Logger.appError(errorLevel, errorInfo.getData(), ex);
  107. }
  108. }
  109. /** {@inheritDoc} */
  110. @Override
  111. public void onPrivateCTCP(final Parser tParser, final String sType,
  112. final String sMessage, final String sHost) {
  113. checkParser(tParser);
  114. owner.doNotification("privateCTCP", CoreActionType.SERVER_CTCP,
  115. owner.getParser().getClient(sHost), sType, sMessage);
  116. owner.sendCTCPReply(tParser.parseHostmask(sHost)[0], sType, sMessage);
  117. }
  118. /** {@inheritDoc} */
  119. @Override
  120. public void onPrivateCTCPReply(final Parser tParser, final String sType,
  121. final String sMessage, final String sHost) {
  122. checkParser(tParser);
  123. owner.doNotification("privateCTCPreply", CoreActionType.SERVER_CTCPR,
  124. owner.getParser().getClient(sHost), sType, sMessage);
  125. }
  126. /** {@inheritDoc} */
  127. @Override
  128. public void onSocketClosed(final Parser tParser) {
  129. if (owner.getParser() == tParser) {
  130. owner.onSocketClosed();
  131. }
  132. }
  133. /** {@inheritDoc} */
  134. @Override
  135. public void onPrivateNotice(final Parser tParser, final String sMessage,
  136. final String sHost) {
  137. checkParser(tParser);
  138. owner.doNotification("privateNotice", CoreActionType.SERVER_NOTICE,
  139. owner.getParser().getClient(sHost), sMessage);
  140. }
  141. /** {@inheritDoc} */
  142. @Override
  143. public void onMOTDStart(final Parser tParser, final String sData) {
  144. checkParser(tParser);
  145. owner.doNotification("motdStart", CoreActionType.SERVER_MOTDSTART, sData);
  146. }
  147. /** {@inheritDoc} */
  148. @Override
  149. public void onMOTDLine(final Parser tParser, final String sData) {
  150. checkParser(tParser);
  151. owner.doNotification("motdLine", CoreActionType.SERVER_MOTDLINE, sData);
  152. }
  153. /** {@inheritDoc} */
  154. @Override
  155. public void onMOTDEnd(final Parser tParser, final boolean noMOTD, final String sData) {
  156. checkParser(tParser);
  157. owner.doNotification("motdEnd", CoreActionType.SERVER_MOTDEND, sData);
  158. }
  159. /** {@inheritDoc} */
  160. @Override
  161. public void onNumeric(final Parser tParser, final int numeric,
  162. final String[] token) {
  163. checkParser(tParser);
  164. owner.onNumeric(numeric, token);
  165. }
  166. /** {@inheritDoc} */
  167. @Override
  168. public void onPingFailed(final Parser tParser) {
  169. checkParser(tParser);
  170. owner.onPingFailed();
  171. }
  172. /** {@inheritDoc} */
  173. @Override
  174. public void onPingSent(final Parser tParser) {
  175. checkParser(tParser);
  176. ActionManager.processEvent(CoreActionType.SERVER_PINGSENT, null, owner);
  177. }
  178. /** {@inheritDoc} */
  179. @Override
  180. public void onPingSuccess(final Parser tParser) {
  181. checkParser(tParser);
  182. ActionManager.processEvent(CoreActionType.SERVER_GOTPING, null, owner,
  183. Long.valueOf(owner.getParser().getServerLatency()));
  184. }
  185. /** {@inheritDoc} */
  186. @Override
  187. public void onAwayState(final Parser tParser, final boolean currentState,
  188. final String reason) {
  189. checkParser(tParser);
  190. owner.updateAwayState(currentState ? reason : null);
  191. if (currentState) {
  192. owner.doNotification("away", CoreActionType.SERVER_AWAY, reason);
  193. } else {
  194. owner.doNotification("back", CoreActionType.SERVER_BACK);
  195. }
  196. }
  197. /** {@inheritDoc} */
  198. @Override
  199. public void onConnectError(final Parser tParser, final ParserError errorInfo) {
  200. checkParser(tParser);
  201. owner.onConnectError(errorInfo);
  202. }
  203. /** {@inheritDoc} */
  204. @Override
  205. public void onNickInUse(final Parser tParser, final String nickname) {
  206. owner.onNickInUse(nickname);
  207. checkParser(tParser);
  208. }
  209. /** {@inheritDoc} */
  210. @Override
  211. public void onPost005(final Parser tParser) {
  212. checkParser(tParser);
  213. owner.onPost005();
  214. }
  215. /** {@inheritDoc} */
  216. @Override
  217. public void onNoticeAuth(final Parser tParser, final String sData) {
  218. checkParser(tParser);
  219. owner.doNotification("authNotice", CoreActionType.SERVER_AUTHNOTICE, sData);
  220. }
  221. /** {@inheritDoc} */
  222. @Override
  223. public void onUnknownNotice(final Parser tParser, final String sMessage,
  224. final String sTarget, final String sHost) {
  225. checkParser(tParser);
  226. owner.doNotification("unknownNotice", CoreActionType.SERVER_UNKNOWNNOTICE,
  227. sHost, sTarget, sMessage);
  228. }
  229. /** {@inheritDoc} */
  230. @Override
  231. public void onUserModeChanged(final Parser tParser,
  232. final ClientInfo cClient, final String sSetBy, final String sModes) {
  233. checkParser(tParser);
  234. owner.doNotification("userModeChanged", CoreActionType.SERVER_USERMODES,
  235. owner.getParser().getClient(sSetBy), sModes);
  236. }
  237. /** {@inheritDoc} */
  238. @Override
  239. public void onUserModeDiscovered(final Parser tParser, final ClientInfo cClient,
  240. final String sModes) {
  241. checkParser(tParser);
  242. owner.doNotification("userModeDiscovered", CoreActionType.SERVER_USERMODES,
  243. cClient, sModes);
  244. }
  245. /** {@inheritDoc} */
  246. @Override
  247. public void onInvite(final Parser tParser, final String userHost,
  248. final String channel) {
  249. checkParser(tParser);
  250. owner.addInvite(new Invite(owner, channel, userHost));
  251. owner.doNotification("inviteReceived",
  252. CoreActionType.SERVER_INVITERECEIVED,
  253. owner.getParser().getClient(userHost), channel);
  254. }
  255. /** {@inheritDoc} */
  256. @Override
  257. public void onWallop(final Parser tParser, final String sMessage,
  258. final String sHost) {
  259. checkParser(tParser);
  260. owner.doNotification("wallop", CoreActionType.SERVER_WALLOPS,
  261. owner.getParser().getClient(sHost), sMessage);
  262. }
  263. /** {@inheritDoc} */
  264. @Override
  265. public void onWalluser(final Parser tParser, final String sMessage,
  266. final String sHost) {
  267. checkParser(tParser);
  268. owner.doNotification("walluser", CoreActionType.SERVER_WALLUSERS,
  269. owner.getParser().getClient(sHost), sMessage);
  270. }
  271. /** {@inheritDoc} */
  272. @Override
  273. public void onWallDesync(final Parser tParser, final String sMessage,
  274. final String sHost) {
  275. checkParser(tParser);
  276. owner.doNotification("walldesync", CoreActionType.SERVER_WALLDESYNC,
  277. owner.getParser().getClient(sHost), sMessage);
  278. }
  279. /** {@inheritDoc} */
  280. @Override
  281. public void onNickChanged(final Parser tParser, final ClientInfo cClient,
  282. final String sOldNick) {
  283. checkParser(tParser);
  284. if (cClient.equals(owner.getParser().getLocalClient())) {
  285. owner.doNotification("selfNickChange", CoreActionType.SERVER_NICKCHANGE,
  286. sOldNick, cClient.getNickname());
  287. owner.updateTitle();
  288. }
  289. }
  290. /** {@inheritDoc} */
  291. @Override
  292. public void onServerError(final Parser tParser, final String sMessage) {
  293. checkParser(tParser);
  294. owner.doNotification("serverError", CoreActionType.SERVER_ERROR, sMessage);
  295. }
  296. /** {@inheritDoc} */
  297. @Override
  298. protected void checkParser(final Parser parser) {
  299. super.checkParser(parser);
  300. if (owner.getState() != ServerState.CONNECTED
  301. && owner.getState() != ServerState.CONNECTING
  302. && owner.getState() != ServerState.DISCONNECTING) {
  303. throw new IllegalArgumentException("Event called from a parser (#"
  304. + owner.getStatus().getParserID(parser) + ") that " +
  305. "shouldn't be in use.\nState history:\n"
  306. + owner.getStatus().getTransitionHistory());
  307. }
  308. }
  309. }