forked from ArchipelProject/StropheCappuccino
-
Notifications
You must be signed in to change notification settings - Fork 1
/
TNStropheRoster.j
590 lines (468 loc) · 17.9 KB
/
TNStropheRoster.j
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
/*
* TNStropheRoster.j
*
* Copyright (C) 2010 Antoine Mercadal <antoine.mercadal@inframonde.eu>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
@import <Foundation/Foundation.j>
@import "TNStropheJID.j"
@import "TNStropheRosterBase.j"
/*! @ingroup strophecappuccino
this is an implementation of a basic XMPP Roster
*/
@implementation TNStropheRoster : TNStropheRosterBase
{
CPArray _groups @accessors(getter=groups);
CPDictionary _pendingPresence @accessors(getter=pendingPresence);
}
#pragma mark -
#pragma mark Class methods
+ (TNStropheRoster)rosterWithConnection:(TNStropheConnection)aConnection
{
return [[TNStropheRoster alloc] initWithConnection:aConnection];
}
#pragma mark -
#pragma mark Initialization
/*! initialize a roster with a valid TNStropheConnection
@return initialized instance of TNStropheRoster
*/
- (id)initWithConnection:(TNStropheConnection)aConnection
{
if (self = [super initWithConnection:aConnection])
{
_groups = [CPArray array];
_pendingPresence = [CPDictionary dictionary];
var rosterPushParams = [CPDictionary dictionaryWithObjectsAndKeys:@"iq", @"name", Strophe.NS.ROSTER, @"namespace", @"set", @"type"],
presenceParams = [CPDictionary dictionaryWithObjectsAndKeys:@"presence", @"name", [[_connection JID] bare], @"to"];
[_connection registerSelector:@selector(_didReceiveRosterPush:) ofObject:self withDict:rosterPushParams];
[_connection registerSelector:@selector(_didReceivePresence:) ofObject:self withDict:presenceParams];
}
return self;
}
- (void)clear
{
[_groups removeAllObjects];
[_pendingPresence removeAllObjects];
[super clear];
}
#pragma mark -
#pragma mark Fetch
/*! ask the server to get the roster of the TNStropheConnection user
*/
- (void)getRoster
{
var uid = [_connection getUniqueIdWithSuffix:@"roster"],
rosteriq = [TNStropheStanza iqWithAttributes:{@"id": uid, @"type": @"get"}],
rosterResultParams = [CPDictionary dictionaryWithObjectsAndKeys:uid, @"id"];
[rosteriq addChildWithName:@"query" andAttributes:{'xmlns':Strophe.NS.ROSTER}];
[_connection registerSelector:@selector(_didReceiveRosterResult:) ofObject:self withDict:rosterResultParams];
[_connection send:rosteriq];
}
/*! this called when the roster is recieved. Will post TNStropheRosterRetrievedNotification
@return NO to remove the selector registered from TNStropheConnection
*/
- (BOOL)_didReceiveRosterResult:(id)aStanza
{
var items = [aStanza childrenWithName:@"item"];
for (var i = 0; i < [items count]; i++)
{
var item = [items objectAtIndex:i],
subscription = [item valueForAttribute:@"subscription"],
allowedSubs = [CPArray arrayWithObjects:@"none", @"to", @"from", @"both"];
if (!subscription || ![allowedSubs containsObject:subscription])
[item setValue:@"none" forAttribute:@"subscription"];
[self _addContactFromRosterItem:item];
}
[[CPNotificationCenter defaultCenter] postNotificationName:TNStropheRosterRetrievedNotification object:self];
return NO;
}
/*! this called when a roster push is recieved. Will post TNStropheRosterPushNotification
@return YES to keep the selector registered with TNStropheConnection
*/
- (BOOL)_didReceiveRosterPush:(id)aStanza
{
var item = [aStanza firstChildWithName:@"item"],
theJID = [TNStropheJID stropheJIDWithString:[item valueForAttribute:@"jid"]],
subscription = [item valueForAttribute:@"subscription"],
allowedSubs = [CPArray arrayWithObjects:@"none", @"to", @"from", @"both", @"remove"],
response = [TNStropheStanza iqTo:[aStanza from] withAttributes:{@"id": [aStanza ID], @"type": @"result"}],
contact;
/*! A receiving client MUST ignore the stanza unless it has no 'from' attribute (i.e., implicitly from the
bare JID of the user's account) or it has a 'from' attribute whose value matches the user's bare
JID <user@domainpart>.
*/
if ([aStanza from] && [aStanza from] != [[_connection JID] bare])
return;
/*! TODO: Should only send this if the stuff below has actually been successful
Otherwise should send appropriate error response
*/
[_connection send:response];
if (!subscription || ![allowedSubs containsObject:subscription])
[item setValue:@"none" forAttribute:@"subscription"];
if ([self containsJID:theJID])
{
contact = [self _updateContactFromRosterItem:item];
}
else
{
contact = [self _addContactFromRosterItem:item];
[[CPNotificationCenter defaultCenter] postNotificationName:TNStropheRosterPushAddedContactNotification object:self userInfo:contact];
}
[[CPNotificationCenter defaultCenter] postNotificationName:TNStropheRosterPushNotification object:self];
return YES;
}
/*! message sent when a presence information received
send roster:receiveSubscriptionRequest: to the delegate with the stanza as parameter
@return YES to keep the selector registred in TNStropheConnection
*/
- (BOOL)_didReceivePresence:(TNStropheStanza)aStanza
{
if ([aStanza type] === @"subscribe")
{
if ([_delegate respondsToSelector:@selector(roster:receiveSubscriptionRequest:)])
[_delegate roster:self receiveSubscriptionRequest:aStanza];
}
else
{
if ([self containsJID:[aStanza from]])
[[self contactWithJID:[aStanza from]] _didReceivePresence:aStanza];
else
{
var from = [aStanza fromBare];
if (![_pendingPresence containsKey:from])
[_pendingPresence setValue:[CPArray array] forKey:from];
[[_pendingPresence valueForKey:from] addObject:aStanza];
}
}
return YES;
}
- (CPArray)pendingPresenceForJID:(TNStropheJID)aJID
{
return [_pendingPresence valueForKey:[aJID bare]];
}
#pragma mark -
#pragma mark Groups
/*! add a group to the roster with given name
@param aGroupName the name of the new group
@return TNStropheGroup object representing the new group
*/
- (TNStropheGroup)addGroup:(TNStropheGroup)aGroup
{
[_groups addObject:aGroup];
[[CPNotificationCenter defaultCenter] postNotificationName:TNStropheRosterAddedGroupNotification object:aGroup];
return aGroup;
}
- (TNStropheGroup)addGroupWithName:(CPString)aGroupName
{
if ([self containsGroupWithName:aGroupName])
return;
return [self addGroup:[TNStropheGroup stropheGroupWithName:aGroupName]];
}
/*! remove a group from the roster with given name
@param aGroupName the name of the group to remove
@return YES if group has been removed, NO otherwise
*/
- (void)removeGroup:(TNStropheGroup)aGroup
{
for (var i = 0; i < [_contacts count]; i++)
{
var contact = [_contacts objectAtIndex:i];
[contact removeGroup:aGroup];
}
[_groups removeObject:aGroup];
[[CPNotificationCenter defaultCenter] postNotificationName:TNStropheRosterRemovedGroupNotification object:aGroup];
}
/*! checks if given TNStropheGroup is in roster
@param aGroup the group
@return YES if group is in roster, NO otherwise
*/
- (BOOL)containsGroup:(TNStropheGroup)aGroup
{
for (var i = 0; i < [_groups count]; i++)
{
if ([_groups objectAtIndex:i] == aGroup)
return YES;
}
return NO;
}
/*! checks if group with given name exist in roster
@param aGroup the group name
@return YES if group is in roster, NO otherwise
*/
- (BOOL)containsGroupWithName:(CPString)aGroupName
{
return [self containsGroup:[self groupWithName:aGroupName]];
}
/*! return TNStropheGroup object according to the given name
@param aGroupName the group name
@return TNStropheGroup the group. nil if group doesn't exist
*/
- (TNStropheGroup)groupWithName:(CPString)aGroupName
{
for (var i = 0; i < [_groups count]; i++)
{
var group = [_groups objectAtIndex:i];
if ([group name] == aGroupName)
return group;
}
return;
}
/*! return or create and return a TNStropheGroup with aGroupName
@param aGroupName CPstring of the name
@return a TNStropheGroup;
*/
- (TNStropheGroup)groupWithName:(CPString)aGroupName orCreate:(BOOL)shouldCreate
{
var newGroup = [self groupWithName:aGroupName];
if (shouldCreate && !newGroup)
return [self addGroupWithName:aGroupName];
return newGroup;
}
/*! return the group of given contact
@param aContact the contact
@return CPArray of TNStropheGroups of the the contact
*/
- (CPArray)groupsOfContact:(TNStropheContact)aContact
{
var tempArray = [CPArray array];
for (var i = 0; i < [_groups count]; i++)
{
var group = [_groups objectAtIndex:i];
if ([[group contacts] containsObject:aContact])
[tempArray addObject:group];
}
return tempArray;
}
- (int)populatedGroupsCount
{
return [[self populatedGroups] count];
}
- (CPArray)populatedGroups
{
var tempGroups = [CPArray array];
for (var i = 0; i < [_groups count]; i++)
{
var group = [_groups objectAtIndex:i];
if ([group count] > 0)
[tempGroups addObject:group];
}
return tempGroups;
}
#pragma mark -
#pragma mark Contacts
/*! add a new contact to the roster with given information
@param aJID the JID of the new contact
@param aName the nickname of the new contact. If nil, it will be the JID
@param aGroup the group of the new contact. if nil, it will be "General"
*/
- (void)addContact:(TNStropheJID)aJID withName:(CPString)aName inGroupWithName:(CPString)aGroupName
{
if ([self containsJID:aJID])
return;
if (!aName)
aName = [aJID node];
if (!aGroupName)
aGroupName = @"General";
var uid = [_connection getUniqueId],
addReq = [TNStropheStanza iqWithAttributes:{"type": "set", "id": uid}],
params = [CPDictionary dictionaryWithObjectsAndKeys:uid, @"id"],
contact = [TNStropheContact contactWithConnection:_connection JID:aJID groupName:aGroupName];
[contact setNickname:aName];
[_connection registerSelector:@selector(_didAddContact:userInfo:) ofObject:self withDict:params userInfo:aJID];
[contact sendRosterSet];
}
- (BOOL)_didAddContact:(TNStropheStanza)aStanza userInfo:(TNStropheJID)theJID
{
if ([aStanza type] === @"result")
CPLog.debug("Contact with JID " + theJID + " was added to roster!");
else
CPLog.error("Error adding contact with JID " + theJID + " to roster!");
return NO;
}
/*! add a contact into the roster from a item XML node from roster iq
@param aRosterItem TNXMLNode representing the roster item
@param return a new TNStropheContact
*/
- (void)_addContactFromRosterItem:(TNXMLNode)aRosterItem
{
var theJID = [TNStropheJID stropheJIDWithString:[aRosterItem valueForAttribute:@"jid"]];
if ([theJID bareEquals:[_connection JID]])
{
CPLog.warn("Cannot add: jid is the current account JID " + theJID)
return;
}
var contact = [TNStropheContact contactWithConnection:_connection JID:theJID groupName:nil],
nickname = [aRosterItem valueForAttribute:@"name"] || [theJID node],
groupNodes = [aRosterItem childrenWithName:@"group"],
groupNames = [CPArray array],
queuedPresence = [self pendingPresenceForJID:theJID],
subscription = [aRosterItem valueForAttribute:@"subscription"];
[_contacts addObject:contact];
for (var i = 0; i < [groupNodes count]; i++)
[groupNames addObject:[[groupNodes objectAtIndex:i] text]];
if ([groupNames count] === 0)
[groupNames addObject:[_defaultGroup name]];
for (var i = 0; i < [groupNames count]; i++)
{
var groupName = [groupNames objectAtIndex:i],
group = [self groupWithName:groupName orCreate:YES];
// Fix group names on contact
[[contact groupNames] addObject:groupName];
// Add contact to all new groups
[group addContact:contact];
}
for (var j = 0; j < [queuedPresence count]; j++)
[contact _didReceivePresence:[queuedPresence objectAtIndex:j]];
[contact setNickname:nickname];
[contact setSubscription:subscription];
[contact getMessages];
return contact;
}
/*! update a contact from a item XML node from roster iq
@param aRosterItem TNXMLNode representing the roster item
@param return the updated TNStropheContact
*/
- (void)_updateContactFromRosterItem:(TNXMLNode)aRosterItem
{
var theJID = [TNStropheJID stropheJIDWithString:[aRosterItem valueForAttribute:@"jid"]];
if ([theJID bareEquals:[_connection JID]])
{
CPLog.warn("Cannot update: jid is the current account JID " + theJID)
return;
}
var contact = [self contactWithJID:theJID],
subscription = [aRosterItem valueForAttribute:@"subscription"];
if (subscription === @"remove")
{
var groups = [self groupsOfContact:contact];
for (var i = 0; i < [groups count]; i++)
{
var group = [groups objectAtIndex:i];
[group removeContact:contact];
[_contacts removeObject:contact];
if ([[group contacts] count] === 0)
[self removeGroup:group];
}
[[CPNotificationCenter defaultCenter] postNotificationName:TNStropheRosterPushRemovedContactNotification object:self userInfo:contact];
}
else
{
var nickname = [aRosterItem valueForAttribute:@"name"] || [theJID node],
groupNodes = [aRosterItem childrenWithName:@"group"],
groupNames = [CPArray array];
for (var i = 0; i < [groupNodes count]; i++)
[groupNames addObject:[[groupNodes objectAtIndex:i] text]];
if ([groupNames count] === 0)
[groupNames addObject:[_defaultGroup name]];
[contact setNickname:nickname];
// Remove contact from all groups
var oldGroups = [self groupsOfContact:contact];
for (var i = 0; i < [oldGroups count]; i++)
[[oldGroups objectAtIndex:i] removeContact:contact];
for (var i = 0; i < [groupNames count]; i++)
{
var groupName = [groupNames objectAtIndex:i],
group = [self groupWithName:groupName orCreate:YES];
// Fix group names on contact
[[contact groupNames] addObject:groupName];
// Add contact to all new groups
[group addContact:contact];
}
[[CPNotificationCenter defaultCenter] postNotificationName:TNStropheRosterPushUpdatedContactNotification object:self userInfo:contact];
}
return contact;
}
/*! remove a TNStropheContact from the roster
@param aContact the contact to remove
*/
- (void)removeContact:(TNStropheContact)aContact
{
var uid = [_connection getUniqueIdWithSuffix:@"roster"],
removeReq = [TNStropheStanza iqWithAttributes:{"type": "set", "id": uid}],
params = [CPDictionary dictionaryWithObjectsAndKeys:uid, @"id"];
[removeReq addChildWithName:@"query" andAttributes: {'xmlns':Strophe.NS.ROSTER}];
[removeReq addChildWithName:@"item" andAttributes:{'jid': [[aContact JID] bare], 'subscription': 'remove'}];
[_connection registerSelector:@selector(_didRemoveContact:userInfo:) ofObject:self withDict:params userInfo:aContact];
[_connection send:removeReq];
}
/*! remove a contact from the roster according to its JID
@param aJID the JID of the contact to remove
*/
- (void)removeContactWithJID:(TNStropheJID)aJID
{
[self removeContact:[self contactWithJID:aJID]];
}
- (BOOL)_didRemoveContact:(TNStropheStanza)aStanza userInfo:(TNStropheContact)theContact
{
if ([aStanza type] === @"result")
CPLog.debug("Contact was removed from roster!");
else
{
CPLog.error("Error removing contact from roster!");
CPLog.error(theContact);
}
return NO;
}
#pragma mark -
#pragma mark Subscriptions
/*! subscribe to the given JID
@param aJID the JID to subscribe
*/
- (void)authorizeJID:(TNStropheJID)aJID
{
var contact = [self contactWithJID:aJID];
if (!contact)
return;
[contact subscribe];
}
/*! unsubscribe to the given JID
@param aJID the JID to unsubscribe
*/
- (void)unauthorizeJID:(TNStropheJID)aJID
{
var contact = [self contactWithJID:aJID];
if (!contact)
return;
[[self contactWithJID:aJID] unsubscribe];
}
/*! ask subscribtion to the given JID
@param aJID the JID to ask subscribtion
*/
- (void)askAuthorizationTo:(TNStropheJID)aJID
{
var contact = [self contactWithJID:aJID];
if (!contact)
return;
[[self contactWithJID:aJID] askSubscription];
}
/*! answer to a pending subscription request.
@param TNStropheStanza the subscription request
@param theAnswer if YES contact is subscribed and added to the roster. If NO, the subscription request is declined
*/
- (void)answerAuthorizationRequest:(id)aStanza answer:(BOOL)theAnswer
{
var requester = [aStanza from];
if (theAnswer == YES)
{
[self authorizeJID:requester];
[self askAuthorizationTo:requester];
}
else
[self unauthorizeJID:requester];
if (![self containsJID:requester])
[self addContact:requester withName:[requester node] inGroupWithName:nil];
}
@end