1package eu.siacs.conversations.services;
2
3import android.util.Log;
4
5import java.math.BigInteger;
6import java.util.ArrayList;
7import java.util.HashSet;
8import java.util.Iterator;
9import java.util.List;
10
11import eu.siacs.conversations.Config;
12import eu.siacs.conversations.R;
13import eu.siacs.conversations.entities.Account;
14import eu.siacs.conversations.entities.Conversation;
15import eu.siacs.conversations.entities.ReceiptRequest;
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 boolean isCatchingUp(Conversation conversation) {
106 final Account account = conversation.getAccount();
107 if (account.getXmppConnection().isWaitingForSmCatchup()) {
108 return true;
109 } else {
110 synchronized (this.queries) {
111 for(Query query : this.queries) {
112 if (query.getAccount() == account && query.isCatchup() && ((conversation.getMode() == Conversation.MODE_SINGLE && query.getWith() == null) || query.getConversation() == conversation)) {
113 return true;
114 }
115 }
116 }
117 return false;
118 }
119 }
120
121 public Query query(final Conversation conversation, long end, boolean allowCatchup) {
122 return this.query(conversation,conversation.getLastMessageTransmitted(),end, allowCatchup);
123 }
124
125 public Query query(Conversation conversation, MamReference start, long end, boolean allowCatchup) {
126 synchronized (this.queries) {
127 final Query query;
128 final MamReference startActual = MamReference.max(start,mXmppConnectionService.getAutomaticMessageDeletionDate());
129 if (start.getTimestamp() == 0) {
130 query = new Query(conversation, startActual, end, false);
131 query.reference = conversation.getFirstMamReference();
132 } else {
133 if (allowCatchup) {
134 MamReference maxCatchup = MamReference.max(startActual, System.currentTimeMillis() - Config.MAM_MAX_CATCHUP);
135 if (maxCatchup.greaterThan(startActual)) {
136 Query reverseCatchup = new Query(conversation, startActual, maxCatchup.getTimestamp(), false);
137 this.queries.add(reverseCatchup);
138 this.execute(reverseCatchup);
139 }
140 query = new Query(conversation, maxCatchup, end, allowCatchup);
141 } else {
142 query = new Query(conversation, startActual, end, false);
143 }
144 }
145 if (start.greaterThan(end)) {
146 return null;
147 }
148 this.queries.add(query);
149 this.execute(query);
150 return query;
151 }
152 }
153
154 public void executePendingQueries(final Account account) {
155 List<Query> pending = new ArrayList<>();
156 synchronized(this.pendingQueries) {
157 for(Iterator<Query> iterator = this.pendingQueries.iterator(); iterator.hasNext();) {
158 Query query = iterator.next();
159 if (query.getAccount() == account) {
160 pending.add(query);
161 iterator.remove();
162 }
163 }
164 }
165 for(Query query : pending) {
166 this.execute(query);
167 }
168 }
169
170 private void execute(final Query query) {
171 final Account account= query.getAccount();
172 if (account.getStatus() == Account.State.ONLINE) {
173 Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": running mam query " + query.toString());
174 IqPacket packet = this.mXmppConnectionService.getIqGenerator().queryMessageArchiveManagement(query);
175 this.mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() {
176 @Override
177 public void onIqPacketReceived(Account account, IqPacket packet) {
178 Element fin = packet.findChild("fin", Namespace.MAM);
179 if (packet.getType() == IqPacket.TYPE.TIMEOUT) {
180 synchronized (MessageArchiveService.this.queries) {
181 MessageArchiveService.this.queries.remove(query);
182 if (query.hasCallback()) {
183 query.callback(false);
184 }
185 }
186 } else if (packet.getType() == IqPacket.TYPE.RESULT && fin != null ) {
187 processFin(query, fin);
188 } else if (packet.getType() == IqPacket.TYPE.RESULT && query.isLegacy()) {
189 //do nothing
190 } else {
191 Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": error executing mam: " + packet.toString());
192 finalizeQuery(query, true);
193 }
194 }
195 });
196 } else {
197 synchronized (this.pendingQueries) {
198 this.pendingQueries.add(query);
199 }
200 }
201 }
202
203 private void finalizeQuery(Query query, boolean done) {
204 synchronized (this.queries) {
205 this.queries.remove(query);
206 }
207 final Conversation conversation = query.getConversation();
208 if (conversation != null) {
209 conversation.sort();
210 conversation.setHasMessagesLeftOnServer(!done);
211 } else {
212 for(Conversation tmp : this.mXmppConnectionService.getConversations()) {
213 if (tmp.getAccount() == query.getAccount()) {
214 tmp.sort();
215 }
216 }
217 }
218 if (query.hasCallback()) {
219 query.callback(done);
220 } else {
221 this.mXmppConnectionService.updateConversationUi();
222 }
223 }
224
225 public boolean inCatchup(Account account) {
226 synchronized (this.queries) {
227 for(Query query : queries) {
228 if (query.account == account && query.isCatchup() && query.getWith() == null) {
229 return true;
230 }
231 }
232 }
233 return false;
234 }
235
236 public boolean queryInProgress(Conversation conversation, XmppConnectionService.OnMoreMessagesLoaded callback) {
237 synchronized (this.queries) {
238 for(Query query : queries) {
239 if (query.conversation == conversation) {
240 if (!query.hasCallback() && callback != null) {
241 query.setCallback(callback);
242 }
243 return true;
244 }
245 }
246 return false;
247 }
248 }
249
250 public boolean queryInProgress(Conversation conversation) {
251 return queryInProgress(conversation, null);
252 }
253
254 public void processFinLegacy(Element fin, Jid from) {
255 Query query = findQuery(fin.getAttribute("queryid"));
256 if (query != null && query.validFrom(from)) {
257 processFin(query, fin);
258 }
259 }
260
261 private void processFin(Query query, Element fin) {
262 boolean complete = fin.getAttributeAsBoolean("complete");
263 Element set = fin.findChild("set","http://jabber.org/protocol/rsm");
264 Element last = set == null ? null : set.findChild("last");
265 String count = set == null ? null : set.findChildContent("count");
266 Element first = set == null ? null : set.findChild("first");
267 Element relevant = query.getPagingOrder() == PagingOrder.NORMAL ? last : first;
268 boolean abort = (!query.isCatchup() && query.getTotalCount() >= Config.PAGE_SIZE) || query.getTotalCount() >= Config.MAM_MAX_MESSAGES;
269 if (query.getConversation() != null) {
270 query.getConversation().setFirstMamReference(first == null ? null : first.getContent());
271 }
272 if (complete || relevant == null || abort) {
273 boolean done = !query.isCatchup();
274 if (count != null && !query.isCatchup()) {
275 try {
276 done = Integer.parseInt(count) <= query.getTotalCount();
277 } catch (NumberFormatException e) {
278 done = false;
279 }
280 }
281 done = done || (query.getActualMessageCount() == 0 && !query.isCatchup());
282 this.finalizeQuery(query, done);
283
284 Log.d(Config.LOGTAG,query.getAccount().getJid().toBareJid()+": finished mam after "+query.getTotalCount()+"("+query.getActualMessageCount()+") messages. messages left="+Boolean.toString(!done)+" count="+count);
285 if (query.isCatchup() && query.getActualMessageCount() > 0) {
286 mXmppConnectionService.getNotificationService().finishBacklog(true,query.getAccount());
287 }
288 processPostponed(query);
289 } else {
290 final Query nextQuery;
291 if (query.getPagingOrder() == PagingOrder.NORMAL) {
292 nextQuery = query.next(last == null ? null : last.getContent());
293 } else {
294 nextQuery = query.prev(first == null ? null : first.getContent());
295 }
296 this.execute(nextQuery);
297 this.finalizeQuery(query, false);
298 synchronized (this.queries) {
299 this.queries.add(nextQuery);
300 }
301 }
302 }
303
304 private void processPostponed(Query query) {
305 query.account.getAxolotlService().processPostponed();
306 Log.d(Config.LOGTAG,query.getAccount().getJid().toBareJid()+": found "+query.pendingReceiptRequests.size()+" pending receipt requests");
307 Iterator<ReceiptRequest> iterator = query.pendingReceiptRequests.iterator();
308 while (iterator.hasNext()) {
309 ReceiptRequest rr = iterator.next();
310 mXmppConnectionService.sendMessagePacket(query.account,mXmppConnectionService.getMessageGenerator().received(query.account, rr.getJid(),rr.getId()));
311 iterator.remove();
312 }
313 }
314
315 public Query findQuery(String id) {
316 if (id == null) {
317 return null;
318 }
319 synchronized (this.queries) {
320 for(Query query : this.queries) {
321 if (query.getQueryId().equals(id)) {
322 return query;
323 }
324 }
325 return null;
326 }
327 }
328
329 @Override
330 public void onAdvancedStreamFeaturesAvailable(Account account) {
331 if (account.getXmppConnection() != null && account.getXmppConnection().getFeatures().mam()) {
332 this.catchup(account);
333 }
334 }
335
336 public class Query {
337 private int totalCount = 0;
338 private int actualCount = 0;
339 private int actualInThisQuery = 0;
340 private long start;
341 private long end;
342 private String queryId;
343 private String reference = null;
344 private Account account;
345 private Conversation conversation;
346 private PagingOrder pagingOrder = PagingOrder.NORMAL;
347 private XmppConnectionService.OnMoreMessagesLoaded callback = null;
348 private boolean catchup = true;
349 public HashSet<ReceiptRequest> pendingReceiptRequests = new HashSet<>();
350
351
352 public Query(Conversation conversation, MamReference start, long end, boolean catchup) {
353 this(conversation.getAccount(),catchup ? start : start.timeOnly(),end);
354 this.conversation = conversation;
355 this.pagingOrder = catchup ? PagingOrder.NORMAL : PagingOrder.REVERSE;
356 this.catchup = catchup;
357 }
358
359 public Query(Account account, MamReference start, long end) {
360 this.account = account;
361 if (start.getReference() != null) {
362 this.reference = start.getReference();
363 } else {
364 this.start = start.getTimestamp();
365 }
366 this.end = end;
367 this.queryId = new BigInteger(50, mXmppConnectionService.getRNG()).toString(32);
368 }
369
370 private Query page(String reference) {
371 Query query = new Query(this.account,new MamReference(this.start,reference),this.end);
372 query.conversation = conversation;
373 query.totalCount = totalCount;
374 query.actualCount = actualCount;
375 query.pendingReceiptRequests = pendingReceiptRequests;
376 query.callback = callback;
377 query.catchup = catchup;
378 return query;
379 }
380
381 public boolean isLegacy() {
382 if (conversation == null || conversation.getMode() == Conversation.MODE_SINGLE) {
383 return account.getXmppConnection().getFeatures().mamLegacy();
384 } else {
385 return conversation.getMucOptions().mamLegacy();
386 }
387 }
388
389 public boolean safeToExtractTrueCounterpart() {
390 return muc() && !isLegacy();
391 }
392
393 public Query next(String reference) {
394 Query query = page(reference);
395 query.pagingOrder = PagingOrder.NORMAL;
396 return query;
397 }
398
399 public Query prev(String reference) {
400 Query query = page(reference);
401 query.pagingOrder = PagingOrder.REVERSE;
402 return query;
403 }
404
405 public String getReference() {
406 return reference;
407 }
408
409 public PagingOrder getPagingOrder() {
410 return this.pagingOrder;
411 }
412
413 public String getQueryId() {
414 return queryId;
415 }
416
417 public Jid getWith() {
418 return conversation == null ? null : conversation.getJid().toBareJid();
419 }
420
421 public boolean muc() {
422 return conversation != null && conversation.getMode() == Conversation.MODE_MULTI;
423 }
424
425 public long getStart() {
426 return start;
427 }
428
429 public boolean isCatchup() {
430 return catchup;
431 }
432
433 public void setCallback(XmppConnectionService.OnMoreMessagesLoaded callback) {
434 this.callback = callback;
435 }
436
437 public void callback(boolean done) {
438 if (this.callback != null) {
439 this.callback.onMoreMessagesLoaded(actualCount,conversation);
440 if (done) {
441 this.callback.informUser(R.string.no_more_history_on_server);
442 }
443 }
444 }
445
446 public long getEnd() {
447 return end;
448 }
449
450 public Conversation getConversation() {
451 return conversation;
452 }
453
454 public Account getAccount() {
455 return this.account;
456 }
457
458 public void incrementMessageCount() {
459 this.totalCount++;
460 }
461
462 public void incrementActualMessageCount() {
463 this.actualInThisQuery++;
464 this.actualCount++;
465 }
466
467 public int getTotalCount() {
468 return this.totalCount;
469 }
470
471 public int getActualMessageCount() {
472 return this.actualCount;
473 }
474
475 public int getActualInThisQuery() {
476 return this.actualInThisQuery;
477 }
478
479 public boolean validFrom(Jid from) {
480 if (muc()) {
481 return getWith().equals(from);
482 } else {
483 return (from == null) || account.getJid().toBareJid().equals(from.toBareJid());
484 }
485 }
486
487 @Override
488 public String toString() {
489 StringBuilder builder = new StringBuilder();
490 if (this.muc()) {
491 builder.append("to=");
492 builder.append(this.getWith().toString());
493 } else {
494 builder.append("with=");
495 if (this.getWith() == null) {
496 builder.append("*");
497 } else {
498 builder.append(getWith().toString());
499 }
500 }
501 if (this.start != 0) {
502 builder.append(", start=");
503 builder.append(AbstractGenerator.getTimestamp(this.start));
504 }
505 builder.append(", end=");
506 builder.append(AbstractGenerator.getTimestamp(this.end));
507 builder.append(", order="+pagingOrder.toString());
508 if (this.reference!=null) {
509 if (this.pagingOrder == PagingOrder.NORMAL) {
510 builder.append(", after=");
511 } else {
512 builder.append(", before=");
513 }
514 builder.append(this.reference);
515 }
516 builder.append(", catchup="+Boolean.toString(catchup));
517 return builder.toString();
518 }
519
520 public boolean hasCallback() {
521 return this.callback != null;
522 }
523 }
524}