forked from tinycode/xmppframework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXMPPUser.m
388 lines (330 loc) · 9.88 KB
/
XMPPUser.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#import "XMPPUser.h"
#import "XMPPJID.h"
#import "XMPPIQ.h"
#import "XMPPPresence.h"
#import "XMPPResource.h"
#import "NSXMLElementAdditions.h"
@implementation XMPPUser
- (id)initWithJID:(XMPPJID *)aJid
{
if(self = [super init])
{
jid = [[aJid bareJID] retain];
itemAttributes = [[NSMutableDictionary alloc] initWithCapacity:0];
resources = [[NSMutableDictionary alloc] initWithCapacity:1];
tag = 0;
}
return self;
}
- (id)initWithItem:(NSXMLElement *)item
{
if(self = [super init])
{
// Example item:
// <item subscription='both' name='Robbie' jid='robbiehanson@deusty.com'/>
NSString *jidStr = [[item attributeForName:@"jid"] stringValue];
jid = [[[XMPPJID jidWithString:jidStr] bareJID] retain];
itemAttributes = [[item attributesAsDictionary] mutableCopy];
resources = [[NSMutableDictionary alloc] initWithCapacity:1];
tag = 0;
}
return self;
}
- (void)dealloc
{
[jid release];
[itemAttributes release];
[resources release];
[primaryResource release];
[super dealloc];
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark Encoding, Decoding
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#if ! TARGET_OS_IPHONE
- (id)replacementObjectForPortCoder:(NSPortCoder *)encoder
{
if([encoder isBycopy])
return self;
else
return [NSDistantObject proxyWithLocal:self connection:[encoder connection]];
}
#endif
- (id)initWithCoder:(NSCoder *)coder
{
if(self = [super init])
{
if([coder allowsKeyedCoding])
{
jid = [[coder decodeObjectForKey:@"jid"] retain];
itemAttributes = [[coder decodeObjectForKey:@"itemAttributes"] mutableCopy];
resources = [[coder decodeObjectForKey:@"resources"] mutableCopy];
primaryResource = [[coder decodeObjectForKey:@"primaryResource"] retain];
#if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_4
tag = [coder decodeIntForKey:@"tag"];
#else
tag = [coder decodeIntegerForKey:@"tag"];
#endif
}
else
{
jid = [[coder decodeObject] retain];
itemAttributes = [[coder decodeObject] mutableCopy];
resources = [[coder decodeObject] mutableCopy];
primaryResource = [[coder decodeObject] retain];
#if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_4
tag = [[coder decodeObject] intValue];
#else
tag = [[coder decodeObject] integerValue];
#endif
}
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)coder
{
if([coder allowsKeyedCoding])
{
[coder encodeObject:jid forKey:@"jid"];
[coder encodeObject:itemAttributes forKey:@"itemAttributes"];
[coder encodeObject:resources forKey:@"resources"];
[coder encodeObject:primaryResource forKey:@"primaryResource"];
#if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_4
[coder encodeInt:tag forKey:@"tag"];
#else
[coder encodeInteger:tag forKey:@"tag"];
#endif
}
else
{
[coder encodeObject:jid];
[coder encodeObject:itemAttributes];
[coder encodeObject:resources];
[coder encodeObject:primaryResource];
#if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_4
[coder encodeObject:[NSNumber numberWithInt:tag]];
#else
[coder encodeObject:[NSNumber numberWithInteger:tag]];
#endif
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark Standard Methods
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (XMPPJID *)jid
{
return jid;
}
- (NSString *)nickname
{
return (NSString *)[itemAttributes objectForKey:@"name"];
}
- (NSString *)displayName
{
NSString *nickname = [self nickname];
if(nickname)
return nickname;
else
return [jid bare];
}
- (BOOL)isOnline
{
return (primaryResource != nil);
}
- (BOOL)isPendingApproval
{
// Either of the following mean we're waiting to have our presence subscription approved:
// <item ask='subscribe' subscription='none' jid='robbiehanson@deusty.com'/>
// <item ask='subscribe' subscription='from' jid='robbiehanson@deusty.com'/>
NSString *subscription = [itemAttributes objectForKey:@"subscription"];
NSString *ask = [itemAttributes objectForKey:@"ask"];
if([subscription isEqualToString:@"none"] || [subscription isEqualToString:@"from"])
{
if([ask isEqualToString:@"subscribe"])
{
return YES;
}
}
return NO;
}
- (XMPPResource *)primaryResource
{
return primaryResource;
}
- (XMPPResource *)resourceForJID:(XMPPJID *)aJid
{
return [resources objectForKey:aJid];
}
- (NSArray *)sortedResources
{
return [[resources allValues] sortedArrayUsingSelector:@selector(compare:)];
}
- (NSArray *)unsortedResources
{
return [resources allValues];
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark Helper Methods
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (void)recalculatePrimaryResource
{
[primaryResource release];
primaryResource = nil;
NSArray *sortedResources = [self sortedResources];
if([sortedResources count] > 0)
{
XMPPResource *possiblePrimary = [sortedResources objectAtIndex:0];
// Primary resource must have a non-negative priority
if([[possiblePrimary presence] priority] >= 0)
{
primaryResource = [possiblePrimary retain];
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark Update Methods
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (void)updateWithItem:(NSXMLElement *)item
{
NSArray *attributes = [item attributes];
int i;
for(i = 0; i < [attributes count]; i++)
{
NSXMLNode *node = [attributes objectAtIndex:i];
NSString *key = [node name];
NSString *value = [node stringValue];
if(![value isEqualToString:[itemAttributes objectForKey:key]])
{
[itemAttributes setObject:value forKey:key];
}
}
}
- (void)updateWithPresence:(XMPPPresence *)presence
{
if([[presence type] isEqualToString:@"unavailable"])
{
[resources removeObjectForKey:[presence from]];
}
else
{
XMPPJID *key = [presence from];
XMPPResource *resource = [resources objectForKey:key];
if(resource)
{
[resource updateWithPresence:presence];
}
else
{
XMPPResource *newResource = [[[XMPPResource alloc] initWithPresence:presence] autorelease];
[resources setObject:newResource forKey:key];
}
}
[self recalculatePrimaryResource];
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark Comparison Methods
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Returns the result of invoking compareByName:options: with no options.
**/
- (NSComparisonResult)compareByName:(XMPPUser *)another
{
return [self compareByName:another options:0];
}
/**
* This method compares the two users according to their name.
* If either of the users has no set name (or has an empty string name), the name is considered to be the JID.
*
* Options for the search Ñ you can combine any of the following using a C bitwise OR operator:
* NSCaseInsensitiveSearch, NSLiteralSearch, NSNumericSearch.
* See "String Programming Guide for Cocoa" for details on these options.
**/
- (NSComparisonResult)compareByName:(XMPPUser *)another options:(NSStringCompareOptions)mask
{
NSString *myName = [self displayName];
NSString *theirName = [another displayName];
return [myName compare:theirName options:mask];
}
/**
* Returns the result of invoking compareByAvailabilityName:options: with no options.
**/
- (NSComparisonResult)compareByAvailabilityName:(XMPPUser *)another
{
return [self compareByAvailabilityName:another options:0];
}
/**
* This method compares the two users according to availability first, and then name.
* Thus available users come before unavailable users.
* If both users are available, or both users are not available,
* this method follows the same functionality as the compareByName:options: as documented above.
**/
- (NSComparisonResult)compareByAvailabilityName:(XMPPUser *)another options:(NSStringCompareOptions)mask
{
if([self isOnline])
{
if([another isOnline])
return [self compareByName:another options:mask];
else
return NSOrderedAscending;
}
else
{
if([another isOnline])
return NSOrderedDescending;
else
return [self compareByName:another options:mask];
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark NSObject Methods
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_4
- (unsigned)hash
{
return [jid hash];
}
#else
- (NSUInteger)hash
{
return [jid hash];
}
#endif
- (BOOL)isEqual:(id)anObject
{
if([anObject isMemberOfClass:[self class]])
{
XMPPUser *another = (XMPPUser *)anObject;
return [jid isEqual:[another jid]];
}
return NO;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"XMPPUser: %@", [jid bare]];
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark User Defined Content
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_4
- (int)tag
{
return tag;
}
#else
- (NSInteger)tag
{
return tag;
}
#endif
#if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_4
- (void)setTag:(int)anInt
{
tag = anInt;
}
#else
- (void)setTag:(NSInteger)anInt
{
tag = anInt;
}
#endif
@end