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