2014-05-14 10:56:34 +00:00
|
|
|
package eu.siacs.conversations.parser;
|
2014-02-19 00:35:23 +00:00
|
|
|
|
2015-05-15 03:14:15 +00:00
|
|
|
import android.util.Log;
|
|
|
|
import android.util.Pair;
|
|
|
|
|
2014-02-19 00:35:23 +00:00
|
|
|
import net.java.otr4j.session.Session;
|
|
|
|
import net.java.otr4j.session.SessionStatus;
|
2014-11-04 17:27:20 +00:00
|
|
|
|
2015-05-15 03:14:15 +00:00
|
|
|
import eu.siacs.conversations.Config;
|
2014-02-28 17:46:01 +00:00
|
|
|
import eu.siacs.conversations.entities.Account;
|
2014-08-05 20:58:46 +00:00
|
|
|
import eu.siacs.conversations.entities.Contact;
|
2014-02-28 17:46:01 +00:00
|
|
|
import eu.siacs.conversations.entities.Conversation;
|
|
|
|
import eu.siacs.conversations.entities.Message;
|
2015-01-07 17:34:24 +00:00
|
|
|
import eu.siacs.conversations.entities.MucOptions;
|
2015-01-11 21:18:18 +00:00
|
|
|
import eu.siacs.conversations.http.HttpConnectionManager;
|
2014-12-13 11:25:52 +00:00
|
|
|
import eu.siacs.conversations.services.MessageArchiveService;
|
2014-02-28 17:46:01 +00:00
|
|
|
import eu.siacs.conversations.services.XmppConnectionService;
|
2014-06-20 15:30:19 +00:00
|
|
|
import eu.siacs.conversations.utils.CryptoHelper;
|
2014-02-28 17:46:01 +00:00
|
|
|
import eu.siacs.conversations.xml.Element;
|
2014-07-12 00:36:37 +00:00
|
|
|
import eu.siacs.conversations.xmpp.OnMessagePacketReceived;
|
2015-02-21 10:06:52 +00:00
|
|
|
import eu.siacs.conversations.xmpp.chatstate.ChatState;
|
2014-11-06 19:10:03 +00:00
|
|
|
import eu.siacs.conversations.xmpp.jid.Jid;
|
2014-08-05 20:58:46 +00:00
|
|
|
import eu.siacs.conversations.xmpp.pep.Avatar;
|
2014-03-10 18:22:13 +00:00
|
|
|
import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
|
2014-02-19 00:35:23 +00:00
|
|
|
|
2014-07-12 00:36:37 +00:00
|
|
|
public class MessageParser extends AbstractParser implements
|
|
|
|
OnMessagePacketReceived {
|
2014-05-14 10:56:34 +00:00
|
|
|
public MessageParser(XmppConnectionService service) {
|
2014-06-06 16:26:40 +00:00
|
|
|
super(service);
|
2014-05-14 10:56:34 +00:00
|
|
|
}
|
2014-06-04 10:31:19 +00:00
|
|
|
|
2015-05-15 03:14:15 +00:00
|
|
|
private boolean extractChatState(Conversation conversation, final MessagePacket packet) {
|
|
|
|
ChatState state = ChatState.parse(packet);
|
2015-02-21 10:06:52 +00:00
|
|
|
if (state != null && conversation != null) {
|
|
|
|
final Account account = conversation.getAccount();
|
2015-05-15 03:14:15 +00:00
|
|
|
Jid from = packet.getFrom();
|
|
|
|
if (from.toBareJid().equals(account.getJid().toBareJid())) {
|
2015-02-21 10:06:52 +00:00
|
|
|
conversation.setOutgoingChatState(state);
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return conversation.setIncomingChatState(state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-05-15 03:14:15 +00:00
|
|
|
private Message parseOtrChat(String body, Jid from, String id, Conversation conversation) {
|
2014-08-11 11:46:32 +00:00
|
|
|
String presence;
|
2014-11-06 19:10:03 +00:00
|
|
|
if (from.isBareJid()) {
|
2015-03-16 22:23:51 +00:00
|
|
|
presence = "";
|
2014-08-11 11:46:32 +00:00
|
|
|
} else {
|
2014-11-06 19:10:03 +00:00
|
|
|
presence = from.getResourcepart();
|
2014-08-11 11:46:32 +00:00
|
|
|
}
|
2015-01-24 15:19:58 +00:00
|
|
|
if (body.matches("^\\?OTRv\\d{1,2}\\?.*")) {
|
2014-10-05 09:14:50 +00:00
|
|
|
conversation.endOtrIfNeeded();
|
2014-09-06 16:21:31 +00:00
|
|
|
}
|
2014-02-19 00:35:23 +00:00
|
|
|
if (!conversation.hasValidOtrSession()) {
|
2015-05-15 03:14:15 +00:00
|
|
|
conversation.startOtrSession(presence,false);
|
2014-03-07 23:48:52 +00:00
|
|
|
} else {
|
2015-05-15 03:14:15 +00:00
|
|
|
String foreignPresence = conversation.getOtrSession().getSessionID().getUserID();
|
2014-08-11 11:46:32 +00:00
|
|
|
if (!foreignPresence.equals(presence)) {
|
2014-06-25 14:55:47 +00:00
|
|
|
conversation.endOtrIfNeeded();
|
2015-05-15 03:14:15 +00:00
|
|
|
conversation.startOtrSession(presence, false);
|
2014-03-07 23:48:52 +00:00
|
|
|
}
|
2014-02-19 00:35:23 +00:00
|
|
|
}
|
|
|
|
try {
|
2015-05-15 03:14:15 +00:00
|
|
|
conversation.setLastReceivedOtrMessageId(id);
|
2014-02-19 00:35:23 +00:00
|
|
|
Session otrSession = conversation.getOtrSession();
|
2014-06-04 10:31:19 +00:00
|
|
|
SessionStatus before = otrSession.getSessionStatus();
|
2014-02-19 00:35:23 +00:00
|
|
|
body = otrSession.transformReceiving(body);
|
|
|
|
SessionStatus after = otrSession.getSessionStatus();
|
2014-06-04 10:31:19 +00:00
|
|
|
if ((before != after) && (after == SessionStatus.ENCRYPTED)) {
|
2015-01-25 12:07:31 +00:00
|
|
|
conversation.setNextEncryption(Message.ENCRYPTION_OTR);
|
2014-06-11 19:53:25 +00:00
|
|
|
mXmppConnectionService.onOtrSessionEstablished(conversation);
|
2014-02-19 00:35:23 +00:00
|
|
|
} else if ((before != after) && (after == SessionStatus.FINISHED)) {
|
2015-01-25 12:07:31 +00:00
|
|
|
conversation.setNextEncryption(Message.ENCRYPTION_NONE);
|
2014-02-19 00:35:23 +00:00
|
|
|
conversation.resetOtrSession();
|
2014-07-20 10:36:57 +00:00
|
|
|
mXmppConnectionService.updateConversationUi();
|
2014-02-19 00:35:23 +00:00
|
|
|
}
|
2014-06-04 10:31:19 +00:00
|
|
|
if ((body == null) || (body.isEmpty())) {
|
2014-03-23 13:15:14 +00:00
|
|
|
return null;
|
|
|
|
}
|
2014-06-20 15:30:19 +00:00
|
|
|
if (body.startsWith(CryptoHelper.FILETRANSFER)) {
|
|
|
|
String key = body.substring(CryptoHelper.FILETRANSFER.length());
|
|
|
|
conversation.setSymmetricKey(CryptoHelper.hexToBytes(key));
|
|
|
|
return null;
|
|
|
|
}
|
2015-05-15 03:14:15 +00:00
|
|
|
Message finishedMessage = new Message(conversation, body, Message.ENCRYPTION_OTR, Message.STATUS_RECEIVED);
|
2015-03-21 15:07:17 +00:00
|
|
|
conversation.setLastReceivedOtrMessageId(null);
|
2014-06-06 16:26:40 +00:00
|
|
|
return finishedMessage;
|
2014-02-19 00:35:23 +00:00
|
|
|
} catch (Exception e) {
|
2014-09-06 16:21:31 +00:00
|
|
|
conversation.resetOtrSession();
|
2014-02-19 00:35:23 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2014-06-04 10:31:19 +00:00
|
|
|
|
2015-04-21 16:36:11 +00:00
|
|
|
private class Invite {
|
|
|
|
Jid jid;
|
|
|
|
String password;
|
|
|
|
Invite(Jid jid, String password) {
|
|
|
|
this.jid = jid;
|
|
|
|
this.password = password;
|
2015-01-07 15:45:44 +00:00
|
|
|
}
|
2015-05-19 06:23:12 +00: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 16:36:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private Invite extractInvite(Element message) {
|
2015-05-15 03:14:15 +00:00
|
|
|
Element x = message.findChild("x", "http://jabber.org/protocol/muc#user");
|
2015-04-21 16:36:11 +00: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 15:45:44 +00:00
|
|
|
} else {
|
2015-04-21 16:36:11 +00:00
|
|
|
x = message.findChild("x","jabber:x:conference");
|
|
|
|
if (x != null) {
|
|
|
|
return new Invite(x.getAttributeAsJid("jid"),x.getAttribute("password"));
|
|
|
|
}
|
2014-06-06 16:49:35 +00:00
|
|
|
}
|
2015-04-21 16:36:11 +00:00
|
|
|
return null;
|
2014-06-06 16:49:35 +00:00
|
|
|
}
|
2014-02-27 23:22:56 +00:00
|
|
|
|
2014-11-06 19:10:03 +00:00
|
|
|
private void parseEvent(final Element event, final Jid from, final Account account) {
|
2014-08-04 23:36:17 +00:00
|
|
|
Element items = event.findChild("items");
|
2015-05-19 06:23:12 +00:00
|
|
|
String node = items == null ? null : items.getAttribute("node");
|
|
|
|
if ("urn:xmpp:avatar:metadata".equals(node)) {
|
2014-11-20 17:42:47 +00:00
|
|
|
Avatar avatar = Avatar.parseMetadata(items);
|
|
|
|
if (avatar != null) {
|
|
|
|
avatar.owner = from;
|
2015-05-19 06:23:12 +00:00
|
|
|
if (mXmppConnectionService.getFileBackend().isAvatarCached(avatar)) {
|
2014-11-20 17:42:47 +00:00
|
|
|
if (account.getJid().toBareJid().equals(from)) {
|
|
|
|
if (account.setAvatar(avatar.getFilename())) {
|
2015-05-19 06:23:12 +00:00
|
|
|
mXmppConnectionService.databaseBackend.updateAccount(account);
|
2014-08-15 11:11:33 +00:00
|
|
|
}
|
2015-05-19 06:23:12 +00:00
|
|
|
mXmppConnectionService.getAvatarService().clear(account);
|
2014-11-20 17:42:47 +00:00
|
|
|
mXmppConnectionService.updateConversationUi();
|
|
|
|
mXmppConnectionService.updateAccountUi();
|
2014-08-05 20:58:46 +00:00
|
|
|
} else {
|
2015-05-19 06:23:12 +00:00
|
|
|
Contact contact = account.getRoster().getContact(from);
|
2015-05-05 04:17:34 +00:00
|
|
|
contact.setAvatar(avatar);
|
2015-05-19 06:23:12 +00:00
|
|
|
mXmppConnectionService.getAvatarService().clear(contact);
|
2014-11-20 17:42:47 +00:00
|
|
|
mXmppConnectionService.updateConversationUi();
|
|
|
|
mXmppConnectionService.updateRosterUi();
|
2014-08-05 20:58:46 +00:00
|
|
|
}
|
2014-11-20 17:42:47 +00:00
|
|
|
} else {
|
|
|
|
mXmppConnectionService.fetchAvatar(account, avatar);
|
2014-08-05 20:58:46 +00:00
|
|
|
}
|
2014-11-20 17:42:47 +00:00
|
|
|
}
|
2015-05-19 06:23:12 +00: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) {
|
|
|
|
Contact contact = account.getRoster().getContact(from);
|
|
|
|
contact.setPresenceName(nick.getContent());
|
|
|
|
mXmppConnectionService.getAvatarService().clear(account);
|
|
|
|
mXmppConnectionService.updateConversationUi();
|
|
|
|
mXmppConnectionService.updateAccountUi();
|
2014-08-05 20:58:46 +00:00
|
|
|
}
|
2014-08-04 23:36:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-19 06:23:12 +00:00
|
|
|
private boolean handleErrorMessage(Account account, MessagePacket packet) {
|
|
|
|
if (packet.getType() == MessagePacket.TYPE_ERROR) {
|
|
|
|
Jid from = packet.getFrom();
|
|
|
|
if (from != null) {
|
|
|
|
mXmppConnectionService.markMessage(account, from.toBareJid(), packet.getId(), Message.STATUS_SEND_FAILED);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-05-15 03:14:15 +00:00
|
|
|
@Override
|
|
|
|
public void onMessagePacketReceived(Account account, MessagePacket original) {
|
2015-05-19 06:23:12 +00:00
|
|
|
if (handleErrorMessage(account, original)) {
|
|
|
|
return;
|
|
|
|
}
|
2015-05-15 03:14:15 +00:00
|
|
|
final MessagePacket packet;
|
|
|
|
Long timestamp = null;
|
|
|
|
final boolean isForwarded;
|
2015-05-15 04:31:27 +00:00
|
|
|
String serverMsgId = null;
|
2015-05-15 10:29:45 +00:00
|
|
|
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");
|
|
|
|
query.incrementTotalCount();
|
|
|
|
} else if (query != null) {
|
|
|
|
Log.d(Config.LOGTAG,account.getJid().toBareJid()+": received mam result from invalid sender");
|
|
|
|
return;
|
|
|
|
} else if (original.fromServer(account)) {
|
2015-05-15 03:14:15 +00:00
|
|
|
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 06:23:12 +00:00
|
|
|
if (handleErrorMessage(account, packet)) {
|
|
|
|
return;
|
|
|
|
}
|
2015-05-15 03:14:15 +00:00
|
|
|
timestamp = f != null ? f.second : null;
|
|
|
|
isForwarded = f != null;
|
2014-05-22 14:17:51 +00:00
|
|
|
} else {
|
2015-05-15 03:14:15 +00:00
|
|
|
packet = original;
|
|
|
|
isForwarded = false;
|
|
|
|
}
|
2015-05-19 06:23:12 +00:00
|
|
|
|
2015-05-15 03:14:15 +00:00
|
|
|
if (timestamp == null) {
|
|
|
|
timestamp = AbstractParser.getTimestamp(packet, System.currentTimeMillis());
|
|
|
|
}
|
|
|
|
final String body = packet.getBody();
|
|
|
|
final String encrypted = packet.findChildContent("x", "jabber:x:encrypted");
|
|
|
|
int status;
|
2015-05-15 10:29:45 +00:00
|
|
|
final Jid counterpart;
|
2015-05-15 03:14:15 +00:00
|
|
|
final Jid to = packet.getTo();
|
|
|
|
final Jid from = packet.getFrom();
|
2015-05-15 04:31:27 +00:00
|
|
|
final String remoteMsgId = packet.getId();
|
2015-05-15 03:14:15 +00:00
|
|
|
boolean isTypeGroupChat = packet.getType() == MessagePacket.TYPE_GROUPCHAT;
|
|
|
|
boolean properlyAddressed = !to.isBareJid() || account.countPresences() == 1;
|
|
|
|
if (packet.fromAccount(account)) {
|
|
|
|
status = Message.STATUS_SEND;
|
|
|
|
counterpart = to;
|
|
|
|
} else {
|
|
|
|
status = Message.STATUS_RECEIVED;
|
|
|
|
counterpart = from;
|
2014-02-27 23:22:56 +00:00
|
|
|
}
|
2014-07-12 00:36:37 +00:00
|
|
|
|
2015-05-15 03:14:15 +00:00
|
|
|
if (from == null || to == null) {
|
|
|
|
Log.d(Config.LOGTAG,"no to or from in: "+packet.toString());
|
|
|
|
return;
|
|
|
|
}
|
2014-06-06 16:49:35 +00:00
|
|
|
|
2015-05-15 03:14:15 +00:00
|
|
|
Invite invite = extractInvite(packet);
|
2015-05-19 06:23:12 +00:00
|
|
|
if (invite != null && invite.execute(account)) {
|
2015-05-15 03:14:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (extractChatState(mXmppConnectionService.find(account,from), packet)) {
|
|
|
|
mXmppConnectionService.updateConversationUi();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (body != null || encrypted != null) {
|
|
|
|
Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, counterpart.toBareJid(), isTypeGroupChat);
|
|
|
|
if (isTypeGroupChat) {
|
|
|
|
if (counterpart.getResourcepart().equals(conversation.getMucOptions().getActualNick())) {
|
|
|
|
status = Message.STATUS_SEND;
|
2015-05-15 04:31:27 +00:00
|
|
|
if (mXmppConnectionService.markMessage(conversation, remoteMsgId, Message.STATUS_SEND_RECEIVED)) {
|
2015-05-15 03:14:15 +00:00
|
|
|
return;
|
2015-06-02 04:23:32 +00:00
|
|
|
} else {
|
|
|
|
Message message = conversation.findSentMessageWithBody(body);
|
2015-05-15 03:14:15 +00:00
|
|
|
if (message != null) {
|
2015-06-02 04:23:32 +00:00
|
|
|
message.setRemoteMsgId(remoteMsgId);
|
2015-05-15 03:14:15 +00:00
|
|
|
mXmppConnectionService.markMessage(message, Message.STATUS_SEND_RECEIVED);
|
|
|
|
return;
|
|
|
|
}
|
2014-07-12 00:36:37 +00:00
|
|
|
}
|
2015-05-15 03:14:15 +00:00
|
|
|
} else {
|
|
|
|
status = Message.STATUS_RECEIVED;
|
2014-07-12 00:36:37 +00:00
|
|
|
}
|
2015-05-15 03:14:15 +00:00
|
|
|
}
|
|
|
|
Message message;
|
|
|
|
if (body != null && body.startsWith("?OTR")) {
|
|
|
|
if (!isForwarded && !isTypeGroupChat && properlyAddressed) {
|
2015-05-15 04:31:27 +00:00
|
|
|
message = parseOtrChat(body, from, remoteMsgId, conversation);
|
2015-05-15 03:14:15 +00:00
|
|
|
if (message == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
message = new Message(conversation, body, Message.ENCRYPTION_NONE, status);
|
2014-12-05 00:54:16 +00:00
|
|
|
}
|
2015-05-15 03:14:15 +00:00
|
|
|
} else if (encrypted != null) {
|
|
|
|
message = new Message(conversation, encrypted, Message.ENCRYPTION_PGP, status);
|
2014-07-23 12:30:27 +00:00
|
|
|
} else {
|
2015-05-15 03:14:15 +00:00
|
|
|
message = new Message(conversation, body, Message.ENCRYPTION_NONE, status);
|
2014-07-12 00:36:37 +00:00
|
|
|
}
|
2015-05-15 03:14:15 +00:00
|
|
|
message.setCounterpart(counterpart);
|
2015-05-15 04:31:27 +00:00
|
|
|
message.setRemoteMsgId(remoteMsgId);
|
|
|
|
message.setServerMsgId(serverMsgId);
|
2015-05-15 03:14:15 +00:00
|
|
|
message.setTime(timestamp);
|
|
|
|
message.markable = packet.hasChild("markable", "urn:xmpp:chat-markers:0");
|
|
|
|
if (conversation.getMode() == Conversation.MODE_MULTI) {
|
|
|
|
message.setTrueCounterpart(conversation.getMucOptions().getTrueCounterpart(counterpart.getResourcepart()));
|
|
|
|
if (!isTypeGroupChat) {
|
|
|
|
message.setType(Message.TYPE_PRIVATE);
|
2014-07-12 00:36:37 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-15 03:14:15 +00:00
|
|
|
updateLastseen(packet,account,true);
|
2015-05-26 10:00:55 +00:00
|
|
|
boolean checkForDuplicates = serverMsgId != null
|
|
|
|
|| (isTypeGroupChat && packet.hasChild("delay","urn:xmpp:delay"))
|
|
|
|
|| message.getType() == Message.TYPE_PRIVATE;
|
2015-05-15 04:31:27 +00:00
|
|
|
if (checkForDuplicates && conversation.hasDuplicateMessage(message)) {
|
|
|
|
Log.d(Config.LOGTAG,"skipping duplicate message from "+message.getCounterpart().toString()+" "+message.getBody());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (query != null) {
|
|
|
|
query.incrementMessageCount();
|
|
|
|
}
|
2015-05-15 03:14:15 +00:00
|
|
|
conversation.add(message);
|
2015-05-15 09:51:20 +00:00
|
|
|
if (serverMsgId == null) {
|
|
|
|
if (status == Message.STATUS_SEND) {
|
|
|
|
mXmppConnectionService.markRead(conversation);
|
|
|
|
account.activateGracePeriod();
|
|
|
|
} else {
|
|
|
|
message.markUnread();
|
|
|
|
}
|
2015-05-15 10:29:45 +00:00
|
|
|
mXmppConnectionService.updateConversationUi();
|
2014-07-23 23:04:25 +00:00
|
|
|
}
|
2015-05-15 03:14:15 +00:00
|
|
|
|
2015-05-15 04:31:27 +00:00
|
|
|
if (mXmppConnectionService.confirmMessages() && remoteMsgId != null && !isForwarded) {
|
2015-05-15 03:14:15 +00:00
|
|
|
if (packet.hasChild("markable", "urn:xmpp:chat-markers:0")) {
|
|
|
|
MessagePacket receipt = mXmppConnectionService
|
|
|
|
.getMessageGenerator().received(account, packet, "urn:xmpp:chat-markers:0");
|
|
|
|
mXmppConnectionService.sendMessagePacket(account, receipt);
|
|
|
|
}
|
|
|
|
if (packet.hasChild("request", "urn:xmpp:receipts")) {
|
|
|
|
MessagePacket receipt = mXmppConnectionService
|
|
|
|
.getMessageGenerator().received(account, packet, "urn:xmpp:receipts");
|
|
|
|
mXmppConnectionService.sendMessagePacket(account, receipt);
|
|
|
|
}
|
2014-07-12 00:36:37 +00:00
|
|
|
}
|
2015-05-15 03:14:15 +00:00
|
|
|
if (account.getXmppConnection() != null && account.getXmppConnection().getFeatures().advancedStreamFeaturesLoaded()) {
|
|
|
|
if (conversation.setLastMessageTransmitted(System.currentTimeMillis())) {
|
|
|
|
mXmppConnectionService.updateConversation(conversation);
|
|
|
|
}
|
2014-12-08 20:59:14 +00:00
|
|
|
}
|
2014-10-28 17:02:12 +00:00
|
|
|
|
2015-05-15 03:14:15 +00:00
|
|
|
if (message.getStatus() == Message.STATUS_RECEIVED
|
|
|
|
&& conversation.getOtrSession() != null
|
|
|
|
&& !conversation.getOtrSession().getSessionID().getUserID()
|
|
|
|
.equals(message.getCounterpart().getResourcepart())) {
|
|
|
|
conversation.endOtrIfNeeded();
|
|
|
|
}
|
2014-10-28 17:02:12 +00:00
|
|
|
|
2015-05-15 10:29:45 +00:00
|
|
|
if (message.getEncryption() == Message.ENCRYPTION_NONE || mXmppConnectionService.saveEncryptedMessages()) {
|
2014-09-08 11:37:22 +00:00
|
|
|
mXmppConnectionService.databaseBackend.createMessage(message);
|
|
|
|
}
|
2015-05-15 03:14:15 +00:00
|
|
|
final HttpConnectionManager manager = this.mXmppConnectionService.getHttpConnectionManager();
|
|
|
|
if (message.trusted() && message.bodyContainsDownloadable() && manager.getAutoAcceptFileSize() > 0) {
|
|
|
|
manager.createNewConnection(message);
|
|
|
|
} else if (!message.isRead()) {
|
|
|
|
mXmppConnectionService.getNotificationService().push(message);
|
|
|
|
}
|
2015-05-15 10:29:45 +00:00
|
|
|
} else { //no body
|
2015-05-15 03:14:15 +00:00
|
|
|
if (packet.hasChild("subject") && isTypeGroupChat) {
|
|
|
|
Conversation conversation = mXmppConnectionService.find(account, from.toBareJid());
|
|
|
|
if (conversation != null && conversation.getMode() == Conversation.MODE_MULTI) {
|
|
|
|
conversation.setHasMessagesLeftOnServer(true);
|
|
|
|
conversation.getMucOptions().setSubject(packet.findChildContent("subject"));
|
|
|
|
mXmppConnectionService.updateConversationUi();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2014-07-12 00:36:37 +00:00
|
|
|
}
|
2015-05-15 03:14:15 +00:00
|
|
|
|
|
|
|
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 10:43:38 +00:00
|
|
|
if (conversation != null) {
|
|
|
|
mXmppConnectionService.markRead(conversation);
|
|
|
|
}
|
2015-05-15 03:14:15 +00:00
|
|
|
} else {
|
|
|
|
updateLastseen(packet, account, true);
|
|
|
|
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 13:21:56 +00:00
|
|
|
}
|
2014-08-04 23:36:17 +00:00
|
|
|
|
2015-05-15 03:14:15 +00:00
|
|
|
Element event = packet.findChild("event", "http://jabber.org/protocol/pubsub#event");
|
|
|
|
if (event != null) {
|
|
|
|
parseEvent(event, from, account);
|
2014-08-04 23:36:17 +00:00
|
|
|
}
|
2014-09-04 08:50:06 +00:00
|
|
|
|
2015-05-15 03:14:15 +00:00
|
|
|
String nick = packet.findChildContent("nick", "http://jabber.org/protocol/nick");
|
2014-09-04 08:50:06 +00:00
|
|
|
if (nick != null) {
|
2015-05-15 03:14:15 +00:00
|
|
|
Contact contact = account.getRoster().getContact(from);
|
|
|
|
contact.setPresenceName(nick);
|
2014-09-04 08:50:06 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-15 03:14:15 +00:00
|
|
|
}
|