ObjXMPP  Check-in [9815ad146d]

Overview
Comment:Add capability to register callbacks when sending IQs
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 9815ad146d58ceb44c77000a76ffee90d9095c11bcc85a97dfbc0b181c570b8a
User & Date: florob@babelmonkeys.de on 2012-01-01 02:42:01
Other Links: manifest | tags
Context
2012-01-04
22:25
Use OF_SETTER/OF_GETTER. check-in: 220daa147d user: js tags: trunk
2012-01-01
02:42
Add capability to register callbacks when sending IQs check-in: 9815ad146d user: florob@babelmonkeys.de tags: trunk
2011-12-11
23:40
Get rid of a warning. check-in: e85080a478 user: js tags: trunk
Changes

Modified src/Makefile from [9c79822df1] to [c7688681a6].

1
2
3
4
5
6
7
8

9
10
11
12
13
14
15
include ../extra.mk

LIB = ${OBJXMPP_SHARED_LIB}
LIB_MAJOR = 0
LIB_MINOR = 0
STATIC_LIB = ${OBJXMPP_STATIC_LIB}

SRCS = XMPPAuthenticator.m	\

       XMPPConnection.m		\
       XMPPExceptions.m		\
       XMPPIQ.m			\
       XMPPJID.m		\
       XMPPMessage.m		\
       XMPPPLAINAuth.m		\
       XMPPPresence.m		\








>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
include ../extra.mk

LIB = ${OBJXMPP_SHARED_LIB}
LIB_MAJOR = 0
LIB_MINOR = 0
STATIC_LIB = ${OBJXMPP_STATIC_LIB}

SRCS = XMPPAuthenticator.m	\
       XMPPCallback.m		\
       XMPPConnection.m		\
       XMPPExceptions.m		\
       XMPPIQ.m			\
       XMPPJID.m		\
       XMPPMessage.m		\
       XMPPPLAINAuth.m		\
       XMPPPresence.m		\

Added src/XMPPCallback.h version [9b4ffbc375].









































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
 * Copyright (c) 2011, Florian Zeitz <florob@babelmonkeys.de>
 *
 * https://webkeks.org/hg/objxmpp/
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice is present in all copies.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#import <ObjFW/ObjFW.h>

@class XMPPIQ;

@protocol XMPPCallback <OFObject>
- (void)runWithIQ: (XMPPIQ*)iq;
@end

#ifdef OF_HAVE_BLOCKS
typedef void(^xmpp_callback_block)(XMPPIQ*);

@interface XMPPBlockCallback: OFObject <XMPPCallback>
{
	xmpp_callback_block callback;
}
+ callbackWithCallbackBlock: (xmpp_callback_block)callback;
- initWithCallbackBlock: (xmpp_callback_block)callback;
@end
#endif

@interface XMPPObjectCallback: OFObject <XMPPCallback>
{
	id object;
	SEL selector;
}
+ callbackWithCallbackObject: (id)object
		    selector: (SEL)selector;
- initWithCallbackObject: (id)object
		selector: (SEL)selector;
@end

