MessageParser.java

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