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