Added src/XMPPCallback.m version [2b6add7aa0].























































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
/*
 * Copyright (c) 2011, Florian Zeitz <florob@babelmonkeys.de>
 *
 * https://webkeks.org/hg/objxmpp/
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice is present in all copies.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#import "XMPPCallback.h"

#ifdef OF_HAVE_BLOCKS
@implementation XMPPBlockCallback
+ callbackWithCallbackBlock: (xmpp_callback_block)callback_
{
	return [[[self alloc] initWithCallbackBlock: callback_] autorelease];
}

- initWithCallbackBlock: (xmpp_callback_block)callback_
{
	self = [super init];

	callback = [callback_ copy];

	return self;
}

- (void)dealloc
{
	[callback release];

	[super dealloc];
}

- (void)runWithIQ: (XMPPIQ*)iq
{
	callback(iq);
}
@end
#endif

@implementation XMPPObjectCallback
+ callbackWithCallbackObject: (id)object_
		    selector: (SEL)selector_
{
	return [[[self alloc] initWithCallbackObject: object_
					    selector: selector_] autorelease];
}

- initWithCallbackObject: (id)object_
		selector: (SEL)selector_
{
	self = [super init];

	// TODO: Retain or follow delegate paradigm?
	object = [object_ retain];
	selector = selector_;

	return self;
}

- (void)dealloc
{
	[object release];

	[super dealloc];
}

- (void)runWithIQ: (XMPPIQ*)iq
{
	[object performSelector: selector
		     withObject: iq];
}
@end

Modified src/XMPPConnection.h from [75217b7182] to [bd5ddff49e].

73
74
75
76
77
78
79

80
81
82
83
84
85
86
87
88
89
90
91
	OFXMLParser *parser, *oldParser;
	OFXMLElementBuilder *elementBuilder, *oldElementBuilder;
	OFString *username, *password, *server, *resource;
	OFString *domain, *domainToASCII;
	XMPPJID *JID;
	uint16_t port;
	id <XMPPConnectionDelegate, OFObject> delegate;

	XMPPAuthenticator *authModule;
	BOOL needsSession;
	BOOL encryptionRequired, encrypted;
	unsigned int lastID;
	OFString *bindID, *sessionID;
	XMPPRoster *roster;
}

#ifdef OF_HAVE_PROPERTIES
@property (copy) OFString *username, *password, *server, *domain, *resource;
@property (copy, readonly) XMPPJID *JID;
@property (assign) uint16_t port;







>




<







73
74
75
76
77
78
79
80
81
82
83
84

85
86
87
88
89
90
91
	OFXMLParser *parser, *oldParser;
	OFXMLElementBuilder *elementBuilder, *oldElementBuilder;
	OFString *username, *password, *server, *resource;
	OFString *domain, *domainToASCII;
	XMPPJID *JID;
	uint16_t port;
	id <XMPPConnectionDelegate, OFObject> delegate;
	OFMutableDictionary *callbacks;
	XMPPAuthenticator *authModule;
	BOOL needsSession;
	BOOL encryptionRequired, encrypted;
	unsigned int lastID;

	XMPPRoster *roster;
}

#ifdef OF_HAVE_PROPERTIES
@property (copy) OFString *username, *password, *server, *domain, *resource;
@property (copy, readonly) XMPPJID *JID;
@property (assign) uint16_t port;
154
155
156
157
158
159
160





















161
162
163
164
165
166
167
/**
 * Sends an OFXMLElement, usually an XMPPStanza.
 *
 * \param element The element to send
 */
- (void)sendStanza: (OFXMLElement*)element;






















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








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







154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
 * Sends an OFXMLElement, usually an XMPPStanza.
 *
 * \param element The element to send
 */
- (void)sendStanza: (OFXMLElement*)element;

/**
 * Sends an XMPPIQ, registering a callback method
 *
 * \param object The object that contains the callback method
 * \param selector The selector of the callback method,
 *		   must take exactly one parameter of type XMPPIQ*
 */
-     (void)sendIQ: (XMPPIQ*)iq
withCallbackObject: (id)object
	  selector: (SEL)selector;

#ifdef OF_HAVE_BLOCKS
/**
 * Sends an XMPPIQ, registering a callback block
 *
 * \param callback The callback block
 */
-    (void)sendIQ: (XMPPIQ*)iq
withCallbackBlock: (void(^)(XMPPIQ*))callback;
#endif

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

Modified src/XMPPConnection.m from [7b09f7e693] to [e52bbff4d6].

33
34
35
36
37
38
39

40
41
42
43
44
45
46
#include <idna.h>

#import <ObjOpenSSL/SSLSocket.h>
#import <ObjOpenSSL/SSLInvalidCertificateException.h>
#import <ObjOpenSSL/X509Certificate.h>

#import "XMPPConnection.h"

