File Source: ServerEventHandler.java
/*
P/P * Method: com.dmdirc.ServerEventHandler__static_init
*/
1 /*
2 * Copyright (c) 2006-2009 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
23 package com.dmdirc;
24
25 import com.dmdirc.actions.ActionManager;
26 import com.dmdirc.actions.CoreActionType;
27 import com.dmdirc.logger.ErrorLevel;
28 import com.dmdirc.logger.Logger;
29 import com.dmdirc.parser.irc.ChannelInfo;
30 import com.dmdirc.parser.irc.ClientInfo;
31 import com.dmdirc.parser.irc.IRCParser;
32 import com.dmdirc.parser.irc.ParserError;
33 import com.dmdirc.parser.irc.callbacks.CallbackManager;
34 import com.dmdirc.parser.irc.callbacks.CallbackNotFoundException;
35 import com.dmdirc.parser.irc.callbacks.interfaces.*;
36
37 /**
38 * Handles parser events for a Server object.
39 *
40 * @author chris
41 */
42 public final class ServerEventHandler extends EventHandler
43 implements IChannelSelfJoin, IPrivateMessage, IPrivateAction,
44 IErrorInfo, IPrivateCTCP, IPrivateCTCPReply, ISocketClosed,
45 IPrivateNotice, IMOTDStart, IMOTDLine, IMOTDEnd, INumeric, IPingFailed,
46 IPingSuccess, IAwayState, IConnectError, INickInUse, IPost005,
47 INoticeAuth, IUnknownNotice, IUserModeChanged, IInvite, IWallop,
48 IWalluser, IWallDesync, INickChanged, IServerError, IPingSent,
49 IUserModeDiscovered {
50
51 /** The server instance that owns this event handler. */
52 private final Server owner;
53
54 /**
55 * Creates a new instance of ServerEventHandler.
56 *
57 * @param owner The Server instance that we're handling events for
58 */
59 public ServerEventHandler(final Server owner) {
/*
P/P * Method: void com.dmdirc.ServerEventHandler(Server)
*
* Postconditions:
* this.owner == owner
* init'ed(this.owner)
*/
60 super();
61
62 this.owner = owner;
63 }
64
65 /** {@inheritDoc} */
66 @Override
67 protected void addCallback(final CallbackManager cbm, final String name)
68 throws CallbackNotFoundException {
/*
P/P * Method: void addCallback(CallbackManager, String)
*
* Preconditions:
* cbm != null
*/
69 cbm.addCallback(name, this);
70 }
71
72 /** {@inheritDoc} */
73 @Override
74 protected Server getServer() {
/*
P/P * Method: Server getServer()
*
* Postconditions:
* return_value == this.owner
* init'ed(return_value)
*/
75 return owner;
76 }
77
78 /** {@inheritDoc} */
79 @Override
80 public void onChannelSelfJoin(final IRCParser tParser, final ChannelInfo cChannel) {
/*
P/P * Method: void onChannelSelfJoin(IRCParser, ChannelInfo)
*
* Preconditions:
* this.owner != null
* this.owner.myState != null
* this.owner.myState.state != null
* (soft) cChannel != null
* (soft) this.owner.channels != null
* (soft) this.owner.converter != null
* (soft) this.owner.server != null
* (soft) this.owner.tabCompleter != null
*/
81 checkParser(tParser);
82 owner.addChannel(cChannel);
83 }
84
85 /** {@inheritDoc} */
86 @Override
87 public void onPrivateMessage(final IRCParser tParser, final String sMessage,
88 final String sHost) {
/*
P/P * Method: void onPrivateMessage(IRCParser, String, String)
*
* Preconditions:
* this.owner != null
* this.owner.queries != null
* (soft) this.owner.converter != null
* (soft) this.owner.myState != null
* (soft) this.owner.myState.state != null
* (soft) this.owner.server != null
* (soft) this.owner.tabCompleter != null
*/
89 checkParser(tParser);
90
91 if (!owner.hasQuery(sHost)) {
92 owner.addQuery(sHost);
93 owner.getQuery(sHost).onPrivateMessage(tParser, sMessage, sHost);
94 }
95 }
96
97 /** {@inheritDoc} */
98 @Override
99 public void onPrivateAction(final IRCParser tParser, final String sMessage,
100 final String sHost) {
/*
P/P * Method: void onPrivateAction(IRCParser, String, String)
*
* Preconditions:
* this.owner != null
* this.owner.queries != null
* (soft) this.owner.converter != null
* (soft) this.owner.myState != null
* (soft) this.owner.myState.state != null
* (soft) this.owner.server != null
* (soft) this.owner.tabCompleter != null
*/
101 checkParser(tParser);
102
103 if (!owner.hasQuery(sHost)) {
104 owner.addQuery(sHost);
105 owner.getQuery(sHost).onPrivateAction(tParser, sMessage, sHost);
106 }
107 }
108
109 /** {@inheritDoc} */
110 @Override
111 public void onErrorInfo(final IRCParser tParser, final ParserError errorInfo) {
/*
P/P * Method: void onErrorInfo(IRCParser, ParserError)
*
* Preconditions:
* errorInfo != null
* (soft) this.owner != null
* (soft) this.owner.serverInfo != null
*
* Presumptions:
* init'ed(com.dmdirc.logger.ErrorLevel.UNKNOWN)
*
* Test Vectors:
* com.dmdirc.parser.irc.ParserError:isException(...)@114: {0}, {1}
* com.dmdirc.parser.irc.ParserError:isUserError(...)@118: {0}, {1}
*/
112 final ErrorLevel errorLevel = ErrorLevel.UNKNOWN;
113
114 final Exception ex = (errorInfo.isException()) ? errorInfo.getException()
115 : new Exception("Parser exception.\n\n\tLast line:\t" //NOPMD
116 + errorInfo.getLastLine() + "\n\tServer:\t" + owner.getName() + "\n");
117
118 if (errorInfo.isUserError()) {
119 Logger.userError(errorLevel, errorInfo.getData(), ex);
120 } else {
121 Logger.appError(errorLevel, errorInfo.getData(), ex);
122 }
123 }
124
125 /** {@inheritDoc} */
126 @Override
127 public void onPrivateCTCP(final IRCParser tParser, final String sType,
128 final String sMessage, final String sHost) {
/*
P/P * Method: void onPrivateCTCP(IRCParser, String, String, String)
*
* Preconditions:
* sType != null
* tParser != null
* this.owner != null
* this.owner.myState != null
* this.owner.myState.state != null
* (soft) this.owner.config != null
* (soft) this.owner.parser != null
* (soft) this.owner.server != null
*/
129 checkParser(tParser);
130
131 owner.doNotification("privateCTCP", CoreActionType.SERVER_CTCP,
132 tParser.getClientInfoOrFake(sHost), sType, sMessage);
133
134 owner.sendCTCPReply(ClientInfo.parseHost(sHost), sType, sMessage);
135 }
136
137 /** {@inheritDoc} */
138 @Override
139 public void onPrivateCTCPReply(final IRCParser tParser, final String sType,
140 final String sMessage, final String sHost) {
/*
P/P * Method: void onPrivateCTCPReply(IRCParser, String, String, String)
*
* Preconditions:
* tParser != null
* this.owner != null
* this.owner.myState != null
* this.owner.myState.state != null
* (soft) this.owner.server != null
*/
141 checkParser(tParser);
142
143 owner.doNotification("privateCTCPreply", CoreActionType.SERVER_CTCPR,
144 tParser.getClientInfoOrFake(sHost), sType, sMessage);
145 }
146
147 /** {@inheritDoc} */
148 @Override
149 public void onSocketClosed(final IRCParser tParser) {
/*
P/P * Method: void onSocketClosed(IRCParser)
*
* Preconditions:
* this.owner != null
* init'ed(this.owner.parser)
*
* Test Vectors:
* tParser == this.owner.parser: {0}, {1}
*/
150 if (owner.getParser() == tParser) {
151 owner.onSocketClosed();
152 }
153 }
154
155 /** {@inheritDoc} */
156 @Override
157 public void onPrivateNotice(final IRCParser tParser, final String sMessage,
158 final String sHost) {
/*
P/P * Method: void onPrivateNotice(IRCParser, String, String)
*
* Preconditions:
* tParser != null
* this.owner != null
* this.owner.myState != null
* this.owner.myState.state != null
* (soft) this.owner.server != null
*/
159 checkParser(tParser);
160
161 owner.doNotification("privateNotice", CoreActionType.SERVER_NOTICE,
162 tParser.getClientInfoOrFake(sHost), sMessage);
163 }
164
165 /** {@inheritDoc} */
166 @Override
167 public void onMOTDStart(final IRCParser tParser, final String sData) {
/*
P/P * Method: void onMOTDStart(IRCParser, String)
*
* Preconditions:
* this.owner != null
* this.owner.myState != null
* this.owner.myState.state != null
* (soft) this.owner.server != null
*/
168 checkParser(tParser);
169
170 owner.doNotification("motdStart", CoreActionType.SERVER_MOTDSTART, sData);
171 }
172
173 /** {@inheritDoc} */
174 @Override
175 public void onMOTDLine(final IRCParser tParser, final String sData) {
/*
P/P * Method: void onMOTDLine(IRCParser, String)
*
* Preconditions:
* this.owner != null
* this.owner.myState != null
* this.owner.myState.state != null
* (soft) this.owner.server != null
*/
176 checkParser(tParser);
177
178 owner.doNotification("motdLine", CoreActionType.SERVER_MOTDLINE, sData);
179 }
180
181 /** {@inheritDoc} */
182 @Override
183 public void onMOTDEnd(final IRCParser tParser, final boolean noMOTD, final String sData) {
/*
P/P * Method: void onMOTDEnd(IRCParser, bool, String)
*
* Preconditions:
* this.owner != null
* this.owner.myState != null
* this.owner.myState.state != null
* (soft) this.owner.server != null
*/
184 checkParser(tParser);
185
186 owner.doNotification("motdEnd", CoreActionType.SERVER_MOTDEND, sData);
187 }
188
189 /** {@inheritDoc} */
190 @Override
191 public void onNumeric(final IRCParser tParser, final int numeric,
192 final String[] token) {
/*
P/P * Method: void onNumeric(IRCParser, int, String[])
*
* Preconditions:
* this.owner != null
* this.owner.myState != null
* this.owner.myState.state != null
* (soft) this.owner.server != null
*/
193 checkParser(tParser);
194 owner.onNumeric(numeric, token);
195 }
196
197 /** {@inheritDoc} */
198 @Override
199 public void onPingFailed(final IRCParser tParser) {
/*
P/P * Method: void onPingFailed(IRCParser)
*
* Preconditions:
* this.owner != null
* this.owner.myState != null
* this.owner.myState.state != null
* (soft) this.owner.server != null
*/
200 checkParser(tParser);
201 owner.onPingFailed();
202 }
203
204 /** {@inheritDoc} */
205 @Override
206 public void onPingSent(final IRCParser tParser) {
/*
P/P * Method: void onPingSent(IRCParser)
*
* Preconditions:
* init'ed(com/dmdirc/actions/ActionManager.killSwitch)
* this.owner != null
* this.owner.myState != null
* this.owner.myState.state != null
* (soft) com.dmdirc.actions.CoreActionType__static_init.new CoreActionType(CoreActionType__static_init#34).type != null
* (soft) init'ed(com/dmdirc/ServerManager.me)
* (soft) this.owner.server != null
*
* Postconditions:
* com/dmdirc/ServerManager.me == old com/dmdirc/ServerManager.me
* new ArrayList(ServerManager#1) num objects == undefined
* new ArrayList(ServerManager#1) num objects == 0, if init'ed
* new ServerManager(getServerManager#1) num objects == new ArrayList(ServerManager#1) num objects
* new ServerManager(getServerManager#1).servers == undefined
* new ServerManager(getServerManager#1).servers == null
*/
207 checkParser(tParser);
208
209 ActionManager.processEvent(CoreActionType.SERVER_PINGSENT, null, owner);
210 }
211
212 /** {@inheritDoc} */
213 @Override
214 public void onPingSuccess(final IRCParser tParser) {
/*
P/P * Method: void onPingSuccess(IRCParser)
*
* Preconditions:
* init'ed(com/dmdirc/actions/ActionManager.killSwitch)
* tParser != null
* this.owner != null
* this.owner.myState != null
* this.owner.myState.state != null
* (soft) com.dmdirc.actions.CoreActionType__static_init.new CoreActionType(CoreActionType__static_init#32).type != null
* (soft) init'ed(com/dmdirc/ServerManager.me)
* (soft) this.owner.server != null
*
* Postconditions:
* com/dmdirc/ServerManager.me == old com/dmdirc/ServerManager.me
* new ArrayList(ServerManager#1) num objects == undefined
* new ArrayList(ServerManager#1) num objects == 0, if init'ed
* new ServerManager(getServerManager#1) num objects == new ArrayList(ServerManager#1) num objects
* new ServerManager(getServerManager#1).servers == undefined
* new ServerManager(getServerManager#1).servers == null
*/
215 checkParser(tParser);
216
217 ActionManager.processEvent(CoreActionType.SERVER_GOTPING, null, owner,
218 Long.valueOf(tParser.getServerLag()));
219 }
220
221 /** {@inheritDoc} */
222 @Override
223 public void onAwayState(final IRCParser tParser, final boolean currentState,
224 final String reason) {
/*
P/P * Method: void onAwayState(IRCParser, bool, String)
*
* Preconditions:
* init'ed(this.owner.awayMessage)
* this.owner != null
* this.owner.myState != null
* this.owner.myState.state != null
* (soft) this.owner.listeners != null
* (soft) this.owner.server != null
*
* Postconditions:
* this.owner.awayMessage == One-of{old this.owner.awayMessage, reason, null}
* init'ed(this.owner.awayMessage)
*
* Test Vectors:
* currentState: {0}, {1}
*/
225 checkParser(tParser);
226
227 owner.updateAwayState(currentState ? reason : null);
228
229 if (currentState) {
230 owner.doNotification("away", CoreActionType.SERVER_AWAY, reason);
231 } else {
232 owner.doNotification("back", CoreActionType.SERVER_BACK);
233 }
234 }
235
236 /** {@inheritDoc} */
237 @Override
238 public void onConnectError(final IRCParser tParser, final ParserError errorInfo) {
/*
P/P * Method: void onConnectError(IRCParser, ParserError)
*
* Preconditions:
* this.owner.myState.state != null
* this.owner != null
* this.owner.myState != null
* (soft) this.owner.server != null
*
* Postconditions:
* this.owner.myState.state == old this.owner.myState.state
* this.owner.myState.state != null
* this.owner.parser == old this.owner.parser
* this.owner.reconnectTimer == old this.owner.reconnectTimer
* new Timer(doDelayedReconnect#4) num objects == undefined
* new Timer(doDelayedReconnect#4) num objects == 0, if init'ed
*/
239 checkParser(tParser);
240 owner.onConnectError(errorInfo);
241 }
242
243 /** {@inheritDoc} */
244 @Override
245 public void onNickInUse(final IRCParser tParser, final String nickname) {
/*
P/P * Method: void onNickInUse(IRCParser, String)
*
* Preconditions:
* this.owner != null
* this.owner.converter != null
* this.owner.myState != null
* this.owner.myState.state != null
* this.owner.parser != null
* (soft) this.owner.profile != null
* (soft) this.owner.server != null
*/
246 owner.onNickInUse(nickname);
247 checkParser(tParser);
248 }
249
250 /** {@inheritDoc} */
251 @Override
252 public void onPost005(final IRCParser tParser) {
/*
P/P * Method: void onPost005(IRCParser)
*
* Preconditions:
* this.owner.myState.state != null
* this.owner != null
* this.owner.myState != null
* (soft) this.owner.server != null
*
* Postconditions:
* this.owner.converter == old this.owner.converter
* this.owner.myState.state == old this.owner.myState.state
* this.owner.myState.state != null
*/
253 checkParser(tParser);
254 owner.onPost005();
255 }
256
257 /** {@inheritDoc} */
258 @Override
259 public void onNoticeAuth(final IRCParser tParser, final String sData) {
/*
P/P * Method: void onNoticeAuth(IRCParser, String)
*
* Preconditions:
* this.owner != null
* this.owner.myState != null
* this.owner.myState.state != null
* (soft) this.owner.server != null
*/
260 checkParser(tParser);
261
262 owner.doNotification("authNotice", CoreActionType.SERVER_AUTHNOTICE, sData);
263 }
264
265 /** {@inheritDoc} */
266 @Override
267 public void onUnknownNotice(final IRCParser tParser, final String sMessage,
268 final String sTarget, final String sHost) {
/*
P/P * Method: void onUnknownNotice(IRCParser, String, String, String)
*
* Preconditions:
* this.owner != null
* this.owner.myState != null
* this.owner.myState.state != null
* (soft) this.owner.server != null
*/
269 checkParser(tParser);
270
271 owner.doNotification("unknownNotice", CoreActionType.SERVER_UNKNOWNNOTICE,
272 sHost, sTarget, sMessage);
273 }
274
275 /** {@inheritDoc} */
276 @Override
277 public void onUserModeChanged(final IRCParser tParser,
278 final ClientInfo cClient, final String sSetBy, final String sModes) {
/*
P/P * Method: void onUserModeChanged(IRCParser, ClientInfo, String, String)
*
* Preconditions:
* tParser != null
* this.owner != null
* this.owner.myState != null
* this.owner.myState.state != null
* (soft) this.owner.server != null
*/
279 checkParser(tParser);
280
281 owner.doNotification("userModeChanged", CoreActionType.SERVER_USERMODES,
282 tParser.getClientInfoOrFake(sSetBy), sModes);
283 }
284
285 /** {@inheritDoc} */
286 @Override
287 public void onUserModeDiscovered(final IRCParser tParser, final ClientInfo cClient,
288 final String sModes) {
/*
P/P * Method: void onUserModeDiscovered(IRCParser, ClientInfo, String)
*
* Preconditions:
* this.owner != null
* this.owner.myState != null
* this.owner.myState.state != null
* (soft) this.owner.server != null
*/
289 checkParser(tParser);
290
291 owner.doNotification("userModeDiscovered", CoreActionType.SERVER_USERMODES,
292 cClient, sModes);
293 }
294
295 /** {@inheritDoc} */
296 @Override
297 public void onInvite(final IRCParser tParser, final String userHost,
298 final String channel) {
/*
P/P * Method: void onInvite(IRCParser, String, String)
*
* Preconditions:
* tParser != null
* this.owner != null
* this.owner.invites != null
* this.owner.listeners != null
* this.owner.myState != null
* this.owner.myState.state != null
* (soft) this.owner.server != null
*/
299 checkParser(tParser);
300
301 owner.addInvite(new Invite(owner, channel, userHost));
302 owner.doNotification("inviteReceived",
303 CoreActionType.SERVER_INVITERECEIVED,
304 tParser.getClientInfoOrFake(userHost), channel);
305 }
306
307 /** {@inheritDoc} */
308 @Override
309 public void onWallop(final IRCParser tParser, final String sMessage,
310 final String sHost) {
/*
P/P * Method: void onWallop(IRCParser, String, String)
*
* Preconditions:
* tParser != null
* this.owner != null
* this.owner.myState != null
* this.owner.myState.state != null
* (soft) this.owner.server != null
*/
311 checkParser(tParser);
312
313 owner.doNotification("wallop", CoreActionType.SERVER_WALLOPS,
314 tParser.getClientInfoOrFake(sHost), sMessage);
315
316 }
317
318 /** {@inheritDoc} */
319 @Override
320 public void onWalluser(final IRCParser tParser, final String sMessage,
321 final String sHost) {
/*
P/P * Method: void onWalluser(IRCParser, String, String)
*
* Preconditions:
* tParser != null
* this.owner != null
* this.owner.myState != null
* this.owner.myState.state != null
* (soft) this.owner.server != null
*/
322 checkParser(tParser);
323
324 owner.doNotification("walluser", CoreActionType.SERVER_WALLUSERS,
325 tParser.getClientInfoOrFake(sHost), sMessage);
326 }
327
328 /** {@inheritDoc} */
329 @Override
330 public void onWallDesync(final IRCParser tParser, final String sMessage,
331 final String sHost) {
/*
P/P * Method: void onWallDesync(IRCParser, String, String)
*
* Preconditions:
* tParser != null
* this.owner != null
* this.owner.myState != null
* this.owner.myState.state != null
* (soft) this.owner.server != null
*/
332 checkParser(tParser);
333
334 owner.doNotification("walldesync", CoreActionType.SERVER_WALLDESYNC,
335 tParser.getClientInfoOrFake(sHost), sMessage);
336 }
337
338 /** {@inheritDoc} */
339 @Override
340 public void onNickChanged(final IRCParser tParser, final ClientInfo cClient,
341 final String sOldNick) {
/*
P/P * Method: void onNickChanged(IRCParser, ClientInfo, String)
*
* Preconditions:
* cClient != null
* tParser != null
* this.owner.myState != null
* this.owner.myState.state != null
* (soft) this.owner != null
* (soft) this.owner.server != null
*
* Test Vectors:
* java.lang.Object:equals(...)@344: {0}, {1}
*/
342 checkParser(tParser);
343
344 if (cClient.equals(tParser.getMyself())) {
345 owner.doNotification("selfNickChange", CoreActionType.SERVER_NICKCHANGE,
346 sOldNick, cClient.getNickname());
347 }
348 }
349
350 /** {@inheritDoc} */
351 @Override
352 public void onServerError(final IRCParser tParser, final String sMessage) {
/*
P/P * Method: void onServerError(IRCParser, String)
*
* Preconditions:
* this.owner != null
* this.owner.myState != null
* this.owner.myState.state != null
* (soft) this.owner.server != null
*/
353 checkParser(tParser);
354
355 owner.doNotification("serverError", CoreActionType.SERVER_ERROR, sMessage);
356 }
357
358 /** {@inheritDoc} */
359 @Override
360 protected void checkParser(final IRCParser parser) {
/*
P/P * Method: void checkParser(IRCParser)
*
* Preconditions:
* this.owner != null
* this.owner.myState != null
* this.owner.myState.state != null
* (soft) this.owner.server != null
*/
361 super.checkParser(parser);
362
363 if (owner.getState() != ServerState.CONNECTED
364 && owner.getState() != ServerState.CONNECTING
365 && owner.getState() != ServerState.DISCONNECTING) {
366 throw new IllegalArgumentException("Event called from a parser that " +
367 "shouldn't be in use.\nState history:\n"
368 + owner.getStatus().getTransitionHistory());
369 }
370 }
371
372 }
SofCheck Inspector Build Version : 2.17854
| ServerEventHandler.java |
2009-Jun-25 01:54:24 |
| ServerEventHandler.class |
2009-Sep-02 17:04:12 |