︙ | | |
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
-
-
-
-
+
+
+
+
|
#define NS_STARTTLS @"urn:ietf:params:xml:ns:xmpp-tls"
#define NS_SESSION @"urn:ietf:params:xml:ns:xmpp-session"
#define NS_STREAM @"http://etherx.jabber.org/streams"
@interface XMPPConnection ()
- (void)XMPP_startStream;
- (void)XMPP_sendAuth: (OFString*)name;
- (void)XMPP_sendResourceBind;
- (void)XMPP_sendSession;
- (void)XMPP_handleResourceBind: (XMPPIQ*)iq;
- (void)XMPP_handleSession;
- (void)XMPP_handleFeatures: (OFXMLElement*)elem;
- (void)XMPP_handleIQ: (XMPPIQ*)iq;
- (void)XMPP_handleMessage: (XMPPMessage*)msg;
- (void)XMPP_handlePresence: (XMPPPresence*)pres;
- (void)XMPP_sendResourceBind;
- (void)XMPP_handleResourceBind: (XMPPIQ*)iq;
- (void)XMPP_sendSession;
- (void)XMPP_handleSession: (XMPPIQ*)iq;
@end
@implementation XMPPConnection
@synthesize JID, port, useTLS, delegate;
- init
{
|
︙ | | |
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
+
+
+
+
+
+
+
+
|
}
- (void)dealloc
{
[sock release];
[parser release];
[elementBuilder release];
[username release];
[password release];
[server release];
[resource release];
[JID release];
[delegate release];
[authModule release];
[bindID release];
[sessionID release];
[super dealloc];
}
- (void)setUsername: (OFString*)username_
{
OFString *old = username;
|
︙ | | |
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
+
+
+
+
+
|
}
- (void)sendStanza: (OFXMLElement*)elem
{
of_log(@"Out: %@", elem);
[sock writeString: [elem stringValue]];
}
- (OFString*)generateStanzaID
{
return [OFString stringWithFormat: @"objxmpp_%u", lastID++];
}
- (void)parser: (OFXMLParser*)p
didStartElement: (OFString*)name
withPrefix: (OFString*)prefix
namespace: (OFString*)ns
attributes: (OFArray*)attrs
{
|
︙ | | |
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
|
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
|
-
-
-
-
-
-
-
-
-
-
+
-
-
+
+
|
assert(0);
}
assert(0);
}
- (void)elementBuilder: (OFXMLElementBuilder*)b
didNotExpectCloseTag: (OFString*)name
withPrefix: (OFString*)prefix
namespace: (OFString*)ns
{
// TODO
}
- (void)XMPP_startStream
{
[sock writeFormat: @"<?xml version='1.0'?>\n"
@"<stream:stream to='%@' xmlns='" NS_CLIENT @"' "
@"xmlns:stream='" NS_STREAM @"' "
@"version='1.0'>", server];
}
- (void)XMPP_handleIQ: (XMPPIQ*)iq
{
// FIXME: More checking!
if ([iq.ID isEqual: @"bind0"] && [iq.type isEqual: @"result"]) {
if ([iq.ID isEqual: bindID] && [iq.type isEqual: @"result"]) {
[self XMPP_handleResourceBind: iq];
return;
}
if ([iq.ID isEqual: @"session0"] && [iq.type isEqual: @"result"]) {
[self XMPP_handleSession];
if ([iq.ID isEqual: sessionID] && [iq.type isEqual: @"result"]) {
[self XMPP_handleSession: iq];
return;
}
if ([delegate respondsToSelector: @selector(connection:didReceiveIQ:)])
[delegate connection: self
didReceiveIQ: iq];
}
|
︙ | | |
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
|
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
|
+
+
+
+
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
-
+
+
+
+
-
+
+
+
+
|
[[authModule clientFirstMessage] stringByBase64Encoding]]];
[self sendStanza: authTag];
}
- (void)XMPP_sendResourceBind
{
XMPPIQ *iq;
OFXMLElement *bind;
bindID = [[self generateStanzaID] retain];
XMPPIQ *iq = [XMPPIQ IQWithType: @"set"
ID: @"bind0"];
OFXMLElement *bind = [OFXMLElement elementWithName: @"bind"
namespace: NS_BIND];
if (resource)
iq = [XMPPIQ IQWithType: @"set"
ID: bindID];
bind = [OFXMLElement elementWithName: @"bind"
namespace: NS_BIND];
if (resource != nil)
[bind addChild: [OFXMLElement elementWithName: @"resource"
stringValue: resource]];
[iq addChild: bind];
[self sendStanza: iq];
}
- (void)XMPP_handleResourceBind: (XMPPIQ*)iq
{
OFXMLElement *bindElem = iq.children.firstObject;
OFXMLElement *jidElem;
if (![bindElem.name isEqual: @"bind"] ||
![bindElem.namespace isEqual: NS_BIND])
assert(0);
jidElem = bindElem.children.firstObject;
JID = [[XMPPJID alloc] initWithString:
[jidElem.children.firstObject stringValue]];
[bindID release];
bindID = nil;
if (needsSession) {
[self XMPP_sendSession];
return;
}
if ([delegate respondsToSelector: @selector(connection:wasBoundToJID:)])
[delegate connection: self
wasBoundToJID: JID];
}
- (void)XMPP_sendSession
{
XMPPIQ *iq;
sessionID = [[self generateStanzaID] retain];
XMPPIQ *iq = [XMPPIQ IQWithType: @"set"
ID: @"session0"];
iq = [XMPPIQ IQWithType: @"set"
ID: sessionID];
[iq addChild: [OFXMLElement elementWithName: @"session"
namespace: NS_SESSION]];
[self sendStanza: iq];
}
- (void)XMPP_handleSession
- (void)XMPP_handleSession: (XMPPIQ*)iq
{
if (![iq.type isEqual: @"result"])
assert(0);
if ([delegate respondsToSelector: @selector(connection:wasBoundToJID:)])
[delegate connection: self
wasBoundToJID: JID];
}
[sessionID release];
sessionID = nil;
}
@end
|