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