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