Skip to content

Commit

Permalink
added support for XEP-0030 with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Andres Canal committed May 27, 2016
1 parent cc83f0b commit 46ff0b0
Show file tree
Hide file tree
Showing 6 changed files with 365 additions and 1 deletion.
31 changes: 31 additions & 0 deletions Extensions/XEP-0030/XMPPServiceDiscovery.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// XMPPServiceDiscovery.h
// Mangosta
//
// Created by Andres Canal on 4/27/16.
// Copyright © 2016 Inaka. All rights reserved.
//

#import <XMPPFramework/XMPPFramework.h>

@class XMPPIDTracker;

@interface XMPPServiceDiscovery : XMPPModule {
XMPPIDTracker *xmppIDTracker;
}

- (void)discoverInformationAboutJID:(XMPPJID *)jid;
- (void)discoverItemsAssociatedWithJID:(XMPPJID *)jid;

@end

@protocol XMPPServiceDiscoveryDelegate

@optional

- (void)xmppServiceDiscovery:(XMPPServiceDiscovery *)sender didDiscoverInformation:(NSArray *)items;
- (void)xmppServiceDiscovery:(XMPPServiceDiscovery *)sender didDiscoverItems:(NSArray *)items;

- (void)xmppServiceDiscovery:(XMPPServiceDiscovery *)sender didFailToDiscover:(XMPPIQ *)iq;

@end
115 changes: 115 additions & 0 deletions Extensions/XEP-0030/XMPPServiceDiscovery.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
//
// XMPPServiceDiscovery.m
// Mangosta
//
// Created by Andres Canal on 4/27/16.
// Copyright © 2016 Inaka. All rights reserved.
//

#define XMLNS_DISCO_ITEMS @"http://jabber.org/protocol/disco#items"
#define XMLNS_DISCO_INFO @"http://jabber.org/protocol/disco#info"
#import "XMPPServiceDiscovery.h"
#import "XMPPIDTracker.h"

@interface XMPPServiceDiscovery()
@property BOOL discoveringInfo;
@end

@implementation XMPPServiceDiscovery

- (BOOL)activate:(XMPPStream *)aXmppStream {

if ([super activate:aXmppStream]) {
xmppIDTracker = [[XMPPIDTracker alloc] initWithDispatchQueue:moduleQueue];

return YES;
}

return NO;
}

- (void)deactivate {
dispatch_block_t block = ^{ @autoreleasepool {

[xmppIDTracker removeAllIDs];
xmppIDTracker = nil;

}};

if (dispatch_get_specific(moduleQueueTag))
block();
else
dispatch_sync(moduleQueue, block);

[super deactivate];
}

- (void) discoverInfoOrItem:(NSString *) infoOrItems jid:(XMPPJID *) jid {

dispatch_block_t block = ^{ @autoreleasepool {
// <iq type='get'
// from='[email protected]/orchard'
// to='shakespeare.lit'
// id='items1'>
// <query xmlns='http://jabber.org/protocol/disco#items'/> // disco#info
// </iq>

NSString *iqID = [XMPPStream generateUUID];
NSXMLElement *query = [NSXMLElement elementWithName:@"query" xmlns: infoOrItems];
XMPPIQ *iq = [XMPPIQ iqWithType:@"get" to:jid elementID:iqID child:query];

[xmppIDTracker addID:iqID
target:self
selector:@selector(handleDiscovery:withInfo:)
timeout:60.0];

[xmppStream sendElement:iq];
}};

if (dispatch_get_specific(moduleQueueTag))
block();
else
dispatch_async(moduleQueue, block);
}

- (void)discoverInformationAboutJID:(XMPPJID *)jid{
self.discoveringInfo = true;
[self discoverInfoOrItem:XMLNS_DISCO_INFO jid:jid];
}


- (void)discoverItemsAssociatedWithJID:(XMPPJID *)jid{
self.discoveringInfo = false;
[self discoverInfoOrItem:XMLNS_DISCO_ITEMS jid:jid];
}

- (void)handleDiscovery:(XMPPIQ *)iq withInfo:(id <XMPPTrackingInfo>)info{

if ([[iq type] isEqualToString:@"result"]){
NSXMLElement *query = [iq elementForName:@"query"];
NSArray *items = [query children];

if (self.discoveringInfo) {
[multicastDelegate xmppServiceDiscovery:self didDiscoverInformation:items];
} else {
[multicastDelegate xmppServiceDiscovery:self didDiscoverItems:items];
}

} else {
[multicastDelegate xmppServiceDiscovery:self didFailToDiscover:iq];
}
}

