MessageParser.java

  1package eu.siacs.conversations.parser;
  2
  3import android.util.Log;
  4import android.util.Pair;
  5
  6import java.net.URL;
  7import java.text.SimpleDateFormat;
  8import java.util.ArrayList;
  9import java.util.Collections;
 10import java.util.Date;
 11import java.util.List;
 12import java.util.Locale;
 13import java.util.Map;
 14import java.util.Set;
 15import java.util.UUID;
 16
 17import eu.siacs.conversations.Config;
 18import eu.siacs.conversations.R;
 19import eu.siacs.conversations.crypto.axolotl.AxolotlService;
 20import eu.siacs.conversations.crypto.axolotl.BrokenSessionException;
 21import eu.siacs.conversations.crypto.axolotl.NotEncryptedForThisDeviceException;
 22import eu.siacs.conversations.crypto.axolotl.XmppAxolotlMessage;
 23import eu.siacs.conversations.entities.Account;
 24import eu.siacs.conversations.entities.Bookmark;
 25import eu.siacs.conversations.entities.Contact;
 26import eu.siacs.conversations.entities.Conversation;
 27import eu.siacs.conversations.entities.Conversational;
 28import eu.siacs.conversations.entities.Message;
 29import eu.siacs.conversations.entities.MucOptions;
 30import eu.siacs.conversations.entities.ReadByMarker;
 31import eu.siacs.conversations.entities.ReceiptRequest;
 32import eu.siacs.conversations.http.HttpConnectionManager;
 33import eu.siacs.conversations.http.P1S3UrlStreamHandler;
 34import eu.siacs.conversations.services.MessageArchiveService;
 35import eu.siacs.conversations.services.QuickConversationsService;
 36import eu.siacs.conversations.services.XmppConnectionService;
 37import eu.siacs.conversations.utils.CryptoHelper;
 38import eu.siacs.conversations.xml.LocalizedContent;
 39import eu.siacs.conversations.xml.Namespace;
 40import eu.siacs.conversations.xml.Element;
 41import eu.siacs.conversations.xmpp.InvalidJid;
 42import eu.siacs.conversations.xmpp.OnMessagePacketReceived;
 43import eu.siacs.conversations.xmpp.chatstate.ChatState;
 44import eu.siacs.conversations.xmpp.pep.Avatar;
 45import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
 46import rocks.xmpp.addr.Jid;
 47
 48public class MessageParser extends AbstractParser implements OnMessagePacketReceived {
 49
 50    private static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm:ss", Locale.ENGLISH);
 51
 52    public MessageParser(XmppConnectionService service) {
 53        super(service);
 54    }
 55
 56    private static String extractStanzaId(Element packet, boolean isTypeGroupChat, Conversation conversation) {
 57        final Jid by;
 58        final boolean safeToExtract;
 59        if (isTypeGroupChat) {
 60            by = conversation.getJid().asBareJid();
 61            safeToExtract = conversation.getMucOptions().hasFeature(Namespace.STANZA_IDS);
 62        } else {
 63            Account account = conversation.getAccount();
 64            by = account.getJid().asBareJid();
 65            safeToExtract = account.getXmppConnection().getFeatures().stanzaIds();
 66        }
 67        return safeToExtract ? extractStanzaId(packet, by) : null;
 68    }
 69
 70    private static String extractStanzaId(Element packet, Jid by) {
 71        for (Element child : packet.getChildren()) {
 72            if (child.getName().equals("stanza-id")
 73                    && Namespace.STANZA_IDS.equals(child.getNamespace())
 74                    && by.equals(InvalidJid.getNullForInvalid(child.getAttributeAsJid("by")))) {
 75                return child.getAttribute("id");
 76            }
 77        }
 78        return null;
 79    }
 80
 81    private static Jid getTrueCounterpart(Element mucUserElement, Jid fallback) {
 82        final Element item = mucUserElement == null ? null : mucUserElement.findChild("item");
 83        Jid result = item == null ? null : InvalidJid.getNullForInvalid(item.getAttributeAsJid("jid"));
 84        return result != null ? result : fallback;
 85    }
 86
 87    private boolean extractChatState(Conversation c, final boolean isTypeGroupChat, final MessagePacket packet) {
 88        ChatState state = ChatState.parse(packet);
 89        if (state != null && c != null) {
 90            final Account account = c.getAccount();
 91            Jid from = packet.getFrom();
 92            if (from.asBareJid().equals(account.getJid().asBareJid())) {
 93                c.setOutgoingChatState(state);
 94                if (state == ChatState.ACTIVE || state == ChatState.COMPOSING) {
 95                    mXmppConnectionService.markRead(c);
 96                    activateGracePeriod(account);
 97                }
 98                return false;
 99            } else {
100                if (isTypeGroupChat) {
101                    MucOptions.User user = c.getMucOptions().findUserByFullJid(from);
102                    if (user != null) {
103                        return user.setChatState(state);
104                    } else {
105                        return false;
106                    }
107                } else {
108                    return c.setIncomingChatState(state);
109                }
110            }
111        }
112        return false;
113    }
114
115    private Message parseAxolotlChat(Element axolotlMessage, Jid from, Conversation conversation, int status, boolean checkedForDuplicates, boolean postpone) {
116        final AxolotlService service = conversation.getAccount().getAxolotlService();
117        final XmppAxolotlMessage xmppAxolotlMessage;
118        try {
119            xmppAxolotlMessage = XmppAxolotlMessage.fromElement(axolotlMessage, from.asBareJid());
120        } catch (Exception e) {
121            Log.d(Config.LOGTAG, conversation.getAccount().getJid().asBareJid() + ": invalid omemo message received " + e.getMessage());
122            return null;
123        }
124        if (xmppAxolotlMessage.hasPayload()) {
125            final XmppAxolotlMessage.XmppAxolotlPlaintextMessage plaintextMessage;
126            try {
127                plaintextMessage = service.processReceivingPayloadMessage(xmppAxolotlMessage, postpone);
128            } catch (BrokenSessionException e) {
129                if (checkedForDuplicates) {
130                    if (service.trustedOrPreviouslyResponded(from.asBareJid())) {
131                        service.reportBrokenSessionException(e, postpone);
132                        return new Message(conversation, "", Message.ENCRYPTION_AXOLOTL_FAILED, status);
133                    } else {
134                        Log.d(Config.LOGTAG, "ignoring broken session exception because contact was not trusted");
135                        return new Message(conversation, "", Message.ENCRYPTION_AXOLOTL_FAILED, status);
136                    }
137                } else {
138                    Log.d(Config.LOGTAG,"ignoring broken session exception because checkForDuplicates failed");
139                    //TODO should be still emit a failed message?
140                    return null;
141                }
142            } catch (NotEncryptedForThisDeviceException e) {
143                return new Message(conversation, "", Message.ENCRYPTION_AXOLOTL_NOT_FOR_THIS_DEVICE, status);
144            }
145            if (plaintextMessage != null) {
146                Message finishedMessage = new Message(conversation, plaintextMessage.getPlaintext(), Message.ENCRYPTION_AXOLOTL, status);
147                finishedMessage.setFingerprint(plaintextMessage.getFingerprint());
148                Log.d(Config.LOGTAG, AxolotlService.getLogprefix(finishedMessage.getConversation().getAccount()) + " Received Message with session fingerprint: " + plaintextMessage.getFingerprint());
149                return finishedMessage;
150            }
151        } else {
152            Log.d(Config.LOGTAG, conversation.getAccount().getJid().asBareJid() + ": received OMEMO key transport message");
153            service.processReceivingKeyTransportMessage(xmppAxolotlMessage, postpone);
154        }
155        return null;
156    }
157
158    private Invite extractInvite(Element message) {
159        final Element mucUser = message.findChild("x", Namespace.MUC_USER);
160        if (mucUser != null) {
161            Element invite = mucUser.findChild("invite");
162            if (invite != null) {
163                String password = mucUser.findChildContent("password");
164                Jid from = InvalidJid.getNullForInvalid(invite.getAttributeAsJid("from"));
165                Jid room = InvalidJid.getNullForInvalid(message.getAttributeAsJid("from"));
166                if (room == null) {
167                    return null;
168                }
169                return new Invite(room, password, false, from);
170            }
171        }
172        final Element conference = message.findChild("x", "jabber:x:conference");
173        if (conference != null) {
174            Jid from = InvalidJid.getNullForInvalid(message.getAttributeAsJid("from"));
175            Jid room = InvalidJid.getNullForInvalid(conference.getAttributeAsJid("jid"));
176            if (room == null) {
177                return null;
178            }
179            return new Invite(room, conference.getAttribute("password"), true, from);
180        }
181        return null;
182    }
183
184    private void parseEvent(final Element event, final Jid from, final Account account) {
185        Element items = event.findChild("items");
186        String node = items == null ? null : items.getAttribute("node");
187        if ("urn:xmpp:avatar:metadata".equals(node)) {
188            Avatar avatar = Avatar.parseMetadata(items);
189            if (avatar != null) {
190                avatar.owner = from.asBareJid();
191                if (mXmppConnectionService.getFileBackend().isAvatarCached(avatar)) {
192                    if (account.getJid().asBareJid().equals(from)) {
193                        if (account.setAvatar(avatar.getFilename())) {
194                            mXmppConnectionService.databaseBackend.updateAccount(account);
195                            mXmppConnectionService.notifyAccountAvatarHasChanged(account);
196                        }
197                        mXmppConnectionService.getAvatarService().clear(account);
198                        mXmppConnectionService.updateConversationUi();
199                        mXmppConnectionService.updateAccountUi();
200                    } else {
201                        Contact contact = account.getRoster().getContact(from);
202                        if (contact.setAvatar(avatar)) {
203                            mXmppConnectionService.syncRoster(account);
204                            mXmppConnectionService.getAvatarService().clear(contact);
205                            mXmppConnectionService.updateConversationUi();
206                            mXmppConnectionService.updateRosterUi();
207                        }
208                    }
209                } else if (mXmppConnectionService.isDataSaverDisabled()) {
210                    mXmppConnectionService.fetchAvatar(account, avatar);
211                }
212            }
213        } else if (Namespace.NICK.equals(node)) {
214            final Element i = items.findChild("item");
215            final String nick = i == null ? null : i.findChildContent("nick", Namespace.NICK);
216            if (nick != null) {
217                setNick(account, from, nick);
218            }
219        } else if (AxolotlService.PEP_DEVICE_LIST.equals(node)) {
220            Element item = items.findChild("item");
221            Set<Integer> deviceIds = mXmppConnectionService.getIqParser().deviceIds(item);
222            Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Received PEP device list " + deviceIds + " update from " + from + ", processing... ");
223            AxolotlService axolotlService = account.getAxolotlService();
224            axolotlService.registerDevices(from, deviceIds);
225        } else if (Namespace.BOOKMARKS.equals(node) && account.getJid().asBareJid().equals(from)) {
226            if (account.getXmppConnection().getFeatures().bookmarksConversion()) {
227                final Element i = items.findChild("item");
228                final Element storage = i == null ? null : i.findChild("storage", Namespace.BOOKMARKS);
229                Map<Jid, Bookmark> bookmarks = Bookmark.parseFromStorage(storage, account);
230                mXmppConnectionService.processBookmarksInitial(account, bookmarks, true);
231                Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": processing bookmark PEP event");
232            } else {
233                Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": ignoring bookmark PEP event because bookmark conversion was not detected");
234            }
235        } else if (Namespace.BOOKMARKS2.equals(node) && account.getJid().asBareJid().equals(from)) {
236            final Element item = items.findChild("item");
237            final Element retract = items.findChild("retract");
238            if (item != null) {
239                final Bookmark bookmark = Bookmark.parseFromItem(item, account);
240                if (bookmark != null) {
241                    mXmppConnectionService.processModifiedBookmark(bookmark);
242                }
243            }
244            if (retract != null) {
245                final Jid id = InvalidJid.getNullForInvalid(retract.getAttributeAsJid("id"));
246                if (id != null) {
247                    account.removeBookmark(id);
248                    Log.d(Config.LOGTAG,account.getJid().asBareJid()+": deleted bookmark for "+id);
249                    mXmppConnectionService.processDeletedBookmark(account, id);
250                }
251            }
252        } else {
253            Log.d(Config.LOGTAG,account.getJid().asBareJid()+" received pubsub notification for node="+node);
254        }
255    }
256
257    private void parseDeleteEvent(final Element event, final Jid from, final Account account) {
258        final Element delete = event.findChild("delete");
259        final String node = delete == null ? null : delete.getAttribute("node");
260        if (Namespace.NICK.equals(node)) {
261            Log.d(Config.LOGTAG, "parsing nick delete event from " + from);
262            setNick(account, from, null);
263        } else if (Namespace.BOOKMARKS2.equals(node) && account.getJid().asBareJid().equals(from)) {
264            account.setBookmarks(Collections.emptyMap());
265            Log.d(Config.LOGTAG,account.getJid().asBareJid()+": deleted bookmarks node");
266        }
267    }
268
269    private void parsePurgeEvent(final Element event, final Jid from, final Account account) {
270        final Element purge = event.findChild("purge");
271        final String node = purge == null ? null : purge.getAttribute("node");
272        if (Namespace.BOOKMARKS2.equals(node) && account.getJid().asBareJid().equals(from)) {
273            account.setBookmarks(Collections.emptyMap());
274            Log.d(Config.LOGTAG,account.getJid().asBareJid()+": purged bookmarks");
275        }
276    }
277
278    private void setNick(Account account, Jid user, String nick) {
279        if (user.asBareJid().equals(account.getJid().asBareJid())) {
280            account.setDisplayName(nick);
281            if (QuickConversationsService.isQuicksy()) {
282                mXmppConnectionService.getAvatarService().clear(account);
283            }
284        } else {
285            Contact contact = account.getRoster().getContact(user);
286            if (contact.setPresenceName(nick)) {
287                mXmppConnectionService.getAvatarService().clear(contact);
288            }
289        }
290        mXmppConnectionService.updateConversationUi();
291        mXmppConnectionService.updateAccountUi();
292    }
293
294    private boolean handleErrorMessage(Account account, MessagePacket packet) {
295        if (packet.getType() == MessagePacket.TYPE_ERROR) {
296            Jid from = packet.getFrom();
297            if (from != null) {
298                mXmppConnectionService.markMessage(account,
299                        from.asBareJid(),
300                        packet.getId(),
301                        Message.STATUS_SEND_FAILED,
302                        extractErrorMessage(packet));
303                final Element error = packet.findChild("error");
304                final boolean pingWorthyError = error != null && (error.hasChild("not-acceptable") || error.hasChild("remote-server-timeout") || error.hasChild("remote-server-not-found"));
305                if (pingWorthyError) {
306                    Conversation conversation = mXmppConnectionService.find(account,from);
307                    if (conversation != null && conversation.getMode() == Conversational.MODE_MULTI) {
308                        if (conversation.getMucOptions().online()) {
309                            Log.d(Config.LOGTAG,account.getJid().asBareJid()+": received ping worthy error for seemingly online muc at "+from);
310                            mXmppConnectionService.mucSelfPingAndRejoin(conversation);
311                        }
312                    }
313                }
314            }
315            return true;
316        }
317        return false;
318    }
319
320    @Override
321    public void onMessagePacketReceived(Account account, MessagePacket original) {
322        if (handleErrorMessage(account, original)) {
323            return;
324        }
325        final MessagePacket packet;
326        Long timestamp = null;
327        boolean isCarbon = false;
328        String serverMsgId = null;
329        final Element fin = original.findChild("fin", MessageArchiveService.Version.MAM_0.namespace);
330        if (fin != null) {
331            mXmppConnectionService.getMessageArchiveService().processFinLegacy(fin, original.getFrom());
332            return;
333        }
334        final Element result = MessageArchiveService.Version.findResult(original);
335        final MessageArchiveService.Query query = result == null ? null : mXmppConnectionService.getMessageArchiveService().findQuery(result.getAttribute("queryid"));
336        if (query != null && query.validFrom(original.getFrom())) {
337            Pair<MessagePacket, Long> f = original.getForwardedMessagePacket("result", query.version.namespace);
338            if (f == null) {
339                return;
340            }
341            timestamp = f.second;
342            packet = f.first;
343            serverMsgId = result.getAttribute("id");
344            query.incrementMessageCount();
345        } else if (query != null) {
346            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": received mam result from invalid sender");
347            return;
348        } else if (original.fromServer(account)) {
349            Pair<MessagePacket, Long> f;
350            f = original.getForwardedMessagePacket("received", "urn:xmpp:carbons:2");
351            f = f == null ? original.getForwardedMessagePacket("sent", "urn:xmpp:carbons:2") : f;
352            packet = f != null ? f.first : original;
353            if (handleErrorMessage(account, packet)) {
354                return;
355            }
356            timestamp = f != null ? f.second : null;
357            isCarbon = f != null;
358        } else {
359            packet = original;
360        }
361
362        if (timestamp == null) {
363            timestamp = AbstractParser.parseTimestamp(original, AbstractParser.parseTimestamp(packet));
364        }
365        final LocalizedContent body = packet.getBody();
366        final Element mucUserElement = packet.findChild("x", Namespace.MUC_USER);
367        final String pgpEncrypted = packet.findChildContent("x", "jabber:x:encrypted");
368        final Element replaceElement = packet.findChild("replace", "urn:xmpp:message-correct:0");
369        final Element oob = packet.findChild("x", Namespace.OOB);
370        final Element xP1S3 = packet.findChild("x", Namespace.P1_S3_FILE_TRANSFER);
371        final URL xP1S3url = xP1S3 == null ? null : P1S3UrlStreamHandler.of(xP1S3);
372        final String oobUrl = oob != null ? oob.findChildContent("url") : null;
373        final String replacementId = replaceElement == null ? null : replaceElement.getAttribute("id");
374        final Element axolotlEncrypted = packet.findChildEnsureSingle(XmppAxolotlMessage.CONTAINERTAG, AxolotlService.PEP_PREFIX);
375        int status;
376        final Jid counterpart;
377        final Jid to = packet.getTo();
378        final Jid from = packet.getFrom();
379        final Element originId = packet.findChild("origin-id", Namespace.STANZA_IDS);
380        final String remoteMsgId;
381        if (originId != null && originId.getAttribute("id") != null) {
382            remoteMsgId = originId.getAttribute("id");
383        } else {
384            remoteMsgId = packet.getId();
385        }
386        boolean notify = false;
387
388        if (from == null || !InvalidJid.isValid(from) || !InvalidJid.isValid(to)) {
389            Log.e(Config.LOGTAG, "encountered invalid message from='" + from + "' to='" + to + "'");
390            return;
391        }
392
393        boolean isTypeGroupChat = packet.getType() == MessagePacket.TYPE_GROUPCHAT;
394        if (query != null && !query.muc() && isTypeGroupChat) {
395            Log.e(Config.LOGTAG, account.getJid().asBareJid() + ": received groupchat (" + from + ") message on regular MAM request. skipping");
396            return;
397        }
398        boolean isMucStatusMessage = InvalidJid.hasValidFrom(packet) && from.isBareJid() && mucUserElement != null && mucUserElement.hasChild("status");
399        boolean selfAddressed;
400        if (packet.fromAccount(account)) {
401            status = Message.STATUS_SEND;
402            selfAddressed = to == null || account.getJid().asBareJid().equals(to.asBareJid());
403            if (selfAddressed) {
404                counterpart = from;
405            } else {
406                counterpart = to != null ? to : account.getJid();
407            }
408        } else {
409            status = Message.STATUS_RECEIVED;
410            counterpart = from;
411            selfAddressed = false;
412        }
413
414        final Invite invite = extractInvite(packet);
415        if (invite != null) {
416            if (isTypeGroupChat) {
417                Log.d(Config.LOGTAG,account.getJid().asBareJid()+": ignoring invite to "+invite.jid+" because type=groupchat");
418            } else if (invite.direct && (mucUserElement != null || invite.inviter == null || mXmppConnectionService.isMuc(account, invite.inviter))) {
419                Log.d(Config.LOGTAG, account.getJid().asBareJid()+": ignoring direct invite to "+invite.jid+" because it was received in MUC");
420            } else {
421                invite.execute(account);
422                return;
423            }
424        }
425
426        if ((body != null || pgpEncrypted != null || (axolotlEncrypted != null && axolotlEncrypted.hasChild("payload")) || oobUrl != null || xP1S3 != null) && !isMucStatusMessage) {
427            final boolean conversationIsProbablyMuc = isTypeGroupChat || mucUserElement != null || account.getXmppConnection().getMucServersWithholdAccount().contains(counterpart.getDomain());
428            final Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, counterpart.asBareJid(), conversationIsProbablyMuc, false, query, false);
429            final boolean conversationMultiMode = conversation.getMode() == Conversation.MODE_MULTI;
430
431            if (serverMsgId == null) {
432                serverMsgId = extractStanzaId(packet, isTypeGroupChat, conversation);
433            }
434
435
436            if (selfAddressed) {
437                if (mXmppConnectionService.markMessage(conversation, remoteMsgId, Message.STATUS_SEND_RECEIVED, serverMsgId)) {
438                    return;
439                }
440                status = Message.STATUS_RECEIVED;
441                if (remoteMsgId != null && conversation.findMessageWithRemoteId(remoteMsgId, counterpart) != null) {
442                    return;
443                }
444            }
445
446            if (isTypeGroupChat) {
447                if (conversation.getMucOptions().isSelf(counterpart)) {
448                    status = Message.STATUS_SEND_RECEIVED;
449                    isCarbon = true; //not really carbon but received from another resource
450                    if (mXmppConnectionService.markMessage(conversation, remoteMsgId, status, serverMsgId)) {
451                        return;
452                    } else if (remoteMsgId == null || Config.IGNORE_ID_REWRITE_IN_MUC) {
453                        LocalizedContent localizedBody = packet.getBody();
454                        if (localizedBody != null) {
455                            Message message = conversation.findSentMessageWithBody(localizedBody.content);
456                            if (message != null) {
457                                mXmppConnectionService.markMessage(message, status);
458                                return;
459                            }
460                        }
461                    }
462                } else {
463                    status = Message.STATUS_RECEIVED;
464                }
465            }
466            final Message message;
467            if (xP1S3url != null) {
468                message = new Message(conversation, xP1S3url.toString(), Message.ENCRYPTION_NONE, status);
469                message.setOob(true);
470                if (CryptoHelper.isPgpEncryptedUrl(xP1S3url.toString())) {
471                    message.setEncryption(Message.ENCRYPTION_DECRYPTED);
472                }
473            } else if (pgpEncrypted != null && Config.supportOpenPgp()) {
474                message = new Message(conversation, pgpEncrypted, Message.ENCRYPTION_PGP, status);
475            } else if (axolotlEncrypted != null && Config.supportOmemo()) {
476                Jid origin;
477                Set<Jid> fallbacksBySourceId = Collections.emptySet();
478                if (conversationMultiMode) {
479                    final Jid fallback = conversation.getMucOptions().getTrueCounterpart(counterpart);
480                    origin = getTrueCounterpart(query != null ? mucUserElement : null, fallback);
481                    if (origin == null) {
482                        try {
483                            fallbacksBySourceId = account.getAxolotlService().findCounterpartsBySourceId(XmppAxolotlMessage.parseSourceId(axolotlEncrypted));
484                        } catch (IllegalArgumentException e) {
485                            //ignoring
486                        }
487                    }
488                    if (origin == null && fallbacksBySourceId.size() == 0) {
489                        Log.d(Config.LOGTAG, "axolotl message in anonymous conference received and no possible fallbacks");
490                        return;
491                    }
492                } else {
493                    fallbacksBySourceId = Collections.emptySet();
494                    origin = from;
495                }
496
497                //TODO either or is probably fine?
498                final boolean checkedForDuplicates = serverMsgId != null && remoteMsgId != null && !conversation.possibleDuplicate(serverMsgId, remoteMsgId);
499
500                if (origin != null) {
501                    message = parseAxolotlChat(axolotlEncrypted, origin, conversation, status,  checkedForDuplicates,query != null);
502                } else {
503                    Message trial = null;
504                    for (Jid fallback : fallbacksBySourceId) {
505                        trial = parseAxolotlChat(axolotlEncrypted, fallback, conversation, status, checkedForDuplicates && fallbacksBySourceId.size() == 1, query != null);
506                        if (trial != null) {
507                            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": decoded muc message using fallback");
508                            origin = fallback;
509                            break;
510                        }
511                    }
512                    message = trial;
513                }
514                if (message == null) {
515                    if (query == null && extractChatState(mXmppConnectionService.find(account, counterpart.asBareJid()), isTypeGroupChat, packet)) {
516                        mXmppConnectionService.updateConversationUi();
517                    }
518                    if (query != null && status == Message.STATUS_SEND && remoteMsgId != null) {
519                        Message previouslySent = conversation.findSentMessageWithUuid(remoteMsgId);
520                        if (previouslySent != null && previouslySent.getServerMsgId() == null && serverMsgId != null) {
521                            previouslySent.setServerMsgId(serverMsgId);
522                            mXmppConnectionService.databaseBackend.updateMessage(previouslySent, false);
523                            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": encountered previously sent OMEMO message without serverId. updating...");
524                        }
525                    }
526                    return;
527                }
528                if (conversationMultiMode) {
529                    message.setTrueCounterpart(origin);
530                }
531            } else if (body == null && oobUrl != null) {
532                message = new Message(conversation, oobUrl, Message.ENCRYPTION_NONE, status);
533                message.setOob(true);
534                if (CryptoHelper.isPgpEncryptedUrl(oobUrl)) {
535                    message.setEncryption(Message.ENCRYPTION_DECRYPTED);
536                }
537            } else {
538                message = new Message(conversation, body.content, Message.ENCRYPTION_NONE, status);
539                if (body.count > 1) {
540                    message.setBodyLanguage(body.language);
541                }
542            }
543
544            message.setCounterpart(counterpart);
545            message.setRemoteMsgId(remoteMsgId);
546            message.setServerMsgId(serverMsgId);
547            message.setCarbon(isCarbon);
548            message.setTime(timestamp);
549            if (body != null && body.content != null && body.content.equals(oobUrl)) {
550                message.setOob(true);
551                if (CryptoHelper.isPgpEncryptedUrl(oobUrl)) {
552                    message.setEncryption(Message.ENCRYPTION_DECRYPTED);
553                }
554            }
555            message.markable = packet.hasChild("markable", "urn:xmpp:chat-markers:0");
556            if (conversationMultiMode) {
557                message.setMucUser(conversation.getMucOptions().findUserByFullJid(counterpart));
558                final Jid fallback = conversation.getMucOptions().getTrueCounterpart(counterpart);
559                Jid trueCounterpart;
560                if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL) {
561                    trueCounterpart = message.getTrueCounterpart();
562                } else if (query != null && query.safeToExtractTrueCounterpart()) {
563                    trueCounterpart = getTrueCounterpart(mucUserElement, fallback);
564                } else {
565                    trueCounterpart = fallback;
566                }
567                if (trueCounterpart != null && isTypeGroupChat) {
568                    if (trueCounterpart.asBareJid().equals(account.getJid().asBareJid())) {
569                        status = isTypeGroupChat ? Message.STATUS_SEND_RECEIVED : Message.STATUS_SEND;
570                    } else {
571                        status = Message.STATUS_RECEIVED;
572                        message.setCarbon(false);
573                    }
574                }
575                message.setStatus(status);
576                message.setTrueCounterpart(trueCounterpart);
577                if (!isTypeGroupChat) {
578                    message.setType(Message.TYPE_PRIVATE);
579                }
580            } else {
581                updateLastseen(account, from);
582            }
583
584            if (replacementId != null && mXmppConnectionService.allowMessageCorrection()) {
585                final Message replacedMessage = conversation.findMessageWithRemoteIdAndCounterpart(replacementId,
586                        counterpart,
587                        message.getStatus() == Message.STATUS_RECEIVED,
588                        message.isCarbon());
589                if (replacedMessage != null) {
590                    final boolean fingerprintsMatch = replacedMessage.getFingerprint() == null
591                            || replacedMessage.getFingerprint().equals(message.getFingerprint());
592                    final boolean trueCountersMatch = replacedMessage.getTrueCounterpart() != null
593                            && message.getTrueCounterpart() != null
594                            && replacedMessage.getTrueCounterpart().asBareJid().equals(message.getTrueCounterpart().asBareJid());
595                    final boolean mucUserMatches = query == null && replacedMessage.sameMucUser(message); //can not be checked when using mam
596                    final boolean duplicate = conversation.hasDuplicateMessage(message);
597                    if (fingerprintsMatch && (trueCountersMatch || !conversationMultiMode || mucUserMatches) && !duplicate) {
598                        Log.d(Config.LOGTAG, "replaced message '" + replacedMessage.getBody() + "' with '" + message.getBody() + "'");
599                        synchronized (replacedMessage) {
600                            final String uuid = replacedMessage.getUuid();
601                            replacedMessage.setUuid(UUID.randomUUID().toString());
602                            replacedMessage.setBody(message.getBody());
603                            replacedMessage.putEdited(replacedMessage.getRemoteMsgId(), replacedMessage.getServerMsgId());
604                            replacedMessage.setRemoteMsgId(remoteMsgId);
605                            if (replacedMessage.getServerMsgId() == null || message.getServerMsgId() != null) {
606                                replacedMessage.setServerMsgId(message.getServerMsgId());
607                            }
608                            replacedMessage.setEncryption(message.getEncryption());
609                            if (replacedMessage.getStatus() == Message.STATUS_RECEIVED) {
610                                replacedMessage.markUnread();
611                            }
612                            extractChatState(mXmppConnectionService.find(account, counterpart.asBareJid()), isTypeGroupChat, packet);
613                            mXmppConnectionService.updateMessage(replacedMessage, uuid);
614                            if (mXmppConnectionService.confirmMessages()
615                                    && replacedMessage.getStatus() == Message.STATUS_RECEIVED
616                                    && (replacedMessage.trusted() || replacedMessage.isPrivateMessage()) //TODO do we really want to send receipts for all PMs?
617                                    && remoteMsgId != null
618                                    && !selfAddressed
619                                    && !isTypeGroupChat) {
620                                processMessageReceipts(account, packet, query);
621                            }
622                            if (replacedMessage.getEncryption() == Message.ENCRYPTION_PGP) {
623                                conversation.getAccount().getPgpDecryptionService().discard(replacedMessage);
624                                conversation.getAccount().getPgpDecryptionService().decrypt(replacedMessage, false);
625                            }
626                        }
627                        mXmppConnectionService.getNotificationService().updateNotification();
628                        return;
629                    } else {
630                        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": received message correction but verification didn't check out");
631                    }
632                }
633            }
634
635            long deletionDate = mXmppConnectionService.getAutomaticMessageDeletionDate();
636            if (deletionDate != 0 && message.getTimeSent() < deletionDate) {
637                Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": skipping message from " + message.getCounterpart().toString() + " because it was sent prior to our deletion date");
638                return;
639            }
640
641            boolean checkForDuplicates = (isTypeGroupChat && packet.hasChild("delay", "urn:xmpp:delay"))
642                    || message.isPrivateMessage()
643                    || message.getServerMsgId() != null
644                    || (query == null && mXmppConnectionService.getMessageArchiveService().isCatchupInProgress(conversation));
645            if (checkForDuplicates) {
646                final Message duplicate = conversation.findDuplicateMessage(message);
647                if (duplicate != null) {
648                    final boolean serverMsgIdUpdated;
649                    if (duplicate.getStatus() != Message.STATUS_RECEIVED
650                            && duplicate.getUuid().equals(message.getRemoteMsgId())
651                            && duplicate.getServerMsgId() == null
652                            && message.getServerMsgId() != null) {
653                        duplicate.setServerMsgId(message.getServerMsgId());
654                        if (mXmppConnectionService.databaseBackend.updateMessage(duplicate, false)) {
655                            serverMsgIdUpdated = true;
656                        } else {
657                            serverMsgIdUpdated = false;
658                            Log.e(Config.LOGTAG, "failed to update message");
659                        }
660                    } else {
661                        serverMsgIdUpdated = false;
662                    }
663                    Log.d(Config.LOGTAG, "skipping duplicate message with " + message.getCounterpart() + ". serverMsgIdUpdated=" + serverMsgIdUpdated);
664                    return;
665                }
666            }
667
668            if (query != null && query.getPagingOrder() == MessageArchiveService.PagingOrder.REVERSE) {
669                conversation.prepend(query.getActualInThisQuery(), message);
670            } else {
671                conversation.add(message);
672            }
673            if (query != null) {
674                query.incrementActualMessageCount();
675            }
676
677            if (query == null || query.isCatchup()) { //either no mam or catchup
678                if (status == Message.STATUS_SEND || status == Message.STATUS_SEND_RECEIVED) {
679                    mXmppConnectionService.markRead(conversation);
680                    if (query == null) {
681                        activateGracePeriod(account);
682                    }
683                } else {
684                    message.markUnread();
685                    notify = true;
686                }
687            }
688
689            if (message.getEncryption() == Message.ENCRYPTION_PGP) {
690                notify = conversation.getAccount().getPgpDecryptionService().decrypt(message, notify);
691            } else if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL_NOT_FOR_THIS_DEVICE || message.getEncryption() == Message.ENCRYPTION_AXOLOTL_FAILED) {
692                notify = false;
693            }
694
695            if (query == null) {
696                extractChatState(mXmppConnectionService.find(account, counterpart.asBareJid()), isTypeGroupChat, packet);
697                mXmppConnectionService.updateConversationUi();
698            }
699
700            if (mXmppConnectionService.confirmMessages()
701                    && message.getStatus() == Message.STATUS_RECEIVED
702                    && (message.trusted() || message.isPrivateMessage())
703                    && remoteMsgId != null
704                    && !selfAddressed
705                    && !isTypeGroupChat) {
706                processMessageReceipts(account, packet, query);
707            }
708
709            mXmppConnectionService.databaseBackend.createMessage(message);
710            final HttpConnectionManager manager = this.mXmppConnectionService.getHttpConnectionManager();
711            if (message.trusted() && message.treatAsDownloadable() && manager.getAutoAcceptFileSize() > 0) {
712                manager.createNewDownloadConnection(message);
713            } else if (notify) {
714                if (query != null && query.isCatchup()) {
715                    mXmppConnectionService.getNotificationService().pushFromBacklog(message);
716                } else {
717                    mXmppConnectionService.getNotificationService().push(message);
718                }
719            }
720        } else if (!packet.hasChild("body")) { //no body
721
722            final Conversation conversation = mXmppConnectionService.find(account, from.asBareJid());
723            if (axolotlEncrypted != null) {
724                Jid origin;
725                if (conversation != null && conversation.getMode() == Conversation.MODE_MULTI) {
726                    final Jid fallback = conversation.getMucOptions().getTrueCounterpart(counterpart);
727                    origin = getTrueCounterpart(query != null ? mucUserElement : null, fallback);
728                    if (origin == null) {
729                        Log.d(Config.LOGTAG, "omemo key transport message in anonymous conference received");
730                        return;
731                    }
732                } else if (isTypeGroupChat) {
733                    return;
734                } else {
735                    origin = from;
736                }
737                try {
738                    final XmppAxolotlMessage xmppAxolotlMessage = XmppAxolotlMessage.fromElement(axolotlEncrypted, origin.asBareJid());
739                    account.getAxolotlService().processReceivingKeyTransportMessage(xmppAxolotlMessage, query != null);
740                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": omemo key transport message received from " + origin);
741                } catch (Exception e) {
742                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": invalid omemo key transport message received " + e.getMessage());
743                    return;
744                }
745            }
746
747            if (query == null && extractChatState(mXmppConnectionService.find(account, counterpart.asBareJid()), isTypeGroupChat, packet)) {
748                mXmppConnectionService.updateConversationUi();
749            }
750
751            if (isTypeGroupChat) {
752                if (packet.hasChild("subject")) { //TODO usually we would want to check for lack of body; however some servers do set a body :(
753                    if (conversation != null && conversation.getMode() == Conversation.MODE_MULTI) {
754                        conversation.setHasMessagesLeftOnServer(conversation.countMessages() > 0);
755                        final LocalizedContent subject = packet.findInternationalizedChildContentInDefaultNamespace("subject");
756                        if (subject != null && conversation.getMucOptions().setSubject(subject.content)) {
757                            mXmppConnectionService.updateConversation(conversation);
758                        }
759                        mXmppConnectionService.updateConversationUi();
760                        return;
761                    }
762                }
763            }
764            if (conversation != null && mucUserElement != null && InvalidJid.hasValidFrom(packet) && from.isBareJid()) {
765                for (Element child : mucUserElement.getChildren()) {
766                    if ("status".equals(child.getName())) {
767                        try {
768                            int code = Integer.parseInt(child.getAttribute("code"));
769                            if ((code >= 170 && code <= 174) || (code >= 102 && code <= 104)) {
770                                mXmppConnectionService.fetchConferenceConfiguration(conversation);
771                                break;
772                            }
773                        } catch (Exception e) {
774                            //ignored
775                        }
776                    } else if ("item".equals(child.getName())) {
777                        MucOptions.User user = AbstractParser.parseItem(conversation, child);
778                        Log.d(Config.LOGTAG, account.getJid() + ": changing affiliation for "
779                                + user.getRealJid() + " to " + user.getAffiliation() + " in "
780                                + conversation.getJid().asBareJid());
781                        if (!user.realJidMatchesAccount()) {
782                            boolean isNew = conversation.getMucOptions().updateUser(user);
783                            mXmppConnectionService.getAvatarService().clear(conversation);
784                            mXmppConnectionService.updateMucRosterUi();
785                            mXmppConnectionService.updateConversationUi();
786                            Contact contact = user.getContact();
787                            if (!user.getAffiliation().ranks(MucOptions.Affiliation.MEMBER)) {
788                                Jid jid = user.getRealJid();
789                                List<Jid> cryptoTargets = conversation.getAcceptedCryptoTargets();
790                                if (cryptoTargets.remove(user.getRealJid())) {
791                                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": removed " + jid + " from crypto targets of " + conversation.getName());
792                                    conversation.setAcceptedCryptoTargets(cryptoTargets);
793                                    mXmppConnectionService.updateConversation(conversation);
794                                }
795                            } else if (isNew
796                                    && user.getRealJid() != null
797                                    && conversation.getMucOptions().isPrivateAndNonAnonymous()
798                                    && (contact == null || !contact.mutualPresenceSubscription())
799                                    && account.getAxolotlService().hasEmptyDeviceList(user.getRealJid())) {
800                                account.getAxolotlService().fetchDeviceIds(user.getRealJid());
801                            }
802                        }
803                    }
804                }
805            }
806        }
807
808        Element received = packet.findChild("received", "urn:xmpp:chat-markers:0");
809        if (received == null) {
810            received = packet.findChild("received", "urn:xmpp:receipts");
811        }
812        if (received != null) {
813            String id = received.getAttribute("id");
814            if (packet.fromAccount(account)) {
815                if (query != null && id != null && packet.getTo() != null) {
816                    query.removePendingReceiptRequest(new ReceiptRequest(packet.getTo(), id));
817                }
818            } else {
819                mXmppConnectionService.markMessage(account, from.asBareJid(), received.getAttribute("id"), Message.STATUS_SEND_RECEIVED);
820            }
821        }
822        Element displayed = packet.findChild("displayed", "urn:xmpp:chat-markers:0");
823        if (displayed != null) {
824            final String id = displayed.getAttribute("id");
825            final Jid sender = InvalidJid.getNullForInvalid(displayed.getAttributeAsJid("sender"));
826            if (packet.fromAccount(account) && !selfAddressed) {
827                dismissNotification(account, counterpart, query);
828            } else if (isTypeGroupChat) {
829                Conversation conversation = mXmppConnectionService.find(account, counterpart.asBareJid());
830                if (conversation != null && id != null && sender != null) {
831                    Message message = conversation.findMessageWithRemoteId(id, sender);
832                    if (message != null) {
833                        final Jid fallback = conversation.getMucOptions().getTrueCounterpart(counterpart);
834                        final Jid trueJid = getTrueCounterpart((query != null && query.safeToExtractTrueCounterpart()) ? mucUserElement : null, fallback);
835                        final boolean trueJidMatchesAccount = account.getJid().asBareJid().equals(trueJid == null ? null : trueJid.asBareJid());
836                        if (trueJidMatchesAccount || conversation.getMucOptions().isSelf(counterpart)) {
837                            if (!message.isRead() && (query == null || query.isCatchup())) { //checking if message is unread fixes race conditions with reflections
838                                mXmppConnectionService.markRead(conversation);
839                            }
840                        } else if (!counterpart.isBareJid() && trueJid != null) {
841                            final ReadByMarker readByMarker = ReadByMarker.from(counterpart, trueJid);
842                            if (message.addReadByMarker(readByMarker)) {
843                                mXmppConnectionService.updateMessage(message, false);
844                            }
845                        }
846                    }
847                }
848            } else {
849                final Message displayedMessage = mXmppConnectionService.markMessage(account, from.asBareJid(), id, Message.STATUS_SEND_DISPLAYED);
850                Message message = displayedMessage == null ? null : displayedMessage.prev();
851                while (message != null
852                        && message.getStatus() == Message.STATUS_SEND_RECEIVED
853                        && message.getTimeSent() < displayedMessage.getTimeSent()) {
854                    mXmppConnectionService.markMessage(message, Message.STATUS_SEND_DISPLAYED);
855                    message = message.prev();
856                }
857                if (displayedMessage != null && selfAddressed) {
858                    dismissNotification(account, counterpart, query);
859                }
860            }
861        }
862
863        final Element event = original.findChild("event", "http://jabber.org/protocol/pubsub#event");
864        if (event != null && InvalidJid.hasValidFrom(original)) {
865            if (event.hasChild("items")) {
866                parseEvent(event, original.getFrom(), account);
867            } else if (event.hasChild("delete")) {
868                parseDeleteEvent(event, original.getFrom(), account);
869            } else if (event.hasChild("purge")) {
870                parsePurgeEvent(event, original.getFrom(), account);
871            }
872        }
873
874        final String nick = packet.findChildContent("nick", Namespace.NICK);
875        if (nick != null && InvalidJid.hasValidFrom(original)) {
876            Contact contact = account.getRoster().getContact(from);
877            if (contact.setPresenceName(nick)) {
878                mXmppConnectionService.getAvatarService().clear(contact);
879            }
880        }
881    }
882
883    private void dismissNotification(Account account, Jid counterpart, MessageArchiveService.Query query) {
884        Conversation conversation = mXmppConnectionService.find(account, counterpart.asBareJid());
885        if (conversation != null && (query == null || query.isCatchup())) {
886            mXmppConnectionService.markRead(conversation); //TODO only mark messages read that are older than timestamp
887        }
888    }
889
890    private void processMessageReceipts(Account account, MessagePacket packet, MessageArchiveService.Query query) {
891        final boolean markable = packet.hasChild("markable", "urn:xmpp:chat-markers:0");
892        final boolean request = packet.hasChild("request", "urn:xmpp:receipts");
893        if (query == null) {
894            final ArrayList<String> receiptsNamespaces = new ArrayList<>();
895            if (markable) {
896                receiptsNamespaces.add("urn:xmpp:chat-markers:0");
897            }
898            if (request) {
899                receiptsNamespaces.add("urn:xmpp:receipts");
900            }
901            if (receiptsNamespaces.size() > 0) {
902                MessagePacket receipt = mXmppConnectionService.getMessageGenerator().received(account,
903                        packet,
904                        receiptsNamespaces,
905                        packet.getType());
906                mXmppConnectionService.sendMessagePacket(account, receipt);
907            }
908        } else if (query.isCatchup()) {
909            if (request) {
910                query.addPendingReceiptRequest(new ReceiptRequest(packet.getFrom(), packet.getId()));
911            }
912        }
913    }
914
915    private void activateGracePeriod(Account account) {
916        long duration = mXmppConnectionService.getLongPreference("grace_period_length", R.integer.grace_period) * 1000;
917        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": activating grace period till " + TIME_FORMAT.format(new Date(System.currentTimeMillis() + duration)));
918        account.activateGracePeriod(duration);
919    }
920
921    private class Invite {
922        final Jid jid;
923        final String password;
924        final boolean direct;
925        final Jid inviter;
926
927        Invite(Jid jid, String password, boolean direct, Jid inviter) {
928            this.jid = jid;
929            this.password = password;
930            this.direct = direct;
931            this.inviter = inviter;
932        }
933
934        public boolean execute(Account account) {
935            if (jid != null) {
936                Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, jid, true, false);
937                if (conversation.getMucOptions().online()) {
938                    Log.d(Config.LOGTAG,account.getJid().asBareJid()+": received invite to "+jid+" but muc is considered to be online");
939                    mXmppConnectionService.mucSelfPingAndRejoin(conversation);
940                } else {
941                    conversation.getMucOptions().setPassword(password);
942                    mXmppConnectionService.databaseBackend.updateConversation(conversation);
943                    final Contact contact = inviter != null ? account.getRoster().getContactFromContactList(inviter) : null;
944                    mXmppConnectionService.joinMuc(conversation, contact != null && contact.mutualPresenceSubscription());
945                    mXmppConnectionService.updateConversationUi();
946                }
947                return true;
948            }
949            return false;
950        }
951    }
952}