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 bodyB.delete(bodyB.offsetByCodePoints(0, parseInt(span.getAttribute("start"))), bodyB.offsetByCodePoints(0, parseInt(span.getAttribute("end"))));
519 }
520 }
521 }
522 }
523
524 final var emojiMaybe = bodyB.toString().replaceAll("\\s", "");
525 if (Emoticons.isEmoji(emojiMaybe)) {
526 appendReactions = true;
527 reactions = im.conversations.android.xmpp.model.reactions.Reactions.to(reply.getAttribute("id"));
528 reactions.addExtension(new im.conversations.android.xmpp.model.reactions.Reaction(emojiMaybe));
529 }
530 }
531
532 final Element axolotlEncrypted = packet.findChildEnsureSingle(XmppAxolotlMessage.CONTAINERTAG, AxolotlService.PEP_PREFIX);
533 int status;
534 final Jid counterpart;
535 final Jid to = packet.getTo();
536 final Jid from = packet.getFrom();
537 final Element originId = packet.findChild("origin-id", Namespace.STANZA_IDS);
538 final String remoteMsgId;
539 if (originId != null && originId.getAttribute("id") != null) {
540 remoteMsgId = originId.getAttribute("id");
541 } else {
542 remoteMsgId = packet.getId();
543 }
544 boolean notify = false;
545
546 Element html = packet.findChild("html", "http://jabber.org/protocol/xhtml-im");
547 if (html != null && html.findChild("body", "http://www.w3.org/1999/xhtml") == null) {
548 html = null;
549 }
550
551 if (from == null || !InvalidJid.isValid(from) || !InvalidJid.isValid(to)) {
552 Log.e(Config.LOGTAG, "encountered invalid message from='" + from + "' to='" + to + "'");
553 return;
554 }
555
556 boolean isTypeGroupChat = packet.getType() == im.conversations.android.xmpp.model.stanza.Message.Type.GROUPCHAT;
557 if (query != null && !query.muc() && isTypeGroupChat) {
558 Log.e(Config.LOGTAG, account.getJid().asBareJid() + ": received groupchat (" + from + ") message on regular MAM request. skipping");
559 return;
560 }
561 boolean isMucStatusMessage = InvalidJid.hasValidFrom(packet) && from.isBareJid() && mucUserElement != null && mucUserElement.hasChild("status");
562 boolean selfAddressed;
563 if (packet.fromAccount(account)) {
564 status = Message.STATUS_SEND;
565 selfAddressed = to == null || account.getJid().asBareJid().equals(to.asBareJid());
566 if (selfAddressed) {
567 counterpart = from;
568 } else {
569 counterpart = to != null ? to : account.getJid();
570 }
571 } else {
572 status = Message.STATUS_RECEIVED;
573 counterpart = from;
574 selfAddressed = false;
575 }
576
577 final Invite invite = extractInvite(packet);
578 if (invite != null) {
579 if (invite.jid.asBareJid().equals(account.getJid().asBareJid())) {
580 Log.d(Config.LOGTAG,account.getJid().asBareJid()+": ignore invite to "+invite.jid+" because it matches account");
581 } else if (isTypeGroupChat) {
582 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": ignoring invite to " + invite.jid + " because it was received as group chat");
583 } else if (invite.direct && (mucUserElement != null || invite.inviter == null || mXmppConnectionService.isMuc(account, invite.inviter))) {
584 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": ignoring direct invite to " + invite.jid + " because it was received in MUC");
585 } else {
586 invite.execute(account);
587 return;
588 }
589 }
590
591 final boolean conversationIsProbablyMuc = isTypeGroupChat || mucUserElement != null || account.getXmppConnection().getMucServersWithholdAccount().contains(counterpart.getDomain().toEscapedString());
592 final Element webxdc = packet.findChild("x", "urn:xmpp:webxdc:0");
593 final Element thread = packet.findChild("thread");
594 if (webxdc != null && thread != null) {
595 final Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, counterpart.asBareJid(), conversationIsProbablyMuc, false, query, false);
596 Jid webxdcSender = counterpart.asBareJid();
597 if (conversation.getMode() == Conversation.MODE_MULTI) {
598 if(conversation.getMucOptions().nonanonymous()) {
599 webxdcSender = conversation.getMucOptions().getTrueCounterpart(counterpart);
600 } else {
601 webxdcSender = counterpart;
602 }
603 }
604 final var document = webxdc.findChildContent("document", "urn:xmpp:webxdc:0");
605 final var summary = webxdc.findChildContent("summary", "urn:xmpp:webxdc:0");
606 final var payload = webxdc.findChildContent("json", "urn:xmpp:json:0");
607 if (document != null || summary != null || payload != null) {
608 mXmppConnectionService.insertWebxdcUpdate(new WebxdcUpdate(
609 conversation,
610 remoteMsgId,
611 counterpart,
612 thread,
613 body == null ? null : body.content,
614 document,
615 summary,
616 payload
617 ));
618 }
619
620 final var realtime = webxdc.findChildContent("data", "urn:xmpp:webxdc:0");
621 if (realtime != null) conversation.webxdcRealtimeData(thread, realtime);
622
623 mXmppConnectionService.updateConversationUi();
624 }
625
626 // Basic visibility for voice requests
627 if (body == null && html == null && pgpEncrypted == null && axolotlEncrypted == null && !isMucStatusMessage) {
628 final Element formEl = packet.findChild("x", "jabber:x:data");
629 if (formEl != null) {
630 final Data form = Data.parse(formEl);
631 final String role = form.getValue("muc#role");
632 final String nick = form.getValue("muc#roomnick");
633 if ("http://jabber.org/protocol/muc#request".equals(form.getFormType()) && "participant".equals(role)) {
634 body = new LocalizedContent("" + nick + " is requesting to speak", "en", 1);
635 }
636 }
637 }
638
639 if (reactions == null && (body != null || pgpEncrypted != null || (axolotlEncrypted != null && axolotlEncrypted.hasChild("payload")) || !attachments.isEmpty() || html != null || (packet.hasChild("subject") && packet.hasChild("thread"))) && !isMucStatusMessage) {
640 final Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, counterpart.asBareJid(), conversationIsProbablyMuc, false, query, false);
641 final boolean conversationMultiMode = conversation.getMode() == Conversation.MODE_MULTI;
642
643 if (serverMsgId == null) {
644 serverMsgId = extractStanzaId(packet, isTypeGroupChat, conversation);
645 }
646
647
648 if (selfAddressed) {
649 if (mXmppConnectionService.markMessage(conversation, remoteMsgId, Message.STATUS_SEND_RECEIVED, serverMsgId)) {
650 return;
651 }
652 status = Message.STATUS_RECEIVED;
653 if (remoteMsgId != null && conversation.findMessageWithRemoteId(remoteMsgId, counterpart) != null) {
654 return;
655 }
656 }
657
658 if (isTypeGroupChat) {
659 if (conversation.getMucOptions().isSelf(counterpart)) {
660 status = Message.STATUS_SEND_RECEIVED;
661 isCarbon = true; //not really carbon but received from another resource
662 if (mXmppConnectionService.markMessage(conversation, remoteMsgId, status, serverMsgId, body, html, packet.findChildContent("subject"), packet.findChild("thread"), attachments)) {
663 return;
664 } else if (remoteMsgId == null || Config.IGNORE_ID_REWRITE_IN_MUC) {
665 if (body != null) {
666 Message message = conversation.findSentMessageWithBody(body.content);
667 if (message != null) {
668 mXmppConnectionService.markMessage(message, status);
669 return;
670 }
671 }
672 }
673 } else {
674 status = Message.STATUS_RECEIVED;
675 }
676 }
677 final Message message;
678 if (pgpEncrypted != null && Config.supportOpenPgp()) {
679 message = new Message(conversation, pgpEncrypted, Message.ENCRYPTION_PGP, status);
680 } else if (axolotlEncrypted != null && Config.supportOmemo()) {
681 Jid origin;
682 Set<Jid> fallbacksBySourceId = Collections.emptySet();
683 if (conversationMultiMode) {
684 final Jid fallback = conversation.getMucOptions().getTrueCounterpart(counterpart);
685 origin = getTrueCounterpart(query != null ? mucUserElement : null, fallback);
686 if (origin == null) {
687 try {
688 fallbacksBySourceId = account.getAxolotlService().findCounterpartsBySourceId(XmppAxolotlMessage.parseSourceId(axolotlEncrypted));
689 } catch (IllegalArgumentException e) {
690 //ignoring
691 }
692 }
693 if (origin == null && fallbacksBySourceId.size() == 0) {
694 Log.d(Config.LOGTAG, "axolotl message in anonymous conference received and no possible fallbacks");
695 return;
696 }
697 } else {
698 fallbacksBySourceId = Collections.emptySet();
699 origin = from;
700 }
701
702 final boolean liveMessage = query == null && !isTypeGroupChat && mucUserElement == null;
703 final boolean checkedForDuplicates = liveMessage || (serverMsgId != null && remoteMsgId != null && !conversation.possibleDuplicate(serverMsgId, remoteMsgId));
704
705 if (origin != null) {
706 message = parseAxolotlChat(axolotlEncrypted, origin, conversation, status, checkedForDuplicates, query != null);
707 } else {
708 Message trial = null;
709 for (Jid fallback : fallbacksBySourceId) {
710 trial = parseAxolotlChat(axolotlEncrypted, fallback, conversation, status, checkedForDuplicates && fallbacksBySourceId.size() == 1, query != null);
711 if (trial != null) {
712 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": decoded muc message using fallback");
713 origin = fallback;
714 break;
715 }
716 }
717 message = trial;
718 }
719 if (message == null) {
720 if (query == null && extractChatState(mXmppConnectionService.find(account, counterpart.asBareJid()), isTypeGroupChat, packet)) {
721 mXmppConnectionService.updateConversationUi();
722 }
723 if (query != null && status == Message.STATUS_SEND && remoteMsgId != null) {
724 Message previouslySent = conversation.findSentMessageWithUuid(remoteMsgId);
725 if (previouslySent != null && previouslySent.getServerMsgId() == null && serverMsgId != null) {
726 previouslySent.setServerMsgId(serverMsgId);
727 mXmppConnectionService.databaseBackend.updateMessage(previouslySent, false);
728 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": encountered previously sent OMEMO message without serverId. updating...");
729 }
730 }
731 return;
732 }
733 if (conversationMultiMode) {
734 message.setTrueCounterpart(origin);
735 }
736 } else if (body == null && !attachments.isEmpty()) {
737 message = new Message(conversation, "", Message.ENCRYPTION_NONE, status);
738 } else {
739 message = new Message(conversation, body == null ? null : body.content, Message.ENCRYPTION_NONE, status);
740 if (body != null && body.count > 1) {
741 message.setBodyLanguage(body.language);
742 }
743 }
744
745 Element addresses = packet.findChild("addresses", "http://jabber.org/protocol/address");
746 if (status == Message.STATUS_RECEIVED && addresses != null) {
747 for (Element address : addresses.getChildren()) {
748 if (!address.getName().equals("address") || !address.getNamespace().equals("http://jabber.org/protocol/address")) continue;
749
750 if (address.getAttribute("type").equals("ofrom") && address.getAttribute("jid") != null) {
751 Jid ofrom = address.getAttributeAsJid("jid");
752 if (InvalidJid.isValid(ofrom) && ofrom.getDomain().equals(counterpart.getDomain()) &&
753 conversation.getAccount().getRoster().getContact(counterpart.getDomain()).getPresences().anySupport("http://jabber.org/protocol/address")) {
754
755 message.setTrueCounterpart(ofrom);
756 }
757 }
758 }
759 }
760
761 if (html != null) message.addPayload(html);
762 message.setSubject(packet.findChildContent("subject"));
763 message.setCounterpart(counterpart);
764 message.setRemoteMsgId(remoteMsgId);
765 message.setServerMsgId(serverMsgId);
766 message.setCarbon(isCarbon);
767 message.setTime(timestamp);
768 if (!attachments.isEmpty()) {
769 message.setFileParams(attachments.iterator().next());
770 if (CryptoHelper.isPgpEncryptedUrl(message.getFileParams().url)) {
771 message.setEncryption(Message.ENCRYPTION_DECRYPTED);
772 }
773 }
774 message.markable = packet.hasChild("markable", "urn:xmpp:chat-markers:0");
775 for (Element el : packet.getChildren()) {
776 if ((el.getName().equals("query") && el.getNamespace().equals("http://jabber.org/protocol/disco#items") && el.getAttribute("node").equals("http://jabber.org/protocol/commands")) ||
777 (el.getName().equals("fallback") && el.getNamespace().equals("urn:xmpp:fallback:0"))) {
778 message.addPayload(el);
779 }
780 if (el.getName().equals("thread") && (el.getNamespace() == null || el.getNamespace().equals("jabber:client"))) {
781 el.setAttribute("xmlns", "jabber:client");
782 message.addPayload(el);
783 }
784 if (el.getName().equals("reply") && el.getNamespace() != null && el.getNamespace().equals("urn:xmpp:reply:0")) {
785 message.addPayload(el);
786 if (el.getAttribute("id") != null) {
787 for (final var parent : mXmppConnectionService.getMessageFuzzyIds(conversation, List.of(el.getAttribute("id"))).entrySet()) {
788 message.setInReplyTo(parent.getValue());
789 }
790 }
791 }
792 if (el.getName().equals("attention") && el.getNamespace() != null && el.getNamespace().equals("urn:xmpp:attention:0")) {
793 message.addPayload(el);
794 }
795 if (el.getName().equals("Description") && el.getNamespace() != null && el.getNamespace().equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#")) {
796 message.addPayload(el);
797 }
798 }
799 if (conversationMultiMode) {
800 final var mucOptions = conversation.getMucOptions();
801 final var occupantId =
802 mucOptions.occupantId() ? packet.getExtension(OccupantId.class) : null;
803 if (occupantId != null) {
804 message.setOccupantId(occupantId.getId());
805 }
806 message.setMucUser(mucOptions.findUserByFullJid(counterpart));
807 final Jid fallback = mucOptions.getTrueCounterpart(counterpart);
808 Jid trueCounterpart;
809 if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL) {
810 trueCounterpart = message.getTrueCounterpart();
811 } else if (query != null && query.safeToExtractTrueCounterpart()) {
812 trueCounterpart = getTrueCounterpart(mucUserElement, fallback);
813 } else {
814 trueCounterpart = fallback;
815 }
816 if (trueCounterpart != null && isTypeGroupChat) {
817 if (trueCounterpart.asBareJid().equals(account.getJid().asBareJid())) {
818 status = isTypeGroupChat ? Message.STATUS_SEND_RECEIVED : Message.STATUS_SEND;
819 } else {
820 status = Message.STATUS_RECEIVED;
821 message.setCarbon(false);
822 }
823 }
824 message.setStatus(status);
825 message.setTrueCounterpart(trueCounterpart);
826 if (!isTypeGroupChat) {
827 message.setType(Message.TYPE_PRIVATE);
828 }
829 } else {
830 updateLastseen(account, from);
831 }
832
833 if (replacementId != null && mXmppConnectionService.allowMessageCorrection()) {
834 final Message replacedMessage = conversation.findMessageWithRemoteIdAndCounterpart(replacementId, counterpart);
835 if (replacedMessage != null) {
836 final boolean fingerprintsMatch = replacedMessage.getFingerprint() == null
837 || replacedMessage.getFingerprint().equals(message.getFingerprint());
838 final boolean trueCountersMatch = replacedMessage.getTrueCounterpart() != null
839 && message.getTrueCounterpart() != null
840 && replacedMessage.getTrueCounterpart().asBareJid().equals(message.getTrueCounterpart().asBareJid());
841 final boolean occupantIdMatch =
842 replacedMessage.getOccupantId() != null
843 && replacedMessage
844 .getOccupantId()
845 .equals(message.getOccupantId());
846 final boolean mucUserMatches = query == null && replacedMessage.sameMucUser(message); //can not be checked when using mam
847 final boolean duplicate = conversation.hasDuplicateMessage(message);
848 if (fingerprintsMatch && (trueCountersMatch || occupantIdMatch || !conversationMultiMode || mucUserMatches) && !duplicate) {
849 synchronized (replacedMessage) {
850 final String uuid = replacedMessage.getUuid();
851 replacedMessage.setUuid(UUID.randomUUID().toString());
852 replacedMessage.setBody(message.getBody());
853 replacedMessage.setSubject(message.getSubject());
854 replacedMessage.setThread(message.getThread());
855 replacedMessage.putEdited(replacedMessage.getRemoteMsgId(), replacedMessage.getServerMsgId());
856 replacedMessage.setRemoteMsgId(remoteMsgId);
857 if (replaceElement != null && !replaceElement.getName().equals("replace")) {
858 mXmppConnectionService.getFileBackend().deleteFile(replacedMessage);
859 mXmppConnectionService.evictPreview(message.getUuid());
860 List<Element> thumbs = replacedMessage.getFileParams() != null ? replacedMessage.getFileParams().getThumbnails() : null;
861 if (thumbs != null && !thumbs.isEmpty()) {
862 for (Element thumb : thumbs) {
863 Uri uri = Uri.parse(thumb.getAttribute("uri"));
864 if (uri.getScheme().equals("cid")) {
865 Cid cid = BobTransfer.cid(uri);
866 if (cid == null) continue;
867 DownloadableFile f = mXmppConnectionService.getFileForCid(cid);
868 if (f != null) {
869 mXmppConnectionService.evictPreview(f);
870 f.delete();
871 }
872 }
873 }
874 }
875 replacedMessage.clearPayloads();
876 replacedMessage.setFileParams(null);
877 replacedMessage.addPayload(replaceElement);
878 }
879 if (replacedMessage.getServerMsgId() == null || message.getServerMsgId() != null) {
880 replacedMessage.setServerMsgId(message.getServerMsgId());
881 }
882 replacedMessage.setEncryption(message.getEncryption());
883 if (replacedMessage.getStatus() == Message.STATUS_RECEIVED) {
884 replacedMessage.markUnread();
885 }
886 extractChatState(mXmppConnectionService.find(account, counterpart.asBareJid()), isTypeGroupChat, packet);
887 mXmppConnectionService.updateMessage(replacedMessage, uuid);
888 if (mXmppConnectionService.confirmMessages()
889 && replacedMessage.getStatus() == Message.STATUS_RECEIVED
890 && (replacedMessage.trusted() || replacedMessage.isPrivateMessage()) //TODO do we really want to send receipts for all PMs?
891 && remoteMsgId != null
892 && !selfAddressed
893 && !isTypeGroupChat) {
894 processMessageReceipts(account, packet, remoteMsgId, query);
895 }
896 if (replacedMessage.getEncryption() == Message.ENCRYPTION_PGP) {
897 conversation.getAccount().getPgpDecryptionService().discard(replacedMessage);
898 conversation.getAccount().getPgpDecryptionService().decrypt(replacedMessage, false);
899 }
900 }
901 mXmppConnectionService.getNotificationService().updateNotification();
902 return;
903 } else {
904 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": received message correction but verification didn't check out");
905 }
906 } else if (message.getBody() == null || message.getBody().equals("") || message.getBody().equals(" ")) {
907 return;
908 }
909 if (replaceElement != null && !replaceElement.getName().equals("replace")) return;
910 }
911
912 boolean checkForDuplicates = (isTypeGroupChat && packet.hasChild("delay", "urn:xmpp:delay"))
913 || message.isPrivateMessage()
914 || message.getServerMsgId() != null
915 || (query == null && mXmppConnectionService.getMessageArchiveService().isCatchupInProgress(conversation));
916 if (checkForDuplicates) {
917 final Message duplicate = conversation.findDuplicateMessage(message);
918 if (duplicate != null) {
919 final boolean serverMsgIdUpdated;
920 if (duplicate.getStatus() != Message.STATUS_RECEIVED
921 && duplicate.getUuid().equals(message.getRemoteMsgId())
922 && duplicate.getServerMsgId() == null
923 && message.getServerMsgId() != null) {
924 duplicate.setServerMsgId(message.getServerMsgId());
925 if (mXmppConnectionService.databaseBackend.updateMessage(duplicate, false)) {
926 serverMsgIdUpdated = true;
927 } else {
928 serverMsgIdUpdated = false;
929 Log.e(Config.LOGTAG, "failed to update message");
930 }
931 } else {
932 serverMsgIdUpdated = false;
933 }
934 Log.d(Config.LOGTAG, "skipping duplicate message with " + message.getCounterpart() + ". serverMsgIdUpdated=" + serverMsgIdUpdated);
935 return;
936 }
937 }
938
939 if (query != null && query.getPagingOrder() == MessageArchiveService.PagingOrder.REVERSE) {
940 conversation.prepend(query.getActualInThisQuery(), message);
941 } else {
942 conversation.add(message);
943 }
944 if (query != null) {
945 query.incrementActualMessageCount();
946 }
947
948 if (query == null || query.isCatchup()) { //either no mam or catchup
949 if (status == Message.STATUS_SEND || status == Message.STATUS_SEND_RECEIVED) {
950 mXmppConnectionService.markRead(conversation);
951 if (query == null) {
952 activateGracePeriod(account);
953 }
954 } else {
955 message.markUnread();
956 notify = true;
957 }
958 }
959
960 if (message.getEncryption() == Message.ENCRYPTION_PGP) {
961 notify = conversation.getAccount().getPgpDecryptionService().decrypt(message, notify);
962 } else if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL_NOT_FOR_THIS_DEVICE || message.getEncryption() == Message.ENCRYPTION_AXOLOTL_FAILED) {
963 notify = false;
964 }
965
966 if (query == null) {
967 extractChatState(mXmppConnectionService.find(account, counterpart.asBareJid()), isTypeGroupChat, packet);
968 mXmppConnectionService.updateConversationUi();
969 }
970
971 if (mXmppConnectionService.confirmMessages()
972 && message.getStatus() == Message.STATUS_RECEIVED
973 && (message.trusted() || message.isPrivateMessage())
974 && remoteMsgId != null
975 && !selfAddressed
976 && !isTypeGroupChat) {
977 processMessageReceipts(account, packet, remoteMsgId, query);
978 }
979
980 if (message.getFileParams() != null) {
981 for (Cid cid : message.getFileParams().getCids()) {
982 File f = mXmppConnectionService.getFileForCid(cid);
983 if (f != null && f.canRead()) {
984 message.setRelativeFilePath(f.getAbsolutePath());
985 mXmppConnectionService.getFileBackend().updateFileParams(message, null, false);
986 break;
987 }
988 }
989 }
990
991 mXmppConnectionService.databaseBackend.createMessage(message);
992
993 final HttpConnectionManager manager = this.mXmppConnectionService.getHttpConnectionManager();
994 if (message.getRelativeFilePath() == null && message.trusted() && message.treatAsDownloadable() && manager.getAutoAcceptFileSize() > 0) {
995 if (message.getOob() != null && "cid".equalsIgnoreCase(message.getOob().getScheme())) {
996 try {
997 BobTransfer transfer = new BobTransfer.ForMessage(message, mXmppConnectionService);
998 message.setTransferable(transfer);
999 transfer.start();
1000 } catch (URISyntaxException e) {
1001 Log.d(Config.LOGTAG, "BobTransfer failed to parse URI");
1002 }
1003 } else {
1004 manager.createNewDownloadConnection(message);
1005 }
1006 } else if (notify) {
1007 if (query != null && query.isCatchup()) {
1008 mXmppConnectionService.getNotificationService().pushFromBacklog(message);
1009 } else {
1010 mXmppConnectionService.getNotificationService().push(message);
1011 }
1012 }
1013 } else if (!packet.hasChild("body")) { //no body
1014
1015 final Conversation conversation = mXmppConnectionService.find(account, from.asBareJid());
1016 if (axolotlEncrypted != null) {
1017 Jid origin;
1018 if (conversation != null && conversation.getMode() == Conversation.MODE_MULTI) {
1019 final Jid fallback = conversation.getMucOptions().getTrueCounterpart(counterpart);
1020 origin = getTrueCounterpart(query != null ? mucUserElement : null, fallback);
1021 if (origin == null) {
1022 Log.d(Config.LOGTAG, "omemo key transport message in anonymous conference received");
1023 return;
1024 }
1025 } else if (isTypeGroupChat) {
1026 return;
1027 } else {
1028 origin = from;
1029 }
1030 try {
1031 final XmppAxolotlMessage xmppAxolotlMessage = XmppAxolotlMessage.fromElement(axolotlEncrypted, origin.asBareJid());
1032 account.getAxolotlService().processReceivingKeyTransportMessage(xmppAxolotlMessage, query != null);
1033 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": omemo key transport message received from " + origin);
1034 } catch (Exception e) {
1035 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": invalid omemo key transport message received " + e.getMessage());
1036 return;
1037 }
1038 }
1039
1040 if (query == null && extractChatState(mXmppConnectionService.find(account, counterpart.asBareJid()), isTypeGroupChat, packet)) {
1041 mXmppConnectionService.updateConversationUi();
1042 }
1043
1044 if (isTypeGroupChat) {
1045 if (packet.hasChild("subject") && !packet.hasChild("thread")) { // We already know it has no body per above
1046 if (conversation != null && conversation.getMode() == Conversation.MODE_MULTI) {
1047 conversation.setHasMessagesLeftOnServer(conversation.countMessages() > 0);
1048 final LocalizedContent subject = packet.findInternationalizedChildContentInDefaultNamespace("subject");
1049 if (subject != null && conversation.getMucOptions().setSubject(subject.content)) {
1050 mXmppConnectionService.updateConversation(conversation);
1051 }
1052 mXmppConnectionService.updateConversationUi();
1053 return;
1054 }
1055 }
1056 }
1057 if (conversation != null && mucUserElement != null && InvalidJid.hasValidFrom(packet) && from.isBareJid()) {
1058 for (Element child : mucUserElement.getChildren()) {
1059 if ("status".equals(child.getName())) {
1060 try {
1061 int code = Integer.parseInt(child.getAttribute("code"));
1062 if ((code >= 170 && code <= 174) || (code >= 102 && code <= 104)) {
1063 mXmppConnectionService.fetchConferenceConfiguration(conversation);
1064 break;
1065 }
1066 } catch (Exception e) {
1067 //ignored
1068 }
1069 } else if ("item".equals(child.getName())) {
1070 MucOptions.User user = AbstractParser.parseItem(conversation, child);
1071 Log.d(Config.LOGTAG, account.getJid() + ": changing affiliation for "
1072 + user.getRealJid() + " to " + user.getAffiliation() + " in "
1073 + conversation.getJid().asBareJid());
1074 if (!user.realJidMatchesAccount()) {
1075 boolean isNew = conversation.getMucOptions().updateUser(user);
1076 mXmppConnectionService.getAvatarService().clear(conversation);
1077 mXmppConnectionService.updateMucRosterUi();
1078 mXmppConnectionService.updateConversationUi();
1079 Contact contact = user.getContact();
1080 if (!user.getAffiliation().ranks(MucOptions.Affiliation.MEMBER)) {
1081 Jid jid = user.getRealJid();
1082 List<Jid> cryptoTargets = conversation.getAcceptedCryptoTargets();
1083 if (cryptoTargets.remove(user.getRealJid())) {
1084 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": removed " + jid + " from crypto targets of " + conversation.getName());
1085 conversation.setAcceptedCryptoTargets(cryptoTargets);
1086 mXmppConnectionService.updateConversation(conversation);
1087 }
1088 } else if (isNew
1089 && user.getRealJid() != null
1090 && conversation.getMucOptions().isPrivateAndNonAnonymous()
1091 && (contact == null || !contact.mutualPresenceSubscription())
1092 && account.getAxolotlService().hasEmptyDeviceList(user.getRealJid())) {
1093 account.getAxolotlService().fetchDeviceIds(user.getRealJid());
1094 }
1095 }
1096 }
1097 }
1098 }
1099 if (!isTypeGroupChat) {
1100 for (Element child : packet.getChildren()) {
1101 if (Namespace.JINGLE_MESSAGE.equals(child.getNamespace()) && JINGLE_MESSAGE_ELEMENT_NAMES.contains(child.getName())) {
1102 final String action = child.getName();
1103 final String sessionId = child.getAttribute("id");
1104 if (sessionId == null) {
1105 break;
1106 }
1107 if (query == null && offlineMessagesRetrieved) {
1108 if (serverMsgId == null) {
1109 serverMsgId = extractStanzaId(account, packet);
1110 }
1111 mXmppConnectionService
1112 .getJingleConnectionManager()
1113 .deliverMessage(
1114 account,
1115 packet.getTo(),
1116 packet.getFrom(),
1117 child,
1118 remoteMsgId,
1119 serverMsgId,
1120 timestamp);
1121 final Contact contact = account.getRoster().getContact(from);
1122 // this is the same condition that is found in JingleRtpConnection for
1123 // the 'ringing' response. Responding with delivery receipts predates
1124 // the 'ringing' spec'd
1125 final boolean sendReceipts =
1126 (mXmppConnectionService.confirmMessages()
1127 && contact.showInContactList())
1128 || Config.JINGLE_MESSAGE_INIT_STRICT_OFFLINE_CHECK;
1129 if (remoteMsgId != null && !contact.isSelf() && sendReceipts) {
1130 processMessageReceipts(account, packet, remoteMsgId, null);
1131 }
1132 } else if ((query != null && query.isCatchup()) || !offlineMessagesRetrieved) {
1133 if ("propose".equals(action)) {
1134 final Element description = child.findChild("description");
1135 final String namespace =
1136 description == null ? null : description.getNamespace();
1137 if (Namespace.JINGLE_APPS_RTP.equals(namespace)) {
1138 final Conversation c =
1139 mXmppConnectionService.findOrCreateConversation(
1140 account, counterpart.asBareJid(), false, false);
1141 final Message preExistingMessage =
1142 c.findRtpSession(sessionId, status);
1143 if (preExistingMessage != null) {
1144 preExistingMessage.setServerMsgId(serverMsgId);
1145 mXmppConnectionService.updateMessage(preExistingMessage);
1146 break;
1147 }
1148 final Message message =
1149 new Message(
1150 c, status, Message.TYPE_RTP_SESSION, sessionId);
1151 message.setServerMsgId(serverMsgId);
1152 message.setTime(timestamp);
1153 message.setBody(new RtpSessionStatus(false, 0).toString());
1154 c.add(message);
1155 mXmppConnectionService.databaseBackend.createMessage(message);
1156 }
1157 } else if ("proceed".equals(action)) {
1158 // status needs to be flipped to find the original propose
1159 final Conversation c =
1160 mXmppConnectionService.findOrCreateConversation(
1161 account, counterpart.asBareJid(), false, false);
1162 final int s =
1163 packet.fromAccount(account)
1164 ? Message.STATUS_RECEIVED
1165 : Message.STATUS_SEND;
1166 final Message message = c.findRtpSession(sessionId, s);
1167 if (message != null) {
1168 message.setBody(new RtpSessionStatus(true, 0).toString());
1169 if (serverMsgId != null) {
1170 message.setServerMsgId(serverMsgId);
1171 }
1172 message.setTime(timestamp);
1173 mXmppConnectionService.updateMessage(message, true);
1174 } else {
1175 Log.d(
1176 Config.LOGTAG,
1177 "unable to find original rtp session message for received propose");
1178 }
1179
1180 } else if ("finish".equals(action)) {
1181 Log.d(
1182 Config.LOGTAG,
1183 "received JMI 'finish' during MAM catch-up. Can be used to update success/failure and duration");
1184 }
1185 } else {
1186 //MAM reloads (non catchups
1187 if ("propose".equals(action)) {
1188 final Element description = child.findChild("description");
1189 final String namespace = description == null ? null : description.getNamespace();
1190 if (Namespace.JINGLE_APPS_RTP.equals(namespace)) {
1191 final Conversation c = mXmppConnectionService.findOrCreateConversation(account, counterpart.asBareJid(), false, false);
1192 final Message preExistingMessage = c.findRtpSession(sessionId, status);
1193 if (preExistingMessage != null) {
1194 preExistingMessage.setServerMsgId(serverMsgId);
1195 mXmppConnectionService.updateMessage(preExistingMessage);
1196 break;
1197 }
1198 final Message message = new Message(
1199 c,
1200 status,
1201 Message.TYPE_RTP_SESSION,
1202 sessionId
1203 );
1204 message.setServerMsgId(serverMsgId);
1205 message.setTime(timestamp);
1206 message.setBody(new RtpSessionStatus(true, 0).toString());
1207 if (query.getPagingOrder() == MessageArchiveService.PagingOrder.REVERSE) {
1208 c.prepend(query.getActualInThisQuery(), message);
1209 } else {
1210 c.add(message);
1211 }
1212 query.incrementActualMessageCount();
1213 mXmppConnectionService.databaseBackend.createMessage(message);
1214 }
1215 }
1216 }
1217 break;
1218 }
1219 }
1220 }
1221 }
1222
1223 Element received = packet.findChild("received", "urn:xmpp:chat-markers:0");
1224 if (received == null) {
1225 received = packet.findChild("received", "urn:xmpp:receipts");
1226 }
1227 if (received != null) {
1228 String id = received.getAttribute("id");
1229 if (packet.fromAccount(account)) {
1230 if (query != null && id != null && packet.getTo() != null) {
1231 query.removePendingReceiptRequest(new ReceiptRequest(packet.getTo(), id));
1232 }
1233 } else if (id != null) {
1234 if (id.startsWith(JingleRtpConnection.JINGLE_MESSAGE_PROPOSE_ID_PREFIX)) {
1235 final String sessionId = id.substring(JingleRtpConnection.JINGLE_MESSAGE_PROPOSE_ID_PREFIX.length());
1236 mXmppConnectionService.getJingleConnectionManager()
1237 .updateProposedSessionDiscovered(account, from, sessionId, JingleConnectionManager.DeviceDiscoveryState.DISCOVERED);
1238 } else {
1239 mXmppConnectionService.markMessage(account, from.asBareJid(), id, Message.STATUS_SEND_RECEIVED);
1240 }
1241 }
1242 }
1243 final Element displayed = packet.findChild("displayed", "urn:xmpp:chat-markers:0");
1244 if (displayed != null) {
1245 final String id = displayed.getAttribute("id");
1246 // TODO we don’t even use 'sender' any more. Remove this!
1247 final Jid sender = InvalidJid.getNullForInvalid(displayed.getAttributeAsJid("sender"));
1248 if (packet.fromAccount(account) && !selfAddressed) {
1249 final Conversation c =
1250 mXmppConnectionService.find(account, counterpart.asBareJid());
1251 final Message message =
1252 (c == null || id == null) ? null : c.findReceivedWithRemoteId(id);
1253 if (message != null && (query == null || query.isCatchup())) {
1254 mXmppConnectionService.markReadUpTo(c, message);
1255 }
1256 if (query == null) {
1257 activateGracePeriod(account);
1258 }
1259 } else if (isTypeGroupChat) {
1260 final Conversation conversation = mXmppConnectionService.find(account, counterpart.asBareJid());
1261 final Message message;
1262 if (conversation != null && id != null) {
1263 if (sender != null) {
1264 message = conversation.findMessageWithRemoteId(id, sender);
1265 } else {
1266 message = conversation.findMessageWithServerMsgId(id);
1267 }
1268 } else {
1269 message = null;
1270 }
1271 if (message != null) {
1272 // TODO use occupantId to extract true counterpart from presence
1273 final Jid fallback = conversation.getMucOptions().getTrueCounterpart(counterpart);
1274 // TODO try to externalize mucTrueCounterpart
1275 final Jid trueJid = getTrueCounterpart((query != null && query.safeToExtractTrueCounterpart()) ? mucUserElement : null, fallback);
1276 final boolean trueJidMatchesAccount = account.getJid().asBareJid().equals(trueJid == null ? null : trueJid.asBareJid());
1277 if (trueJidMatchesAccount || conversation.getMucOptions().isSelf(counterpart)) {
1278 if (!message.isRead() && (query == null || query.isCatchup())) { //checking if message is unread fixes race conditions with reflections
1279 mXmppConnectionService.markReadUpTo(conversation, message);
1280 }
1281 } else if (!counterpart.isBareJid() && trueJid != null) {
1282 final ReadByMarker readByMarker = ReadByMarker.from(counterpart, trueJid);
1283 if (message.addReadByMarker(readByMarker)) {
1284 final var mucOptions = conversation.getMucOptions();
1285 final var everyone = ImmutableSet.copyOf(mucOptions.getMembers(false));
1286 final var readyBy = message.getReadyByTrue();
1287 final var mStatus = message.getStatus();
1288 if (mucOptions.isPrivateAndNonAnonymous()
1289 && (mStatus == Message.STATUS_SEND_RECEIVED
1290 || mStatus == Message.STATUS_SEND)
1291 && readyBy.containsAll(everyone)) {
1292 message.setStatus(Message.STATUS_SEND_DISPLAYED);
1293 }
1294 mXmppConnectionService.updateMessage(message, false);
1295 }
1296 }
1297 }
1298 } else {
1299 final Message displayedMessage = mXmppConnectionService.markMessage(account, from.asBareJid(), id, Message.STATUS_SEND_DISPLAYED);
1300 Message message = displayedMessage == null ? null : displayedMessage.prev();
1301 while (message != null
1302 && message.getStatus() == Message.STATUS_SEND_RECEIVED
1303 && message.getTimeSent() < displayedMessage.getTimeSent()) {
1304 mXmppConnectionService.markMessage(message, Message.STATUS_SEND_DISPLAYED);
1305 message = message.prev();
1306 }
1307 if (displayedMessage != null && selfAddressed) {
1308 dismissNotification(account, counterpart, query, id);
1309 }
1310 }
1311 }
1312
1313 if (reactions != null) {
1314 final String reactingTo = reactions.getId();
1315 final Conversation conversation =
1316 mXmppConnectionService.find(account, counterpart.asBareJid());
1317
1318 if (conversation != null) {
1319 if (isTypeGroupChat && conversation.getMode() == Conversational.MODE_MULTI) {
1320 final var mucOptions = conversation.getMucOptions();
1321 final var occupant =
1322 mucOptions.occupantId() ? packet.getExtension(OccupantId.class) : null;
1323 final var occupantId = occupant == null ? null : occupant.getId();
1324 final var message = conversation.findMessageWithServerMsgId(reactingTo);
1325 // TODO use occupant id for isSelf assessment
1326 final boolean isReceived = !mucOptions.isSelf(counterpart);
1327 if (occupantId != null && message != null) {
1328 final var combinedReactions =
1329 appendReactions ? Reaction.append(message.getReactions(), reactions.getReactions(), isReceived, counterpart, null, occupantId) :
1330 Reaction.withOccupantId(
1331 message.getReactions(),
1332 reactions.getReactions(),
1333 isReceived,
1334 counterpart,
1335 null,
1336 occupantId);
1337 message.setReactions(combinedReactions);
1338 mXmppConnectionService.updateMessage(message, false);
1339 } else {
1340 Log.d(Config.LOGTAG,"not found occupant or message");
1341 }
1342 } else if (conversation.getMode() == Conversational.MODE_SINGLE) {
1343 final var message = conversation.findMessageWithUuidOrRemoteId(reactingTo);
1344 final boolean isReceived;
1345 final Jid reactionFrom;
1346 if (packet.fromAccount(account)) {
1347 isReceived = false;
1348 reactionFrom = account.getJid().asBareJid();
1349 } else {
1350 isReceived = true;
1351 reactionFrom = counterpart;
1352 }
1353 packet.fromAccount(account);
1354 if (message != null) {
1355 final var combinedReactions =
1356 appendReactions ? Reaction.append(message.getReactions(), reactions.getReactions(), isReceived, reactionFrom, null, null) :
1357 Reaction.withFrom(
1358 message.getReactions(),
1359 reactions.getReactions(),
1360 isReceived,
1361 reactionFrom);
1362 message.setReactions(combinedReactions);
1363 mXmppConnectionService.updateMessage(message, false);
1364 }
1365 }
1366 }
1367 }
1368
1369 final Element event = original.findChild("event", "http://jabber.org/protocol/pubsub#event");
1370 if (event != null && InvalidJid.hasValidFrom(original) && original.getFrom().isBareJid()) {
1371 if (event.hasChild("items")) {
1372 parseEvent(event, original.getFrom(), account);
1373 } else if (event.hasChild("delete")) {
1374 parseDeleteEvent(event, original.getFrom(), account);
1375 } else if (event.hasChild("purge")) {
1376 parsePurgeEvent(event, original.getFrom(), account);
1377 }
1378 }
1379
1380 final String nick = packet.findChildContent("nick", Namespace.NICK);
1381 if (nick != null && InvalidJid.hasValidFrom(original)) {
1382 if (mXmppConnectionService.isMuc(account, from)) {
1383 return;
1384 }
1385 final Contact contact = account.getRoster().getContact(from);
1386 if (contact.setPresenceName(nick)) {
1387 mXmppConnectionService.syncRoster(account);
1388 mXmppConnectionService.getAvatarService().clear(contact);
1389 }
1390 }
1391 }
1392
1393 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) {
1394 final var extension = original.getExtension(clazz);
1395 final var forwarded = extension == null ? null : extension.getExtension(Forwarded.class);
1396 if (forwarded == null) {
1397 return null;
1398 }
1399 final Long timestamp = AbstractParser.parseTimestamp(forwarded, null);
1400 final var forwardedMessage = forwarded.getMessage();
1401 if (forwardedMessage == null) {
1402 return null;
1403 }
1404 return new Pair<>(forwardedMessage,timestamp);
1405 }
1406
1407 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) {
1408 final Element wrapper = original.findChild(name, namespace);
1409 final var forwardedElement = wrapper == null ? null : wrapper.findChild("forwarded",Namespace.FORWARD);
1410 if (forwardedElement instanceof Forwarded forwarded) {
1411 final Long timestamp = AbstractParser.parseTimestamp(forwarded, null);
1412 final var forwardedMessage = forwarded.getMessage();
1413 if (forwardedMessage == null) {
1414 return null;
1415 }
1416 return new Pair<>(forwardedMessage,timestamp);
1417 }
1418 return null;
1419 }
1420
1421 private void dismissNotification(Account account, Jid counterpart, MessageArchiveService.Query query, final String id) {
1422 final Conversation conversation = mXmppConnectionService.find(account, counterpart.asBareJid());
1423 if (conversation != null && (query == null || query.isCatchup())) {
1424 final String displayableId = conversation.findMostRecentRemoteDisplayableId();
1425 if (displayableId != null && displayableId.equals(id)) {
1426 mXmppConnectionService.markRead(conversation);
1427 } else {
1428 Log.w(Config.LOGTAG, account.getJid().asBareJid() + ": received dismissing display marker that did not match our last id in that conversation");
1429 }
1430 }
1431 }
1432
1433 private void processMessageReceipts(final Account account, final im.conversations.android.xmpp.model.stanza.Message packet, final String remoteMsgId, MessageArchiveService.Query query) {
1434 final boolean markable = packet.hasChild("markable", "urn:xmpp:chat-markers:0");
1435 final boolean request = packet.hasChild("request", "urn:xmpp:receipts");
1436 if (query == null) {
1437 final ArrayList<String> receiptsNamespaces = new ArrayList<>();
1438 if (markable) {
1439 receiptsNamespaces.add("urn:xmpp:chat-markers:0");
1440 }
1441 if (request) {
1442 receiptsNamespaces.add("urn:xmpp:receipts");
1443 }
1444 if (receiptsNamespaces.size() > 0) {
1445 final var receipt = mXmppConnectionService.getMessageGenerator().received(account,
1446 packet.getFrom(),
1447 remoteMsgId,
1448 receiptsNamespaces,
1449 packet.getType());
1450 mXmppConnectionService.sendMessagePacket(account, receipt);
1451 }
1452 } else if (query.isCatchup()) {
1453 if (request) {
1454 query.addPendingReceiptRequest(new ReceiptRequest(packet.getFrom(), remoteMsgId));
1455 }
1456 }
1457 }
1458
1459 private void activateGracePeriod(Account account) {
1460 long duration = mXmppConnectionService.getLongPreference("grace_period_length", R.integer.grace_period) * 1000;
1461 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": activating grace period till " + TIME_FORMAT.format(new Date(System.currentTimeMillis() + duration)));
1462 account.activateGracePeriod(duration);
1463 }
1464
1465 private class Invite {
1466 final Jid jid;
1467 final String password;
1468 final boolean direct;
1469 final Jid inviter;
1470
1471 Invite(Jid jid, String password, boolean direct, Jid inviter) {
1472 this.jid = jid;
1473 this.password = password;
1474 this.direct = direct;
1475 this.inviter = inviter;
1476 }
1477
1478 public boolean execute(final Account account) {
1479 if (this.jid == null) {
1480 return false;
1481 }
1482 final Contact contact = this.inviter != null ? account.getRoster().getContact(this.inviter) : null;
1483 if (contact != null && contact.isBlocked()) {
1484 Log.d(Config.LOGTAG,account.getJid().asBareJid()+": ignore invite from "+contact.getJid()+" because contact is blocked");
1485 return false;
1486 }
1487 final Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, jid, true, false);
1488 conversation.setAttribute("inviter", inviter.toEscapedString());
1489 if (conversation.getMucOptions().online()) {
1490 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": received invite to " + jid + " but muc is considered to be online");
1491 mXmppConnectionService.mucSelfPingAndRejoin(conversation);
1492 } else {
1493 conversation.getMucOptions().setPassword(password);
1494 mXmppConnectionService.databaseBackend.updateConversation(conversation);
1495 mXmppConnectionService.joinMuc(conversation, contact != null && contact.showInContactList());
1496 mXmppConnectionService.updateConversationUi();
1497 }
1498 return true;
1499 }
1500 }
1501
1502 private static int parseInt(String value) {
1503 try {
1504 return Integer.parseInt(value);
1505 } catch (NumberFormatException e) {
1506 return 0;
1507 }
1508 }
1509}