MessageParser.java

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