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