|
| 1 | +#import "XMPPIQ+XEP_0066.h" |
| 2 | +#import "NSXMLElement+XMPP.h" |
| 3 | + |
| 4 | +#if ! __has_feature(objc_arc) |
| 5 | +#warning This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC). |
| 6 | +#endif |
| 7 | + |
| 8 | +#define NAME_OUT_OF_BAND @"query" |
| 9 | +#define XMLNS_OUT_OF_BAND @"jabber:iq:oob" |
| 10 | + |
| 11 | +@implementation XMPPIQ (XEP_0066) |
| 12 | + |
| 13 | + |
| 14 | ++ (XMPPIQ *)outOfBandDataRequestTo:(XMPPJID *)jid |
| 15 | + elementID:(NSString *)eid |
| 16 | + URL:(NSURL *)URL |
| 17 | + desc:(NSString *)desc |
| 18 | +{ |
| 19 | + return [[XMPPIQ alloc] initOutOfBandDataRequestTo:jid |
| 20 | + elementID:eid |
| 21 | + URL:URL |
| 22 | + desc:desc]; |
| 23 | +} |
| 24 | + |
| 25 | ++ (XMPPIQ *)outOfBandDataRequestTo:(XMPPJID *)jid |
| 26 | + elementID:(NSString *)eid |
| 27 | + URI:(NSString *)URI |
| 28 | + desc:(NSString *)desc |
| 29 | +{ |
| 30 | + return [[XMPPIQ alloc] initOutOfBandDataRequestTo:jid |
| 31 | + elementID:eid |
| 32 | + URI:URI |
| 33 | + desc:desc]; |
| 34 | +} |
| 35 | + |
| 36 | + |
| 37 | +- (id)initOutOfBandDataRequestTo:(XMPPJID *)jid |
| 38 | + elementID:(NSString *)eid |
| 39 | + URL:(NSURL *)URL |
| 40 | + desc:(NSString *)desc |
| 41 | +{ |
| 42 | + if((self = [self initWithType:@"set" to:jid elementID:eid])) |
| 43 | + { |
| 44 | + [self addOutOfBandURL:URL desc:desc]; |
| 45 | + } |
| 46 | + |
| 47 | + return self; |
| 48 | +} |
| 49 | + |
| 50 | +- (id)initOutOfBandDataRequestTo:(XMPPJID *)jid |
| 51 | + elementID:(NSString *)eid |
| 52 | + URI:(NSString *)URI |
| 53 | + desc:(NSString *)desc |
| 54 | +{ |
| 55 | + if((self = [self initWithType:@"set" to:jid elementID:eid])) |
| 56 | + { |
| 57 | + [self addOutOfBandURI:URI desc:desc]; |
| 58 | + } |
| 59 | + |
| 60 | + return self; |
| 61 | +} |
| 62 | + |
| 63 | +- (void)addOutOfBandURL:(NSURL *)URL desc:(NSString *)desc |
| 64 | +{ |
| 65 | + NSXMLElement *outOfBand = [NSXMLElement elementWithName:NAME_OUT_OF_BAND xmlns:XMLNS_OUT_OF_BAND]; |
| 66 | + |
| 67 | + if([[URL path] length]) |
| 68 | + { |
| 69 | + NSXMLElement *URLElement = [NSXMLElement elementWithName:@"url" stringValue:[URL path]]; |
| 70 | + [outOfBand addChild:URLElement]; |
| 71 | + } |
| 72 | + |
| 73 | + if([desc length]) |
| 74 | + { |
| 75 | + NSXMLElement *descElement = [NSXMLElement elementWithName:@"desc" stringValue:desc]; |
| 76 | + [outOfBand addChild:descElement]; |
| 77 | + } |
| 78 | + |
| 79 | + [self addChild:outOfBand]; |
| 80 | +} |
| 81 | + |
| 82 | +- (void)addOutOfBandURI:(NSString *)URI desc:(NSString *)desc |
| 83 | +{ |
| 84 | + NSXMLElement *outOfBand = [NSXMLElement elementWithName:NAME_OUT_OF_BAND xmlns:XMLNS_OUT_OF_BAND]; |
| 85 | + |
| 86 | + if([URI length]) |
| 87 | + { |
| 88 | + NSXMLElement *URLElement = [NSXMLElement elementWithName:@"url" stringValue:URI]; |
| 89 | + [outOfBand addChild:URLElement]; |
| 90 | + } |
| 91 | + |
| 92 | + if([desc length]) |
| 93 | + { |
| 94 | + NSXMLElement *descElement = [NSXMLElement elementWithName:@"desc" stringValue:desc]; |
| 95 | + [outOfBand addChild:descElement]; |
| 96 | + } |
| 97 | + |
| 98 | + [self addChild:outOfBand]; |
| 99 | +} |
| 100 | + |
| 101 | +- (XMPPIQ *)generateOutOfBandDataSuccessResponse |
| 102 | +{ |
| 103 | + return [XMPPIQ iqWithType:@"result" to:[self from] elementID:[self elementID]]; |
| 104 | +} |
| 105 | + |
| 106 | +- (XMPPIQ *)generateOutOfBandDataFailureResponse |
| 107 | +{ |
| 108 | + XMPPIQ *outOfBandDataFailureResponse = [XMPPIQ iqWithType:@"error" to:[self from] elementID:[self elementID]]; |
| 109 | + |
| 110 | + [outOfBandDataFailureResponse addOutOfBandURI:[self outOfBandURI] desc:[self outOfBandDesc]]; |
| 111 | + |
| 112 | + NSXMLElement *errorElement = [NSXMLElement elementWithName:@"error"]; |
| 113 | + [errorElement addAttributeWithName:@"code" stringValue:@"404"]; |
| 114 | + [errorElement addAttributeWithName:@"type" stringValue:@"cancel"]; |
| 115 | + |
| 116 | + NSXMLElement *itemNotFoundElement = [NSXMLElement elementWithName:@"item-not-found" xmlns:@"rn:ietf:params:xml:ns:xmpp-stanzas"]; |
| 117 | + [errorElement addChild:itemNotFoundElement]; |
| 118 | + |
| 119 | + [outOfBandDataFailureResponse addChild:errorElement]; |
| 120 | + |
| 121 | + |
| 122 | + return outOfBandDataFailureResponse; |
| 123 | +} |
| 124 | + |
| 125 | +- (XMPPIQ *)generateOutOfBandDataRejectResponse |
| 126 | +{ |
| 127 | + XMPPIQ *outOfBandDataRejectResponse = [XMPPIQ iqWithType:@"error" to:[self from] elementID:[self elementID]]; |
| 128 | + |
| 129 | + [outOfBandDataRejectResponse addOutOfBandURI:[self outOfBandURI] desc:[self outOfBandDesc]]; |
| 130 | + |
| 131 | + NSXMLElement *errorElement = [NSXMLElement elementWithName:@"error"]; |
| 132 | + [errorElement addAttributeWithName:@"code" stringValue:@"406"]; |
| 133 | + [errorElement addAttributeWithName:@"type" stringValue:@"modify"]; |
| 134 | + |
| 135 | + NSXMLElement *notAcceptableElement = [NSXMLElement elementWithName:@"not-acceptable" xmlns:@"rn:ietf:params:xml:ns:xmpp-stanzas"]; |
| 136 | + [errorElement addChild:notAcceptableElement]; |
| 137 | + |
| 138 | + [outOfBandDataRejectResponse addChild:errorElement]; |
| 139 | + |
| 140 | + return outOfBandDataRejectResponse; |
| 141 | +} |
| 142 | + |
| 143 | +- (BOOL)isOutOfBandDataRequest |
| 144 | +{ |
| 145 | + if([self hasOutOfBandData] && [self isSetIQ]) |
| 146 | + { |
| 147 | + return YES; |
| 148 | + }else{ |
| 149 | + return NO; |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +- (BOOL)isOutOfBandDataFailureResponse |
| 154 | +{ |
| 155 | + NSXMLElement *errorElement = [self elementForName:@"error"]; |
| 156 | + |
| 157 | + NSUInteger errorCode = [errorElement attributeIntegerValueForName:@"code"]; |
| 158 | + NSString *errorType = [errorElement attributeStringValueForName:@"type"]; |
| 159 | + |
| 160 | + if([self hasOutOfBandData] && [self isErrorIQ] && errorCode == 404 && [errorType isEqualToString:@"cancel"]) |
| 161 | + { |
| 162 | + return YES; |
| 163 | + }else{ |
| 164 | + return NO; |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +- (BOOL)isOutOfBandDataRejectResponse |
| 169 | +{ |
| 170 | + NSXMLElement *errorElement = [self elementForName:@"error"]; |
| 171 | + |
| 172 | + NSUInteger errorCode = [errorElement attributeIntegerValueForName:@"code"]; |
| 173 | + NSString *errorType = [errorElement attributeStringValueForName:@"type"]; |
| 174 | + |
| 175 | + if([self hasOutOfBandData] && [self isErrorIQ] && errorCode == 406 && [errorType isEqualToString:@"modify"]) |
| 176 | + { |
| 177 | + return YES; |
| 178 | + }else{ |
| 179 | + return NO; |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +- (BOOL)hasOutOfBandData |
| 184 | +{ |
| 185 | + return ([self elementForName:NAME_OUT_OF_BAND xmlns:XMLNS_OUT_OF_BAND] ? YES : NO); |
| 186 | +} |
| 187 | + |
| 188 | +- (NSURL *)outOfBandURL |
| 189 | +{ |
| 190 | + NSURL *URL = nil; |
| 191 | + |
| 192 | + NSXMLElement *outOfBand = [self elementForName:NAME_OUT_OF_BAND xmlns:XMLNS_OUT_OF_BAND]; |
| 193 | + |
| 194 | + NSXMLElement *URLElement = [outOfBand elementForName:@"url"]; |
| 195 | + |
| 196 | + NSString *URLString = [URLElement stringValue]; |
| 197 | + |
| 198 | + if([URLString length]) |
| 199 | + { |
| 200 | + URL = [NSURL URLWithString:URLString]; |
| 201 | + } |
| 202 | + |
| 203 | + return URL; |
| 204 | + |
| 205 | +} |
| 206 | + |
| 207 | +- (NSString *)outOfBandURI |
| 208 | +{ |
| 209 | + NSXMLElement *outOfBand = [self elementForName:NAME_OUT_OF_BAND xmlns:XMLNS_OUT_OF_BAND]; |
| 210 | + |
| 211 | + NSXMLElement *URLElement = [outOfBand elementForName:@"url"]; |
| 212 | + |
| 213 | + NSString *URI= [URLElement stringValue]; |
| 214 | + |
| 215 | + return URI; |
| 216 | +} |
| 217 | + |
| 218 | +- (NSString *)outOfBandDesc |
| 219 | +{ |
| 220 | + NSXMLElement *outOfBand = [self elementForName:NAME_OUT_OF_BAND xmlns:XMLNS_OUT_OF_BAND]; |
| 221 | + |
| 222 | + NSXMLElement *descElement = [outOfBand elementForName:@"desc"]; |
| 223 | + |
| 224 | + NSString *desc = [descElement stringValue]; |
| 225 | + |
| 226 | + return desc; |
| 227 | +} |
| 228 | + |
| 229 | +@end |
0 commit comments