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