forked from robbiehanson/XMPPFramework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXMPPvCardAvatarModule.m
executable file
·296 lines (222 loc) · 8.72 KB
/
XMPPvCardAvatarModule.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
//
// XMPPvCardAvatarModule.h
// XEP-0153 vCard-Based Avatars
//
// Created by Eric Chamberlain on 3/9/11.
// Copyright 2011 RF.com. All rights reserved.
#import "XMPPvCardAvatarModule.h"
#import "NSData+XMPP.h"
#import "NSXMLElement+XMPP.h"
#import "XMPPLogging.h"
#import "XMPPPresence.h"
#import "XMPPStream.h"
#import "XMPPvCardTempModule.h"
#import "XMPPvCardTemp.h"
#if ! __has_feature(objc_arc)
#warning This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
#endif
// Log levels: off, error, warn, info, verbose
// Log flags: trace
#if DEBUG
static const int xmppLogLevel = XMPP_LOG_LEVEL_WARN; // | XMPP_LOG_FLAG_TRACE;
#else
static const int xmppLogLevel = XMPP_LOG_LEVEL_WARN;
#endif
NSString *const kXMPPvCardAvatarElement = @"x";
NSString *const kXMPPvCardAvatarNS = @"vcard-temp:x:update";
NSString *const kXMPPvCardAvatarPhotoElement = @"photo";
@interface XMPPvCardAvatarModule() {
__strong XMPPvCardTempModule *_xmppvCardTempModule;
__strong id <XMPPvCardAvatarStorage> _moduleStorage;
BOOL _autoClearMyvcard;
}
@end
@implementation XMPPvCardAvatarModule
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark Init/dealloc
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (id)initWithvCardTempModule:(XMPPvCardTempModule *)xmppvCardTempModule
{
return [self initWithvCardTempModule:xmppvCardTempModule dispatchQueue:NULL];
}
- (id)initWithvCardTempModule:(XMPPvCardTempModule *)xmppvCardTempModule dispatchQueue:(dispatch_queue_t)queue
{
NSParameterAssert(xmppvCardTempModule != nil);
if ((self = [super initWithDispatchQueue:queue])) {
_xmppvCardTempModule = xmppvCardTempModule;
// we don't need to call the storage configureWithParent:queue: method,
// because the vCardTempModule already did that.
_moduleStorage = (id <XMPPvCardAvatarStorage>)xmppvCardTempModule.xmppvCardTempModuleStorage;
[_xmppvCardTempModule addDelegate:self delegateQueue:moduleQueue];
_autoClearMyvcard = YES;
}
return self;
}
- (void)dealloc {
[_xmppvCardTempModule removeDelegate:self];
_moduleStorage = nil;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark Properties
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (BOOL)autoClearMyvcard
{
__block BOOL result = NO;
dispatch_block_t block = ^{
result = self->_autoClearMyvcard;
};
if (dispatch_get_specific(moduleQueueTag))
block();
else
dispatch_sync(moduleQueue, block);
return result;
}
- (void)setAutoClearMyvcard:(BOOL)flag
{
dispatch_block_t block = ^{
self->_autoClearMyvcard = flag;
};
if (dispatch_get_specific(moduleQueueTag))
block();
else
dispatch_async(moduleQueue, block);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark Public
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (NSData *)photoDataForJID:(XMPPJID *)jid
{
// This is a public method, so it may be invoked on any thread/queue.
//
// The vCardTempModule is thread safe.
// The moduleStorage should be thread safe. (User may be using custom module storage class).
// The multicastDelegate is NOT thread safe.
__block NSData *photoData;
dispatch_block_t block = ^{ @autoreleasepool {
photoData = [self->_moduleStorage photoDataForJID:jid xmppStream:self->xmppStream];
if (photoData == nil)
{
[self->_xmppvCardTempModule vCardTempForJID:jid shouldFetch:YES];
}
}};
if (dispatch_get_specific(moduleQueueTag))
block();
else
dispatch_sync(moduleQueue, block);
return photoData;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark XMPPStreamDelegate
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (void)xmppStreamDidConnect:(XMPPStream *)sender {
XMPPLogTrace();
if(self.autoClearMyvcard)
{
/*
* XEP-0153 Section 4.2 rule 1
*
* A client MUST NOT advertise an avatar image without first downloading the current vCard.
* Once it has done this, it MAY advertise an image.
*/
[_moduleStorage clearvCardTempForJID:[sender myJID] xmppStream:sender];
}
}
- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender {
XMPPLogTrace();
[_xmppvCardTempModule fetchvCardTempForJID:[sender myJID] ignoreStorage:YES];
}
- (XMPPPresence *)xmppStream:(XMPPStream *)sender willSendPresence:(XMPPPresence *)presence {
XMPPLogTrace();
NSXMLElement *currentXElement = [presence elementForName:kXMPPvCardAvatarElement xmlns:kXMPPvCardAvatarNS];
//If there is already a x element then remove it
if(currentXElement)
{
NSUInteger currentXElementIndex = [[presence children] indexOfObject:currentXElement];
if(currentXElementIndex != NSNotFound)
{
[presence removeChildAtIndex:currentXElementIndex];
}
}
// add our photo info to the presence stanza
NSXMLElement *photoElement = nil;
NSXMLElement *xElement = [NSXMLElement elementWithName:kXMPPvCardAvatarElement xmlns:kXMPPvCardAvatarNS];
NSString *photoHash = [_moduleStorage photoHashForJID:[sender myJID] xmppStream:sender];
if (photoHash != nil)
{
photoElement = [NSXMLElement elementWithName:kXMPPvCardAvatarPhotoElement stringValue:photoHash];
} else {
photoElement = [NSXMLElement elementWithName:kXMPPvCardAvatarPhotoElement];
}
[xElement addChild:photoElement];
[presence addChild:xElement];
// Question: If photoElement is nil, should we be adding xElement?
return presence;
}
- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence {
XMPPLogTrace();
NSXMLElement *xElement = [presence elementForName:kXMPPvCardAvatarElement xmlns:kXMPPvCardAvatarNS];
if (xElement == nil) {
return;
}
NSXMLElement *photoElement = [xElement elementForName:kXMPPvCardAvatarPhotoElement];
if (photoElement == nil) {
return;
}
NSString *photoHash = [photoElement stringValue];
XMPPJID *jid = [presence from];
NSString *savedPhotoHash = [_moduleStorage photoHashForJID:jid xmppStream:xmppStream];
// check the hash
if ([photoHash caseInsensitiveCompare:savedPhotoHash] != NSOrderedSame
&& !([photoHash length] == 0 && [savedPhotoHash length] == 0)) {
[_xmppvCardTempModule fetchvCardTempForJID:jid ignoreStorage:YES];
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark XMPPvCardTempModuleDelegate
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (void)xmppvCardTempModule:(XMPPvCardTempModule *)vCardTempModule
didReceivevCardTemp:(XMPPvCardTemp *)vCardTemp
forJID:(XMPPJID *)jid
{
XMPPLogTrace();
if (vCardTemp.photo != nil)
{
#if TARGET_OS_IPHONE
UIImage *photo = [UIImage imageWithData:vCardTemp.photo];
#else
NSImage *photo = [[NSImage alloc] initWithData:vCardTemp.photo];
#endif
if (photo != nil)
{
[multicastDelegate xmppvCardAvatarModule:self
didReceivePhoto:photo
forJID:jid];
}
}
/*
* XEP-0153 4.1.3
* If the client subsequently obtains an avatar image (e.g., by updating or retrieving the vCard),
* it SHOULD then publish a new <presence/> stanza with character data in the <photo/> element.
*/
if ([[xmppStream myJID] isEqualToJID:jid options:XMPPJIDCompareBare])
{
XMPPPresence *presence = xmppStream.myPresence;
if(presence)
{
[xmppStream sendElement:presence];
}
}
}
- (void)xmppvCardTempModuleDidUpdateMyvCard:(XMPPvCardTempModule *)vCardTempModule{
//The vCard has been updated on the server so we need to cache it
[_xmppvCardTempModule fetchvCardTempForJID:[xmppStream myJID] ignoreStorage:NO];
}
- (void)xmppvCardTempModule:(XMPPvCardTempModule *)vCardTempModule failedToUpdateMyvCard:(NSXMLElement *)error{
//The vCard failed to update so we fetch the current one from the server
[_xmppvCardTempModule fetchvCardTempForJID:[xmppStream myJID] ignoreStorage:YES];
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark Getter/setter
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@synthesize xmppvCardTempModule = _xmppvCardTempModule;
@end