ObjXMPP  Diff

Differences From Artifact [4a47a74ef8]:

To Artifact [b2823f282f]:


30
31
32
33
34
35
36


37
38
39
40
41
42
43
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45







+
+








#import "XMPPConnection.h"
#import "XMPPSCRAMAuth.h"
#import "XMPPPLAINAuth.h"
#import "XMPPStanza.h"
#import "XMPPJID.h"
#import "XMPPIQ.h"
#import "XMPPMessage.h"
#import "XMPPPresence.h"
#import "XMPPExceptions.h"

#define NS_BIND @"urn:ietf:params:xml:ns:xmpp-bind"
#define NS_CLIENT @"jabber:client"
#define NS_SASL @"urn:ietf:params:xml:ns:xmpp-sasl"
#define NS_STARTTLS @"urn:ietf:params:xml:ns:xmpp-tls"
#define NS_STREAM @"http://etherx.jabber.org/streams"
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
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







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+








+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+










+
+
+
-
+


+
+



















+
+
+
-
+








+
+
+
-
+







-
-
+
+
-
-
-
-
+
-
-
-
-
+
+










			    initWithAuthcid: username
				   password: password];
			[self _sendAuth: @"PLAIN"];
		}
	} else if (bind != nil)
		[self _sendResourceBind];
}

- (void)_handleIQ: (XMPPIQ*)iq
{
	// FIXME: More checking!
	if ([iq.ID isEqual: @"bind0"] && [iq.type isEqual: @"result"]) {
		[self _handleResourceBind: iq];
		return;
	}

	[delegate connection: self
		didReceiveIQ: iq];
}

- (void)_handleMessage: (XMPPMessage*)msg
{
	[delegate connection: self
	   didReceiveMessage: msg];
}

- (void)_handlePresence: (XMPPPresence*)pres
{
	[delegate connection: self
	  didReceivePresence: pres];
}

- (void)elementBuilder: (OFXMLElementBuilder*)b
       didBuildElement: (OFXMLElement*)elem
{
	elem.defaultNamespace = NS_CLIENT;
	[elem setPrefix: @"stream"
	   forNamespace: NS_STREAM];

	if ([elem.namespace isEqual: NS_CLIENT]) {
		if ([elem.name isEqual: @"iq"]) {
			[self _handleIQ: [XMPPIQ stanzaWithElement: elem]];
			return;
		}

		if ([elem.name isEqual: @"message"]) {
			[self _handleMessage:
			    [XMPPMessage stanzaWithElement: elem]];
			return;
		}

	if ([elem.name isEqual: @"features"] &&
	    [elem.namespace isEqual: NS_STREAM]) {
		[self _handleFeatures: elem];
		return;
		if ([elem.name isEqual: @"presence"]) {
			[self _handlePresence:
			    [XMPPPresence stanzaWithElement: elem]];
			return;
		}

		assert(0);
	}

	if ([elem.namespace isEqual: NS_STREAM]) {
		if ([elem.name isEqual: @"features"]) {
			[self _handleFeatures: elem];
			return;
		}

		assert(0);
	}

	if ([elem.namespace isEqual: NS_STARTTLS]) {
		if ([elem.name isEqual: @"proceed"]) {
			/* FIXME: Catch errors here */
			sock = [[GTLSSocket alloc] initWithSocket: sock];

			/* Stream restart */
			parser.delegate = self;
			[self _startStream];
			return;
		}

		} else if ([elem.name isEqual: @"failure"])
		if ([elem.name isEqual: @"failure"])
			/* TODO: Find/create an exception to throw here */
			@throw [OFException newWithClass: isa];

		assert(0);
	}

	if ([elem.namespace isEqual: NS_SASL]) {
		if ([elem.name isEqual: @"challenge"]) {
			OFXMLElement *responseTag;
			OFDataArray *challenge =
			    [OFDataArray dataArrayWithBase64EncodedString:
				[elem.children.firstObject stringValue]];
			OFDataArray *response =
			    [authModule
			        calculateResponseWithChallenge: challenge];

			responseTag = [OFXMLElement elementWithName: @"response"
							  namespace: NS_SASL];
			[responseTag
			    addChild: [OFXMLElement elementWithCharacters:
				[response stringByBase64Encoding]]];

			[self sendStanza: responseTag];
			return;
		}

		} else if ([elem.name isEqual: @"success"]) {
		if ([elem.name isEqual: @"success"]) {
			[authModule parseServerFinalMessage:
			    [OFDataArray dataArrayWithBase64EncodedString:
				[elem.children.firstObject stringValue]]];
			of_log(@"Auth successful");

			/* Stream restart */
			parser.delegate = self;
			[self _startStream];
			return;
		}

		} else if ([elem.name isEqual: @"failure"]) {
		if ([elem.name isEqual: @"failure"]) {
			of_log(@"Auth failed!");
			// FIXME: Do more parsing/handling
			@throw [XMPPAuthFailedException
			    newWithClass: isa
			      connection: self
				  reason: [elem stringValue]];
		}
	}


		assert(0);
	if ([elem.name isEqual: @"iq"] &&
	    [elem.namespace isEqual: NS_CLIENT]) {
		XMPPIQ *iq = [XMPPIQ stanzaWithElement: elem];

	}
		// FIXME: More checking!
		if ([iq.ID isEqual: @"bind0"] && [iq.type isEqual: @"result"])
			[self _handleResourceBind: iq];
	}

	assert(0);
}

- (void)elementBuilder: (OFXMLElementBuilder*)b
  didNotExpectCloseTag: (OFString*)name
	    withPrefix: (OFString*)prefix
	     namespace: (OFString*)ns
{
	// TODO
}
@end