1package eu.siacs.conversations.parser;
2
3import android.net.Uri;
4import android.util.Log;
5import android.util.Pair;
6
7import com.cheogram.android.BobTransfer;
8import com.cheogram.android.WebxdcUpdate;
9
10import com.google.common.collect.ImmutableSet;
11
12import java.io.File;
13import java.net.URISyntaxException;
14import java.text.SimpleDateFormat;
15import java.util.ArrayList;
16import java.util.Arrays;
17import java.util.Collections;
18import java.util.Date;
19import java.util.LinkedHashSet;
20import java.util.List;
21import java.util.Locale;
22import java.util.Map;
23import java.util.Set;
24import java.util.UUID;
25import java.util.function.Consumer;
26
27import io.ipfs.cid.Cid;
28
29import eu.siacs.conversations.AppSettings;
30import eu.siacs.conversations.Config;
31import eu.siacs.conversations.R;
32import eu.siacs.conversations.crypto.axolotl.AxolotlService;
33import eu.siacs.conversations.crypto.axolotl.BrokenSessionException;
34import eu.siacs.conversations.crypto.axolotl.NotEncryptedForThisDeviceException;
35import eu.siacs.conversations.crypto.axolotl.OutdatedSenderException;
36import eu.siacs.conversations.crypto.axolotl.XmppAxolotlMessage;
37import eu.siacs.conversations.entities.Account;
38import eu.siacs.conversations.entities.Bookmark;
39import eu.siacs.conversations.entities.Contact;
40import eu.siacs.conversations.entities.Conversation;
41import eu.siacs.conversations.entities.Conversational;
42import eu.siacs.conversations.entities.DownloadableFile;
43import eu.siacs.conversations.entities.Message;
44import eu.siacs.conversations.entities.MucOptions;
45import eu.siacs.conversations.entities.ReadByMarker;
46import eu.siacs.conversations.entities.ReceiptRequest;
47import eu.siacs.conversations.entities.RtpSessionStatus;
48import eu.siacs.conversations.http.HttpConnectionManager;
49import eu.siacs.conversations.services.MessageArchiveService;
50import eu.siacs.conversations.services.QuickConversationsService;
51import eu.siacs.conversations.services.XmppConnectionService;
52import eu.siacs.conversations.utils.CryptoHelper;
53import eu.siacs.conversations.xml.Element;
54import eu.siacs.conversations.xml.LocalizedContent;
55import eu.siacs.conversations.xml.Namespace;
56import eu.siacs.conversations.xmpp.InvalidJid;
57import eu.siacs.conversations.xmpp.Jid;
58import eu.siacs.conversations.xmpp.OnMessagePacketReceived;
59import eu.siacs.conversations.xmpp.chatstate.ChatState;
60import eu.siacs.conversations.xmpp.forms.Data;
61import eu.siacs.conversations.xmpp.jingle.JingleConnectionManager;
62import eu.siacs.conversations.xmpp.jingle.JingleRtpConnection;
63import eu.siacs.conversations.xmpp.pep.Avatar;
64import im.conversations.android.xmpp.model.Extension;
65import im.conversations.android.xmpp.model.carbons.Received;
66import im.conversations.android.xmpp.model.carbons.Sent;
67import im.conversations.android.xmpp.model.forward.Forwarded;
68
69public class MessageParser extends AbstractParser implements Consumer<im.conversations.android.xmpp.model.stanza.Message> {
70
71 private static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm:ss", Locale.ENGLISH);
72
73 private static final List<String> JINGLE_MESSAGE_ELEMENT_NAMES =
74 Arrays.asList("accept", "propose", "proceed", "reject", "retract", "ringing", "finish");
75
76 public MessageParser(final XmppConnectionService service, final Account account) {
77 super(service, account);
78 }
79
80 private static String extractStanzaId(Element packet, boolean isTypeGroupChat, Conversation conversation) {
81 final Jid by;
82 final boolean safeToExtract;
83 if (isTypeGroupChat) {
84 by = conversation.getJid().asBareJid();
85 safeToExtract = conversation.getMucOptions().hasFeature(Namespace.STANZA_IDS);
86 } else {
87 Account account = conversation.getAccount();
88 by = account.getJid().asBareJid();
89 safeToExtract = account.getXmppConnection().getFeatures().stanzaIds();
90 }
91 return safeToExtract ? extractStanzaId(packet, by) : null;
92 }
93
94 private static String extractStanzaId(Account account, Element packet) {
95 final boolean safeToExtract = account.getXmppConnection().getFeatures().stanzaIds();
96 return safeToExtract ? extractStanzaId(packet, account.getJid().asBareJid()) : null;
97 }
98
99 private static String extractStanzaId(Element packet, Jid by) {
100 for (Element child : packet.getChildren()) {
101 if (child.getName().equals("stanza-id")
102 && Namespace.STANZA_IDS.equals(child.getNamespace())
103 && by.equals(InvalidJid.getNullForInvalid(child.getAttributeAsJid("by")))) {
104 return child.getAttribute("id");
105 }
106 }
107 return null;
108 }
109
110 private static Jid getTrueCounterpart(Element mucUserElement, Jid fallback) {
111 final Element item = mucUserElement == null ? null : mucUserElement.findChild("item");
112 Jid result = item == null ? null : InvalidJid.getNullForInvalid(item.getAttributeAsJid("jid"));
113 return result != null ? result : fallback;
114 }
115
116 private boolean extractChatState(Conversation c, final boolean isTypeGroupChat, final im.conversations.android.xmpp.model.stanza.Message packet) {
117 ChatState state = ChatState.parse(packet);
118 if (state != null && c != null) {
119 final Account account = c.getAccount();
120 final Jid from = packet.getFrom();
121 if (from.asBareJid().equals(account.getJid().asBareJid())) {
122 c.setOutgoingChatState(state);
123 if (state == ChatState.ACTIVE || state == ChatState.COMPOSING) {
124 if (c.getContact().isSelf()) {
125 return false;
126 }
127 mXmppConnectionService.markRead(c);
128 activateGracePeriod(account);
129 }
130 return false;
131 } else {
132 if (isTypeGroupChat) {
133 MucOptions.User user = c.getMucOptions().findUserByFullJid(from);
134 if (user != null) {
135 return user.setChatState(state);
136 } else {
137 return false;
138 }
139 } else {
140 return c.setIncomingChatState(state);
141 }
142 }
143 }
144 return false;
145 }
146
147 private Message parseAxolotlChat(Element axolotlMessage, Jid from, Conversation conversation, int status, final boolean checkedForDuplicates, boolean postpone) {
148 final AxolotlService service = conversation.getAccount().getAxolotlService();
149 final XmppAxolotlMessage xmppAxolotlMessage;
150 try {
151 xmppAxolotlMessage = XmppAxolotlMessage.fromElement(axolotlMessage, from.asBareJid());
152 } catch (Exception e) {
153 Log.d(Config.LOGTAG, conversation.getAccount().getJid().asBareJid() + ": invalid omemo message received " + e.getMessage());
154 return null;
155 }
156 if (xmppAxolotlMessage.hasPayload()) {
157 final XmppAxolotlMessage.XmppAxolotlPlaintextMessage plaintextMessage;
158 try {
159 plaintextMessage = service.processReceivingPayloadMessage(xmppAxolotlMessage, postpone);
160 } catch (BrokenSessionException e) {
161 if (checkedForDuplicates) {
162 if (service.trustedOrPreviouslyResponded(from.asBareJid())) {
163 service.reportBrokenSessionException(e, postpone);
164 return new Message(conversation, "", Message.ENCRYPTION_AXOLOTL_FAILED, status);
165 } else {
166 Log.d(Config.LOGTAG, "ignoring broken session exception because contact was not trusted");
167 return new Message(conversation, "", Message.ENCRYPTION_AXOLOTL_FAILED, status);
168 }
169 } else {
170 Log.d(Config.LOGTAG, "ignoring broken session exception because checkForDuplicates failed");
171 return null;
172 }
173 } catch (NotEncryptedForThisDeviceException e) {
174 return new Message(conversation, "", Message.ENCRYPTION_AXOLOTL_NOT_FOR_THIS_DEVICE, status);
175 } catch (OutdatedSenderException e) {
176 return new Message(conversation, "", Message.ENCRYPTION_AXOLOTL_FAILED, status);
177 }
178 if (plaintextMessage != null) {
179 Message finishedMessage = new Message(conversation, plaintextMessage.getPlaintext(), Message.ENCRYPTION_AXOLOTL, status);
180 finishedMessage.setFingerprint(plaintextMessage.getFingerprint());
181 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(finishedMessage.getConversation().getAccount()) + " Received Message with session fingerprint: " + plaintextMessage.getFingerprint());
182 return finishedMessage;
183 }
184 } else {
185 Log.d(Config.LOGTAG, conversation.getAccount().getJid().asBareJid() + ": received OMEMO key transport message");
186 service.processReceivingKeyTransportMessage(xmppAxolotlMessage, postpone);
187 }
188 return null;
189 }
190
191 private Invite extractInvite(final Element message) {
192 final Element mucUser = message.findChild("x", Namespace.MUC_USER);
193 if (mucUser != null) {
194 final Element invite = mucUser.findChild("invite");
195 if (invite != null) {
196 final String password = mucUser.findChildContent("password");
197 final Jid from = InvalidJid.getNullForInvalid(invite.getAttributeAsJid("from"));
198 final Jid to = InvalidJid.getNullForInvalid(invite.getAttributeAsJid("to"));
199 if (to != null && from == null) {
200 Log.d(Config.LOGTAG,"do not parse outgoing mediated invite "+message);
201 return null;
202 }
203 final Jid room = InvalidJid.getNullForInvalid(message.getAttributeAsJid("from"));
204 if (room == null) {
205 return null;
206 }
207 return new Invite(room, password, false, from);
208 }
209 }
210 final Element conference = message.findChild("x", "jabber:x:conference");
211 if (conference != null) {
212 Jid from = InvalidJid.getNullForInvalid(message.getAttributeAsJid("from"));
213 Jid room = InvalidJid.getNullForInvalid(conference.getAttributeAsJid("jid"));
214 if (room == null) {
215 return null;
216 }
217 return new Invite(room, conference.getAttribute("password"), true, from);
218 }
219 return null;
220 }
221
222 private void parseEvent(final Element event, final Jid from, final Account account) {
223 final Element items = event.findChild("items");
224 final String node = items == null ? null : items.getAttribute("node");
225 if ("urn:xmpp:avatar:metadata".equals(node)) {
226 Avatar avatar = Avatar.parseMetadata(items);
227 if (avatar != null) {
228 avatar.owner = from.asBareJid();
229 if (mXmppConnectionService.getFileBackend().isAvatarCached(avatar)) {
230 if (account.getJid().asBareJid().equals(from)) {
231 if (account.setAvatar(avatar.getFilename())) {
232 mXmppConnectionService.databaseBackend.updateAccount(account);
233 mXmppConnectionService.notifyAccountAvatarHasChanged(account);
234 }
235 mXmppConnectionService.getAvatarService().clear(account);
236 mXmppConnectionService.updateConversationUi();
237 mXmppConnectionService.updateAccountUi();
238 } else {
239 final Contact contact = account.getRoster().getContact(from);
240 contact.setAvatar(avatar);
241 mXmppConnectionService.syncRoster(account);
242 mXmppConnectionService.getAvatarService().clear(contact);
243 mXmppConnectionService.updateConversationUi();
244 mXmppConnectionService.updateRosterUi(XmppConnectionService.UpdateRosterReason.AVATAR);
245 }
246 } else if (mXmppConnectionService.isDataSaverDisabled()) {
247 mXmppConnectionService.fetchAvatar(account, avatar);
248 }
249 }
250 } else if (Namespace.NICK.equals(node)) {
251 final Element i = items.findChild("item");
252 final String nick = i == null ? null : i.findChildContent("nick", Namespace.NICK);
253 if (nick != null) {
254 setNick(account, from, nick);
255 }
256 } else if (AxolotlService.PEP_DEVICE_LIST.equals(node)) {
257 Element item = items.findChild("item");
258 final Set<Integer> deviceIds = IqParser.deviceIds(item);
259 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Received PEP device list " + deviceIds + " update from " + from + ", processing... ");
260 final AxolotlService axolotlService = account.getAxolotlService();
261 axolotlService.registerDevices(from, deviceIds);
262 } else if (Namespace.BOOKMARKS.equals(node) && account.getJid().asBareJid().equals(from)) {
263 final var connection = account.getXmppConnection();
264 if (connection.getFeatures().bookmarksConversion()) {
265 if (connection.getFeatures().bookmarks2()) {
266 Log.w(
267 Config.LOGTAG,
268 account.getJid().asBareJid()
269 + ": received storage:bookmark notification even though we opted into bookmarks:1");
270 }
271 final Element i = items.findChild("item");
272 final Element storage =
273 i == null ? null : i.findChild("storage", Namespace.BOOKMARKS);
274 final Map<Jid, Bookmark> bookmarks = Bookmark.parseFromStorage(storage, account);
275 mXmppConnectionService.processBookmarksInitial(account, bookmarks, true);
276 Log.d(
277 Config.LOGTAG,
278 account.getJid().asBareJid() + ": processing bookmark PEP event");
279 } else {
280 Log.d(
281 Config.LOGTAG,
282 account.getJid().asBareJid()
283 + ": ignoring bookmark PEP event because bookmark conversion was not detected");
284 }
285 } else if (Namespace.BOOKMARKS2.equals(node) && account.getJid().asBareJid().equals(from)) {
286 final Element item = items.findChild("item");
287 final Element retract = items.findChild("retract");
288 if (item != null) {
289 final Bookmark bookmark = Bookmark.parseFromItem(item, account);
290 if (bookmark != null) {
291 account.putBookmark(bookmark);
292 mXmppConnectionService.processModifiedBookmark(bookmark);
293 mXmppConnectionService.updateConversationUi();
294 }
295 }
296 if (retract != null) {
297 final Jid id = InvalidJid.getNullForInvalid(retract.getAttributeAsJid("id"));
298 if (id != null) {
299 account.removeBookmark(id);
300 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": deleted bookmark for " + id);
301 mXmppConnectionService.processDeletedBookmark(account, id);
302 mXmppConnectionService.updateConversationUi();
303 }
304 }
305 } else if (Config.MESSAGE_DISPLAYED_SYNCHRONIZATION
306 && Namespace.MDS_DISPLAYED.equals(node)
307 && account.getJid().asBareJid().equals(from)) {
308 final Element item = items.findChild("item");
309 mXmppConnectionService.processMdsItem(account, item);
310 } else {
311 Log.d(
312 Config.LOGTAG,
313 account.getJid().asBareJid()
314 + " received pubsub notification for node="
315 + node);
316 }
317 }
318
319 private void parseDeleteEvent(final Element event, final Jid from, final Account account) {
320 final Element delete = event.findChild("delete");
321 final String node = delete == null ? null : delete.getAttribute("node");
322 if (Namespace.NICK.equals(node)) {
323 Log.d(Config.LOGTAG, "parsing nick delete event from " + from);
324 setNick(account, from, null);
325 } else if (Namespace.BOOKMARKS2.equals(node) && account.getJid().asBareJid().equals(from)) {
326 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": deleted bookmarks node");
327 deleteAllBookmarks(account);
328 } else if (Namespace.AVATAR_METADATA.equals(node) && account.getJid().asBareJid().equals(from)) {
329 Log.d(Config.LOGTAG,account.getJid().asBareJid()+": deleted avatar metadata node");
330 }
331 }
332
333 private void parsePurgeEvent(final Element event, final Jid from, final Account account) {
334 final Element purge = event.findChild("purge");
335 final String node = purge == null ? null : purge.getAttribute("node");
336 if (Namespace.BOOKMARKS2.equals(node) && account.getJid().asBareJid().equals(from)) {
337 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": purged bookmarks");
338 deleteAllBookmarks(account);
339 }
340 }
341
342 private void deleteAllBookmarks(final Account account) {
343 final var previous = account.getBookmarkedJids();
344 account.setBookmarks(Collections.emptyMap());
345 mXmppConnectionService.processDeletedBookmarks(account, previous);
346 }
347
348 private void setNick(Account account, Jid user, String nick) {
349 if (user.asBareJid().equals(account.getJid().asBareJid())) {
350 account.setDisplayName(nick);
351 if (QuickConversationsService.isQuicksy()) {
352 mXmppConnectionService.getAvatarService().clear(account);
353 }
354 } else {
355 Contact contact = account.getRoster().getContact(user);
356 if (contact.setPresenceName(nick)) {
357 mXmppConnectionService.syncRoster(account);
358 mXmppConnectionService.getAvatarService().clear(contact);
359 }
360 }
361 mXmppConnectionService.updateConversationUi();
362 mXmppConnectionService.updateAccountUi();
363 }
364
365 private boolean handleErrorMessage(final Account account, final im.conversations.android.xmpp.model.stanza.Message packet) {
366 if (packet.getType() == im.conversations.android.xmpp.model.stanza.Message.Type.ERROR) {
367 if (packet.fromServer(account)) {
368 final var forwarded = getForwardedMessagePacket(packet,"received", Namespace.CARBONS);
369 if (forwarded != null) {
370 return handleErrorMessage(account, forwarded.first);
371 }
372 }
373 final Jid from = packet.getFrom();
374 final String id = packet.getId();
375 if (from != null && id != null) {
376 if (id.startsWith(JingleRtpConnection.JINGLE_MESSAGE_PROPOSE_ID_PREFIX)) {
377 final String sessionId = id.substring(JingleRtpConnection.JINGLE_MESSAGE_PROPOSE_ID_PREFIX.length());
378 mXmppConnectionService.getJingleConnectionManager()
379 .updateProposedSessionDiscovered(account, from, sessionId, JingleConnectionManager.DeviceDiscoveryState.FAILED);
380 return true;
381 }
382 if (id.startsWith(JingleRtpConnection.JINGLE_MESSAGE_PROCEED_ID_PREFIX)) {
383 final String sessionId = id.substring(JingleRtpConnection.JINGLE_MESSAGE_PROCEED_ID_PREFIX.length());
384 final String message = extractErrorMessage(packet);
385 mXmppConnectionService.getJingleConnectionManager().failProceed(account, from, sessionId, message);
386 return true;
387 }
388 mXmppConnectionService.markMessage(account,
389 from.asBareJid(),
390 id,
391 Message.STATUS_SEND_FAILED,
392 extractErrorMessage(packet));
393 final Element error = packet.findChild("error");
394 final boolean pingWorthyError = error != null && (error.hasChild("not-acceptable") || error.hasChild("remote-server-timeout") || error.hasChild("remote-server-not-found"));
395 if (pingWorthyError) {
396 Conversation conversation = mXmppConnectionService.find(account, from);
397 if (conversation != null && conversation.getMode() == Conversational.MODE_MULTI) {
398 if (conversation.getMucOptions().online()) {
399 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": received ping worthy error for seemingly online muc at " + from);
400 mXmppConnectionService.mucSelfPingAndRejoin(conversation);
401 }
402 }
403 }
404 }
405 return true;
406 }
407 return false;
408 }
409
410 @Override
411 public void accept(final im.conversations.android.xmpp.model.stanza.Message original) {
412 if (handleErrorMessage(account, original)) {
413 return;
414 }
415 final im.conversations.android.xmpp.model.stanza.Message packet;
416 Long timestamp = null;
417 boolean isCarbon = false;
418 String serverMsgId = null;
419 final Element fin = original.findChild("fin", MessageArchiveService.Version.MAM_0.namespace);
420 if (fin != null) {
421 mXmppConnectionService.getMessageArchiveService().processFinLegacy(fin, original.getFrom());
422 return;
423 }
424 final Element result = MessageArchiveService.Version.findResult(original);
425 final String queryId = result == null ? null : result.getAttribute("queryid");
426 final MessageArchiveService.Query query = queryId == null ? null : mXmppConnectionService.getMessageArchiveService().findQuery(queryId);
427 final boolean offlineMessagesRetrieved = account.getXmppConnection().isOfflineMessagesRetrieved();
428 if (query != null && query.validFrom(original.getFrom())) {
429 final var f = getForwardedMessagePacket(original,"result", query.version.namespace);
430 if (f == null) {
431 return;
432 }
433 timestamp = f.second;
434 packet = f.first;
435 serverMsgId = result.getAttribute("id");
436 query.incrementMessageCount();
437 if (handleErrorMessage(account, packet)) {
438 return;
439 }
440 final var contact = packet.getFrom() == null || packet.getFrom() instanceof InvalidJid ? null : account.getRoster().getContact(packet.getFrom());
441 if (contact != null && contact.isBlocked()) {
442 Log.d(Config.LOGTAG, "Got MAM result from blocked contact, ignoring...");
443 return;
444 }
445 } else if (query != null) {
446 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": received mam result with invalid from (" + original.getFrom() + ") or queryId (" + queryId + ")");
447 return;
448 } else if (original.fromServer(account)) {
449 Pair<im.conversations.android.xmpp.model.stanza.Message, Long> f;
450 f = getForwardedMessagePacket(original, Received.class);
451 f = f == null ? getForwardedMessagePacket(original, Sent.class) : f;
452 packet = f != null ? f.first : original;
453 if (handleErrorMessage(account, packet)) {
454 return;
455 }
456 timestamp = f != null ? f.second : null;
457 isCarbon = f != null;
458 } else {
459 packet = original;
460 }
461
462 if (timestamp == null) {
463 timestamp = AbstractParser.parseTimestamp(original, AbstractParser.parseTimestamp(packet));
464 }
465 final Element mucUserElement = packet.findChild("x", Namespace.MUC_USER);
466 final String pgpEncrypted = packet.findChildContent("x", "jabber:x:encrypted");
467 Element replaceElement = packet.findChild("replace", "urn:xmpp:message-correct:0");
468 Set<Message.FileParams> attachments = new LinkedHashSet<>();
469 for (Element child : packet.getChildren()) {
470 // SIMS first so they get preference in the set
471 if (child.getName().equals("reference") && child.getNamespace().equals("urn:xmpp:reference:0")) {
472 if (child.findChild("media-sharing", "urn:xmpp:sims:1") != null) {
473 attachments.add(new Message.FileParams(child));
474 }
475 }
476 }
477 for (Element child : packet.getChildren()) {
478 if (child.getName().equals("x") && child.getNamespace().equals(Namespace.OOB)) {
479 attachments.add(new Message.FileParams(child));
480 }
481 }
482 String replacementId = replaceElement == null ? null : replaceElement.getAttribute("id");
483 if (replacementId == null) {
484 final Element fasten = packet.findChild("apply-to", "urn:xmpp:fasten:0");
485 if (fasten != null) {
486 replaceElement = fasten.findChild("retract", "urn:xmpp:message-retract:0");
487 if (replaceElement == null) replaceElement = fasten.findChild("moderated", "urn:xmpp:message-moderate:0");
488 }
489 if (replaceElement == null) replaceElement = packet.findChild("retract", "urn:xmpp:message-retract:1");
490 if (replaceElement == null) replaceElement = packet.findChild("moderate", "urn:xmpp:message-moderate:1");
491 if (replaceElement != null) {
492 var reason = replaceElement.findChildContent("reason", "urn:xmpp:message-moderate:0");
493 if (reason == null) reason = replaceElement.findChildContent("reason", "urn:xmpp:message-moderate:1");
494 replacementId = (fasten == null ? replaceElement : fasten).getAttribute("id");
495 packet.setBody(reason == null ? "" : reason);
496 }
497 }
498 LocalizedContent body = packet.getBody();
499
500 final Element axolotlEncrypted = packet.findChildEnsureSingle(XmppAxolotlMessage.CONTAINERTAG, AxolotlService.PEP_PREFIX);
501 int status;
502 final Jid counterpart;
503 final Jid to = packet.getTo();
504 final Jid from = packet.getFrom();
505 final Element originId = packet.findChild("origin-id", Namespace.STANZA_IDS);
506 final String remoteMsgId;
507 if (originId != null && originId.getAttribute("id") != null) {
508 remoteMsgId = originId.getAttribute("id");
509 } else {
510 remoteMsgId = packet.getId();
511 }
512 boolean notify = false;
513
514 Element html = packet.findChild("html", "http://jabber.org/protocol/xhtml-im");
515 if (html != null && html.findChild("body", "http://www.w3.org/1999/xhtml") == null) {
516 html = null;
517 }
518
519 if (from == null || !InvalidJid.isValid(from) || !InvalidJid.isValid(to)) {
520 Log.e(Config.LOGTAG, "encountered invalid message from='" + from + "' to='" + to + "'");
521 return;
522 }
523
524 boolean isTypeGroupChat = packet.getType() == im.conversations.android.xmpp.model.stanza.Message.Type.GROUPCHAT;
525 if (query != null && !query.muc() && isTypeGroupChat) {
526 Log.e(Config.LOGTAG, account.getJid().asBareJid() + ": received groupchat (" + from + ") message on regular MAM request. skipping");
527 return;
528 }
529 boolean isMucStatusMessage = InvalidJid.hasValidFrom(packet) && from.isBareJid() && mucUserElement != null && mucUserElement.hasChild("status");
530 boolean selfAddressed;
531 if (packet.fromAccount(account)) {
532 status = Message.STATUS_SEND;
533 selfAddressed = to == null || account.getJid().asBareJid().equals(to.asBareJid());
534 if (selfAddressed) {
535 counterpart = from;
536 } else {
537 counterpart = to != null ? to : account.getJid();
538 }
539 } else {
540 status = Message.STATUS_RECEIVED;
541 counterpart = from;
542 selfAddressed = false;
543 }
544
545 final Invite invite = extractInvite(packet);
546 if (invite != null) {
547 if (invite.jid.asBareJid().equals(account.getJid().asBareJid())) {
548 Log.d(Config.LOGTAG,account.getJid().asBareJid()+": ignore invite to "+invite.jid+" because it matches account");
549 } else if (isTypeGroupChat) {
550 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": ignoring invite to " + invite.jid + " because it was received as group chat");
551 } else if (invite.direct && (mucUserElement != null || invite.inviter == null || mXmppConnectionService.isMuc(account, invite.inviter))) {
552 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": ignoring direct invite to " + invite.jid + " because it was received in MUC");
553 } else {
554 invite.execute(account);
555 return;
556 }
557 }
558
559 final Element reactions = packet.findChild("reactions", "urn:xmpp:reactions:0");
560 if (body == null && html == null) {
561 if (reactions != null && reactions.getAttribute("id") != null) {
562 final Conversation conversation = mXmppConnectionService.find(account, counterpart.asBareJid());
563 if (conversation != null) {
564 final Message reactionTo = conversation.findMessageWithRemoteIdAndCounterpart(reactions.getAttribute("id"), null);
565 if (reactionTo != null) {
566 String bodyS = reactionTo.reply().getBody();
567 for (Element el : reactions.getChildren()) {
568 if (el.getName().equals("reaction") && el.getNamespace().equals("urn:xmpp:reactions:0")) {
569 bodyS += el.getContent();
570 }
571 }
572 body = new LocalizedContent(bodyS, "en", 1);
573 final Message previousReaction = conversation.findMessageReactingTo(reactions.getAttribute("id"), counterpart);
574 if (previousReaction != null) replacementId = previousReaction.replyId();
575 }
576 }
577 }
578 }
579
580 final boolean conversationIsProbablyMuc = isTypeGroupChat || mucUserElement != null || account.getXmppConnection().getMucServersWithholdAccount().contains(counterpart.getDomain().toEscapedString());
581 final Element webxdc = packet.findChild("x", "urn:xmpp:webxdc:0");
582 final Element thread = packet.findChild("thread");
583 if (webxdc != null && thread != null) {
584 final Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, counterpart.asBareJid(), conversationIsProbablyMuc, false, query, false);
585 Jid webxdcSender = counterpart.asBareJid();
586 if (conversation.getMode() == Conversation.MODE_MULTI) {
587 if(conversation.getMucOptions().nonanonymous()) {
588 webxdcSender = conversation.getMucOptions().getTrueCounterpart(counterpart);
589 } else {
590 webxdcSender = counterpart;
591 }
592 }
593 final var document = webxdc.findChildContent("document", "urn:xmpp:webxdc:0");
594 final var summary = webxdc.findChildContent("summary", "urn:xmpp:webxdc:0");
595 final var payload = webxdc.findChildContent("json", "urn:xmpp:json:0");
596 if (document != null || summary != null || payload != null) {
597 mXmppConnectionService.insertWebxdcUpdate(new WebxdcUpdate(
598 conversation,
599 remoteMsgId,
600 counterpart,
601 thread,
602 body == null ? null : body.content,
603 document,
604 summary,
605 payload
606 ));
607 }
608
609 final var realtime = webxdc.findChildContent("data", "urn:xmpp:webxdc:0");
610 if (realtime != null) conversation.webxdcRealtimeData(thread, realtime);
611
612 mXmppConnectionService.updateConversationUi();
613 }
614
615 // Basic visibility for voice requests
616 if (body == null && html == null && pgpEncrypted == null && axolotlEncrypted == null && !isMucStatusMessage) {
617 final Element formEl = packet.findChild("x", "jabber:x:data");
618 if (formEl != null) {
619 final Data form = Data.parse(formEl);
620 final String role = form.getValue("muc#role");
621 final String nick = form.getValue("muc#roomnick");
622 if ("http://jabber.org/protocol/muc#request".equals(form.getFormType()) && "participant".equals(role)) {
623 body = new LocalizedContent("" + nick + " is requesting to speak", "en", 1);
624 }
625 }
626 }
627
628 if ((body != null || pgpEncrypted != null || (axolotlEncrypted != null && axolotlEncrypted.hasChild("payload")) || !attachments.isEmpty() || html != null || (packet.hasChild("subject") && packet.hasChild("thread"))) && !isMucStatusMessage) {
629 final Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, counterpart.asBareJid(), conversationIsProbablyMuc, false, query, false);
630 final boolean conversationMultiMode = conversation.getMode() == Conversation.MODE_MULTI;
631
632 if (serverMsgId == null) {
633 serverMsgId = extractStanzaId(packet, isTypeGroupChat, conversation);
634 }
635
636
637 if (selfAddressed) {
638 if (mXmppConnectionService.markMessage(conversation, remoteMsgId, Message.STATUS_SEND_RECEIVED, serverMsgId)) {
639 return;
640 }
641 status = Message.STATUS_RECEIVED;
642 if (remoteMsgId != null && conversation.findMessageWithRemoteId(remoteMsgId, counterpart) != null) {
643 return;
644 }
645 }
646
647 if (isTypeGroupChat) {
648 if (conversation.getMucOptions().isSelf(counterpart)) {
649 status = Message.STATUS_SEND_RECEIVED;
650 isCarbon = true; //not really carbon but received from another resource
651 if (mXmppConnectionService.markMessage(conversation, remoteMsgId, status, serverMsgId, body, html, packet.findChildContent("subject"), packet.findChild("thread"), attachments)) {
652 return;
653 } else if (remoteMsgId == null || Config.IGNORE_ID_REWRITE_IN_MUC) {
654 if (body != null) {
655 Message message = conversation.findSentMessageWithBody(body.content);
656 if (message != null) {
657 mXmppConnectionService.markMessage(message, status);
658 return;
659 }
660 }
661 }
662 } else {
663 status = Message.STATUS_RECEIVED;
664 }
665 }
666 final Message message;
667 if (pgpEncrypted != null && Config.supportOpenPgp()) {
668 message = new Message(conversation, pgpEncrypted, Message.ENCRYPTION_PGP, status);
669 } else if (axolotlEncrypted != null && Config.supportOmemo()) {
670 Jid origin;
671 Set<Jid> fallbacksBySourceId = Collections.emptySet();
672 if (conversationMultiMode) {
673 final Jid fallback = conversation.getMucOptions().getTrueCounterpart(counterpart);
674 origin = getTrueCounterpart(query != null ? mucUserElement : null, fallback);
675 if (origin == null) {
676 try {
677 fallbacksBySourceId = account.getAxolotlService().findCounterpartsBySourceId(XmppAxolotlMessage.parseSourceId(axolotlEncrypted));
678 } catch (IllegalArgumentException e) {
679 //ignoring
680 }
681 }
682 if (origin == null && fallbacksBySourceId.size() == 0) {
683 Log.d(Config.LOGTAG, "axolotl message in anonymous conference received and no possible fallbacks");
684 return;
685 }
686 } else {
687 fallbacksBySourceId = Collections.emptySet();
688 origin = from;
689 }
690
691 final boolean liveMessage = query == null && !isTypeGroupChat && mucUserElement == null;
692 final boolean checkedForDuplicates = liveMessage || (serverMsgId != null && remoteMsgId != null && !conversation.possibleDuplicate(serverMsgId, remoteMsgId));
693
694 if (origin != null) {
695 message = parseAxolotlChat(axolotlEncrypted, origin, conversation, status, checkedForDuplicates, query != null);
696 } else {
697 Message trial = null;
698 for (Jid fallback : fallbacksBySourceId) {
699 trial = parseAxolotlChat(axolotlEncrypted, fallback, conversation, status, checkedForDuplicates && fallbacksBySourceId.size() == 1, query != null);
700 if (trial != null) {
701 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": decoded muc message using fallback");
702 origin = fallback;
703 break;
704 }
705 }
706 message = trial;
707 }
708 if (message == null) {
709 if (query == null && extractChatState(mXmppConnectionService.find(account, counterpart.asBareJid()), isTypeGroupChat, packet)) {
710 mXmppConnectionService.updateConversationUi();
711 }
712 if (query != null && status == Message.STATUS_SEND && remoteMsgId != null) {
713 Message previouslySent = conversation.findSentMessageWithUuid(remoteMsgId);
714 if (previouslySent != null && previouslySent.getServerMsgId() == null && serverMsgId != null) {
715 previouslySent.setServerMsgId(serverMsgId);
716 mXmppConnectionService.databaseBackend.updateMessage(previouslySent, false);
717 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": encountered previously sent OMEMO message without serverId. updating...");
718 }
719 }
720 return;
721 }
722 if (conversationMultiMode) {
723 message.setTrueCounterpart(origin);
724 }
725 } else if (body == null && !attachments.isEmpty()) {
726 message = new Message(conversation, "", Message.ENCRYPTION_NONE, status);
727 } else {
728 message = new Message(conversation, body == null ? null : body.content, Message.ENCRYPTION_NONE, status);
729 if (body != null && body.count > 1) {
730 message.setBodyLanguage(body.language);
731 }
732 }
733
734 Element addresses = packet.findChild("addresses", "http://jabber.org/protocol/address");
735 if (status == Message.STATUS_RECEIVED && addresses != null) {
736 for (Element address : addresses.getChildren()) {
737 if (!address.getName().equals("address") || !address.getNamespace().equals("http://jabber.org/protocol/address")) continue;
738
739 if (address.getAttribute("type").equals("ofrom") && address.getAttribute("jid") != null) {
740 Jid ofrom = address.getAttributeAsJid("jid");
741 if (InvalidJid.isValid(ofrom) && ofrom.getDomain().equals(counterpart.getDomain()) &&
742 conversation.getAccount().getRoster().getContact(counterpart.getDomain()).getPresences().anySupport("http://jabber.org/protocol/address")) {
743
744 message.setTrueCounterpart(ofrom);
745 }
746 }
747 }
748 }
749
750 if (html != null) message.addPayload(html);
751 message.setSubject(packet.findChildContent("subject"));
752 message.setCounterpart(counterpart);
753 message.setRemoteMsgId(remoteMsgId);
754 message.setServerMsgId(serverMsgId);
755 message.setCarbon(isCarbon);
756 message.setTime(timestamp);
757 if (!attachments.isEmpty()) {
758 message.setFileParams(attachments.iterator().next());
759 if (CryptoHelper.isPgpEncryptedUrl(message.getFileParams().url)) {
760 message.setEncryption(Message.ENCRYPTION_DECRYPTED);
761 }
762 }
763 message.markable = packet.hasChild("markable", "urn:xmpp:chat-markers:0");
764 if (reactions != null) message.addPayload(reactions);
765 for (Element el : packet.getChildren()) {
766 if ((el.getName().equals("query") && el.getNamespace().equals("http://jabber.org/protocol/disco#items") && el.getAttribute("node").equals("http://jabber.org/protocol/commands")) ||
767 (el.getName().equals("fallback") && el.getNamespace().equals("urn:xmpp:fallback:0"))) {
768 message.addPayload(el);
769 }
770 if (el.getName().equals("thread") && (el.getNamespace() == null || el.getNamespace().equals("jabber:client"))) {
771 el.setAttribute("xmlns", "jabber:client");
772 message.addPayload(el);
773 }
774 if (el.getName().equals("reply") && el.getNamespace() != null && el.getNamespace().equals("urn:xmpp:reply:0")) {
775 message.addPayload(el);
776 if (el.getAttribute("id") != null) {
777 for (final var parent : mXmppConnectionService.getMessageFuzzyIds(conversation, List.of(el.getAttribute("id"))).entrySet()) {
778 message.setInReplyTo(parent.getValue());
779 }
780 }
781 }
782 if (el.getName().equals("attention") && el.getNamespace() != null && el.getNamespace().equals("urn:xmpp:attention:0")) {
783 message.addPayload(el);
784 }
785 if (el.getName().equals("Description") && el.getNamespace() != null && el.getNamespace().equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#")) {
786 message.addPayload(el);
787 }
788 }
789 if (conversationMultiMode) {
790 message.setMucUser(conversation.getMucOptions().findUserByFullJid(counterpart));
791 final Element occupantId = packet.findChild("occupant-id", "urn:xmpp:occupant-id:0");
792 if (occupantId != null) message.setOccupantId(occupantId.getAttribute("id"));
793 final Jid fallback = conversation.getMucOptions().getTrueCounterpart(counterpart);
794 Jid trueCounterpart;
795 if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL) {
796 trueCounterpart = message.getTrueCounterpart();
797 } else if (query != null && query.safeToExtractTrueCounterpart()) {
798 trueCounterpart = getTrueCounterpart(mucUserElement, fallback);
799 } else {
800 trueCounterpart = fallback;
801 }
802 if (trueCounterpart != null && isTypeGroupChat) {
803 if (trueCounterpart.asBareJid().equals(account.getJid().asBareJid())) {
804 status = isTypeGroupChat ? Message.STATUS_SEND_RECEIVED : Message.STATUS_SEND;
805 } else {
806 status = Message.STATUS_RECEIVED;
807 message.setCarbon(false);
808 }
809 }
810 message.setStatus(status);
811 message.setTrueCounterpart(trueCounterpart);
812 if (!isTypeGroupChat) {
813 message.setType(Message.TYPE_PRIVATE);
814 }
815 } else {
816 updateLastseen(account, from);
817 }
818
819 if (replacementId != null && mXmppConnectionService.allowMessageCorrection()) {
820 final Message replacedMessage = conversation.findMessageWithRemoteIdAndCounterpart(replacementId, counterpart);
821 if (replacedMessage != null) {
822 final boolean fingerprintsMatch = replacedMessage.getFingerprint() == null
823 || replacedMessage.getFingerprint().equals(message.getFingerprint());
824 final boolean trueCountersMatch = replacedMessage.getTrueCounterpart() != null
825 && message.getTrueCounterpart() != null
826 && replacedMessage.getTrueCounterpart().asBareJid().equals(message.getTrueCounterpart().asBareJid());
827 final boolean mucUserMatches = query == null && replacedMessage.sameMucUser(message);
828 final boolean duplicate = conversation.hasDuplicateMessage(message);
829 if (fingerprintsMatch && (trueCountersMatch || !conversationMultiMode || mucUserMatches || counterpart.isBareJid()) && !duplicate) {
830 Log.d(Config.LOGTAG, "replaced message '" + replacedMessage.getBody() + "' with '" + message.getBody() + "'");
831 synchronized (replacedMessage) {
832 final String uuid = replacedMessage.getUuid();
833 replacedMessage.setUuid(UUID.randomUUID().toString());
834 replacedMessage.setBody(message.getBody());
835 replacedMessage.setSubject(message.getSubject());
836 replacedMessage.setThread(message.getThread());
837 replacedMessage.putEdited(replacedMessage.getRemoteMsgId(), replacedMessage.getServerMsgId());
838 replacedMessage.setRemoteMsgId(remoteMsgId);
839 if (replaceElement != null && !replaceElement.getName().equals("replace")) {
840 mXmppConnectionService.getFileBackend().deleteFile(replacedMessage);
841 mXmppConnectionService.evictPreview(message.getUuid());
842 List<Element> thumbs = replacedMessage.getFileParams() != null ? replacedMessage.getFileParams().getThumbnails() : null;
843 if (thumbs != null && !thumbs.isEmpty()) {
844 for (Element thumb : thumbs) {
845 Uri uri = Uri.parse(thumb.getAttribute("uri"));
846 if (uri.getScheme().equals("cid")) {
847 Cid cid = BobTransfer.cid(uri);
848 if (cid == null) continue;
849 DownloadableFile f = mXmppConnectionService.getFileForCid(cid);
850 if (f != null) {
851 mXmppConnectionService.evictPreview(f);
852 f.delete();
853 }
854 }
855 }
856 }
857 replacedMessage.clearPayloads();
858 replacedMessage.setFileParams(null);
859 replacedMessage.addPayload(replaceElement);
860 }
861 if (replacedMessage.getServerMsgId() == null || message.getServerMsgId() != null) {
862 replacedMessage.setServerMsgId(message.getServerMsgId());
863 }
864 replacedMessage.setEncryption(message.getEncryption());
865 if (replacedMessage.getStatus() == Message.STATUS_RECEIVED) {
866 replacedMessage.markUnread();
867 }
868 extractChatState(mXmppConnectionService.find(account, counterpart.asBareJid()), isTypeGroupChat, packet);
869 mXmppConnectionService.updateMessage(replacedMessage, uuid);
870 if (mXmppConnectionService.confirmMessages()
871 && replacedMessage.getStatus() == Message.STATUS_RECEIVED
872 && (replacedMessage.trusted() || replacedMessage.isPrivateMessage()) //TODO do we really want to send receipts for all PMs?
873 && remoteMsgId != null
874 && !selfAddressed
875 && !isTypeGroupChat) {
876 processMessageReceipts(account, packet, remoteMsgId, query);
877 }
878 if (replacedMessage.getEncryption() == Message.ENCRYPTION_PGP) {
879 conversation.getAccount().getPgpDecryptionService().discard(replacedMessage);
880 conversation.getAccount().getPgpDecryptionService().decrypt(replacedMessage, false);
881 }
882 }
883 mXmppConnectionService.getNotificationService().updateNotification();
884 return;
885 } else {
886 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": received message correction but verification didn't check out");
887 }
888 } else if (message.getBody() == null || message.getBody().equals("") || message.getBody().equals(" ")) {
889 return;
890 }
891 if (replaceElement != null && !replaceElement.getName().equals("replace")) return;
892 }
893
894 boolean checkForDuplicates = (isTypeGroupChat && packet.hasChild("delay", "urn:xmpp:delay"))
895 || message.isPrivateMessage()
896 || message.getServerMsgId() != null
897 || (query == null && mXmppConnectionService.getMessageArchiveService().isCatchupInProgress(conversation));
898 if (checkForDuplicates) {
899 final Message duplicate = conversation.findDuplicateMessage(message);
900 if (duplicate != null) {
901 final boolean serverMsgIdUpdated;
902 if (duplicate.getStatus() != Message.STATUS_RECEIVED
903 && duplicate.getUuid().equals(message.getRemoteMsgId())
904 && duplicate.getServerMsgId() == null
905 && message.getServerMsgId() != null) {
906 duplicate.setServerMsgId(message.getServerMsgId());
907 if (mXmppConnectionService.databaseBackend.updateMessage(duplicate, false)) {
908 serverMsgIdUpdated = true;
909 } else {
910 serverMsgIdUpdated = false;
911 Log.e(Config.LOGTAG, "failed to update message");
912 }
913 } else {
914 serverMsgIdUpdated = false;
915 }
916 Log.d(Config.LOGTAG, "skipping duplicate message with " + message.getCounterpart() + ". serverMsgIdUpdated=" + serverMsgIdUpdated);
917 return;
918 }
919 }
920
921 if (query != null && query.getPagingOrder() == MessageArchiveService.PagingOrder.REVERSE) {
922 conversation.prepend(query.getActualInThisQuery(), message);
923 } else {
924 conversation.add(message);
925 }
926 if (query != null) {
927 query.incrementActualMessageCount();
928 }
929
930 if (query == null || query.isCatchup()) { //either no mam or catchup
931 if (status == Message.STATUS_SEND || status == Message.STATUS_SEND_RECEIVED) {
932 mXmppConnectionService.markRead(conversation);
933 if (query == null) {
934 activateGracePeriod(account);
935 }
936 } else {
937 message.markUnread();
938 notify = true;
939 }
940 }
941
942 if (message.getEncryption() == Message.ENCRYPTION_PGP) {
943 notify = conversation.getAccount().getPgpDecryptionService().decrypt(message, notify);
944 } else if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL_NOT_FOR_THIS_DEVICE || message.getEncryption() == Message.ENCRYPTION_AXOLOTL_FAILED) {
945 notify = false;
946 }
947
948 if (query == null) {
949 extractChatState(mXmppConnectionService.find(account, counterpart.asBareJid()), isTypeGroupChat, packet);
950 mXmppConnectionService.updateConversationUi();
951 }
952
953 if (mXmppConnectionService.confirmMessages()
954 && message.getStatus() == Message.STATUS_RECEIVED
955 && (message.trusted() || message.isPrivateMessage())
956 && remoteMsgId != null
957 && !selfAddressed
958 && !isTypeGroupChat) {
959 processMessageReceipts(account, packet, remoteMsgId, query);
960 }
961
962 if (message.getFileParams() != null) {
963 for (Cid cid : message.getFileParams().getCids()) {
964 File f = mXmppConnectionService.getFileForCid(cid);
965 if (f != null && f.canRead()) {
966 message.setRelativeFilePath(f.getAbsolutePath());
967 mXmppConnectionService.getFileBackend().updateFileParams(message, null, false);
968 break;
969 }
970 }
971 }
972
973 mXmppConnectionService.databaseBackend.createMessage(message);
974
975 final HttpConnectionManager manager = this.mXmppConnectionService.getHttpConnectionManager();
976 if (message.getRelativeFilePath() == null && message.trusted() && message.treatAsDownloadable() && manager.getAutoAcceptFileSize() > 0) {
977 if (message.getOob() != null && "cid".equalsIgnoreCase(message.getOob().getScheme())) {
978 try {
979 BobTransfer transfer = new BobTransfer.ForMessage(message, mXmppConnectionService);
980 message.setTransferable(transfer);
981 transfer.start();
982 } catch (URISyntaxException e) {
983 Log.d(Config.LOGTAG, "BobTransfer failed to parse URI");
984 }
985 } else {
986 manager.createNewDownloadConnection(message);
987 }
988 } else if (notify) {
989 if (query != null && query.isCatchup()) {
990 mXmppConnectionService.getNotificationService().pushFromBacklog(message);
991 } else {
992 mXmppConnectionService.getNotificationService().push(message);
993 }
994 }
995 } else if (!packet.hasChild("body")) { //no body
996
997 final Conversation conversation = mXmppConnectionService.find(account, from.asBareJid());
998 if (axolotlEncrypted != null) {
999 Jid origin;
1000 if (conversation != null && conversation.getMode() == Conversation.MODE_MULTI) {
1001 final Jid fallback = conversation.getMucOptions().getTrueCounterpart(counterpart);
1002 origin = getTrueCounterpart(query != null ? mucUserElement : null, fallback);
1003 if (origin == null) {
1004 Log.d(Config.LOGTAG, "omemo key transport message in anonymous conference received");
1005 return;
1006 }
1007 } else if (isTypeGroupChat) {
1008 return;
1009 } else {
1010 origin = from;
1011 }
1012 try {
1013 final XmppAxolotlMessage xmppAxolotlMessage = XmppAxolotlMessage.fromElement(axolotlEncrypted, origin.asBareJid());
1014 account.getAxolotlService().processReceivingKeyTransportMessage(xmppAxolotlMessage, query != null);
1015 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": omemo key transport message received from " + origin);
1016 } catch (Exception e) {
1017 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": invalid omemo key transport message received " + e.getMessage());
1018 return;
1019 }
1020 }
1021
1022 if (query == null && extractChatState(mXmppConnectionService.find(account, counterpart.asBareJid()), isTypeGroupChat, packet)) {
1023 mXmppConnectionService.updateConversationUi();
1024 }
1025
1026 if (isTypeGroupChat) {
1027 if (packet.hasChild("subject") && !packet.hasChild("thread")) { // We already know it has no body per above
1028 if (conversation != null && conversation.getMode() == Conversation.MODE_MULTI) {
1029 conversation.setHasMessagesLeftOnServer(conversation.countMessages() > 0);
1030 final LocalizedContent subject = packet.findInternationalizedChildContentInDefaultNamespace("subject");
1031 if (subject != null && conversation.getMucOptions().setSubject(subject.content)) {
1032 mXmppConnectionService.updateConversation(conversation);
1033 }
1034 mXmppConnectionService.updateConversationUi();
1035 return;
1036 }
1037 }
1038 }
1039 if (conversation != null && mucUserElement != null && InvalidJid.hasValidFrom(packet) && from.isBareJid()) {
1040 for (Element child : mucUserElement.getChildren()) {
1041 if ("status".equals(child.getName())) {
1042 try {
1043 int code = Integer.parseInt(child.getAttribute("code"));
1044 if ((code >= 170 && code <= 174) || (code >= 102 && code <= 104)) {
1045 mXmppConnectionService.fetchConferenceConfiguration(conversation);
1046 break;
1047 }
1048 } catch (Exception e) {
1049 //ignored
1050 }
1051 } else if ("item".equals(child.getName())) {
1052 MucOptions.User user = AbstractParser.parseItem(conversation, child);
1053 Log.d(Config.LOGTAG, account.getJid() + ": changing affiliation for "
1054 + user.getRealJid() + " to " + user.getAffiliation() + " in "
1055 + conversation.getJid().asBareJid());
1056 if (!user.realJidMatchesAccount()) {
1057 boolean isNew = conversation.getMucOptions().updateUser(user);
1058 mXmppConnectionService.getAvatarService().clear(conversation);
1059 mXmppConnectionService.updateMucRosterUi();
1060 mXmppConnectionService.updateConversationUi();
1061 Contact contact = user.getContact();
1062 if (!user.getAffiliation().ranks(MucOptions.Affiliation.MEMBER)) {
1063 Jid jid = user.getRealJid();
1064 List<Jid> cryptoTargets = conversation.getAcceptedCryptoTargets();
1065 if (cryptoTargets.remove(user.getRealJid())) {
1066 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": removed " + jid + " from crypto targets of " + conversation.getName());
1067 conversation.setAcceptedCryptoTargets(cryptoTargets);
1068 mXmppConnectionService.updateConversation(conversation);
1069 }
1070 } else if (isNew
1071 && user.getRealJid() != null
1072 && conversation.getMucOptions().isPrivateAndNonAnonymous()
1073 && (contact == null || !contact.mutualPresenceSubscription())
1074 && account.getAxolotlService().hasEmptyDeviceList(user.getRealJid())) {
1075 account.getAxolotlService().fetchDeviceIds(user.getRealJid());
1076 }
1077 }
1078 }
1079 }
1080 }
1081 if (!isTypeGroupChat) {
1082 for (Element child : packet.getChildren()) {
1083 if (Namespace.JINGLE_MESSAGE.equals(child.getNamespace()) && JINGLE_MESSAGE_ELEMENT_NAMES.contains(child.getName())) {
1084 final String action = child.getName();
1085 final String sessionId = child.getAttribute("id");
1086 if (sessionId == null) {
1087 break;
1088 }
1089 if (query == null && offlineMessagesRetrieved) {
1090 if (serverMsgId == null) {
1091 serverMsgId = extractStanzaId(account, packet);
1092 }
1093 mXmppConnectionService
1094 .getJingleConnectionManager()
1095 .deliverMessage(
1096 account,
1097 packet.getTo(),
1098 packet.getFrom(),
1099 child,
1100 remoteMsgId,
1101 serverMsgId,
1102 timestamp);
1103 final Contact contact = account.getRoster().getContact(from);
1104 // this is the same condition that is found in JingleRtpConnection for
1105 // the 'ringing' response. Responding with delivery receipts predates
1106 // the 'ringing' spec'd
1107 final boolean sendReceipts =
1108 (mXmppConnectionService.confirmMessages()
1109 && contact.showInContactList())
1110 || Config.JINGLE_MESSAGE_INIT_STRICT_OFFLINE_CHECK;
1111 if (remoteMsgId != null && !contact.isSelf() && sendReceipts) {
1112 processMessageReceipts(account, packet, remoteMsgId, null);
1113 }
1114 } else if ((query != null && query.isCatchup()) || !offlineMessagesRetrieved) {
1115 if ("propose".equals(action)) {
1116 final Element description = child.findChild("description");
1117 final String namespace =
1118 description == null ? null : description.getNamespace();
1119 if (Namespace.JINGLE_APPS_RTP.equals(namespace)) {
1120 final Conversation c =
1121 mXmppConnectionService.findOrCreateConversation(
1122 account, counterpart.asBareJid(), false, false);
1123 final Message preExistingMessage =
1124 c.findRtpSession(sessionId, status);
1125 if (preExistingMessage != null) {
1126 preExistingMessage.setServerMsgId(serverMsgId);
1127 mXmppConnectionService.updateMessage(preExistingMessage);
1128 break;
1129 }
1130 final Message message =
1131 new Message(
1132 c, status, Message.TYPE_RTP_SESSION, sessionId);
1133 message.setServerMsgId(serverMsgId);
1134 message.setTime(timestamp);
1135 message.setBody(new RtpSessionStatus(false, 0).toString());
1136 c.add(message);
1137 mXmppConnectionService.databaseBackend.createMessage(message);
1138 }
1139 } else if ("proceed".equals(action)) {
1140 // status needs to be flipped to find the original propose
1141 final Conversation c =
1142 mXmppConnectionService.findOrCreateConversation(
1143 account, counterpart.asBareJid(), false, false);
1144 final int s =
1145 packet.fromAccount(account)
1146 ? Message.STATUS_RECEIVED
1147 : Message.STATUS_SEND;
1148 final Message message = c.findRtpSession(sessionId, s);
1149 if (message != null) {
1150 message.setBody(new RtpSessionStatus(true, 0).toString());
1151 if (serverMsgId != null) {
1152 message.setServerMsgId(serverMsgId);
1153 }
1154 message.setTime(timestamp);
1155 mXmppConnectionService.updateMessage(message, true);
1156 } else {
1157 Log.d(
1158 Config.LOGTAG,
1159 "unable to find original rtp session message for received propose");
1160 }
1161
1162 } else if ("finish".equals(action)) {
1163 Log.d(
1164 Config.LOGTAG,
1165 "received JMI 'finish' during MAM catch-up. Can be used to update success/failure and duration");
1166 }
1167 } else {
1168 //MAM reloads (non catchups
1169 if ("propose".equals(action)) {
1170 final Element description = child.findChild("description");
1171 final String namespace = description == null ? null : description.getNamespace();
1172 if (Namespace.JINGLE_APPS_RTP.equals(namespace)) {
1173 final Conversation c = mXmppConnectionService.findOrCreateConversation(account, counterpart.asBareJid(), false, false);
1174 final Message preExistingMessage = c.findRtpSession(sessionId, status);
1175 if (preExistingMessage != null) {
1176 preExistingMessage.setServerMsgId(serverMsgId);
1177 mXmppConnectionService.updateMessage(preExistingMessage);
1178 break;
1179 }
1180 final Message message = new Message(
1181 c,
1182 status,
1183 Message.TYPE_RTP_SESSION,
1184 sessionId
1185 );
1186 message.setServerMsgId(serverMsgId);
1187 message.setTime(timestamp);
1188 message.setBody(new RtpSessionStatus(true, 0).toString());
1189 if (query.getPagingOrder() == MessageArchiveService.PagingOrder.REVERSE) {
1190 c.prepend(query.getActualInThisQuery(), message);
1191 } else {
1192 c.add(message);
1193 }
1194 query.incrementActualMessageCount();
1195 mXmppConnectionService.databaseBackend.createMessage(message);
1196 }
1197 }
1198 }
1199 break;
1200 }
1201 }
1202 }
1203 }
1204
1205 Element received = packet.findChild("received", "urn:xmpp:chat-markers:0");
1206 if (received == null) {
1207 received = packet.findChild("received", "urn:xmpp:receipts");
1208 }
1209 if (received != null) {
1210 String id = received.getAttribute("id");
1211 if (packet.fromAccount(account)) {
1212 if (query != null && id != null && packet.getTo() != null) {
1213 query.removePendingReceiptRequest(new ReceiptRequest(packet.getTo(), id));
1214 }
1215 } else if (id != null) {
1216 if (id.startsWith(JingleRtpConnection.JINGLE_MESSAGE_PROPOSE_ID_PREFIX)) {
1217 final String sessionId = id.substring(JingleRtpConnection.JINGLE_MESSAGE_PROPOSE_ID_PREFIX.length());
1218 mXmppConnectionService.getJingleConnectionManager()
1219 .updateProposedSessionDiscovered(account, from, sessionId, JingleConnectionManager.DeviceDiscoveryState.DISCOVERED);
1220 } else {
1221 mXmppConnectionService.markMessage(account, from.asBareJid(), id, Message.STATUS_SEND_RECEIVED);
1222 }
1223 }
1224 }
1225 final Element displayed = packet.findChild("displayed", "urn:xmpp:chat-markers:0");
1226 if (displayed != null) {
1227 final String id = displayed.getAttribute("id");
1228 final Jid sender = InvalidJid.getNullForInvalid(displayed.getAttributeAsJid("sender"));
1229 if (packet.fromAccount(account) && !selfAddressed) {
1230 final Conversation c =
1231 mXmppConnectionService.find(account, counterpart.asBareJid());
1232 final Message message =
1233 (c == null || id == null) ? null : c.findReceivedWithRemoteId(id);
1234 if (message != null && (query == null || query.isCatchup())) {
1235 mXmppConnectionService.markReadUpTo(c, message);
1236 }
1237 if (query == null) {
1238 activateGracePeriod(account);
1239 }
1240 } else if (isTypeGroupChat) {
1241 final Conversation conversation = mXmppConnectionService.find(account, counterpart.asBareJid());
1242 final Message message;
1243 if (conversation != null && id != null) {
1244 if (sender != null) {
1245 message = conversation.findMessageWithRemoteId(id, sender);
1246 } else {
1247 message = conversation.findMessageWithServerMsgId(id);
1248 }
1249 } else {
1250 message = null;
1251 }
1252 if (message != null) {
1253 final Jid fallback = conversation.getMucOptions().getTrueCounterpart(counterpart);
1254 final Jid trueJid = getTrueCounterpart((query != null && query.safeToExtractTrueCounterpart()) ? mucUserElement : null, fallback);
1255 final boolean trueJidMatchesAccount = account.getJid().asBareJid().equals(trueJid == null ? null : trueJid.asBareJid());
1256 if (trueJidMatchesAccount || conversation.getMucOptions().isSelf(counterpart)) {
1257 if (!message.isRead() && (query == null || query.isCatchup())) { //checking if message is unread fixes race conditions with reflections
1258 mXmppConnectionService.markReadUpTo(conversation, message);
1259 }
1260 } else if (!counterpart.isBareJid() && trueJid != null) {
1261 final ReadByMarker readByMarker = ReadByMarker.from(counterpart, trueJid);
1262 if (message.addReadByMarker(readByMarker)) {
1263 final var mucOptions = conversation.getMucOptions();
1264 final var everyone = ImmutableSet.copyOf(mucOptions.getMembers(false));
1265 final var readyBy = message.getReadyByTrue();
1266 final var mStatus = message.getStatus();
1267 if (mucOptions.isPrivateAndNonAnonymous()
1268 && (mStatus == Message.STATUS_SEND_RECEIVED
1269 || mStatus == Message.STATUS_SEND)
1270 && readyBy.containsAll(everyone)) {
1271 message.setStatus(Message.STATUS_SEND_DISPLAYED);
1272 }
1273 mXmppConnectionService.updateMessage(message, false);
1274 }
1275 }
1276 }
1277 } else {
1278 final Message displayedMessage = mXmppConnectionService.markMessage(account, from.asBareJid(), id, Message.STATUS_SEND_DISPLAYED);
1279 Message message = displayedMessage == null ? null : displayedMessage.prev();
1280 while (message != null
1281 && message.getStatus() == Message.STATUS_SEND_RECEIVED
1282 && message.getTimeSent() < displayedMessage.getTimeSent()) {
1283 mXmppConnectionService.markMessage(message, Message.STATUS_SEND_DISPLAYED);
1284 message = message.prev();
1285 }
1286 if (displayedMessage != null && selfAddressed) {
1287 dismissNotification(account, counterpart, query, id);
1288 }
1289 }
1290 }
1291
1292 final Element event = original.findChild("event", "http://jabber.org/protocol/pubsub#event");
1293 if (event != null && InvalidJid.hasValidFrom(original) && original.getFrom().isBareJid()) {
1294 if (event.hasChild("items")) {
1295 parseEvent(event, original.getFrom(), account);
1296 } else if (event.hasChild("delete")) {
1297 parseDeleteEvent(event, original.getFrom(), account);
1298 } else if (event.hasChild("purge")) {
1299 parsePurgeEvent(event, original.getFrom(), account);
1300 }
1301 }
1302
1303 final String nick = packet.findChildContent("nick", Namespace.NICK);
1304 if (nick != null && InvalidJid.hasValidFrom(original)) {
1305 if (mXmppConnectionService.isMuc(account, from)) {
1306 return;
1307 }
1308 final Contact contact = account.getRoster().getContact(from);
1309 if (contact.setPresenceName(nick)) {
1310 mXmppConnectionService.syncRoster(account);
1311 mXmppConnectionService.getAvatarService().clear(contact);
1312 }
1313 }
1314 }
1315
1316 private static Pair<im.conversations.android.xmpp.model.stanza.Message,Long> getForwardedMessagePacket(final im.conversations.android.xmpp.model.stanza.Message original, Class<? extends Extension> clazz) {
1317 final var extension = original.getExtension(clazz);
1318 final var forwarded = extension == null ? null : extension.getExtension(Forwarded.class);
1319 if (forwarded == null) {
1320 return null;
1321 }
1322 final Long timestamp = AbstractParser.parseTimestamp(forwarded, null);
1323 final var forwardedMessage = forwarded.getMessage();
1324 if (forwardedMessage == null) {
1325 return null;
1326 }
1327 return new Pair<>(forwardedMessage,timestamp);
1328 }
1329
1330 private static Pair<im.conversations.android.xmpp.model.stanza.Message,Long> getForwardedMessagePacket(final im.conversations.android.xmpp.model.stanza.Message original, final String name, final String namespace) {
1331 final Element wrapper = original.findChild(name, namespace);
1332 final var forwardedElement = wrapper == null ? null : wrapper.findChild("forwarded",Namespace.FORWARD);
1333 if (forwardedElement instanceof Forwarded forwarded) {
1334 final Long timestamp = AbstractParser.parseTimestamp(forwarded, null);
1335 final var forwardedMessage = forwarded.getMessage();
1336 if (forwardedMessage == null) {
1337 return null;
1338 }
1339 return new Pair<>(forwardedMessage,timestamp);
1340 }
1341 return null;
1342 }
1343
1344 private void dismissNotification(Account account, Jid counterpart, MessageArchiveService.Query query, final String id) {
1345 final Conversation conversation = mXmppConnectionService.find(account, counterpart.asBareJid());
1346 if (conversation != null && (query == null || query.isCatchup())) {
1347 final String displayableId = conversation.findMostRecentRemoteDisplayableId();
1348 if (displayableId != null && displayableId.equals(id)) {
1349 mXmppConnectionService.markRead(conversation);
1350 } else {
1351 Log.w(Config.LOGTAG, account.getJid().asBareJid() + ": received dismissing display marker that did not match our last id in that conversation");
1352 }
1353 }
1354 }
1355
1356 private void processMessageReceipts(final Account account, final im.conversations.android.xmpp.model.stanza.Message packet, final String remoteMsgId, MessageArchiveService.Query query) {
1357 final boolean markable = packet.hasChild("markable", "urn:xmpp:chat-markers:0");
1358 final boolean request = packet.hasChild("request", "urn:xmpp:receipts");
1359 if (query == null) {
1360 final ArrayList<String> receiptsNamespaces = new ArrayList<>();
1361 if (markable) {
1362 receiptsNamespaces.add("urn:xmpp:chat-markers:0");
1363 }
1364 if (request) {
1365 receiptsNamespaces.add("urn:xmpp:receipts");
1366 }
1367 if (receiptsNamespaces.size() > 0) {
1368 final var receipt = mXmppConnectionService.getMessageGenerator().received(account,
1369 packet.getFrom(),
1370 remoteMsgId,
1371 receiptsNamespaces,
1372 packet.getType());
1373 mXmppConnectionService.sendMessagePacket(account, receipt);
1374 }
1375 } else if (query.isCatchup()) {
1376 if (request) {
1377 query.addPendingReceiptRequest(new ReceiptRequest(packet.getFrom(), remoteMsgId));
1378 }
1379 }
1380 }
1381
1382 private void activateGracePeriod(Account account) {
1383 long duration = mXmppConnectionService.getLongPreference("grace_period_length", R.integer.grace_period) * 1000;
1384 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": activating grace period till " + TIME_FORMAT.format(new Date(System.currentTimeMillis() + duration)));
1385 account.activateGracePeriod(duration);
1386 }
1387
1388 private class Invite {
1389 final Jid jid;
1390 final String password;
1391 final boolean direct;
1392 final Jid inviter;
1393
1394 Invite(Jid jid, String password, boolean direct, Jid inviter) {
1395 this.jid = jid;
1396 this.password = password;
1397 this.direct = direct;
1398 this.inviter = inviter;
1399 }
1400
1401 public boolean execute(final Account account) {
1402 if (this.jid == null) {
1403 return false;
1404 }
1405 final Contact contact = this.inviter != null ? account.getRoster().getContact(this.inviter) : null;
1406 if (contact != null && contact.isBlocked()) {
1407 Log.d(Config.LOGTAG,account.getJid().asBareJid()+": ignore invite from "+contact.getJid()+" because contact is blocked");
1408 return false;
1409 }
1410 final Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, jid, true, false);
1411 conversation.setAttribute("inviter", inviter.toEscapedString());
1412 if (conversation.getMucOptions().online()) {
1413 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": received invite to " + jid + " but muc is considered to be online");
1414 mXmppConnectionService.mucSelfPingAndRejoin(conversation);
1415 } else {
1416 conversation.getMucOptions().setPassword(password);
1417 mXmppConnectionService.databaseBackend.updateConversation(conversation);
1418 mXmppConnectionService.joinMuc(conversation, contact != null && contact.showInContactList());
1419 mXmppConnectionService.updateConversationUi();
1420 }
1421 return true;
1422 }
1423 }
1424}