MessageParser.java

   1package eu.siacs.conversations.parser;
   2
   3import android.net.Uri;
   4import android.util.Log;
   5import android.util.Pair;
   6
   7import com.cheogram.android.BobTransfer;
   8import com.cheogram.android.WebxdcUpdate;
   9
  10import com.google.common.base.Strings;
  11import com.google.common.collect.ImmutableSet;
  12
  13import java.io.File;
  14import java.net.URISyntaxException;
  15import java.text.SimpleDateFormat;
  16import java.util.ArrayList;
  17import java.util.Arrays;
  18import java.util.Collections;
  19import java.util.Date;
  20import java.util.HashSet;
  21import java.util.LinkedHashSet;
  22import java.util.List;
  23import java.util.Locale;
  24import java.util.Map;
  25import java.util.Set;
  26import java.util.UUID;
  27import java.util.function.Consumer;
  28import java.util.stream.Collectors;
  29
  30import io.ipfs.cid.Cid;
  31
  32import eu.siacs.conversations.AppSettings;
  33import eu.siacs.conversations.Config;
  34import eu.siacs.conversations.R;
  35import eu.siacs.conversations.crypto.axolotl.AxolotlService;
  36import eu.siacs.conversations.crypto.axolotl.BrokenSessionException;
  37import eu.siacs.conversations.crypto.axolotl.NotEncryptedForThisDeviceException;
  38import eu.siacs.conversations.crypto.axolotl.OutdatedSenderException;
  39import eu.siacs.conversations.crypto.axolotl.XmppAxolotlMessage;
  40import eu.siacs.conversations.entities.Account;
  41import eu.siacs.conversations.entities.Bookmark;
  42import eu.siacs.conversations.entities.Contact;
  43import eu.siacs.conversations.entities.Conversation;
  44import eu.siacs.conversations.entities.Conversational;
  45import eu.siacs.conversations.entities.DownloadableFile;
  46import eu.siacs.conversations.entities.Message;
  47import eu.siacs.conversations.entities.MucOptions;
  48import eu.siacs.conversations.entities.Reaction;
  49import eu.siacs.conversations.entities.ReadByMarker;
  50import eu.siacs.conversations.entities.ReceiptRequest;
  51import eu.siacs.conversations.entities.RtpSessionStatus;
  52import eu.siacs.conversations.http.HttpConnectionManager;
  53import eu.siacs.conversations.services.MessageArchiveService;
  54import eu.siacs.conversations.services.QuickConversationsService;
  55import eu.siacs.conversations.services.XmppConnectionService;
  56import eu.siacs.conversations.utils.CryptoHelper;
  57import eu.siacs.conversations.utils.Emoticons;
  58import eu.siacs.conversations.xml.Element;
  59import eu.siacs.conversations.xml.LocalizedContent;
  60import eu.siacs.conversations.xml.Namespace;
  61import eu.siacs.conversations.xmpp.InvalidJid;
  62import eu.siacs.conversations.xmpp.Jid;
  63import eu.siacs.conversations.xmpp.OnMessagePacketReceived;
  64import eu.siacs.conversations.xmpp.chatstate.ChatState;
  65import eu.siacs.conversations.xmpp.forms.Data;
  66import eu.siacs.conversations.xmpp.jingle.JingleConnectionManager;
  67import eu.siacs.conversations.xmpp.jingle.JingleRtpConnection;
  68import eu.siacs.conversations.xmpp.pep.Avatar;
  69import im.conversations.android.xmpp.model.Extension;
  70import im.conversations.android.xmpp.model.carbons.Received;
  71import im.conversations.android.xmpp.model.carbons.Sent;
  72import im.conversations.android.xmpp.model.correction.Replace;
  73import im.conversations.android.xmpp.model.forward.Forwarded;
  74import im.conversations.android.xmpp.model.occupant.OccupantId;
  75import im.conversations.android.xmpp.model.reactions.Reactions;
  76
  77public class MessageParser extends AbstractParser implements Consumer<im.conversations.android.xmpp.model.stanza.Message> {
  78
  79    private static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm:ss", Locale.ENGLISH);
  80
  81    private static final List<String> JINGLE_MESSAGE_ELEMENT_NAMES =
  82            Arrays.asList("accept", "propose", "proceed", "reject", "retract", "ringing", "finish");
  83
  84    public MessageParser(final XmppConnectionService service, final Account account) {
  85        super(service, account);
  86    }
  87
  88    private static String extractStanzaId(Element packet, boolean isTypeGroupChat, Conversation conversation) {
  89        final Jid by;
  90        final boolean safeToExtract;
  91        if (isTypeGroupChat) {
  92            by = conversation.getJid().asBareJid();
  93            safeToExtract = conversation.getMucOptions().hasFeature(Namespace.STANZA_IDS);
  94        } else {
  95            Account account = conversation.getAccount();
  96            by = account.getJid().asBareJid();
  97            safeToExtract = account.getXmppConnection().getFeatures().stanzaIds();
  98        }
  99        return safeToExtract ? extractStanzaId(packet, by) : null;
 100    }
 101
 102    private static String extractStanzaId(Account account, Element packet) {
 103        final boolean safeToExtract = account.getXmppConnection().getFeatures().stanzaIds();
 104        return safeToExtract ? extractStanzaId(packet, account.getJid().asBareJid()) : null;
 105    }
 106
 107    private static String extractStanzaId(Element packet, Jid by) {
 108        for (Element child : packet.getChildren()) {
 109            if (child.getName().equals("stanza-id")
 110                    && Namespace.STANZA_IDS.equals(child.getNamespace())
 111                    && by.equals(InvalidJid.getNullForInvalid(child.getAttributeAsJid("by")))) {
 112                return child.getAttribute("id");
 113            }
 114        }
 115        return null;
 116    }
 117
 118    private static Jid getTrueCounterpart(Element mucUserElement, Jid fallback) {
 119        final Element item = mucUserElement == null ? null : mucUserElement.findChild("item");
 120        Jid result = item == null ? null : InvalidJid.getNullForInvalid(item.getAttributeAsJid("jid"));
 121        return result != null ? result : fallback;
 122    }
 123
 124    private boolean extractChatState(Conversation c, final boolean isTypeGroupChat, final im.conversations.android.xmpp.model.stanza.Message packet) {
 125        ChatState state = ChatState.parse(packet);
 126        if (state != null && c != null) {
 127            final Account account = c.getAccount();
 128            final Jid from = packet.getFrom();
 129            if (from.asBareJid().equals(account.getJid().asBareJid())) {
 130                c.setOutgoingChatState(state);
 131                if (state == ChatState.ACTIVE || state == ChatState.COMPOSING) {
 132                    if (c.getContact().isSelf()) {
 133                        return false;
 134                    }
 135                    mXmppConnectionService.markRead(c);
 136                    activateGracePeriod(account);
 137                }
 138                return false;
 139            } else {
 140                if (isTypeGroupChat) {
 141                    MucOptions.User user = c.getMucOptions().findUserByFullJid(from);
 142                    if (user != null) {
 143                        return user.setChatState(state);
 144                    } else {
 145                        return false;
 146                    }
 147                } else {
 148                    return c.setIncomingChatState(state);
 149                }
 150            }
 151        }
 152        return false;
 153    }
 154
 155    private Message parseAxolotlChat(Element axolotlMessage, Jid from, Conversation conversation, int status, final boolean checkedForDuplicates, boolean postpone) {
 156        final AxolotlService service = conversation.getAccount().getAxolotlService();
 157        final XmppAxolotlMessage xmppAxolotlMessage;
 158        try {
 159            xmppAxolotlMessage = XmppAxolotlMessage.fromElement(axolotlMessage, from.asBareJid());
 160        } catch (Exception e) {
 161            Log.d(Config.LOGTAG, conversation.getAccount().getJid().asBareJid() + ": invalid omemo message received " + e.getMessage());
 162            return null;
 163        }
 164        if (xmppAxolotlMessage.hasPayload()) {
 165            final XmppAxolotlMessage.XmppAxolotlPlaintextMessage plaintextMessage;
 166            try {
 167                plaintextMessage = service.processReceivingPayloadMessage(xmppAxolotlMessage, postpone);
 168            } catch (BrokenSessionException e) {
 169                if (checkedForDuplicates) {
 170                    if (service.trustedOrPreviouslyResponded(from.asBareJid())) {
 171                        service.reportBrokenSessionException(e, postpone);
 172                        return new Message(conversation, "", Message.ENCRYPTION_AXOLOTL_FAILED, status);
 173                    } else {
 174                        Log.d(Config.LOGTAG, "ignoring broken session exception because contact was not trusted");
 175                        return new Message(conversation, "", Message.ENCRYPTION_AXOLOTL_FAILED, status);
 176                    }
 177                } else {
 178                    Log.d(Config.LOGTAG, "ignoring broken session exception because checkForDuplicates failed");
 179                    return null;
 180                }
 181            } catch (NotEncryptedForThisDeviceException e) {
 182                return new Message(conversation, "", Message.ENCRYPTION_AXOLOTL_NOT_FOR_THIS_DEVICE, status);
 183            } catch (OutdatedSenderException e) {
 184                return new Message(conversation, "", Message.ENCRYPTION_AXOLOTL_FAILED, status);
 185            }
 186            if (plaintextMessage != null) {
 187                Message finishedMessage = new Message(conversation, plaintextMessage.getPlaintext(), Message.ENCRYPTION_AXOLOTL, status);
 188                finishedMessage.setFingerprint(plaintextMessage.getFingerprint());
 189                Log.d(Config.LOGTAG, AxolotlService.getLogprefix(finishedMessage.getConversation().getAccount()) + " Received Message with session fingerprint: " + plaintextMessage.getFingerprint());
 190                return finishedMessage;
 191            }
 192        } else {
 193            Log.d(Config.LOGTAG, conversation.getAccount().getJid().asBareJid() + ": received OMEMO key transport message");
 194            service.processReceivingKeyTransportMessage(xmppAxolotlMessage, postpone);
 195        }
 196        return null;
 197    }
 198
 199    private Invite extractInvite(final Element message) {
 200        final Element mucUser = message.findChild("x", Namespace.MUC_USER);
 201        if (mucUser != null) {
 202            final Element invite = mucUser.findChild("invite");
 203            if (invite != null) {
 204                final String password = mucUser.findChildContent("password");
 205                final Jid from = InvalidJid.getNullForInvalid(invite.getAttributeAsJid("from"));
 206                final Jid to = InvalidJid.getNullForInvalid(invite.getAttributeAsJid("to"));
 207                if (to != null && from == null) {
 208                    Log.d(Config.LOGTAG,"do not parse outgoing mediated invite "+message);
 209                    return null;
 210                }
 211                final Jid room = InvalidJid.getNullForInvalid(message.getAttributeAsJid("from"));
 212                if (room == null) {
 213                    return null;
 214                }
 215                return new Invite(room, password, false, from);
 216            }
 217        }
 218        final Element conference = message.findChild("x", "jabber:x:conference");
 219        if (conference != null) {
 220            Jid from = InvalidJid.getNullForInvalid(message.getAttributeAsJid("from"));
 221            Jid room = InvalidJid.getNullForInvalid(conference.getAttributeAsJid("jid"));
 222            if (room == null) {
 223                return null;
 224            }
 225            return new Invite(room, conference.getAttribute("password"), true, from);
 226        }
 227        return null;
 228    }
 229
 230    private void parseEvent(final Element event, final Jid from, final Account account) {
 231        final Element items = event.findChild("items");
 232        final String node = items == null ? null : items.getAttribute("node");
 233        if ("urn:xmpp:avatar:metadata".equals(node)) {
 234            Avatar avatar = Avatar.parseMetadata(items);
 235            if (avatar != null) {
 236                avatar.owner = from.asBareJid();
 237                if (mXmppConnectionService.getFileBackend().isAvatarCached(avatar)) {
 238                    if (account.getJid().asBareJid().equals(from)) {
 239                        if (account.setAvatar(avatar.getFilename())) {
 240                            mXmppConnectionService.databaseBackend.updateAccount(account);
 241                            mXmppConnectionService.notifyAccountAvatarHasChanged(account);
 242                        }
 243                        mXmppConnectionService.getAvatarService().clear(account);
 244                        mXmppConnectionService.updateConversationUi();
 245                        mXmppConnectionService.updateAccountUi();
 246                    } else {
 247                        final Contact contact = account.getRoster().getContact(from);
 248                        contact.setAvatar(avatar);
 249                        mXmppConnectionService.syncRoster(account);
 250                        mXmppConnectionService.getAvatarService().clear(contact);
 251                        mXmppConnectionService.updateConversationUi();
 252                        mXmppConnectionService.updateRosterUi(XmppConnectionService.UpdateRosterReason.AVATAR);
 253                    }
 254                } else if (mXmppConnectionService.isDataSaverDisabled()) {
 255                    mXmppConnectionService.fetchAvatar(account, avatar);
 256                }
 257            }
 258        } else if (Namespace.NICK.equals(node)) {
 259            final Element i = items.findChild("item");
 260            final String nick = i == null ? null : i.findChildContent("nick", Namespace.NICK);
 261            if (nick != null) {
 262                setNick(account, from, nick);
 263            }
 264        } else if (AxolotlService.PEP_DEVICE_LIST.equals(node)) {
 265            Element item = items.findChild("item");
 266            final Set<Integer> deviceIds = IqParser.deviceIds(item);
 267            Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Received PEP device list " + deviceIds + " update from " + from + ", processing... ");
 268            final AxolotlService axolotlService = account.getAxolotlService();
 269            axolotlService.registerDevices(from, deviceIds);
 270        } else if (Namespace.BOOKMARKS.equals(node) && account.getJid().asBareJid().equals(from)) {
 271            final var connection = account.getXmppConnection();
 272            if (connection.getFeatures().bookmarksConversion()) {
 273                if (connection.getFeatures().bookmarks2()) {
 274                    Log.w(
 275                            Config.LOGTAG,
 276                            account.getJid().asBareJid()
 277                                    + ": received storage:bookmark notification even though we opted into bookmarks:1");
 278                }
 279                final Element i = items.findChild("item");
 280                final Element storage =
 281                        i == null ? null : i.findChild("storage", Namespace.BOOKMARKS);
 282                final Map<Jid, Bookmark> bookmarks = Bookmark.parseFromStorage(storage, account);
 283                mXmppConnectionService.processBookmarksInitial(account, bookmarks, true);
 284                Log.d(
 285                        Config.LOGTAG,
 286                        account.getJid().asBareJid() + ": processing bookmark PEP event");
 287            } else {
 288                Log.d(
 289                        Config.LOGTAG,
 290                        account.getJid().asBareJid()
 291                                + ": ignoring bookmark PEP event because bookmark conversion was not detected");
 292            }
 293        } else if (Namespace.BOOKMARKS2.equals(node) && account.getJid().asBareJid().equals(from)) {
 294            final Element item = items.findChild("item");
 295            final Element retract = items.findChild("retract");
 296            if (item != null) {
 297                final Bookmark bookmark = Bookmark.parseFromItem(item, account);
 298                if (bookmark != null) {
 299                    account.putBookmark(bookmark);
 300                    mXmppConnectionService.processModifiedBookmark(bookmark);
 301                    mXmppConnectionService.updateConversationUi();
 302                }
 303            }
 304            if (retract != null) {
 305                final Jid id = InvalidJid.getNullForInvalid(retract.getAttributeAsJid("id"));
 306                if (id != null) {
 307                    account.removeBookmark(id);
 308                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": deleted bookmark for " + id);
 309                    mXmppConnectionService.processDeletedBookmark(account, id);
 310                    mXmppConnectionService.updateConversationUi();
 311                }
 312            }
 313        } else if (Config.MESSAGE_DISPLAYED_SYNCHRONIZATION
 314                && Namespace.MDS_DISPLAYED.equals(node)
 315                && account.getJid().asBareJid().equals(from)) {
 316            final Element item = items.findChild("item");
 317            mXmppConnectionService.processMdsItem(account, item);
 318        } else {
 319            Log.d(
 320                    Config.LOGTAG,
 321                    account.getJid().asBareJid()
 322                            + " received pubsub notification for node="
 323                            + node);
 324        }
 325    }
 326
 327    private void parseDeleteEvent(final Element event, final Jid from, final Account account) {
 328        final Element delete = event.findChild("delete");
 329        final String node = delete == null ? null : delete.getAttribute("node");
 330        if (Namespace.NICK.equals(node)) {
 331            Log.d(Config.LOGTAG, "parsing nick delete event from " + from);
 332            setNick(account, from, null);
 333        } else if (Namespace.BOOKMARKS2.equals(node) && account.getJid().asBareJid().equals(from)) {
 334            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": deleted bookmarks node");
 335            deleteAllBookmarks(account);
 336        } else if (Namespace.AVATAR_METADATA.equals(node) && account.getJid().asBareJid().equals(from)) {
 337            Log.d(Config.LOGTAG,account.getJid().asBareJid()+": deleted avatar metadata node");
 338        }
 339    }
 340
 341    private void parsePurgeEvent(final Element event, final Jid from, final Account account) {
 342        final Element purge = event.findChild("purge");
 343        final String node = purge == null ? null : purge.getAttribute("node");
 344        if (Namespace.BOOKMARKS2.equals(node) && account.getJid().asBareJid().equals(from)) {
 345            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": purged bookmarks");
 346            deleteAllBookmarks(account);
 347        }
 348    }
 349
 350    private void deleteAllBookmarks(final Account account) {
 351        final var previous = account.getBookmarkedJids();
 352        account.setBookmarks(Collections.emptyMap());
 353        mXmppConnectionService.processDeletedBookmarks(account, previous);
 354    }
 355
 356    private void setNick(final Account account, final Jid user, final String nick) {
 357        if (user.asBareJid().equals(account.getJid().asBareJid())) {
 358            account.setDisplayName(nick);
 359            if (QuickConversationsService.isQuicksy()) {
 360                mXmppConnectionService.getAvatarService().clear(account);
 361            }
 362            mXmppConnectionService.checkMucRequiresRename();
 363        } else {
 364            Contact contact = account.getRoster().getContact(user);
 365            if (contact.setPresenceName(nick)) {
 366                mXmppConnectionService.syncRoster(account);
 367                mXmppConnectionService.getAvatarService().clear(contact);
 368            }
 369        }
 370        mXmppConnectionService.updateConversationUi();
 371        mXmppConnectionService.updateAccountUi();
 372    }
 373
 374    private boolean handleErrorMessage(final Account account, final im.conversations.android.xmpp.model.stanza.Message packet) {
 375        if (packet.getType() == im.conversations.android.xmpp.model.stanza.Message.Type.ERROR) {
 376            if (packet.fromServer(account)) {
 377                final var forwarded = getForwardedMessagePacket(packet,"received", Namespace.CARBONS);
 378                if (forwarded != null) {
 379                    return handleErrorMessage(account, forwarded.first);
 380                }
 381            }
 382            final Jid from = packet.getFrom();
 383            final String id = packet.getId();
 384            if (from != null && id != null) {
 385                if (id.startsWith(JingleRtpConnection.JINGLE_MESSAGE_PROPOSE_ID_PREFIX)) {
 386                    final String sessionId = id.substring(JingleRtpConnection.JINGLE_MESSAGE_PROPOSE_ID_PREFIX.length());
 387                    mXmppConnectionService.getJingleConnectionManager()
 388                            .updateProposedSessionDiscovered(account, from, sessionId, JingleConnectionManager.DeviceDiscoveryState.FAILED);
 389                    return true;
 390                }
 391                if (id.startsWith(JingleRtpConnection.JINGLE_MESSAGE_PROCEED_ID_PREFIX)) {
 392                    final String sessionId = id.substring(JingleRtpConnection.JINGLE_MESSAGE_PROCEED_ID_PREFIX.length());
 393                    final String message = extractErrorMessage(packet);
 394                    mXmppConnectionService.getJingleConnectionManager().failProceed(account, from, sessionId, message);
 395                    return true;
 396                }
 397                mXmppConnectionService.markMessage(account,
 398                        from.asBareJid(),
 399                        id,
 400                        Message.STATUS_SEND_FAILED,
 401                        extractErrorMessage(packet));
 402                final Element error = packet.findChild("error");
 403                final boolean pingWorthyError = error != null && (error.hasChild("not-acceptable") || error.hasChild("remote-server-timeout") || error.hasChild("remote-server-not-found"));
 404                if (pingWorthyError) {
 405                    Conversation conversation = mXmppConnectionService.find(account, from);
 406                    if (conversation != null && conversation.getMode() == Conversational.MODE_MULTI) {
 407                        if (conversation.getMucOptions().online()) {
 408                            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": received ping worthy error for seemingly online muc at " + from);
 409                            mXmppConnectionService.mucSelfPingAndRejoin(conversation);
 410                        }
 411                    }
 412                }
 413            }
 414            return true;
 415        }
 416        return false;
 417    }
 418
 419    @Override
 420    public void accept(final im.conversations.android.xmpp.model.stanza.Message original) {
 421        if (handleErrorMessage(account, original)) {
 422            return;
 423        }
 424        final im.conversations.android.xmpp.model.stanza.Message packet;
 425        Long timestamp = null;
 426        boolean isCarbon = false;
 427        String serverMsgId = null;
 428        final Element fin = original.findChild("fin", MessageArchiveService.Version.MAM_0.namespace);
 429        if (fin != null) {
 430            mXmppConnectionService.getMessageArchiveService().processFinLegacy(fin, original.getFrom());
 431            return;
 432        }
 433        final Element result = MessageArchiveService.Version.findResult(original);
 434        final String queryId = result == null ? null : result.getAttribute("queryid");
 435        final MessageArchiveService.Query query = queryId == null ? null : mXmppConnectionService.getMessageArchiveService().findQuery(queryId);
 436        final boolean offlineMessagesRetrieved = account.getXmppConnection().isOfflineMessagesRetrieved();
 437        if (query != null && query.validFrom(original.getFrom())) {
 438            final var f = getForwardedMessagePacket(original,"result", query.version.namespace);
 439            if (f == null) {
 440                return;
 441            }
 442            timestamp = f.second;
 443            packet = f.first;
 444            serverMsgId = result.getAttribute("id");
 445            query.incrementMessageCount();
 446            if (handleErrorMessage(account, packet)) {
 447                return;
 448            }
 449            final var contact = packet.getFrom() == null || packet.getFrom() instanceof InvalidJid ? null : account.getRoster().getContact(packet.getFrom());
 450            if (contact != null && contact.isBlocked()) {
 451                Log.d(Config.LOGTAG, "Got MAM result from blocked contact, ignoring...");
 452                return;
 453            }
 454        } else if (query != null) {
 455            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": received mam result with invalid from (" + original.getFrom() + ") or queryId (" + queryId + ")");
 456            return;
 457        } else if (original.fromServer(account)
 458                && original.getType() != im.conversations.android.xmpp.model.stanza.Message.Type.GROUPCHAT) {
 459            Pair<im.conversations.android.xmpp.model.stanza.Message, Long> f;
 460            f = getForwardedMessagePacket(original, Received.class);
 461            f = f == null ? getForwardedMessagePacket(original, Sent.class) : f;
 462            packet = f != null ? f.first : original;
 463            if (handleErrorMessage(account, packet)) {
 464                return;
 465            }
 466            timestamp = f != null ? f.second : null;
 467            isCarbon = f != null;
 468        } else {
 469            packet = original;
 470        }
 471
 472        if (timestamp == null) {
 473            timestamp = AbstractParser.parseTimestamp(original, AbstractParser.parseTimestamp(packet));
 474        }
 475        final Element mucUserElement = packet.findChild("x", Namespace.MUC_USER);
 476        final boolean isTypeGroupChat = packet.getType() == im.conversations.android.xmpp.model.stanza.Message.Type.GROUPCHAT;
 477        final String pgpEncrypted = packet.findChildContent("x", "jabber:x:encrypted");
 478
 479        Element replaceElement = packet.findChild("replace", "urn:xmpp:message-correct:0");
 480        Set<Message.FileParams> attachments = new LinkedHashSet<>();
 481        for (Element child : packet.getChildren()) {
 482            // SIMS first so they get preference in the set
 483            if (child.getName().equals("reference") && child.getNamespace().equals("urn:xmpp:reference:0")) {
 484                if (child.findChild("media-sharing", "urn:xmpp:sims:1") != null) {
 485                    attachments.add(new Message.FileParams(child));
 486                }
 487            }
 488        }
 489        for (Element child : packet.getChildren()) {
 490            if (child.getName().equals("x") && child.getNamespace().equals(Namespace.OOB)) {
 491                attachments.add(new Message.FileParams(child));
 492            }
 493        }
 494        String replacementId = replaceElement == null ? null : replaceElement.getAttribute("id");
 495        if (replacementId == null) {
 496            final Element fasten = packet.findChild("apply-to", "urn:xmpp:fasten:0");
 497            if (fasten != null) {
 498                replaceElement = fasten.findChild("retract", "urn:xmpp:message-retract:0");
 499                if (replaceElement == null) replaceElement = fasten.findChild("moderated", "urn:xmpp:message-moderate:0");
 500            }
 501            if (replaceElement == null) replaceElement = packet.findChild("retract", "urn:xmpp:message-retract:1");
 502            if (replaceElement == null) replaceElement = packet.findChild("moderate", "urn:xmpp:message-moderate:1");
 503            if (replaceElement != null) {
 504                var reason = replaceElement.findChildContent("reason", "urn:xmpp:message-moderate:0");
 505                if (reason == null) reason = replaceElement.findChildContent("reason", "urn:xmpp:message-moderate:1");
 506                replacementId = (fasten == null ? replaceElement : fasten).getAttribute("id");
 507                packet.setBody(reason == null ? "" : reason);
 508            }
 509        }
 510        LocalizedContent body = packet.getBody();
 511
 512        final var reactions = packet.getExtension(Reactions.class);
 513
 514        final Element axolotlEncrypted = packet.findChildEnsureSingle(XmppAxolotlMessage.CONTAINERTAG, AxolotlService.PEP_PREFIX);
 515        int status;
 516        final Jid counterpart;
 517        final Jid to = packet.getTo();
 518        final Jid from = packet.getFrom();
 519        final Element originId = packet.findChild("origin-id", Namespace.STANZA_IDS);
 520        final String remoteMsgId;
 521        if (originId != null && originId.getAttribute("id") != null) {
 522            remoteMsgId = originId.getAttribute("id");
 523        } else {
 524            remoteMsgId = packet.getId();
 525        }
 526        boolean notify = false;
 527
 528        Element html = packet.findChild("html", "http://jabber.org/protocol/xhtml-im");
 529        if (html != null && html.findChild("body", "http://www.w3.org/1999/xhtml") == null) {
 530            html = null;
 531        }
 532
 533        if (from == null || !InvalidJid.isValid(from) || !InvalidJid.isValid(to)) {
 534            Log.e(Config.LOGTAG, "encountered invalid message from='" + from + "' to='" + to + "'");
 535            return;
 536        }
 537        if (query != null && !query.muc() && isTypeGroupChat) {
 538            Log.e(Config.LOGTAG, account.getJid().asBareJid() + ": received groupchat (" + from + ") message on regular MAM request. skipping");
 539            return;
 540        }
 541        final Jid mucTrueCounterPart;
 542        final OccupantId occupant;
 543        if (isTypeGroupChat) {
 544            final Conversation conversation =
 545                    mXmppConnectionService.find(account, from.asBareJid());
 546            final Jid mucTrueCounterPartByPresence;
 547            if (conversation != null) {
 548                final var mucOptions = conversation.getMucOptions();
 549                occupant = mucOptions.occupantId() ? packet.getExtension(OccupantId.class) : null;
 550                final var user =
 551                        occupant == null ? null : mucOptions.findUserByOccupantId(occupant.getId(), from);
 552                mucTrueCounterPartByPresence = user == null ? null : user.getRealJid();
 553            } else {
 554                occupant = null;
 555                mucTrueCounterPartByPresence = null;
 556            }
 557            mucTrueCounterPart =
 558                    getTrueCounterpart(
 559                            (query != null && query.safeToExtractTrueCounterpart())
 560                                    ? mucUserElement
 561                                    : null,
 562                            mucTrueCounterPartByPresence);
 563        } else {
 564            mucTrueCounterPart = null;
 565            occupant = null;
 566        }
 567        boolean isMucStatusMessage = InvalidJid.hasValidFrom(packet) && from.isBareJid() && mucUserElement != null && mucUserElement.hasChild("status");
 568        boolean selfAddressed;
 569        if (packet.fromAccount(account)) {
 570            status = Message.STATUS_SEND;
 571            selfAddressed = to == null || account.getJid().asBareJid().equals(to.asBareJid());
 572            if (selfAddressed) {
 573                counterpart = from;
 574            } else {
 575                counterpart = to != null ? to : account.getJid();
 576            }
 577        } else {
 578            status = Message.STATUS_RECEIVED;
 579            counterpart = from;
 580            selfAddressed = false;
 581        }
 582
 583        final Invite invite = extractInvite(packet);
 584        if (invite != null) {
 585            if (invite.jid.asBareJid().equals(account.getJid().asBareJid())) {
 586                Log.d(Config.LOGTAG,account.getJid().asBareJid()+": ignore invite to "+invite.jid+" because it matches account");
 587            } else if (isTypeGroupChat) {
 588                Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": ignoring invite to " + invite.jid + " because it was received as group chat");
 589            } else if (invite.direct && (mucUserElement != null || invite.inviter == null || mXmppConnectionService.isMuc(account, invite.inviter))) {
 590                Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": ignoring direct invite to " + invite.jid + " because it was received in MUC");
 591            } else {
 592                invite.execute(account);
 593                return;
 594            }
 595        }
 596
 597        final boolean conversationIsProbablyMuc = isTypeGroupChat || mucUserElement != null || account.getXmppConnection().getMucServersWithholdAccount().contains(counterpart.getDomain().toEscapedString());
 598        final Element webxdc = packet.findChild("x", "urn:xmpp:webxdc:0");
 599        final Element thread = packet.findChild("thread");
 600        if (webxdc != null && thread != null) {
 601            final Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, counterpart.asBareJid(), conversationIsProbablyMuc, false, query, false);
 602            Jid webxdcSender = counterpart.asBareJid();
 603            if (conversation.getMode() == Conversation.MODE_MULTI) {
 604                if(conversation.getMucOptions().nonanonymous()) {
 605                    webxdcSender = conversation.getMucOptions().getTrueCounterpart(counterpart);
 606                } else {
 607                    webxdcSender = counterpart;
 608                }
 609            }
 610            final var document = webxdc.findChildContent("document", "urn:xmpp:webxdc:0");
 611            final var summary = webxdc.findChildContent("summary", "urn:xmpp:webxdc:0");
 612            final var payload = webxdc.findChildContent("json", "urn:xmpp:json:0");
 613            if (document != null || summary != null || payload != null) {
 614                mXmppConnectionService.insertWebxdcUpdate(new WebxdcUpdate(
 615                    conversation,
 616                    remoteMsgId,
 617                    counterpart,
 618                    thread,
 619                    body == null ? null : body.content,
 620                    document,
 621                    summary,
 622                    payload
 623                ));
 624            }
 625
 626            final var realtime = webxdc.findChildContent("data", "urn:xmpp:webxdc:0");
 627            if (realtime != null) conversation.webxdcRealtimeData(thread, realtime);
 628
 629            mXmppConnectionService.updateConversationUi();
 630        }
 631
 632        // Basic visibility for voice requests
 633        if (body == null && html == null && pgpEncrypted == null && axolotlEncrypted == null && !isMucStatusMessage) {
 634            final Element formEl = packet.findChild("x", "jabber:x:data");
 635            if (formEl != null) {
 636                final Data form = Data.parse(formEl);
 637                final String role = form.getValue("muc#role");
 638                final String nick = form.getValue("muc#roomnick");
 639                if ("http://jabber.org/protocol/muc#request".equals(form.getFormType()) && "participant".equals(role)) {
 640                    body = new LocalizedContent("" + nick + " is requesting to speak", "en", 1);
 641                }
 642            }
 643        }
 644
 645        if (reactions == null && (body != null || pgpEncrypted != null || (axolotlEncrypted != null && axolotlEncrypted.hasChild("payload")) || !attachments.isEmpty() || html != null || (packet.hasChild("subject") && packet.hasChild("thread"))) && !isMucStatusMessage) {
 646            final Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, counterpart.asBareJid(), conversationIsProbablyMuc, false, query, false);
 647            final boolean conversationMultiMode = conversation.getMode() == Conversation.MODE_MULTI;
 648
 649            if (serverMsgId == null) {
 650                serverMsgId = extractStanzaId(packet, isTypeGroupChat, conversation);
 651            }
 652
 653
 654            if (selfAddressed) {
 655                // don’t store serverMsgId on reflections for edits
 656                final var reflectedServerMsgId =
 657                        Strings.isNullOrEmpty(replacementId) ? serverMsgId : null;
 658                if (mXmppConnectionService.markMessage(
 659                        conversation,
 660                        remoteMsgId,
 661                        Message.STATUS_SEND_RECEIVED,
 662                        reflectedServerMsgId)) {
 663                    return;
 664                }
 665                status = Message.STATUS_RECEIVED;
 666                if (remoteMsgId != null && conversation.findMessageWithRemoteId(remoteMsgId, counterpart) != null) {
 667                    return;
 668                }
 669            }
 670
 671            if (isTypeGroupChat) {
 672                if (conversation.getMucOptions().isSelf(counterpart)) {
 673                    status = Message.STATUS_SEND_RECEIVED;
 674                    isCarbon = true; //not really carbon but received from another resource
 675                    // don’t store serverMsgId on reflections for edits
 676                    final var reflectedServerMsgId =
 677                            Strings.isNullOrEmpty(replacementId) ? serverMsgId : null;
 678                    if (mXmppConnectionService.markMessage(conversation, remoteMsgId, status, reflectedServerMsgId, body, html, packet.findChildContent("subject"), packet.findChild("thread"), attachments)) {
 679                        return;
 680                    } else if (remoteMsgId == null || Config.IGNORE_ID_REWRITE_IN_MUC) {
 681                        if (body != null) {
 682                            Message message = conversation.findSentMessageWithBody(body.content);
 683                            if (message != null) {
 684                                mXmppConnectionService.markMessage(message, status);
 685                                return;
 686                            }
 687                        }
 688                    }
 689                } else {
 690                    status = Message.STATUS_RECEIVED;
 691                }
 692            }
 693            final Message message;
 694            if (pgpEncrypted != null && Config.supportOpenPgp()) {
 695                message = new Message(conversation, pgpEncrypted, Message.ENCRYPTION_PGP, status);
 696            } else if (axolotlEncrypted != null && Config.supportOmemo()) {
 697                Jid origin;
 698                Set<Jid> fallbacksBySourceId = Collections.emptySet();
 699                if (conversationMultiMode) {
 700                    final Jid fallback = conversation.getMucOptions().getTrueCounterpart(counterpart);
 701                    origin = getTrueCounterpart(query != null ? mucUserElement : null, fallback);
 702                    if (origin == null) {
 703                        try {
 704                            fallbacksBySourceId = account.getAxolotlService().findCounterpartsBySourceId(XmppAxolotlMessage.parseSourceId(axolotlEncrypted));
 705                        } catch (IllegalArgumentException e) {
 706                            //ignoring
 707                        }
 708                    }
 709                    if (origin == null && fallbacksBySourceId.size() == 0) {
 710                        Log.d(Config.LOGTAG, "axolotl message in anonymous conference received and no possible fallbacks");
 711                        return;
 712                    }
 713                } else {
 714                    fallbacksBySourceId = Collections.emptySet();
 715                    origin = from;
 716                }
 717
 718                final boolean liveMessage = query == null && !isTypeGroupChat && mucUserElement == null;
 719                final boolean checkedForDuplicates = liveMessage || (serverMsgId != null && remoteMsgId != null && !conversation.possibleDuplicate(serverMsgId, remoteMsgId));
 720
 721                if (origin != null) {
 722                    message = parseAxolotlChat(axolotlEncrypted, origin, conversation, status, checkedForDuplicates, query != null);
 723                } else {
 724                    Message trial = null;
 725                    for (Jid fallback : fallbacksBySourceId) {
 726                        trial = parseAxolotlChat(axolotlEncrypted, fallback, conversation, status, checkedForDuplicates && fallbacksBySourceId.size() == 1, query != null);
 727                        if (trial != null) {
 728                            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": decoded muc message using fallback");
 729                            origin = fallback;
 730                            break;
 731                        }
 732                    }
 733                    message = trial;
 734                }
 735                if (message == null) {
 736                    if (query == null && extractChatState(mXmppConnectionService.find(account, counterpart.asBareJid()), isTypeGroupChat, packet)) {
 737                        mXmppConnectionService.updateConversationUi();
 738                    }
 739                    if (query != null && status == Message.STATUS_SEND && remoteMsgId != null) {
 740                        Message previouslySent = conversation.findSentMessageWithUuid(remoteMsgId);
 741                        if (previouslySent != null && previouslySent.getServerMsgId() == null && serverMsgId != null) {
 742                            previouslySent.setServerMsgId(serverMsgId);
 743                            mXmppConnectionService.databaseBackend.updateMessage(previouslySent, false);
 744                            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": encountered previously sent OMEMO message without serverId. updating...");
 745                        }
 746                    }
 747                    return;
 748                }
 749                if (conversationMultiMode) {
 750                    message.setTrueCounterpart(origin);
 751                }
 752            } else if (body == null && !attachments.isEmpty()) {
 753                message = new Message(conversation, "", Message.ENCRYPTION_NONE, status);
 754            } else {
 755                message = new Message(conversation, body == null ? null : body.content, Message.ENCRYPTION_NONE, status);
 756                if (body != null && body.count > 1) {
 757                    message.setBodyLanguage(body.language);
 758                }
 759            }
 760
 761            Element addresses = packet.findChild("addresses", "http://jabber.org/protocol/address");
 762            if (status == Message.STATUS_RECEIVED && addresses != null) {
 763                for (Element address : addresses.getChildren()) {
 764                    if (!address.getName().equals("address") || !address.getNamespace().equals("http://jabber.org/protocol/address")) continue;
 765
 766                    if (address.getAttribute("type").equals("ofrom") && address.getAttribute("jid") != null) {
 767                        Jid ofrom = address.getAttributeAsJid("jid");
 768                        if (InvalidJid.isValid(ofrom) && ofrom.getDomain().equals(counterpart.getDomain()) &&
 769                            conversation.getAccount().getRoster().getContact(counterpart.getDomain()).getPresences().anySupport("http://jabber.org/protocol/address")) {
 770
 771                            message.setTrueCounterpart(ofrom);
 772                        }
 773                    }
 774                }
 775            }
 776
 777            if (html != null) message.addPayload(html);
 778            message.setSubject(packet.findChildContent("subject"));
 779            message.setCounterpart(counterpart);
 780            message.setRemoteMsgId(remoteMsgId);
 781            message.setServerMsgId(serverMsgId);
 782            message.setCarbon(isCarbon);
 783            message.setTime(timestamp);
 784            if (!attachments.isEmpty()) {
 785                message.setFileParams(attachments.iterator().next());
 786                if (CryptoHelper.isPgpEncryptedUrl(message.getFileParams().url)) {
 787                    message.setEncryption(Message.ENCRYPTION_DECRYPTED);
 788                }
 789            }
 790            message.markable = packet.hasChild("markable", "urn:xmpp:chat-markers:0");
 791            for (Element el : packet.getChildren()) {
 792                if ((el.getName().equals("query") && el.getNamespace().equals("http://jabber.org/protocol/disco#items") && el.getAttribute("node").equals("http://jabber.org/protocol/commands")) ||
 793                    (el.getName().equals("fallback") && el.getNamespace().equals("urn:xmpp:fallback:0"))) {
 794                    message.addPayload(el);
 795                }
 796                if (el.getName().equals("thread") && (el.getNamespace() == null || el.getNamespace().equals("jabber:client"))) {
 797                    el.setAttribute("xmlns", "jabber:client");
 798                    message.addPayload(el);
 799                }
 800                if (el.getName().equals("reply") && el.getNamespace() != null && el.getNamespace().equals("urn:xmpp:reply:0")) {
 801                    message.addPayload(el);
 802                    if (el.getAttribute("id") != null) {
 803                        for (final var parent : mXmppConnectionService.getMessageFuzzyIds(conversation, List.of(el.getAttribute("id"))).entrySet()) {
 804                            message.setInReplyTo(parent.getValue());
 805                        }
 806                    }
 807                }
 808                if (el.getName().equals("attention") && el.getNamespace() != null && el.getNamespace().equals("urn:xmpp:attention:0")) {
 809                    message.addPayload(el);
 810                }
 811                if (el.getName().equals("Description") && el.getNamespace() != null && el.getNamespace().equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#")) {
 812                    message.addPayload(el);
 813                }
 814            }
 815            if (conversationMultiMode) {
 816                final var mucOptions = conversation.getMucOptions();
 817                if (occupant != null) {
 818                    message.setOccupantId(occupant.getId());
 819                }
 820                message.setMucUser(mucOptions.findUserByFullJid(counterpart));
 821                final Jid fallback = mucOptions.getTrueCounterpart(counterpart);
 822                Jid trueCounterpart;
 823                if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL) {
 824                    trueCounterpart = message.getTrueCounterpart();
 825                } else if (query != null && query.safeToExtractTrueCounterpart()) {
 826                    trueCounterpart = getTrueCounterpart(mucUserElement, fallback);
 827                } else {
 828                    trueCounterpart = fallback;
 829                }
 830                if (trueCounterpart != null && isTypeGroupChat) {
 831                    if (trueCounterpart.asBareJid().equals(account.getJid().asBareJid())) {
 832                        status = isTypeGroupChat ? Message.STATUS_SEND_RECEIVED : Message.STATUS_SEND;
 833                    } else {
 834                        status = Message.STATUS_RECEIVED;
 835                        message.setCarbon(false);
 836                    }
 837                }
 838                message.setStatus(status);
 839                message.setTrueCounterpart(trueCounterpart);
 840                if (!isTypeGroupChat) {
 841                    message.setType(Message.TYPE_PRIVATE);
 842                }
 843            } else {
 844                updateLastseen(account, from);
 845            }
 846
 847            if (replacementId != null && mXmppConnectionService.allowMessageCorrection()) {
 848                final Message replacedMessage = conversation.findMessageWithRemoteIdAndCounterpart(replacementId, counterpart);
 849Log.d("WUT", "" + replacementId + "  " + replacedMessage);
 850                if (replacedMessage != null) {
 851                    final boolean fingerprintsMatch = replacedMessage.getFingerprint() == null
 852                            || replacedMessage.getFingerprint().equals(message.getFingerprint());
 853                    final boolean trueCountersMatch = replacedMessage.getTrueCounterpart() != null
 854                            && message.getTrueCounterpart() != null
 855                            && replacedMessage.getTrueCounterpart().asBareJid().equals(message.getTrueCounterpart().asBareJid());
 856                    final boolean occupantIdMatch =
 857                            replacedMessage.getOccupantId() != null
 858                                    && replacedMessage
 859                                            .getOccupantId()
 860                                            .equals(message.getOccupantId());
 861                    final boolean mucUserMatches = query == null && replacedMessage.sameMucUser(message); //can not be checked when using mam
 862                    final boolean duplicate = conversation.hasDuplicateMessage(message);
 863                    if (fingerprintsMatch && (trueCountersMatch || occupantIdMatch || !conversationMultiMode || mucUserMatches || counterpart.isBareJid()) && !duplicate) {
 864                        synchronized (replacedMessage) {
 865                            final String uuid = replacedMessage.getUuid();
 866                            replacedMessage.setUuid(UUID.randomUUID().toString());
 867                            replacedMessage.setBody(message.getBody());
 868                            replacedMessage.setSubject(message.getSubject());
 869                            replacedMessage.setThread(message.getThread());
 870                            replacedMessage.putEdited(replacedMessage.getRemoteMsgId(), replacedMessage.getServerMsgId());
 871                            replacedMessage.setRemoteMsgId(remoteMsgId);
 872                            if (replaceElement != null && !replaceElement.getName().equals("replace")) {
 873                                mXmppConnectionService.getFileBackend().deleteFile(replacedMessage);
 874                                mXmppConnectionService.evictPreview(message.getUuid());
 875                                List<Element> thumbs = replacedMessage.getFileParams() != null ? replacedMessage.getFileParams().getThumbnails() : null;
 876                                if (thumbs != null && !thumbs.isEmpty()) {
 877                                    for (Element thumb : thumbs) {
 878                                        Uri uri = Uri.parse(thumb.getAttribute("uri"));
 879                                        if (uri.getScheme().equals("cid")) {
 880                                            Cid cid = BobTransfer.cid(uri);
 881                                            if (cid == null) continue;
 882                                            DownloadableFile f = mXmppConnectionService.getFileForCid(cid);
 883                                            if (f != null) {
 884                                                mXmppConnectionService.evictPreview(f);
 885                                                f.delete();
 886                                            }
 887                                        }
 888                                    }
 889                                }
 890                                replacedMessage.clearPayloads();
 891                                replacedMessage.setFileParams(null);
 892                                replacedMessage.addPayload(replaceElement);
 893                            } else {
 894                                replacedMessage.clearPayloads();
 895                                for (final var p : message.getPayloads()) {
 896                                    replacedMessage.addPayload(p);
 897                                }
 898                            }
 899                            replacedMessage.setInReplyTo(message.getInReplyTo());
 900
 901                            // we store the IDs of the replacing message. This is essentially unused
 902                            // today (only the fact that there are _some_ edits causes the edit icon
 903                            // to appear)
 904                            replacedMessage.putEdited(
 905                                    message.getRemoteMsgId(), message.getServerMsgId());
 906
 907                            // we used to call
 908                            // `replacedMessage.setServerMsgId(message.getServerMsgId());` so during
 909                            // catchup we could start from the edit; not the original message
 910                            // however this caused problems for things like reactions that refer to
 911                            // the serverMsgId
 912
 913                            replacedMessage.setEncryption(message.getEncryption());
 914                            if (replacedMessage.getStatus() == Message.STATUS_RECEIVED) {
 915                                replacedMessage.markUnread();
 916                            }
 917                            extractChatState(
 918                                    mXmppConnectionService.find(account, counterpart.asBareJid()),
 919                                    isTypeGroupChat,
 920                                    packet);
 921                            mXmppConnectionService.updateMessage(replacedMessage, uuid);
 922                            if (mXmppConnectionService.confirmMessages()
 923                                    && replacedMessage.getStatus() == Message.STATUS_RECEIVED
 924                                    && (replacedMessage.trusted() || replacedMessage.isPrivateMessage()) //TODO do we really want to send receipts for all PMs?
 925                                    && remoteMsgId != null
 926                                    && !selfAddressed
 927                                    && !isTypeGroupChat) {
 928                                processMessageReceipts(account, packet, remoteMsgId, query);
 929                            }
 930                            if (replacedMessage.getEncryption() == Message.ENCRYPTION_PGP) {
 931                                conversation.getAccount().getPgpDecryptionService().discard(replacedMessage);
 932                                conversation.getAccount().getPgpDecryptionService().decrypt(replacedMessage, false);
 933                            }
 934                        }
 935                        mXmppConnectionService.getNotificationService().updateNotification();
 936                        return;
 937                    } else {
 938                        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": received message correction but verification didn't check out");
 939                    }
 940                } else if (message.getBody() == null || message.getBody().equals("") || message.getBody().equals(" ")) {
 941                    return;
 942                }
 943                if (replaceElement != null && !replaceElement.getName().equals("replace")) return;
 944            }
 945
 946            boolean checkForDuplicates = (isTypeGroupChat && packet.hasChild("delay", "urn:xmpp:delay"))
 947                    || message.isPrivateMessage()
 948                    || message.getServerMsgId() != null
 949                    || (query == null && mXmppConnectionService.getMessageArchiveService().isCatchupInProgress(conversation));
 950            if (checkForDuplicates) {
 951                final Message duplicate = conversation.findDuplicateMessage(message);
 952                if (duplicate != null) {
 953                    final boolean serverMsgIdUpdated;
 954                    if (duplicate.getStatus() != Message.STATUS_RECEIVED
 955                            && duplicate.getUuid().equals(message.getRemoteMsgId())
 956                            && duplicate.getServerMsgId() == null
 957                            && message.getServerMsgId() != null) {
 958                        duplicate.setServerMsgId(message.getServerMsgId());
 959                        if (mXmppConnectionService.databaseBackend.updateMessage(duplicate, false)) {
 960                            serverMsgIdUpdated = true;
 961                        } else {
 962                            serverMsgIdUpdated = false;
 963                            Log.e(Config.LOGTAG, "failed to update message");
 964                        }
 965                    } else {
 966                        serverMsgIdUpdated = false;
 967                    }
 968                    Log.d(Config.LOGTAG, "skipping duplicate message with " + message.getCounterpart() + ". serverMsgIdUpdated=" + serverMsgIdUpdated);
 969                    return;
 970                }
 971            }
 972
 973            if (query != null && query.getPagingOrder() == MessageArchiveService.PagingOrder.REVERSE) {
 974                conversation.prepend(query.getActualInThisQuery(), message);
 975            } else {
 976                conversation.add(message);
 977            }
 978            if (query != null) {
 979                query.incrementActualMessageCount();
 980            }
 981
 982            if (query == null || query.isCatchup()) { //either no mam or catchup
 983                if (status == Message.STATUS_SEND || status == Message.STATUS_SEND_RECEIVED) {
 984                    mXmppConnectionService.markRead(conversation);
 985                    if (query == null) {
 986                        activateGracePeriod(account);
 987                    }
 988                } else {
 989                    message.markUnread();
 990                    notify = true;
 991                }
 992            }
 993
 994            if (message.getEncryption() == Message.ENCRYPTION_PGP) {
 995                notify = conversation.getAccount().getPgpDecryptionService().decrypt(message, notify);
 996            } else if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL_NOT_FOR_THIS_DEVICE || message.getEncryption() == Message.ENCRYPTION_AXOLOTL_FAILED) {
 997                notify = false;
 998            }
 999
