123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350 |
- /*
- * Copyright (c) 2006-2008 Chris Smith, Shane Mc Cormack, Gregory Holmes
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- package com.dmdirc;
-
- import com.dmdirc.actions.ActionManager;
- import com.dmdirc.actions.CoreActionType;
- import com.dmdirc.logger.ErrorLevel;
- import com.dmdirc.logger.Logger;
- import com.dmdirc.parser.ChannelInfo;
- import com.dmdirc.parser.ClientInfo;
- import com.dmdirc.parser.IRCParser;
- import com.dmdirc.parser.ParserError;
- import com.dmdirc.parser.callbacks.CallbackManager;
- import com.dmdirc.parser.callbacks.CallbackNotFoundException;
- import com.dmdirc.parser.callbacks.interfaces.*;
-
- /**
- * Handles parser events for a Server object.
- *
- * @author chris
- */
- public final class ServerEventHandler extends EventHandler
- implements IChannelSelfJoin, IPrivateMessage, IPrivateAction,
- IErrorInfo, IPrivateCTCP, IPrivateCTCPReply, ISocketClosed,
- IPrivateNotice, IMOTDStart, IMOTDLine, IMOTDEnd, INumeric, IPingFailed,
- IPingSuccess, IAwayState, IConnectError, IAwayStateOther, INickInUse,
- IPost005, INoticeAuth, IUnknownNotice, IUserModeChanged, IInvite,
- IWallop, IWalluser, IWallDesync, INickChanged, IServerError, IPingSent {
-
- /** The server instance that owns this event handler. */
- private final Server owner;
-
- /**
- * Creates a new instance of ServerEventHandler.
- *
- * @param owner The Server instance that we're handling events for
- */
- public ServerEventHandler(final Server owner) {
- super();
-
- this.owner = owner;
- }
-
- /** {@inheritDoc} */
- @Override
- protected void addCallback(final CallbackManager cbm, final String name)
- throws CallbackNotFoundException {
- cbm.addCallback(name, this);
- }
-
- /** {@inheritDoc} */
- @Override
- protected IRCParser getParser() {
- return owner.getParser();
- }
-
- /** {@inheritDoc} */
- @Override
- public void onChannelSelfJoin(final IRCParser tParser, final ChannelInfo cChannel) {
- checkParser(tParser);
- owner.addChannel(cChannel);
- }
-
- /** {@inheritDoc} */
- @Override
- public void onPrivateMessage(final IRCParser tParser, final String sMessage,
- final String sHost) {
- checkParser(tParser);
- owner.addQuery(sHost);
- }
-
- /** {@inheritDoc} */
- @Override
- public void onPrivateAction(final IRCParser tParser, final String sMessage,
- final String sHost) {
- checkParser(tParser);
- owner.addQuery(sHost);
- }
-
- /** {@inheritDoc} */
- @Override
- public void onErrorInfo(final IRCParser tParser, final ParserError errorInfo) {
- checkParser(tParser);
-
- final ErrorLevel errorLevel = ErrorLevel.UNKNOWN;
-
- if (errorInfo.isException()) {
- Logger.appError(errorLevel, errorInfo.getData(), errorInfo.getException());
- } else {
- Logger.appError(errorLevel, errorInfo.getData(),
- new Exception("Parser exception.\n\n\tLast line:\t" //NOPMD
- + errorInfo.getLastLine() + "\n\tServer:\t" + owner.getName() + "\n"));
- }
- }
-
- /** {@inheritDoc} */
- @Override
- public void onPrivateCTCP(final IRCParser tParser, final String sType,
- final String sMessage, final String sHost) {
- checkParser(tParser);
-
- owner.doNotification("privateCTCP", CoreActionType.SERVER_CTCP,
- tParser.getClientInfoOrFake(sHost), sType, sMessage);
-
- owner.sendCTCPReply(ClientInfo.parseHost(sHost), sType, sMessage);
- }
-
- /** {@inheritDoc} */
- @Override
- public void onPrivateCTCPReply(final IRCParser tParser, final String sType,
- final String sMessage, final String sHost) {
- checkParser(tParser);
-
- owner.doNotification("privateCTCPreply", CoreActionType.SERVER_CTCPR,
- tParser.getClientInfoOrFake(sHost), sType, sMessage);
- }
-
- /** {@inheritDoc} */
- @Override
- public void onSocketClosed(final IRCParser tParser) {
- checkParser(tParser);
- owner.onSocketClosed();
- }
-
- /** {@inheritDoc} */
- @Override
- public void onPrivateNotice(final IRCParser tParser, final String sMessage,
- final String sHost) {
- checkParser(tParser);
-
- owner.doNotification("privateNotice", CoreActionType.SERVER_NOTICE,
- tParser.getClientInfoOrFake(sHost), sMessage);
- }
-
- /** {@inheritDoc} */
- @Override
- public void onMOTDStart(final IRCParser tParser, final String sData) {
- checkParser(tParser);
-
- owner.doNotification("motdStart", CoreActionType.SERVER_MOTDSTART, sData);
- }
-
- /** {@inheritDoc} */
- @Override
- public void onMOTDLine(final IRCParser tParser, final String sData) {
- checkParser(tParser);
-
- owner.doNotification("motdLine", CoreActionType.SERVER_MOTDLINE, sData);
- }
-
- /** {@inheritDoc} */
- @Override
- public void onMOTDEnd(final IRCParser tParser, final boolean noMOTD, final String sData) {
- checkParser(tParser);
-
- owner.doNotification("motdEnd", CoreActionType.SERVER_MOTDEND, sData);
- }
-
- /** {@inheritDoc} */
- @Override
- public void onNumeric(final IRCParser tParser, final int numeric,
- final String[] token) {
- checkParser(tParser);
- owner.onNumeric(numeric, token);
- }
-
- /** {@inheritDoc} */
- @Override
- public void onPingFailed(final IRCParser tParser) {
- checkParser(tParser);
- owner.onPingFailed();
- }
-
- /** {@inheritDoc} */
- @Override
- public void onPingSent(IRCParser tParser) {
- checkParser(tParser);
-
- ActionManager.processEvent(CoreActionType.SERVER_PINGSENT, null, owner);
- }
-
- /** {@inheritDoc} */
- @Override
- public void onPingSuccess(final IRCParser tParser) {
- checkParser(tParser);
-
- ActionManager.processEvent(CoreActionType.SERVER_GOTPING, null, owner,
- Long.valueOf(tParser.getServerLag()));
- }
-
- /** {@inheritDoc} */
- @Override
- public void onAwayState(final IRCParser tParser, final boolean currentState,
- final String reason) {
- checkParser(tParser);
-
- owner.updateAwayState(currentState ? reason : "");
-
- if (currentState) {
- owner.doNotification("away", CoreActionType.SERVER_AWAY, reason);
- } else {
- owner.doNotification("back", CoreActionType.SERVER_BACK);
- }
- }
-
- /** {@inheritDoc} */
- @Override
- public void onConnectError(final IRCParser tParser, final ParserError errorInfo) {
- checkParser(tParser);
- owner.onConnectError(errorInfo);
- }
-
- /** {@inheritDoc} */
- @Override
- public void onAwayStateOther(final IRCParser tParser, final ClientInfo client,
- final boolean state) {
- checkParser(tParser);
-
- // Just proxy it
- for (String chan : owner.getChannels()) {
- owner.getChannel(chan).onAwayStateOther(client, state);
- }
- }
-
- /** {@inheritDoc} */
- @Override
- public void onNickInUse(final IRCParser tParser, final String nickname) {
- owner.onNickInUse(nickname);
- checkParser(tParser);
- }
-
- /** {@inheritDoc} */
- @Override
- public void onPost005(final IRCParser tParser) {
- checkParser(tParser);
- owner.onPost005();
- }
-
- /** {@inheritDoc} */
- @Override
- public void onNoticeAuth(final IRCParser tParser, final String sData) {
- checkParser(tParser);
-
- owner.doNotification("authNotice", CoreActionType.SERVER_AUTHNOTICE, sData);
- }
-
- /** {@inheritDoc} */
- @Override
- public void onUnknownNotice(final IRCParser tParser, final String sMessage,
- final String sTarget, final String sHost) {
- checkParser(tParser);
-
- owner.doNotification("unknownNotice", CoreActionType.SERVER_UNKNOWNNOTICE,
- sHost, sTarget, sMessage);
- }
-
- /** {@inheritDoc} */
- @Override
- public void onUserModeChanged(final IRCParser tParser,
- final ClientInfo cClient, final String sSetBy, final String sModes) {
- checkParser(tParser);
-
- owner.doNotification("userModeChanged", CoreActionType.SERVER_USERMODES,
- tParser.getClientInfoOrFake(sSetBy), sModes);
- }
-
- /** {@inheritDoc} */
- @Override
- public void onInvite(final IRCParser tParser, final String userHost,
- final String channel) {
- checkParser(tParser);
-
- owner.addInvite(new Invite(owner, channel, userHost));
- owner.doNotification("inviteReceived",
- CoreActionType.SERVER_INVITERECEIVED,
- tParser.getClientInfoOrFake(userHost), channel);
- }
-
- /** {@inheritDoc} */
- @Override
- public void onWallop(final IRCParser tParser, final String sMessage,
- final String sHost) {
- checkParser(tParser);
-
- owner.doNotification("wallop", CoreActionType.SERVER_WALLOPS,
- tParser.getClientInfoOrFake(sHost), sMessage);
-
- }
-
- /** {@inheritDoc} */
- @Override
- public void onWalluser(final IRCParser tParser, final String sMessage,
- final String sHost) {
- checkParser(tParser);
-
- owner.doNotification("walluser", CoreActionType.SERVER_WALLUSERS,
- tParser.getClientInfoOrFake(sHost), sMessage);
- }
-
- /** {@inheritDoc} */
- @Override
- public void onWallDesync(final IRCParser tParser, final String sMessage,
- final String sHost) {
- checkParser(tParser);
-
- owner.doNotification("walldesync", CoreActionType.SERVER_WALLDESYNC,
- tParser.getClientInfoOrFake(sHost), sMessage);
- }
-
- /** {@inheritDoc} */
- @Override
- public void onNickChanged(final IRCParser tParser, final ClientInfo cClient,
- final String sOldNick) {
- checkParser(tParser);
-
- if (cClient.equals(tParser.getMyself())) {
- owner.doNotification("selfNickChange", CoreActionType.SERVER_NICKCHANGE,
- sOldNick, cClient.getNickname());
- }
- }
-
- /** {@inheritDoc} */
- @Override
- public void onServerError(final IRCParser tParser, final String sMessage) {
- checkParser(tParser);
-
- owner.doNotification("serverError", CoreActionType.SERVER_ERROR, sMessage);
- }
-
- }
|