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