ObjXMPP  Check-in [ae80606d3d]

Overview
Comment:Request session and send initial presence in tests.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: ae80606d3d88f7eb21082a13908a0af16d37d44d22e438caa7ab9dc382905f76
User & Date: js on 2011-03-21 15:15:35
Other Links: manifest | tags
Context
2011-03-21
16:51
Add Xcode project. check-in: 1ea39ac77e user: js tags: trunk
15:15
Request session and send initial presence in tests. check-in: ae80606d3d user: js tags: trunk
14:44
Move auth and bound handling to delegate. check-in: 15c050df49 user: js tags: trunk
Changes

Modified src/XMPPConnection.h from [5712350844] to [2b28aa53b0].

65
66
67
68
69
70
71

72
73
74
75
76
77
78
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79







+







	XMPPJID *JID;
	/// The port to connect to
	short port;
	/// Whether to use TLS
	BOOL useTLS;
	id <XMPPConnectionDelegate, OFObject> delegate;
	XMPPAuthenticator *authModule;
	BOOL needsSession;
}

@property (copy) OFString *username;
@property (copy) OFString *password;
@property (copy) OFString *server;
@property (copy) OFString *resource;
@property (copy, readonly) XMPPJID *JID;

Modified src/XMPPConnection.m from [bdd589660a] to [761caabf32].

38
39
40
41
42
43
44

45
46
47
48
49
50
51
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52







+







#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_SESSION @"urn:ietf:params:xml:ns:xmpp-session"
#define NS_STREAM @"http://etherx.jabber.org/streams"

@implementation XMPPConnection
@synthesize username, password, server, resource, JID, port, useTLS, delegate;

- init
{
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206

207
208
209
210
211
212
213
192
193
194
195
196
197
198


199
200
201
202
203
204
205
206
207
208
209
210
211
212
213







-
-







+







		size_t len = [sock readNBytes: 512
				   intoBuffer: buf];

		if (len < 1 && [delegate respondsToSelector:
		    @selector(connectionWasClosed:)])
			[delegate connectionWasClosed: self];

		[of_stdout writeNBytes: len
			    fromBuffer: buf];
		[parser parseBuffer: buf
			   withSize: len];
	}
}

- (void)sendStanza: (OFXMLElement*)elem
{
	of_log(@"Out: %@", elem);
	[sock writeString: [elem stringValue]];
}

-    (void)parser: (OFXMLParser*)p
  didStartElement: (OFString*)name
       withPrefix: (OFString*)prefix
	namespace: (OFString*)ns
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
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








+
+
+
+
+
+
+
+
+



+

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

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









+
+




-
+


+
+
+
-
+










+
+
+
-
+




+

+
+
+
+
+
+
+
-
+

+
+
+
+









+
+
+
+
+







	if (resource)
		[bind addChild: [OFXMLElement elementWithName: @"resource"
						  stringValue: resource]];
	[iq addChild: bind];

	[self sendStanza: iq];
}

- (void)_sendSession
{
	XMPPIQ *iq = [XMPPIQ IQWithType: @"set"
				     ID: @"session0"];
	[iq addChild: [OFXMLElement elementWithName: @"session"
					  namespace: NS_SESSION]];
	[self sendStanza: iq];
}

- (void)_handleResourceBind: (XMPPIQ*)iq
{
	OFXMLElement *bindElem = iq.children.firstObject;
	OFXMLElement *jidElem;

	if ([bindElem.name isEqual: @"bind"] &&
	    [bindElem.namespace isEqual: NS_BIND]) {
		OFXMLElement *jidElem = bindElem.children.firstObject;
		JID = [[XMPPJID alloc] initWithString:
		    [jidElem.children.firstObject stringValue]];
	if (![bindElem.name isEqual: @"bind"] ||
	    ![bindElem.namespace isEqual: NS_BIND])
		assert(0);

	jidElem = bindElem.children.firstObject;
	JID = [[XMPPJID alloc] initWithString:
	    [jidElem.children.firstObject stringValue]];

	if (needsSession) {
		[self _sendSession];
		return;
	}
		if ([delegate respondsToSelector:
		    @selector(connection:wasBoundToJID:)])
			[delegate connection: self
			       wasBoundToJID: JID];
	}

	if ([delegate respondsToSelector: @selector(connection:wasBoundToJID:)])
		    [delegate connection: self
			   wasBoundToJID: JID];
}

