Permalink
Browse files

XMPPCapabilities autoFetchMyServerCapabilities

Some XEPs require you to check if the server supports them before attempting to use them, setting autoFetchMyServerCapabilities to YES means the server is automatically queried upon authentication.
  • Loading branch information...
1 parent dfaf53f commit b4cc97065af405635a33b89dae14c165436a1f4e @ObjColumnist ObjColumnist committed May 20, 2013
Showing with 57 additions and 29 deletions.
  1. +11 −1 Extensions/XEP-0115/XMPPCapabilities.h
  2. +46 −28 Extensions/XEP-0115/XMPPCapabilities.m
@@ -30,6 +30,7 @@
BOOL autoFetchHashedCapabilities;
BOOL autoFetchNonHashedCapabilities;
+ BOOL autoFetchMyServerCapabilities;
NSTimeInterval capabilitiesRequestTimeout;
@@ -72,6 +73,15 @@
@property (assign) BOOL autoFetchNonHashedCapabilities;
/**
+ * Auto fetch the capabilities of the server upon authentication.
+ * This uses the non hashed approach outlined in XEP-0030: Service Discovery.
+ *
+ * The default value is NO.
+**/
+
+@property (assign) BOOL autoFetchMyServerCapabilities;
+
+/**
* Manually fetch the capabilities for the given jid.
*
* The jid must be a full jid (user@domain/resource) or a domain JID (domain without user or resource).
@@ -342,7 +352,7 @@
* Duplicate features are automatically discarded
* For more control over your capablities use xmppCapabilities:collectingMyCapabilities:
**/
-- (NSArray *)featuresForXMPPCapabilities:(XMPPCapabilities *)sender;
+- (NSArray *)myFeaturesForXMPPCapabilities:(XMPPCapabilities *)sender;
/**
* Invoked when capabilities have been discovered for an available JID.
@@ -81,6 +81,7 @@ @implementation XMPPCapabilities
@dynamic xmppCapabilitiesStorage;
@dynamic autoFetchHashedCapabilities;
@dynamic autoFetchNonHashedCapabilities;
+@dynamic autoFetchMyServerCapabilities;
- (id)init
{
@@ -143,34 +144,13 @@ - (id)initWithCapabilitiesStorage:(id <XMPPCapabilitiesStorage>)storage dispatch
autoFetchHashedCapabilities = YES;
autoFetchNonHashedCapabilities = NO;
+ autoFetchMyServerCapabilities = NO;
}
return self;
}
-- (BOOL)activate:(XMPPStream *)aXmppStream
-{
- if ([super activate:aXmppStream])
- {
- // Custom code goes here (if needed)
-
- return YES;
- }
-
- return NO;
-}
-
-- (void)deactivate
-{
- // Custom code goes here (if needed)
-
- [super deactivate];
-}
-
- (void)dealloc
-{
-
-
-
+{
for (GCDTimerWrapper *timerWrapper in discoTimerJidDict)
{
[timerWrapper cancel];
@@ -243,6 +223,34 @@ - (void)setAutoFetchNonHashedCapabilities:(BOOL)flag
dispatch_async(moduleQueue, block);
}
+- (BOOL)autoFetchMyServerCapabilities
+{
+ __block BOOL result = NO;
+
+ dispatch_block_t block = ^{
+ result = autoFetchMyServerCapabilities;
+ };
+
+ if (dispatch_get_specific(moduleQueueTag))
+ block();
+ else
+ dispatch_sync(moduleQueue, block);
+
+ return result;
+}
+
+- (void)setAutoFetchMyServerCapabilities:(BOOL)flag
+{
+ dispatch_block_t block = ^{
+ autoFetchMyServerCapabilities = flag;
+ };
+
+ if (dispatch_get_specific(moduleQueueTag))
+ block();
+ else
+ dispatch_async(moduleQueue, block);
+}
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark Hashing
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -665,10 +673,10 @@ - (void)collectMyCapabilities
// Now prompt the delegates to add any additional features.
SEL collectingMyCapabilitiesSelector = @selector(xmppCapabilities:collectingMyCapabilities:);
- SEL featuresForXMPPCapabilitiesSelector = @selector(featuresForXMPPCapabilities:);
+ SEL myFeaturesForXMPPCapabilitiesSelector = @selector(myFeaturesForXMPPCapabilities:);
if (![multicastDelegate hasDelegateThatRespondsToSelector:collectingMyCapabilitiesSelector]
- && ![multicastDelegate hasDelegateThatRespondsToSelector:featuresForXMPPCapabilitiesSelector])
+ && ![multicastDelegate hasDelegateThatRespondsToSelector:myFeaturesForXMPPCapabilitiesSelector])
{
// None of the delegates implement the method.
// Use a shortcut.
@@ -681,7 +689,7 @@ - (void)collectMyCapabilities
// This must be done serially to allow them to alter the element in a thread-safe manner.
GCDMulticastDelegateEnumerator *collectingMyCapabilitiesDelegateEnumerator = [multicastDelegate delegateEnumerator];
- GCDMulticastDelegateEnumerator *featuresForXMPPCapabilitiesDelegateEnumerator = [multicastDelegate delegateEnumerator];
+ GCDMulticastDelegateEnumerator *myFeaturesForXMPPCapabilitiesDelegateEnumerator = [multicastDelegate delegateEnumerator];
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
@@ -699,11 +707,11 @@ - (void)collectMyCapabilities
}});
}
- while ([featuresForXMPPCapabilitiesDelegateEnumerator getNextDelegate:&del delegateQueue:&dq forSelector:featuresForXMPPCapabilitiesSelector])
+ while ([myFeaturesForXMPPCapabilitiesDelegateEnumerator getNextDelegate:&del delegateQueue:&dq forSelector:myFeaturesForXMPPCapabilitiesSelector])
{
dispatch_sync(dq, ^{ @autoreleasepool {
- NSArray *features = [del featuresForXMPPCapabilities:self];
+ NSArray *features = [del myFeaturesForXMPPCapabilities:self];
for(NSString *feature in features){
@@ -1423,6 +1431,16 @@ - (void)xmppStreamDidConnect:(XMPPStream *)sender
}
}
+- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender
+{
+ if (autoFetchMyServerCapabilities)
+ {
+ XMPPJID *myJID = [xmppStream myJID];
+ XMPPJID *myServerJID = [XMPPJID jidWithUser:nil domain:[myJID domain] resource:nil];
+ [self fetchCapabilitiesForJID:myServerJID];
+ }
+}
+
- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence
{
// This method is invoked on the moduleQueue.

0 comments on commit b4cc970

Please sign in to comment.