2014-05-14 10:56:34 +00:00
|
|
|
package eu.siacs.conversations.parser;
|
2014-02-19 00:35:23 +00:00
|
|
|
|
|
|
|
import net.java.otr4j.session.Session;
|
|
|
|
import net.java.otr4j.session.SessionStatus;
|
2014-02-28 17:46:01 +00:00
|
|
|
import eu.siacs.conversations.entities.Account;
|
|
|
|
import eu.siacs.conversations.entities.Conversation;
|
|
|
|
import eu.siacs.conversations.entities.Message;
|
|
|
|
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-03-10 18:22:13 +00:00
|
|
|
import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
|
2014-02-19 00:35:23 +00:00
|
|
|
|
2014-06-06 16:26:40 +00:00
|
|
|
public class MessageParser extends AbstractParser {
|
2014-06-04 10:31:19 +00:00
|
|
|
|
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
|
|
|
|
2014-06-04 09:55:38 +00:00
|
|
|
public Message parseChat(MessagePacket packet, Account account) {
|
2014-02-19 00:35:23 +00:00
|
|
|
String[] fromParts = packet.getFrom().split("/");
|
2014-06-04 10:31:19 +00:00
|
|
|
Conversation conversation = mXmppConnectionService
|
|
|
|
.findOrCreateConversation(account, fromParts[0], false);
|
2014-06-04 16:44:15 +00:00
|
|
|
conversation.setLatestMarkableMessageId(getMarkableMessageId(packet));
|
2014-06-06 16:49:35 +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-06-22 16:21:04 +00:00
|
|
|
finishedMessage = new Message(conversation, packet.getFrom(), pgpBody,
|
2014-06-04 10:31:19 +00:00
|
|
|
Message.ENCRYPTION_PGP, Message.STATUS_RECIEVED);
|
2014-06-04 09:55:38 +00:00
|
|
|
} else {
|
2014-06-22 16:21:04 +00:00
|
|
|
finishedMessage = new Message(conversation, packet.getFrom(),
|
2014-06-04 10:31:19 +00:00
|
|
|
packet.getBody(), Message.ENCRYPTION_NONE,
|
|
|
|
Message.STATUS_RECIEVED);
|
2014-06-04 09:55:38 +00:00
|
|
|
}
|
2014-06-22 16:21:04 +00:00
|
|
|
finishedMessage.setTime(getTimestamp(packet));
|
|
|
|
return finishedMessage;
|
2014-02-27 23:22:56 +00:00
|
|
|
}
|
2014-06-04 09:55:38 +00:00
|
|
|
|
2014-05-14 10:56:34 +00:00
|
|
|
public Message parseOtrChat(MessagePacket packet, Account account) {
|
2014-06-04 10:31:19 +00:00
|
|
|
boolean properlyAddressed = (packet.getTo().split("/").length == 2)
|
|
|
|
|| (account.countPresences() == 1);
|
2014-02-19 00:35:23 +00:00
|
|
|
String[] fromParts = packet.getFrom().split("/");
|
2014-06-04 10:31:19 +00:00
|
|
|
Conversation conversation = mXmppConnectionService
|
|
|
|
.findOrCreateConversation(account, fromParts[0], false);
|
2014-06-06 16:49:35 +00:00
|
|
|
updateLastseen(packet, account,true);
|
2014-02-19 00:35:23 +00:00
|
|
|
String body = packet.getBody();
|
|
|
|
if (!conversation.hasValidOtrSession()) {
|
2014-03-19 15:16:40 +00:00
|
|
|
if (properlyAddressed) {
|
2014-06-04 10:31:19 +00:00
|
|
|
conversation.startOtrSession(
|
|
|
|
mXmppConnectionService.getApplicationContext(),
|
|
|
|
fromParts[1], 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-03-12 18:47:42 +00:00
|
|
|
if (!foreignPresence.equals(fromParts[1])) {
|
2014-06-25 14:55:47 +00:00
|
|
|
conversation.endOtrIfNeeded();
|
2014-03-19 15:16:40 +00:00
|
|
|
if (properlyAddressed) {
|
2014-06-04 10:31:19 +00:00
|
|
|
conversation.startOtrSession(
|
|
|
|
mXmppConnectionService.getApplicationContext(),
|
|
|
|
fromParts[1], 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)) {
|
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)) {
|
|
|
|
conversation.resetOtrSession();
|
|
|
|
}
|
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-06-04 16:44:15 +00:00
|
|
|
conversation.setLatestMarkableMessageId(getMarkableMessageId(packet));
|
2014-06-06 16:26:40 +00:00
|
|
|
Message finishedMessage = new Message(conversation, packet.getFrom(), body,
|
2014-06-04 10:31:19 +00:00
|
|
|
Message.ENCRYPTION_OTR, Message.STATUS_RECIEVED);
|
2014-06-06 16:26:40 +00:00
|
|
|
finishedMessage.setTime(getTimestamp(packet));
|
|
|
|
return finishedMessage;
|
2014-02-19 00:35:23 +00:00
|
|
|
} catch (Exception e) {
|
2014-06-25 14:55:47 +00:00
|
|
|
String receivedId = packet.getId();
|
|
|
|
if (receivedId!=null) {
|
2014-06-26 14:42:24 +00:00
|
|
|
mXmppConnectionService.replyWithNotAcceptable(account,packet);
|
2014-06-25 14:55:47 +00:00
|
|
|
}
|
2014-06-26 14:42:24 +00:00
|
|
|
conversation.endOtrIfNeeded();
|
2014-02-19 00:35:23 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2014-06-04 10:31:19 +00:00
|
|
|
|
2014-05-14 10:56:34 +00:00
|
|
|
public Message parseGroupchat(MessagePacket packet, Account account) {
|
2014-02-19 00:35:23 +00:00
|
|
|
int status;
|
|
|
|
String[] fromParts = packet.getFrom().split("/");
|
2014-06-04 10:31:19 +00:00
|
|
|
Conversation conversation = mXmppConnectionService
|
|
|
|
.findOrCreateConversation(account, fromParts[0], true);
|
2014-03-14 21:40:56 +00:00
|
|
|
if (packet.hasChild("subject")) {
|
2014-06-04 10:31:19 +00:00
|
|
|
conversation.getMucOptions().setSubject(
|
|
|
|
packet.findChild("subject").getContent());
|
2014-05-14 10:56:34 +00:00
|
|
|
mXmppConnectionService.updateUi(conversation, false);
|
2014-03-14 21:40:56 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if ((fromParts.length == 1)) {
|
2014-02-19 00:35:23 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
String counterPart = fromParts[1];
|
2014-03-03 04:01:02 +00:00
|
|
|
if (counterPart.equals(conversation.getMucOptions().getNick())) {
|
2014-06-04 10:31:19 +00:00
|
|
|
if (mXmppConnectionService.markMessage(conversation,
|
|
|
|
packet.getId(), Message.STATUS_SEND)) {
|
2014-05-16 20:46:15 +00:00
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
status = Message.STATUS_SEND;
|
|
|
|
}
|
2014-02-19 00:35:23 +00:00
|
|
|
} else {
|
|
|
|
status = Message.STATUS_RECIEVED;
|
|
|
|
}
|
2014-05-22 14:17:51 +00:00
|
|
|
String pgpBody = getPgpBody(packet);
|
2014-06-04 16:44:15 +00:00
|
|
|
conversation.setLatestMarkableMessageId(getMarkableMessageId(packet));
|
2014-06-06 16:26:40 +00:00
|
|
|
Message finishedMessage;
|
2014-06-04 10:31:19 +00:00
|
|
|
if (pgpBody == null) {
|
2014-06-06 16:26:40 +00:00
|
|
|
finishedMessage = new Message(conversation, counterPart, packet.getBody(),
|
2014-06-04 10:31:19 +00:00
|
|
|
Message.ENCRYPTION_NONE, status);
|
2014-05-22 14:17:51 +00:00
|
|
|
} else {
|
2014-06-06 16:26:40 +00:00
|
|
|
finishedMessage= new Message(conversation, counterPart, pgpBody,
|
2014-06-04 10:31:19 +00:00
|
|
|
Message.ENCRYPTION_PGP, status);
|
2014-05-22 14:17:51 +00:00
|
|
|
}
|
2014-06-06 16:26:40 +00:00
|
|
|
finishedMessage.setTime(getTimestamp(packet));
|
|
|
|
return finishedMessage;
|
2014-02-19 00:35:23 +00:00
|
|
|
}
|
|
|
|
|
2014-06-04 10:31:19 +00:00
|
|
|
public Message parseCarbonMessage(MessagePacket packet, Account account) {
|
2014-02-19 00:35:23 +00:00
|
|
|
int status;
|
|
|
|
String fullJid;
|
|
|
|
Element forwarded;
|
|
|
|
if (packet.hasChild("received")) {
|
2014-06-04 10:31:19 +00:00
|
|
|
forwarded = packet.findChild("received").findChild("forwarded");
|
2014-02-19 00:35:23 +00:00
|
|
|
status = Message.STATUS_RECIEVED;
|
|
|
|
} else if (packet.hasChild("sent")) {
|
2014-06-04 10:31:19 +00:00
|
|
|
forwarded = packet.findChild("sent").findChild("forwarded");
|
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");
|
|
|
|
if ((message == null) || (!message.hasChild("body")))
|
|
|
|
return null; // either malformed or boring
|
|
|
|
if (status == Message.STATUS_RECIEVED) {
|
|
|
|
fullJid = message.getAttribute("from");
|
2014-06-06 16:49:35 +00:00
|
|
|
updateLastseen(message, account,true);
|
2014-02-19 00:35:23 +00:00
|
|
|
} else {
|
|
|
|
fullJid = message.getAttribute("to");
|
|
|
|
}
|
|
|
|
String[] parts = fullJid.split("/");
|
2014-06-04 10:31:19 +00:00
|
|
|
Conversation conversation = mXmppConnectionService
|
|
|
|
.findOrCreateConversation(account, parts[0], false);
|
2014-06-04 16:44:15 +00:00
|
|
|
conversation.setLatestMarkableMessageId(getMarkableMessageId(packet));
|
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-06-06 16:26:40 +00:00
|
|
|
finishedMessage = new Message(conversation, fullJid, pgpBody,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-06-06 16:26:40 +00:00
|
|
|
finishedMessage= new Message(conversation, fullJid, body,Message.ENCRYPTION_NONE, status);
|
2014-06-04 09:55:38 +00:00
|
|
|
}
|
2014-06-06 16:26:40 +00:00
|
|
|
finishedMessage.setTime(getTimestamp(message));
|
|
|
|
return finishedMessage;
|
2014-02-19 00:35:23 +00:00
|
|
|
}
|
2014-02-20 16:00:50 +00:00
|
|
|
|
2014-05-14 10:56:34 +00:00
|
|
|
public void parseError(MessagePacket packet, Account account) {
|
2014-04-11 07:13:56 +00:00
|
|
|
String[] fromParts = packet.getFrom().split("/");
|
2014-06-04 10:31:19 +00:00
|
|
|
mXmppConnectionService.markMessage(account, fromParts[0],
|
|
|
|
packet.getId(), Message.STATUS_SEND_FAILED);
|
2014-02-20 16:00:50 +00:00
|
|
|
}
|
2014-06-06 16:49:35 +00:00
|
|
|
|
|
|
|
public void parseNormal(MessagePacket packet, Account account) {
|
|
|
|
if (packet.hasChild("displayed","urn:xmpp:chat-markers:0")) {
|
|
|
|
String id = packet.findChild("displayed","urn:xmpp:chat-markers:0").getAttribute("id");
|
|
|
|
String[] fromParts = packet.getFrom().split("/");
|
|
|
|
updateLastseen(packet, account,true);
|
|
|
|
mXmppConnectionService.markMessage(account,fromParts[0], id, Message.STATUS_SEND_DISPLAYED);
|
|
|
|
} else if (packet.hasChild("received","urn:xmpp:chat-markers:0")) {
|
|
|
|
String id = packet.findChild("received","urn:xmpp:chat-markers:0").getAttribute("id");
|
|
|
|
String[] fromParts = packet.getFrom().split("/");
|
|
|
|
updateLastseen(packet, account,false);
|
|
|
|
mXmppConnectionService.markMessage(account,fromParts[0], id, Message.STATUS_SEND_RECEIVED);
|
|
|
|
} else if (packet.hasChild("x")) {
|
|
|
|
Element x = packet.findChild("x");
|
|
|
|
if (x.hasChild("invite")) {
|
|
|
|
Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, packet.getFrom(),
|
|
|
|
true);
|
|
|
|
mXmppConnectionService.updateUi(conversation, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2014-02-27 23:22:56 +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-06-04 16:44:15 +00:00
|
|
|
|
|
|
|
private String getMarkableMessageId(Element message) {
|
|
|
|
if (message.hasChild("markable", "urn:xmpp:chat-markers:0")) {
|
|
|
|
return message.getAttribute("id");
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2014-06-06 16:49:35 +00:00
|
|
|
|
|
|
|
|
2014-02-19 00:35:23 +00:00
|
|
|
}
|