ObjXMPP  Check-in [b74a310cc3]

Overview
Comment:Request and handle roster.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: b74a310cc362edadbbc5dff428db56cf488dd6081afc1b8fd46977d32e608093
User & Date: js on 2011-03-21 19:51:17
Other Links: manifest | tags
Context
2011-03-21
20:36
Send an error reply for unhandled IQ stanzas. check-in: f4868153e6 user: js tags: trunk
19:51
Request and handle roster. check-in: b74a310cc3 user: js tags: trunk
18:27
Generate unique IDs and free all instance variables on dealloc. check-in: 913f68c8af user: js tags: trunk
Changes

Modified src/Makefile from [61a2f5a5b5] to [992c84981f].

1
2
3
4
5
6
all:
	objfw-compile -Wall --lib 0.0 -o objxmpp *.m \
		`pkg-config --cflags --libs libidn` -lobjgnutls

clean:
	rm -f *.o *.so *.dylib *.dll

|




1
2
3
4
5
6
all:
	objfw-compile -Wall -g --lib 0.0 -o objxmpp *.m \
		`pkg-config --cflags --libs libidn` -lobjgnutls

clean:
	rm -f *.o *.so *.dylib *.dll

Modified src/XMPPConnection.h from [48873be5e7] to [eb4dbab3f1].

31
32
33
34
35
36
37

38
39
40
41
42
43
44
@class XMPPAuthenticator;

@protocol XMPPConnectionDelegate
@optional
- (void)connectionWasAuthenticated: (XMPPConnection*)conn;
- (void)connection: (XMPPConnection*)conn
     wasBoundToJID: (XMPPJID*)jid;

- (void)connection: (XMPPConnection*)conn
      didReceiveIQ: (XMPPIQ*)iq;
-   (void)connection: (XMPPConnection*)conn
  didReceivePresence: (XMPPPresence*)pres;
-  (void)connection: (XMPPConnection*)conn
  didReceiveMessage: (XMPPMessage*)msg;
- (void)connectionWasClosed: (XMPPConnection*)conn;







>







31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
@class XMPPAuthenticator;

@protocol XMPPConnectionDelegate
@optional
- (void)connectionWasAuthenticated: (XMPPConnection*)conn;
- (void)connection: (XMPPConnection*)conn
     wasBoundToJID: (XMPPJID*)jid;
- (void)connectionDidReceiveRoster: (XMPPConnection*)conn;
- (void)connection: (XMPPConnection*)conn
      didReceiveIQ: (XMPPIQ*)iq;
-   (void)connection: (XMPPConnection*)conn
  didReceivePresence: (XMPPPresence*)pres;
-  (void)connection: (XMPPConnection*)conn
  didReceiveMessage: (XMPPMessage*)msg;
- (void)connectionWasClosed: (XMPPConnection*)conn;
58
59
60
61
62
63
64
65

66
67
68
69
70
71
72

73
74
75
76
77
78
79
	uint16_t port;
	/// Whether to use TLS
	BOOL useTLS;
	id <XMPPConnectionDelegate, OFObject> delegate;
	XMPPAuthenticator *authModule;
	BOOL needsSession;
	unsigned int lastID;
	OFString *bindID, *sessionID;

}

@property (copy) OFString *username, *password, *server, *resource;
@property (copy, readonly) XMPPJID *JID;
@property (assign) uint16_t port;
@property (assign) BOOL useTLS;
@property (retain) id <XMPPConnectionDelegate> delegate;


/**
 * Connects to the XMPP service.
 */
- (void)connect;

/**







|
>







>







59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
	uint16_t port;
	/// Whether to use TLS
	BOOL useTLS;
	id <XMPPConnectionDelegate, OFObject> delegate;
	XMPPAuthenticator *authModule;
	BOOL needsSession;
	unsigned int lastID;
	OFString *bindID, *sessionID, *rosterID;
	OFMutableDictionary *roster;
}

@property (copy) OFString *username, *password, *server, *resource;
@property (copy, readonly) XMPPJID *JID;
@property (assign) uint16_t port;
@property (assign) BOOL useTLS;
@property (retain) id <XMPPConnectionDelegate> delegate;
@property (copy, readonly) OFDictionary *roster;

/**
 * Connects to the XMPP service.
 */
- (void)connect;

/**
90
91
92
93
94
95
96





97

/**
 * Generates a new, unique stanza ID.
 *
 * \return A new, generated, unique stanza ID.
 */
- (OFString*)generateStanzaID;





@end







>
>
>
>
>

93
94
95
96
97
98
99
100
101
102
103
104
105

/**
 * Generates a new, unique stanza ID.
 *
 * \return A new, generated, unique stanza ID.
 */
- (OFString*)generateStanzaID;

/**
 * Requests the roster.
 */
- (void)requestRoster;
@end

Modified src/XMPPConnection.m from [0ae0e9e2bc] to [374e4b1629].

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
#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_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_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
{
	self = [super init];


	sock = [[OFTCPSocket alloc] init];
	parser = [[OFXMLParser alloc] init];
	elementBuilder = [[OFXMLElementBuilder alloc] init];

	port = 5222;
	useTLS = YES;

	parser.delegate = self;
	elementBuilder.delegate = self;







	return self;
}

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







>
















>



|





>
|
|
|

|
|

|
|
>
>
>
>
>
>


















>
>







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
#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_ROSTER @"jabber:iq:roster"
#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"

