MessageParser.java

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