-
Notifications
You must be signed in to change notification settings - Fork 2
/
XMPPConnection.m
664 lines (606 loc) · 21.5 KB
/
XMPPConnection.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
//
// XMPPConnection.m
// Jabber
//
// Created by David Chisnall on Sun Apr 18 2004.
// Copyright (c) 2004 David Chisnall. All rights reserved.
//
#include <netdb.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <objc/runtime.h>
#import "XMPPConnection.h"
#import "XMPPStreamFeatures.h"
#import "XMPPDefaultHandler.h"
#import "XMPPPresence.h"
#import "XMPPAccount.h"
#import <EtoileXML/ETXMLParser.h>
#import <EtoileXML/ETXMLWriter.h>
static NSMutableDictionary * connections = nil;
static NSDictionary * STANZA_CLASSES;
static NSDictionary * STANZA_KEYS;
#define SET_STATE(x) do { object_setClass(self, [XMPP ## x ## Connection class]); ETLog(@"Entering state %s", #x); } while(0)
@interface NSObject( XMLLogging)
+ (void) logIncomingXML:(NSString*)xml;
+ (void) logOutgoingXML:(NSString*)xml;
@end
/**
* Each state in the XMPPConnection state machine is represented by a custom
* subclass.
*/
@interface XMPPConnectingConnection : XMPPConnection @end
@interface XMPPOfflineConnection : XMPPConnection @end
@interface XMPPConnectedConnection : XMPPConnectingConnection @end
@interface XMPPEncryptingConnection : XMPPConnectedConnection @end
@interface XMPPLoggingInConnection : XMPPConnectedConnection @end
@interface XMPPUnboundConnection : XMPPConnectedConnection @end
@interface XMPPNoSessionConnection : XMPPConnectedConnection @end
@interface XMPPLoggedInConnection : XMPPConnectedConnection @end
@interface XMPPDroppedConnection : XMPPConnection @end
@interface XMPPConnection (Private)
- (void) legacyLogIn;
@end
@implementation XMPPConnection
+ (void) initialize
{
//Create default handler classes
STANZA_CLASSES = [NSDictionary dictionaryWithObjectsAndKeys:
[XMPPMessage class], @"message",
[XMPPPresence class], @"presence",
[XMPPInfoQueryStanza class], @"iq",
[XMPPStreamFeatures class], @"stream:features",
nil];
STANZA_KEYS = [NSDictionary dictionaryWithObjectsAndKeys:
@"message", @"message",
@"presence", @"presence",
@"iq", @"iq",
@"streamFeatures", @"stream:features",
nil];
#ifndef DNDEBUG
ETLog(@"Stanza delegate classes: %@", STANZA_CLASSES);
#endif
}
+ (id) connectionWithAccount:(NSString*)_account
{
XMPPConnection * connection;
if (connections == nil)
{
connections = [[NSMutableDictionary alloc] init];
}
connection = [connections objectForKey:_account];
if (connection == nil)
{
connection = [XMPPConnection alloc];
[connections setObject:connection forKey:_account];
connection = [connection initWithAccount:_account];
}
return connection;
}
- (id) initWithAccount:(id)anAccount
{
if (![anAccount isKindOfClass:[XMPPAccount class]])
{
return nil;
}
SUPERINIT;
res = [[NSHost currentHost] name];
//Get the log class, if it has been built
xmlLog = NSClassFromString(@"XMLLog");
account = anAccount;
roster = [(XMPPAccount*)account roster];
XMPPDefaultHandler * defaultHandler = [[XMPPDefaultHandler alloc] initWithAccount:account];
dispatcher = [XMPPDispatcher dispatcherWithDefaultInfoQueryHandler:roster
messageHandler:defaultHandler
presenceHandler:roster];
return self;
}
- (void)resetKeepAlive
{
[keepalive invalidate];
keepalive = [NSTimer scheduledTimerWithTimeInterval: 50
target: self
selector: @selector(sendKeepAlive:)
userInfo: nil
repeats: NO];
}
- (void) reconnectToJabberServer
{
ETLog(@"Connecting...");
socket = [ETSocket socketConnectedToRemoteHost: serverHost
forService: @"xmpp-client"];
if (nil == socket)
{
// Legacy service description for operating systems (e.g. OS X) that
// haven't updated /etc/services to the standardised version.
socket = [ETSocket socketConnectedToRemoteHost: serverHost
forService: @"jabber-client"];
if (nil == socket)
{
ETLog(@"Connect failing\n");
return;
}
}
SET_STATE(Connecting);
//Initialise the parser
parser = [[ETXMLParser alloc] init];
[parser pushContentHandler:self];
[self resetKeepAlive];
[socket setDelegate: self];
xmlWriter = [ETXMLSocketWriter new];
[xmlWriter setSocket: socket];
[self receivedData: nil fromSocket: nil];
}
//Connect to an XMPP server.
- (void) connectToJabberServer:(NSString*) jabberServer
withJID:(JID*) aJID
password:(NSString*) password
{
user = [aJID node];
server = [aJID domain];
pass=password;
if (jabberServer == nil)
{
serverHost = server;
}
else
{
serverHost = jabberServer;
}
#ifndef DNDEBUG
ETLog(@"Connecting to %@ with username %@ and password %@", serverHost, user, pass);
#endif
[self reconnectToJabberServer];
}
- (void) disconnect {}
- (void)characters:(NSString *)_chars
{
ETLog(@"Unexpected CDATA encountered in <stream:stream /> tag:\n\%@", _chars);
}
- (void)sendString: (NSString*)aString
{
#ifndef DNDEBUG
ETLog(@"SENT: %@", aString);
#endif
[self resetKeepAlive];
[socket sendData: [aString dataUsingEncoding: NSUTF8StringEncoding]];
}
- (void)sendKeepAlive: (id)sender
{
[self sendString: @" "];
}
- (void)receivedData: (NSData*)aData fromSocket: (ETSocket*)aSocket {}
- (NSString*) server
{
return server;
}
- (void)startElement:(NSString *)aName
attributes:(NSDictionary *)_attributes
{
#ifndef DNDEBUG
ETLog(@"Parsing element: %@", aName);
#endif
if ([aName isEqualToString:@"stream:stream"])
{
sessionID = [_attributes objectForKey:@"id"];
server = [_attributes objectForKey:@"from"];
if (![[_attributes objectForKey:@"version"] isEqualToString:@"1.0"])
{
[self legacyLogIn];
}
}
else
{
NSString * childKey = [STANZA_KEYS objectForKey:aName];
id <ETXMLParserDelegate> stanzaDelegate = [[[STANZA_CLASSES objectForKey:aName] alloc] initWithXMLParser:parser key:childKey];
[stanzaDelegate startElement:aName
attributes:_attributes];
}
}
- (void)logInWithMechansisms:(NSSet*) aFeatureSet
{
//TODO: DIGEST-MD5 auth
if ([aFeatureSet containsObject:@"PLAIN"])
{
NSMutableData * authData = [NSMutableData dataWithBytes:"\0" length:1];
[authData appendData:[user dataUsingEncoding:NSUTF8StringEncoding]];
[authData appendBytes:"\0" length:1];
[authData appendData:[pass dataUsingEncoding:NSUTF8StringEncoding]];
NSString * authstring = [authData base64String];
//Send auth mechanism
[xmlWriter startAndEndElement: @"auth"
attributes: D(@"urn:ietf:params:xml:ns:xmpp-sasl",
@"xmlns", @"PLAIN", @"mechanism")
cdata: authstring];
SET_STATE(LoggingIn);
}
else
{
ETLog(@"No supported authentication mechanisms found. Aborting.");
}
}
- (void) startSession
{
NSString * sessionIqID = [self nextMessageID];
[xmlWriter startElement: @"iq"
attributes: D(@"set", @"type", sessionIqID, @"id")];
[xmlWriter startAndEndElement: @"session"
attributes: D(@"urn:ietf:params:xml:ns:xmpp-session",
@"xmlns")];
[xmlWriter endElement]; // </iq>
[dispatcher addInfoQueryResultHandler:self forID:sessionIqID];
}
- (void) bind
{
//Bind to a resource
//<iq type='set' id='bind_2'><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><resource>someresource</resource></bind></iq>
NSString * bindID = [self nextMessageID];
[xmlWriter startElement: @"iq"
attributes: D(@"set", @"type", bindID, @"id")];
[xmlWriter startElement: @"bind"
attributes: D(@"urn:ietf:params:xml:ns:xmpp-bind", @"xmlns")];
[xmlWriter startAndEndElement: @"resource"];
[xmlWriter endElement]; // </bind>
[xmlWriter endElement]; // </iq>
[dispatcher addInfoQueryResultHandler: self forID: bindID];
}
//Child stanza handlers
- (void) addmessage:(XMPPMessage*)aMessage
{
[dispatcher dispatchMessage:aMessage];
}
- (void) addiq:(XMPPInfoQueryStanza*)anIQ
{
[dispatcher dispatchInfoQuery:anIQ];
}
- (void) addpresence:(XMPPPresence*)aPresence
{
[dispatcher dispatchPresence:aPresence];
}
//END child stanza handlers
- (void)endElement:(NSString *)_Name
{
if ([_Name isEqualToString:@"stream:stream"])
{
/*
if(connectionState != loggedIn)
{
Jesse says: we need some other kind of solution here since we don't have
a -connectionFailed method anymore... not sure what to do. I commented it
out since it was causing XCode's build to fail.
if([[NSApp delegate] respondsToSelector:@selector(connectionFailed:)])
{
[(JabberApp*)[NSApp delegate] connectionFailed:account];
}
}
//If we have not manually disconnected, try to reconnect.
*/
[presenceDisplay setPresence:PRESENCE_OFFLINE withMessage:@"Disconnected"];
}
}
- (void) setPresenceDisplay:(id<XMPPPresenceDisplay,NSObject>)_display
{
presenceDisplay = _display;
}
- (void) handleInfoQuery:(XMPPInfoQueryStanza*)anIq {}
- (NSString*) nextMessageID
{
unsigned int i = messageID++;
return [NSString stringWithFormat:@"ETXMPP_%d", i];
}
- (void) XMPPSend: (NSString*) buffer
{
[xmlLog logOutgoingXML:buffer];
//If we are not connected, buffer the input until we are.
if (unsentBuffer == nil)
{
unsentBuffer = [[NSMutableString alloc] init];
}
[unsentBuffer appendString:buffer];
}
- (void) setStatus:(unsigned char)aStatus withMessage:(NSString*)aMessage
{
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
if (aStatus == PRESENCE_OFFLINE)
{
[attributes setObject: @"unavailable" forKey: @"type"];
}
[xmlWriter startElement: @"presence"
attributes: attributes];
if (aStatus != PRESENCE_ONLINE)
{
[xmlWriter startAndEndElement: @"show"
cdata: [XMPPPresence xmppStringForPresence: aStatus]];
}
NSDictionary * presenceDictionary;
if (aMessage != nil)
{
[xmlWriter startAndEndElement: @"status"
cdata: aMessage];
presenceDictionary = D([NSNumber numberWithChar:aStatus],@"show",
aMessage,@"status");
}
else
{
presenceDictionary = D([NSNumber numberWithChar:aStatus], @"show");
}
[xmlWriter endElement];
//Notify anyone who cares that our presence has changed
NSNotificationCenter * local = [NSNotificationCenter defaultCenter];
NSNotificationCenter * remote = [NSDistributedNotificationCenter defaultCenter];
[local postNotificationName:@"LocalPresenceChangedNotification"
object:account
userInfo:presenceDictionary];
[remote postNotificationName:@"LocalPresenceChangedNotification"
object:[account name]
userInfo:presenceDictionary];
}
- (void) setParser:(id)aParser
{
parser = aParser;
}
//Does nothing. This should never be used, since we are the root element...
- (void) setParent:(id) newParent {}
- (XMPPDispatcher*) dispatcher
{
return dispatcher;
}
- (BOOL)isConnected
{
return NO;
}
- (ETXMLSocketWriter*)xmlWriter
{
return xmlWriter;
}
- (BOOL) isDied
{
return [socket connectionIsBroken];
}
@end
/**
* Offline behaviour is implemented in the superclass, so this subclass doesn't
* provide any methods.
*/
@implementation XMPPOfflineConnection @end
@implementation XMPPConnectedConnection
- (BOOL)isConnected
{
return YES;
}
//Digest non-SASL auth.
- (void) legacyLogIn
{
NSString * nextMessageID = [self nextMessageID];
[dispatcher addInfoQueryResultHandler:self forID:nextMessageID];
[xmlWriter startElement: @"iq"
attributes: D(nextMessageID, @"id", @"set", @"type", server, @"to")];
NSString * sessionPassword = [sessionID stringByAppendingString:pass];
NSData *data = [sessionPassword dataUsingEncoding: NSUTF8StringEncoding];
NSString * digest = [data sha1];
[xmlWriter startElement: @"query"
attributes: D(@"jabber:iq:auth", @"xmlns")];
[xmlWriter startAndEndElement: @"username"
cdata: user];
[xmlWriter startAndEndElement: @"digest"
cdata: digest];
[xmlWriter startAndEndElement: @"resource"
cdata: res];
[xmlWriter endElement]; // </query>
[xmlWriter endElement]; // </iq>
SET_STATE(LoggingIn);
}
- (void)receivedData: (NSData*)aData fromSocket: (ETSocket*)aSocket
{
if ([self isDied])
{
NSLog(@"Connection Dropped!");
SET_STATE(Dropped);
[self receivedData:nil fromSocket:nil];
return;
}
[self resetKeepAlive];
NSString *xml =
[[NSString alloc] initWithData: aData
encoding: NSUTF8StringEncoding];
#ifndef DNDEBUG
ETLog(@"Received: '%@'", xml);
#endif
[xmlLog logIncomingXML: xml];
[parser parseFromSource: xml];
}
- (void) addstreamFeatures:(NSDictionary*) aFeatureSet
{
streamFeatures = aFeatureSet;
//If we are connected, try logging in
if ([[aFeatureSet objectForKey: @"starttls"]
isEqualToString: @"urn:ietf:params:xml:ns:xmpp-tls"])
{
[self sendString: @"<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>"];
SET_STATE(Encrypting);
}
//Hack for broken servers
else if ([[aFeatureSet objectForKey:@"auth"] isEqualToString:@"http://jabber.org/features/iq-auth"])
{
[self legacyLogIn];
}
else
{
[self logInWithMechansisms:[aFeatureSet objectForKey:@"mechanisms"]];
}
}
@end
@implementation XMPPConnectingConnection
- (void) reconnectToJabberServer
{
[self disconnect];
SET_STATE(Offline);
[self reconnectToJabberServer];
}
- (void)endElement:(NSString *)_Name
{
if ([_Name isEqualToString:@"stream:stream"])
{
//If we have not manually disconnected, try to reconnect.
[self reconnectToJabberServer];
}
}
- (void) disconnect
{
[self XMPPSend:@"</stream:stream>"];
socket = nil;
[xmlWriter setSocket:nil];
xmlWriter = nil;
SET_STATE(Offline);
}
- (void)receivedData: (NSData*)aData fromSocket: (ETSocket*)aSocket
{
[self resetKeepAlive];
[self sendString: [NSString stringWithFormat:
@"<?xml version='1.0' encoding='UTF-8' ?><stream:stream to='%@'"
" xmlns='jabber:client' version='1.0' xmlns:stream="
"'http://etherx.jabber.org/streams'>",
server]];
SET_STATE(Connected);
}
@end
@implementation XMPPLoggedInConnection
- (void)startElement:(NSString *)aName
attributes:(NSDictionary *)_attributes
{
NSString * childKey = [STANZA_KEYS objectForKey:aName];
id <ETXMLParserDelegate> stanzaDelegate = [[[STANZA_CLASSES objectForKey:aName] alloc] initWithXMLParser:parser key:childKey];
[stanzaDelegate startElement:aName
attributes:_attributes];
}
- (void) handleInfoQuery:(XMPPInfoQueryStanza*)anIq
{
if (([anIq type] == IQ_TYPE_RESULT))
{
NSString * nextMessageID = [self nextMessageID];
[dispatcher addInfoQueryResultHandler:roster forID:nextMessageID];
[xmlWriter startElement: @"iq"
attributes: D(nextMessageID, @"id", @"get", @"type")];
[xmlWriter startElement: @"query"
attributes: D(@"jabber:iq:roster", @"xmlns")];
[xmlWriter endElement];
[xmlWriter endElement];
SET_STATE(LoggedIn);
if(unsentBuffer!=nil && [unsentBuffer isEqualToString:@""] == NO )
[self XMPPSend:unsentBuffer];
[unsentBuffer setString:@""];
}
}
- (void) XMPPSend: (NSString*) buffer
{
[xmlLog logOutgoingXML:buffer];
if (unsentBuffer != nil)
{
[self sendString: unsentBuffer];
unsentBuffer = nil;
}
[self sendString: buffer];
}
@end
@implementation XMPPUnboundConnection
- (void) addstreamFeatures:(NSDictionary*) aFeatureSet
{
streamFeatures = aFeatureSet;
if ([aFeatureSet objectForKey:@"bind"] != nil)
{
[self bind];
}
else if ([aFeatureSet objectForKey:@"session"] != nil)
{
SET_STATE(NoSession);
[self startSession];
}
else
{
SET_STATE(LoggedIn);
}
}
- (void) handleInfoQuery:(XMPPInfoQueryStanza*)anIq
{
if ([streamFeatures objectForKey:@"session"] != nil)
{
SET_STATE(NoSession);
[self startSession];
}
else
{
SET_STATE(LoggedIn);
[self handleInfoQuery: anIq];
}
}
@end
@implementation XMPPEncryptingConnection
- (void)startElement:(NSString *)aName
attributes:(NSDictionary *)_attributes
{
if ([aName isEqualToString: @"proceed"])
{
ETLog(@"SSL returned %d", [socket negotiateSSL]);
SET_STATE(Connecting);
// Reset the connection
[self receivedData: nil fromSocket: nil];
}
}
@end
@implementation XMPPLoggingInConnection
- (void) handleInfoQuery:(XMPPInfoQueryStanza*)anIq
{
SET_STATE(LoggedIn);
[self handleInfoQuery: anIq];
}
- (void)startElement:(NSString *)aName
attributes:(NSDictionary *)_attributes
{
if ([aName isEqualToString:@"success"])
{
//Once we're authenticated, re-initialise the stream...ha
SET_STATE(Unbound);
// FIXME: Move this to a method
NSString * newStream = [NSString stringWithFormat:@"<stream:stream to='%@' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>",
server];
[self sendString: newStream];
}
// TODO: Handle failure.
}
@end
@implementation XMPPNoSessionConnection
- (void) handleInfoQuery:(XMPPInfoQueryStanza*)anIq
{
SET_STATE(LoggedIn);
[self handleInfoQuery: anIq];
}
@end
@implementation XMPPDroppedConnection
- (void)receivedData: (NSData*)aData fromSocket: (ETSocket*)aSocket
{
socket = nil;
[xmlWriter setSocket:nil];
xmlWriter = nil;
[presenceDisplay setPresence:PRESENCE_OFFLINE withMessage:nil];
[roster offline];
NSTimer *connectionStabilizer = [NSTimer scheduledTimerWithTimeInterval: 5
target: self
selector: @selector(stabilizerHelper)
userInfo: nil
repeats: NO];
NSLog(@"Try to reconnect");
[self reconnectToJabberServer];
if (![[self className] isEqualToString:@"XMPPDroppedConnection"])
{
[connectionStabilizer invalidate];
}
}
- (void) stabilizerHelper
{
[self receivedData:nil fromSocket:nil];
}
@end