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