Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ServerEventHandler.java 15KB

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