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