Permalink
Browse files

XEP-0066: Out of Band Data

Fixes #196
  • Loading branch information...
1 parent 6027964 commit e110992291e8bf8618606ea874dd156c9404f7ff @ObjColumnist ObjColumnist committed May 18, 2013
@@ -0,0 +1,45 @@
+#import "XMPPIQ.h"
+
+@interface XMPPIQ (XEP_0066)
+
++ (XMPPIQ *)outOfBandDataRequestTo:(XMPPJID *)jid
+ elementID:(NSString *)eid
+ URL:(NSURL *)URL
+ desc:(NSString *)dec;
+
++ (XMPPIQ *)outOfBandDataRequestTo:(XMPPJID *)jid
+ elementID:(NSString *)eid
+ URI:(NSString *)URI
+ desc:(NSString *)dec;
+
+
+- (id)initOutOfBandDataRequestTo:(XMPPJID *)jid
+ elementID:(NSString *)eid
+ URL:(NSURL *)URL
+ desc:(NSString *)dec;
+
+- (id)initOutOfBandDataRequestTo:(XMPPJID *)jid
+ elementID:(NSString *)eid
+ URI:(NSString *)URI
+ desc:(NSString *)dec;
+
+- (void)addOutOfBandURL:(NSURL *)URL desc:(NSString *)desc;
+- (void)addOutOfBandURI:(NSString *)URI desc:(NSString *)desc;
+
+- (XMPPIQ *)generateOutOfBandDataSuccessResponse;
+
+- (XMPPIQ *)generateOutOfBandDataFailureResponse;
+
+- (XMPPIQ *)generateOutOfBandDataRejectResponse;
+
+- (BOOL)isOutOfBandDataRequest;
+- (BOOL)isOutOfBandDataFailureResponse;
+- (BOOL)isOutOfBandDataRejectResponse;
+
+- (BOOL)hasOutOfBandData;
+
+- (NSURL *)outOfBandURL;
+- (NSString *)outOfBandURI;
+- (NSString *)outOfBandDesc;
+
+@end
@@ -0,0 +1,229 @@
+#import "XMPPIQ+XEP_0066.h"
+#import "NSXMLElement+XMPP.h"
+
+#if ! __has_feature(objc_arc)
+#warning This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
+#endif
+
+#define NAME_OUT_OF_BAND @"query"
+#define XMLNS_OUT_OF_BAND @"jabber:iq:oob"
+
+@implementation XMPPIQ (XEP_0066)
+
+
++ (XMPPIQ *)outOfBandDataRequestTo:(XMPPJID *)jid
+ elementID:(NSString *)eid
+ URL:(NSURL *)URL
+ desc:(NSString *)desc
+{
+ return [[XMPPIQ alloc] initOutOfBandDataRequestTo:jid
+ elementID:eid
+ URL:URL
+ desc:desc];
+}
+
++ (XMPPIQ *)outOfBandDataRequestTo:(XMPPJID *)jid
+ elementID:(NSString *)eid
+ URI:(NSString *)URI
+ desc:(NSString *)desc
+{
+ return [[XMPPIQ alloc] initOutOfBandDataRequestTo:jid
+ elementID:eid
+ URI:URI
+ desc:desc];
+}
+
+
+- (id)initOutOfBandDataRequestTo:(XMPPJID *)jid
+ elementID:(NSString *)eid
+ URL:(NSURL *)URL
+ desc:(NSString *)desc
+{
+ if((self = [self initWithType:@"set" to:jid elementID:eid]))
+ {
+ [self addOutOfBandURL:URL desc:desc];
+ }
+
+ return self;
+}
+
+- (id)initOutOfBandDataRequestTo:(XMPPJID *)jid
+ elementID:(NSString *)eid
+ URI:(NSString *)URI
+ desc:(NSString *)desc
+{
+ if((self = [self initWithType:@"set" to:jid elementID:eid]))
+ {
+ [self addOutOfBandURI:URI desc:desc];
+ }
+
+ return self;
+}
+
+- (void)addOutOfBandURL:(NSURL *)URL desc:(NSString *)desc
+{
+ NSXMLElement *outOfBand = [NSXMLElement elementWithName:NAME_OUT_OF_BAND xmlns:XMLNS_OUT_OF_BAND];
+
+ if([[URL path] length])
+ {
+ NSXMLElement *URLElement = [NSXMLElement elementWithName:@"url" stringValue:[URL path]];
+ [outOfBand addChild:URLElement];
+ }
+
+ if([desc length])
+ {
+ NSXMLElement *descElement = [NSXMLElement elementWithName:@"desc" stringValue:desc];
+ [outOfBand addChild:descElement];
+ }
+
+ [self addChild:outOfBand];
+}
+
+- (void)addOutOfBandURI:(NSString *)URI desc:(NSString *)desc
+{
+ NSXMLElement *outOfBand = [NSXMLElement elementWithName:NAME_OUT_OF_BAND xmlns:XMLNS_OUT_OF_BAND];
+
+ if([URI length])
+ {
+ NSXMLElement *URLElement = [NSXMLElement elementWithName:@"url" stringValue:URI];
+ [outOfBand addChild:URLElement];
+ }
+
+ if([desc length])
+ {
+ NSXMLElement *descElement = [NSXMLElement elementWithName:@"desc" stringValue:desc];
+ [outOfBand addChild:descElement];
+ }
+
+ [self addChild:outOfBand];
+}
+
+- (XMPPIQ *)generateOutOfBandDataSuccessResponse
+{
+ return [XMPPIQ iqWithType:@"result" to:[self from] elementID:[self elementID]];
+}
+
+- (XMPPIQ *)generateOutOfBandDataFailureResponse
+{
+ XMPPIQ *outOfBandDataFailureResponse = [XMPPIQ iqWithType:@"error" to:[self from] elementID:[self elementID]];
+
+ [outOfBandDataFailureResponse addOutOfBandURI:[self outOfBandURI] desc:[self outOfBandDesc]];
+
+ NSXMLElement *errorElement = [NSXMLElement elementWithName:@"error"];
+ [errorElement addAttributeWithName:@"code" stringValue:@"404"];
+ [errorElement addAttributeWithName:@"type" stringValue:@"cancel"];
+
+ NSXMLElement *itemNotFoundElement = [NSXMLElement elementWithName:@"item-not-found" xmlns:@"rn:ietf:params:xml:ns:xmpp-stanzas"];
+ [errorElement addChild:itemNotFoundElement];
+
+ [outOfBandDataFailureResponse addChild:errorElement];
+
+
+ return outOfBandDataFailureResponse;
+}
+
+- (XMPPIQ *)generateOutOfBandDataRejectResponse
+{
+ XMPPIQ *outOfBandDataRejectResponse = [XMPPIQ iqWithType:@"error" to:[self from] elementID:[self elementID]];
+
+ [outOfBandDataRejectResponse addOutOfBandURI:[self outOfBandURI] desc:[self outOfBandDesc]];
+
+ NSXMLElement *errorElement = [NSXMLElement elementWithName:@"error"];
+ [errorElement addAttributeWithName:@"code" stringValue:@"406"];
+ [errorElement addAttributeWithName:@"type" stringValue:@"modify"];
+
+ NSXMLElement *notAcceptableElement = [NSXMLElement elementWithName:@"not-acceptable" xmlns:@"rn:ietf:params:xml:ns:xmpp-stanzas"];
+ [errorElement addChild:notAcceptableElement];
+
+ [outOfBandDataRejectResponse addChild:errorElement];
+
+ return outOfBandDataRejectResponse;
+}
+
+- (BOOL)isOutOfBandDataRequest
+{
+ if([self hasOutOfBandData] && [self isSetIQ])
+ {
+ return YES;
+ }else{
+ return NO;
+ }
+}
+
+- (BOOL)isOutOfBandDataFailureResponse
+{
+ NSXMLElement *errorElement = [self elementForName:@"error"];
+
+ NSUInteger errorCode = [errorElement attributeIntegerValueForName:@"code"];
+ NSString *errorType = [errorElement attributeStringValueForName:@"type"];
+
+ if([self hasOutOfBandData] && [self isErrorIQ] && errorCode == 404 && [errorType isEqualToString:@"cancel"])
+ {
+ return YES;
+ }else{
+ return NO;
+ }
+}
+
+- (BOOL)isOutOfBandDataRejectResponse
+{
+ NSXMLElement *errorElement = [self elementForName:@"error"];
+
+ NSUInteger errorCode = [errorElement attributeIntegerValueForName:@"code"];
+ NSString *errorType = [errorElement attributeStringValueForName:@"type"];
+
+ if([self hasOutOfBandData] && [self isErrorIQ] && errorCode == 406 && [errorType isEqualToString:@"modify"])
+ {
+ return YES;
+ }else{
+ return NO;
+ }
+}
+
+- (BOOL)hasOutOfBandData
+{
+ return ([self elementForName:NAME_OUT_OF_BAND xmlns:XMLNS_OUT_OF_BAND] ? YES : NO);
+}
+
+- (NSURL *)outOfBandURL
+{
+ NSURL *URL = nil;
+
+ NSXMLElement *outOfBand = [self elementForName:NAME_OUT_OF_BAND xmlns:XMLNS_OUT_OF_BAND];
+
+ NSXMLElement *URLElement = [outOfBand elementForName:@"url"];
+
+ NSString *URLString = [URLElement stringValue];
+
+ if([URLString length])
+ {
+ URL = [NSURL URLWithString:URLString];
+ }
+
+ return URL;
+
+}
+
+- (NSString *)outOfBandURI
+{
+ NSXMLElement *outOfBand = [self elementForName:NAME_OUT_OF_BAND xmlns:XMLNS_OUT_OF_BAND];
+
+ NSXMLElement *URLElement = [outOfBand elementForName:@"url"];
+
+ NSString *URI= [URLElement stringValue];
+
+ return URI;
+}
+
+- (NSString *)outOfBandDesc
+{
+ NSXMLElement *outOfBand = [self elementForName:NAME_OUT_OF_BAND xmlns:XMLNS_OUT_OF_BAND];
+
+ NSXMLElement *descElement = [outOfBand elementForName:@"desc"];
+
+ NSString *desc = [descElement stringValue];
+
+ return desc;
+}
+
+@end
@@ -0,0 +1,14 @@
+#import "XMPPMessage.h"
+
+@interface XMPPMessage (XEP_0066)
+
+- (void)addOutOfBandURL:(NSURL *)URL desc:(NSString *)desc;
+- (void)addOutOfBandURI:(NSString *)URI desc:(NSString *)desc;
+
+- (BOOL)hasOutOfBandData;
+
+- (NSURL *)outOfBandURL;
+- (NSString *)outOfBandURI;
+- (NSString *)outOfBandDesc;
+
+@end
@@ -0,0 +1,97 @@
+#import "XMPPMessage+XEP_0066.h"
+#import "NSXMLElement+XMPP.h"
+
+#if ! __has_feature(objc_arc)
+#warning This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
+#endif
+
+#define NAME_OUT_OF_BAND @"x"
+#define XMLNS_OUT_OF_BAND @"jabber:iq:oob"
+
+@implementation XMPPMessage (XEP_0066)
+
+- (void)addOutOfBandURL:(NSURL *)URL desc:(NSString *)desc
+{
+ NSXMLElement *outOfBand = [NSXMLElement elementWithName:NAME_OUT_OF_BAND xmlns:XMLNS_OUT_OF_BAND];
+
+ if([[URL path] length])
+ {
+ NSXMLElement *URLElement = [NSXMLElement elementWithName:@"url" stringValue:[URL path]];
+ [outOfBand addChild:URLElement];
+ }
+
+ if([desc length])
+ {
+ NSXMLElement *descElement = [NSXMLElement elementWithName:@"desc" stringValue:desc];
+ [outOfBand addChild:descElement];
+ }
+
+ [self addChild:outOfBand];
+}
+
+- (void)addOutOfBandURI:(NSString *)URI desc:(NSString *)desc
+{
+ NSXMLElement *outOfBand = [NSXMLElement elementWithName:NAME_OUT_OF_BAND xmlns:XMLNS_OUT_OF_BAND];
+
+ if([URI length])
+ {
+ NSXMLElement *URLElement = [NSXMLElement elementWithName:@"url" stringValue:URI];
+ [outOfBand addChild:URLElement];
+ }
+
+ if([desc length])
+ {
+ NSXMLElement *descElement = [NSXMLElement elementWithName:@"desc" stringValue:desc];
+ [outOfBand addChild:descElement];
+ }
+
+ [self addChild:outOfBand];
+}
+
+- (BOOL)hasOutOfBandData
+{
+ return ([self elementForName:NAME_OUT_OF_BAND xmlns:XMLNS_OUT_OF_BAND] ? YES : NO);
+}
+
+- (NSURL *)outOfBandURL
+{
+ NSURL *URL = nil;
+
+ NSXMLElement *outOfBand = [self elementForName:NAME_OUT_OF_BAND xmlns:XMLNS_OUT_OF_BAND];
+
+ NSXMLElement *URLElement = [outOfBand elementForName:@"url"];
+
+ NSString *URLString = [URLElement stringValue];
+
+ if([URLString length])
+ {
+ URL = [NSURL URLWithString:URLString];
+ }
+
+ return URL;
+
+}
+
+- (NSString *)outOfBandURI
+{
+ NSXMLElement *outOfBand = [self elementForName:NAME_OUT_OF_BAND xmlns:XMLNS_OUT_OF_BAND];
+
+ NSXMLElement *URLElement = [outOfBand elementForName:@"url"];
+
+ NSString *URI= [URLElement stringValue];
+
+ return URI;
+}
+
+- (NSString *)outOfBandDesc
+{
+ NSXMLElement *outOfBand = [self elementForName:NAME_OUT_OF_BAND xmlns:XMLNS_OUT_OF_BAND];
+
+ NSXMLElement *descElement = [outOfBand elementForName:@"desc"];
+
+ NSString *desc = [descElement stringValue];
+
+ return desc;
+}
+
+@end

0 comments on commit e110992

Please sign in to comment.