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);
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			Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, counterpart.toBareJid(), isTypeGroupChat, false, query);
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			} else {
478				message = new Message(conversation, body, Message.ENCRYPTION_NONE, status);
479			}
480
481			message.setCounterpart(counterpart);
482			message.setRemoteMsgId(remoteMsgId);
483			message.setServerMsgId(serverMsgId);
484			message.setCarbon(isCarbon);
485			message.setTime(timestamp);
486			message.setOob(body != null && body.equals(oobUrl));
487			message.markable = packet.hasChild("markable", "urn:xmpp:chat-markers:0");
488			if (conversationMultiMode) {
489				final Jid fallback = conversation.getMucOptions().getTrueCounterpart(counterpart);
490				Jid trueCounterpart;
491				if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL) {
492					trueCounterpart = message.getTrueCounterpart();
493				} else if (Config.PARSE_REAL_JID_FROM_MUC_MAM) {
494					trueCounterpart = getTrueCounterpart(query != null ? mucUserElement : null, fallback);
495				} else {
496					trueCounterpart = fallback;
497				}
498				if (trueCounterpart != null && trueCounterpart.toBareJid().equals(account.getJid().toBareJid())) {
499					status = isTypeGroupChat ? Message.STATUS_SEND_RECEIVED : Message.STATUS_SEND;
500				}
501				message.setStatus(status);
502				message.setTrueCounterpart(trueCounterpart);
503				if (!isTypeGroupChat) {
504					message.setType(Message.TYPE_PRIVATE);
505				}
506			} else {
507				updateLastseen(account, from);
508			}
509
510			if (replacementId != null && mXmppConnectionService.allowMessageCorrection()) {
511				Message replacedMessage = conversation.findMessageWithRemoteIdAndCounterpart(replacementId,
512						counterpart,
513						message.getStatus() == Message.STATUS_RECEIVED,
514						message.isCarbon());
515				if (replacedMessage != null) {
516					final boolean fingerprintsMatch = replacedMessage.getFingerprint() == null
517							|| replacedMessage.getFingerprint().equals(message.getFingerprint());
518					final boolean trueCountersMatch = replacedMessage.getTrueCounterpart() != null
519							&& replacedMessage.getTrueCounterpart().equals(message.getTrueCounterpart());
520					final boolean duplicate = conversation.hasDuplicateMessage(message);
521					if (fingerprintsMatch && (trueCountersMatch || !conversationMultiMode) && !duplicate) {
522						Log.d(Config.LOGTAG, "replaced message '" + replacedMessage.getBody() + "' with '" + message.getBody() + "'");
523						synchronized (replacedMessage) {
524							final String uuid = replacedMessage.getUuid();
525							replacedMessage.setUuid(UUID.randomUUID().toString());
526							replacedMessage.setBody(message.getBody());
527							replacedMessage.setEdited(replacedMessage.getRemoteMsgId());
528							replacedMessage.setRemoteMsgId(remoteMsgId);
529							replacedMessage.setEncryption(message.getEncryption());
530							if (replacedMessage.getStatus() == Message.STATUS_RECEIVED) {
531								replacedMessage.markUnread();
532							}
533							mXmppConnectionService.updateMessage(replacedMessage, uuid);
534							mXmppConnectionService.getNotificationService().updateNotification(false);
535							if (mXmppConnectionService.confirmMessages()
536									&& (replacedMessage.trusted() || replacedMessage.getType() == Message.TYPE_PRIVATE)
537									&& remoteMsgId != null
538									&& !isForwarded
539									&& !isTypeGroupChat) {
540								sendMessageReceipts(account, packet);
541							}
542							if (replacedMessage.getEncryption() == Message.ENCRYPTION_PGP) {
543								conversation.getAccount().getPgpDecryptionService().discard(replacedMessage);
544								conversation.getAccount().getPgpDecryptionService().decrypt(replacedMessage, false);
545							}
546						}
547						return;
548					} else {
549						Log.d(Config.LOGTAG,account.getJid().toBareJid()+": received message correction but verification didn't check out");
550					}
551				}
552			}
553
554			long deletionDate = mXmppConnectionService.getAutomaticMessageDeletionDate();
555			if (deletionDate != 0 && message.getTimeSent() < deletionDate) {
556				Log.d(Config.LOGTAG,account.getJid().toBareJid()+": skipping message from "+message.getCounterpart().toString()+" because it was sent prior to our deletion date");
557				return;
558			}
559
560			boolean checkForDuplicates = (isTypeGroupChat && packet.hasChild("delay","urn:xmpp:delay"))
561					|| message.getType() == Message.TYPE_PRIVATE
562					|| message.getServerMsgId() != null;
563			if (checkForDuplicates && conversation.hasDuplicateMessage(message)) {
564				Log.d(Config.LOGTAG,"skipping duplicate message from "+message.getCounterpart().toString()+" "+message.getBody());
565				return;
566			}
567
568			if (query != null && query.getPagingOrder() == MessageArchiveService.PagingOrder.REVERSE) {
569				conversation.prepend(message);
570			} else {
571				conversation.add(message);
572			}
573			if (query != null) {
574				query.incrementActualMessageCount();
575			}
576
577			if (query == null || query.isCatchup()) { //either no mam or catchup
578				if (status == Message.STATUS_SEND || status == Message.STATUS_SEND_RECEIVED) {
579					mXmppConnectionService.markRead(conversation);
580					if (query == null) {
581						activateGracePeriod(account);
582					}
583				} else {
584					message.markUnread();
585					notify = true;
586				}
587			}
588
589			if (message.getEncryption() == Message.ENCRYPTION_PGP) {
590				notify = conversation.getAccount().getPgpDecryptionService().decrypt(message, notify);
591			}
592
593			if (query == null) {
594				mXmppConnectionService.updateConversationUi();
595			}
596
597			if (mXmppConnectionService.confirmMessages()
598					&& (message.trusted() || message.getType() == Message.TYPE_PRIVATE)
599					&& remoteMsgId != null
600					&& !isForwarded
601					&& !isTypeGroupChat) {
602				sendMessageReceipts(account, packet);
603			}
604
605			if (message.getStatus() == Message.STATUS_RECEIVED
606					&& conversation.getOtrSession() != null
607					&& !conversation.getOtrSession().getSessionID().getUserID()
608					.equals(message.getCounterpart().getResourcepart())) {
609				conversation.endOtrIfNeeded();
610			}
611
612			mXmppConnectionService.databaseBackend.createMessage(message);
613			final HttpConnectionManager manager = this.mXmppConnectionService.getHttpConnectionManager();
614			if (message.trusted() && message.treatAsDownloadable() != Message.Decision.NEVER && manager.getAutoAcceptFileSize() > 0) {
615				manager.createNewDownloadConnection(message);
616			} else if (notify) {
617				if (query != null && query.isCatchup()) {
618					mXmppConnectionService.getNotificationService().pushFromBacklog(message);
619				} else if (account.getXmppConnection().isWaitingForSmCatchup()) {
620					account.getXmppConnection().incrementSmCatchupMessageCounter();
621					mXmppConnectionService.getNotificationService().pushFromBacklog(message);
622				} else {
623					mXmppConnectionService.getNotificationService().push(message);
624				}
625			}
626		} else if (!packet.hasChild("body")){ //no body
627			Conversation conversation = mXmppConnectionService.find(account, from.toBareJid());
628			if (isTypeGroupChat) {
629				if (packet.hasChild("subject")) {
630					if (conversation != null && conversation.getMode() == Conversation.MODE_MULTI) {
631						conversation.setHasMessagesLeftOnServer(conversation.countMessages() > 0);
632						String subject = packet.findChildContent("subject");
633						conversation.getMucOptions().setSubject(subject);
634						final Bookmark bookmark = conversation.getBookmark();
635						if (bookmark != null && bookmark.getBookmarkName() == null) {
636							if (bookmark.setBookmarkName(subject)) {
637								mXmppConnectionService.pushBookmarks(account);
638							}
639						}
640						mXmppConnectionService.updateConversationUi();
641						return;
642					}
643				}
644			}
645			if (conversation != null && mucUserElement != null && from.isBareJid()) {
646				for (Element child : mucUserElement.getChildren()) {
647					if ("status".equals(child.getName())) {
648						try {
649							int code = Integer.parseInt(child.getAttribute("code"));
650							if ((code >= 170 && code <= 174) || (code >= 102 && code <= 104)) {
651								mXmppConnectionService.fetchConferenceConfiguration(conversation);
652								break;
653							}
654						} catch (Exception e) {
655							//ignored
656						}
657					} else if ("item".equals(child.getName())) {
658						MucOptions.User user = AbstractParser.parseItem(conversation,child);
659						Log.d(Config.LOGTAG,account.getJid()+": changing affiliation for "
660								+user.getRealJid()+" to "+user.getAffiliation()+" in "
661								+conversation.getJid().toBareJid());
662						if (!user.realJidMatchesAccount()) {
663							conversation.getMucOptions().updateUser(user);
664							mXmppConnectionService.getAvatarService().clear(conversation);
665							mXmppConnectionService.updateMucRosterUi();
666							mXmppConnectionService.updateConversationUi();
667							if (!user.getAffiliation().ranks(MucOptions.Affiliation.MEMBER)) {
668								Jid jid = user.getRealJid();
669								List<Jid> cryptoTargets = conversation.getAcceptedCryptoTargets();
670								if (cryptoTargets.remove(user.getRealJid())) {
671									Log.d(Config.LOGTAG,account.getJid().toBareJid()+": removed "+jid+" from crypto targets of "+conversation.getName());
672									conversation.setAcceptedCryptoTargets(cryptoTargets);
673									mXmppConnectionService.updateConversation(conversation);
674								}
675							}
676						}
677					}
678				}
679			}
680		}
681
682
683
684		Element received = packet.findChild("received", "urn:xmpp:chat-markers:0");
685		if (received == null) {
686			received = packet.findChild("received", "urn:xmpp:receipts");
687		}
688		if (received != null && !packet.fromAccount(account)) {
689			mXmppConnectionService.markMessage(account, from.toBareJid(), received.getAttribute("id"), Message.STATUS_SEND_RECEIVED);
690		}
691		Element displayed = packet.findChild("displayed", "urn:xmpp:chat-markers:0");
692		if (displayed != null) {
693			if (packet.fromAccount(account)) {
694				Conversation conversation = mXmppConnectionService.find(account,counterpart.toBareJid());
695				if (conversation != null) {
696					mXmppConnectionService.markRead(conversation);
697				}
698			} else {
699				final Message displayedMessage = mXmppConnectionService.markMessage(account, from.toBareJid(), displayed.getAttribute("id"), Message.STATUS_SEND_DISPLAYED);
700				Message message = displayedMessage == null ? null : displayedMessage.prev();
701				while (message != null
702						&& message.getStatus() == Message.STATUS_SEND_RECEIVED
703						&& message.getTimeSent() < displayedMessage.getTimeSent()) {
704					mXmppConnectionService.markMessage(message, Message.STATUS_SEND_DISPLAYED);
705					message = message.prev();
706				}
707			}
708		}
709
710		Element event = original.findChild("event", "http://jabber.org/protocol/pubsub#event");
711		if (event != null) {
712			parseEvent(event, original.getFrom(), account);
713		}
714
715		String nick = packet.findChildContent("nick", "http://jabber.org/protocol/nick");
716		if (nick != null) {
717			Contact contact = account.getRoster().getContact(from);
718			contact.setPresenceName(nick);
719		}
720	}
721
722	private static Jid getTrueCounterpart(Element mucUserElement, Jid fallback) {
723		final Element item = mucUserElement == null ? null : mucUserElement.findChild("item");
724		Jid result = item == null ? null : item.getAttributeAsJid("jid");
725		return result != null ? result : fallback;
726	}
727
728	private void sendMessageReceipts(Account account, MessagePacket packet) {
729		ArrayList<String> receiptsNamespaces = new ArrayList<>();
730		if (packet.hasChild("markable", "urn:xmpp:chat-markers:0")) {
731			receiptsNamespaces.add("urn:xmpp:chat-markers:0");
732		}
733		if (packet.hasChild("request", "urn:xmpp:receipts")) {
734			receiptsNamespaces.add("urn:xmpp:receipts");
735		}
736		if (receiptsNamespaces.size() > 0) {
737			MessagePacket receipt = mXmppConnectionService.getMessageGenerator().received(account,
738					packet,
739					receiptsNamespaces,
740					packet.getType());
741			mXmppConnectionService.sendMessagePacket(account, receipt);
742		}
743	}
744
745	private static SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm:ss");
746
747	private void activateGracePeriod(Account account) {
748		long duration = mXmppConnectionService.getPreferences().getLong("race_period_length", 144) * 1000;
749		Log.d(Config.LOGTAG,account.getJid().toBareJid()+": activating grace period till "+TIME_FORMAT.format(new Date(System.currentTimeMillis() + duration)));
750		account.activateGracePeriod(duration);
751	}
752}