Conversations/src/main/java/eu/siacs/conversations/parser/MessageParser.java

653 lines
26 KiB
Java
Raw Normal View History

2014-05-14 06:56:34 -04:00
package eu.siacs.conversations.parser;
import android.text.Html;
import android.util.Log;
import android.util.Pair;
import net.java.otr4j.session.Session;
import net.java.otr4j.session.SessionStatus;
2016-06-01 18:24:37 -04:00
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
2016-06-01 18:24:37 -04:00
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Set;
2016-02-19 18:01:39 -05:00
import java.util.UUID;
import eu.siacs.conversations.Config;
2016-03-31 15:15:49 -04:00
import eu.siacs.conversations.crypto.OtrService;
import eu.siacs.conversations.crypto.axolotl.AxolotlService;
import eu.siacs.conversations.crypto.axolotl.XmppAxolotlMessage;
2014-02-28 12:46:01 -05:00
import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Bookmark;
2014-08-05 16:58:46 -04:00
import eu.siacs.conversations.entities.Contact;
2014-02-28 12:46:01 -05:00
import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.entities.MucOptions;
import eu.siacs.conversations.entities.Presence;
import eu.siacs.conversations.entities.ServiceDiscoveryResult;
import eu.siacs.conversations.http.HttpConnectionManager;
2014-12-13 06:25:52 -05:00
import eu.siacs.conversations.services.MessageArchiveService;
2014-02-28 12:46:01 -05:00
import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.utils.CryptoHelper;
2014-02-28 12:46:01 -05:00
import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xmpp.OnMessagePacketReceived;
import eu.siacs.conversations.xmpp.chatstate.ChatState;
2014-11-06 14:10:03 -05:00
import eu.siacs.conversations.xmpp.jid.Jid;
2014-08-05 16:58:46 -04:00
import eu.siacs.conversations.xmpp.pep.Avatar;
2014-03-10 14:22:13 -04:00
import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
public class MessageParser extends AbstractParser implements OnMessagePacketReceived {
private static final List<String> CLIENTS_SENDING_HTML_IN_OTR = Arrays.asList(new String[]{"Pidgin","Adium"});
2014-05-14 06:56:34 -04:00
public MessageParser(XmppConnectionService service) {
super(service);
2014-05-14 06:56:34 -04:00
}
2014-06-04 06:31:19 -04:00
private boolean extractChatState(Conversation conversation, final MessagePacket packet) {
ChatState state = ChatState.parse(packet);
if (state != null && conversation != null) {
final Account account = conversation.getAccount();
Jid from = packet.getFrom();
if (from.toBareJid().equals(account.getJid().toBareJid())) {
conversation.setOutgoingChatState(state);
if (state == ChatState.ACTIVE || state == ChatState.COMPOSING) {
mXmppConnectionService.markRead(conversation);
2016-06-01 18:24:37 -04:00
activateGracePeriod(account);
}
return false;
} else {
return conversation.setIncomingChatState(state);
}
}
return false;
}
private Message parseOtrChat(String body, Jid from, String id, Conversation conversation) {
String presence;
2014-11-06 14:10:03 -05:00
if (from.isBareJid()) {
presence = "";
} else {
2014-11-06 14:10:03 -05:00
presence = from.getResourcepart();
}
if (body.matches("^\\?OTRv\\d{1,2}\\?.*")) {
conversation.endOtrIfNeeded();
}
if (!conversation.hasValidOtrSession()) {
conversation.startOtrSession(presence,false);
2014-03-07 18:48:52 -05:00
} else {
String foreignPresence = conversation.getOtrSession().getSessionID().getUserID();
if (!foreignPresence.equals(presence)) {
conversation.endOtrIfNeeded();
conversation.startOtrSession(presence, false);
2014-03-07 18:48:52 -05:00
}
}
try {
conversation.setLastReceivedOtrMessageId(id);
Session otrSession = conversation.getOtrSession();
body = otrSession.transformReceiving(body);
SessionStatus status = otrSession.getSessionStatus();
if (body == null && status == SessionStatus.ENCRYPTED) {
2014-06-11 15:53:25 -04:00
mXmppConnectionService.onOtrSessionEstablished(conversation);
return null;
} else if (body == null && status == SessionStatus.FINISHED) {
conversation.resetOtrSession();
mXmppConnectionService.updateConversationUi();
return null;
} else if (body == null || (body.isEmpty())) {
return null;
}
if (body.startsWith(CryptoHelper.FILETRANSFER)) {
String key = body.substring(CryptoHelper.FILETRANSFER.length());
conversation.setSymmetricKey(CryptoHelper.hexToBytes(key));
return null;
}
if (clientMightSendHtml(conversation.getAccount(), from)) {
Log.d(Config.LOGTAG,conversation.getAccount().getJid().toBareJid()+": received OTR message from bad behaving client. escaping HTML…");
body = Html.fromHtml(body).toString();
}
2016-03-31 15:15:49 -04:00
final OtrService otrService = conversation.getAccount().getOtrService();
Message finishedMessage = new Message(conversation, body, Message.ENCRYPTION_OTR, Message.STATUS_RECEIVED);
2016-03-31 15:15:49 -04:00
finishedMessage.setFingerprint(otrService.getFingerprint(otrSession.getRemotePublicKey()));
conversation.setLastReceivedOtrMessageId(null);
2016-03-31 15:15:49 -04:00
return finishedMessage;
} catch (Exception e) {
conversation.resetOtrSession();
return null;
}
}
2014-06-04 06:31:19 -04:00
private static boolean clientMightSendHtml(Account account, Jid from) {
String resource = from.getResourcepart();
if (resource == null) {
return false;
}
Presence presence = account.getRoster().getContact(from).getPresences().getPresences().get(resource);
ServiceDiscoveryResult disco = presence == null ? null : presence.getServiceDiscoveryResult();
if (disco == null) {
return false;
}
return hasIdentityKnowForSendingHtml(disco.getIdentities());
}
private static boolean hasIdentityKnowForSendingHtml(List<ServiceDiscoveryResult.Identity> identities) {
for(ServiceDiscoveryResult.Identity identity : identities) {
if (identity.getName() != null) {
if (CLIENTS_SENDING_HTML_IN_OTR.contains(identity.getName())) {
return true;
}
}
}
return false;
}
private Message parseAxolotlChat(Element axolotlMessage, Jid from, Conversation conversation, int status) {
Message finishedMessage = null;
AxolotlService service = conversation.getAccount().getAxolotlService();
XmppAxolotlMessage xmppAxolotlMessage = XmppAxolotlMessage.fromElement(axolotlMessage, from.toBareJid());
XmppAxolotlMessage.XmppAxolotlPlaintextMessage plaintextMessage = service.processReceivingPayloadMessage(xmppAxolotlMessage);
if(plaintextMessage != null) {
finishedMessage = new Message(conversation, plaintextMessage.getPlaintext(), Message.ENCRYPTION_AXOLOTL, status);
2016-03-31 15:15:49 -04:00
finishedMessage.setFingerprint(plaintextMessage.getFingerprint());
Log.d(Config.LOGTAG, AxolotlService.getLogprefix(finishedMessage.getConversation().getAccount())+" Received Message with session fingerprint: "+plaintextMessage.getFingerprint());
}
return finishedMessage;
}
2015-04-21 12:36:11 -04:00
private class Invite {
Jid jid;
String password;
Invite(Jid jid, String password) {
this.jid = jid;
this.password = password;
2015-01-07 10:45:44 -05:00
}
2015-05-19 02:23:12 -04:00
public boolean execute(Account account) {
if (jid != null) {
Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, jid, true);
if (!conversation.getMucOptions().online()) {
conversation.getMucOptions().setPassword(password);
mXmppConnectionService.databaseBackend.updateConversation(conversation);
mXmppConnectionService.joinMuc(conversation);
mXmppConnectionService.updateConversationUi();
}
return true;
}
return false;
}
2015-04-21 12:36:11 -04:00
}
private Invite extractInvite(Element message) {
Element x = message.findChild("x", "http://jabber.org/protocol/muc#user");
2015-04-21 12:36:11 -04:00
if (x != null) {
Element invite = x.findChild("invite");
if (invite != null) {
Element pw = x.findChild("password");
return new Invite(message.getAttributeAsJid("from"), pw != null ? pw.getContent(): null);
}
2015-01-07 10:45:44 -05:00
} else {
2015-04-21 12:36:11 -04:00
x = message.findChild("x","jabber:x:conference");
if (x != null) {
return new Invite(x.getAttributeAsJid("jid"),x.getAttribute("password"));
}
2014-06-06 12:49:35 -04:00
}
2015-04-21 12:36:11 -04:00
return null;
2014-06-06 12:49:35 -04:00
}
2014-02-27 18:22:56 -05:00
2015-12-04 16:03:46 -05:00
private static String extractStanzaId(Element packet, Jid by) {
for(Element child : packet.getChildren()) {
if (child.getName().equals("stanza-id")
&& "urn:xmpp:sid:0".equals(child.getNamespace())
&& by.equals(child.getAttributeAsJid("by"))) {
return child.getAttribute("id");
}
}
return null;
}
2014-11-06 14:10:03 -05:00
private void parseEvent(final Element event, final Jid from, final Account account) {
2014-08-04 19:36:17 -04:00
Element items = event.findChild("items");
2015-05-19 02:23:12 -04:00
String node = items == null ? null : items.getAttribute("node");
if ("urn:xmpp:avatar:metadata".equals(node)) {
2014-11-20 12:42:47 -05:00
Avatar avatar = Avatar.parseMetadata(items);
if (avatar != null) {
2015-12-03 12:18:34 -05:00
avatar.owner = from.toBareJid();
2015-05-19 02:23:12 -04:00
if (mXmppConnectionService.getFileBackend().isAvatarCached(avatar)) {
2014-11-20 12:42:47 -05:00
if (account.getJid().toBareJid().equals(from)) {
if (account.setAvatar(avatar.getFilename())) {
2015-05-19 02:23:12 -04:00
mXmppConnectionService.databaseBackend.updateAccount(account);
2014-08-15 07:11:33 -04:00
}
2015-05-19 02:23:12 -04:00
mXmppConnectionService.getAvatarService().clear(account);
2014-11-20 12:42:47 -05:00
mXmppConnectionService.updateConversationUi();
mXmppConnectionService.updateAccountUi();
2014-08-05 16:58:46 -04:00
} else {
2015-05-19 02:23:12 -04:00
Contact contact = account.getRoster().getContact(from);
contact.setAvatar(avatar);
2015-05-19 02:23:12 -04:00
mXmppConnectionService.getAvatarService().clear(contact);
2014-11-20 12:42:47 -05:00
mXmppConnectionService.updateConversationUi();
mXmppConnectionService.updateRosterUi();
2014-08-05 16:58:46 -04:00
}
2014-11-20 12:42:47 -05:00
} else {
mXmppConnectionService.fetchAvatar(account, avatar);
2014-08-05 16:58:46 -04:00
}
2014-11-20 12:42:47 -05:00
}
2015-05-19 02:23:12 -04:00
} else if ("http://jabber.org/protocol/nick".equals(node)) {
Element i = items.findChild("item");
Element nick = i == null ? null : i.findChild("nick", "http://jabber.org/protocol/nick");
if (nick != null && nick.getContent() != null) {
2015-05-19 02:23:12 -04:00
Contact contact = account.getRoster().getContact(from);
contact.setPresenceName(nick.getContent());
mXmppConnectionService.getAvatarService().clear(account);
mXmppConnectionService.updateConversationUi();
mXmppConnectionService.updateAccountUi();
2014-08-05 16:58:46 -04:00
}
} else if (AxolotlService.PEP_DEVICE_LIST.equals(node)) {
Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Received PEP device list update from "+ from + ", processing...");
Element item = items.findChild("item");
Set<Integer> deviceIds = mXmppConnectionService.getIqParser().deviceIds(item);
AxolotlService axolotlService = account.getAxolotlService();
axolotlService.registerDevices(from, deviceIds);
mXmppConnectionService.updateAccountUi();
2014-08-04 19:36:17 -04:00
}
}
2015-05-19 02:23:12 -04:00
private boolean handleErrorMessage(Account account, MessagePacket packet) {
if (packet.getType() == MessagePacket.TYPE_ERROR) {
Jid from = packet.getFrom();
if (from != null) {
2015-07-28 08:44:11 -04:00
Element error = packet.findChild("error");
String text = error == null ? null : error.findChildContent("text");
if (text != null) {
Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": sending message to "+ from+ " failed - " + text);
} else if (error != null) {
Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": sending message to "+ from+ " failed - " + error);
}
Message message = mXmppConnectionService.markMessage(account,
from.toBareJid(),
packet.getId(),
Message.STATUS_SEND_FAILED);
2015-07-21 13:44:25 -04:00
if (message != null && message.getEncryption() == Message.ENCRYPTION_OTR) {
message.getConversation().endOtrIfNeeded();
}
2015-05-19 02:23:12 -04:00
}
return true;
}
return false;
}
@Override
public void onMessagePacketReceived(Account account, MessagePacket original) {
2015-05-19 02:23:12 -04:00
if (handleErrorMessage(account, original)) {
return;
}
final MessagePacket packet;
Long timestamp = null;
final boolean isForwarded;
boolean isCarbon = false;
2015-05-15 00:31:27 -04:00
String serverMsgId = null;
final Element fin = original.findChild("fin", "urn:xmpp:mam:0");
if (fin != null) {
mXmppConnectionService.getMessageArchiveService().processFin(fin,original.getFrom());
return;
}
final Element result = original.findChild("result","urn:xmpp:mam:0");
final MessageArchiveService.Query query = result == null ? null : mXmppConnectionService.getMessageArchiveService().findQuery(result.getAttribute("queryid"));
if (query != null && query.validFrom(original.getFrom())) {
Pair<MessagePacket, Long> f = original.getForwardedMessagePacket("result", "urn:xmpp:mam:0");
if (f == null) {
return;
}
timestamp = f.second;
packet = f.first;
isForwarded = true;
serverMsgId = result.getAttribute("id");
2016-02-03 10:04:21 -05:00
query.incrementMessageCount();
} else if (query != null) {
Log.d(Config.LOGTAG,account.getJid().toBareJid()+": received mam result from invalid sender");
return;
} else if (original.fromServer(account)) {
Pair<MessagePacket, Long> f;
f = original.getForwardedMessagePacket("received", "urn:xmpp:carbons:2");
f = f == null ? original.getForwardedMessagePacket("sent", "urn:xmpp:carbons:2") : f;
packet = f != null ? f.first : original;
2015-05-19 02:23:12 -04:00
if (handleErrorMessage(account, packet)) {
return;
}
timestamp = f != null ? f.second : null;
isCarbon = f != null;
isForwarded = isCarbon;
} else {
packet = original;
isForwarded = false;
}
2015-05-19 02:23:12 -04:00
if (timestamp == null) {
timestamp = AbstractParser.getTimestamp(packet, System.currentTimeMillis());
}
final String body = packet.getBody();
final Element mucUserElement = packet.findChild("x", "http://jabber.org/protocol/muc#user");
final String pgpEncrypted = packet.findChildContent("x", "jabber:x:encrypted");
final Element replaceElement = packet.findChild("replace", "urn:xmpp:message-correct:0");
final Element oob = packet.findChild("x", "jabber:x:oob");
final boolean isOob = oob!= null && body != null && body.equals(oob.findChildContent("url"));
final String replacementId = replaceElement == null ? null : replaceElement.getAttribute("id");
final Element axolotlEncrypted = packet.findChild(XmppAxolotlMessage.CONTAINERTAG, AxolotlService.PEP_PREFIX);
int status;
final Jid counterpart;
final Jid to = packet.getTo();
final Jid from = packet.getFrom();
2015-05-15 00:31:27 -04:00
final String remoteMsgId = packet.getId();
if (from == null) {
Log.d(Config.LOGTAG,"no from in: "+packet.toString());
return;
}
boolean isTypeGroupChat = packet.getType() == MessagePacket.TYPE_GROUPCHAT;
boolean isProperlyAddressed = (to != null ) && (!to.isBareJid() || account.countPresences() == 0);
boolean isMucStatusMessage = from.isBareJid() && mucUserElement != null && mucUserElement.hasChild("status");
if (packet.fromAccount(account)) {
status = Message.STATUS_SEND;
counterpart = to != null ? to : account.getJid();
} else {
status = Message.STATUS_RECEIVED;
counterpart = from;
2014-02-27 18:22:56 -05:00
}
Invite invite = extractInvite(packet);
2015-05-19 02:23:12 -04:00
if (invite != null && invite.execute(account)) {
return;
}
if (extractChatState(mXmppConnectionService.find(account, counterpart.toBareJid()), packet)) {
mXmppConnectionService.updateConversationUi();
}
if ((body != null || pgpEncrypted != null || axolotlEncrypted != null) && !isMucStatusMessage) {
Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, counterpart.toBareJid(), isTypeGroupChat, query);
final boolean conversationMultiMode = conversation.getMode() == Conversation.MODE_MULTI;
if (isTypeGroupChat) {
if (counterpart.getResourcepart().equals(conversation.getMucOptions().getActualNick())) {
status = Message.STATUS_SEND_RECEIVED;
isCarbon = true; //not really carbon but received from another resource
if (mXmppConnectionService.markMessage(conversation, remoteMsgId, status)) {
return;
} else if (remoteMsgId == null || Config.IGNORE_ID_REWRITE_IN_MUC) {
Message message = conversation.findSentMessageWithBody(packet.getBody());
if (message != null) {
mXmppConnectionService.markMessage(message, status);
return;
}
}
} else {
status = Message.STATUS_RECEIVED;
}
}
Message message;
if (body != null && body.startsWith("?OTR") && Config.supportOtr()) {
if (!isForwarded && !isTypeGroupChat && isProperlyAddressed && !conversationMultiMode) {
2015-05-15 00:31:27 -04:00
message = parseOtrChat(body, from, remoteMsgId, conversation);
if (message == null) {
return;
}
} else {
Log.d(Config.LOGTAG,account.getJid().toBareJid()+": ignoring OTR message from "+from+" isForwarded="+Boolean.toString(isForwarded)+", isProperlyAddressed="+Boolean.valueOf(isProperlyAddressed));
message = new Message(conversation, body, Message.ENCRYPTION_NONE, status);
2014-12-04 19:54:16 -05:00
}
} else if (pgpEncrypted != null && Config.supportOpenPgp()) {
2016-02-19 15:02:33 -05:00
message = new Message(conversation, pgpEncrypted, Message.ENCRYPTION_PGP, status);
} else if (axolotlEncrypted != null && Config.supportOmemo()) {
Jid origin;
if (conversationMultiMode) {
origin = conversation.getMucOptions().getTrueCounterpart(counterpart);
if (origin == null) {
Log.d(Config.LOGTAG,"axolotl message in non anonymous conference received");
return;
}
} else {
origin = from;
}
message = parseAxolotlChat(axolotlEncrypted, origin, conversation, status);
if (message == null) {
return;
}
} else {
message = new Message(conversation, body, Message.ENCRYPTION_NONE, status);
}
2015-12-04 16:03:46 -05:00
if (serverMsgId == null) {
serverMsgId = extractStanzaId(packet, isTypeGroupChat ? conversation.getJid().toBareJid() : account.getServer());
}
message.setCounterpart(counterpart);
2015-05-15 00:31:27 -04:00
message.setRemoteMsgId(remoteMsgId);
message.setServerMsgId(serverMsgId);
message.setCarbon(isCarbon);
message.setTime(timestamp);
message.setOob(isOob);
message.markable = packet.hasChild("markable", "urn:xmpp:chat-markers:0");
if (conversationMultiMode) {
Jid trueCounterpart = conversation.getMucOptions().getTrueCounterpart(counterpart);
2015-10-19 17:20:33 -04:00
message.setTrueCounterpart(trueCounterpart);
if (!isTypeGroupChat) {
message.setType(Message.TYPE_PRIVATE);
}
} else {
2016-05-10 03:41:30 -04:00
updateLastseen(timestamp, account, from);
}
if (replacementId != null && mXmppConnectionService.allowMessageCorrection()) {
Message replacedMessage = conversation.findMessageWithRemoteIdAndCounterpart(replacementId,
counterpart,
message.getStatus() == Message.STATUS_RECEIVED,
message.isCarbon());
if (replacedMessage != null) {
2016-03-31 15:15:49 -04:00
final boolean fingerprintsMatch = replacedMessage.getFingerprint() == null
|| replacedMessage.getFingerprint().equals(message.getFingerprint());
final boolean trueCountersMatch = replacedMessage.getTrueCounterpart() != null
&& replacedMessage.getTrueCounterpart().equals(message.getTrueCounterpart());
if (fingerprintsMatch && (trueCountersMatch || !conversationMultiMode)) {
Log.d(Config.LOGTAG, "replaced message '" + replacedMessage.getBody() + "' with '" + message.getBody() + "'");
2016-02-19 18:01:39 -05:00
final String uuid = replacedMessage.getUuid();
replacedMessage.setUuid(UUID.randomUUID().toString());
replacedMessage.setBody(message.getBody());
replacedMessage.setEdited(replacedMessage.getRemoteMsgId());
replacedMessage.setRemoteMsgId(remoteMsgId);
2016-02-19 15:02:33 -05:00
replacedMessage.setEncryption(message.getEncryption());
if (replacedMessage.getStatus() == Message.STATUS_RECEIVED) {
replacedMessage.markUnread();
}
2016-02-19 18:01:39 -05:00
mXmppConnectionService.updateMessage(replacedMessage, uuid);
mXmppConnectionService.getNotificationService().updateNotification(false);
if (mXmppConnectionService.confirmMessages() && remoteMsgId != null && !isForwarded && !isTypeGroupChat) {
sendMessageReceipts(account, packet);
}
2016-02-19 15:02:33 -05:00
if (replacedMessage.getEncryption() == Message.ENCRYPTION_PGP) {
conversation.getAccount().getPgpDecryptionService().add(replacedMessage);
}
return;
} else {
Log.d(Config.LOGTAG,account.getJid().toBareJid()+": received message correction but verification didn't check out");
}
}
}
boolean checkForDuplicates = query != null
2015-05-26 06:00:55 -04:00
|| (isTypeGroupChat && packet.hasChild("delay","urn:xmpp:delay"))
|| message.getType() == Message.TYPE_PRIVATE;
2015-05-15 00:31:27 -04:00
if (checkForDuplicates && conversation.hasDuplicateMessage(message)) {
Log.d(Config.LOGTAG,"skipping duplicate message from "+message.getCounterpart().toString()+" "+message.getBody());
return;
}
2015-12-04 16:03:46 -05:00
if (query != null && query.getPagingOrder() == MessageArchiveService.PagingOrder.REVERSE) {
conversation.prepend(message);
} else {
conversation.add(message);
}
2015-12-10 17:05:11 -05:00
2016-02-19 15:02:33 -05:00
if (message.getEncryption() == Message.ENCRYPTION_PGP) {
conversation.getAccount().getPgpDecryptionService().add(message);
}
2015-12-10 17:05:11 -05:00
if (query == null || query.getWith() == null) { //either no mam or catchup
if (status == Message.STATUS_SEND || status == Message.STATUS_SEND_RECEIVED) {
2015-05-15 05:51:20 -04:00
mXmppConnectionService.markRead(conversation);
2015-12-10 17:05:11 -05:00
if (query == null) {
2016-06-01 18:24:37 -04:00
activateGracePeriod(account);
2015-12-10 17:05:11 -05:00
}
2015-05-15 05:51:20 -04:00
} else {
message.markUnread();
}
2015-12-10 17:05:11 -05:00
}
2016-02-03 10:04:21 -05:00
if (query == null) {
mXmppConnectionService.updateConversationUi();
}
if (mXmppConnectionService.confirmMessages() && remoteMsgId != null && !isForwarded && !isTypeGroupChat) {
sendMessageReceipts(account, packet);
}
if (message.getStatus() == Message.STATUS_RECEIVED
&& conversation.getOtrSession() != null
&& !conversation.getOtrSession().getSessionID().getUserID()
.equals(message.getCounterpart().getResourcepart())) {
conversation.endOtrIfNeeded();
}
if (message.getEncryption() == Message.ENCRYPTION_NONE || mXmppConnectionService.saveEncryptedMessages()) {
mXmppConnectionService.databaseBackend.createMessage(message);
}
final HttpConnectionManager manager = this.mXmppConnectionService.getHttpConnectionManager();
2015-07-01 10:01:18 -04:00
if (message.trusted() && message.treatAsDownloadable() != Message.Decision.NEVER && manager.getAutoAcceptFileSize() > 0) {
manager.createNewDownloadConnection(message);
} else if (!message.isRead()) {
2015-12-10 17:05:11 -05:00
if (query == null) {
mXmppConnectionService.getNotificationService().push(message);
} else if (query.getWith() == null) { // mam catchup
mXmppConnectionService.getNotificationService().pushFromBacklog(message);
}
}
} else if (!packet.hasChild("body")){ //no body
if (Config.BACKGROUND_STANZA_LOGGING && mXmppConnectionService.checkListeners()) {
Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": " + packet+ " (carbon="+Boolean.toString(isCarbon)+")");
}
Conversation conversation = mXmppConnectionService.find(account, from.toBareJid());
if (isTypeGroupChat) {
if (packet.hasChild("subject")) {
if (conversation != null && conversation.getMode() == Conversation.MODE_MULTI) {
conversation.setHasMessagesLeftOnServer(conversation.countMessages() > 0);
String subject = packet.findChildContent("subject");
conversation.getMucOptions().setSubject(subject);
final Bookmark bookmark = conversation.getBookmark();
if (bookmark != null && bookmark.getBookmarkName() == null) {
if (bookmark.setBookmarkName(subject)) {
mXmppConnectionService.pushBookmarks(account);
}
}
mXmppConnectionService.updateConversationUi();
return;
}
}
}
if (conversation != null && mucUserElement != null && from.isBareJid()) {
if (mucUserElement.hasChild("status")) {
for (Element child : mucUserElement.getChildren()) {
if (child.getName().equals("status")
&& MucOptions.STATUS_CODE_ROOM_CONFIG_CHANGED.equals(child.getAttribute("code"))) {
mXmppConnectionService.fetchConferenceConfiguration(conversation);
}
}
} else if (mucUserElement.hasChild("item")) {
for(Element child : mucUserElement.getChildren()) {
if ("item".equals(child.getName())) {
MucOptions.User user = AbstractParser.parseItem(conversation,child);
Log.d(Config.LOGTAG,account.getJid()+": changing affiliation for "
+user.getRealJid()+" to "+user.getAffiliation()+" in "
+conversation.getJid().toBareJid());
if (!user.realJidMatchesAccount()) {
conversation.getMucOptions().addUser(user);
2016-05-21 03:25:37 -04:00
mXmppConnectionService.getAvatarService().clear(conversation);
mXmppConnectionService.updateMucRosterUi();
mXmppConnectionService.updateConversationUi();
}
}
}
}
}
}
Element received = packet.findChild("received", "urn:xmpp:chat-markers:0");
if (received == null) {
received = packet.findChild("received", "urn:xmpp:receipts");
}
if (received != null && !packet.fromAccount(account)) {
mXmppConnectionService.markMessage(account, from.toBareJid(), received.getAttribute("id"), Message.STATUS_SEND_RECEIVED);
}
Element displayed = packet.findChild("displayed", "urn:xmpp:chat-markers:0");
if (displayed != null) {
if (packet.fromAccount(account)) {
Conversation conversation = mXmppConnectionService.find(account,counterpart.toBareJid());
2015-05-16 06:43:38 -04:00
if (conversation != null) {
mXmppConnectionService.markRead(conversation);
}
} else {
2016-05-10 03:41:30 -04:00
updateLastseen(timestamp, account, from);
final Message displayedMessage = mXmppConnectionService.markMessage(account, from.toBareJid(), displayed.getAttribute("id"), Message.STATUS_SEND_DISPLAYED);
Message message = displayedMessage == null ? null : displayedMessage.prev();
while (message != null
&& message.getStatus() == Message.STATUS_SEND_RECEIVED
&& message.getTimeSent() < displayedMessage.getTimeSent()) {
mXmppConnectionService.markMessage(message, Message.STATUS_SEND_DISPLAYED);
message = message.prev();
}
}
2014-09-28 09:21:56 -04:00
}
2014-08-04 19:36:17 -04:00
Element event = packet.findChild("event", "http://jabber.org/protocol/pubsub#event");
if (event != null) {
parseEvent(event, from, account);
2014-08-04 19:36:17 -04:00
}
String nick = packet.findChildContent("nick", "http://jabber.org/protocol/nick");
if (nick != null) {
Contact contact = account.getRoster().getContact(from);
contact.setPresenceName(nick);
}
}
private void sendMessageReceipts(Account account, MessagePacket packet) {
ArrayList<String> receiptsNamespaces = new ArrayList<>();
if (packet.hasChild("markable", "urn:xmpp:chat-markers:0")) {
receiptsNamespaces.add("urn:xmpp:chat-markers:0");
}
if (packet.hasChild("request", "urn:xmpp:receipts")) {
receiptsNamespaces.add("urn:xmpp:receipts");
}
if (receiptsNamespaces.size() > 0) {
MessagePacket receipt = mXmppConnectionService.getMessageGenerator().received(account,
packet,
receiptsNamespaces,
packet.getType());
mXmppConnectionService.sendMessagePacket(account, receipt);
}
}
2016-06-01 18:24:37 -04:00
private static SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm:ss");
private void activateGracePeriod(Account account) {
long duration = mXmppConnectionService.getPreferences().getLong("race_period_length", 144) * 1000;
Log.d(Config.LOGTAG,account.getJid().toBareJid()+": activating grace period till "+TIME_FORMAT.format(new Date(System.currentTimeMillis() + duration)));
account.activateGracePeriod(duration);
}
2015-08-15 08:26:37 -04:00
}