MessageParser.java

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