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