ObjXMPP  Check-in [8742a0f583]

Overview
Comment:Improve stanza handling and call delegate for iq, message and presence.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 8742a0f583fbefab892af5815f0b3013cd8b7d4330c5c2877a028ee2ad8f96e4
User & Date: js on 2011-03-17 20:36:04
Other Links: manifest | tags
Context
2011-03-21
13:56
Make use of optional protocols. check-in: 57b5811d80 user: js tags: trunk
2011-03-17
20:36
Improve stanza handling and call delegate for iq, message and presence. check-in: 8742a0f583 user: js tags: trunk
2011-03-10
21:14
Stop caching authentication mechanisms check-in: 991c7aeff2 user: florob@babelmonkeys.de tags: trunk
Changes

Modified src/XMPPConnection.m from [4a47a74ef8] to [b2823f282f].

30
31
32
33
34
35
36


37
38
39
40
41
42
43

#import "XMPPConnection.h"
#import "XMPPSCRAMAuth.h"
#import "XMPPPLAINAuth.h"
#import "XMPPStanza.h"
#import "XMPPJID.h"
#import "XMPPIQ.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"







>
>







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
			    initWithAuthcid: username
				   password: password];
			[self _sendAuth: @"PLAIN"];
		}
	} else if (bind != nil)
		[self _sendResourceBind];
}

























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













	if ([elem.name isEqual: @"features"] &&








	    [elem.namespace isEqual: NS_STREAM]) {

		[self _handleFeatures: elem];
		return;



	}

	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];



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


	}

	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];



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

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



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

	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];
	}

}

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







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>








>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
|
>
|
|
>
>
>










>
>
>
|


>
>



















>
>
>
|








>
>
>
|







|
|
<
<
<
|
<
<
<
|
>










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: @"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;
		}

		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;
		}

		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;
		}

		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);



	}




	assert(0);
}

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

Modified tests/test.m from [1d590d00a0] to [a03f952d74].

28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#import "XMPPConnection.h"
#import "XMPPJID.h"
#import "XMPPStanza.h"
#import "XMPPIQ.h"
#import "XMPPMessage.h"
#import "XMPPPresence.h"

@interface AppDelegate: OFObject
{
	XMPPConnection *conn;
}
@end

OF_APPLICATION_DELEGATE(AppDelegate)








|







28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#import "XMPPConnection.h"
#import "XMPPJID.h"
#import "XMPPStanza.h"
#import "XMPPIQ.h"
#import "XMPPMessage.h"
#import "XMPPPresence.h"

@interface AppDelegate: OFObject <XMPPConnectionDelegate>
{
	XMPPConnection *conn;
}
@end

OF_APPLICATION_DELEGATE(AppDelegate)

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
	XMPPStanza *stanza = [XMPPStanza stanzaWithElement: elem];
	assert([[elem stringValue] isEqual: [stanza stringValue]]);
	assert(([[OFString stringWithFormat: @"%@, %@, %@, %@",
	    stanza.from.fullJID, stanza.to.fullJID, stanza.type, stanza.ID]
	    isEqual: @"bob@localhost, alice@localhost, get, 42"]));

	conn = [[XMPPConnection alloc] init];


	if (arguments.count != 3) {
		of_log(@"Invalid count of command line arguments!");
		[OFApplication terminateWithStatus: 1];
	}

	[conn setServer: [arguments objectAtIndex: 0]];
	[conn setUsername: [arguments objectAtIndex: 1]];
	[conn setPassword: [arguments objectAtIndex: 2]];
	[conn setResource: @"ObjXMPP"];
	[conn setUseTLS: NO];

	[conn connect];
	@try {
		[conn handleConnection];
	} @catch (id e) {
		of_log(@"%@", e);
	}
}























@end







>



















>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

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
	XMPPStanza *stanza = [XMPPStanza stanzaWithElement: elem];
	assert([[elem stringValue] isEqual: [stanza stringValue]]);
	assert(([[OFString stringWithFormat: @"%@, %@, %@, %@",
	    stanza.from.fullJID, stanza.to.fullJID, stanza.type, stanza.ID]
	    isEqual: @"bob@localhost, alice@localhost, get, 42"]));

	conn = [[XMPPConnection alloc] init];
	conn.delegate = self;

	if (arguments.count != 3) {
		of_log(@"Invalid count of command line arguments!");
		[OFApplication terminateWithStatus: 1];
	}

	[conn setServer: [arguments objectAtIndex: 0]];
	[conn setUsername: [arguments objectAtIndex: 1]];
	[conn setPassword: [arguments objectAtIndex: 2]];
	[conn setResource: @"ObjXMPP"];
	[conn setUseTLS: NO];

	[conn connect];
	@try {
		[conn handleConnection];
	} @catch (id e) {
		of_log(@"%@", e);
	}
}

- (void)connectionWasClosed: (XMPPConnection*)conn
{
	of_log(@"Connection was closed!");
}

- (void)connection: (XMPPConnection*)conn
      didReceiveIQ: (XMPPIQ*)iq
{
	of_log(@"IQ: %@", iq);
}

-  (void)connection: (XMPPConnection*)conn
  didReceiveMessage: (XMPPMessage*)msg
{
	of_log(@"Message: %@", msg);
}

-   (void)connection: (XMPPConnection*)conn
  didReceivePresence: (XMPPPresence*)pres
{
	of_log(@"Presence: %@", pres);
}
@end