1000            if (query == null) {
1001                extractChatState(mXmppConnectionService.find(account, counterpart.asBareJid()), isTypeGroupChat, packet);
1002                mXmppConnectionService.updateConversationUi();
1003            }
1004
1005            if (mXmppConnectionService.confirmMessages()
1006                    && message.getStatus() == Message.STATUS_RECEIVED
1007                    && (message.trusted() || message.isPrivateMessage())
1008                    && remoteMsgId != null
1009                    && !selfAddressed
1010                    && !isTypeGroupChat) {
1011                processMessageReceipts(account, packet, remoteMsgId, query);
1012            }
1013
1014            if (message.getFileParams() != null) {
1015                for (Cid cid : message.getFileParams().getCids()) {
1016                    File f = mXmppConnectionService.getFileForCid(cid);
1017                    if (f != null && f.canRead()) {
1018                        message.setRelativeFilePath(f.getAbsolutePath());
1019                        mXmppConnectionService.getFileBackend().updateFileParams(message, null, false);
1020                        break;
1021                    }
1022                }
1023            }
1024
1025            mXmppConnectionService.databaseBackend.createMessage(message);
1026
1027            final HttpConnectionManager manager = this.mXmppConnectionService.getHttpConnectionManager();
1028            if (message.getRelativeFilePath() == null && message.trusted() && message.treatAsDownloadable() && manager.getAutoAcceptFileSize() > 0) {
1029                if (message.getOob() != null && "cid".equalsIgnoreCase(message.getOob().getScheme())) {
1030                    try {
1031                        BobTransfer transfer = new BobTransfer.ForMessage(message, mXmppConnectionService);
1032                        message.setTransferable(transfer);
1033                        transfer.start();
1034                    } catch (URISyntaxException e) {
1035                        Log.d(Config.LOGTAG, "BobTransfer failed to parse URI");
1036                    }
1037                } else {
1038                    manager.createNewDownloadConnection(message);
1039                }
1040            } else if (notify) {
1041                if (query != null && query.isCatchup()) {
1042                    mXmppConnectionService.getNotificationService().pushFromBacklog(message);
1043                } else {
1044                    mXmppConnectionService.getNotificationService().push(message);
1045                }
1046            }
1047        } else if (!packet.hasChild("body")) { //no body
1048
1049            final Conversation conversation = mXmppConnectionService.find(account, from.asBareJid());
1050            if (axolotlEncrypted != null) {
1051                Jid origin;
1052                if (conversation != null && conversation.getMode() == Conversation.MODE_MULTI) {
1053                    final Jid fallback = conversation.getMucOptions().getTrueCounterpart(counterpart);
1054                    origin = getTrueCounterpart(query != null ? mucUserElement : null, fallback);
1055                    if (origin == null) {
1056                        Log.d(Config.LOGTAG, "omemo key transport message in anonymous conference received");
1057                        return;
1058                    }
1059                } else if (isTypeGroupChat) {
1060                    return;
1061                } else {
1062                    origin = from;
1063                }
1064                try {
1065                    final XmppAxolotlMessage xmppAxolotlMessage = XmppAxolotlMessage.fromElement(axolotlEncrypted, origin.asBareJid());
1066                    account.getAxolotlService().processReceivingKeyTransportMessage(xmppAxolotlMessage, query != null);
1067                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": omemo key transport message received from " + origin);
1068                } catch (Exception e) {
1069                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": invalid omemo key transport message received " + e.getMessage());
1070                    return;
1071                }
1072            }
1073
1074            if (query == null && extractChatState(mXmppConnectionService.find(account, counterpart.asBareJid()), isTypeGroupChat, packet)) {
1075                mXmppConnectionService.updateConversationUi();
1076            }
1077
1078            if (isTypeGroupChat) {
1079                if (packet.hasChild("subject") && !packet.hasChild("thread")) { // We already know it has no body per above
1080                    if (conversation != null && conversation.getMode() == Conversation.MODE_MULTI) {
1081                        conversation.setHasMessagesLeftOnServer(conversation.countMessages() > 0);
1082                        final LocalizedContent subject = packet.findInternationalizedChildContentInDefaultNamespace("subject");
1083                        if (subject != null && conversation.getMucOptions().setSubject(subject.content)) {
1084                            mXmppConnectionService.updateConversation(conversation);
1085                        }
1086                        mXmppConnectionService.updateConversationUi();
1087                        return;
1088                    }
1089                }
1090            }
1091            if (conversation != null && mucUserElement != null && InvalidJid.hasValidFrom(packet) && from.isBareJid()) {
1092                for (Element child : mucUserElement.getChildren()) {
1093                    if ("status".equals(child.getName())) {
1094                        try {
1095                            int code = Integer.parseInt(child.getAttribute("code"));
1096                            if ((code >= 170 && code <= 174) || (code >= 102 && code <= 104)) {
1097                                mXmppConnectionService.fetchConferenceConfiguration(conversation);
1098                                break;
1099                            }
1100                        } catch (Exception e) {
1101                            //ignored
1102                        }
1103                    } else if ("item".equals(child.getName())) {
1104                        MucOptions.User user = AbstractParser.parseItem(conversation, child);
1105                        Log.d(Config.LOGTAG, account.getJid() + ": changing affiliation for "
1106                                + user.getRealJid() + " to " + user.getAffiliation() + " in "
1107                                + conversation.getJid().asBareJid());
1108                        if (!user.realJidMatchesAccount()) {
1109                            boolean isNew = conversation.getMucOptions().updateUser(user);
1110                            mXmppConnectionService.getAvatarService().clear(conversation);
1111                            mXmppConnectionService.updateMucRosterUi();
1112                            mXmppConnectionService.updateConversationUi();
1113                            Contact contact = user.getContact();
1114                            if (!user.getAffiliation().ranks(MucOptions.Affiliation.MEMBER)) {
1115                                Jid jid = user.getRealJid();
1116                                List<Jid> cryptoTargets = conversation.getAcceptedCryptoTargets();
1117                                if (cryptoTargets.remove(user.getRealJid())) {
1118                                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": removed " + jid + " from crypto targets of " + conversation.getName());
1119                                    conversation.setAcceptedCryptoTargets(cryptoTargets);
1120                                    mXmppConnectionService.updateConversation(conversation);
1121                                }
1122                            } else if (isNew
1123                                    && user.getRealJid() != null
1124                                    && conversation.getMucOptions().isPrivateAndNonAnonymous()
1125                                    && (contact == null || !contact.mutualPresenceSubscription())
1126                                    && account.getAxolotlService().hasEmptyDeviceList(user.getRealJid())) {
1127                                account.getAxolotlService().fetchDeviceIds(user.getRealJid());
1128                            }
1129                        }
1130                    }
1131                }
1132            }
1133            if (!isTypeGroupChat) {
1134                for (Element child : packet.getChildren()) {
1135                    if (Namespace.JINGLE_MESSAGE.equals(child.getNamespace()) && JINGLE_MESSAGE_ELEMENT_NAMES.contains(child.getName())) {
1136                        final String action = child.getName();
1137                        final String sessionId = child.getAttribute("id");
1138                        if (sessionId == null) {
1139                            break;
1140                        }
1141                        if (query == null && offlineMessagesRetrieved) {
1142                            if (serverMsgId == null) {
1143                                serverMsgId = extractStanzaId(account, packet);
1144                            }
1145                            mXmppConnectionService
1146                                    .getJingleConnectionManager()
1147                                    .deliverMessage(
1148                                            account,
1149                                            packet.getTo(),
1150                                            packet.getFrom(),
1151                                            child,
1152                                            remoteMsgId,
1153                                            serverMsgId,
1154                                            timestamp);
1155                            final Contact contact = account.getRoster().getContact(from);
1156                            // this is the same condition that is found in JingleRtpConnection for
1157                            // the 'ringing' response. Responding with delivery receipts predates
1158                            // the 'ringing' spec'd
1159                            final boolean sendReceipts =
1160                                    contact.showInContactList()
1161                                            || Config.JINGLE_MESSAGE_INIT_STRICT_OFFLINE_CHECK;
1162                            if (remoteMsgId != null && !contact.isSelf() && sendReceipts) {
1163                                processMessageReceipts(account, packet, remoteMsgId, null);
1164                            }
1165                        } else if ((query != null && query.isCatchup()) || !offlineMessagesRetrieved) {
1166                            if ("propose".equals(action)) {
1167                                final Element description = child.findChild("description");
1168                                final String namespace =
1169                                        description == null ? null : description.getNamespace();
1170                                if (Namespace.JINGLE_APPS_RTP.equals(namespace)) {
1171                                    final Conversation c =
1172                                            mXmppConnectionService.findOrCreateConversation(
1173                                                    account, counterpart.asBareJid(), false, false);
1174                                    final Message preExistingMessage =
1175                                            c.findRtpSession(sessionId, status);
1176                                    if (preExistingMessage != null) {
1177                                        preExistingMessage.setServerMsgId(serverMsgId);
1178                                        mXmppConnectionService.updateMessage(preExistingMessage);
1179                                        break;
1180                                    }
1181                                    final Message message =
1182                                            new Message(
1183                                                    c, status, Message.TYPE_RTP_SESSION, sessionId);
1184                                    message.setServerMsgId(serverMsgId);
1185                                    message.setTime(timestamp);
1186                                    message.setBody(new RtpSessionStatus(false, 0).toString());
1187                                    c.add(message);
1188                                    mXmppConnectionService.databaseBackend.createMessage(message);
1189                                }
1190                            } else if ("proceed".equals(action)) {
1191                                // status needs to be flipped to find the original propose
1192                                final Conversation c =
1193                                        mXmppConnectionService.findOrCreateConversation(
1194                                                account, counterpart.asBareJid(), false, false);
1195                                final int s =
1196                                        packet.fromAccount(account)
1197                                                ? Message.STATUS_RECEIVED
1198                                                : Message.STATUS_SEND;
1199                                final Message message = c.findRtpSession(sessionId, s);
1200                                if (message != null) {
1201                                    message.setBody(new RtpSessionStatus(true, 0).toString());
1202                                    if (serverMsgId != null) {
1203                                        message.setServerMsgId(serverMsgId);
1204                                    }
1205                                    message.setTime(timestamp);
1206                                    mXmppConnectionService.updateMessage(message, true);
1207                                } else {
1208                                    Log.d(
1209                                            Config.LOGTAG,
1210                                            "unable to find original rtp session message for received propose");
1211                                }
1212
1213                            } else if ("finish".equals(action)) {
1214                                Log.d(
1215                                        Config.LOGTAG,
1216                                        "received JMI 'finish' during MAM catch-up. Can be used to update success/failure and duration");
1217                            }
1218                        } else {
1219                            //MAM reloads (non catchups
1220                            if ("propose".equals(action)) {
1221                                final Element description = child.findChild("description");
1222                                final String namespace = description == null ? null : description.getNamespace();
1223                                if (Namespace.JINGLE_APPS_RTP.equals(namespace)) {
1224                                    final Conversation c = mXmppConnectionService.findOrCreateConversation(account, counterpart.asBareJid(), false, false);
1225                                    final Message preExistingMessage = c.findRtpSession(sessionId, status);
1226                                    if (preExistingMessage != null) {
1227                                        preExistingMessage.setServerMsgId(serverMsgId);
1228                                        mXmppConnectionService.updateMessage(preExistingMessage);
1229                                        break;
1230                                    }
1231                                    final Message message = new Message(
1232                                            c,
1233                                            status,
1234                                            Message.TYPE_RTP_SESSION,
1235                                            sessionId
1236                                    );
1237                                    message.setServerMsgId(serverMsgId);
1238                                    message.setTime(timestamp);
1239                                    message.setBody(new RtpSessionStatus(true, 0).toString());
1240                                    if (query.getPagingOrder() == MessageArchiveService.PagingOrder.REVERSE) {
1241                                        c.prepend(query.getActualInThisQuery(), message);
1242                                    } else {
1243                                        c.add(message);
1244                                    }
1245                                    query.incrementActualMessageCount();
1246                                    mXmppConnectionService.databaseBackend.createMessage(message);
1247                                }
1248                            }
1249                        }
1250                        break;
1251                    }
1252                }
1253            }
1254        }
1255
1256        Element received = packet.findChild("received", "urn:xmpp:chat-markers:0");
1257        if (received == null) {
1258            received = packet.findChild("received", "urn:xmpp:receipts");
1259        }
1260        if (received != null) {
1261            String id = received.getAttribute("id");
1262            if (packet.fromAccount(account)) {
1263                if (query != null && id != null && packet.getTo() != null) {
1264                    query.removePendingReceiptRequest(new ReceiptRequest(packet.getTo(), id));
1265                }
1266            } else if (id != null) {
1267                if (id.startsWith(JingleRtpConnection.JINGLE_MESSAGE_PROPOSE_ID_PREFIX)) {
1268                    final String sessionId = id.substring(JingleRtpConnection.JINGLE_MESSAGE_PROPOSE_ID_PREFIX.length());
1269                    mXmppConnectionService.getJingleConnectionManager()
1270                            .updateProposedSessionDiscovered(account, from, sessionId, JingleConnectionManager.DeviceDiscoveryState.DISCOVERED);
1271                } else {
1272                    mXmppConnectionService.markMessage(account, from.asBareJid(), id, Message.STATUS_SEND_RECEIVED);
1273                }
1274            }
1275        }
1276        final Element displayed = packet.findChild("displayed", "urn:xmpp:chat-markers:0");
1277        if (displayed != null) {
1278            final String id = displayed.getAttribute("id");
1279            // TODO we don’t even use 'sender' any more. Remove this!
1280            final Jid sender = InvalidJid.getNullForInvalid(displayed.getAttributeAsJid("sender"));
1281            if (packet.fromAccount(account) && !selfAddressed) {
1282                final Conversation c =
1283                        mXmppConnectionService.find(account, counterpart.asBareJid());
1284                final Message message =
1285                        (c == null || id == null) ? null : c.findReceivedWithRemoteId(id);
1286                if (message != null && (query == null || query.isCatchup())) {
1287                    mXmppConnectionService.markReadUpTo(c, message);
1288                }
1289                if (query == null) {
1290                    activateGracePeriod(account);
1291                }
1292            } else if (isTypeGroupChat) {
1293                final Conversation conversation = mXmppConnectionService.find(account, counterpart.asBareJid());
1294                final Message message;
1295                if (conversation != null && id != null) {
1296                    if (sender != null) {
1297                        message = conversation.findMessageWithRemoteId(id, sender);
1298                    } else {
1299                        message = conversation.findMessageWithServerMsgId(id);
1300                    }
1301                } else {
1302                    message = null;
1303                }
1304                if (message != null) {
1305                    // TODO use occupantId to extract true counterpart from presence
1306                    final Jid fallback = conversation.getMucOptions().getTrueCounterpart(counterpart);
1307                    // TODO try to externalize mucTrueCounterpart
1308                    final Jid trueJid = getTrueCounterpart((query != null && query.safeToExtractTrueCounterpart()) ? mucUserElement : null, fallback);
1309                    final boolean trueJidMatchesAccount = account.getJid().asBareJid().equals(trueJid == null ? null : trueJid.asBareJid());
1310                    if (trueJidMatchesAccount || conversation.getMucOptions().isSelf(counterpart)) {
1311                        if (!message.isRead() && (query == null || query.isCatchup())) { //checking if message is unread fixes race conditions with reflections
1312                            mXmppConnectionService.markReadUpTo(conversation, message);
1313                        }
1314                    } else if (!counterpart.isBareJid() && trueJid != null) {
1315                        final ReadByMarker readByMarker = ReadByMarker.from(counterpart, trueJid);
1316                        if (message.addReadByMarker(readByMarker)) {
1317                            final var mucOptions = conversation.getMucOptions();
1318                            final var everyone = ImmutableSet.copyOf(mucOptions.getMembers(false));
1319                            final var readyBy = message.getReadyByTrue();
1320                            final var mStatus = message.getStatus();
1321                            if (mucOptions.isPrivateAndNonAnonymous()
1322                                    && (mStatus == Message.STATUS_SEND_RECEIVED
1323                                            || mStatus == Message.STATUS_SEND)
1324                                    && readyBy.containsAll(everyone)) {
1325                                message.setStatus(Message.STATUS_SEND_DISPLAYED);
1326                            }
1327                            mXmppConnectionService.updateMessage(message, false);
1328                        }
1329                    }
1330                }
1331            } else {
1332                final Message displayedMessage = mXmppConnectionService.markMessage(account, from.asBareJid(), id, Message.STATUS_SEND_DISPLAYED);
1333                Message message = displayedMessage == null ? null : displayedMessage.prev();
1334                while (message != null
1335                        && message.getStatus() == Message.STATUS_SEND_RECEIVED
1336                        && message.getTimeSent() < displayedMessage.getTimeSent()) {
1337                    mXmppConnectionService.markMessage(message, Message.STATUS_SEND_DISPLAYED);
1338                    message = message.prev();
1339                }
1340                if (displayedMessage != null && selfAddressed) {
1341                    dismissNotification(account, counterpart, query, id);
1342                }
1343            }
1344        }
1345
1346        if (reactions != null) {
1347            final String reactingTo = reactions.getId();
1348            final Conversation conversation =
1349                    mXmppConnectionService.find(account, counterpart.asBareJid());
1350
1351            if (conversation != null && reactingTo != null) {
1352                if (isTypeGroupChat && conversation.getMode() == Conversational.MODE_MULTI) {
1353                    final var mucOptions = conversation.getMucOptions();
1354                    final var occupantId = occupant == null ? null : occupant.getId();
1355                    if (occupantId != null) {
1356                        final boolean isReceived = !mucOptions.isSelf(occupantId);
1357                        final Message message;
1358                        final var inMemoryMessage =
1359                                conversation.findMessageWithServerMsgId(reactingTo);
1360                        if (inMemoryMessage != null) {
1361                            message = inMemoryMessage;
1362                        } else {
1363                            message =
1364                                    mXmppConnectionService.databaseBackend
1365                                            .getMessageWithServerMsgId(conversation, reactingTo);
1366                        }
1367                        if (message != null) {
1368                            final var newReactions = new HashSet<>(reactions.getReactions());
1369                            newReactions.removeAll(message.getReactions().stream().filter(r -> occupantId.equals(r.occupantId)).map(r -> r.reaction).collect(Collectors.toList()));
1370                            final var combinedReactions =
1371                                    Reaction.withOccupantId(
1372                                            message.getReactions(),
1373                                            reactions.getReactions(),
1374                                            isReceived,
1375                                            counterpart,
1376                                            null,
1377                                            occupantId,
1378                                            message.getRemoteMsgId());
1379                            message.setReactions(combinedReactions);
1380                            mXmppConnectionService.updateMessage(message, false);
1381                            if (isReceived) mXmppConnectionService.getNotificationService().push(message, counterpart, occupantId, newReactions);
1382                        } else {
1383                            Log.d(Config.LOGTAG, "message with id " + reactingTo + " not found");
1384                        }
1385                    } else {
1386                        Log.d(
1387                                Config.LOGTAG,
1388                                "received reaction in channel w/o occupant ids. ignoring");
1389                    }
1390                } else if (conversation.getMode() == Conversational.MODE_SINGLE) {
1391                    final Message message;
1392                    final var inMemoryMessage =
1393                            conversation.findMessageWithUuidOrRemoteId(reactingTo);
1394                    if (inMemoryMessage != null) {
1395                        message = inMemoryMessage;
1396                    } else {
1397                        message =
1398                                mXmppConnectionService.databaseBackend.getMessageWithUuidOrRemoteId(
1399                                        conversation, reactingTo);
1400                    }
1401                    final boolean isReceived;
1402                    final Jid reactionFrom;
1403                    if (packet.fromAccount(account)) {
1404                        isReceived = false;
1405                        reactionFrom = account.getJid().asBareJid();
1406                    } else {
1407                        isReceived = true;
1408                        reactionFrom = counterpart;
1409                    }
1410                    packet.fromAccount(account);
1411                    if (message != null) {
1412                        final var newReactions = new HashSet<>(reactions.getReactions());
1413                        newReactions.removeAll(message.getReactions().stream().filter(r -> reactionFrom.equals(r.from)).map(r -> r.reaction).collect(Collectors.toList()));
1414                        final var combinedReactions =
1415                                Reaction.withFrom(
1416                                        message.getReactions(),
1417                                        reactions.getReactions(),
1418                                        isReceived,
1419                                        reactionFrom,
1420                                        message.getRemoteMsgId());
1421                        message.setReactions(combinedReactions);
1422                        mXmppConnectionService.updateMessage(message, false);
1423                        if (status < Message.STATUS_SEND) mXmppConnectionService.getNotificationService().push(message, counterpart, null, newReactions);
1424                    } else {
1425                        Log.d(Config.LOGTAG, "message with id " + reactingTo + " not found");
1426                    }
1427                }
1428            }
1429        }
1430
1431        final Element event = original.findChild("event", "http://jabber.org/protocol/pubsub#event");
1432        if (event != null && InvalidJid.hasValidFrom(original) && original.getFrom().isBareJid()) {
1433            if (event.hasChild("items")) {
1434                parseEvent(event, original.getFrom(), account);
1435            } else if (event.hasChild("delete")) {
1436                parseDeleteEvent(event, original.getFrom(), account);
1437            } else if (event.hasChild("purge")) {
1438                parsePurgeEvent(event, original.getFrom(), account);
1439            }
1440        }
1441
1442        final String nick = packet.findChildContent("nick", Namespace.NICK);
1443        if (nick != null && InvalidJid.hasValidFrom(original)) {
1444            if (mXmppConnectionService.isMuc(account, from)) {
1445                return;
1446            }
1447            final Contact contact = account.getRoster().getContact(from);
1448            if (contact.setPresenceName(nick)) {
1449                mXmppConnectionService.syncRoster(account);
1450                mXmppConnectionService.getAvatarService().clear(contact);
1451            }
1452        }
1453    }
1454
1455    private static Pair<im.conversations.android.xmpp.model.stanza.Message,Long> getForwardedMessagePacket(final im.conversations.android.xmpp.model.stanza.Message original, Class<? extends Extension> clazz) {
1456        final var extension = original.getExtension(clazz);
1457        final var forwarded = extension == null ? null : extension.getExtension(Forwarded.class);
1458        if (forwarded == null) {
1459            return null;
1460        }
1461        final Long timestamp = AbstractParser.parseTimestamp(forwarded, null);
1462        final var forwardedMessage = forwarded.getMessage();
1463        if (forwardedMessage == null) {
1464            return null;
1465        }
1466        return new Pair<>(forwardedMessage,timestamp);
1467    }
1468
1469    private static Pair<im.conversations.android.xmpp.model.stanza.Message,Long> getForwardedMessagePacket(final im.conversations.android.xmpp.model.stanza.Message original, final String name, final String namespace) {
1470        final Element wrapper = original.findChild(name, namespace);
1471        final var forwardedElement = wrapper == null ? null : wrapper.findChild("forwarded",Namespace.FORWARD);
1472        if (forwardedElement instanceof Forwarded forwarded) {
1473            final Long timestamp = AbstractParser.parseTimestamp(forwarded, null);
1474            final var forwardedMessage = forwarded.getMessage();
1475            if (forwardedMessage == null) {
1476                return null;
1477            }
1478            return new Pair<>(forwardedMessage,timestamp);
1479        }
1480        return null;
1481    }
1482
1483    private void dismissNotification(Account account, Jid counterpart, MessageArchiveService.Query query, final String id) {
1484        final Conversation conversation = mXmppConnectionService.find(account, counterpart.asBareJid());
1485        if (conversation != null && (query == null || query.isCatchup())) {
1486            final String displayableId = conversation.findMostRecentRemoteDisplayableId();
1487            if (displayableId != null && displayableId.equals(id)) {
1488                mXmppConnectionService.markRead(conversation);
1489            } else {
1490                Log.w(Config.LOGTAG, account.getJid().asBareJid() + ": received dismissing display marker that did not match our last id in that conversation");
1491            }
1492        }
1493    }
1494
1495    private void processMessageReceipts(final Account account, final im.conversations.android.xmpp.model.stanza.Message packet, final String remoteMsgId, MessageArchiveService.Query query) {
1496        final boolean markable = packet.hasChild("markable", "urn:xmpp:chat-markers:0");
1497        final boolean request = packet.hasChild("request", "urn:xmpp:receipts");
1498        if (query == null) {
1499            final ArrayList<String> receiptsNamespaces = new ArrayList<>();
1500            if (markable) {
1501                receiptsNamespaces.add("urn:xmpp:chat-markers:0");
1502            }
1503            if (request) {
1504                receiptsNamespaces.add("urn:xmpp:receipts");
1505            }
1506            if (receiptsNamespaces.size() > 0) {
1507                final var receipt = mXmppConnectionService.getMessageGenerator().received(account,
1508                        packet.getFrom(),
1509                        remoteMsgId,
1510                        receiptsNamespaces,
1511                        packet.getType());
1512                mXmppConnectionService.sendMessagePacket(account, receipt);
1513            }
1514        } else if (query.isCatchup()) {
1515            if (request) {
1516                query.addPendingReceiptRequest(new ReceiptRequest(packet.getFrom(), remoteMsgId));
1517            }
1518        }
1519    }
1520
1521    private void activateGracePeriod(Account account) {
1522        long duration = mXmppConnectionService.getLongPreference("grace_period_length", R.integer.grace_period) * 1000;
1523        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": activating grace period till " + TIME_FORMAT.format(new Date(System.currentTimeMillis() + duration)));
1524        account.activateGracePeriod(duration);
1525    }
1526
1527    private class Invite {
1528        final Jid jid;
1529        final String password;
1530        final boolean direct;
1531        final Jid inviter;
1532
1533        Invite(Jid jid, String password, boolean direct, Jid inviter) {
1534            this.jid = jid;
1535            this.password = password;
1536            this.direct = direct;
1537            this.inviter = inviter;
1538        }
1539
1540        public boolean execute(final Account account) {
1541            if (this.jid == null) {
1542                return false;
1543            }
1544            final Contact contact = this.inviter != null ? account.getRoster().getContact(this.inviter) : null;
1545            if (contact != null && contact.isBlocked()) {
1546                Log.d(Config.LOGTAG,account.getJid().asBareJid()+": ignore invite from "+contact.getJid()+" because contact is blocked");
1547                return false;
1548            }
1549            final Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, jid, true, false);
1550            conversation.setAttribute("inviter", inviter.toEscapedString());
1551            if (conversation.getMucOptions().online()) {
1552                Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": received invite to " + jid + " but muc is considered to be online");
1553                mXmppConnectionService.mucSelfPingAndRejoin(conversation);
1554            } else {
1555                conversation.getMucOptions().setPassword(password);
1556                mXmppConnectionService.databaseBackend.updateConversation(conversation);
1557                mXmppConnectionService.joinMuc(conversation, contact != null && contact.showInContactList());
1558                mXmppConnectionService.updateConversationUi();
1559            }
1560            return true;
1561        }
1562    }
1563
1564    private static int parseInt(String value) {
1565        try {
1566            return Integer.parseInt(value);
1567        } catch (NumberFormatException e) {
1568            return 0;
1569        }
1570    }
1571}