#import "XMPPSRVLookup.h"
#import "XMPPSCRAMAuth.h"
#import "XMPPPLAINAuth.h"
#import "XMPPStanza.h"
#import "XMPPJID.h"
#import "XMPPIQ.h"
#import "XMPPMessage.h"







>







33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <idna.h>

#import <ObjOpenSSL/SSLSocket.h>
#import <ObjOpenSSL/SSLInvalidCertificateException.h>
#import <ObjOpenSSL/X509Certificate.h>

#import "XMPPConnection.h"
#import "XMPPCallback.h"
#import "XMPPSRVLookup.h"
#import "XMPPSCRAMAuth.h"
#import "XMPPPLAINAuth.h"
#import "XMPPStanza.h"
#import "XMPPJID.h"
#import "XMPPIQ.h"
#import "XMPPMessage.h"
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
{
	self = [super init];

	@try {
		sock = [[OFTCPSocket alloc] init];
		port = 5222;
		encrypted = NO;

	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[sock release];
	[parser release];
	[elementBuilder release];
	[username release];
	[password release];
	[server release];
	[domain release];
	[resource release];
	[JID release];
	[authModule release];
	[bindID release];
	[sessionID release];
	[roster release];

	[super dealloc];
}

- (void)setUsername: (OFString*)username_
{







>



















<
|
|







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

	@try {
		sock = [[OFTCPSocket alloc] init];
		port = 5222;
		encrypted = NO;
		callbacks = [[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];
	[domain release];
	[resource release];
	[JID release];

	[callbacks release];
	[authModule release];
	[roster release];

	[super dealloc];
}

- (void)setUsername: (OFString*)username_
{
347
348
349
350
351
352
353








































354
355
356
357
358
359
360
	if ([delegate respondsToSelector:
	    @selector(connection:didSendElement:)])
		[delegate connection: self
		      didSendElement: element];

	[sock writeString: [element XMLString]];
}









































- (OFString*)generateStanzaID
{
	return [OFString stringWithFormat: @"objxmpp_%u", lastID++];
}

-    (void)parser: (OFXMLParser*)p







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







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

	[sock writeString: [element XMLString]];
}

-     (void)sendIQ: (XMPPIQ*)iq
withCallbackObject: (id)object
	  selector: (SEL)selector
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	@try {
		if (![iq ID])
			[iq setID: [self generateStanzaID]];

		[callbacks setObject: [XMPPObjectCallback
					 callbackWithCallbackObject: object
							   selector: selector]
			      forKey: [iq ID]];
	} @finally {
		[pool release];
	}

	[self sendStanza: iq];
}

#ifdef OF_HAVE_BLOCKS
-    (void)sendIQ: (XMPPIQ*)iq
withCallbackBlock: (xmpp_callback_block)callback;
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	@try {
		if (![iq ID])
			[iq setID: [self generateStanzaID]];

		[callbacks setObject: [XMPPBlockCallback
					  callbackWithCallbackBlock: callback]
			      forKey: [iq ID]];
	} @finally {
		[pool release];
	}

	[self sendStanza: iq];
}
#endif

- (OFString*)generateStanzaID
{
	return [OFString stringWithFormat: @"objxmpp_%u", lastID++];
}

-    (void)parser: (OFXMLParser*)p
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666

667
668
669
670
671
672
673

	assert(0);
}

- (void)XMPP_handleIQ: (XMPPIQ*)iq
{
	BOOL handled = NO;

	if ([[iq ID] isEqual: bindID]) {
		[self XMPP_handleResourceBind: iq];
		return;
	}

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

		return;
	}

	if ([iq elementForName: @"query"
		     namespace: XMPP_NS_ROSTER])
		if ([roster handleIQ: iq])
			return;







|
<
<
<
|
|
<
|
>







693
694
695
696
697
698
699
700



701
702

703
704
705
706
707
708
709
710
711

	assert(0);
}

