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.

ServerEventHandler.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * Copyright (c) 2006-2008 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.ChannelInfo;
  28. import com.dmdirc.parser.ClientInfo;
  29. import com.dmdirc.parser.IRCParser;
  30. import com.dmdirc.parser.ParserError;
  31. import com.dmdirc.parser.callbacks.CallbackManager;
  32. import com.dmdirc.parser.callbacks.CallbackNotFoundException;
  33. import com.dmdirc.parser.callbacks.interfaces.*;
  34. /**
  35. * Handles parser events for a Server object.
  36. *
  37. * @author chris
  38. */
  39. public final class ServerEventHandler extends EventHandler
  40. implements IChannelSelfJoin, IPrivateMessage, IPrivateAction,
  41. IErrorInfo, IPrivateCTCP, IPrivateCTCPReply, ISocketClosed,
  42. IPrivateNotice, IMOTDStart, IMOTDLine, IMOTDEnd, INumeric, IPingFailed,
  43. IPingSuccess, IAwayState, IConnectError, IAwayStateOther, INickInUse,
  44. IPost005, INoticeAuth, IUnknownNotice, IUserModeChanged, IInvite,
  45. IWallop, IWalluser, IWallDesync, INickChanged, IServerError, IPingSent {
  46. /** The server instance that owns this event handler. */
  47. private final Server owner;
  48. /**
  49. * Creates a new instance of ServerEventHandler.
  50. *
  51. * @param owner The Server instance that we're handling events for
  52. */
  53. public ServerEventHandler(final Server owner) {
  54. super();
  55. this.owner = owner;
  56. }
  57. /** {@inheritDoc} */
  58. @Override
  59. protected void addCallback(final CallbackManager cbm, final String name)
  60. throws CallbackNotFoundException {
  61. cbm.addCallback(name, this);
  62. }
  63. /** {@inheritDoc} */
  64. @Override
  65. protected IRCParser getParser() {
  66. return owner.getParser();
  67. }
  68. /** {@inheritDoc} */
  69. @Override
  70. public void onChannelSelfJoin(final IRCParser tParser, final ChannelInfo cChannel) {
  71. checkParser(tParser);
  72. owner.addChannel(cChannel);
  73. }
  74. /** {@inheritDoc} */
  75. @Override
  76. public void onPrivateMessage(final IRCParser tParser, final String sMessage,
  77. final String sHost) {
  78. checkParser(tParser);
  79. owner.addQuery(sHost);
  80. }
  81. /** {@inheritDoc} */
  82. @Override
  83. public void onPrivateAction(final IRCParser tParser, final String sMessage,
  84. final String sHost) {
  85. checkParser(tParser);
  86. owner.addQuery(sHost);
  87. }
  88. /** {@inheritDoc} */
  89. @Override
  90. public void onErrorInfo(final IRCParser tParser, final ParserError errorInfo) {
  91. checkParser(tParser);
  92. final ErrorLevel errorLevel = ErrorLevel.UNKNOWN;
  93. if (errorInfo.isException()) {
  94. Logger.appError(errorLevel, errorInfo.getData(), errorInfo.getException());
  95. } else {
  96. Logger.appError(errorLevel, errorInfo.getData(),
  97. new Exception("Parser exception.\n\n\tLast line:\t" //NOPMD
  98. + errorInfo.getLastLine() + "\n\tServer:\t" + owner.getName() + "\n"));
  99. }
  100. }
  101. /** {@inheritDoc} */
  102. @Override
  103. public void onPrivateCTCP(final IRCParser tParser, final String sType,
  104. final String sMessage, final String sHost) {
  105. checkParser(tParser);
  106. owner.doNotification("privateCTCP", CoreActionType.SERVER_CTCP,
  107. tParser.getClientInfoOrFake(sHost), sType, sMessage);
  108. owner.sendCTCPReply(ClientInfo.parseHost(sHost), sType, sMessage);
  109. }
  110. /** {@inheritDoc} */
  111. @Override
  112. public void onPrivateCTCPReply(final IRCParser tParser, final String sType,
  113. final String sMessage, final String sHost) {
  114. checkParser(tParser);
  115. owner.doNotification("privateCTCPreply", CoreActionType.SERVER_CTCPR,
  116. tParser.getClientInfoOrFake(sHost), sType, sMessage);
  117. }
  118. /** {@inheritDoc} */
  119. @Override
  120. public void onSocketClosed(final IRCParser tParser) {
  121. checkParser(tParser);
  122. owner.onSocketClosed();
  123. }
  124. /** {@inheritDoc} */
  125. @Override
  126. public void onPrivateNotice(final IRCParser tParser, final String sMessage,
  127. final String sHost) {
  128. checkParser(tParser);
  129. owner.doNotification("privateNotice", CoreActionType.SERVER_NOTICE,
  130. tParser.getClientInfoOrFake(sHost), sMessage);
  131. }
  132. /** {@inheritDoc} */
  133. @Override
  134. public void onMOTDStart(final IRCParser tParser, final String sData) {
  135. checkParser(tParser);
  136. owner.doNotification("motdStart", CoreActionType.SERVER_MOTDSTART, sData);
  137. }
  138. /** {@inheritDoc} */
  139. @Override
  140. public void onMOTDLine(final IRCParser tParser, final String sData) {
  141. checkParser(tParser);
  142. owner.doNotification("motdLine", CoreActionType.SERVER_MOTDLINE, sData);
  143. }
  144. /** {@inheritDoc} */
  145. @Override
  146. public void onMOTDEnd(final IRCParser tParser, final boolean noMOTD, final String sData) {
  147. checkParser(tParser);
  148. owner.doNotification("motdEnd", CoreActionType.SERVER_MOTDEND, sData);
  149. }
  150. /** {@inheritDoc} */
  151. @Override
  152. public void onNumeric(final IRCParser tParser, final int numeric,
  153. final String[] token) {
  154. checkParser(tParser);
  155. owner.onNumeric(numeric, token);
  156. }
  157. /** {@inheritDoc} */
  158. @Override
  159. public void onPingFailed(final IRCParser tParser) {
  160. checkParser(tParser);
  161. owner.onPingFailed();
  162. }
  163. /** {@inheritDoc} */
  164. @Override
  165. public void onPingSent(IRCParser tParser) {
  166. checkParser(tParser);
  167. ActionManager.processEvent(CoreActionType.SERVER_PINGSENT, null, owner);
  168. }
  169. /** {@inheritDoc} */
  170. @Override
  171. public void onPingSuccess(final IRCParser tParser) {
  172. checkParser(tParser);
  173. ActionManager.processEvent(CoreActionType.SERVER_GOTPING, null, owner,
  174. Long.valueOf(tParser.getServerLag()));
  175. }
  176. /** {@inheritDoc} */
  177. @Override
  178. public void onAwayState(final IRCParser tParser, final boolean currentState,
  179. final String reason) {
  180. checkParser(tParser);
  181. owner.updateAwayState(currentState ? reason : "");
  182. if (currentState) {
  183. owner.doNotification("away", CoreActionType.SERVER_AWAY, reason);
  184. } else {
  185. owner.doNotification("back", CoreActionType.SERVER_BACK);
  186. }
  187. }
  188. /** {@inheritDoc} */
  189. @Override
  190. public void onConnectError(final IRCParser tParser, final ParserError errorInfo) {
  191. checkParser(tParser);
  192. owner.onConnectError(errorInfo);
  193. }
  194. /** {@inheritDoc} */
  195. @Override
  196. public void onAwayStateOther(final IRCParser tParser, final ClientInfo client,
  197. final boolean state) {
  198. checkParser(tParser);
  199. // Just proxy it
  200. for (String chan : owner.getChannels()) {
  201. owner.getChannel(chan).onAwayStateOther(client, state);
  202. }
  203. }
  204. /** {@inheritDoc} */
  205. @Override
  206. public void onNickInUse(final IRCParser tParser, final String nickname) {
  207. owner.onNickInUse(nickname);
  208. checkParser(tParser);
  209. }
  210. /** {@inheritDoc} */
  211. @Override
  212. public void onPost005(final IRCParser tParser) {
  213. checkParser(tParser);
  214. owner.onPost005();
  215. }
  216. /** {@inheritDoc} */
  217. @Override
  218. public void onNoticeAuth(final IRCParser tParser, final String sData) {
  219. checkParser(tParser);
  220. owner.doNotification("authNotice", CoreActionType.SERVER_AUTHNOTICE, sData);
  221. }
  222. /** {@inheritDoc} */
  223. @Override
  224. public void onUnknownNotice(final IRCParser tParser, final String sMessage,
  225. final String sTarget, final String sHost) {
  226. checkParser(tParser);
  227. owner.doNotification("unknownNotice", CoreActionType.SERVER_UNKNOWNNOTICE,
  228. sHost, sTarget, sMessage);
  229. }
  230. /** {@inheritDoc} */
  231. @Override
  232. public void onUserModeChanged(final IRCParser tParser,
  233. final ClientInfo cClient, final String sSetBy, final String sModes) {
  234. checkParser(tParser);
  235. owner.doNotification("userModeChanged", CoreActionType.SERVER_USERMODES,
  236. tParser.getClientInfoOrFake(sSetBy), sModes);
  237. }
  238. /** {@inheritDoc} */
  239. @Override
  240. public void onInvite(final IRCParser tParser, final String userHost,
  241. final String channel) {
  242. checkParser(tParser);
  243. owner.addInvite(new Invite(owner, channel, userHost));
  244. owner.doNotification("inviteReceived",
  245. CoreActionType.SERVER_INVITERECEIVED,
  246. tParser.getClientInfoOrFake(userHost), channel);
  247. }
  248. /** {@inheritDoc} */
  249. @Override
  250. public void onWallop(final IRCParser tParser, final String sMessage,
  251. final String sHost) {
  252. checkParser(tParser);
  253. owner.doNotification("wallop", CoreActionType.SERVER_WALLOPS,
  254. tParser.getClientInfoOrFake(sHost), sMessage);
  255. }
  256. /** {@inheritDoc} */
  257. @Override
  258. public void onWalluser(final IRCParser tParser, final String sMessage,
  259. final String sHost) {
  260. checkParser(tParser);
  261. owner.doNotification("walluser", CoreActionType.SERVER_WALLUSERS,
  262. tParser.getClientInfoOrFake(sHost), sMessage);
  263. }
  264. /** {@inheritDoc} */
  265. @Override
  266. public void onWallDesync(final IRCParser tParser, final String sMessage,
  267. final String sHost) {
  268. checkParser(tParser);
  269. owner.doNotification("walldesync", CoreActionType.SERVER_WALLDESYNC,
  270. tParser.getClientInfoOrFake(sHost), sMessage);
  271. }
  272. /** {@inheritDoc} */
  273. @Override
  274. public void onNickChanged(final IRCParser tParser, final ClientInfo cClient,
  275. final String sOldNick) {
  276. checkParser(tParser);
  277. if (cClient.equals(tParser.getMyself())) {
  278. owner.doNotification("selfNickChange", CoreActionType.SERVER_NICKCHANGE,
  279. sOldNick, cClient.getNickname());
  280. }
  281. }
  282. /** {@inheritDoc} */
  283. @Override
  284. public void onServerError(final IRCParser tParser, final String sMessage) {
  285. checkParser(tParser);
  286. owner.doNotification("serverError", CoreActionType.SERVER_ERROR, sMessage);
  287. }
  288. }