-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
XMPPMessageArchiveManagement.m
225 lines (177 loc) · 7.37 KB
/
XMPPMessageArchiveManagement.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
//
// XMPPMessageArchiveManagement.m
//
// Created by Andres Canal on 4/8/16.
// Copyright © 2016 Inaka. All rights reserved.
//
#import "XMPPMessageArchiveManagement.h"
#import "XMPPFramework.h"
#import "XMPPLogging.h"
#import "XMPPIDTracker.h"
#import "NSXMLElement+XEP_0297.h"
#import "XMPPLogging.h"
#import "XMPPMessage+XEP_0313.h"
// Log levels: off, error, warn, info, verbose
#if DEBUG
static const int xmppLogLevel = XMPP_LOG_LEVEL_INFO | XMPP_LOG_FLAG_SEND_RECV; // | XMPP_LOG_FLAG_TRACE;
#else
static const int xmppLogLevel = XMPP_LOG_LEVEL_WARN;
#endif
NSString *const XMLNS_XMPP_MAM = @"urn:xmpp:mam:2";
static NSString *const QueryIdAttributeName = @"queryid";
@interface XMPPMessageArchiveManagement()
/** Only safe to access on moduleQueue. */
@property (nonatomic, strong, readonly, nonnull) NSMutableSet<NSString*> *outstandingQueryIds;
/** Setup in activate: */
@property (strong, nonatomic, nullable, readonly) XMPPIDTracker *xmppIDTracker;
@end
@implementation XMPPMessageArchiveManagement
@synthesize resultAutomaticPagingPageSize = _resultAutomaticPagingPageSize;
@synthesize xmppIDTracker = _xmppIDTracker;
- (instancetype) initWithDispatchQueue:(dispatch_queue_t)queue {
if (self = [super initWithDispatchQueue:queue]) {
_outstandingQueryIds = [NSMutableSet set];
}
return self;
}
- (NSInteger)resultAutomaticPagingPageSize
{
__block NSInteger result = NO;
[self performBlock:^{
result = self->_resultAutomaticPagingPageSize;
}];
return result;
}
- (void)setResultAutomaticPagingPageSize:(NSInteger)resultAutomaticPagingPageSize
{
[self performBlockAsync:^{
self->_resultAutomaticPagingPageSize = resultAutomaticPagingPageSize;
}];
}
- (void)retrieveMessageArchiveWithFields:(NSArray *)fields withResultSet:(XMPPResultSet *)resultSet {
[self retrieveMessageArchiveAt:nil withFields:fields withResultSet:resultSet];
}
- (void)retrieveMessageArchiveAt:(XMPPJID *)archiveJID withFields:(NSArray *)fields withResultSet:(XMPPResultSet *)resultSet {
NSXMLElement *formElement = [NSXMLElement elementWithName:@"x" xmlns:@"jabber:x:data"];
[formElement addAttributeWithName:@"type" stringValue:@"submit"];
[formElement addChild:[XMPPMessageArchiveManagement fieldWithVar:@"FORM_TYPE" type:@"hidden" andValue:XMLNS_XMPP_MAM]];
for (NSXMLElement *field in fields) {
[formElement addChild:field];
}
[self retrieveMessageArchiveAt:archiveJID withFormElement:formElement resultSet:resultSet];
}
- (void)retrieveMessageArchiveAt:(XMPPJID *)archiveJID withFormElement:(NSXMLElement *)formElement resultSet:(XMPPResultSet *)resultSet {
[self performBlockAsync:^{
XMPPIQ *iq = [XMPPIQ iqWithType:@"set"];
[iq addAttributeWithName:@"id" stringValue:[XMPPStream generateUUID]];
if (archiveJID) {
[iq addAttributeWithName:@"to" stringValue:[archiveJID full]];
}
NSString *queryId = [XMPPStream generateUUID];
[self->_outstandingQueryIds addObject:queryId];
NSXMLElement *queryElement = [NSXMLElement elementWithName:@"query" xmlns:XMLNS_XMPP_MAM];
[queryElement addAttributeWithName:QueryIdAttributeName stringValue:queryId];
[iq addChild:queryElement];
[queryElement addChild:formElement];
if (resultSet) {
[queryElement addChild:resultSet];
}
[self.xmppIDTracker addElement:iq
target:self
selector:@selector(handleMessageArchiveIQ:withInfo:)
timeout:60];
[self->xmppStream sendElement:iq];
}];
}
- (void)handleMessageArchiveIQ:(XMPPIQ *)iq withInfo:(XMPPBasicTrackingInfo *)trackerInfo {
if ([[iq type] isEqualToString:@"result"]) {
NSXMLElement *finElement = [iq elementForName:@"fin" xmlns:XMLNS_XMPP_MAM];
NSString *queryId = [finElement attributeStringValueForName:QueryIdAttributeName];
NSXMLElement *setElement = [finElement elementForName:@"set" xmlns:@"http://jabber.org/protocol/rsm"];
XMPPResultSet *resultSet = [XMPPResultSet resultSetFromElement:setElement];
NSString *lastId = [resultSet elementForName:@"last"].stringValue;
if (self.resultAutomaticPagingPageSize == 0 || [finElement attributeBoolValueForName:@"complete"] || !lastId) {
if (queryId.length) {
[self.outstandingQueryIds removeObject:queryId];
}
[multicastDelegate xmppMessageArchiveManagement:self didFinishReceivingMessagesWithSet:resultSet];
return;
}
XMPPIQ *originalIq = [XMPPIQ iqFromElement:[trackerInfo element]];
XMPPJID *originalArchiveJID = [originalIq to];
NSXMLElement *originalFormElement = [[[originalIq elementForName:@"query"] elementForName:@"x"] copy];
XMPPResultSet *pagingResultSet = [[XMPPResultSet alloc] initWithMax:self.resultAutomaticPagingPageSize after:lastId];
[self retrieveMessageArchiveAt:originalArchiveJID withFormElement:originalFormElement resultSet:pagingResultSet];
} else {
[multicastDelegate xmppMessageArchiveManagement:self didFailToReceiveMessages:iq];
}
}
+ (NSXMLElement *)fieldWithVar:(NSString *)var type:(NSString *)type andValue:(NSString *)value {
NSXMLElement *field = [NSXMLElement elementWithName:@"field"];
[field addAttributeWithName:@"var" stringValue:var];
if(type){
[field addAttributeWithName:@"type" stringValue:type];
}
NSXMLElement *elementValue = [NSXMLElement elementWithName:@"value"];
elementValue.stringValue = value;
[field addChild:elementValue];
return field;
}
- (void)retrieveFormFields {
[self performBlockAsync:^{
XMPPIQ *iq = [XMPPIQ iqWithType:@"get"];
[iq addAttributeWithName:@"id" stringValue:[XMPPStream generateUUID]];
NSXMLElement *queryElement = [NSXMLElement elementWithName:@"query" xmlns:XMLNS_XMPP_MAM];
[iq addChild:queryElement];
[self.xmppIDTracker addElement:iq
target:self
selector:@selector(handleFormFieldsIQ:withInfo:)
timeout:60];
[self->xmppStream sendElement:iq];
}];
}
- (void)handleFormFieldsIQ:(XMPPIQ *)iq withInfo:(XMPPBasicTrackingInfo *)trackerInfo {
if ([[iq type] isEqualToString:@"result"]) {
[multicastDelegate xmppMessageArchiveManagement:self didReceiveFormFields:iq];
} else {
[multicastDelegate xmppMessageArchiveManagement:self didFailToReceiveFormFields:iq];
}
}
- (BOOL)activate:(XMPPStream *)aXmppStream {
if ([super activate:aXmppStream]) {
_xmppIDTracker = [[XMPPIDTracker alloc] initWithDispatchQueue:moduleQueue];
return YES;
}
return NO;
}
- (void)deactivate {
[self performBlock:^{ @autoreleasepool {
[self.xmppIDTracker removeAllIDs];
self->_xmppIDTracker = nil;
}}];
[super deactivate];
}
#pragma mark XMPPStream Delegate
- (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq
{
NSString *type = [iq type];
if ([type isEqualToString:@"result"] || [type isEqualToString:@"error"])
{
return [self.xmppIDTracker invokeForID:[iq elementID] withObject:iq];
}
return NO;
}
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message {
NSXMLElement *result = message.mamResult;
BOOL forwarded = result.hasForwardedStanza;
if (!forwarded) {
return;
}
NSString *queryID = [result attributeForName:QueryIdAttributeName].stringValue;
if (queryID.length && [self.outstandingQueryIds containsObject:queryID]) {
[multicastDelegate xmppMessageArchiveManagement:self didReceiveMAMMessage:message];
} else {
XMPPLogWarn(@"Received unexpected MAM response queryid %@", queryID);
}
}
@end