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