-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathXMPPPerson.m
429 lines (401 loc) · 12 KB
/
XMPPPerson.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//
// XMPPPerson.m
// Jabber
//
// Created by David Chisnall on Sun Apr 25 2004.
// Copyright (c) 2004 __MyCompanyName__. All rights reserved.
//
#import "XMPPRootIdentity.h"
#import "XMPPPerson.h"
#import "XMPPDispatcher.h"
#import "XMPPRoster.h"
#import "CompareHack.h"
#import "XMPPConnection.h"
#import "XMPPServiceDiscovery.h"
#import "ABPerson+merging.h"
#ifndef GNUSTEP
#define kABJabberInstantProperty kABInstantMessageServiceJabber
#define kABMSNInstantProperty kABInstantMessageServiceMSN
#define kABAIMInstantProperty kABInstantMessageServiceAIM
#define kABICQInstantProperty kABInstantMessageServiceICQ
#define kABYahooInstantProperty kABInstantMessageServiceYahoo
#define kABMSNHomeLabel kABInstantMessageServiceMSN
#define kABAIMHomeLabel kABInstantMessageServiceAIM
#define kABJabberHomeLabel kABInstantMessageServiceJabber
#endif
static NSString * avatarCachePath = nil;
@implementation XMPPPerson
+ (void) initialize
{
avatarCachePath = [NSString stringWithFormat:@"%@/%@/avatars/",
[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0],
[[NSProcessInfo processInfo] processName]];
NSArray * components = [avatarCachePath pathComponents];
NSString * currentPath = [components objectAtIndex:0];
NSFileManager * fm = [NSFileManager defaultManager];
for(unsigned int i=0 ; i<[components count] ; i++)
{
currentPath = [currentPath stringByAppendingPathComponent:[components objectAtIndex:i]];
if(![fm fileExistsAtPath:currentPath])
{
NSError *err;
[fm createDirectoryAtPath:currentPath withIntermediateDirectories:YES attributes:nil error:&err];
}
}
[super initialize];
}
+ (id) personWithIdentity:(XMPPIdentity*)_identity forRoster:(id)_roster
{
return [[XMPPPerson alloc] initWithIdentity:_identity forRoster:(id)_roster];
}
- (void) calculateIdentityList
{
[identityList removeAllObjects];
FOREACH(identities, identity, XMPPIdentity*)
{
[identityList addObject:identity];
if([identity isKindOfClass:[XMPPRootIdentity class]])
{
[identityList addObjectsFromArray:[(XMPPRootIdentity*)identity resources]];
}
}
//[identityList sortUsingSelector:@selector(compareByPriority:)];
[identityList sortUsingFunction:compareByPriority context:nil];
identityCount = [identityList count];
}
- (id) init
{
SUPERINIT
identities = [[NSMutableDictionary alloc] init];
identityList = [[NSMutableArray alloc] init];
photoHashes = [[NSMutableDictionary alloc] init];
return self;
}
- (id) initWithIdentity:(XMPPIdentity*)_identity forRoster:(id)_roster
{
SELFINIT
roster = _roster;
[identities setValue:_identity forKey:[[_identity jid] jidString]];
name = [_identity name];
group = [_identity group];
hash = [[NSString stringWithFormat:@"%@ some random wibble %@",name, group] hash];
[_identity person:self];
//Load the person's vCard
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
NSDictionary * vCards = [defaults dictionaryForKey:@"vCards"];
NSString * vCardID = [vCards objectForKey:[NSString stringWithFormat:@"%@!%@", group, name]];
if(vCardID != nil)
{
ABAddressBook * ab = [ABAddressBook sharedAddressBook];
vCard = (ABPerson*)[ab recordForUniqueId:vCardID];
}
[self calculateIdentityList];
return self;
}
- (void) requestvCard:(NSString*)jidString
{
XMPPConnection * connection = (id)[roster connection];
NSString * vCardRequestID = [connection nextMessageID];
[connection XMPPSend:
[NSString stringWithFormat:@"<iq to='%@' type='get' id='%@'><vCard xmlns='vcard-temp'/></iq>"
, jidString, vCardRequestID]];
[[roster dispatcher] addInfoQueryResultHandler:self forID:vCardRequestID];
}
- (void) addIdentity:(XMPPIdentity*)anIdentity
{
NSString * jidString = [[anIdentity jid] jidString];
[anIdentity person:self];
[identities setValue:anIdentity forKey:jidString];
[self calculateIdentityList];
//Request the identity's vcard
if(vCard == nil)
{
[self requestvCard:jidString];
}
}
- (void) removeIdentity:(XMPPIdentity*)_identity
{
[identities removeObjectForKey:[[_identity jid] jidString]];
[self calculateIdentityList];
}
- (NSString*) group
{
return group;
}
- (void) group:(NSString*)_group
{
group = _group;
}
- (NSString*) name
{
return name;
}
- (void) name:(NSString*)_name
{
name = _name;
}
- (XMPPIdentity*) identityForJID:(JID*)jid
{
XMPPIdentity * identity = [identities objectForKey:[jid jidString]];
if([jid type] == resourceJID)
{
if (identity == nil)
{
return [identities objectForKey:[jid jidStringWithNoResource]];
}
}
return identity;
}
- (NSUInteger) identities
{
return identityCount;
}
- (XMPPIdentity*) defaultIdentity
{
if(identityCount == 0)
{
return nil;
}
return [identityList objectAtIndex:0];
}
- (NSArray*) identityList
{
return identityList;
}
- (id)copyWithZone:(NSZone *)zone
{
return self;
}
//Hash value is computed when the object is created, and so will remain constant throughout the lifespan of the object.
- (NSUInteger) hash
{
return hash;
}
- (void) handlePresence:(XMPPPresence*)aPresence
{
NSString * from = [[aPresence jid] jidStringWithNoResource];
XMPPIdentity * identity = [identities objectForKey:from];
//vCard updates
NSString * newPhotoHash = [[aPresence children] objectForKey:@"vCardUpdate"];
if(newPhotoHash != nil && ![newPhotoHash isEqualToString:@""])
{
if(currentHash == nil)
{
currentHash = [[vCard imageData] sha1];
}
if(![photoHashes objectForKey:newPhotoHash])
{
NSData * data = [NSData dataWithContentsOfFile:[avatarCachePath stringByAppendingString:newPhotoHash]];
if(data != nil)
{
currentHash = newPhotoHash;
avatar = [[NSImage alloc] initWithData:data];
}
else
{
[self requestvCard:from];
}
}
}
NSNotificationCenter * notifier = [NSNotificationCenter defaultCenter];
//Set the presence for the identity
XMPPPresence * oldPresence = [[self defaultIdentity] presence];//Retain this in case changing the presence would cause it to be released
[identity setPresence:aPresence];
[self calculateIdentityList];
//Notify anyone who cares that a new presence has been received if it results in the presence of this person changing
if([oldPresence show] != [aPresence show]
||
![[oldPresence status] isEqualToString:[aPresence status]]
)
{
//TODO: Make each Identity do this as well
NSDictionary * userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:[aPresence show]], @"NewPresence",
[NSNumber numberWithInt:[oldPresence show]], @"OldPresence",
[aPresence status], @"NewStatus",
[oldPresence status], @"OldStatus",
nil];
[notifier postNotificationName:@"TRXMPPPresenceChanged" object:self userInfo:userInfo];
}
[notifier postNotificationName:@"TRXMPPIdentityPresenceChanged" object:self userInfo:nil];
}
- (void) handleInfoQuery:(XMPPInfoQueryStanza*)anIq
{
ABPerson * identityvCard = [[anIq children] objectForKey:@"vCard"];
if(identityvCard != nil)
{
ABAddressBook * ab = [ABAddressBook sharedAddressBook];
if(vCard != nil)
{
//Merge the vCard data
NSArray * unmerged = [vCard mergePerson:identityvCard];
if([unmerged count] > 0)
{
//TODO: Ask about which ones to merge
}
}
else
{
vCard = identityvCard;
if([vCard valueForProperty:kABNicknameProperty] == nil)
{
[vCard setValue:name forProperty:kABNicknameProperty];
}
//Parse bridged JIDs correctly (i.e. parse [email protected]
//as an ICQ UIN, not a JID)
id property = kABJabberInstantProperty;
id label = kABJabberHomeLabel;
JID * jid = [anIq jid];
NSString * address = [jid jidStringWithNoResource];
NSArray * serverIdentities = [[roster disco] identitiesForJID:[JID jidWithString:[jid domain]] node:nil];
NSString * gatewayType = nil;
FOREACH(serverIdentities, serverIdentity, NSDictionary*)
{
if([[serverIdentity objectForKey:@"category"] isEqualToString:@"gateway"])
{
gatewayType = [serverIdentity objectForKey:@"type"];
}
}
if(gatewayType != nil)
{
if([gatewayType isEqualToString:@"msn"])
{
property = kABMSNInstantProperty;
label = kABMSNHomeLabel;
NSMutableString * msnAddress = [[jid node] mutableCopy];
[msnAddress replaceOccurrencesOfString:@"%"
withString:@"@"
options:0
range:NSMakeRange(0, [msnAddress length])];
address = msnAddress;
}
else if([gatewayType isEqualToString:@"aim"])
{
property = kABAIMInstantProperty;
label = kABAIMHomeLabel;
address = [jid node];
}
}
if(address != nil && [vCard valueForProperty:kABJabberInstantProperty] == nil)
{
#ifdef GNUSTEP
ABMutableMultiValue * vCardJID =
[(ABMutableMultiValue*)[ABMutableMultiValue alloc]
initWithType:kABMultiStringProperty];
#else
ABMutableMultiValue * vCardJID = [[ABMutableMultiValue alloc] init];
#endif
[vCardJID addValue:address withLabel:label];
[vCard setValue:vCardJID forProperty:property];
}
ABPerson * oldPerson = [vCard findExistingPerson];
if(oldPerson != nil)
{
NSString * notes = [oldPerson valueForProperty:kABNoteProperty];
if(notes == nil)
{
notes = @"";
}
notes = [notes stringByAppendingFormat:@"\nMerged properties from %@", [jid jidString]];
[oldPerson setValue:notes forProperty:kABNoteProperty];
[oldPerson mergePerson:vCard];
vCard = oldPerson;
}
else
{
//Get the group that contains Jabber people
ABGroup * jabberGroup = nil;
NSArray * groups = [ab groups];
FOREACH(groups, abgroup, ABRecord*)
{
if([[abgroup valueForProperty:kABGroupNameProperty] isEqualToString:@"Jabber People"])
{
jabberGroup = (ABGroup*)abgroup;
}
}
if(jabberGroup == nil)
{
jabberGroup = [[ABGroup alloc] init];
[jabberGroup setValue:@"Jabber People" forProperty:kABGroupNameProperty];
[ab addRecord:jabberGroup];
}
[ab addRecord:vCard];
[jabberGroup addMember:vCard];
}
[ab save];
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
NSDictionary * vCards = [defaults dictionaryForKey:@"vCards"];
if(vCards == nil)
{
vCards = [NSDictionary dictionary];
}
NSMutableDictionary * newvCards = [vCards mutableCopy];
[newvCards setObject:[vCard uniqueId] forKey:[NSString stringWithFormat:@"%@!%@", group, name]];
[defaults setObject:newvCards forKey:@"vCards"];
}
NSData * imageData = [identityvCard imageData];
if(imageData != nil)
{
NSString * imageHash = [imageData sha1];
// FIXME: -writeToFile:options:error: has to be implemented in GNUstep base.
#ifdef GNUSTEP
[imageData writeToFile:[avatarCachePath stringByAppendingString:imageHash]
atomically:YES];
#else
[imageData writeToFile:[avatarCachePath stringByAppendingString:imageHash]
options:0
error:NULL];
#endif
if(![imageHash isEqualToString:currentHash])
{
[photoHashes setObject:imageData forKey:imageHash];
avatar = nil;
currentHash = imageHash;
}
}
}
}
- (NSComparisonResult) compare:(XMPPPerson*)otherPerson
{
XMPPPresence * myDefaultPresence = [[self defaultIdentity] presence];
unsigned char myPresence = myDefaultPresence == nil ? PRESENCE_UNKNOWN : [myDefaultPresence show];
unsigned char otherPresence = [[[otherPerson defaultIdentity] presence] show];
NSComparisonResult result;
if (myPresence < otherPresence)
{
result = NSOrderedAscending;
}
else if (myPresence > otherPresence)
{
result = NSOrderedDescending;
}
else //if (myPresence == otherPresence)
{
result = [name caseInsensitiveCompare:[otherPerson name]];
//result = NSOrderedSame;
}
//ETLog(@"Comparing %@(%d) to %@(%d) gives %d", name, (int)myPresence, [otherPerson name], (int)otherPresence, result);
return result;
}
- (NSImage*) avatar
{
if(avatar == nil)
{
NSData * avatarData = [photoHashes objectForKey:currentHash];
if(avatarData == nil)
{
avatarData = [vCard imageData];
if(avatarData != nil)
{
currentHash = [avatarData sha1];
[photoHashes setObject:avatarData forKey:currentHash];
}
}
if(avatarData != nil)
{
avatar = [[NSImage alloc] initWithData:avatarData];
}
}
return avatar;
}
@end