@interface XMPPConnection ()
- (void)XMPP_startStream;
- (void)XMPP_sendAuth: (OFString*)name;
- (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;
- (void)XMPP_handleRoster: (XMPPIQ*)iq;
@end

@implementation XMPPConnection
@synthesize JID, port, useTLS, delegate, roster;

- init
{
	self = [super init];

	@try {
		sock = [[OFTCPSocket alloc] init];
		parser = [[OFXMLParser alloc] init];
		elementBuilder = [[OFXMLElementBuilder alloc] init];

		port = 5222;
		useTLS = YES;

		parser.delegate = self;
		elementBuilder.delegate = self;

		roster = [[OFMutableDictionary alloc] init];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (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];
	[rosterID release];
	[roster release];

	[super dealloc];
}

- (void)setUsername: (OFString*)username_
{
	OFString *old = username;
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396





397
398
399
400
401
402
403
			   @"<stream:stream to='%@' xmlns='" NS_CLIENT @"' "
			   @"xmlns:stream='" NS_STREAM @"' "
			   @"version='1.0'>", server];
}

- (void)XMPP_handleIQ: (XMPPIQ*)iq
{
	if ([iq.ID isEqual: bindID] && [iq.type isEqual: @"result"]) {
		[self XMPP_handleResourceBind: iq];
		return;
	}

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

- (void)XMPP_handleMessage: (XMPPMessage*)msg







|




|



>
>
>
>
>







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
			   @"<stream:stream to='%@' xmlns='" NS_CLIENT @"' "
			   @"xmlns:stream='" NS_STREAM @"' "
			   @"version='1.0'>", server];
}

- (void)XMPP_handleIQ: (XMPPIQ*)iq
{
	if ([iq.ID isEqual: bindID]) {
		[self XMPP_handleResourceBind: iq];
		return;
	}

	if ([iq.ID isEqual: sessionID]) {
		[self XMPP_handleSession: iq];
		return;
	}

	if ([iq.ID isEqual: rosterID]) {
		[self XMPP_handleRoster: iq];
		return;
	}

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

- (void)XMPP_handleMessage: (XMPPMessage*)msg
504
505
506
507
508
509
510
511
512
513





514
515
516
517
518
519
520
	[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]];







|


>
>
>
>
>







520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
	[iq addChild: bind];

	[self sendStanza: iq];
}

- (void)XMPP_handleResourceBind: (XMPPIQ*)iq
{
	OFXMLElement *bindElem;
	OFXMLElement *jidElem;

	if (![iq.type isEqual: @"result"])
		assert(0);

	bindElem = iq.children.firstObject;

	if (![bindElem.name isEqual: @"bind"] ||
	    ![bindElem.namespace isEqual: NS_BIND])
		assert(0);

	jidElem = bindElem.children.firstObject;
	JID = [[XMPPJID alloc] initWithString:
	    [jidElem.children.firstObject stringValue]];
552
553
554
555
556
557
558




























































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

	[sessionID release];
	sessionID = nil;
}




























































@end







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

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
	if ([delegate respondsToSelector: @selector(connection:wasBoundToJID:)])
		[delegate connection: self
		       wasBoundToJID: JID];

	[sessionID release];
	sessionID = nil;
}

- (void)requestRoster
{
	XMPPIQ *iq;

	if (rosterID != nil)
		assert(0);

	rosterID = [[self generateStanzaID] retain];
	iq = [XMPPIQ IQWithType: @"get"
			     ID: rosterID];
	[iq addChild: [OFXMLElement elementWithName: @"query"
					  namespace: NS_ROSTER]];
	[self sendStanza: iq];
}

- (void)XMPP_handleRoster: (XMPPIQ*)iq
{
	OFXMLElement *rosterElem;

	if (![iq.type isEqual: @"result"])
		assert(0);

	rosterElem = iq.children.firstObject;

	if (![rosterElem.name isEqual: @"query"] ||
	    ![rosterElem.namespace isEqual: NS_ROSTER])
		assert(0);

	for (OFXMLElement *elem in rosterElem.children) {
		OFString *group;
		OFMutableArray *rosterGroup;

		if (![elem.name isEqual: @"item"] ||
		    ![elem.ns isEqual: NS_ROSTER])
			continue;

		group =	[[elem
		    elementsForName: @"group"
			  namespace: NS_ROSTER].firstObject stringValue];

		if (group == nil)
			group = @"";

		if ((rosterGroup = [roster objectForKey: group]) == nil) {
			rosterGroup = [OFMutableArray array];
			[roster setObject: rosterGroup
				   forKey: group];
		}

		[rosterGroup addObject: elem];
	}

	if ([delegate respondsToSelector:
	    @selector(connectionDidReceiveRoster:)])
		[delegate connectionDidReceiveRoster: self];

	[rosterID release];
	rosterID = nil;
}
@end

Modified tests/Makefile from [45abb6a72d] to [8334b4231c].

1
2
3
4
5
all:
	objfw-compile -Wall -o tests *.m -I../src -L../src -lobjxmpp

clean:
	rm -f tests *.o

|



1
2
3
4
5
all:
	objfw-compile -Wall -g -o tests *.m -I../src -L../src -lobjxmpp

clean:
	rm -f tests *.o

Modified tests/test.m from [b2456e219f] to [05cdbc9644].

107
108
109
110
111
112
113







114
115
116
117
118
119
120
121
122
123
{
	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];
}







>
>
>
>
>
>
>


|







107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
{
	of_log(@"Auth successful");
}

- (void)connection: (XMPPConnection*)conn
     wasBoundToJID: (XMPPJID*)jid
{
	of_log(@"Bound to JID: %@", [jid fullJID]);

	[conn requestRoster];
}

- (void)connectionDidReceiveRoster :(XMPPConnection*)conn
{
	XMPPPresence *pres;

	of_log(@"Got roster");

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

	[conn sendStanza: pres];
}