- Update
This commit is contained in:
1 parent
d281d765ea
commit
5b074a5176
1261 files changed
+199158
-7303
No files matched your search
@@ -0,0 +1,69 @@
|
||||
//
|
||||
// XMPPURI.h
|
||||
// XMPPFramework
|
||||
//
|
||||
// Created by Christopher Ballinger on 5/15/15.
|
||||
// Copyright (c) 2015 Chris Ballinger. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "XMPPJID.h"
|
||||
|
||||
/**
|
||||
* For parsing and creating XMPP URIs RFC5122/XEP-0147
|
||||
* e.g. xmpp:username@domain.com?subscribe
|
||||
* http://www.xmpp.org/extensions/xep-0147.html
|
||||
*/
|
||||
@interface XMPPURI : NSObject
|
||||
|
||||
/**
|
||||
* User JID. e.g. romeo@montague.net
|
||||
* Example: xmpp:romeo@montague.net
|
||||
*/
|
||||
@property (nonatomic, strong, readonly) XMPPJID *jid;
|
||||
|
||||
/**
|
||||
* Account JID. (Optional)
|
||||
* Used to specify an account with which to perform an action.
|
||||
* For example 'guest@example.com' would be the authority portion of
|
||||
* xmpp://guest@example.com/support@example.com?message
|
||||
* so the application would show a dialog with an outgoing message
|
||||
* to support@example.com from the user's account guest@example.com.
|
||||
*/
|
||||
@property (nonatomic, strong, readonly) XMPPJID *accountJID;
|
||||
|
||||
/**
|
||||
* XMPP query action. e.g. subscribe
|
||||
* For example, the query action below would be 'subscribe'
|
||||
* xmpp:romeo@montague.net?subscribe
|
||||
* For full list: http://xmpp.org/registrar/querytypes.html
|
||||
*/
|
||||
@property (nonatomic, strong, readonly) NSString *queryAction;
|
||||
|
||||
/**
|
||||
* XMPP query parameters. e.g. subject=Test
|
||||
*
|
||||
* For example the query parameters for
|
||||
* xmpp:romeo@montague.net?message;subject=Test%20Message;body=Here%27s%20a%20test%20message
|
||||
* would be
|
||||
* {"subject": "Test Message",
|
||||
* "body": "Here's a test message"}
|
||||
*/
|
||||
@property (nonatomic, strong, readonly) NSDictionary *queryParameters;
|
||||
|
||||
/**
|
||||
* Generates URI string from jid, queryAction, and queryParameters
|
||||
* e.g. xmpp:romeo@montague.net?subscribe
|
||||
*/
|
||||
- (NSString*) uriString;
|
||||
|
||||
// Parsing XMPP URIs
|
||||
- (instancetype) initWithURL:(NSURL*)url;
|
||||
- (instancetype) initWithURIString:(NSString*)uriString;
|
||||
|
||||
// Creating XMPP URIs
|
||||
- (instancetype) initWithJID:(XMPPJID*)jid
|
||||
queryAction:(NSString*)queryAction
|
||||
queryParameters:(NSDictionary*)queryParameters;
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,108 @@
|
||||
//
|
||||
// XMPPURI.m
|
||||
// XMPPFramework
|
||||
//
|
||||
// Created by Christopher Ballinger on 5/15/15.
|
||||
// Copyright (c) 2015 Chris Ballinger. All rights reserved.
|
||||
//
|
||||
|
||||
#import "XMPPURI.h"
|
||||
|
||||
@implementation XMPPURI
|
||||
|
||||
- (instancetype) initWithURIString:(NSString *)uriString {
|
||||
if (self = [super init]) {
|
||||
[self parseURIString:uriString];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (instancetype) initWithURL:(NSURL *)url {
|
||||
if (self = [self initWithURIString:url.absoluteString]) {
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (instancetype) initWithJID:(XMPPJID*)jid
|
||||
queryAction:(NSString*)queryAction
|
||||
queryParameters:(NSDictionary*)queryParameters {
|
||||
if (self = [super init]) {
|
||||
_jid = [jid copy];
|
||||
_queryAction = [queryAction copy];
|
||||
_queryParameters = [queryParameters copy];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (NSString*) uriString {
|
||||
NSMutableString *uriString = [NSMutableString stringWithFormat:@"xmpp:%@", self.jid.bare];
|
||||
if (self.queryAction) {
|
||||
[uriString appendFormat:@"?%@", self.queryAction];
|
||||
}
|
||||
NSMutableCharacterSet *allowedCharacterSet = NSCharacterSet.URLQueryAllowedCharacterSet.mutableCopy;
|
||||
[allowedCharacterSet removeCharactersInString:@"'"]; // what other characters should be removed?
|
||||
[self.queryParameters enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL *stop) {
|
||||
NSString *value = [obj stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacterSet];
|
||||
[uriString appendFormat:@";%@=%@", key, value];
|
||||
}];
|
||||
return uriString;
|
||||
}
|
||||
|
||||
- (void) parseURIString:(NSString*)uriString {
|
||||
NSString *authority = nil;
|
||||
// Parse authority component
|
||||
if ([uriString containsString:@"://"]) {
|
||||
NSRange fullRange = NSMakeRange(0, uriString.length);
|
||||
NSRange startRange = [uriString rangeOfString:@"://"];
|
||||
NSUInteger trailingLocation = startRange.location + startRange.length;
|
||||
NSRange trailingRange = NSMakeRange(trailingLocation, uriString.length - trailingLocation);
|
||||
NSRange endRange = [uriString rangeOfString:@"/" options:0 range:trailingRange];
|
||||
NSUInteger authorityLocation = startRange.location + startRange.length;
|
||||
NSRange authorityRange = NSMakeRange(authorityLocation, endRange.location - authorityLocation);
|
||||
authority = [uriString substringWithRange:authorityRange];
|
||||
NSString *stringToRemove = [NSString stringWithFormat:@"://%@/", authority];
|
||||
uriString = [uriString stringByReplacingOccurrencesOfString:stringToRemove withString:@":" options:0 range:fullRange];
|
||||
}
|
||||
if (authority) {
|
||||
_accountJID = [XMPPJID jidWithString:authority];
|
||||
}
|
||||
|
||||
NSArray *uriComponents = [uriString componentsSeparatedByString:@":"];
|
||||
NSString *scheme = nil;
|
||||
NSString *jidString = nil;
|
||||
|
||||
if (uriComponents.count >= 2) {
|
||||
scheme = uriComponents[0];
|
||||
NSString *path = uriComponents[1];
|
||||
if ([path containsString:@"?"]) {
|
||||
NSArray *queryComponents = [path componentsSeparatedByString:@"?"];
|
||||
jidString = queryComponents[0];
|
||||
NSString *query = queryComponents[1];
|
||||
NSArray *queryKeys = [query componentsSeparatedByString:@";"];
|
||||
|
||||
NSMutableDictionary *queryParameters = [NSMutableDictionary dictionaryWithCapacity:queryKeys.count];
|
||||
[queryKeys enumerateObjectsUsingBlock:^(NSString *queryItem, NSUInteger idx, BOOL *stop) {
|
||||
if (idx == 0) {
|
||||
_queryAction = queryItem;
|
||||
} else {
|
||||
NSArray *keyValue = [queryItem componentsSeparatedByString:@"="];
|
||||
if (keyValue.count == 2) {
|
||||
NSString *key = keyValue[0];
|
||||
NSString *value = [keyValue[1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
|
||||
if (key && value) {
|
||||
queryParameters[key] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}];
|
||||
_queryParameters = queryParameters;
|
||||
} else {
|
||||
jidString = path;
|
||||
}
|
||||
}
|
||||
if (jidString) {
|
||||
_jid = [XMPPJID jidWithString:jidString];
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in new issue
Block a user