XMPPMUCLight.m
8.83 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
//
// XMPPMUCLight.m
// Mangosta
//
// Created by Andres on 5/30/16.
// Copyright © 2016 Inaka. All rights reserved.
//
#import "XMPPMUC.h"
#import "XMPPFramework.h"
#import "XMPPLogging.h"
#import "XMPPIDTracker.h"
#import "XMPPMUCLight.h"
#import "XMPPRoomLight.h"
NSString *const XMPPMUCLightDiscoItemsNamespace = @"http://jabber.org/protocol/disco#items";
NSString *const XMPPRoomLightAffiliations = @"urn:xmpp:muclight:0#affiliations";
NSString *const XMPPMUCLightErrorDomain = @"XMPPMUCErrorDomain";
NSString *const XMPPMUCLightBlocking = @"urn:xmpp:muclight:0#blocking";
@interface XMPPMUCLight() {
NSMutableSet *rooms;
}
@end
@implementation XMPPMUCLight
- (instancetype)init {
return [self initWithDispatchQueue:nil];
}
- (instancetype)initWithDispatchQueue:(dispatch_queue_t)queue {
if ((self = [super initWithDispatchQueue:queue])) {
rooms = [[NSMutableSet alloc] init];
}
return self;
}
- (BOOL)activate:(XMPPStream *)aXmppStream {
if ([super activate:aXmppStream]) {
xmppIDTracker = [[XMPPIDTracker alloc] initWithDispatchQueue:moduleQueue];
return YES;
}
return NO;
}
- (void)deactivate {
dispatch_block_t block = ^{ @autoreleasepool {
[xmppIDTracker removeAllIDs];
xmppIDTracker = nil;
}};
if (dispatch_get_specific(moduleQueueTag))
block();
else
dispatch_sync(moduleQueue, block);
[super deactivate];
}
- (nonnull NSSet *)rooms{
@synchronized(rooms) {
return [rooms copy];
}
}
- (BOOL)discoverRoomsForServiceNamed:(nonnull NSString *)serviceName {
if (serviceName.length < 2)
return NO;
dispatch_block_t block = ^{ @autoreleasepool {
NSXMLElement *query = [NSXMLElement elementWithName:@"query"
xmlns:XMPPMUCLightDiscoItemsNamespace];
XMPPIQ *iq = [XMPPIQ iqWithType:@"get"
to:[XMPPJID jidWithString:serviceName]
elementID:[xmppStream generateUUID]
child:query];
[xmppIDTracker addElement:iq
target:self
selector:@selector(handleDiscoverRoomsQueryIQ:withInfo:)
timeout:60];
[xmppStream sendElement:iq];
}};
if (dispatch_get_specific(moduleQueueTag))
block();
else
dispatch_async(moduleQueue, block);
return YES;
}
- (void)handleDiscoverRoomsQueryIQ:(XMPPIQ *)iq withInfo:(XMPPBasicTrackingInfo *)info {
dispatch_block_t block = ^{ @autoreleasepool {
NSXMLElement *errorElem = [iq elementForName:@"error"];
NSString *serviceName = [iq attributeStringValueForName:@"from" withDefaultValue:@""];
if (errorElem) {
NSString *errMsg = [errorElem.children componentsJoinedByString:@", "];
NSInteger errorCode = [errorElem attributeIntegerValueForName:@"code" withDefaultValue:0];
NSDictionary *dict = @{NSLocalizedDescriptionKey : errMsg};
NSError *error = [NSError errorWithDomain:XMPPMUCLightErrorDomain
code:errorCode
userInfo:dict];
[multicastDelegate xmppMUCLight:self failedToDiscoverRoomsForServiceNamed:serviceName withError:error];
return;
}
NSXMLElement *query = [iq elementForName:@"query"
xmlns:XMPPMUCLightDiscoItemsNamespace];
NSArray *items = [query elementsForName:@"item"];
[multicastDelegate xmppMUCLight:self didDiscoverRooms:items forServiceNamed:serviceName];
}};
if (dispatch_get_specific(moduleQueueTag))
block();
else
dispatch_async(moduleQueue, block);
}
- (BOOL)requestBlockingList:(nonnull NSString *)serviceName{
if (serviceName.length < 2)
return NO;
// <iq from='crone1@shakespeare.lit/desktop' id='getblock1' to='muclight.shakespeare.lit' type='get'>
// <query xmlns='urn:xmpp:muclight:0#blocking'> </query>
// </iq>
dispatch_block_t block = ^{ @autoreleasepool {
NSXMLElement *query = [NSXMLElement elementWithName:@"query"
xmlns:XMPPMUCLightBlocking];
XMPPIQ *iq = [XMPPIQ iqWithType:@"get"
to:[XMPPJID jidWithString:serviceName]
elementID:[xmppStream generateUUID]
child:query];
[xmppIDTracker addElement:iq
target:self
selector:@selector(handleRequestBlockingList:withInfo:)
timeout:60];
[xmppStream sendElement:iq];
}};
if (dispatch_get_specific(moduleQueueTag))
block();
else
dispatch_async(moduleQueue, block);
return YES;
}
- (void)handleRequestBlockingList:(XMPPIQ *)iq withInfo:(XMPPBasicTrackingInfo *)info {
NSString *serviceName = [iq attributeStringValueForName:@"from" withDefaultValue:@""];
if ([[iq type] isEqualToString:@"result"]) {
NSXMLElement *query = [iq elementForName:@"query"];
NSArray *children = [query children];
if (!children) { children = @[]; }
[multicastDelegate xmppMUCLight:self didRequestBlockingList:children forServiceNamed:serviceName];
}else{
[multicastDelegate xmppMUCLight:self failedToRequestBlockingList:serviceName withError:iq];
}
}
- (BOOL)performActionOnElements:(nonnull NSArray<NSXMLElement *> *)elements forServiceNamed:(nonnull NSString *)serviceName{
if (serviceName.length < 2)
return NO;
// <iq from='crone1@shakespeare.lit/desktop' id='block2' to='muclight.shakespeare.lit' type='set'>
// <query xmlns='urn:xmpp:muclight:0#blocking'>
// <user action='deny'>hag66@shakespeare.lit</room>
// <user action='deny'>hag77@shakespeare.lit</room>
// </query>
// </iq>
dispatch_block_t block = ^{ @autoreleasepool {
NSXMLElement *query = [NSXMLElement elementWithName:@"query"
xmlns:XMPPMUCLightBlocking];
for (NSXMLElement *element in elements) {
[query addChild:element];
}
XMPPIQ *iq = [XMPPIQ iqWithType:@"set"
to:[XMPPJID jidWithString:serviceName]
elementID:[xmppStream generateUUID]
child:query];
[xmppIDTracker addElement:iq
target:self
selector:@selector(handlePerformAction:withInfo:)
timeout:60];
[xmppStream sendElement:iq];
}};
if (dispatch_get_specific(moduleQueueTag))
block();
else
dispatch_async(moduleQueue, block);
return YES;
}
- (void)handlePerformAction:(XMPPIQ *)iq withInfo:(XMPPBasicTrackingInfo *)info {
//NSString *serviceName = [iq attributeStringValueForName:@"from" withDefaultValue:@""];
if ([[iq type] isEqualToString:@"result"]) {
//NSXMLElement *query = [iq elementForName:@"query"];
[multicastDelegate xmppMUCLight:self didPerformAction:iq];
}else{
[multicastDelegate xmppMUCLight:self failedToPerformAction:iq];
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark XMPPStream Delegate
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message {
// <message from='coven@muclight.shakespeare.lit'
// to='user2@shakespeare.lit'
// type='groupchat'
// id='createnotif'>
// <x xmlns='urn:xmpp:muclight:0#affiliations'>
// <version>aaaaaaa</version>
// <user affiliation='member'>user2@shakespeare.lit</user>
// </x>
// <body />
// </message>
XMPPJID *from = message.from;
NSXMLElement *x = [message elementForName:@"x" xmlns:XMPPRoomLightAffiliations];
NSXMLElement *user = [x elementForName:@"user"];
NSString *affiliation = [user attributeForName:@"affiliation"].stringValue;
if (affiliation) {
[multicastDelegate xmppMUCLight:self changedAffiliation:affiliation roomJID:from];
}
}
- (void)xmppStream:(XMPPStream *)sender didRegisterModule:(id)module {
dispatch_block_t block = ^{ @autoreleasepool {
if ([module isKindOfClass:[XMPPRoomLight class]]){
XMPPJID *roomJID = [(XMPPRoomLight *)module roomJID];
[rooms addObject:roomJID];
}
}};
if (dispatch_get_specific(moduleQueueTag))
block();
else
dispatch_async(moduleQueue, block);
}
- (void)xmppStream:(XMPPStream *)sender willUnregisterModule:(id)module {
dispatch_block_t block = ^{ @autoreleasepool {
if ([module isKindOfClass:[XMPPRoomLight class]]){
XMPPJID *roomJID = [(XMPPRoomLight *)module roomJID];
// It's common for the room to get deactivated and deallocated before
// we've received the goodbye presence from the server.
// So we're going to postpone for a bit removing the roomJID from the list.
// This way the isMUCRoomElement will still remain accurate
// for presence elements that may arrive momentarily.
double delayInSeconds = [self delayInSeconds];
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, moduleQueue, ^{ @autoreleasepool {
[rooms removeObject:roomJID];
}});
}
}};
if (dispatch_get_specific(moduleQueueTag))
block();
else
dispatch_async(moduleQueue, block);
}
- (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq {
NSString *type = [iq type];
if ([type isEqualToString:@"result"] || [type isEqualToString:@"error"]) {
return [xmppIDTracker invokeForID:[iq elementID] withObject:iq];
}
return NO;
}
- (double) delayInSeconds {
return 30.0;
}
@end