1package eu.siacs.conversations.services;
2
3import android.util.Log;
4import android.util.Pair;
5
6import java.math.BigInteger;
7import java.util.ArrayList;
8import java.util.HashSet;
9import java.util.Iterator;
10import java.util.List;
11
12import eu.siacs.conversations.Config;
13import eu.siacs.conversations.R;
14import eu.siacs.conversations.entities.Account;
15import eu.siacs.conversations.entities.Conversation;
16import eu.siacs.conversations.generator.AbstractGenerator;
17import eu.siacs.conversations.xml.Namespace;
18import eu.siacs.conversations.xml.Element;
19import eu.siacs.conversations.xmpp.OnAdvancedStreamFeaturesLoaded;
20import eu.siacs.conversations.xmpp.OnIqPacketReceived;
21import eu.siacs.conversations.xmpp.jid.Jid;
22import eu.siacs.conversations.xmpp.mam.MamReference;
23import eu.siacs.conversations.xmpp.stanzas.IqPacket;
24
25public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded {
26
27 private final XmppConnectionService mXmppConnectionService;
28
29 private final HashSet<Query> queries = new HashSet<>();
30 private final ArrayList<Query> pendingQueries = new ArrayList<>();
31
32 public enum PagingOrder {
33 NORMAL,
34 REVERSE
35 }
36
37 public MessageArchiveService(final XmppConnectionService service) {
38 this.mXmppConnectionService = service;
39 }
40
41 private void catchup(final Account account) {
42 synchronized (this.queries) {
43 for(Iterator<Query> iterator = this.queries.iterator(); iterator.hasNext();) {
44 Query query = iterator.next();
45 if (query.getAccount() == account) {
46 iterator.remove();
47 }
48 }
49 }
50 MamReference mamReference = MamReference.max(
51 mXmppConnectionService.databaseBackend.getLastMessageReceived(account),
52 mXmppConnectionService.databaseBackend.getLastClearDate(account)
53 );
54 mamReference = MamReference.max(mamReference,mXmppConnectionService.getAutomaticMessageDeletionDate());
55 long endCatchup = account.getXmppConnection().getLastSessionEstablished();
56 final Query query;
57 if (mamReference.getTimestamp() == 0) {
58 return;
59 } else if (endCatchup - mamReference.getTimestamp() >= Config.MAM_MAX_CATCHUP) {
60 long startCatchup = endCatchup - Config.MAM_MAX_CATCHUP;
61 List<Conversation> conversations = mXmppConnectionService.getConversations();
62 for (Conversation conversation : conversations) {
63 if (conversation.getMode() == Conversation.MODE_SINGLE && conversation.getAccount() == account && startCatchup > conversation.getLastMessageTransmitted().getTimestamp()) {
64 this.query(conversation,startCatchup,true);
65 }
66 }
67 query = new Query(account, new MamReference(startCatchup), endCatchup);
68 } else {
69 query = new Query(account, mamReference, endCatchup);
70 }
71 synchronized (this.queries) {
72 this.queries.add(query);
73 }
74 this.execute(query);
75 }
76
77 public void catchupMUC(final Conversation conversation) {
78 if (conversation.getLastMessageTransmitted().getTimestamp() < 0 && conversation.countMessages() == 0) {
79 query(conversation,
80 new MamReference(0),
81 System.currentTimeMillis(),
82 true);
83 } else {
84 query(conversation,
85 conversation.getLastMessageTransmitted(),
86 System.currentTimeMillis(),
87 true);
88 }
89 }
90
91 public Query query(final Conversation conversation) {
92 if (conversation.getLastMessageTransmitted().getTimestamp() < 0 && conversation.countMessages() == 0) {
93 return query(conversation,
94 new MamReference(0),
95 System.currentTimeMillis(),
96 false);
97 } else {
98 return query(conversation,
99 conversation.getLastMessageTransmitted(),
100 conversation.getAccount().getXmppConnection().getLastSessionEstablished(),
101 false);
102 }
103 }
104
105 public Query query(final Conversation conversation, long end, boolean allowCatchup) {
106 return this.query(conversation,conversation.getLastMessageTransmitted(),end, allowCatchup);
107 }
108
109 public Query query(Conversation conversation, MamReference start, long end, boolean allowCatchup) {
110 synchronized (this.queries) {
111 final Query query;
112 final MamReference startActual = MamReference.max(start,mXmppConnectionService.getAutomaticMessageDeletionDate());
113 if (start.getTimestamp() == 0) {
114 query = new Query(conversation, startActual, end, false);
115 query.reference = conversation.getFirstMamReference();
116 } else {
117 if (allowCatchup) {
118 MamReference maxCatchup = MamReference.max(startActual, System.currentTimeMillis() - Config.MAM_MAX_CATCHUP);
119 if (maxCatchup.greaterThan(startActual)) {
120 Query reverseCatchup = new Query(conversation, startActual, maxCatchup.getTimestamp(), false);
121 this.queries.add(reverseCatchup);
122 this.execute(reverseCatchup);
123 }
124 query = new Query(conversation, maxCatchup, end, allowCatchup);
125 } else {
126 query = new Query(conversation, startActual, end, false);
127 }
128 }
129 if (start.greaterThan(end)) {
130 return null;
131 }
132 this.queries.add(query);
133 this.execute(query);
134 return query;
135 }
136 }
137
138 public void executePendingQueries(final Account account) {
139 List<Query> pending = new ArrayList<>();
140 synchronized(this.pendingQueries) {
141 for(Iterator<Query> iterator = this.pendingQueries.iterator(); iterator.hasNext();) {
142 Query query = iterator.next();
143 if (query.getAccount() == account) {
144 pending.add(query);
145 iterator.remove();
146 }
147 }
148 }
149 for(Query query : pending) {
150 this.execute(query);
151 }
152 }
153
154 private void execute(final Query query) {
155 final Account account= query.getAccount();
156 if (account.getStatus() == Account.State.ONLINE) {
157 Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": running mam query " + query.toString());
158 IqPacket packet = this.mXmppConnectionService.getIqGenerator().queryMessageArchiveManagement(query);
159 this.mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() {
160 @Override
161 public void onIqPacketReceived(Account account, IqPacket packet) {
162 Element fin = packet.findChild("fin", Namespace.MAM);
163 if (packet.getType() == IqPacket.TYPE.TIMEOUT) {
164 synchronized (MessageArchiveService.this.queries) {
165 MessageArchiveService.this.queries.remove(query);
166 if (query.hasCallback()) {
167 query.callback(false);
168 }
169 }
170 } else if (packet.getType() == IqPacket.TYPE.RESULT && fin != null ) {
171 processFin(fin);
172 } else if (packet.getType() == IqPacket.TYPE.RESULT && query.isLegacy()) {
173 //do nothing
174 } else {
175 Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": error executing mam: " + packet.toString());
176 finalizeQuery(query, true);
177 }
178 }
179 });
180 } else {
181 synchronized (this.pendingQueries) {
182 this.pendingQueries.add(query);
183 }
184 }
185 }
186
187 private void finalizeQuery(Query query, boolean done) {
188 synchronized (this.queries) {
189 this.queries.remove(query);
190 }
191 final Conversation conversation = query.getConversation();
192 if (conversation != null) {
193 conversation.sort();
194 conversation.setHasMessagesLeftOnServer(!done);
195 } else {
196 for(Conversation tmp : this.mXmppConnectionService.getConversations()) {
197 if (tmp.getAccount() == query.getAccount()) {
198 tmp.sort();
199 }
200 }
201 }
202 if (query.hasCallback()) {
203 query.callback(done);
204 } else {
205 this.mXmppConnectionService.updateConversationUi();
206 }
207 }
208
209 public boolean queryInProgress(Conversation conversation, XmppConnectionService.OnMoreMessagesLoaded callback) {
210 synchronized (this.queries) {
211 for(Query query : queries) {
212 if (query.conversation == conversation) {
213 if (!query.hasCallback() && callback != null) {
214 query.setCallback(callback);
215 }
216 return true;
217 }
218 }
219 return false;
220 }
221 }
222
223 public boolean queryInProgress(Conversation conversation) {
224 return queryInProgress(conversation, null);
225 }
226
227 public void processFinLegacy(Element fin, Jid from) {
228 Query query = findQuery(fin.getAttribute("queryid"));
229 if (query != null && query.validFrom(from)) {
230 processFin(fin);
231 }
232 }
233
234 public void processFin(Element fin) {
235 Query query = findQuery(fin.getAttribute("queryid"));
236 if (query == null) {
237 return;
238 }
239 boolean complete = fin.getAttributeAsBoolean("complete");
240 Element set = fin.findChild("set","http://jabber.org/protocol/rsm");
241 Element last = set == null ? null : set.findChild("last");
242 Element first = set == null ? null : set.findChild("first");
243 Element relevant = query.getPagingOrder() == PagingOrder.NORMAL ? last : first;
244 boolean abort = (!query.isCatchup() && query.getTotalCount() >= Config.PAGE_SIZE) || query.getTotalCount() >= Config.MAM_MAX_MESSAGES;
245 if (query.getConversation() != null) {
246 query.getConversation().setFirstMamReference(first == null ? null : first.getContent());
247 }
248 if (complete || relevant == null || abort) {
249 final boolean done = (complete || query.getActualMessageCount() == 0) && !query.isCatchup();
250 this.finalizeQuery(query, done);
251 Log.d(Config.LOGTAG,query.getAccount().getJid().toBareJid()+": finished mam after "+query.getTotalCount()+"("+query.getActualMessageCount()+") messages. messages left="+Boolean.toString(!done));
252 if (query.isCatchup() && query.getActualMessageCount() > 0) {
253 mXmppConnectionService.getNotificationService().finishBacklog(true,query.getAccount());
254 }
255 } else {
256 final Query nextQuery;
257 if (query.getPagingOrder() == PagingOrder.NORMAL) {
258 nextQuery = query.next(last == null ? null : last.getContent());
259 } else {
260 nextQuery = query.prev(first == null ? null : first.getContent());
261 }
262 this.execute(nextQuery);
263 this.finalizeQuery(query, false);
264 synchronized (this.queries) {
265 this.queries.add(nextQuery);
266 }
267 }
268 }
269
270 public Query findQuery(String id) {
271 if (id == null) {
272 return null;
273 }
274 synchronized (this.queries) {
275 for(Query query : this.queries) {
276 if (query.getQueryId().equals(id)) {
277 return query;
278 }
279 }
280 return null;
281 }
282 }
283
284 @Override
285 public void onAdvancedStreamFeaturesAvailable(Account account) {
286 if (account.getXmppConnection() != null && account.getXmppConnection().getFeatures().mam()) {
287 this.catchup(account);
288 }
289 }
290
291 public class Query {
292 private int totalCount = 0;
293 private int actualCount = 0;
294 private long start;
295 private long end;
296 private String queryId;
297 private String reference = null;
298 private Account account;
299 private Conversation conversation;
300 private PagingOrder pagingOrder = PagingOrder.NORMAL;
301 private XmppConnectionService.OnMoreMessagesLoaded callback = null;
302 private boolean catchup = true;
303
304
305 public Query(Conversation conversation, MamReference start, long end, boolean catchup) {
306 this(conversation.getAccount(),catchup ? start : start.timeOnly(),end);
307 this.conversation = conversation;
308 this.pagingOrder = catchup ? PagingOrder.NORMAL : PagingOrder.REVERSE;
309 this.catchup = catchup;
310 }
311
312 public Query(Account account, MamReference start, long end) {
313 this.account = account;
314 if (start.getReference() != null) {
315 this.reference = start.getReference();
316 } else {
317 this.start = start.getTimestamp();
318 }
319 this.end = end;
320 this.queryId = new BigInteger(50, mXmppConnectionService.getRNG()).toString(32);
321 }
322
323 private Query page(String reference) {
324 Query query = new Query(this.account,new MamReference(this.start,reference),this.end);
325 query.conversation = conversation;
326 query.totalCount = totalCount;
327 query.actualCount = actualCount;
328 query.callback = callback;
329 query.catchup = catchup;
330 return query;
331 }
332
333 public boolean isLegacy() {
334 if (conversation == null || conversation.getMode() == Conversation.MODE_SINGLE) {
335 return account.getXmppConnection().getFeatures().mamLegacy();
336 } else {
337 return conversation.getMucOptions().mamLegacy();
338 }
339 }
340
341 public Query next(String reference) {
342 Query query = page(reference);
343 query.pagingOrder = PagingOrder.NORMAL;
344 return query;
345 }
346
347 public Query prev(String reference) {
348 Query query = page(reference);
349 query.pagingOrder = PagingOrder.REVERSE;
350 return query;
351 }
352
353 public String getReference() {
354 return reference;
355 }
356
357 public PagingOrder getPagingOrder() {
358 return this.pagingOrder;
359 }
360
361 public String getQueryId() {
362 return queryId;
363 }
364
365 public Jid getWith() {
366 return conversation == null ? null : conversation.getJid().toBareJid();
367 }
368
369 public boolean muc() {
370 return conversation != null && conversation.getMode() == Conversation.MODE_MULTI;
371 }
372
373 public long getStart() {
374 return start;
375 }
376
377 public boolean isCatchup() {
378 return catchup;
379 }
380
381 public void setCallback(XmppConnectionService.OnMoreMessagesLoaded callback) {
382 this.callback = callback;
383 }
384
385 public void callback(boolean done) {
386 if (this.callback != null) {
387 this.callback.onMoreMessagesLoaded(actualCount,conversation);
388 if (done) {
389 this.callback.informUser(R.string.no_more_history_on_server);
390 }
391 }
392 }
393
394 public long getEnd() {
395 return end;
396 }
397
398 public Conversation getConversation() {
399 return conversation;
400 }
401
402 public Account getAccount() {
403 return this.account;
404 }
405
406 public void incrementMessageCount() {
407 this.totalCount++;
408 }
409
410 public void incrementActualMessageCount() {
411 this.actualCount++;
412 }
413
414 public int getTotalCount() {
415 return this.totalCount;
416 }
417
418 public int getActualMessageCount() {
419 return this.actualCount;
420 }
421
422 public boolean validFrom(Jid from) {
423 if (muc()) {
424 return getWith().equals(from);
425 } else {
426 return (from == null) || account.getJid().toBareJid().equals(from.toBareJid());
427 }
428 }
429
430 @Override
431 public String toString() {
432 StringBuilder builder = new StringBuilder();
433 if (this.muc()) {
434 builder.append("to=");
435 builder.append(this.getWith().toString());
436 } else {
437 builder.append("with=");
438 if (this.getWith() == null) {
439 builder.append("*");
440 } else {
441 builder.append(getWith().toString());
442 }
443 }
444 if (this.start != 0) {
445 builder.append(", start=");
446 builder.append(AbstractGenerator.getTimestamp(this.start));
447 }
448 builder.append(", end=");
449 builder.append(AbstractGenerator.getTimestamp(this.end));
450 builder.append(", order="+pagingOrder.toString());
451 if (this.reference!=null) {
452 if (this.pagingOrder == PagingOrder.NORMAL) {
453 builder.append(", after=");
454 } else {
455 builder.append(", before=");
456 }
457 builder.append(this.reference);
458 }
459 builder.append(", catchup="+Boolean.toString(catchup));
460 return builder.toString();
461 }
462
463 public boolean hasCallback() {
464 return this.callback != null;
465 }
466 }
467}