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