|
@@ -0,0 +1,718 @@
|
|
1
|
+/*
|
|
2
|
+ * Copyright (c) 2006-2015 DMDirc Developers
|
|
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.parser.xmpp;
|
|
24
|
+
|
|
25
|
+import com.dmdirc.parser.common.AwayState;
|
|
26
|
+import com.dmdirc.parser.common.BaseSocketAwareParser;
|
|
27
|
+import com.dmdirc.parser.common.ChannelJoinRequest;
|
|
28
|
+import com.dmdirc.parser.common.ChildImplementations;
|
|
29
|
+import com.dmdirc.parser.common.CompositionState;
|
|
30
|
+import com.dmdirc.parser.common.DefaultStringConverter;
|
|
31
|
+import com.dmdirc.parser.common.ParserError;
|
|
32
|
+import com.dmdirc.parser.common.QueuePriority;
|
|
33
|
+import com.dmdirc.parser.interfaces.ChannelInfo;
|
|
34
|
+import com.dmdirc.parser.interfaces.ClientInfo;
|
|
35
|
+import com.dmdirc.parser.interfaces.LocalClientInfo;
|
|
36
|
+import com.dmdirc.parser.interfaces.StringConverter;
|
|
37
|
+import com.dmdirc.parser.interfaces.callbacks.AwayStateListener;
|
|
38
|
+import com.dmdirc.parser.interfaces.callbacks.CallbackInterface;
|
|
39
|
+import com.dmdirc.parser.interfaces.callbacks.ChannelSelfJoinListener;
|
|
40
|
+import com.dmdirc.parser.interfaces.callbacks.CompositionStateChangeListener;
|
|
41
|
+import com.dmdirc.parser.interfaces.callbacks.ConnectErrorListener;
|
|
42
|
+import com.dmdirc.parser.interfaces.callbacks.DataInListener;
|
|
43
|
+import com.dmdirc.parser.interfaces.callbacks.DataOutListener;
|
|
44
|
+import com.dmdirc.parser.interfaces.callbacks.NumericListener;
|
|
45
|
+import com.dmdirc.parser.interfaces.callbacks.OtherAwayStateListener;
|
|
46
|
+import com.dmdirc.parser.interfaces.callbacks.PrivateActionListener;
|
|
47
|
+import com.dmdirc.parser.interfaces.callbacks.PrivateMessageListener;
|
|
48
|
+import com.dmdirc.parser.interfaces.callbacks.ServerReadyListener;
|
|
49
|
+import com.dmdirc.parser.interfaces.callbacks.SocketCloseListener;
|
|
50
|
+
|
|
51
|
+import java.net.URI;
|
|
52
|
+import java.util.Collection;
|
|
53
|
+import java.util.Collections;
|
|
54
|
+import java.util.Date;
|
|
55
|
+import java.util.HashMap;
|
|
56
|
+import java.util.List;
|
|
57
|
+import java.util.Map;
|
|
58
|
+import java.util.regex.Matcher;
|
|
59
|
+import java.util.regex.Pattern;
|
|
60
|
+
|
|
61
|
+import org.jivesoftware.smack.Chat;
|
|
62
|
+import org.jivesoftware.smack.ChatManagerListener;
|
|
63
|
+import org.jivesoftware.smack.ConnectionConfiguration;
|
|
64
|
+import org.jivesoftware.smack.ConnectionListener;
|
|
65
|
+import org.jivesoftware.smack.PacketListener;
|
|
66
|
+import org.jivesoftware.smack.RosterEntry;
|
|
67
|
+import org.jivesoftware.smack.RosterListener;
|
|
68
|
+import org.jivesoftware.smack.XMPPConnection;
|
|
69
|
+import org.jivesoftware.smack.XMPPException;
|
|
70
|
+import org.jivesoftware.smack.filter.PacketFilter;
|
|
71
|
+import org.jivesoftware.smack.packet.Message;
|
|
72
|
+import org.jivesoftware.smack.packet.Packet;
|
|
73
|
+import org.jivesoftware.smack.packet.Presence;
|
|
74
|
+import org.jivesoftware.smackx.ChatState;
|
|
75
|
+import org.jivesoftware.smackx.ChatStateListener;
|
|
76
|
+import org.jivesoftware.smackx.ChatStateManager;
|
|
77
|
+import org.jivesoftware.smackx.muc.MultiUserChat;
|
|
78
|
+import org.slf4j.LoggerFactory;
|
|
79
|
+
|
|
80
|
+/**
|
|
81
|
+ * A parser which can understand the XMPP protocol.
|
|
82
|
+ */
|
|
83
|
+@ChildImplementations({
|
|
84
|
+ XmppClientInfo.class, XmppLocalClientInfo.class, XmppFakeChannel.class,
|
|
85
|
+ XmppChannelClientInfo.class
|
|
86
|
+})
|
|
87
|
+public class XmppParser extends BaseSocketAwareParser {
|
|
88
|
+
|
|
89
|
+ private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(XmppParser.class);
|
|
90
|
+ /** Pattern to use to extract priority. */
|
|
91
|
+ private static final Pattern PRIORITY_PATTERN = Pattern.compile(
|
|
92
|
+ "(?i)(?:^|&)priority=([0-9]+)(?:$|&)");
|
|
93
|
+ /** The connection to use. */
|
|
94
|
+ private XMPPConnection connection;
|
|
95
|
+ /** The state manager for the current connection. */
|
|
96
|
+ private ChatStateManager stateManager;
|
|
97
|
+ /** A cache of known chats. */
|
|
98
|
+ private final Map<String, Chat> chats = new HashMap<>();
|
|
99
|
+ /** A cache of known clients. */
|
|
100
|
+ private final Map<String, XmppClientInfo> contacts = new HashMap<>();
|
|
101
|
+ /** Whether or not to use a fake local channel for a buddy list replacement. */
|
|
102
|
+ private final boolean useFakeChannel;
|
|
103
|
+ /** The priority of this endpoint. */
|
|
104
|
+ private final int priority;
|
|
105
|
+ /** The fake channel to use is useFakeChannel is enabled. */
|
|
106
|
+ private XmppFakeChannel fakeChannel;
|
|
107
|
+
|
|
108
|
+ /**
|
|
109
|
+ * Creates a new XMPP parser for the specified address.
|
|
110
|
+ *
|
|
111
|
+ * @param address The address to connect to
|
|
112
|
+ */
|
|
113
|
+ public XmppParser(final URI address) {
|
|
114
|
+ super(address);
|
|
115
|
+
|
|
116
|
+ if (address.getQuery() == null) {
|
|
117
|
+ useFakeChannel = false;
|
|
118
|
+ priority = 0;
|
|
119
|
+ } else {
|
|
120
|
+ final Matcher matcher = PRIORITY_PATTERN.matcher(address.getQuery());
|
|
121
|
+
|
|
122
|
+ useFakeChannel = address.getQuery().matches("(?i).*(^|&)showchannel($|&).*");
|
|
123
|
+ priority = matcher.find() ? Integer.parseInt(matcher.group(1)) : 0;
|
|
124
|
+ }
|
|
125
|
+
|
|
126
|
+ LOG.debug(
|
|
127
|
+ "XMPP parser created with query string {}, parsed fake channel = {}, priority = {}",
|
|
128
|
+ address.getQuery(), useFakeChannel, priority);
|
|
129
|
+ }
|
|
130
|
+
|
|
131
|
+ @Override
|
|
132
|
+ public void disconnect(final String message) {
|
|
133
|
+ super.disconnect(message);
|
|
134
|
+ // TODO: Pass quit message on as presence?
|
|
135
|
+ connection.disconnect();
|
|
136
|
+ }
|
|
137
|
+
|
|
138
|
+ @Override
|
|
139
|
+ public void joinChannels(final ChannelJoinRequest... channels) {
|
|
140
|
+ for (ChannelJoinRequest request : channels) {
|
|
141
|
+ final MultiUserChat muc = new MultiUserChat(connection, request.getName().substring(1));
|
|
142
|
+
|
|
143
|
+ try {
|
|
144
|
+ if (request.getPassword() == null) {
|
|
145
|
+ muc.join(getLocalClient().getNickname());
|
|
146
|
+ } else {
|
|
147
|
+ muc.join(getLocalClient().getNickname(), request.getPassword());
|
|
148
|
+ }
|
|
149
|
+
|
|
150
|
+ // TODO: Send callbacks etc
|
|
151
|
+ } catch (XMPPException ex) {
|
|
152
|
+ // TODO: handle
|
|
153
|
+ }
|
|
154
|
+ }
|
|
155
|
+ }
|
|
156
|
+
|
|
157
|
+ @Override
|
|
158
|
+ public ChannelInfo getChannel(final String channel) {
|
|
159
|
+ // TODO: Implement
|
|
160
|
+ throw new UnsupportedOperationException("Not supported yet.");
|
|
161
|
+ }
|
|
162
|
+
|
|
163
|
+ @Override
|
|
164
|
+ public Collection<? extends ChannelInfo> getChannels() {
|
|
165
|
+ return Collections.<ChannelInfo>emptyList();
|
|
166
|
+ }
|
|
167
|
+
|
|
168
|
+ @Override
|
|
169
|
+ public int getMaxLength(final String type, final String target) {
|
|
170
|
+ return Integer.MAX_VALUE;
|
|
171
|
+ }
|
|
172
|
+
|
|
173
|
+ @Override
|
|
174
|
+ public int getMaxLength() {
|
|
175
|
+ return Integer.MAX_VALUE;
|
|
176
|
+ }
|
|
177
|
+
|
|
178
|
+ @Override
|
|
179
|
+ public LocalClientInfo getLocalClient() {
|
|
180
|
+ final String[] parts = parseHostmask(connection.getUser());
|
|
181
|
+
|
|
182
|
+ // TODO: Cache this
|
|
183
|
+ return new XmppLocalClientInfo(this, parts[0], parts[2], parts[1]);
|
|
184
|
+ }
|
|
185
|
+
|
|
186
|
+ @Override
|
|
187
|
+ public XmppClientInfo getClient(final String details) {
|
|
188
|
+ final String[] parts = parseHostmask(details);
|
|
189
|
+
|
|
190
|
+ if (!contacts.containsKey(parts[0])) {
|
|
191
|
+ contacts.put(parts[0], new XmppClientInfo(this, parts[0], parts[2], parts[1]));
|
|
192
|
+ }
|
|
193
|
+
|
|
194
|
+ return contacts.get(parts[0]);
|
|
195
|
+ }
|
|
196
|
+
|
|
197
|
+ @Override
|
|
198
|
+ public void sendRawMessage(final String message) {
|
|
199
|
+ // Urgh, hacky horrible rubbish. These commands should call methods.
|
|
200
|
+ if (message.toUpperCase().startsWith("WHOIS ")) {
|
|
201
|
+ handleWhois(message.split(" ")[1]);
|
|
202
|
+ } else if (!message.isEmpty() && message.charAt(0) == '<') {
|
|
203
|
+ // Looks vaguely like XML, let's send it.
|
|
204
|
+ connection.sendPacket(new Packet() {
|
|
205
|
+
|
|
206
|
+ @Override
|
|
207
|
+ public String toXML() {
|
|
208
|
+ return message;
|
|
209
|
+ }
|
|
210
|
+ });
|
|
211
|
+ }
|
|
212
|
+ }
|
|
213
|
+
|
|
214
|
+ /**
|
|
215
|
+ * Handles a whois request for the specified target.
|
|
216
|
+ *
|
|
217
|
+ * @param target The user being WHOIS'd
|
|
218
|
+ */
|
|
219
|
+ private void handleWhois(final String target) {
|
|
220
|
+ // Urgh, hacky horrible rubbish. This should be abstracted.
|
|
221
|
+ if (contacts.containsKey(target)) {
|
|
222
|
+ final XmppClientInfo client = contacts.get(target);
|
|
223
|
+ final String[] userParts = client.getNickname().split("@", 2);
|
|
224
|
+
|
|
225
|
+ callNumericCallback(311, target, userParts[0], userParts[1], "*", client.getRealname());
|
|
226
|
+
|
|
227
|
+ for (Map.Entry<String, XmppEndpoint> endpoint : client.getEndpoints().entrySet()) {
|
|
228
|
+ callNumericCallback(399, target, endpoint.getKey(),
|
|
229
|
+ "(" + endpoint.getValue().getPresence() + ")", "has endpoint");
|
|
230
|
+ }
|
|
231
|
+ } else {
|
|
232
|
+ callNumericCallback(401, target, "No such contact found");
|
|
233
|
+ }
|
|
234
|
+
|
|
235
|
+ callNumericCallback(318, target, "End of /WHOIS.");
|
|
236
|
+ }
|
|
237
|
+
|
|
238
|
+ private void callNumericCallback(final int numeric, final String... args) {
|
|
239
|
+ final String[] newArgs = new String[args.length + 3];
|
|
240
|
+ newArgs[0] = ":xmpp.server";
|
|
241
|
+ newArgs[1] = (numeric < 100 ? "0" : "") + (numeric < 10 ? "0" : "") + numeric;
|
|
242
|
+ newArgs[2] = getLocalClient().getNickname();
|
|
243
|
+ System.arraycopy(args, 0, newArgs, 3, args.length);
|
|
244
|
+
|
|
245
|
+ getCallback(NumericListener.class).onNumeric(this, new Date(), numeric, newArgs);
|
|
246
|
+ }
|
|
247
|
+
|
|
248
|
+ @Override
|
|
249
|
+ public void sendRawMessage(final String message, final QueuePriority priority) {
|
|
250
|
+ sendRawMessage(message);
|
|
251
|
+ }
|
|
252
|
+
|
|
253
|
+ @Override
|
|
254
|
+ public StringConverter getStringConverter() {
|
|
255
|
+ return new DefaultStringConverter();
|
|
256
|
+ }
|
|
257
|
+
|
|
258
|
+ @Override
|
|
259
|
+ public boolean isValidChannelName(final String name) {
|
|
260
|
+ return false; // TODO: Implement
|
|
261
|
+ }
|
|
262
|
+
|
|
263
|
+ @Override
|
|
264
|
+ public boolean compareURI(final URI uri) {
|
|
265
|
+ throw new UnsupportedOperationException("Not supported yet.");
|
|
266
|
+ }
|
|
267
|
+
|
|
268
|
+ @Override
|
|
269
|
+ public Collection<? extends ChannelJoinRequest> extractChannels(final URI uri) {
|
|
270
|
+ return Collections.<ChannelJoinRequest>emptyList();
|
|
271
|
+ }
|
|
272
|
+
|
|
273
|
+ @Override
|
|
274
|
+ public String getNetworkName() {
|
|
275
|
+ return "XMPP"; // TODO
|
|
276
|
+ }
|
|
277
|
+
|
|
278
|
+ @Override
|
|
279
|
+ public String getServerSoftware() {
|
|
280
|
+ return "Unknown"; // TODO
|
|
281
|
+ }
|
|
282
|
+
|
|
283
|
+ @Override
|
|
284
|
+ public String getServerSoftwareType() {
|
|
285
|
+ return "XMPP"; // TODO
|
|
286
|
+ }
|
|
287
|
+
|
|
288
|
+ @Override
|
|
289
|
+ public List<String> getServerInformationLines() {
|
|
290
|
+ return Collections.emptyList(); // TODO
|
|
291
|
+ }
|
|
292
|
+
|
|
293
|
+ @Override
|
|
294
|
+ public int getMaxTopicLength() {
|
|
295
|
+ return 0; // TODO
|
|
296
|
+ }
|
|
297
|
+
|
|
298
|
+ @Override
|
|
299
|
+ public String getBooleanChannelModes() {
|
|
300
|
+ return ""; // TODO
|
|
301
|
+ }
|
|
302
|
+
|
|
303
|
+ @Override
|
|
304
|
+ public String getListChannelModes() {
|
|
305
|
+ return ""; // TODO
|
|
306
|
+ }
|
|
307
|
+
|
|
308
|
+ @Override
|
|
309
|
+ public int getMaxListModes(final char mode) {
|
|
310
|
+ return 0; // TODO
|
|
311
|
+ }
|
|
312
|
+
|
|
313
|
+ @Override
|
|
314
|
+ public boolean isUserSettable(final char mode) {
|
|
315
|
+ throw new UnsupportedOperationException("Not supported yet.");
|
|
316
|
+ }
|
|
317
|
+
|
|
318
|
+ @Override
|
|
319
|
+ public String getParameterChannelModes() {
|
|
320
|
+ return ""; // TODO
|
|
321
|
+ }
|
|
322
|
+
|
|
323
|
+ @Override
|
|
324
|
+ public String getDoubleParameterChannelModes() {
|
|
325
|
+ return ""; // TODO
|
|
326
|
+ }
|
|
327
|
+
|
|
328
|
+ @Override
|
|
329
|
+ public String getUserModes() {
|
|
330
|
+ return ""; // TODO
|
|
331
|
+ }
|
|
332
|
+
|
|
333
|
+ @Override
|
|
334
|
+ public String getChannelUserModes() {
|
|
335
|
+ return ""; // TODO
|
|
336
|
+ }
|
|
337
|
+
|
|
338
|
+ @Override
|
|
339
|
+ public String getChannelPrefixes() {
|
|
340
|
+ return "#";
|
|
341
|
+ }
|
|
342
|
+
|
|
343
|
+ @Override
|
|
344
|
+ public long getServerLatency() {
|
|
345
|
+ return 1000L; // TODO
|
|
346
|
+ }
|
|
347
|
+
|
|
348
|
+ @Override
|
|
349
|
+ public void sendCTCP(final String target, final String type, final String message) {
|
|
350
|
+ throw new UnsupportedOperationException("Not supported yet.");
|
|
351
|
+ }
|
|
352
|
+
|
|
353
|
+ @Override
|
|
354
|
+ public void sendCTCPReply(final String target, final String type, final String message) {
|
|
355
|
+ throw new UnsupportedOperationException("Not supported yet.");
|
|
356
|
+ }
|
|
357
|
+
|
|
358
|
+ @Override
|
|
359
|
+ public void sendMessage(final String target, final String message) {
|
|
360
|
+ if (!chats.containsKey(target)) {
|
|
361
|
+ LOG.debug("Creating new chat for {}", target);
|
|
362
|
+ chats.put(target, connection.getChatManager().createChat(target,
|
|
363
|
+ new MessageListenerImpl()));
|
|
364
|
+ }
|
|
365
|
+
|
|
366
|
+ try {
|
|
367
|
+ chats.get(target).sendMessage(message);
|
|
368
|
+ } catch (XMPPException ex) {
|
|
369
|
+ // TODO: Handle this
|
|
370
|
+ }
|
|
371
|
+ }
|
|
372
|
+
|
|
373
|
+ @Override
|
|
374
|
+ public void sendNotice(final String target, final String message) {
|
|
375
|
+ throw new UnsupportedOperationException("Not supported yet.");
|
|
376
|
+ }
|
|
377
|
+
|
|
378
|
+ @Override
|
|
379
|
+ public void sendAction(final String target, final String message) {
|
|
380
|
+ sendMessage(target, "/me " + message);
|
|
381
|
+ }
|
|
382
|
+
|
|
383
|
+ @Override
|
|
384
|
+ public void sendInvite(final String channel, final String user) {
|
|
385
|
+ throw new UnsupportedOperationException("Not supported yet.");
|
|
386
|
+ }
|
|
387
|
+
|
|
388
|
+ @Override
|
|
389
|
+ public void sendWhois(final String nickname) {
|
|
390
|
+ // TODO: Implement this
|
|
391
|
+ }
|
|
392
|
+
|
|
393
|
+ @Override
|
|
394
|
+ public String getLastLine() {
|
|
395
|
+ return "TODO: Implement me";
|
|
396
|
+ }
|
|
397
|
+
|
|
398
|
+ @Override
|
|
399
|
+ public String[] parseHostmask(final String hostmask) {
|
|
400
|
+ return new XmppProtocolDescription().parseHostmask(hostmask);
|
|
401
|
+ }
|
|
402
|
+
|
|
403
|
+ @Override
|
|
404
|
+ public long getPingTime() {
|
|
405
|
+ throw new UnsupportedOperationException("Not supported yet.");
|
|
406
|
+ }
|
|
407
|
+
|
|
408
|
+ @Override
|
|
409
|
+ public void run() {
|
|
410
|
+ if (getURI().getUserInfo() == null || !getURI().getUserInfo().contains(":")) {
|
|
411
|
+ getCallback(ConnectErrorListener.class).onConnectError(this,
|
|
412
|
+ new Date(), new ParserError(ParserError.ERROR_USER,
|
|
413
|
+ "User name and password must be specified in URI", ""));
|
|
414
|
+ return;
|
|
415
|
+ }
|
|
416
|
+ final String[] userInfoParts = getURI().getUserInfo().split(":", 2);
|
|
417
|
+ final String[] userParts = userInfoParts[0].split("@", 2);
|
|
418
|
+
|
|
419
|
+ final ConnectionConfiguration config = new ConnectionConfiguration(getURI().getHost(),
|
|
420
|
+ getURI().getPort(), userParts[0]);
|
|
421
|
+ config.setSecurityMode(getURI().getScheme().equalsIgnoreCase("xmpps")
|
|
422
|
+ ? ConnectionConfiguration.SecurityMode.required
|
|
423
|
+ : ConnectionConfiguration.SecurityMode.disabled);
|
|
424
|
+ config.setSASLAuthenticationEnabled(true);
|
|
425
|
+ config.setReconnectionAllowed(false);
|
|
426
|
+ config.setRosterLoadedAtLogin(true);
|
|
427
|
+ config.setSocketFactory(getSocketFactory());
|
|
428
|
+ connection = new FixedXmppConnection(config);
|
|
429
|
+
|
|
430
|
+ try {
|
|
431
|
+ connection.connect();
|
|
432
|
+
|
|
433
|
+ connection.addConnectionListener(new ConnectionListenerImpl());
|
|
434
|
+ connection.addPacketListener(new PacketListenerImpl(DataInListener.class),
|
|
435
|
+ new AcceptAllPacketFilter());
|
|
436
|
+ connection.addPacketSendingListener(new PacketListenerImpl(DataOutListener.class),
|
|
437
|
+ new AcceptAllPacketFilter());
|
|
438
|
+ connection.getChatManager().addChatListener(new ChatManagerListenerImpl());
|
|
439
|
+
|
|
440
|
+ try {
|
|
441
|
+ connection.login(userInfoParts[0], userInfoParts[1], "DMDirc.");
|
|
442
|
+ } catch (XMPPException ex) {
|
|
443
|
+ getCallback(ConnectErrorListener.class).onConnectError(this,
|
|
444
|
+ new Date(), new ParserError(ParserError.ERROR_USER,
|
|
445
|
+ ex.getMessage(), ""));
|
|
446
|
+ return;
|
|
447
|
+ }
|
|
448
|
+
|
|
449
|
+ connection.sendPacket(new Presence(Presence.Type.available, null, priority,
|
|
450
|
+ Presence.Mode.available));
|
|
451
|
+ connection.getRoster().addRosterListener(new RosterListenerImpl());
|
|
452
|
+
|
|
453
|
+ stateManager = ChatStateManager.getInstance(connection);
|
|
454
|
+
|
|
455
|
+ setServerName(connection.getServiceName());
|
|
456
|
+
|
|
457
|
+ getCallback(ServerReadyListener.class).onServerReady(this, new Date());
|
|
458
|
+
|
|
459
|
+ for (RosterEntry contact : connection.getRoster().getEntries()) {
|
|
460
|
+ getClient(contact.getUser()).setRosterEntry(contact);
|
|
461
|
+ }
|
|
462
|
+
|
|
463
|
+ if (useFakeChannel) {
|
|
464
|
+ fakeChannel = new XmppFakeChannel(this, "&contacts");
|
|
465
|
+ getCallback(ChannelSelfJoinListener.class).
|
|
466
|
+ onChannelSelfJoin(null, null, fakeChannel);
|
|
467
|
+ fakeChannel.updateContacts(contacts.values());
|
|
468
|
+
|
|
469
|
+ contacts.values().stream().filter(XmppClientInfo::isAway).forEach(client ->
|
|
470
|
+ getCallback(OtherAwayStateListener.class).onAwayStateOther(this, new Date(),
|
|
471
|
+ client, AwayState.UNKNOWN, AwayState.AWAY));
|
|
472
|
+ }
|
|
473
|
+ } catch (XMPPException ex) {
|
|
474
|
+ LOG.debug("Go an XMPP exception", ex);
|
|
475
|
+
|
|
476
|
+ connection = null;
|
|
477
|
+
|
|
478
|
+ final ParserError error = new ParserError(ParserError.ERROR_ERROR, "Unable to connect",
|
|
479
|
+ "");
|
|
480
|
+
|
|
481
|
+ if (ex.getWrappedThrowable() instanceof Exception) {
|
|
482
|
+ // Pass along the underlying exception instead of an XMPP
|
|
483
|
+ // specific one
|
|
484
|
+ error.setException((Exception) ex.getWrappedThrowable());
|
|
485
|
+ } else {
|
|
486
|
+ error.setException(ex);
|
|
487
|
+ }
|
|
488
|
+
|
|
489
|
+ getCallback(ConnectErrorListener.class).onConnectError(this, new Date(), error);
|
|
490
|
+ }
|
|
491
|
+ }
|
|
492
|
+
|
|
493
|
+ /**
|
|
494
|
+ * Handles a client's away state changing.
|
|
495
|
+ *
|
|
496
|
+ * @param client The client whose state is changing
|
|
497
|
+ * @param isBack True if the client is coming back, false if they're going away
|
|
498
|
+ */
|
|
499
|
+ public void handleAwayStateChange(final ClientInfo client, final boolean isBack) {
|
|
500
|
+ LOG.debug("Handling away state change for {} to {}", client.getNickname(), isBack);
|
|
501
|
+
|
|
502
|
+ if (useFakeChannel) {
|
|
503
|
+ getCallback(OtherAwayStateListener.class)
|
|
504
|
+ .onAwayStateOther(null, null, client,
|
|
505
|
+ isBack ? AwayState.AWAY : AwayState.HERE,
|
|
506
|
+ isBack ? AwayState.HERE : AwayState.AWAY);
|
|
507
|
+ }
|
|
508
|
+ }
|
|
509
|
+
|
|
510
|
+ /**
|
|
511
|
+ * Marks the local user as away with the specified reason.
|
|
512
|
+ *
|
|
513
|
+ * @param reason The away reason
|
|
514
|
+ */
|
|
515
|
+ public void setAway(final String reason) {
|
|
516
|
+ connection.sendPacket(new Presence(Presence.Type.available, reason,
|
|
517
|
+ priority, Presence.Mode.away));
|
|
518
|
+
|
|
519
|
+ getCallback(AwayStateListener.class).onAwayState(this, new Date(),
|
|
520
|
+ AwayState.HERE, AwayState.AWAY, reason);
|
|
521
|
+ }
|
|
522
|
+
|
|
523
|
+ /**
|
|
524
|
+ * Marks the local user as back.
|
|
525
|
+ */
|
|
526
|
+ public void setBack() {
|
|
527
|
+ connection.sendPacket(new Presence(Presence.Type.available, null,
|
|
528
|
+ priority, Presence.Mode.available));
|
|
529
|
+
|
|
530
|
+ getCallback(AwayStateListener.class).onAwayState(this, new Date(),
|
|
531
|
+ AwayState.AWAY, AwayState.HERE, null);
|
|
532
|
+ }
|
|
533
|
+
|
|
534
|
+ @Override
|
|
535
|
+ public void setCompositionState(final String host, final CompositionState state) {
|
|
536
|
+ LOG.debug("Setting composition state for {} to {}", host, state);
|
|
537
|
+
|
|
538
|
+ final Chat chat = chats.get(parseHostmask(host)[0]);
|
|
539
|
+
|
|
540
|
+ final ChatState newState;
|
|
541
|
+
|
|
542
|
+ switch (state) {
|
|
543
|
+ case ENTERED_TEXT:
|
|
544
|
+ newState = ChatState.paused;
|
|
545
|
+ break;
|
|
546
|
+ case TYPING:
|
|
547
|
+ newState = ChatState.composing;
|
|
548
|
+ break;
|
|
549
|
+ case IDLE:
|
|
550
|
+ default:
|
|
551
|
+ newState = ChatState.active;
|
|
552
|
+ break;
|
|
553
|
+ }
|
|
554
|
+
|
|
555
|
+ if (chat != null && stateManager != null) {
|
|
556
|
+ try {
|
|
557
|
+ stateManager.setCurrentState(newState, chat);
|
|
558
|
+ } catch (XMPPException ex) {
|
|
559
|
+ // Can't set chat state... Oh well?
|
|
560
|
+ LOG.info("Couldn't set composition state", ex);
|
|
561
|
+ }
|
|
562
|
+ }
|
|
563
|
+ }
|
|
564
|
+
|
|
565
|
+ @Override
|
|
566
|
+ public void requestGroupList(final String searchTerms) {
|
|
567
|
+ // Do nothing
|
|
568
|
+ }
|
|
569
|
+
|
|
570
|
+ private class ConnectionListenerImpl implements ConnectionListener {
|
|
571
|
+
|
|
572
|
+ @Override
|
|
573
|
+ public void connectionClosed() {
|
|
574
|
+ getCallback(SocketCloseListener.class).onSocketClosed(XmppParser.this, new Date());
|
|
575
|
+ }
|
|
576
|
+
|
|
577
|
+ @Override
|
|
578
|
+ public void connectionClosedOnError(final Exception excptn) {
|
|
579
|
+ // TODO: Handle exception
|
|
580
|
+ getCallback(SocketCloseListener.class).onSocketClosed(XmppParser.this, new Date());
|
|
581
|
+ }
|
|
582
|
+
|
|
583
|
+ @Override
|
|
584
|
+ public void reconnectingIn(final int i) {
|
|
585
|
+ throw new UnsupportedOperationException("Not supported yet.");
|
|
586
|
+ }
|
|
587
|
+
|
|
588
|
+ @Override
|
|
589
|
+ public void reconnectionSuccessful() {
|
|
590
|
+ throw new UnsupportedOperationException("Not supported yet.");
|
|
591
|
+ }
|
|
592
|
+
|
|
593
|
+ @Override
|
|
594
|
+ public void reconnectionFailed(final Exception excptn) {
|
|
595
|
+ throw new UnsupportedOperationException("Not supported yet.");
|
|
596
|
+ }
|
|
597
|
+
|
|
598
|
+ }
|
|
599
|
+
|
|
600
|
+ private class RosterListenerImpl implements RosterListener {
|
|
601
|
+
|
|
602
|
+ @Override
|
|
603
|
+ public void entriesAdded(final Collection<String> clctn) {
|
|
604
|
+ // Do nothing, yet
|
|
605
|
+ }
|
|
606
|
+
|
|
607
|
+ @Override
|
|
608
|
+ public void entriesUpdated(final Collection<String> clctn) {
|
|
609
|
+ // Do nothing, yet
|
|
610
|
+ }
|
|
611
|
+
|
|
612
|
+ @Override
|
|
613
|
+ public void entriesDeleted(final Collection<String> clctn) {
|
|
614
|
+ // Do nothing, yet
|
|
615
|
+ }
|
|
616
|
+
|
|
617
|
+ @Override
|
|
618
|
+ public void presenceChanged(final Presence prsnc) {
|
|
619
|
+ getClient(prsnc.getFrom()).setPresence(prsnc);
|
|
620
|
+ }
|
|
621
|
+
|
|
622
|
+ }
|
|
623
|
+
|
|
624
|
+ private class ChatManagerListenerImpl implements ChatManagerListener {
|
|
625
|
+
|
|
626
|
+ @Override
|
|
627
|
+ public void chatCreated(final Chat chat, final boolean bln) {
|
|
628
|
+ if (!bln) {
|
|
629
|
+ // Only add chats that weren't created locally
|
|
630
|
+ chats.put(parseHostmask(chat.getParticipant())[0], chat);
|
|
631
|
+ chat.addMessageListener(new MessageListenerImpl());
|
|
632
|
+ }
|
|
633
|
+ }
|
|
634
|
+
|
|
635
|
+ }
|
|
636
|
+
|
|
637
|
+ private class MessageListenerImpl implements ChatStateListener {
|
|
638
|
+
|
|
639
|
+ @Override
|
|
640
|
+ public void processMessage(final Chat chat, final Message msg) {
|
|
641
|
+ if (msg.getType() == Message.Type.error) {
|
|
642
|
+ getCallback(NumericListener.class).onNumeric(XmppParser.this, new Date(),
|
|
643
|
+ 404, new String[]{
|
|
644
|
+ ":xmpp", "404", getLocalClient().getNickname(),
|
|
645
|
+ msg.getFrom(),
|
|
646
|
+ "Cannot send message: " + msg.getError().toString()
|
|
647
|
+ });
|
|
648
|
+ return;
|
|
649
|
+ }
|
|
650
|
+
|
|
651
|
+ if (msg.getBody() != null) {
|
|
652
|
+ if (msg.getBody().startsWith("/me ")) {
|
|
653
|
+ getCallback(PrivateActionListener.class).onPrivateAction(XmppParser.this,
|
|
654
|
+ new Date(), msg.getBody().substring(4), msg.getFrom());
|
|
655
|
+ } else {
|
|
656
|
+ getCallback(PrivateMessageListener.class).onPrivateMessage(XmppParser.this,
|
|
657
|
+ new Date(), msg.getBody(), msg.getFrom());
|
|
658
|
+ }
|
|
659
|
+ }
|
|
660
|
+ }
|
|
661
|
+
|
|
662
|
+ @Override
|
|
663
|
+ public void stateChanged(final Chat chat, final ChatState cs) {
|
|
664
|
+ final CompositionState state;
|
|
665
|
+
|
|
666
|
+ switch (cs) {
|
|
667
|
+ case paused:
|
|
668
|
+ state = CompositionState.ENTERED_TEXT;
|
|
669
|
+ break;
|
|
670
|
+ case composing:
|
|
671
|
+ state = CompositionState.TYPING;
|
|
672
|
+ break;
|
|
673
|
+ case active:
|
|
674
|
+ case gone:
|
|
675
|
+ case inactive:
|
|
676
|
+ default:
|
|
677
|
+ state = CompositionState.IDLE;
|
|
678
|
+ break;
|
|
679
|
+ }
|
|
680
|
+
|
|
681
|
+ getCallback(CompositionStateChangeListener.class)
|
|
682
|
+ .onCompositionStateChanged(XmppParser.this, new Date(), state,
|
|
683
|
+ chat.getParticipant());
|
|
684
|
+ }
|
|
685
|
+
|
|
686
|
+ }
|
|
687
|
+
|
|
688
|
+ private class PacketListenerImpl implements PacketListener {
|
|
689
|
+
|
|
690
|
+ private final Class<? extends CallbackInterface> callback;
|
|
691
|
+
|
|
692
|
+ public PacketListenerImpl(final Class<? extends CallbackInterface> callback) {
|
|
693
|
+ this.callback = callback;
|
|
694
|
+ }
|
|
695
|
+
|
|
696
|
+ @Override
|
|
697
|
+ public void processPacket(final Packet packet) {
|
|
698
|
+ if (callback.equals(DataOutListener.class)) {
|
|
699
|
+ getCallback(DataOutListener.class).onDataOut(XmppParser.this, new Date(),
|
|
700
|
+ packet.toXML(), true);
|
|
701
|
+ } else {
|
|
702
|
+ getCallback(DataInListener.class).onDataIn(XmppParser.this, new Date(),
|
|
703
|
+ packet.toXML());
|
|
704
|
+ }
|
|
705
|
+ }
|
|
706
|
+
|
|
707
|
+ }
|
|
708
|
+
|
|
709
|
+ private static class AcceptAllPacketFilter implements PacketFilter {
|
|
710
|
+
|
|
711
|
+ @Override
|
|
712
|
+ public boolean accept(final Packet packet) {
|
|
713
|
+ return true;
|
|
714
|
+ }
|
|
715
|
+
|
|
716
|
+ }
|
|
717
|
+
|
|
718
|
+}
|