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_PROPOSE_ID_PREFIX)) {
310                    final String sessionId = id.substring(JingleRtpConnection.JINGLE_MESSAGE_PROPOSE_ID_PREFIX.length());
311                    mXmppConnectionService.getJingleConnectionManager()
312                            .updateProposedSessionDiscovered(account, from, sessionId, JingleConnectionManager.DeviceDiscoveryState.FAILED);
313                    return true;
314                }
315                if (id.startsWith(JingleRtpConnection.JINGLE_MESSAGE_PROCEED_ID_PREFIX)) {
316                    final String sessionId = id.substring(JingleRtpConnection.JINGLE_MESSAGE_PROCEED_ID_PREFIX.length());
317                    mXmppConnectionService.getJingleConnectionManager().failProceed(account, from, sessionId);
318                    return true;
319                }
320                mXmppConnectionService.markMessage(account,
321                        from.asBareJid(),
322                        id,
323                        Message.STATUS_SEND_FAILED,
324                        extractErrorMessage(packet));
325                final Element error = packet.findChild("error");
326                final boolean pingWorthyError = error != null && (error.hasChild("not-acceptable") || error.hasChild("remote-server-timeout") || error.hasChild("remote-server-not-found"));
327                if (pingWorthyError) {
328                    Conversation conversation = mXmppConnectionService.find(account, from);
329                    if (conversation != null && conversation.getMode() == Conversational.MODE_MULTI) {
330                        if (conversation.getMucOptions().online()) {
331                            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": received ping worthy error for seemingly online muc at " + from);
332                            mXmppConnectionService.mucSelfPingAndRejoin(conversation);
333                        }
334                    }
335                }
336            }
337            return true;
338        }
339        return false;
340    }
341
342    @Override
343    public void onMessagePacketReceived(Account account, MessagePacket original) {
344        if (handleErrorMessage(account, original)) {
345            return;
346        }
347        final MessagePacket packet;
348        Long timestamp = null;
349        boolean isCarbon = false;
350        String serverMsgId = null;
351        final Element fin = original.findChild("fin", MessageArchiveService.Version.MAM_0.namespace);
352        if (fin != null) {
353            mXmppConnectionService.getMessageArchiveService().processFinLegacy(fin, original.getFrom());
354            return;
355        }
356        final Element result = MessageArchiveService.Version.findResult(original);
357        final MessageArchiveService.Query query = result == null ? null : mXmppConnectionService.getMessageArchiveService().findQuery(result.getAttribute("queryid"));
358        if (query != null && query.validFrom(original.getFrom())) {
359            Pair<MessagePacket, Long> f = original.getForwardedMessagePacket("result", query.version.namespace);
360            if (f == null) {
361                return;
362            }
363            timestamp = f.second;
364            packet = f.first;
365            serverMsgId = result.getAttribute("id");
366            query.incrementMessageCount();
367        } else if (query != null) {
368            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": received mam result from invalid sender");
369            return;
370        } else if (original.fromServer(account)) {
371            Pair<MessagePacket, Long> f;
372            f = original.getForwardedMessagePacket("received", "urn:xmpp:carbons:2");
373            f = f == null ? original.getForwardedMessagePacket("sent", "urn:xmpp:carbons:2") : f;
374            packet = f != null ? f.first : original;
375            if (handleErrorMessage(account, packet)) {
376                return;
377            }
378            timestamp = f != null ? f.second : null;
379            isCarbon = f != null;
380        } else {
381            packet = original;
382        }
383
384        if (timestamp == null) {
385            timestamp = AbstractParser.parseTimestamp(original, AbstractParser.parseTimestamp(packet));
386        }
387        final LocalizedContent body = packet.getBody();
388        final Element mucUserElement = packet.findChild("x", Namespace.MUC_USER);
389        final String pgpEncrypted = packet.findChildContent("x", "jabber:x:encrypted");
390        final Element replaceElement = packet.findChild("replace", "urn:xmpp:message-correct:0");
391        final Element oob = packet.findChild("x", Namespace.OOB);
392        final Element xP1S3 = packet.findChild("x", Namespace.P1_S3_FILE_TRANSFER);
393        final URL xP1S3url = xP1S3 == null ? null : P1S3UrlStreamHandler.of(xP1S3);
394        final String oobUrl = oob != null ? oob.findChildContent("url") : null;
395        final String replacementId = replaceElement == null ? null : replaceElement.getAttribute("id");
396        final Element axolotlEncrypted = packet.findChildEnsureSingle(XmppAxolotlMessage.CONTAINERTAG, AxolotlService.PEP_PREFIX);
397        int status;
398        final Jid counterpart;
399        final Jid to = packet.getTo();
400        final Jid from = packet.getFrom();
401        final Element originId = packet.findChild("origin-id", Namespace.STANZA_IDS);
402        final String remoteMsgId;
403        if (originId != null && originId.getAttribute("id") != null) {
404            remoteMsgId = originId.getAttribute("id");
405        } else {
406            remoteMsgId = packet.getId();
407        }
408        boolean notify = false;
409
410        if (from == null || !InvalidJid.isValid(from) || !InvalidJid.isValid(to)) {
411            Log.e(Config.LOGTAG, "encountered invalid message from='" + from + "' to='" + to + "'");
412            return;
413        }
414
415        boolean isTypeGroupChat = packet.getType() == MessagePacket.TYPE_GROUPCHAT;
416        if (query != null && !query.muc() && isTypeGroupChat) {
417            Log.e(Config.LOGTAG, account.getJid().asBareJid() + ": received groupchat (" + from + ") message on regular MAM request. skipping");
418            return;
419        }
420        boolean isMucStatusMessage = InvalidJid.hasValidFrom(packet) && from.isBareJid() && mucUserElement != null && mucUserElement.hasChild("status");
421        boolean selfAddressed;
422        if (packet.fromAccount(account)) {
423            status = Message.STATUS_SEND;
424            selfAddressed = to == null || account.getJid().asBareJid().equals(to.asBareJid());
425            if (selfAddressed) {
426                counterpart = from;
427            } else {
428                counterpart = to != null ? to : account.getJid();
429            }
430        } else {
431            status = Message.STATUS_RECEIVED;
432            counterpart = from;
433            selfAddressed = false;
434        }
435
436        final Invite invite = extractInvite(packet);
437        if (invite != null) {
438            if (isTypeGroupChat) {
439                Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": ignoring invite to " + invite.jid + " because type=groupchat");
440            } else if (invite.direct && (mucUserElement != null || invite.inviter == null || mXmppConnectionService.isMuc(account, invite.inviter))) {
441                Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": ignoring direct invite to " + invite.jid + " because it was received in MUC");
442            } else {
443                invite.execute(account);
444                return;
445            }
446        }
447
448        if ((body != null || pgpEncrypted != null || (axolotlEncrypted != null && axolotlEncrypted.hasChild("payload")) || oobUrl != null || xP1S3 != null) && !isMucStatusMessage) {
449            final boolean conversationIsProbablyMuc = isTypeGroupChat || mucUserElement != null || account.getXmppConnection().getMucServersWithholdAccount().contains(counterpart.getDomain());
450            final Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, counterpart.asBareJid(), conversationIsProbablyMuc, false, query, false);
451            final boolean conversationMultiMode = conversation.getMode() == Conversation.MODE_MULTI;
452
453            if (serverMsgId == null) {
454                serverMsgId = extractStanzaId(packet, isTypeGroupChat, conversation);
455            }
456
457
458            if (selfAddressed) {
459                if (mXmppConnectionService.markMessage(conversation, remoteMsgId, Message.STATUS_SEND_RECEIVED, serverMsgId)) {
460                    return;
461                }
462                status = Message.STATUS_RECEIVED;
463                if (remoteMsgId != null && conversation.findMessageWithRemoteId(remoteMsgId, counterpart) != null) {
464                    return;
465                }
466            }
467
468            if (isTypeGroupChat) {
469                if (conversation.getMucOptions().isSelf(counterpart)) {
470                    status = Message.STATUS_SEND_RECEIVED;
471                    isCarbon = true; //not really carbon but received from another resource
472                    //TODO this would be the place to change the body after something like mod_pastebin
473                    if (mXmppConnectionService.markMessage(conversation, remoteMsgId, status, serverMsgId)) {
474                        return;
475                    } else if (remoteMsgId == null || Config.IGNORE_ID_REWRITE_IN_MUC) {
476                        LocalizedContent localizedBody = packet.getBody();
477                        if (localizedBody != null) {
478                            Message message = conversation.findSentMessageWithBody(localizedBody.content);
479                            if (message != null) {
480                                mXmppConnectionService.markMessage(message, status);
481                                return;
482                            }
483                        }
484                    }
485                } else {
486                    status = Message.STATUS_RECEIVED;
487                }
488            }
489            final Message message;
490            if (xP1S3url != null) {
491                message = new Message(conversation, xP1S3url.toString(), Message.ENCRYPTION_NONE, status);
492                message.setOob(true);
493                if (CryptoHelper.isPgpEncryptedUrl(xP1S3url.toString())) {
494                    message.setEncryption(Message.ENCRYPTION_DECRYPTED);
495                }
496            } else if (pgpEncrypted != null && Config.supportOpenPgp()) {
497                message = new Message(conversation, pgpEncrypted, Message.ENCRYPTION_PGP, status);
498            } else if (axolotlEncrypted != null && Config.supportOmemo()) {
499                Jid origin;
500                Set<Jid> fallbacksBySourceId = Collections.emptySet();
501                if (conversationMultiMode) {
502                    final Jid fallback = conversation.getMucOptions().getTrueCounterpart(counterpart);
503                    origin = getTrueCounterpart(query != null ? mucUserElement : null, fallback);
504                    if (origin == null) {
505                        try {
506                            fallbacksBySourceId = account.getAxolotlService().findCounterpartsBySourceId(XmppAxolotlMessage.parseSourceId(axolotlEncrypted));
507                        } catch (IllegalArgumentException e) {
508                            //ignoring
509                        }
510                    }
511                    if (origin == null && fallbacksBySourceId.size() == 0) {
512                        Log.d(Config.LOGTAG, "axolotl message in anonymous conference received and no possible fallbacks");
513                        return;
514                    }
515                } else {
516                    fallbacksBySourceId = Collections.emptySet();
517                    origin = from;
518                }
519
520                final boolean liveMessage = query == null && !isTypeGroupChat && mucUserElement == null;
521                final boolean checkedForDuplicates = liveMessage || (serverMsgId != null && remoteMsgId != null && !conversation.possibleDuplicate(serverMsgId, remoteMsgId));
522
523                if (origin != null) {
524                    message = parseAxolotlChat(axolotlEncrypted, origin, conversation, status, checkedForDuplicates, query != null);
525                } else {
526                    Message trial = null;
527                    for (Jid fallback : fallbacksBySourceId) {
528                        trial = parseAxolotlChat(axolotlEncrypted, fallback, conversation, status, checkedForDuplicates && fallbacksBySourceId.size() == 1, query != null);
529                        if (trial != null) {
530                            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": decoded muc message using fallback");
531                            origin = fallback;
532                            break;
533                        }
534                    }
535                    message = trial;
536                }
537                if (message == null) {
538                    if (query == null && extractChatState(mXmppConnectionService.find(account, counterpart.asBareJid()), isTypeGroupChat, packet)) {
539                        mXmppConnectionService.updateConversationUi();
540                    }
541                    if (query != null && status == Message.STATUS_SEND && remoteMsgId != null) {
542                        Message previouslySent = conversation.findSentMessageWithUuid(remoteMsgId);
543                        if (previouslySent != null && previouslySent.getServerMsgId() == null && serverMsgId != null) {
544                            previouslySent.setServerMsgId(serverMsgId);
545                            mXmppConnectionService.databaseBackend.updateMessage(previouslySent, false);
546                            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": encountered previously sent OMEMO message without serverId. updating...");
547                        }
548                    }
549                    return;
550                }
551                if (conversationMultiMode) {
552                    message.setTrueCounterpart(origin);
553                }
554            } else if (body == null && oobUrl != null) {
555                message = new Message(conversation, oobUrl, Message.ENCRYPTION_NONE, status);
556                message.setOob(true);
557                if (CryptoHelper.isPgpEncryptedUrl(oobUrl)) {
558                    message.setEncryption(Message.ENCRYPTION_DECRYPTED);
559                }
560            } else {
561                message = new Message(conversation, body.content, Message.ENCRYPTION_NONE, status);
562                if (body.count > 1) {
563                    message.setBodyLanguage(body.language);
564                }
565            }
566
567            message.setCounterpart(counterpart);
568            message.setRemoteMsgId(remoteMsgId);
569            message.setServerMsgId(serverMsgId);
570            message.setCarbon(isCarbon);
571            message.setTime(timestamp);
572            if (body != null && body.content != null && body.content.equals(oobUrl)) {
573                message.setOob(true);
574                if (CryptoHelper.isPgpEncryptedUrl(oobUrl)) {
575                    message.setEncryption(Message.ENCRYPTION_DECRYPTED);
576                }
577            }
578            message.markable = packet.hasChild("markable", "urn:xmpp:chat-markers:0");
579            if (conversationMultiMode) {
580                message.setMucUser(conversation.getMucOptions().findUserByFullJid(counterpart));
581                final Jid fallback = conversation.getMucOptions().getTrueCounterpart(counterpart);
582                Jid trueCounterpart;
583                if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL) {
584                    trueCounterpart = message.getTrueCounterpart();
585                } else if (query != null && query.safeToExtractTrueCounterpart()) {
586                    trueCounterpart = getTrueCounterpart(mucUserElement, fallback);
587                } else {
588                    trueCounterpart = fallback;
589                }
590                if (trueCounterpart != null && isTypeGroupChat) {
591                    if (trueCounterpart.asBareJid().equals(account.getJid().asBareJid())) {
592                        status = isTypeGroupChat ? Message.STATUS_SEND_RECEIVED : Message.STATUS_SEND;
593                    } else {
594                        status = Message.STATUS_RECEIVED;
595                        message.setCarbon(false);
596                    }
597                }
598                message.setStatus(status);
599                message.setTrueCounterpart(trueCounterpart);
600                if (!isTypeGroupChat) {
601                    message.setType(Message.TYPE_PRIVATE);
602                }
603            } else {
604                updateLastseen(account, from);
605            }
606
607            if (replacementId != null && mXmppConnectionService.allowMessageCorrection()) {
608                final Message replacedMessage = conversation.findMessageWithRemoteIdAndCounterpart(replacementId,
609                        counterpart,
610                        message.getStatus() == Message.STATUS_RECEIVED,
611                        message.isCarbon());
612                if (replacedMessage != null) {
613                    final boolean fingerprintsMatch = replacedMessage.getFingerprint() == null
614                            || replacedMessage.getFingerprint().equals(message.getFingerprint());
615                    final boolean trueCountersMatch = replacedMessage.getTrueCounterpart() != null
616                            && message.getTrueCounterpart() != null
617                            && replacedMessage.getTrueCounterpart().asBareJid().equals(message.getTrueCounterpart().asBareJid());
618                    final boolean mucUserMatches = query == null && replacedMessage.sameMucUser(message); //can not be checked when using mam
619                    final boolean duplicate = conversation.hasDuplicateMessage(message);
620                    if (fingerprintsMatch && (trueCountersMatch || !conversationMultiMode || mucUserMatches) && !duplicate) {
621                        Log.d(Config.LOGTAG, "replaced message '" + replacedMessage.getBody() + "' with '" + message.getBody() + "'");
622                        synchronized (replacedMessage) {
623                            final String uuid = replacedMessage.getUuid();
624                            replacedMessage.setUuid(UUID.randomUUID().toString());
625                            replacedMessage.setBody(message.getBody());
626                            replacedMessage.putEdited(replacedMessage.getRemoteMsgId(), replacedMessage.getServerMsgId());
627                            replacedMessage.setRemoteMsgId(remoteMsgId);
628                            if (replacedMessage.getServerMsgId() == null || message.getServerMsgId() != null) {
629                                replacedMessage.setServerMsgId(message.getServerMsgId());
630                            }
631                            replacedMessage.setEncryption(message.getEncryption());
632                            if (replacedMessage.getStatus() == Message.STATUS_RECEIVED) {
633                                replacedMessage.markUnread();
634                            }
635                            extractChatState(mXmppConnectionService.find(account, counterpart.asBareJid()), isTypeGroupChat, packet);
636                            mXmppConnectionService.updateMessage(replacedMessage, uuid);
637                            if (mXmppConnectionService.confirmMessages()
638                                    && replacedMessage.getStatus() == Message.STATUS_RECEIVED
639                                    && (replacedMessage.trusted() || replacedMessage.isPrivateMessage()) //TODO do we really want to send receipts for all PMs?
640                                    && remoteMsgId != null
641                                    && !selfAddressed
642                                    && !isTypeGroupChat) {
643                                processMessageReceipts(account, packet, query);
644                            }
645                            if (replacedMessage.getEncryption() == Message.ENCRYPTION_PGP) {
646                                conversation.getAccount().getPgpDecryptionService().discard(replacedMessage);
647                                conversation.getAccount().getPgpDecryptionService().decrypt(replacedMessage, false);
648                            }
649                        }
650                        mXmppConnectionService.getNotificationService().updateNotification();
651                        return;
652                    } else {
653                        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": received message correction but verification didn't check out");
654                    }
655                }
656            }
657
658            long deletionDate = mXmppConnectionService.getAutomaticMessageDeletionDate();
659            if (deletionDate != 0 && message.getTimeSent() < deletionDate) {
660                Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": skipping message from " + message.getCounterpart().toString() + " because it was sent prior to our deletion date");
661                return;
662            }
663
664            boolean checkForDuplicates = (isTypeGroupChat && packet.hasChild("delay", "urn:xmpp:delay"))
665                    || message.isPrivateMessage()
666                    || message.getServerMsgId() != null
667                    || (query == null && mXmppConnectionService.getMessageArchiveService().isCatchupInProgress(conversation));
668            if (checkForDuplicates) {
669                final Message duplicate = conversation.findDuplicateMessage(message);
670                if (duplicate != null) {
671                    final boolean serverMsgIdUpdated;
672                    if (duplicate.getStatus() != Message.STATUS_RECEIVED
673                            && duplicate.getUuid().equals(message.getRemoteMsgId())
674                            && duplicate.getServerMsgId() == null
675                            && message.getServerMsgId() != null) {
676                        duplicate.setServerMsgId(message.getServerMsgId());
677                        if (mXmppConnectionService.databaseBackend.updateMessage(duplicate, false)) {
678                            serverMsgIdUpdated = true;
679                        } else {
680                            serverMsgIdUpdated = false;
681                            Log.e(Config.LOGTAG, "failed to update message");
682                        }
683                    } else {
684                        serverMsgIdUpdated = false;
685                    }
686                    Log.d(Config.LOGTAG, "skipping duplicate message with " + message.getCounterpart() + ". serverMsgIdUpdated=" + serverMsgIdUpdated);
687                    return;
688                }
689            }
690
691            if (query != null && query.getPagingOrder() == MessageArchiveService.PagingOrder.REVERSE) {
692                conversation.prepend(query.getActualInThisQuery(), message);
693            } else {
694                conversation.add(message);
695            }
696            if (query != null) {
697                query.incrementActualMessageCount();
698            }
699
700            if (query == null || query.isCatchup()) { //either no mam or catchup
701                if (status == Message.STATUS_SEND || status == Message.STATUS_SEND_RECEIVED) {
702                    mXmppConnectionService.markRead(conversation);
703                    if (query == null) {
704                        activateGracePeriod(account);
705                    }
706                } else {
707                    message.markUnread();
708                    notify = true;
709                }
710            }
711
712            if (message.getEncryption() == Message.ENCRYPTION_PGP) {
713                notify = conversation.getAccount().getPgpDecryptionService().decrypt(message, notify);
714            } else if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL_NOT_FOR_THIS_DEVICE || message.getEncryption() == Message.ENCRYPTION_AXOLOTL_FAILED) {
715                notify = false;
716            }
717
718            if (query == null) {
719                extractChatState(mXmppConnectionService.find(account, counterpart.asBareJid()), isTypeGroupChat, packet);
720                mXmppConnectionService.updateConversationUi();
721            }
722
723            if (mXmppConnectionService.confirmMessages()
724                    && message.getStatus() == Message.STATUS_RECEIVED
725                    && (message.trusted() || message.isPrivateMessage())
726                    && remoteMsgId != null
727                    && !selfAddressed
728                    && !isTypeGroupChat) {
729                processMessageReceipts(account, packet, query);
730            }
731
732            mXmppConnectionService.databaseBackend.createMessage(message);
733            final HttpConnectionManager manager = this.mXmppConnectionService.getHttpConnectionManager();
734            if (message.trusted() && message.treatAsDownloadable() && manager.getAutoAcceptFileSize() > 0) {
735                manager.createNewDownloadConnection(message);
736            } else if (notify) {
737                if (query != null && query.isCatchup()) {
738                    mXmppConnectionService.getNotificationService().pushFromBacklog(message);
739                } else {
740                    mXmppConnectionService.getNotificationService().push(message);
741                }
742            }
743        } else if (!packet.hasChild("body")) { //no body
744
745            final Conversation conversation = mXmppConnectionService.find(account, from.asBareJid());
746            if (axolotlEncrypted != null) {
747                Jid origin;
748                if (conversation != null && conversation.getMode() == Conversation.MODE_MULTI) {
749                    final Jid fallback = conversation.getMucOptions().getTrueCounterpart(counterpart);
750                    origin = getTrueCounterpart(query != null ? mucUserElement : null, fallback);
751                    if (origin == null) {
752                        Log.d(Config.LOGTAG, "omemo key transport message in anonymous conference received");
753                        return;
754                    }
755                } else if (isTypeGroupChat) {
756                    return;
757                } else {
758                    origin = from;
759                }
760                try {
761                    final XmppAxolotlMessage xmppAxolotlMessage = XmppAxolotlMessage.fromElement(axolotlEncrypted, origin.asBareJid());
762                    account.getAxolotlService().processReceivingKeyTransportMessage(xmppAxolotlMessage, query != null);
763                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": omemo key transport message received from " + origin);
764                } catch (Exception e) {
765                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": invalid omemo key transport message received " + e.getMessage());
766                    return;
767                }
768            }
769
770            if (query == null && extractChatState(mXmppConnectionService.find(account, counterpart.asBareJid()), isTypeGroupChat, packet)) {
771                mXmppConnectionService.updateConversationUi();
772            }
773
774            if (isTypeGroupChat) {
775                if (packet.hasChild("subject")) { //TODO usually we would want to check for lack of body; however some servers do set a body :(
776                    if (conversation != null && conversation.getMode() == Conversation.MODE_MULTI) {
777                        conversation.setHasMessagesLeftOnServer(conversation.countMessages() > 0);
778                        final LocalizedContent subject = packet.findInternationalizedChildContentInDefaultNamespace("subject");
779                        if (subject != null && conversation.getMucOptions().setSubject(subject.content)) {
780                            mXmppConnectionService.updateConversation(conversation);
781                        }
782                        mXmppConnectionService.updateConversationUi();
783                        return;
784                    }
785                }
786            }
787            if (conversation != null && mucUserElement != null && InvalidJid.hasValidFrom(packet) && from.isBareJid()) {
788                for (Element child : mucUserElement.getChildren()) {
789                    if ("status".equals(child.getName())) {
790                        try {
791                            int code = Integer.parseInt(child.getAttribute("code"));
792                            if ((code >= 170 && code <= 174) || (code >= 102 && code <= 104)) {
793                                mXmppConnectionService.fetchConferenceConfiguration(conversation);
794                                break;
795                            }
796                        } catch (Exception e) {
797                            //ignored
798                        }
799                    } else if ("item".equals(child.getName())) {
800                        MucOptions.User user = AbstractParser.parseItem(conversation, child);
801                        Log.d(Config.LOGTAG, account.getJid() + ": changing affiliation for "
802                                + user.getRealJid() + " to " + user.getAffiliation() + " in "
803                                + conversation.getJid().asBareJid());
804                        if (!user.realJidMatchesAccount()) {
805                            boolean isNew = conversation.getMucOptions().updateUser(user);
806                            mXmppConnectionService.getAvatarService().clear(conversation);
807                            mXmppConnectionService.updateMucRosterUi();
808                            mXmppConnectionService.updateConversationUi();
809                            Contact contact = user.getContact();
810                            if (!user.getAffiliation().ranks(MucOptions.Affiliation.MEMBER)) {
811                                Jid jid = user.getRealJid();
812                                List<Jid> cryptoTargets = conversation.getAcceptedCryptoTargets();
813                                if (cryptoTargets.remove(user.getRealJid())) {
814                                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": removed " + jid + " from crypto targets of " + conversation.getName());
815                                    conversation.setAcceptedCryptoTargets(cryptoTargets);
816                                    mXmppConnectionService.updateConversation(conversation);
817                                }
818                            } else if (isNew
819                                    && user.getRealJid() != null
820                                    && conversation.getMucOptions().isPrivateAndNonAnonymous()
821                                    && (contact == null || !contact.mutualPresenceSubscription())
822                                    && account.getAxolotlService().hasEmptyDeviceList(user.getRealJid())) {
823                                account.getAxolotlService().fetchDeviceIds(user.getRealJid());
824                            }
825                        }
826                    }
827                }
828            }
829            if (!isTypeGroupChat) {
830                for (Element child : packet.getChildren()) {
831                    if (Namespace.JINGLE_MESSAGE.equals(child.getNamespace()) && JINGLE_MESSAGE_ELEMENT_NAMES.contains(child.getName())) {
832                        if (!account.getJid().asBareJid().equals(from.asBareJid())) {
833                            processMessageReceipts(account, packet, query);
834                        }
835                        mXmppConnectionService.getJingleConnectionManager().deliverMessage(account, packet.getTo(), packet.getFrom(), child);
836                        break;
837                    }
838                }
839            }
840        }
841
842        Element received = packet.findChild("received", "urn:xmpp:chat-markers:0");
843        if (received == null) {
844            received = packet.findChild("received", "urn:xmpp:receipts");
845        }
846        if (received != null) {
847            String id = received.getAttribute("id");
848            if (packet.fromAccount(account)) {
849                if (query != null && id != null && packet.getTo() != null) {
850                    query.removePendingReceiptRequest(new ReceiptRequest(packet.getTo(), id));
851                }
852            } else if (id != null) {
853                if (id.startsWith(JingleRtpConnection.JINGLE_MESSAGE_PROPOSE_ID_PREFIX)) {
854                    final String sessionId = id.substring(JingleRtpConnection.JINGLE_MESSAGE_PROPOSE_ID_PREFIX.length());
855                    mXmppConnectionService.getJingleConnectionManager()
856                            .updateProposedSessionDiscovered(account, from, sessionId, JingleConnectionManager.DeviceDiscoveryState.DISCOVERED);
857                } else {
858                    mXmppConnectionService.markMessage(account, from.asBareJid(), id, Message.STATUS_SEND_RECEIVED);
859                }
860            }
861        }
862        Element displayed = packet.findChild("displayed", "urn:xmpp:chat-markers:0");
863        if (displayed != null) {
864            final String id = displayed.getAttribute("id");
865            final Jid sender = InvalidJid.getNullForInvalid(displayed.getAttributeAsJid("sender"));
866            if (packet.fromAccount(account) && !selfAddressed) {
867                dismissNotification(account, counterpart, query);
868                if (query == null) {
869                    activateGracePeriod(account);
870                }
871            } else if (isTypeGroupChat) {
872                Conversation conversation = mXmppConnectionService.find(account, counterpart.asBareJid());
873                if (conversation != null && id != null && sender != null) {
874                    Message message = conversation.findMessageWithRemoteId(id, sender);
875                    if (message != null) {
876                        final Jid fallback = conversation.getMucOptions().getTrueCounterpart(counterpart);
877                        final Jid trueJid = getTrueCounterpart((query != null && query.safeToExtractTrueCounterpart()) ? mucUserElement : null, fallback);
878                        final boolean trueJidMatchesAccount = account.getJid().asBareJid().equals(trueJid == null ? null : trueJid.asBareJid());
879                        if (trueJidMatchesAccount || conversation.getMucOptions().isSelf(counterpart)) {
880                            if (!message.isRead() && (query == null || query.isCatchup())) { //checking if message is unread fixes race conditions with reflections
881                                mXmppConnectionService.markRead(conversation);
882                            }
883                        } else if (!counterpart.isBareJid() && trueJid != null) {
884                            final ReadByMarker readByMarker = ReadByMarker.from(counterpart, trueJid);
885                            if (message.addReadByMarker(readByMarker)) {
886                                mXmppConnectionService.updateMessage(message, false);
887                            }
888                        }
889                    }
890                }
891            } else {
892                final Message displayedMessage = mXmppConnectionService.markMessage(account, from.asBareJid(), id, Message.STATUS_SEND_DISPLAYED);
893                Message message = displayedMessage == null ? null : displayedMessage.prev();
894                while (message != null
895                        && message.getStatus() == Message.STATUS_SEND_RECEIVED
896                        && message.getTimeSent() < displayedMessage.getTimeSent()) {
897                    mXmppConnectionService.markMessage(message, Message.STATUS_SEND_DISPLAYED);
898                    message = message.prev();
899                }
900                if (displayedMessage != null && selfAddressed) {
901                    dismissNotification(account, counterpart, query);
902                }
903            }
904        }
905
906        final Element event = original.findChild("event", "http://jabber.org/protocol/pubsub#event");
907        if (event != null && InvalidJid.hasValidFrom(original)) {
908            if (event.hasChild("items")) {
909                parseEvent(event, original.getFrom(), account);
910            } else if (event.hasChild("delete")) {
911                parseDeleteEvent(event, original.getFrom(), account);
912            } else if (event.hasChild("purge")) {
913                parsePurgeEvent(event, original.getFrom(), account);
914            }
915        }
916
917        final String nick = packet.findChildContent("nick", Namespace.NICK);
918        if (nick != null && InvalidJid.hasValidFrom(original)) {
919            Contact contact = account.getRoster().getContact(from);
920            if (contact.setPresenceName(nick)) {
921                mXmppConnectionService.getAvatarService().clear(contact);
922            }
923        }
924    }
925
926    private void dismissNotification(Account account, Jid counterpart, MessageArchiveService.Query query) {
927        Conversation conversation = mXmppConnectionService.find(account, counterpart.asBareJid());
928        if (conversation != null && (query == null || query.isCatchup())) {
929            mXmppConnectionService.markRead(conversation); //TODO only mark messages read that are older than timestamp
930        }
931    }
932
933    private void processMessageReceipts(Account account, MessagePacket packet, MessageArchiveService.Query query) {
934        final boolean markable = packet.hasChild("markable", "urn:xmpp:chat-markers:0");
935        final boolean request = packet.hasChild("request", "urn:xmpp:receipts");
936        if (query == null) {
937            final ArrayList<String> receiptsNamespaces = new ArrayList<>();
938            if (markable) {
939                receiptsNamespaces.add("urn:xmpp:chat-markers:0");
940            }
941            if (request) {
942                receiptsNamespaces.add("urn:xmpp:receipts");
943            }
944            if (receiptsNamespaces.size() > 0) {
945                MessagePacket receipt = mXmppConnectionService.getMessageGenerator().received(account,
946                        packet,
947                        receiptsNamespaces,
948                        packet.getType());
949                mXmppConnectionService.sendMessagePacket(account, receipt);
950            }
951        } else if (query.isCatchup()) {
952            if (request) {
953                query.addPendingReceiptRequest(new ReceiptRequest(packet.getFrom(), packet.getId()));
954            }
955        }
956    }
957
958    private void activateGracePeriod(Account account) {
959        long duration = mXmppConnectionService.getLongPreference("grace_period_length", R.integer.grace_period) * 1000;
960        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": activating grace period till " + TIME_FORMAT.format(new Date(System.currentTimeMillis() + duration)));
961        account.activateGracePeriod(duration);
962    }
963
964    private class Invite {
965        final Jid jid;
966        final String password;
967        final boolean direct;
968        final Jid inviter;
969
970        Invite(Jid jid, String password, boolean direct, Jid inviter) {
971            this.jid = jid;
972            this.password = password;
973            this.direct = direct;
974            this.inviter = inviter;
975        }
976
977        public boolean execute(Account account) {
978            if (jid != null) {
979                Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, jid, true, false);
980                if (conversation.getMucOptions().online()) {
981                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": received invite to " + jid + " but muc is considered to be online");
982                    mXmppConnectionService.mucSelfPingAndRejoin(conversation);
983                } else {
984                    conversation.getMucOptions().setPassword(password);
985                    mXmppConnectionService.databaseBackend.updateConversation(conversation);
986                    final Contact contact = inviter != null ? account.getRoster().getContactFromContactList(inviter) : null;
987                    mXmppConnectionService.joinMuc(conversation, contact != null && contact.mutualPresenceSubscription());
988                    mXmppConnectionService.updateConversationUi();
989                }
990                return true;
991            }
992            return false;
993        }
994    }
995}