XMPPRoomLight.m
18.2 KB
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
//
// XMPPRoomLight.m
// Mangosta
//
// Created by Andres Canal on 5/27/16.
// Copyright © 2016 Inaka. All rights reserved.
//
#import "XMPPFramework/XMPPMessage+XEP0045.h"
#import "XMPPRoomLight.h"
static NSString *const XMPPRoomLightAffiliations = @"urn:xmpp:muclight:0#affiliations";
static NSString *const XMPPRoomLightConfiguration = @"urn:xmpp:muclight:0#configuration";
static NSString *const XMPPRoomLightDestroy = @"urn:xmpp:muclight:0#destroy";
@interface XMPPRoomLight() {
NSString *roomname;
NSString *subject;
NSString *configVersion;
NSString *memberListVersion;
}
@end
@implementation XMPPRoomLight
- (instancetype)init{
NSAssert(NO, @"Cannot be instantiated with init!");
return nil;
}
- (nonnull instancetype)initWithJID:(nonnull XMPPJID *)roomJID roomname:(nonnull NSString *)_roomname{
return [self initWithRoomLightStorage:nil jid:roomJID roomname:_roomname dispatchQueue:nil];
}
- (nonnull instancetype)initWithRoomLightStorage:(nullable id <XMPPRoomLightStorage>)storage jid:(nonnull XMPPJID *)aRoomJID roomname:(nonnull NSString *)aRoomname dispatchQueue:(nullable dispatch_queue_t)queue{
NSParameterAssert(aRoomJID != nil);
if ((self = [super initWithDispatchQueue:queue])){
xmppRoomLightStorage = storage;
_domain = aRoomJID.domain;
_roomJID = aRoomJID;
roomname = aRoomname;
configVersion = @"";
memberListVersion = @"";
}
return self;
}
- (BOOL)activate:(XMPPStream *)aXmppStream
{
if ([super activate:aXmppStream])
{
responseTracker = [[XMPPIDTracker alloc] initWithDispatchQueue:moduleQueue];
return YES;
}
return NO;
}
- (void)deactivate
{
dispatch_block_t block = ^{ @autoreleasepool {
[responseTracker removeAllIDs];
responseTracker = nil;
}};
if (dispatch_get_specific(moduleQueueTag))
block();
else
dispatch_sync(moduleQueue, block);
[super deactivate];
}
- (nonnull NSString *)roomname {
@synchronized(roomname) {
return [roomname copy];
}
}
- (nonnull NSString *)subject {
@synchronized(subject) {
return [subject copy];
}
}
- (nonnull NSString *)configVersion {
@synchronized(subject) {
return [configVersion copy];
}
}
- (nonnull NSString *)memberListVersion {
@synchronized(subject) {
return [memberListVersion copy];
}
}
- (void)handleConfigElements:(NSArray<NSXMLElement*> *)configElements{
for (NSXMLElement *element in configElements) {
if([element.name isEqualToString:@"subject"]){
[self setSubject:element.stringValue];
} else if([element.name isEqualToString:@"roomname"]) {
[self setRoomname:element.stringValue];
}
}
}
- (void)setRoomname:(NSString *)aRoomname{
dispatch_block_t block = ^{ @autoreleasepool {
roomname = aRoomname;
}};
if (dispatch_get_specific(moduleQueueTag))
block();
else
dispatch_async(moduleQueue, block);
}
- (void)setSubject:(NSString *)aSubject{
dispatch_block_t block = ^{ @autoreleasepool {
subject = aSubject;
}};
if (dispatch_get_specific(moduleQueueTag))
block();
else
dispatch_async(moduleQueue, block);
}
- (void)setMemberListVersion:(NSString *)aVersion{
dispatch_block_t block = ^{ @autoreleasepool {
memberListVersion = aVersion;
}};
if (dispatch_get_specific(moduleQueueTag))
block();
else
dispatch_async(moduleQueue, block);
}
- (void)setConfigVersion:(NSString *)aVersion{
dispatch_block_t block = ^{ @autoreleasepool {
configVersion = aVersion;
}};
if (dispatch_get_specific(moduleQueueTag))
block();
else
dispatch_async(moduleQueue, block);
}
- (void)flushVersion{
[self setConfigVersion:@""];
[self setMemberListVersion:@""];
}
- (void)createRoomLightWithMembersJID:(nullable NSArray<XMPPJID *> *) members{
// <iq from='crone1@shakespeare.lit/desktop'
// id='create1'
// to='coven@muclight.shakespeare.lit'
// type='set'>
// <query xmlns='urn:xmpp:muclight:0#create'>
// <configuration>
// <roomname>A Dark Cave</roomname>
// </configuration>
// <occupants>
// <user affiliation='member'>user1@shakespeare.lit</user>
// <user affiliation='member'>user2@shakespeare.lit</user>
// </occupants>
// </query>
// </iq>
dispatch_block_t block = ^{ @autoreleasepool {
_roomJID = [XMPPJID jidWithUser:[XMPPStream generateUUID]
domain:self.domain
resource:nil];
NSString *iqID = [XMPPStream generateUUID];
NSXMLElement *iq = [NSXMLElement elementWithName:@"iq"];
[iq addAttributeWithName:@"id" stringValue:iqID];
[iq addAttributeWithName:@"to" stringValue:self.roomJID.full];
[iq addAttributeWithName:@"type" stringValue:@"set"];
NSXMLElement *query = [NSXMLElement elementWithName:@"query" xmlns:@"urn:xmpp:muclight:0#create"];
NSXMLElement *configuration = [NSXMLElement elementWithName:@"configuration"];
[configuration addChild:[NSXMLElement elementWithName:@"roomname" stringValue:roomname]];
NSXMLElement *ocupants = [NSXMLElement elementWithName:@"ocupants"];
for (XMPPJID *jid in members){
NSXMLElement *userElement = [NSXMLElement elementWithName:@"user" stringValue:jid.bare];
[userElement addAttributeWithName:@"affiliation" stringValue:@"member"];
[ocupants addChild:userElement];
}
[query addChild:configuration];
[query addChild:ocupants];
[iq addChild:query];
[responseTracker addID:iqID
target:self
selector:@selector(handleCreateRoomLight:withInfo:)
timeout:60.0];
[xmppStream sendElement:iq];
}};
if (dispatch_get_specific(moduleQueueTag))
block();
else
dispatch_async(moduleQueue, block);
}
- (void)handleCreateRoomLight:(XMPPIQ *)iq withInfo:(id <XMPPTrackingInfo>)info{
if ([[iq type] isEqualToString:@"result"]){
[multicastDelegate xmppRoomLight:self didCreateRoomLight:iq];
}else{
[multicastDelegate xmppRoomLight:self didFailToCreateRoomLight:iq];
}
}
- (void)leaveRoomLight{
// <iq from='crone1@shakespeare.lit/desktop'
// id='member2'
// to='coven@chat.shakespeare.lit'
// type='set'>
// <query xmlns="urn:xmpp:muclight:0#affiliations">
// <item affiliation='none' jid='hag66@shakespeare.lit'/>
// </query>
// </iq>
dispatch_block_t block = ^{ @autoreleasepool {
NSString *iqID = [XMPPStream generateUUID];
NSXMLElement *iq = [NSXMLElement elementWithName:@"iq"];
[iq addAttributeWithName:@"id" stringValue:iqID];
[iq addAttributeWithName:@"to" stringValue:self.roomJID.full];
[iq addAttributeWithName:@"type" stringValue:@"set"];
NSXMLElement *query = [NSXMLElement elementWithName:@"query" xmlns:XMPPRoomLightAffiliations];
NSXMLElement *user = [NSXMLElement elementWithName:@"user"];
[user addAttributeWithName:@"affiliation" stringValue:@"none"];
user.stringValue = xmppStream.myJID.bare;
[query addChild:user];
[iq addChild:query];
[responseTracker addID:iqID
target:self
selector:@selector(handleLeaveRoomLight:withInfo:)
timeout:60.0];
[xmppStream sendElement:iq];
}};
if (dispatch_get_specific(moduleQueueTag))
block();
else
dispatch_async(moduleQueue, block);
}
- (void)handleLeaveRoomLight:(XMPPIQ *)iq withInfo:(id <XMPPTrackingInfo>)info{
if ([[iq type] isEqualToString:@"result"]){
[multicastDelegate xmppRoomLight:self didLeaveRoomLight:iq];
}else{
[multicastDelegate xmppRoomLight:self didFailToLeaveRoomLight:iq];
}
}
- (void)addUsers:(nonnull NSArray<XMPPJID *> *)users{
// <iq from="crone1@shakespeare.lit/desktop"
// id="member1"
// to="coven@chat.shakespeare.lit"
// type="set">
// <query xmlns="http://jabber.org/protocol/muc#admin">
// <item affiliation="member" jid="hag66@shakespeare.lit" />
// </query>
// </iq>
dispatch_block_t block = ^{ @autoreleasepool {
NSString *iqID = [XMPPStream generateUUID];
NSXMLElement *iq = [NSXMLElement elementWithName:@"iq"];
[iq addAttributeWithName:@"id" stringValue:iqID];
[iq addAttributeWithName:@"to" stringValue:self.roomJID.full];
[iq addAttributeWithName:@"type" stringValue:@"set"];
NSXMLElement *query = [NSXMLElement elementWithName:@"query" xmlns:XMPPRoomLightAffiliations];
for (XMPPJID *userJID in users) {
NSXMLElement *user = [NSXMLElement elementWithName:@"user"];
[user addAttributeWithName:@"affiliation" stringValue:@"member"];
user.stringValue = userJID.full;
[query addChild:user];
}
[iq addChild:query];
[responseTracker addID:iqID
target:self
selector:@selector(handleAddUsers:withInfo:)
timeout:60.0];
[xmppStream sendElement:iq];
}};
if (dispatch_get_specific(moduleQueueTag)){
block();
}else{
dispatch_async(moduleQueue, block);
}
}
- (void)handleAddUsers:(XMPPIQ *)iq withInfo:(id <XMPPTrackingInfo>)info{
if ([[iq type] isEqualToString:@"result"]){
[multicastDelegate xmppRoomLight:self didAddUsers:iq];
} else {
[multicastDelegate xmppRoomLight:self didFailToAddUsers:iq];
}
}
- (void)fetchMembersList{
dispatch_block_t block = ^{ @autoreleasepool {
// <iq from='crone1@shakespeare.lit/desktop' id='getmembers'
// to='coven@muclight.shakespeare.lit'
// type='get'>
// <query xmlns='urn:xmpp:muclight:0#affiliations'>
// <version>abcdefg</version>
// </query>
// </iq>
NSString *iqID = [XMPPStream generateUUID];
XMPPIQ *iq = [XMPPIQ iqWithType:@"get" to:_roomJID elementID:iqID];
NSXMLElement *query = [NSXMLElement elementWithName:@"query" xmlns:XMPPRoomLightAffiliations];
[query addChild:[NSXMLElement elementWithName:@"version" stringValue:self.memberListVersion]];
[iq addChild:query];
[responseTracker addID:iqID
target:self
selector:@selector(handleFetchMembersListResponse:withInfo:)
timeout:60.0];
[xmppStream sendElement:iq];
}};
if (dispatch_get_specific(moduleQueueTag))
block();
else
dispatch_async(moduleQueue, block);
}
- (void)handleFetchMembersListResponse:(XMPPIQ *)iq withInfo:(id <XMPPTrackingInfo>)info{
if ([[iq type] isEqualToString:@"result"]){
NSXMLElement *query = [iq elementForName:@"query"
xmlns:XMPPRoomLightAffiliations];
NSXMLElement *inVersion = [query elementForName:@"version"];
if(inVersion){
[self setMemberListVersion:inVersion.stringValue];
}
NSArray *items = [query elementsForName:@"user"];
if (!items) { items = @[]; }
[multicastDelegate xmppRoomLight:self didFetchMembersList:items];
}else{
[multicastDelegate xmppRoomLight:self didFailToFetchMembersList:iq];
}
}
- (void)destroyRoom {
dispatch_block_t block = ^{ @autoreleasepool {
// <iq from='crone1@shakespeare.lit/desktop'
// id='destroy1'
// to='coven@muclight.shakespeare.lit'
// type='set'>
// <query xmlns='urn:xmpp:muclight:0#destroy' />
// </iq>
NSString *iqID = [XMPPStream generateUUID];
XMPPIQ *iq = [XMPPIQ iqWithType:@"set" to:_roomJID elementID:iqID];
NSXMLElement *query = [NSXMLElement elementWithName:@"query" xmlns:XMPPRoomLightDestroy];
[iq addChild:query];
[responseTracker addID:iqID
target:self
selector:@selector(handleDestroyRoom:withInfo:)
timeout:60.0];
[xmppStream sendElement:iq];
}};
if (dispatch_get_specific(moduleQueueTag))
block();
else
dispatch_async(moduleQueue, block);
}
- (void)handleDestroyRoom:(XMPPIQ *)iq withInfo:(id <XMPPTrackingInfo>)info{
if ([[iq type] isEqualToString:@"result"]){
[multicastDelegate xmppRoomLight:self didDestroyRoomLight:iq];
} else {
[multicastDelegate xmppRoomLight:self didFailToDestroyRoomLight:iq];
}
}
- (void)sendMessage:(nonnull XMPPMessage *)message{
dispatch_block_t block = ^{ @autoreleasepool {
[message addAttributeWithName:@"to" stringValue:[_roomJID full]];
[message addAttributeWithName:@"type" stringValue:@"groupchat"];
[xmppStream sendElement:message];
}};
if (dispatch_get_specific(moduleQueueTag))
block();
else
dispatch_async(moduleQueue, block);
}
- (void)sendMessageWithBody:(nonnull NSString *)messageBody{
if ([messageBody length] == 0) return;
NSXMLElement *body = [NSXMLElement elementWithName:@"body" stringValue:messageBody];
XMPPMessage *message = [XMPPMessage message];
[message addChild:body];
[self sendMessage:message];
}
- (void)changeRoomSubject:(nonnull NSString *)roomSubject{
[self setConfiguration:@[[NSXMLElement elementWithName:@"subject" stringValue:roomSubject]]];
}
- (void)changeAffiliations:(nonnull NSArray<NSXMLElement *> *)members{
dispatch_block_t block = ^{ @autoreleasepool {
// <iq from='crone1@shakespeare.lit/desktop'
// id='member1'
// to='coven@muclight.shakespeare.lit'
// type='set'>
// <query xmlns='urn:xmpp:muclight:0#affiliations'>
// <user affiliation='member'>hag66@shakespeare.lit</user>
// <user affiliation='owner'>hag77@shakespeare.lit</user>
// <user affiliation='none'>hag88@shakespeare.lit</user>
// </query>
// </iq>
NSString *iqID = [XMPPStream generateUUID];
XMPPIQ *iq = [XMPPIQ iqWithType:@"set" to:_roomJID elementID:iqID];
NSXMLElement *query = [NSXMLElement elementWithName:@"query" xmlns:XMPPRoomLightAffiliations];
for (NSXMLElement *element in members){
[query addChild:element];
}
[iq addChild:query];
[responseTracker addID:iqID
target:self
selector:@selector(handleChangeAffiliations:withInfo:)
timeout:60.0];
[xmppStream sendElement:iq];
}};
if (dispatch_get_specific(moduleQueueTag))
block();
else
dispatch_async(moduleQueue, block);
}
- (void)handleChangeAffiliations:(XMPPIQ *)iq withInfo:(id <XMPPTrackingInfo>)info{
if ([[iq type] isEqualToString:@"result"]) {
[multicastDelegate xmppRoomLight:self didChangeAffiliations:iq];
}else{
[multicastDelegate xmppRoomLight:self didFailToChangeAffiliations:iq];
}
}
- (void)getConfiguration {
dispatch_block_t block = ^{ @autoreleasepool {
// <iq from='crone1@shakespeare.lit/desktop' id='config0'
// to='coven@muclight.shakespeare.lit'
// type='get'>
// <query xmlns='urn:xmpp:muclight:0#configuration'>
// <version>abcdefg</version>
// </query>
// </iq>
NSString *iqID = [XMPPStream generateUUID];
XMPPIQ *iq = [XMPPIQ iqWithType:@"get" to:_roomJID elementID:iqID];
NSXMLElement *query = [NSXMLElement elementWithName:@"query" xmlns:XMPPRoomLightConfiguration];
[query addChild:[NSXMLElement elementWithName:@"version" stringValue:self.configVersion]];
[iq addChild:query];
[responseTracker addID:iqID
target:self
selector:@selector(handleGetConfiguration:withInfo:)
timeout:60.0];
[xmppStream sendElement:iq];
}};
if (dispatch_get_specific(moduleQueueTag))
block();
else
dispatch_async(moduleQueue, block);
}
- (void)handleGetConfiguration:(XMPPIQ *)iq withInfo:(id <XMPPTrackingInfo>)info{
if ([[iq type] isEqualToString:@"result"]) {
NSXMLElement *query = [[iq elementsForLocalName:@"query" URI:XMPPRoomLightConfiguration] firstObject];
NSXMLElement *inVersion = [query elementForName:@"version"];
if(inVersion){
[self setConfigVersion:inVersion.stringValue];
}
NSArray *configElements = [[iq elementsForLocalName:@"query" URI:XMPPRoomLightConfiguration] firstObject].children;
[self handleConfigElements:configElements];
[multicastDelegate xmppRoomLight:self didGetConfiguration:iq];
}else{
[multicastDelegate xmppRoomLight:self didFailToGetConfiguration:iq];
}
}
- (void)setConfiguration:(nonnull NSArray<NSXMLElement *> *)configs{
dispatch_block_t block = ^{ @autoreleasepool {
// <iq from='crone1@shakespeare.lit/desktop' id='conf2' to='coven@muclight.shakespeare.lit' type='set'>
// <query xmlns='urn:xmpp:muclight:0#configuration'>
// <roomname>A Darker Cave</roomname>
// </query>
// </iq>
NSString *iqID = [XMPPStream generateUUID];
XMPPIQ *iq = [XMPPIQ iqWithType:@"set" to:_roomJID elementID:iqID];
NSXMLElement *query = [NSXMLElement elementWithName:@"query" xmlns:XMPPRoomLightConfiguration];
for (NSXMLElement *element in configs){
[query addChild:element];
}
[iq addChild:query];
[responseTracker addID:iqID
target:self
selector:@selector(handleSetConfiguration:withInfo:)
timeout:60.0];
[xmppStream sendElement:iq];
}};
if (dispatch_get_specific(moduleQueueTag))
block();
else
dispatch_async(moduleQueue, block);
}
- (void)handleSetConfiguration:(XMPPIQ *)iq withInfo:(id <XMPPTrackingInfo>)info{
if ([[iq type] isEqualToString:@"result"]) {
[multicastDelegate xmppRoomLight:self didSetConfiguration:iq];
}else{
[multicastDelegate xmppRoomLight:self didFailToSetConfiguration:iq];
}
}
- (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq{
NSString *type = [iq type];
if ([type isEqualToString:@"result"] || [type isEqualToString:@"error"]){
return [responseTracker invokeForID:[iq elementID] withObject:iq];
}
return NO;
}
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message{
XMPPJID *from = [message from];
if (![self.roomJID isEqualToJID:from options:XMPPJIDCompareBare]){
return; // Stanza isn't for our room
}
BOOL destroyRoom = false;
BOOL changeConfiguration = false;
NSArray <NSXMLElement*> *xElements = [message elementsForName:@"x"];
for (NSXMLElement *x in xElements) {
if ([x.xmlns isEqualToString:XMPPRoomLightDestroy]) {
destroyRoom = true;
} else if ([x.xmlns isEqualToString:XMPPRoomLightConfiguration]){
changeConfiguration = true;
}
}
// Is this a message we need to store (a chat message)?
//
// We store messages that from is full room-id@domain/user-who-sends-message
// and that have something in the body
if ([from isFull] && [message isGroupChatMessageWithBody]) {
[xmppRoomLightStorage handleIncomingMessage:message room:self];
[multicastDelegate xmppRoomLight:self didReceiveMessage:message];
}else if(destroyRoom){
[multicastDelegate xmppRoomLight:self roomDestroyed:message];
}else if(changeConfiguration){
NSArray *configElements = [message elementForName:@"x"].children;
[self handleConfigElements:configElements];
[multicastDelegate xmppRoomLight:self configurationChanged:message];
}else{
// Todo... Handle other types of messages.
}
}
- (void)xmppStream:(XMPPStream *)sender didSendMessage:(XMPPMessage *)message
{
XMPPJID *to = [message to];
if (![self.roomJID isEqualToJID:to options:XMPPJIDCompareBare]){
return; // Stanza isn't for our room
}
// Is this a message we need to store (a chat message)?
//
// A message to all recipients MUST be of type groupchat.
// A message to an individual recipient would have a <body/>.
if ([message isGroupChatMessageWithBody]){
[xmppRoomLightStorage handleOutgoingMessage:message room:self];
}
}
@end