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