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