/
XMPPRegistration.m
237 lines (197 loc) · 7.64 KB
/
XMPPRegistration.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
//
// Created by Jonathon Staff on 10/11/14.
// Copyright (c) 2014 Jonathon Staff. All rights reserved.
//
#import "XMPPRegistration.h"
#import "XMPPStream.h"
#import "XMPPIDTracker.h"
#import "XMPPIQ.h"
#import "NSXMLElement+XMPP.h"
NSString *const XMPPRegistrationErrorDomain = @"XMPPRegistrationErrorDomain";
@implementation XMPPRegistration
- (void)didActivate
{
xmppIDTracker = [[XMPPIDTracker alloc] initWithStream:xmppStream dispatchQueue:moduleQueue];
}
- (void)willDeactivate
{
[xmppIDTracker removeAllIDs];
xmppIDTracker = nil;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark Public API
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* This method provides functionality of XEP-0077 3.3 User Changes Password.
*
* @link {http://xmpp.org/extensions/xep-0077.html#usecases-changepw}
*
* Example 18. Password Change
*
* <iq type='set' to='shakespeare.lit' id='change1'>
* <query xmlns='jabber:iq:register'>
* <username>bill</username>
* <password>newpass</password>
* </query>
* </iq>
*
*/
- (BOOL)changePassword:(NSString *)newPassword
{
if (![xmppStream isAuthenticated])
return NO; // You must be authenticated in order to change your password
dispatch_block_t block = ^{
@autoreleasepool {
NSString *toStr = self->xmppStream.myJID.domain;
NSXMLElement *query = [NSXMLElement elementWithName:@"query" xmlns:@"jabber:iq:register"];
NSXMLElement *username = [NSXMLElement elementWithName:@"username"
stringValue:self->xmppStream.myJID.user];
NSXMLElement *password = [NSXMLElement elementWithName:@"password"
stringValue:newPassword];
[query addChild:username];
[query addChild:password];
XMPPIQ *iq = [XMPPIQ iqWithType:@"set"
to:[XMPPJID jidWithString:toStr]
elementID:[self->xmppStream generateUUID]
child:query];
[self->xmppIDTracker addID:[iq elementID]
target:self
selector:@selector(handlePasswordChangeQueryIQ:withInfo:)
timeout:60];
[self->xmppStream sendElement:iq];
}
};
if (dispatch_get_specific(moduleQueueTag))
block();
else
dispatch_async(moduleQueue, block);
return YES;
}
/**
* This method provides functionality of XEP-0077 3.2 Entity Cancels an Existing Registration.
*
* @link {http://xmpp.org/extensions/xep-0077.html#usecases-cancel}
*
* <iq type='set' from='bill@shakespeare.lit/globe' id='unreg1'>
* <query xmlns='jabber:iq:register'>
* <remove/>
* </query>
* </iq>
*
*/
- (BOOL)cancelRegistration
{
return [self cancelRegistrationUsingPassword:nil];
}
/**
* Same as cancelRegistration. Handling authentication challenges is not yet implemented.
*/
- (BOOL)cancelRegistrationUsingPassword:(NSString *)password
{
// TODO: Handle the scenario of using password
dispatch_block_t block = ^{
@autoreleasepool {
NSXMLElement *remove = [NSXMLElement elementWithName:@"remove"];
NSXMLElement *query = [NSXMLElement elementWithName:@"query" xmlns:@"jabber:iq:register"];
[query addChild:remove];
XMPPIQ *iq = [XMPPIQ iqWithType:@"set"
elementID:[self->xmppStream generateUUID]
child:query];
[self->xmppIDTracker addElement:iq
target:self
selector:@selector(handleRegistrationCancelQueryIQ:withInfo:)
timeout:60];
[self->xmppStream sendElement:iq];
}
};
if (dispatch_get_specific(moduleQueueTag))
block();
else
dispatch_async(moduleQueue, block);
return YES;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - XMPPIDTracker
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* This method handles the response received (or not received) after calling changePassword.
*/
- (void)handlePasswordChangeQueryIQ:(XMPPIQ *)iq withInfo:(XMPPBasicTrackingInfo *)info
{
dispatch_block_t block = ^{
@autoreleasepool {
NSXMLElement *errorElem = [iq elementForName:@"error"];
if (errorElem) {
NSString *errMsg = [[errorElem children] componentsJoinedByString:@", "];
NSInteger errCode = [errorElem attributeIntegerValueForName:@"code"
withDefaultValue:-1];
NSDictionary *errInfo = @{NSLocalizedDescriptionKey : errMsg};
NSError *err = [NSError errorWithDomain:XMPPRegistrationErrorDomain
code:errCode
userInfo:errInfo];
[self->multicastDelegate passwordChangeFailed:self
withError:err];
return;
}
NSString *type = [iq type];
if ([type isEqualToString:@"result"]) {
[self->multicastDelegate passwordChangeSuccessful:self];
} else {
// this should be impossible to reach, but just for safety's sake...
[self->multicastDelegate passwordChangeFailed:self
withError:nil];
}
}
};
if (dispatch_get_specific(moduleQueueTag))
block();
else
dispatch_async(moduleQueue, block);
}
/**
* This method handles the response received (or not received) after calling cancelRegistration.
*/
- (void)handleRegistrationCancelQueryIQ:(XMPPIQ *)iq withInfo:(XMPPBasicTrackingInfo *)info
{
dispatch_block_t block = ^{
@autoreleasepool {
NSXMLElement *errorElem = [iq elementForName:@"error"];
if (errorElem) {
NSString *errMsg = [[errorElem children] componentsJoinedByString:@", "];
NSInteger errCode = [errorElem attributeIntegerValueForName:@"code"
withDefaultValue:-1];
NSDictionary *errInfo = @{NSLocalizedDescriptionKey : errMsg};
NSError *err = [NSError errorWithDomain:XMPPRegistrationErrorDomain
code:errCode
userInfo:errInfo];
[self->multicastDelegate cancelRegistrationFailed:self
withError:err];
return;
}
NSString *type = [iq type];
if ([type isEqualToString:@"result"]) {
[self->multicastDelegate cancelRegistrationSuccessful:self];
} else {
// this should be impossible to reach, but just for safety's sake...
[self->multicastDelegate cancelRegistrationFailed:self
withError:nil];
}
}
};
if (dispatch_get_specific(moduleQueueTag))
block();
else
dispatch_async(moduleQueue, block);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - XMPPStreamDelegate
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (BOOL)xmppStream:(XMPPStream *)stream didReceiveIQ:(XMPPIQ *)iq
{
NSString *type = [iq type];
if ([type isEqualToString:@"result"] || [type isEqualToString:@"error"]) {
return [xmppIDTracker invokeForElement:iq withObject:iq];
}
return NO;
}
@end