- (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq
{
NSString *type = [iq type];

if ([type isEqualToString:@"result"] || [type isEqualToString:@"error"])
{
return [xmppIDTracker invokeForID:[iq elementID] withObject:iq];
}

return NO;
}

@end
7 changes: 7 additions & 0 deletions XMPPFramework.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,12 @@ s.subspec 'XEP-0363' do |ss|
ss.prefix_header_contents = "#define HAVE_XMPP_SUBSPEC_#{name.upcase.sub('-', '_')}"
end

s.subspec 'XEP-0030' do |ss|
ss.source_files = 'Extensions/XEP-0030/*.{h,m}'
ss.dependency 'XMPPFramework/Core'
ss.prefix_header_contents = "#define HAVE_XMPP_SUBSPEC_#{name.upcase.sub('-', '_')}"
end

s.subspec 'All' do |ss|
ss.dependency 'XMPPFramework/Core'
ss.dependency 'XMPPFramework/BandwidthMonitor'
Expand Down Expand Up @@ -396,5 +402,6 @@ s.subspec 'All' do |ss|
ss.dependency 'XMPPFramework/XEP-0357'
ss.dependency 'XMPPFramework/XEP-0363'
ss.dependency 'XMPPFramework/XEP-0313'
ss.dependency 'XMPPFramework/XEP-0030'
end
end
5 changes: 4 additions & 1 deletion Xcode/Testing-pod/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ PODS:
- XMPPFramework/XEP-0009
- XMPPFramework/XEP-0012
- XMPPFramework/XEP-0016
- XMPPFramework/XEP-0030
- XMPPFramework/XEP-0045
- XMPPFramework/XEP-0054
- XMPPFramework/XEP-0059
Expand Down Expand Up @@ -91,6 +92,8 @@ PODS:
- XMPPFramework/Core
- XMPPFramework/XEP-0016 (3.6.7):
- XMPPFramework/Core
- XMPPFramework/XEP-0030 (3.6.7):
- XMPPFramework/Core
- XMPPFramework/XEP-0045 (3.6.7):
- XMPPFramework/Core
- XMPPFramework/CoreDataStorage
Expand Down Expand Up @@ -184,6 +187,6 @@ SPEC CHECKSUMS:
CocoaAsyncSocket: a18c75dca4b08723628a0bacca6e94803d90be91
CocoaLumberjack: 97fab7ee5f507fe54445cca7ea80f926729cfd15
KissXML: d19dd6dc65e0dc721ba92b3077b8ebdd240f1c1e
XMPPFramework: 231fe0cf84271a0bdf675254653e5747c6ae7f5a
XMPPFramework: 2e6b563c554f3a4c6565b33981c5b527b9c474c4

COCOAPODS: 0.39.0
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
63F50D9F1C6020A100CA0201 /* EncodeDecodeTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 63F50D9C1C6020A100CA0201 /* EncodeDecodeTest.m */; };
63F50DA01C6020A100CA0201 /* XMPPURITests.m in Sources */ = {isa = PBXBuildFile; fileRef = 63F50D9D1C6020A100CA0201 /* XMPPURITests.m */; };
C141EB1A1CF76CE900513A66 /* XMPPMockStream.m in Sources */ = {isa = PBXBuildFile; fileRef = C141EB191CF76CE900513A66 /* XMPPMockStream.m */; };
C1FF91F61CF8906000C88DEA /* XMPPServiceDiscoveryTests.m in Sources */ = {isa = PBXBuildFile; fileRef = C1FF91F51CF8906000C88DEA /* XMPPServiceDiscoveryTests.m */; };
D92C57A41CC2E0820032DE59 /* XMPPStorageHintTests.m in Sources */ = {isa = PBXBuildFile; fileRef = D92C57A31CC2E0820032DE59 /* XMPPStorageHintTests.m */; };
E0ED56D11CF34D28004C726B /* XMPPHTTPFileUploadTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E0ED56D01CF34D28004C726B /* XMPPHTTPFileUploadTests.m */; };
E0ED56D61CF4D333004C726B /* XMPPMessageArchiveManagementTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E0ED56D51CF4D333004C726B /* XMPPMessageArchiveManagementTests.m */; };
Expand All @@ -29,6 +30,7 @@
B02E767684F690CBF1C43DDA /* Pods_XMPPFrameworkTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_XMPPFrameworkTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
C141EB181CF76CE900513A66 /* XMPPMockStream.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XMPPMockStream.h; sourceTree = "<group>"; };
C141EB191CF76CE900513A66 /* XMPPMockStream.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = XMPPMockStream.m; sourceTree = "<group>"; };
C1FF91F51CF8906000C88DEA /* XMPPServiceDiscoveryTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = XMPPServiceDiscoveryTests.m; sourceTree = "<group>"; };
D92C57A31CC2E0820032DE59 /* XMPPStorageHintTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = XMPPStorageHintTests.m; sourceTree = "<group>"; };
E0ED56D01CF34D28004C726B /* XMPPHTTPFileUploadTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = XMPPHTTPFileUploadTests.m; sourceTree = "<group>"; };
E0ED56D51CF4D333004C726B /* XMPPMessageArchiveManagementTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = XMPPMessageArchiveManagementTests.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -81,6 +83,7 @@
63F50D9D1C6020A100CA0201 /* XMPPURITests.m */,
E0ED56D01CF34D28004C726B /* XMPPHTTPFileUploadTests.m */,
E0ED56D51CF4D333004C726B /* XMPPMessageArchiveManagementTests.m */,
C1FF91F51CF8906000C88DEA /* XMPPServiceDiscoveryTests.m */,
637AE2E81C6AC0D50051BF1F /* XMPPPushTests.swift */,
63F50D971C60208200CA0201 /* Info.plist */,
C141EB181CF76CE900513A66 /* XMPPMockStream.h */,
Expand Down Expand Up @@ -217,6 +220,7 @@
files = (
63F50D9E1C6020A100CA0201 /* CapabilitiesHashingTest.m in Sources */,
63F50D9F1C6020A100CA0201 /* EncodeDecodeTest.m in Sources */,
C1FF91F61CF8906000C88DEA /* XMPPServiceDiscoveryTests.m in Sources */,
E0ED56D11CF34D28004C726B /* XMPPHTTPFileUploadTests.m in Sources */,
E0ED56D61CF4D333004C726B /* XMPPMessageArchiveManagementTests.m in Sources */,
C141EB1A1CF76CE900513A66 /* XMPPMockStream.m in Sources */,
Expand Down
Loading

0 comments on commit 46ff0b0

Please sign in to comment.