- (void)_handleSession
{
	if ([delegate respondsToSelector: @selector(connection:wasBoundToJID:)])
		    [delegate connection: self
			   wasBoundToJID: JID];
}

- (void)_handleFeatures: (OFXMLElement*)elem
{
	OFXMLElement *starttls = [elem
	    elementsForName: @"starttls"
		  namespace: NS_STARTTLS].firstObject;
	OFXMLElement *bind = [elem elementsForName: @"bind"
					 namespace: NS_BIND].firstObject;
	OFXMLElement *session = [elem elementsForName: @"session"
					    namespace: NS_SESSION].firstObject;
	OFArray *mechs = [elem elementsForName: @"mechanisms"
				     namespace: NS_SASL];
	OFMutableArray *mechanisms = [OFMutableArray array];

	if (starttls != nil)
	if (starttls != nil) {
		[self sendStanza: [OFXMLElement elementWithName: @"starttls"
						      namespace: NS_STARTTLS]];
		return;
	}

	else if ([mechs count]) {
	if ([mechs count] > 0) {
		for (OFXMLElement *mech in [mechs.firstObject children])
			[mechanisms addObject:
			    [mech.children.firstObject stringValue]];

		if ([mechanisms containsObject: @"SCRAM-SHA-1"]) {
			authModule = [[XMPPSCRAMAuth alloc]
			    initWithAuthcid: username
				   password: password
				       hash: [OFSHA1Hash class]];
			[self _sendAuth: @"SCRAM-SHA-1"];
			return;
		}

		} else if ([mechanisms containsObject: @"PLAIN"]) {
		if ([mechanisms containsObject: @"PLAIN"]) {
			authModule = [[XMPPPLAINAuth alloc]
			    initWithAuthcid: username
				   password: password];
			[self _sendAuth: @"PLAIN"];
			return;
		}

		assert(0);
	}

	if (session != nil)
		needsSession = YES;

	} else if (bind != nil)
	if (bind != nil) {
		[self _sendResourceBind];
		return;
	}

	assert(0);
}

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

	if ([iq.ID isEqual: @"session0"] && [iq.type isEqual: @"result"]) {
		[self _handleSession];
		return;
	}

	if ([delegate respondsToSelector: @selector(connection:didReceiveIQ:)])
		    [delegate connection: self
			    didReceiveIQ: iq];
}

- (void)_handleMessage: (XMPPMessage*)msg
341
342
343
344
345
346
347


348
349
350
351
352
353
354
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403







+
+








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

	of_log(@"In:  %@", elem);

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

Modified tests/test.m from [8016af6ff2] to [b2456e219f].

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

46
47
48
49
50
51
52
29
30
31
32
33
34
35



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50







-
-
-







+







#import "XMPPJID.h"
#import "XMPPStanza.h"
#import "XMPPIQ.h"
#import "XMPPMessage.h"
#import "XMPPPresence.h"

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

OF_APPLICATION_DELEGATE(AppDelegate)

@implementation AppDelegate
- (void)applicationDidFinishLaunching
{
	XMPPConnection *conn;
	OFArray *arguments = [OFApplication arguments];

	XMPPPresence *pres = [XMPPPresence presence];
	[pres addShow: @"chat"];
	[pres addStatus: @"Bored"];
	[pres addPriority: 20];
	pres.to = [XMPPJID JIDWithString: @"alice@example.com"];
109
110
111
112
113
114
115


116






117
118
119
120
121
122
123
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129







+
+

+
+
+
+
+
+







{
	of_log(@"Auth successful");
}

- (void)connection: (XMPPConnection*)conn
     wasBoundToJID: (XMPPJID*)jid
{
	XMPPPresence *pres;

	of_log(@"Bound to JID: %@", [jid fullJID]);

	pres = [XMPPPresence presence];
	[pres addPriority: 10];
	[pres addStatus: @"ObjXMPP test is working!"];

	[conn sendStanza: pres];
}

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