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