Add capability to register callbacks when sending IQs
This commit is contained in:
parent
7e9672b76f
commit
2f7b349539
5 changed files with 221 additions and 22 deletions
|
@ -6,6 +6,7 @@ LIB_MINOR = 0
|
|||
STATIC_LIB = ${OBJXMPP_STATIC_LIB}
|
||||
|
||||
SRCS = XMPPAuthenticator.m \
|
||||
XMPPCallback.m \
|
||||
XMPPConnection.m \
|
||||
XMPPExceptions.m \
|
||||
XMPPIQ.m \
|
||||
|
|
52
src/XMPPCallback.h
Normal file
52
src/XMPPCallback.h
Normal file
|
@ -0,0 +1,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
|
91
src/XMPPCallback.m
Normal file
91
src/XMPPCallback.m
Normal file
|
@ -0,0 +1,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
|
|
@ -77,11 +77,11 @@
|
|||
XMPPJID *JID;
|
||||
uint16_t port;
|
||||
id <XMPPConnectionDelegate, OFObject> delegate;
|
||||
OFMutableDictionary *callbacks;
|
||||
XMPPAuthenticator *authModule;
|
||||
BOOL needsSession;
|
||||
BOOL encryptionRequired, encrypted;
|
||||
unsigned int lastID;
|
||||
OFString *bindID, *sessionID;
|
||||
XMPPRoster *roster;
|
||||
}
|
||||
|
||||
|
@ -158,6 +158,27 @@
|
|||
*/
|
||||
- (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.
|
||||
*
|
||||
|
|
|
@ -37,6 +37,7 @@
|
|||
#import <ObjOpenSSL/X509Certificate.h>
|
||||
|
||||
#import "XMPPConnection.h"
|
||||
#import "XMPPCallback.h"
|
||||
#import "XMPPSRVLookup.h"
|
||||
#import "XMPPSCRAMAuth.h"
|
||||
#import "XMPPPLAINAuth.h"
|
||||
|
@ -64,6 +65,7 @@
|
|||
sock = [[OFTCPSocket alloc] init];
|
||||
port = 5222;
|
||||
encrypted = NO;
|
||||
callbacks = [[OFMutableDictionary alloc] init];
|
||||
} @catch (id e) {
|
||||
[self release];
|
||||
@throw e;
|
||||
|
@ -83,9 +85,8 @@
|
|||
[domain release];
|
||||
[resource release];
|
||||
[JID release];
|
||||
[callbacks release];
|
||||
[authModule release];
|
||||
[bindID release];
|
||||
[sessionID release];
|
||||
[roster release];
|
||||
|
||||
[super dealloc];
|
||||
|
@ -352,6 +353,46 @@
|
|||
[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++];
|
||||
|
@ -656,14 +697,11 @@
|
|||
- (void)XMPP_handleIQ: (XMPPIQ*)iq
|
||||
{
|
||||
BOOL handled = NO;
|
||||
id <XMPPCallback> callback;
|
||||
|
||||
if ([[iq ID] isEqual: bindID]) {
|
||||
[self XMPP_handleResourceBind: iq];
|
||||
return;
|
||||
}
|
||||
|
||||
if ([[iq ID] isEqual: sessionID]) {
|
||||
[self XMPP_handleSession: iq];
|
||||
if ((callback = [callbacks objectForKey: [iq ID]])) {
|
||||
[callback runWithIQ: iq];
|
||||
[callbacks removeObjectForKey: [iq ID]];
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -799,9 +837,8 @@
|
|||
XMPPIQ *iq;
|
||||
OFXMLElement *bind;
|
||||
|
||||
bindID = [[self generateStanzaID] retain];
|
||||
iq = [XMPPIQ IQWithType: @"set"
|
||||
ID: bindID];
|
||||
ID: [self generateStanzaID]];
|
||||
|
||||
bind = [OFXMLElement elementWithName: @"bind"
|
||||
namespace: XMPP_NS_BIND];
|
||||
|
@ -813,7 +850,9 @@
|
|||
|
||||
[iq addChild: bind];
|
||||
|
||||
[self sendStanza: iq];
|
||||
[self sendIQ: iq
|
||||
withCallbackObject: self
|
||||
selector: @selector(XMPP_handleResourceBind:)];
|
||||
}
|
||||
|
||||
- (void)XMPP_handleResourceBind: (XMPPIQ*)iq
|
||||
|
@ -832,9 +871,6 @@
|
|||
namespace: XMPP_NS_BIND];
|
||||
JID = [[XMPPJID alloc] initWithString: [jidElement stringValue]];
|
||||
|
||||
[bindID release];
|
||||
bindID = nil;
|
||||
|
||||
if (needsSession) {
|
||||
[self XMPP_sendSession];
|
||||
return;
|
||||
|
@ -849,12 +885,13 @@
|
|||
{
|
||||
XMPPIQ *iq;
|
||||
|
||||
sessionID = [[self generateStanzaID] retain];
|
||||
iq = [XMPPIQ IQWithType: @"set"
|
||||
ID: sessionID];
|
||||
ID: [self generateStanzaID]];
|
||||
[iq addChild: [OFXMLElement elementWithName: @"session"
|
||||
namespace: XMPP_NS_SESSION]];
|
||||
[self sendStanza: iq];
|
||||
[self sendIQ: iq
|
||||
withCallbackObject: self
|
||||
selector: @selector(XMPP_handleSession:)];
|
||||
}
|
||||
|
||||
- (void)XMPP_handleSession: (XMPPIQ*)iq
|
||||
|
@ -865,9 +902,6 @@
|
|||
if ([delegate respondsToSelector: @selector(connection:wasBoundToJID:)])
|
||||
[delegate connection: self
|
||||
wasBoundToJID: JID];
|
||||
|
||||
[sessionID release];
|
||||
sessionID = nil;
|
||||
}
|
||||
|
||||
- (OFString*)XMPP_IDNAToASCII: (OFString*)domain_
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue