MessageParser.java

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