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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Copyright (c) 2006-2007 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.CallbackNotFoundException;
  32. import com.dmdirc.parser.callbacks.interfaces.*;
  33. /**
  34. * Handles parser events for a Server object.
  35. *
  36. * @author chris
  37. */
  38. public final class ServerEventHandler implements IChannelSelfJoin, IPrivateMessage,
  39. IPrivateAction, IErrorInfo, IPrivateCTCP, IPrivateCTCPReply,
  40. ISocketClosed, IPrivateNotice, IMOTDStart, IMOTDLine, IMOTDEnd,
  41. INumeric, IGotNetwork, IPingFailed, IPingSuccess, IAwayState,
  42. IConnectError, IAwayStateOther, INickInUse, IPost005, INoticeAuth,
  43. IUnknownNotice, IUserModeChanged {
  44. private static final String CALLBACK_PREFIX = "com.dmdirc.parser.callbacks.interfaces.I";
  45. private final Server owner;
  46. /**
  47. * Creates a new instance of ServerEventHandler.
  48. *
  49. * @param owner The Server instance that we're handling events for
  50. */
  51. public ServerEventHandler(final Server owner) {
  52. super();
  53. this.owner = owner;
  54. }
  55. /**
  56. * Registers all callbacks that this event handler implements with the
  57. * owner's parser.
  58. */
  59. public void registerCallbacks() {
  60. try {
  61. for (Class iface : this.getClass().getInterfaces()) {
  62. if (iface.getName().startsWith(CALLBACK_PREFIX)) {
  63. owner.getParser().getCallbackManager().addCallback(
  64. "on" + iface.getName().substring(CALLBACK_PREFIX.length()), this);
  65. }
  66. }
  67. } catch (CallbackNotFoundException exception) {
  68. Logger.appError(ErrorLevel.FATAL, "Unable to register callbacks",
  69. exception);
  70. }
  71. }
  72. /**
  73. * Checks that the specified parser is the same as the one the server is
  74. * currently claiming to be using. If it isn't, we raise an exception to
  75. * prevent further (erroneous) processing.
  76. */
  77. private void checkParser(final IRCParser parser) {
  78. if (parser != owner.getParser()) {
  79. throw new IllegalArgumentException("Event called from a parser that's not in use."
  80. + "\nActual parser: " + owner.getParser().hashCode()
  81. + "\nPassed parser: " + parser.hashCode());
  82. }
  83. }
  84. /** {@inheritDoc} */
  85. public void onChannelSelfJoin(final IRCParser tParser, final ChannelInfo cChannel) {
  86. checkParser(tParser);
  87. owner.addChannel(cChannel);
  88. }
  89. /** {@inheritDoc} */
  90. public void onPrivateMessage(final IRCParser tParser, final String sMessage,
  91. final String sHost) {
  92. checkParser(tParser);
  93. owner.addQuery(sHost);
  94. }
  95. /** {@inheritDoc} */
  96. public void onPrivateAction(final IRCParser tParser, final String sMessage,
  97. final String sHost) {
  98. checkParser(tParser);
  99. owner.addQuery(sHost);
  100. }
  101. /** {@inheritDoc} */
  102. public void onErrorInfo(final IRCParser tParser, final ParserError errorInfo) {
  103. checkParser(tParser);
  104. owner.onErrorInfo(errorInfo);
  105. }
  106. /** {@inheritDoc} */
  107. public void onPrivateCTCP(final IRCParser tParser, final String sType,
  108. final String sMessage, final String sHost) {
  109. checkParser(tParser);
  110. owner.onPrivateCTCP(sType, sMessage, sHost);
  111. }
  112. /** {@inheritDoc} */
  113. public void onPrivateCTCPReply(final IRCParser tParser, final String sType,
  114. final String sMessage, final String sHost) {
  115. checkParser(tParser);
  116. owner.onPrivateCTCPReply(sType, sMessage, sHost);
  117. }
  118. /** {@inheritDoc} */
  119. public void onSocketClosed(final IRCParser tParser) {
  120. checkParser(tParser);
  121. owner.onSocketClosed();
  122. }
  123. /** {@inheritDoc} */
  124. public void onPrivateNotice(final IRCParser tParser, final String sMessage,
  125. final String sHost) {
  126. checkParser(tParser);
  127. owner.onPrivateNotice(sMessage, sHost);
  128. }
  129. /** {@inheritDoc} */
  130. public void onMOTDStart(final IRCParser tParser, final String sData) {
  131. checkParser(tParser);
  132. owner.onMOTDStart(sData);
  133. }
  134. /** {@inheritDoc} */
  135. public void onMOTDLine(final IRCParser tParser, final String sData) {
  136. checkParser(tParser);
  137. owner.onMOTDLine(sData);
  138. }
  139. /** {@inheritDoc} */
  140. public void onMOTDEnd(final IRCParser tParser, final boolean noMOTD) {
  141. checkParser(tParser);
  142. owner.onMOTDEnd(noMOTD);
  143. }
  144. /** {@inheritDoc} */
  145. public void onNumeric(final IRCParser tParser, final int numeric,
  146. final String[] token) {
  147. checkParser(tParser);
  148. owner.onNumeric(numeric, token);
  149. }
  150. /** {@inheritDoc} */
  151. public void onGotNetwork(final IRCParser tParser, final String networkName,
  152. final String ircdVersion, final String ircdType) {
  153. checkParser(tParser);
  154. owner.onGotNetwork(networkName, ircdVersion, ircdType);
  155. }
  156. /** {@inheritDoc} */
  157. public void onPingFailed(final IRCParser tParser) {
  158. checkParser(tParser);
  159. owner.onPingFailed();
  160. }
  161. /** {@inheritDoc} */
  162. public void onPingSuccess(final IRCParser tParser) {
  163. checkParser(tParser);
  164. ActionManager.processEvent(CoreActionType.SERVER_GOTPING, null, owner,
  165. Long.valueOf(tParser.getServerLag()));
  166. }
  167. /** {@inheritDoc} */
  168. public void onAwayState(final IRCParser tParser, final boolean currentState,
  169. final String reason) {
  170. checkParser(tParser);
  171. owner.onAwayState(currentState, reason);
  172. }
  173. /** {@inheritDoc} */
  174. public void onConnectError(final IRCParser tParser, final ParserError errorInfo) {
  175. checkParser(tParser);
  176. owner.onConnectError(errorInfo);
  177. }
  178. /** {@inheritDoc} */
  179. public void onAwayStateOther(final IRCParser tParser, final ClientInfo client,
  180. final boolean state) {
  181. checkParser(tParser);
  182. owner.onAwayStateOther(client, state);
  183. }
  184. /** {@inheritDoc} */
  185. public void onNickInUse(final IRCParser tParser, final String nickname) {
  186. owner.onNickInUse(nickname);
  187. checkParser(tParser);
  188. }
  189. /** {@inheritDoc} */
  190. public void onPost005(final IRCParser tParser) {
  191. checkParser(tParser);
  192. owner.onPost005();
  193. }
  194. /** {@inheritDoc} */
  195. public void onNoticeAuth(final IRCParser tParser, final String sData) {
  196. checkParser(tParser);
  197. owner.onNoticeAuth(sData);
  198. }
  199. /** {@inheritDoc} */
  200. public void onUnknownNotice(final IRCParser tParser, final String sMessage,
  201. final String sTarget, final String sHost) {
  202. checkParser(tParser);
  203. owner.onUnknownNotice(sMessage, sTarget, sHost);
  204. }
  205. /** {@inheritDoc} */
  206. public void onUserModeChanged(final IRCParser tParser,
  207. final ClientInfo cClient, final String sSetBy, final String sModes) {
  208. checkParser(tParser);
  209. owner.onUserModeChanged(cClient, sSetBy, sModes);
  210. }
  211. }