- (void)XMPP_handleIQ: (XMPPIQ*)iq
{
	BOOL handled = NO;
	id <XMPPCallback> callback;




	if ((callback = [callbacks objectForKey: [iq ID]])) {

		[callback runWithIQ: iq];
		[callbacks removeObjectForKey: [iq ID]];
		return;
	}

	if ([iq elementForName: @"query"
		     namespace: XMPP_NS_ROSTER])
		if ([roster handleIQ: iq])
			return;
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816


817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857


858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
}

- (void)XMPP_sendResourceBind
{
	XMPPIQ *iq;
	OFXMLElement *bind;

	bindID = [[self generateStanzaID] retain];
	iq = [XMPPIQ IQWithType: @"set"
			     ID: bindID];

	bind = [OFXMLElement elementWithName: @"bind"
				   namespace: XMPP_NS_BIND];

	if (resource != nil)
		[bind addChild: [OFXMLElement elementWithName: @"resource"
						    namespace: XMPP_NS_BIND
						  stringValue: resource]];

	[iq addChild: bind];

	[self sendStanza: iq];


}

- (void)XMPP_handleResourceBind: (XMPPIQ*)iq
{
	OFXMLElement *bindElement;
	OFXMLElement *jidElement;

	assert([[iq type] isEqual: @"result"]);

	bindElement = [iq elementForName: @"bind"
			       namespace: XMPP_NS_BIND];

	assert(bindElement != nil);

	jidElement = [bindElement elementForName: @"jid"
				       namespace: XMPP_NS_BIND];
	JID = [[XMPPJID alloc] initWithString: [jidElement 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];
	iq = [XMPPIQ IQWithType: @"set"
			     ID: sessionID];
	[iq addChild: [OFXMLElement elementWithName: @"session"
					  namespace: XMPP_NS_SESSION]];
	[self sendStanza: iq];


}

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

- (OFString*)XMPP_IDNAToASCII: (OFString*)domain_
{
	OFString *ret;
	char *cDomain;
	Idna_rc rc;







<

|











|
>
>


















<
<
<














<

|


|
>
>










<
<
<







833
834
835
836
837
838
839

840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873



874
875
876
877
878
879
880
881
882
883
884
885
886
887

888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904



905
906
907
908
909
910
911
}

- (void)XMPP_sendResourceBind
{
	XMPPIQ *iq;
	OFXMLElement *bind;


	iq = [XMPPIQ IQWithType: @"set"
			     ID: [self generateStanzaID]];

	bind = [OFXMLElement elementWithName: @"bind"
				   namespace: XMPP_NS_BIND];

	if (resource != nil)
		[bind addChild: [OFXMLElement elementWithName: @"resource"
						    namespace: XMPP_NS_BIND
						  stringValue: resource]];

	[iq addChild: bind];

	[self sendIQ: iq
  withCallbackObject: self
	    selector: @selector(XMPP_handleResourceBind:)];
}

- (void)XMPP_handleResourceBind: (XMPPIQ*)iq
{
	OFXMLElement *bindElement;
	OFXMLElement *jidElement;

	assert([[iq type] isEqual: @"result"]);

	bindElement = [iq elementForName: @"bind"
			       namespace: XMPP_NS_BIND];

	assert(bindElement != nil);

	jidElement = [bindElement elementForName: @"jid"
				       namespace: XMPP_NS_BIND];
	JID = [[XMPPJID alloc] initWithString: [jidElement stringValue]];




	if (needsSession) {
		[self XMPP_sendSession];
		return;
	}

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

- (void)XMPP_sendSession
{
	XMPPIQ *iq;


	iq = [XMPPIQ IQWithType: @"set"
			     ID: [self generateStanzaID]];
	[iq addChild: [OFXMLElement elementWithName: @"session"
					  namespace: XMPP_NS_SESSION]];
	[self sendIQ: iq
  withCallbackObject: self
	    selector: @selector(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];



}

- (OFString*)XMPP_IDNAToASCII: (OFString*)domain_
{
	OFString *ret;
	char *cDomain;
	Idna_rc rc;