2014-05-14 10:56:34 +00:00
|
|
|
package eu.siacs.conversations.parser;
|
2014-02-19 00:35:23 +00:00
|
|
|
|
2015-02-21 10:06:52 +00:00
|
|
|
import android.util.Log;
|
|
|
|
|
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-02-21 10:06:52 +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-02-21 10:06:52 +00:00
|
|
|
private boolean extractChatState(Conversation conversation, final Element element) {
|
|
|
|
ChatState state = ChatState.parse(element);
|
|
|
|
if (state != null && conversation != null) {
|
|
|
|
final Account account = conversation.getAccount();
|
|
|
|
Jid from = element.getAttributeAsJid("from");
|
|
|
|
if (from != null && from.toBareJid().equals(account.getJid().toBareJid())) {
|
|
|
|
conversation.setOutgoingChatState(state);
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return conversation.setIncomingChatState(state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-12 00:36:37 +00:00
|
|
|
private Message parseChat(MessagePacket packet, Account account) {
|
2014-11-09 20:14:37 +00:00
|
|
|
final Jid jid = packet.getFrom();
|
2014-12-02 22:59:40 +00:00
|
|
|
if (jid == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, jid.toBareJid(), false);
|
2014-07-12 00:36:37 +00:00
|
|
|
updateLastseen(packet, account, true);
|
2014-06-04 09:55:38 +00:00
|
|
|
String pgpBody = getPgpBody(packet);
|
2014-06-22 16:21:04 +00:00
|
|
|
Message finishedMessage;
|
2014-06-04 10:31:19 +00:00
|
|
|
if (pgpBody != null) {
|
2014-11-10 00:24:35 +00:00
|
|
|
finishedMessage = new Message(conversation,
|
2014-08-28 09:01:24 +00:00
|
|
|
pgpBody, Message.ENCRYPTION_PGP, Message.STATUS_RECEIVED);
|
2014-06-04 09:55:38 +00:00
|
|
|
} else {
|
2014-11-10 00:24:35 +00:00
|
|
|
finishedMessage = new Message(conversation,
|
2014-06-04 10:31:19 +00:00
|
|
|
packet.getBody(), Message.ENCRYPTION_NONE,
|
2014-08-28 09:01:24 +00:00
|
|
|
Message.STATUS_RECEIVED);
|
2014-06-04 09:55:38 +00:00
|
|
|
}
|
2014-08-25 17:20:53 +00:00
|
|
|
finishedMessage.setRemoteMsgId(packet.getId());
|
2014-10-07 14:02:52 +00:00
|
|
|
finishedMessage.markable = isMarkable(packet);
|
2014-08-10 13:27:44 +00:00
|
|
|
if (conversation.getMode() == Conversation.MODE_MULTI
|
2014-11-09 18:13:19 +00:00
|
|
|
&& !jid.isBareJid()) {
|
2014-08-10 13:27:44 +00:00
|
|
|
finishedMessage.setType(Message.TYPE_PRIVATE);
|
|
|
|
finishedMessage.setTrueCounterpart(conversation.getMucOptions()
|
2014-11-06 19:10:03 +00:00
|
|
|
.getTrueCounterpart(jid.getResourcepart()));
|
2014-08-25 17:20:53 +00:00
|
|
|
if (conversation.hasDuplicateMessage(finishedMessage)) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-08-10 13:27:44 +00:00
|
|
|
|
|
|
|
}
|
2014-11-09 20:14:37 +00:00
|
|
|
finishedMessage.setCounterpart(jid);
|
2014-06-22 16:21:04 +00:00
|
|
|
finishedMessage.setTime(getTimestamp(packet));
|
2015-02-21 10:06:52 +00:00
|
|
|
extractChatState(conversation,packet);
|
2014-06-22 16:21:04 +00:00
|
|
|
return finishedMessage;
|
2014-02-27 23:22:56 +00:00
|
|
|
}
|
2014-06-04 09:55:38 +00:00
|
|
|
|
2014-07-12 00:36:37 +00:00
|
|
|
private Message parseOtrChat(MessagePacket packet, Account account) {
|
2014-12-15 16:36:16 +00:00
|
|
|
final Jid to = packet.getTo();
|
|
|
|
final Jid from = packet.getFrom();
|
|
|
|
if (to == null || from == null) {
|
2014-12-02 22:59:40 +00:00
|
|
|
return null;
|
|
|
|
}
|
2014-12-15 16:36:16 +00:00
|
|
|
boolean properlyAddressed = !to.isBareJid() || account.countPresences() == 1;
|
2014-06-04 10:31:19 +00:00
|
|
|
Conversation conversation = mXmppConnectionService
|
2014-11-06 19:10:03 +00:00
|
|
|
.findOrCreateConversation(account, from.toBareJid(), false);
|
2014-08-11 11:46:32 +00:00
|
|
|
String presence;
|
2014-11-06 19:10:03 +00:00
|
|
|
if (from.isBareJid()) {
|
|
|
|
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
|
|
|
}
|
2014-07-12 00:36:37 +00:00
|
|
|
updateLastseen(packet, account, true);
|
2014-02-19 00:35:23 +00:00
|
|
|
String body = packet.getBody();
|
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()) {
|
2014-03-19 15:16:40 +00:00
|
|
|
if (properlyAddressed) {
|
2014-11-15 23:20:20 +00:00
|
|
|
conversation.startOtrSession(presence,false);
|
2014-03-19 15:16:40 +00:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2014-03-07 23:48:52 +00:00
|
|
|
} else {
|
2014-06-04 10:31:19 +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();
|
2014-03-19 15:16:40 +00:00
|
|
|
if (properlyAddressed) {
|
2014-11-15 23:20:20 +00:00
|
|
|
conversation.startOtrSession(presence, false);
|
2014-03-19 15:16:40 +00:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2014-03-07 23:48:52 +00:00
|
|
|
}
|
2014-02-19 00:35:23 +00:00
|
|
|
}
|
|
|
|
try {
|
|
|
|
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;
|
|
|
|
}
|
2014-11-10 00:24:35 +00:00
|
|
|
Message finishedMessage = new Message(conversation, body, Message.ENCRYPTION_OTR,
|
2014-08-28 09:01:24 +00:00
|
|
|
Message.STATUS_RECEIVED);
|
2014-06-06 16:26:40 +00:00
|
|
|
finishedMessage.setTime(getTimestamp(packet));
|
2014-08-23 13:57:39 +00:00
|
|
|
finishedMessage.setRemoteMsgId(packet.getId());
|
2014-10-07 14:02:52 +00:00
|
|
|
finishedMessage.markable = isMarkable(packet);
|
2014-11-10 00:24:35 +00:00
|
|
|
finishedMessage.setCounterpart(from);
|
2015-02-21 10:06:52 +00:00
|
|
|
extractChatState(conversation,packet);
|
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
|
|
|
|
2014-07-12 00:36:37 +00:00
|
|
|
private Message parseGroupchat(MessagePacket packet, Account account) {
|
2014-02-19 00:35:23 +00:00
|
|
|
int status;
|
2014-11-06 19:10:03 +00:00
|
|
|
final Jid from = packet.getFrom();
|
2014-11-14 19:40:43 +00:00
|
|
|
if (from == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-08-10 13:27:44 +00:00
|
|
|
if (mXmppConnectionService.find(account.pendingConferenceLeaves,
|
2014-11-06 19:10:03 +00:00
|
|
|
account, from.toBareJid()) != null) {
|
2014-07-20 00:26:23 +00:00
|
|
|
return null;
|
|
|
|
}
|
2014-06-04 10:31:19 +00:00
|
|
|
Conversation conversation = mXmppConnectionService
|
2014-11-06 19:10:03 +00:00
|
|
|
.findOrCreateConversation(account, from.toBareJid(), true);
|
2014-03-14 21:40:56 +00:00
|
|
|
if (packet.hasChild("subject")) {
|
2015-01-23 23:22:51 +00:00
|
|
|
conversation.setHasMessagesLeftOnServer(true);
|
2015-01-07 17:34:24 +00:00
|
|
|
conversation.getMucOptions().setSubject(packet.findChild("subject").getContent());
|
2014-07-12 11:42:17 +00:00
|
|
|
mXmppConnectionService.updateConversationUi();
|
2014-03-14 21:40:56 +00:00
|
|
|
return null;
|
|
|
|
}
|
2015-01-07 17:34:24 +00:00
|
|
|
|
|
|
|
final Element x = packet.findChild("x", "http://jabber.org/protocol/muc#user");
|
|
|
|
if (from.isBareJid() && (x == null || !x.hasChild("status"))) {
|
2014-02-19 00:35:23 +00:00
|
|
|
return null;
|
2015-01-07 17:34:24 +00:00
|
|
|
} else if (from.isBareJid() && x.hasChild("status")) {
|
|
|
|
for(Element child : x.getChildren()) {
|
|
|
|
if (child.getName().equals("status")) {
|
|
|
|
String code = child.getAttribute("code");
|
|
|
|
if (code.contains(MucOptions.STATUS_CODE_ROOM_CONFIG_CHANGED)) {
|
|
|
|
mXmppConnectionService.fetchConferenceConfiguration(conversation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-25 23:26:16 +00:00
|
|
|
return null;
|
2014-02-19 00:35:23 +00:00
|
|
|
}
|
2015-01-07 17:34:24 +00:00
|
|
|
|
2014-11-06 19:10:03 +00:00
|
|
|
if (from.getResourcepart().equals(conversation.getMucOptions().getActualNick())) {
|
2014-06-04 10:31:19 +00:00
|
|
|
if (mXmppConnectionService.markMessage(conversation,
|
2015-01-26 00:25:47 +00:00
|
|
|
packet.getId(), Message.STATUS_SEND_RECEIVED)) {
|
2014-05-16 20:46:15 +00:00
|
|
|
return null;
|
2014-12-03 16:05:37 +00:00
|
|
|
} else if (packet.getId() == null) {
|
|
|
|
Message message = conversation.findSentMessageWithBody(packet.getBody());
|
|
|
|
if (message != null) {
|
|
|
|
mXmppConnectionService.markMessage(message,Message.STATUS_SEND_RECEIVED);
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
status = Message.STATUS_SEND;
|
|
|
|
}
|
2014-05-16 20:46:15 +00:00
|
|
|
} else {
|
|
|
|
status = Message.STATUS_SEND;
|
|
|
|
}
|
2014-02-19 00:35:23 +00:00
|
|
|
} else {
|
2014-08-28 09:01:24 +00:00
|
|
|
status = Message.STATUS_RECEIVED;
|
2014-02-19 00:35:23 +00:00
|
|
|
}
|
2014-05-22 14:17:51 +00:00
|
|
|
String pgpBody = getPgpBody(packet);
|
2014-06-06 16:26:40 +00:00
|
|
|
Message finishedMessage;
|
2014-06-04 10:31:19 +00:00
|
|
|
if (pgpBody == null) {
|
2014-11-10 00:24:35 +00:00
|
|
|
finishedMessage = new Message(conversation,
|
2014-07-12 00:36:37 +00:00
|
|
|
packet.getBody(), Message.ENCRYPTION_NONE, status);
|
2014-05-22 14:17:51 +00:00
|
|
|
} else {
|
2014-11-10 00:24:35 +00:00
|
|
|
finishedMessage = new Message(conversation, pgpBody,
|
2014-06-04 10:31:19 +00:00
|
|
|
Message.ENCRYPTION_PGP, status);
|
2014-05-22 14:17:51 +00:00
|
|
|
}
|
2014-08-23 13:57:39 +00:00
|
|
|
finishedMessage.setRemoteMsgId(packet.getId());
|
2014-10-07 14:02:52 +00:00
|
|
|
finishedMessage.markable = isMarkable(packet);
|
2014-11-10 00:24:35 +00:00
|
|
|
finishedMessage.setCounterpart(from);
|
2014-08-28 09:01:24 +00:00
|
|
|
if (status == Message.STATUS_RECEIVED) {
|
2014-08-10 13:27:44 +00:00
|
|
|
finishedMessage.setTrueCounterpart(conversation.getMucOptions()
|
2014-11-06 19:10:03 +00:00
|
|
|
.getTrueCounterpart(from.getResourcepart()));
|
2014-07-29 12:42:17 +00:00
|
|
|
}
|
2014-08-31 14:28:21 +00:00
|
|
|
if (packet.hasChild("delay")
|
|
|
|
&& conversation.hasDuplicateMessage(finishedMessage)) {
|
2014-08-23 13:57:39 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
finishedMessage.setTime(getTimestamp(packet));
|
2014-06-06 16:26:40 +00:00
|
|
|
return finishedMessage;
|
2014-02-19 00:35:23 +00:00
|
|
|
}
|
|
|
|
|
2014-11-06 19:10:03 +00:00
|
|
|
private Message parseCarbonMessage(final MessagePacket packet, final Account account) {
|
2014-02-19 00:35:23 +00:00
|
|
|
int status;
|
2014-11-06 19:10:03 +00:00
|
|
|
final Jid fullJid;
|
2014-02-19 00:35:23 +00:00
|
|
|
Element forwarded;
|
2014-09-12 18:14:19 +00:00
|
|
|
if (packet.hasChild("received", "urn:xmpp:carbons:2")) {
|
|
|
|
forwarded = packet.findChild("received", "urn:xmpp:carbons:2")
|
2014-09-23 09:48:48 +00:00
|
|
|
.findChild("forwarded", "urn:xmpp:forward:0");
|
2014-08-28 09:01:24 +00:00
|
|
|
status = Message.STATUS_RECEIVED;
|
2014-09-12 18:14:19 +00:00
|
|
|
} else if (packet.hasChild("sent", "urn:xmpp:carbons:2")) {
|
|
|
|
forwarded = packet.findChild("sent", "urn:xmpp:carbons:2")
|
2014-09-23 09:48:48 +00:00
|
|
|
.findChild("forwarded", "urn:xmpp:forward:0");
|
2014-02-19 00:35:23 +00:00
|
|
|
status = Message.STATUS_SEND;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2014-06-04 10:31:19 +00:00
|
|
|
if (forwarded == null) {
|
2014-03-31 07:45:39 +00:00
|
|
|
return null;
|
|
|
|
}
|
2014-02-19 00:35:23 +00:00
|
|
|
Element message = forwarded.findChild("message");
|
2014-10-02 19:07:11 +00:00
|
|
|
if (message == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (!message.hasChild("body")) {
|
2014-08-31 14:28:21 +00:00
|
|
|
if (status == Message.STATUS_RECEIVED
|
|
|
|
&& message.getAttribute("from") != null) {
|
2014-09-11 11:18:29 +00:00
|
|
|
parseNonMessage(message, account);
|
2014-10-02 19:07:11 +00:00
|
|
|
} else if (status == Message.STATUS_SEND
|
|
|
|
&& message.hasChild("displayed", "urn:xmpp:chat-markers:0")) {
|
2014-11-10 00:24:35 +00:00
|
|
|
final Jid to = message.getAttributeAsJid("to");
|
2014-10-02 19:07:11 +00:00
|
|
|
if (to != null) {
|
2014-11-06 19:10:03 +00:00
|
|
|
final Conversation conversation = mXmppConnectionService.find(
|
2014-10-02 19:07:11 +00:00
|
|
|
mXmppConnectionService.getConversations(), account,
|
2014-11-06 19:10:03 +00:00
|
|
|
to.toBareJid());
|
2014-10-02 19:07:11 +00:00
|
|
|
if (conversation != null) {
|
2015-01-02 11:04:33 +00:00
|
|
|
mXmppConnectionService.markRead(conversation);
|
2014-10-02 19:07:11 +00:00
|
|
|
}
|
|
|
|
}
|
2014-07-11 12:49:06 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2014-08-28 09:01:24 +00:00
|
|
|
if (status == Message.STATUS_RECEIVED) {
|
2014-11-10 00:24:35 +00:00
|
|
|
fullJid = message.getAttributeAsJid("from");
|
2014-08-10 13:27:44 +00:00
|
|
|
if (fullJid == null) {
|
2014-07-27 20:31:33 +00:00
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
updateLastseen(message, account, true);
|
|
|
|
}
|
2014-02-19 00:35:23 +00:00
|
|
|
} else {
|
2014-11-10 00:24:35 +00:00
|
|
|
fullJid = message.getAttributeAsJid("to");
|
2014-07-27 20:31:33 +00:00
|
|
|
if (fullJid == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-07-16 10:42:44 +00:00
|
|
|
}
|
2015-01-04 11:37:22 +00:00
|
|
|
if (message.hasChild("x","http://jabber.org/protocol/muc#user")
|
|
|
|
&& "chat".equals(message.getAttribute("type"))) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-06-04 10:31:19 +00:00
|
|
|
Conversation conversation = mXmppConnectionService
|
2014-11-06 19:10:03 +00:00
|
|
|
.findOrCreateConversation(account, fullJid.toBareJid(), false);
|
2014-06-04 10:31:19 +00:00
|
|
|
String pgpBody = getPgpBody(message);
|
2014-06-06 16:26:40 +00:00
|
|
|
Message finishedMessage;
|
2014-06-04 10:31:19 +00:00
|
|
|
if (pgpBody != null) {
|
2014-11-10 00:24:35 +00:00
|
|
|
finishedMessage = new Message(conversation, pgpBody,
|
2014-07-12 00:36:37 +00:00
|
|
|
Message.ENCRYPTION_PGP, status);
|
2014-06-04 09:55:38 +00:00
|
|
|
} else {
|
2014-06-04 10:31:19 +00:00
|
|
|
String body = message.findChild("body").getContent();
|
2014-11-10 00:24:35 +00:00
|
|
|
finishedMessage = new Message(conversation, body,
|
2014-07-12 00:36:37 +00:00
|
|
|
Message.ENCRYPTION_NONE, status);
|
2014-06-04 09:55:38 +00:00
|
|
|
}
|
2015-02-21 10:06:52 +00:00
|
|
|
extractChatState(conversation,message);
|
2014-06-06 16:26:40 +00:00
|
|
|
finishedMessage.setTime(getTimestamp(message));
|
2014-08-23 13:57:39 +00:00
|
|
|
finishedMessage.setRemoteMsgId(message.getAttribute("id"));
|
2014-10-07 14:02:52 +00:00
|
|
|
finishedMessage.markable = isMarkable(message);
|
2014-11-10 00:24:35 +00:00
|
|
|
finishedMessage.setCounterpart(fullJid);
|
2014-08-10 13:27:44 +00:00
|
|
|
if (conversation.getMode() == Conversation.MODE_MULTI
|
2014-11-06 19:10:03 +00:00
|
|
|
&& !fullJid.isBareJid()) {
|
2014-08-10 13:27:44 +00:00
|
|
|
finishedMessage.setType(Message.TYPE_PRIVATE);
|
|
|
|
finishedMessage.setTrueCounterpart(conversation.getMucOptions()
|
2014-11-06 19:10:03 +00:00
|
|
|
.getTrueCounterpart(fullJid.getResourcepart()));
|
2014-08-25 17:20:53 +00:00
|
|
|
if (conversation.hasDuplicateMessage(finishedMessage)) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-08-10 13:27:44 +00:00
|
|
|
}
|
2014-06-06 16:26:40 +00:00
|
|
|
return finishedMessage;
|
2014-02-19 00:35:23 +00:00
|
|
|
}
|
2014-02-20 16:00:50 +00:00
|
|
|
|
2014-12-05 00:54:16 +00:00
|
|
|
private Message parseMamMessage(MessagePacket packet, final Account account) {
|
|
|
|
final Element result = packet.findChild("result","urn:xmpp:mam:0");
|
|
|
|
if (result == null ) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-12-17 05:59:58 +00:00
|
|
|
final MessageArchiveService.Query query = this.mXmppConnectionService.getMessageArchiveService().findQuery(result.getAttribute("queryid"));
|
|
|
|
if (query!=null) {
|
|
|
|
query.incrementTotalCount();
|
|
|
|
}
|
2014-12-05 00:54:16 +00:00
|
|
|
final Element forwarded = result.findChild("forwarded","urn:xmpp:forward:0");
|
|
|
|
if (forwarded == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
final Element message = forwarded.findChild("message");
|
|
|
|
if (message == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
final Element body = message.findChild("body");
|
|
|
|
if (body == null || message.hasChild("private","urn:xmpp:carbons:2") || message.hasChild("no-copy","urn:xmpp:hints")) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
int encryption;
|
|
|
|
String content = getPgpBody(message);
|
|
|
|
if (content != null) {
|
|
|
|
encryption = Message.ENCRYPTION_PGP;
|
|
|
|
} else {
|
|
|
|
encryption = Message.ENCRYPTION_NONE;
|
|
|
|
content = body.getContent();
|
|
|
|
}
|
|
|
|
if (content == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
final long timestamp = getTimestamp(forwarded);
|
|
|
|
final Jid to = message.getAttributeAsJid("to");
|
|
|
|
final Jid from = message.getAttributeAsJid("from");
|
|
|
|
Jid counterpart;
|
|
|
|
int status;
|
|
|
|
Conversation conversation;
|
|
|
|
if (from!=null && to != null && from.toBareJid().equals(account.getJid().toBareJid())) {
|
|
|
|
status = Message.STATUS_SEND;
|
2014-12-13 11:25:52 +00:00
|
|
|
conversation = this.mXmppConnectionService.findOrCreateConversation(account,to.toBareJid(),false,query);
|
2014-12-05 00:54:16 +00:00
|
|
|
counterpart = to;
|
|
|
|
} else if (from !=null && to != null) {
|
|
|
|
status = Message.STATUS_RECEIVED;
|
2014-12-13 11:25:52 +00:00
|
|
|
conversation = this.mXmppConnectionService.findOrCreateConversation(account,from.toBareJid(),false,query);
|
2014-12-05 00:54:16 +00:00
|
|
|
counterpart = from;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
Message finishedMessage = new Message(conversation,content,encryption,status);
|
|
|
|
finishedMessage.setTime(timestamp);
|
|
|
|
finishedMessage.setCounterpart(counterpart);
|
2014-12-09 20:41:49 +00:00
|
|
|
finishedMessage.setRemoteMsgId(message.getAttribute("id"));
|
|
|
|
finishedMessage.setServerMsgId(result.getAttribute("id"));
|
2014-12-17 05:59:58 +00:00
|
|
|
if (conversation.hasDuplicateMessage(finishedMessage)) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-12-15 22:06:29 +00:00
|
|
|
if (query!=null) {
|
2014-12-17 05:59:58 +00:00
|
|
|
query.incrementMessageCount();
|
2014-12-09 20:41:49 +00:00
|
|
|
}
|
2014-12-05 00:54:16 +00:00
|
|
|
return finishedMessage;
|
|
|
|
}
|
|
|
|
|
2014-11-06 19:10:03 +00:00
|
|
|
private void parseError(final MessagePacket packet, final Account account) {
|
|
|
|
final Jid from = packet.getFrom();
|
|
|
|
mXmppConnectionService.markMessage(account, from.toBareJid(),
|
2014-06-04 10:31:19 +00:00
|
|
|
packet.getId(), Message.STATUS_SEND_FAILED);
|
2014-02-20 16:00:50 +00:00
|
|
|
}
|
2014-07-12 00:36:37 +00:00
|
|
|
|
2014-09-11 11:18:29 +00:00
|
|
|
private void parseNonMessage(Element packet, Account account) {
|
2014-11-10 00:24:35 +00:00
|
|
|
final Jid from = packet.getAttributeAsJid("from");
|
2015-02-21 10:06:52 +00:00
|
|
|
if (extractChatState(from == null ? null : mXmppConnectionService.find(account,from), packet)) {
|
|
|
|
mXmppConnectionService.updateConversationUi();
|
|
|
|
}
|
2015-01-07 15:45:44 +00:00
|
|
|
Element invite = extractInvite(packet);
|
|
|
|
if (invite != null) {
|
2015-01-07 17:34:24 +00:00
|
|
|
Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, from, true);
|
2015-01-07 15:45:44 +00:00
|
|
|
if (!conversation.getMucOptions().online()) {
|
|
|
|
Element password = invite.findChild("password");
|
|
|
|
conversation.getMucOptions().setPassword(password == null ? null : password.getContent());
|
|
|
|
mXmppConnectionService.databaseBackend.updateConversation(conversation);
|
|
|
|
mXmppConnectionService.joinMuc(conversation);
|
|
|
|
mXmppConnectionService.updateConversationUi();
|
|
|
|
}
|
|
|
|
}
|
2014-08-10 13:27:44 +00:00
|
|
|
if (packet.hasChild("event", "http://jabber.org/protocol/pubsub#event")) {
|
|
|
|
Element event = packet.findChild("event",
|
|
|
|
"http://jabber.org/protocol/pubsub#event");
|
2014-11-06 19:10:03 +00:00
|
|
|
parseEvent(event, from, account);
|
2014-09-23 09:48:48 +00:00
|
|
|
} else if (from != null
|
|
|
|
&& packet.hasChild("displayed", "urn:xmpp:chat-markers:0")) {
|
2014-07-12 00:36:37 +00:00
|
|
|
String id = packet
|
|
|
|
.findChild("displayed", "urn:xmpp:chat-markers:0")
|
|
|
|
.getAttribute("id");
|
|
|
|
updateLastseen(packet, account, true);
|
2014-11-06 19:10:03 +00:00
|
|
|
mXmppConnectionService.markMessage(account, from.toBareJid(),
|
2014-09-23 09:48:48 +00:00
|
|
|
id, Message.STATUS_SEND_DISPLAYED);
|
|
|
|
} else if (from != null
|
|
|
|
&& packet.hasChild("received", "urn:xmpp:chat-markers:0")) {
|
2014-07-12 00:36:37 +00:00
|
|
|
String id = packet.findChild("received", "urn:xmpp:chat-markers:0")
|
|
|
|
.getAttribute("id");
|
|
|
|
updateLastseen(packet, account, false);
|
2014-11-06 19:10:03 +00:00
|
|
|
mXmppConnectionService.markMessage(account, from.toBareJid(),
|
2014-09-23 09:48:48 +00:00
|
|
|
id, Message.STATUS_SEND_RECEIVED);
|
|
|
|
} else if (from != null
|
|
|
|
&& packet.hasChild("received", "urn:xmpp:receipts")) {
|
2014-09-20 13:49:25 +00:00
|
|
|
String id = packet.findChild("received", "urn:xmpp:receipts")
|
|
|
|
.getAttribute("id");
|
|
|
|
updateLastseen(packet, account, false);
|
2014-11-06 19:10:03 +00:00
|
|
|
mXmppConnectionService.markMessage(account, from.toBareJid(),
|
2014-09-23 09:48:48 +00:00
|
|
|
id, Message.STATUS_SEND_RECEIVED);
|
2015-01-07 15:45:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private Element extractInvite(Element message) {
|
|
|
|
Element x = message.findChild("x","http://jabber.org/protocol/muc#user");
|
|
|
|
if (x == null) {
|
|
|
|
x = message.findChild("x","jabber:x:conference");
|
|
|
|
}
|
|
|
|
if (x != null && x.hasChild("invite")) {
|
|
|
|
return x;
|
|
|
|
} else {
|
|
|
|
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");
|
2014-11-20 17:42:47 +00:00
|
|
|
if (items == null) {
|
|
|
|
return;
|
|
|
|
}
|
2014-08-04 23:36:17 +00:00
|
|
|
String node = items.getAttribute("node");
|
2014-11-20 17:42:47 +00:00
|
|
|
if (node == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (node.equals("urn:xmpp:avatar:metadata")) {
|
|
|
|
Avatar avatar = Avatar.parseMetadata(items);
|
|
|
|
if (avatar != null) {
|
|
|
|
avatar.owner = from;
|
|
|
|
if (mXmppConnectionService.getFileBackend().isAvatarCached(
|
|
|
|
avatar)) {
|
|
|
|
if (account.getJid().toBareJid().equals(from)) {
|
|
|
|
if (account.setAvatar(avatar.getFilename())) {
|
|
|
|
mXmppConnectionService.databaseBackend
|
|
|
|
.updateAccount(account);
|
2014-08-15 11:11:33 +00:00
|
|
|
}
|
2014-11-20 17:42:47 +00:00
|
|
|
mXmppConnectionService.getAvatarService().clear(
|
|
|
|
account);
|
|
|
|
mXmppConnectionService.updateConversationUi();
|
|
|
|
mXmppConnectionService.updateAccountUi();
|
2014-08-05 20:58:46 +00:00
|
|
|
} else {
|
2014-11-20 17:42:47 +00:00
|
|
|
Contact contact = account.getRoster().getContact(
|
|
|
|
from);
|
|
|
|
contact.setAvatar(avatar.getFilename());
|
|
|
|
mXmppConnectionService.getAvatarService().clear(
|
|
|
|
contact);
|
|
|
|
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
|
|
|
}
|
|
|
|
} else if (node.equals("http://jabber.org/protocol/nick")) {
|
|
|
|
Element item = items.findChild("item");
|
|
|
|
if (item != null) {
|
|
|
|
Element nick = item.findChild("nick",
|
|
|
|
"http://jabber.org/protocol/nick");
|
|
|
|
if (nick != null) {
|
|
|
|
if (from != null) {
|
|
|
|
Contact contact = account.getRoster().getContact(
|
|
|
|
from);
|
|
|
|
contact.setPresenceName(nick.getContent());
|
|
|
|
mXmppConnectionService.getAvatarService().clear(account);
|
|
|
|
mXmppConnectionService.updateConversationUi();
|
|
|
|
mXmppConnectionService.updateAccountUi();
|
2014-09-04 08:50:06 +00:00
|
|
|
}
|
|
|
|
}
|
2014-08-05 20:58:46 +00:00
|
|
|
}
|
2014-08-04 23:36:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-04 16:44:15 +00:00
|
|
|
private String getPgpBody(Element message) {
|
|
|
|
Element child = message.findChild("x", "jabber:x:encrypted");
|
2014-06-04 10:31:19 +00:00
|
|
|
if (child == null) {
|
2014-05-22 14:17:51 +00:00
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return child.getContent();
|
2014-02-27 23:22:56 +00:00
|
|
|
}
|
|
|
|
}
|
2014-07-12 00:36:37 +00:00
|
|
|
|
2014-10-07 14:02:52 +00:00
|
|
|
private boolean isMarkable(Element message) {
|
|
|
|
return message.hasChild("markable", "urn:xmpp:chat-markers:0");
|
2014-06-04 16:44:15 +00:00
|
|
|
}
|
2014-06-06 16:49:35 +00:00
|
|
|
|
2014-07-12 00:36:37 +00:00
|
|
|
@Override
|
|
|
|
public void onMessagePacketReceived(Account account, MessagePacket packet) {
|
|
|
|
Message message = null;
|
2014-09-04 08:50:06 +00:00
|
|
|
this.parseNick(packet, account);
|
2014-09-11 11:18:29 +00:00
|
|
|
if ((packet.getType() == MessagePacket.TYPE_CHAT || packet.getType() == MessagePacket.TYPE_NORMAL)) {
|
2014-07-12 00:36:37 +00:00
|
|
|
if ((packet.getBody() != null)
|
|
|
|
&& (packet.getBody().startsWith("?OTR"))) {
|
|
|
|
message = this.parseOtrChat(packet, account);
|
|
|
|
if (message != null) {
|
|
|
|
message.markUnread();
|
|
|
|
}
|
2015-01-07 15:45:44 +00:00
|
|
|
} else if (packet.hasChild("body") && extractInvite(packet) == null) {
|
2014-07-12 00:36:37 +00:00
|
|
|
message = this.parseChat(packet, account);
|
2014-08-25 17:20:53 +00:00
|
|
|
if (message != null) {
|
|
|
|
message.markUnread();
|
|
|
|
}
|
2014-09-12 18:14:19 +00:00
|
|
|
} else if (packet.hasChild("received", "urn:xmpp:carbons:2")
|
|
|
|
|| (packet.hasChild("sent", "urn:xmpp:carbons:2"))) {
|
2014-07-12 00:36:37 +00:00
|
|
|
message = this.parseCarbonMessage(packet, account);
|
|
|
|
if (message != null) {
|
|
|
|
if (message.getStatus() == Message.STATUS_SEND) {
|
2014-10-17 09:01:38 +00:00
|
|
|
account.activateGracePeriod();
|
2015-01-02 11:04:33 +00:00
|
|
|
mXmppConnectionService.markRead(message.getConversation());
|
2014-07-12 00:36:37 +00:00
|
|
|
} else {
|
|
|
|
message.markUnread();
|
|
|
|
}
|
|
|
|
}
|
2014-12-05 00:54:16 +00:00
|
|
|
} else if (packet.hasChild("result","urn:xmpp:mam:0")) {
|
|
|
|
message = parseMamMessage(packet, account);
|
|
|
|
if (message != null) {
|
|
|
|
Conversation conversation = message.getConversation();
|
|
|
|
conversation.add(message);
|
|
|
|
mXmppConnectionService.databaseBackend.createMessage(message);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
} else if (packet.hasChild("fin","urn:xmpp:mam:0")) {
|
|
|
|
Element fin = packet.findChild("fin","urn:xmpp:mam:0");
|
|
|
|
mXmppConnectionService.getMessageArchiveService().processFin(fin);
|
2014-07-23 12:30:27 +00:00
|
|
|
} else {
|
2014-09-11 11:18:29 +00:00
|
|
|
parseNonMessage(packet, account);
|
2014-07-12 00:36:37 +00:00
|
|
|
}
|
|
|
|
} else if (packet.getType() == MessagePacket.TYPE_GROUPCHAT) {
|
|
|
|
message = this.parseGroupchat(packet, account);
|
|
|
|
if (message != null) {
|
2014-08-28 09:01:24 +00:00
|
|
|
if (message.getStatus() == Message.STATUS_RECEIVED) {
|
2014-07-12 00:36:37 +00:00
|
|
|
message.markUnread();
|
|
|
|
} else {
|
2015-01-02 11:04:33 +00:00
|
|
|
mXmppConnectionService.markRead(message.getConversation());
|
2014-10-17 09:01:38 +00:00
|
|
|
account.activateGracePeriod();
|
2014-07-12 00:36:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (packet.getType() == MessagePacket.TYPE_ERROR) {
|
|
|
|
this.parseError(packet, account);
|
|
|
|
return;
|
2014-08-04 23:36:17 +00:00
|
|
|
} else if (packet.getType() == MessagePacket.TYPE_HEADLINE) {
|
|
|
|
this.parseHeadline(packet, account);
|
|
|
|
return;
|
2014-07-12 00:36:37 +00:00
|
|
|
}
|
|
|
|
if ((message == null) || (message.getBody() == null)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ((mXmppConnectionService.confirmMessages())
|
|
|
|
&& ((packet.getId() != null))) {
|
|
|
|
if (packet.hasChild("markable", "urn:xmpp:chat-markers:0")) {
|
2014-08-10 13:27:44 +00:00
|
|
|
MessagePacket receipt = mXmppConnectionService
|
|
|
|
.getMessageGenerator().received(account, packet,
|
|
|
|
"urn:xmpp:chat-markers:0");
|
2014-07-23 23:04:25 +00:00
|
|
|
mXmppConnectionService.sendMessagePacket(account, receipt);
|
|
|
|
}
|
|
|
|
if (packet.hasChild("request", "urn:xmpp:receipts")) {
|
2014-08-10 13:27:44 +00:00
|
|
|
MessagePacket receipt = mXmppConnectionService
|
|
|
|
.getMessageGenerator().received(account, packet,
|
|
|
|
"urn:xmpp:receipts");
|
2014-07-23 23:04:25 +00:00
|
|
|
mXmppConnectionService.sendMessagePacket(account, receipt);
|
2014-07-12 00:36:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Conversation conversation = message.getConversation();
|
2014-10-19 21:13:55 +00:00
|
|
|
conversation.add(message);
|
2014-12-08 20:59:14 +00:00
|
|
|
if (account.getXmppConnection() != null && account.getXmppConnection().getFeatures().advancedStreamFeaturesLoaded()) {
|
2014-12-09 21:50:53 +00:00
|
|
|
if (conversation.setLastMessageTransmitted(System.currentTimeMillis())) {
|
2014-12-08 20:59:14 +00:00
|
|
|
mXmppConnectionService.updateConversation(conversation);
|
|
|
|
}
|
|
|
|
}
|
2014-10-28 17:02:12 +00:00
|
|
|
|
|
|
|
if (message.getStatus() == Message.STATUS_RECEIVED
|
|
|
|
&& conversation.getOtrSession() != null
|
|
|
|
&& !conversation.getOtrSession().getSessionID().getUserID()
|
2014-11-09 15:21:13 +00:00
|
|
|
.equals(message.getCounterpart().getResourcepart())) {
|
2014-10-28 17:02:12 +00:00
|
|
|
conversation.endOtrIfNeeded();
|
|
|
|
}
|
|
|
|
|
2014-07-12 00:36:37 +00:00
|
|
|
if (packet.getType() != MessagePacket.TYPE_ERROR) {
|
2014-09-08 21:58:37 +00:00
|
|
|
if (message.getEncryption() == Message.ENCRYPTION_NONE
|
|
|
|
|| mXmppConnectionService.saveEncryptedMessages()) {
|
2014-09-08 11:37:22 +00:00
|
|
|
mXmppConnectionService.databaseBackend.createMessage(message);
|
|
|
|
}
|
2014-07-12 00:36:37 +00:00
|
|
|
}
|
2015-01-11 21:18:18 +00:00
|
|
|
final HttpConnectionManager manager = this.mXmppConnectionService.getHttpConnectionManager();
|
|
|
|
if (message.trusted() && message.bodyContainsDownloadable() && manager.getAutoAcceptFileSize() > 0) {
|
|
|
|
manager.createNewConnection(message);
|
2014-12-05 00:54:16 +00:00
|
|
|
} else if (!message.isRead()) {
|
2014-09-29 16:28:13 +00:00
|
|
|
mXmppConnectionService.getNotificationService().push(message);
|
2014-09-28 13:21:56 +00:00
|
|
|
}
|
|
|
|
mXmppConnectionService.updateConversationUi();
|
2014-07-12 00:36:37 +00:00
|
|
|
}
|
2014-08-04 23:36:17 +00:00
|
|
|
|
|
|
|
private void parseHeadline(MessagePacket packet, Account account) {
|
2014-08-10 13:27:44 +00:00
|
|
|
if (packet.hasChild("event", "http://jabber.org/protocol/pubsub#event")) {
|
|
|
|
Element event = packet.findChild("event",
|
|
|
|
"http://jabber.org/protocol/pubsub#event");
|
|
|
|
parseEvent(event, packet.getFrom(), account);
|
2014-08-04 23:36:17 +00:00
|
|
|
}
|
|
|
|
}
|
2014-09-04 08:50:06 +00:00
|
|
|
|
|
|
|
private void parseNick(MessagePacket packet, Account account) {
|
|
|
|
Element nick = packet.findChild("nick",
|
|
|
|
"http://jabber.org/protocol/nick");
|
|
|
|
if (nick != null) {
|
|
|
|
if (packet.getFrom() != null) {
|
|
|
|
Contact contact = account.getRoster().getContact(
|
|
|
|
packet.getFrom());
|
|
|
|
contact.setPresenceName(nick.getContent());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-02-19 00:35:23 +00:00
|
|
|
}
|