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 //TODO this would be the place to change the body after something like mod_pastebin
456 if (mXmppConnectionService.markMessage(conversation, remoteMsgId, status, serverMsgId)) {
457 return;
458 } else if (remoteMsgId == null || Config.IGNORE_ID_REWRITE_IN_MUC) {
459 LocalizedContent localizedBody = packet.getBody();
460 if (localizedBody != null) {
461 Message message = conversation.findSentMessageWithBody(localizedBody.content);
462 if (message != null) {
463 mXmppConnectionService.markMessage(message, status);
464 return;
465 }
466 }
467 }
468 } else {
469 status = Message.STATUS_RECEIVED;
470 }
471 }
472 final Message message;
473 if (xP1S3url != null) {
474 message = new Message(conversation, xP1S3url.toString(), Message.ENCRYPTION_NONE, status);
475 message.setOob(true);
476 if (CryptoHelper.isPgpEncryptedUrl(xP1S3url.toString())) {
477 message.setEncryption(Message.ENCRYPTION_DECRYPTED);
478 }
479 } else if (pgpEncrypted != null && Config.supportOpenPgp()) {
480 message = new Message(conversation, pgpEncrypted, Message.ENCRYPTION_PGP, status);
481 } else if (axolotlEncrypted != null && Config.supportOmemo()) {
482 Jid origin;
483 Set<Jid> fallbacksBySourceId = Collections.emptySet();
484 if (conversationMultiMode) {
485 final Jid fallback = conversation.getMucOptions().getTrueCounterpart(counterpart);
486 origin = getTrueCounterpart(query != null ? mucUserElement : null, fallback);
487 if (origin == null) {
488 try {
489 fallbacksBySourceId = account.getAxolotlService().findCounterpartsBySourceId(XmppAxolotlMessage.parseSourceId(axolotlEncrypted));
490 } catch (IllegalArgumentException e) {
491 //ignoring
492 }
493 }
494 if (origin == null && fallbacksBySourceId.size() == 0) {
495 Log.d(Config.LOGTAG, "axolotl message in anonymous conference received and no possible fallbacks");
496 return;
497 }
498 } else {
499 fallbacksBySourceId = Collections.emptySet();
500 origin = from;
501 }
502
503 final boolean liveMessage = query == null && !isTypeGroupChat && mucUserElement == null;
504 final boolean checkedForDuplicates = liveMessage || (serverMsgId != null && remoteMsgId != null && !conversation.possibleDuplicate(serverMsgId, remoteMsgId));
505
506 if (origin != null) {
507 message = parseAxolotlChat(axolotlEncrypted, origin, conversation, status, checkedForDuplicates,query != null);
508 } else {
509 Message trial = null;
510 for (Jid fallback : fallbacksBySourceId) {
511 trial = parseAxolotlChat(axolotlEncrypted, fallback, conversation, status, checkedForDuplicates && fallbacksBySourceId.size() == 1, query != null);
512 if (trial != null) {
513 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": decoded muc message using fallback");
514 origin = fallback;
515 break;
516 }
517 }
518 message = trial;
519 }
520 if (message == null) {
521 if (query == null && extractChatState(mXmppConnectionService.find(account, counterpart.asBareJid()), isTypeGroupChat, packet)) {
522 mXmppConnectionService.updateConversationUi();
523 }
524 if (query != null && status == Message.STATUS_SEND && remoteMsgId != null) {
525 Message previouslySent = conversation.findSentMessageWithUuid(remoteMsgId);
526 if (previouslySent != null && previouslySent.getServerMsgId() == null && serverMsgId != null) {
527 previouslySent.setServerMsgId(serverMsgId);
528 mXmppConnectionService.databaseBackend.updateMessage(previouslySent, false);
529 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": encountered previously sent OMEMO message without serverId. updating...");
530 }
531 }
532 return;
533 }
534 if (conversationMultiMode) {
535 message.setTrueCounterpart(origin);
536 }
537 } else if (body == null && oobUrl != null) {
538 message = new Message(conversation, oobUrl, Message.ENCRYPTION_NONE, status);
539 message.setOob(true);
540 if (CryptoHelper.isPgpEncryptedUrl(oobUrl)) {
541 message.setEncryption(Message.ENCRYPTION_DECRYPTED);
542 }
543 } else {
544 message = new Message(conversation, body.content, Message.ENCRYPTION_NONE, status);
545 if (body.count > 1) {
546 message.setBodyLanguage(body.language);
547 }
548 }
549
550 message.setCounterpart(counterpart);
551 message.setRemoteMsgId(remoteMsgId);
552 message.setServerMsgId(serverMsgId);
553 message.setCarbon(isCarbon);
554 message.setTime(timestamp);
555 if (body != null && body.content != null && body.content.equals(oobUrl)) {
556 message.setOob(true);
557 if (CryptoHelper.isPgpEncryptedUrl(oobUrl)) {
558 message.setEncryption(Message.ENCRYPTION_DECRYPTED);
559 }
560 }
561 message.markable = packet.hasChild("markable", "urn:xmpp:chat-markers:0");
562 if (conversationMultiMode) {
563 message.setMucUser(conversation.getMucOptions().findUserByFullJid(counterpart));
564 final Jid fallback = conversation.getMucOptions().getTrueCounterpart(counterpart);
565 Jid trueCounterpart;
566 if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL) {
567 trueCounterpart = message.getTrueCounterpart();
568 } else if (query != null && query.safeToExtractTrueCounterpart()) {
569 trueCounterpart = getTrueCounterpart(mucUserElement, fallback);
570 } else {
571 trueCounterpart = fallback;
572 }
573 if (trueCounterpart != null && isTypeGroupChat) {
574 if (trueCounterpart.asBareJid().equals(account.getJid().asBareJid())) {
575 status = isTypeGroupChat ? Message.STATUS_SEND_RECEIVED : Message.STATUS_SEND;
576 } else {
577 status = Message.STATUS_RECEIVED;
578 message.setCarbon(false);
579 }
580 }
581 message.setStatus(status);
582 message.setTrueCounterpart(trueCounterpart);
583 if (!isTypeGroupChat) {
584 message.setType(Message.TYPE_PRIVATE);
585 }
586 } else {
587 updateLastseen(account, from);
588 }
589
590 if (replacementId != null && mXmppConnectionService.allowMessageCorrection()) {
591 final Message replacedMessage = conversation.findMessageWithRemoteIdAndCounterpart(replacementId,
592 counterpart,
593 message.getStatus() == Message.STATUS_RECEIVED,
594 message.isCarbon());
595 if (replacedMessage != null) {
596 final boolean fingerprintsMatch = replacedMessage.getFingerprint() == null
597 || replacedMessage.getFingerprint().equals(message.getFingerprint());
598 final boolean trueCountersMatch = replacedMessage.getTrueCounterpart() != null
599 && message.getTrueCounterpart() != null
600 && replacedMessage.getTrueCounterpart().asBareJid().equals(message.getTrueCounterpart().asBareJid());
601 final boolean mucUserMatches = query == null && replacedMessage.sameMucUser(message); //can not be checked when using mam
602 final boolean duplicate = conversation.hasDuplicateMessage(message);
603 if (fingerprintsMatch && (trueCountersMatch || !conversationMultiMode || mucUserMatches) && !duplicate) {
604 Log.d(Config.LOGTAG, "replaced message '" + replacedMessage.getBody() + "' with '" + message.getBody() + "'");
605 synchronized (replacedMessage) {
606 final String uuid = replacedMessage.getUuid();
607 replacedMessage.setUuid(UUID.randomUUID().toString());
608 replacedMessage.setBody(message.getBody());
609 replacedMessage.putEdited(replacedMessage.getRemoteMsgId(), replacedMessage.getServerMsgId());
610 replacedMessage.setRemoteMsgId(remoteMsgId);
611 if (replacedMessage.getServerMsgId() == null || message.getServerMsgId() != null) {
612 replacedMessage.setServerMsgId(message.getServerMsgId());
613 }
614 replacedMessage.setEncryption(message.getEncryption());
615 if (replacedMessage.getStatus() == Message.STATUS_RECEIVED) {
616 replacedMessage.markUnread();
617 }
618 extractChatState(mXmppConnectionService.find(account, counterpart.asBareJid()), isTypeGroupChat, packet);
619 mXmppConnectionService.updateMessage(replacedMessage, uuid);
620 if (mXmppConnectionService.confirmMessages()
621 && replacedMessage.getStatus() == Message.STATUS_RECEIVED
622 && (replacedMessage.trusted() || replacedMessage.isPrivateMessage()) //TODO do we really want to send receipts for all PMs?
623 && remoteMsgId != null
624 && !selfAddressed
625 && !isTypeGroupChat) {
626 processMessageReceipts(account, packet, query);
627 }
628 if (replacedMessage.getEncryption() == Message.ENCRYPTION_PGP) {
629 conversation.getAccount().getPgpDecryptionService().discard(replacedMessage);
630 conversation.getAccount().getPgpDecryptionService().decrypt(replacedMessage, false);
631 }
632 }
633 mXmppConnectionService.getNotificationService().updateNotification();
634 return;
635 } else {
636 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": received message correction but verification didn't check out");
637 }
638 }
639 }
640
641 long deletionDate = mXmppConnectionService.getAutomaticMessageDeletionDate();
642 if (deletionDate != 0 && message.getTimeSent() < deletionDate) {
643 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": skipping message from " + message.getCounterpart().toString() + " because it was sent prior to our deletion date");
644 return;
645 }
646
647 boolean checkForDuplicates = (isTypeGroupChat && packet.hasChild("delay", "urn:xmpp:delay"))
648 || message.isPrivateMessage()
649 || message.getServerMsgId() != null
650 || (query == null && mXmppConnectionService.getMessageArchiveService().isCatchupInProgress(conversation));
651 if (checkForDuplicates) {
652 final Message duplicate = conversation.findDuplicateMessage(message);
653 if (duplicate != null) {
654 final boolean serverMsgIdUpdated;
655 if (duplicate.getStatus() != Message.STATUS_RECEIVED
656 && duplicate.getUuid().equals(message.getRemoteMsgId())
657 && duplicate.getServerMsgId() == null
658 && message.getServerMsgId() != null) {
659 duplicate.setServerMsgId(message.getServerMsgId());
660 if (mXmppConnectionService.databaseBackend.updateMessage(duplicate, false)) {
661 serverMsgIdUpdated = true;
662 } else {
663 serverMsgIdUpdated = false;
664 Log.e(Config.LOGTAG, "failed to update message");
665 }
666 } else {
667 serverMsgIdUpdated = false;
668 }
669 Log.d(Config.LOGTAG, "skipping duplicate message with " + message.getCounterpart() + ". serverMsgIdUpdated=" + serverMsgIdUpdated);
670 return;
671 }
672 }
673
674 if (query != null && query.getPagingOrder() == MessageArchiveService.PagingOrder.REVERSE) {
675 conversation.prepend(query.getActualInThisQuery(), message);
676 } else {
677 conversation.add(message);
678 }
679 if (query != null) {
680 query.incrementActualMessageCount();
681 }
682
683 if (query == null || query.isCatchup()) { //either no mam or catchup
684 if (status == Message.STATUS_SEND || status == Message.STATUS_SEND_RECEIVED) {
685 mXmppConnectionService.markRead(conversation);
686 if (query == null) {
687 activateGracePeriod(account);
688 }
689 } else {
690 message.markUnread();
691 notify = true;
692 }
693 }
694
695 if (message.getEncryption() == Message.ENCRYPTION_PGP) {
696 notify = conversation.getAccount().getPgpDecryptionService().decrypt(message, notify);
697 } else if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL_NOT_FOR_THIS_DEVICE || message.getEncryption() == Message.ENCRYPTION_AXOLOTL_FAILED) {
698 notify = false;
699 }
700
701 if (query == null) {
702 extractChatState(mXmppConnectionService.find(account, counterpart.asBareJid()), isTypeGroupChat, packet);
703 mXmppConnectionService.updateConversationUi();
704 }
705
706 if (mXmppConnectionService.confirmMessages()
707 && message.getStatus() == Message.STATUS_RECEIVED
708 && (message.trusted() || message.isPrivateMessage())
709 && remoteMsgId != null
710 && !selfAddressed
711 && !isTypeGroupChat) {
712 processMessageReceipts(account, packet, query);
713 }
714
715 mXmppConnectionService.databaseBackend.createMessage(message);
716 final HttpConnectionManager manager = this.mXmppConnectionService.getHttpConnectionManager();
717 if (message.trusted() && message.treatAsDownloadable() && manager.getAutoAcceptFileSize() > 0) {
718 manager.createNewDownloadConnection(message);
719 } else if (notify) {
720 if (query != null && query.isCatchup()) {
721 mXmppConnectionService.getNotificationService().pushFromBacklog(message);
722 } else {
723 mXmppConnectionService.getNotificationService().push(message);
724 }
725 }
726 } else if (!packet.hasChild("body")) { //no body
727
728 final Conversation conversation = mXmppConnectionService.find(account, from.asBareJid());
729 if (axolotlEncrypted != null) {
730 Jid origin;
731 if (conversation != null && conversation.getMode() == Conversation.MODE_MULTI) {
732 final Jid fallback = conversation.getMucOptions().getTrueCounterpart(counterpart);
733 origin = getTrueCounterpart(query != null ? mucUserElement : null, fallback);
734 if (origin == null) {
735 Log.d(Config.LOGTAG, "omemo key transport message in anonymous conference received");
736 return;
737 }
738 } else if (isTypeGroupChat) {
739 return;
740 } else {
741 origin = from;
742 }
743 try {
744 final XmppAxolotlMessage xmppAxolotlMessage = XmppAxolotlMessage.fromElement(axolotlEncrypted, origin.asBareJid());
745 account.getAxolotlService().processReceivingKeyTransportMessage(xmppAxolotlMessage, query != null);
746 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": omemo key transport message received from " + origin);
747 } catch (Exception e) {
748 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": invalid omemo key transport message received " + e.getMessage());
749 return;
750 }
751 }
752
753 if (query == null && extractChatState(mXmppConnectionService.find(account, counterpart.asBareJid()), isTypeGroupChat, packet)) {
754 mXmppConnectionService.updateConversationUi();
755 }
756
757 if (isTypeGroupChat) {
758 if (packet.hasChild("subject")) { //TODO usually we would want to check for lack of body; however some servers do set a body :(
759 if (conversation != null && conversation.getMode() == Conversation.MODE_MULTI) {
760 conversation.setHasMessagesLeftOnServer(conversation.countMessages() > 0);
761 final LocalizedContent subject = packet.findInternationalizedChildContentInDefaultNamespace("subject");
762 if (subject != null && conversation.getMucOptions().setSubject(subject.content)) {
763 mXmppConnectionService.updateConversation(conversation);
764 }
765 mXmppConnectionService.updateConversationUi();
766 return;
767 }
768 }
769 }
770 if (conversation != null && mucUserElement != null && InvalidJid.hasValidFrom(packet) && from.isBareJid()) {
771 for (Element child : mucUserElement.getChildren()) {
772 if ("status".equals(child.getName())) {
773 try {
774 int code = Integer.parseInt(child.getAttribute("code"));
775 if ((code >= 170 && code <= 174) || (code >= 102 && code <= 104)) {
776 mXmppConnectionService.fetchConferenceConfiguration(conversation);
777 break;
778 }
779 } catch (Exception e) {
780 //ignored
781 }
782 } else if ("item".equals(child.getName())) {
783 MucOptions.User user = AbstractParser.parseItem(conversation, child);
784 Log.d(Config.LOGTAG, account.getJid() + ": changing affiliation for "
785 + user.getRealJid() + " to " + user.getAffiliation() + " in "
786 + conversation.getJid().asBareJid());
787 if (!user.realJidMatchesAccount()) {
788 boolean isNew = conversation.getMucOptions().updateUser(user);
789 mXmppConnectionService.getAvatarService().clear(conversation);
790 mXmppConnectionService.updateMucRosterUi();
791 mXmppConnectionService.updateConversationUi();
792 Contact contact = user.getContact();
793 if (!user.getAffiliation().ranks(MucOptions.Affiliation.MEMBER)) {
794 Jid jid = user.getRealJid();
795 List<Jid> cryptoTargets = conversation.getAcceptedCryptoTargets();
796 if (cryptoTargets.remove(user.getRealJid())) {
797 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": removed " + jid + " from crypto targets of " + conversation.getName());
798 conversation.setAcceptedCryptoTargets(cryptoTargets);
799 mXmppConnectionService.updateConversation(conversation);
800 }
801 } else if (isNew
802 && user.getRealJid() != null
803 && conversation.getMucOptions().isPrivateAndNonAnonymous()
804 && (contact == null || !contact.mutualPresenceSubscription())
805 && account.getAxolotlService().hasEmptyDeviceList(user.getRealJid())) {
806 account.getAxolotlService().fetchDeviceIds(user.getRealJid());
807 }
808 }
809 }
810 }
811 }
812 }
813
814 Element received = packet.findChild("received", "urn:xmpp:chat-markers:0");
815 if (received == null) {
816 received = packet.findChild("received", "urn:xmpp:receipts");
817 }
818 if (received != null) {
819 String id = received.getAttribute("id");
820 if (packet.fromAccount(account)) {
821 if (query != null && id != null && packet.getTo() != null) {
822 query.removePendingReceiptRequest(new ReceiptRequest(packet.getTo(), id));
823 }
824 } else {
825 mXmppConnectionService.markMessage(account, from.asBareJid(), received.getAttribute("id"), Message.STATUS_SEND_RECEIVED);
826 }
827 }
828 Element displayed = packet.findChild("displayed", "urn:xmpp:chat-markers:0");
829 if (displayed != null) {
830 final String id = displayed.getAttribute("id");
831 final Jid sender = InvalidJid.getNullForInvalid(displayed.getAttributeAsJid("sender"));
832 if (packet.fromAccount(account) && !selfAddressed) {
833 dismissNotification(account, counterpart, query);
834 if (query == null) {
835 activateGracePeriod(account);
836 }
837 } else if (isTypeGroupChat) {
838 Conversation conversation = mXmppConnectionService.find(account, counterpart.asBareJid());
839 if (conversation != null && id != null && sender != null) {
840 Message message = conversation.findMessageWithRemoteId(id, sender);
841 if (message != null) {
842 final Jid fallback = conversation.getMucOptions().getTrueCounterpart(counterpart);
843 final Jid trueJid = getTrueCounterpart((query != null && query.safeToExtractTrueCounterpart()) ? mucUserElement : null, fallback);
844 final boolean trueJidMatchesAccount = account.getJid().asBareJid().equals(trueJid == null ? null : trueJid.asBareJid());
845 if (trueJidMatchesAccount || conversation.getMucOptions().isSelf(counterpart)) {
846 if (!message.isRead() && (query == null || query.isCatchup())) { //checking if message is unread fixes race conditions with reflections
847 mXmppConnectionService.markRead(conversation);
848 }
849 } else if (!counterpart.isBareJid() && trueJid != null) {
850 final ReadByMarker readByMarker = ReadByMarker.from(counterpart, trueJid);
851 if (message.addReadByMarker(readByMarker)) {
852 mXmppConnectionService.updateMessage(message, false);
853 }
854 }
855 }
856 }
857 } else {
858 final Message displayedMessage = mXmppConnectionService.markMessage(account, from.asBareJid(), id, Message.STATUS_SEND_DISPLAYED);
859 Message message = displayedMessage == null ? null : displayedMessage.prev();
860 while (message != null
861 && message.getStatus() == Message.STATUS_SEND_RECEIVED
862 && message.getTimeSent() < displayedMessage.getTimeSent()) {
863 mXmppConnectionService.markMessage(message, Message.STATUS_SEND_DISPLAYED);
864 message = message.prev();
865 }
866 if (displayedMessage != null && selfAddressed) {
867 dismissNotification(account, counterpart, query);
868 }
869 }
870 }
871
872 final Element event = original.findChild("event", "http://jabber.org/protocol/pubsub#event");
873 if (event != null && InvalidJid.hasValidFrom(original)) {
874 if (event.hasChild("items")) {
875 parseEvent(event, original.getFrom(), account);
876 } else if (event.hasChild("delete")) {
877 parseDeleteEvent(event, original.getFrom(), account);
878 } else if (event.hasChild("purge")) {
879 parsePurgeEvent(event, original.getFrom(), account);
880 }
881 }
882
883 final String nick = packet.findChildContent("nick", Namespace.NICK);
884 if (nick != null && InvalidJid.hasValidFrom(original)) {
885 Contact contact = account.getRoster().getContact(from);
886 if (contact.setPresenceName(nick)) {
887 mXmppConnectionService.getAvatarService().clear(contact);
888 }
889 }
890 }
891
892 private void dismissNotification(Account account, Jid counterpart, MessageArchiveService.Query query) {
893 Conversation conversation = mXmppConnectionService.find(account, counterpart.asBareJid());
894 if (conversation != null && (query == null || query.isCatchup())) {
895 mXmppConnectionService.markRead(conversation); //TODO only mark messages read that are older than timestamp
896 }
897 }
898
899 private void processMessageReceipts(Account account, MessagePacket packet, MessageArchiveService.Query query) {
900 final boolean markable = packet.hasChild("markable", "urn:xmpp:chat-markers:0");
901 final boolean request = packet.hasChild("request", "urn:xmpp:receipts");
902 if (query == null) {
903 final ArrayList<String> receiptsNamespaces = new ArrayList<>();
904 if (markable) {
905 receiptsNamespaces.add("urn:xmpp:chat-markers:0");
906 }
907 if (request) {
908 receiptsNamespaces.add("urn:xmpp:receipts");
909 }
910 if (receiptsNamespaces.size() > 0) {
911 MessagePacket receipt = mXmppConnectionService.getMessageGenerator().received(account,
912 packet,
913 receiptsNamespaces,
914 packet.getType());
915 mXmppConnectionService.sendMessagePacket(account, receipt);
916 }
917 } else if (query.isCatchup()) {
918 if (request) {
919 query.addPendingReceiptRequest(new ReceiptRequest(packet.getFrom(), packet.getId()));
920 }
921 }
922 }
923
924 private void activateGracePeriod(Account account) {
925 long duration = mXmppConnectionService.getLongPreference("grace_period_length", R.integer.grace_period) * 1000;
926 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": activating grace period till " + TIME_FORMAT.format(new Date(System.currentTimeMillis() + duration)));
927 account.activateGracePeriod(duration);
928 }
929
930 private class Invite {
931 final Jid jid;
932 final String password;
933 final boolean direct;
934 final Jid inviter;
935
936 Invite(Jid jid, String password, boolean direct, Jid inviter) {
937 this.jid = jid;
938 this.password = password;
939 this.direct = direct;
940 this.inviter = inviter;
941 }
942
943 public boolean execute(Account account) {
944 if (jid != null) {
945 Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, jid, true, false);
946 if (conversation.getMucOptions().online()) {
947 Log.d(Config.LOGTAG,account.getJid().asBareJid()+": received invite to "+jid+" but muc is considered to be online");
948 mXmppConnectionService.mucSelfPingAndRejoin(conversation);
949 } else {
950 conversation.getMucOptions().setPassword(password);
951 mXmppConnectionService.databaseBackend.updateConversation(conversation);
952 final Contact contact = inviter != null ? account.getRoster().getContactFromContactList(inviter) : null;
953 mXmppConnectionService.joinMuc(conversation, contact != null && contact.mutualPresenceSubscription());
954 mXmppConnectionService.updateConversationUi();
955 }
956 return true;
957 }
958 return false;
959 }
960 }
961}