2014-05-14 06:56:34 -04:00
|
|
|
package eu.siacs.conversations.parser;
|
2014-02-18 19:35:23 -05:00
|
|
|
|
2014-07-11 20:36:37 -04:00
|
|
|
import android.os.SystemClock;
|
2014-02-18 19:35:23 -05:00
|
|
|
import net.java.otr4j.session.Session;
|
|
|
|
import net.java.otr4j.session.SessionStatus;
|
2014-02-28 12:46:01 -05: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 11:30:19 -04:00
|
|
|
import eu.siacs.conversations.utils.CryptoHelper;
|
2014-02-28 12:46:01 -05:00
|
|
|
import eu.siacs.conversations.xml.Element;
|
2014-07-11 20:36:37 -04:00
|
|
|
import eu.siacs.conversations.xmpp.OnMessagePacketReceived;
|
2014-03-10 14:22:13 -04:00
|
|
|
import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
|
2014-02-18 19:35:23 -05:00
|
|
|
|
2014-07-11 20:36:37 -04:00
|
|
|
public class MessageParser extends AbstractParser implements
|
|
|
|
OnMessagePacketReceived {
|
|
|
|
|
|
|
|
private long lastCarbonMessageReceived = -XmppConnectionService.CARBON_GRACE_PERIOD;
|
2014-06-04 06:31:19 -04:00
|
|
|
|
2014-05-14 06:56:34 -04:00
|
|
|
public MessageParser(XmppConnectionService service) {
|
2014-06-06 12:26:40 -04:00
|
|
|
super(service);
|
2014-05-14 06:56:34 -04:00
|
|
|
}
|
2014-06-04 06:31:19 -04:00
|
|
|
|
2014-07-11 20:36:37 -04:00
|
|
|
private Message parseChat(MessagePacket packet, Account account) {
|
2014-02-18 19:35:23 -05:00
|
|
|
String[] fromParts = packet.getFrom().split("/");
|
2014-06-04 06:31:19 -04:00
|
|
|
Conversation conversation = mXmppConnectionService
|
|
|
|
.findOrCreateConversation(account, fromParts[0], false);
|
2014-06-04 12:44:15 -04:00
|
|
|
conversation.setLatestMarkableMessageId(getMarkableMessageId(packet));
|
2014-07-11 20:36:37 -04:00
|
|
|
updateLastseen(packet, account, true);
|
2014-06-04 05:55:38 -04:00
|
|
|
String pgpBody = getPgpBody(packet);
|
2014-06-22 12:21:04 -04:00
|
|
|
Message finishedMessage;
|
2014-06-04 06:31:19 -04:00
|
|
|
if (pgpBody != null) {
|
2014-07-11 20:36:37 -04:00
|
|
|
finishedMessage = new Message(conversation, packet.getFrom(),
|
|
|
|
pgpBody, Message.ENCRYPTION_PGP, Message.STATUS_RECIEVED);
|
2014-06-04 05:55:38 -04:00
|
|
|
} else {
|
2014-06-22 12:21:04 -04:00
|
|
|
finishedMessage = new Message(conversation, packet.getFrom(),
|
2014-06-04 06:31:19 -04:00
|
|
|
packet.getBody(), Message.ENCRYPTION_NONE,
|
|
|
|
Message.STATUS_RECIEVED);
|
2014-06-04 05:55:38 -04:00
|
|
|
}
|
2014-06-22 12:21:04 -04:00
|
|
|
finishedMessage.setTime(getTimestamp(packet));
|
|
|
|
return finishedMessage;
|
2014-02-27 18:22:56 -05:00
|
|
|
}
|
2014-06-04 05:55:38 -04:00
|
|
|
|
2014-07-11 20:36:37 -04:00
|
|
|
private Message parseOtrChat(MessagePacket packet, Account account) {
|
2014-06-04 06:31:19 -04:00
|
|
|
boolean properlyAddressed = (packet.getTo().split("/").length == 2)
|
|
|
|
|| (account.countPresences() == 1);
|
2014-02-18 19:35:23 -05:00
|
|
|
String[] fromParts = packet.getFrom().split("/");
|
2014-06-04 06:31:19 -04:00
|
|
|
Conversation conversation = mXmppConnectionService
|
|
|
|
.findOrCreateConversation(account, fromParts[0], false);
|
2014-07-11 20:36:37 -04:00
|
|
|
updateLastseen(packet, account, true);
|
2014-02-18 19:35:23 -05:00
|
|
|
String body = packet.getBody();
|
|
|
|
if (!conversation.hasValidOtrSession()) {
|
2014-03-19 11:16:40 -04:00
|
|
|
if (properlyAddressed) {
|
2014-06-04 06:31:19 -04:00
|
|
|
conversation.startOtrSession(
|
|
|
|
mXmppConnectionService.getApplicationContext(),
|
|
|
|
fromParts[1], false);
|
2014-03-19 11:16:40 -04:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2014-03-07 18:48:52 -05:00
|
|
|
} else {
|
2014-06-04 06:31:19 -04:00
|
|
|
String foreignPresence = conversation.getOtrSession()
|
|
|
|
.getSessionID().getUserID();
|
2014-03-12 14:47:42 -04:00
|
|
|
if (!foreignPresence.equals(fromParts[1])) {
|
2014-06-25 10:55:47 -04:00
|
|
|
conversation.endOtrIfNeeded();
|
2014-03-19 11:16:40 -04:00
|
|
|
if (properlyAddressed) {
|
2014-06-04 06:31:19 -04:00
|
|
|
conversation.startOtrSession(
|
|
|
|
mXmppConnectionService.getApplicationContext(),
|
|
|
|
fromParts[1], false);
|
2014-03-19 11:16:40 -04:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2014-03-07 18:48:52 -05:00
|
|
|
}
|
2014-02-18 19:35:23 -05:00
|
|
|
}
|
|
|
|
try {
|
|
|
|
Session otrSession = conversation.getOtrSession();
|
2014-06-04 06:31:19 -04:00
|
|
|
SessionStatus before = otrSession.getSessionStatus();
|
2014-02-18 19:35:23 -05:00
|
|
|
body = otrSession.transformReceiving(body);
|
|
|
|
SessionStatus after = otrSession.getSessionStatus();
|
2014-06-04 06:31:19 -04:00
|
|
|
if ((before != after) && (after == SessionStatus.ENCRYPTED)) {
|
2014-06-11 15:53:25 -04:00
|
|
|
mXmppConnectionService.onOtrSessionEstablished(conversation);
|
2014-02-18 19:35:23 -05:00
|
|
|
} else if ((before != after) && (after == SessionStatus.FINISHED)) {
|
|
|
|
conversation.resetOtrSession();
|
|
|
|
}
|
2014-06-04 06:31:19 -04:00
|
|
|
if ((body == null) || (body.isEmpty())) {
|
2014-03-23 09:15:14 -04:00
|
|
|
return null;
|
|
|
|
}
|
2014-06-20 11:30:19 -04:00
|
|
|
if (body.startsWith(CryptoHelper.FILETRANSFER)) {
|
|
|
|
String key = body.substring(CryptoHelper.FILETRANSFER.length());
|
|
|
|
conversation.setSymmetricKey(CryptoHelper.hexToBytes(key));
|
|
|
|
return null;
|
|
|
|
}
|
2014-07-11 20:36:37 -04:00
|
|
|
conversation
|
|
|
|
.setLatestMarkableMessageId(getMarkableMessageId(packet));
|
|
|
|
Message finishedMessage = new Message(conversation,
|
|
|
|
packet.getFrom(), body, Message.ENCRYPTION_OTR,
|
|
|
|
Message.STATUS_RECIEVED);
|
2014-06-06 12:26:40 -04:00
|
|
|
finishedMessage.setTime(getTimestamp(packet));
|
|
|
|
return finishedMessage;
|
2014-02-18 19:35:23 -05:00
|
|
|
} catch (Exception e) {
|
2014-06-25 10:55:47 -04:00
|
|
|
String receivedId = packet.getId();
|
2014-07-11 20:36:37 -04:00
|
|
|
if (receivedId != null) {
|
|
|
|
mXmppConnectionService.replyWithNotAcceptable(account, packet);
|
2014-06-25 10:55:47 -04:00
|
|
|
}
|
2014-06-26 10:42:24 -04:00
|
|
|
conversation.endOtrIfNeeded();
|
2014-02-18 19:35:23 -05:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2014-06-04 06:31:19 -04:00
|
|
|
|
2014-07-11 20:36:37 -04:00
|
|
|
private Message parseGroupchat(MessagePacket packet, Account account) {
|
2014-02-18 19:35:23 -05:00
|
|
|
int status;
|
|
|
|
String[] fromParts = packet.getFrom().split("/");
|
2014-06-04 06:31:19 -04:00
|
|
|
Conversation conversation = mXmppConnectionService
|
|
|
|
.findOrCreateConversation(account, fromParts[0], true);
|
2014-03-14 17:40:56 -04:00
|
|
|
if (packet.hasChild("subject")) {
|
2014-06-04 06:31:19 -04:00
|
|
|
conversation.getMucOptions().setSubject(
|
|
|
|
packet.findChild("subject").getContent());
|
2014-07-12 07:42:17 -04:00
|
|
|
mXmppConnectionService.updateConversationUi();
|
2014-03-14 17:40:56 -04:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if ((fromParts.length == 1)) {
|
2014-02-18 19:35:23 -05:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
String counterPart = fromParts[1];
|
2014-07-15 15:54:11 -04:00
|
|
|
if (counterPart.equals(conversation.getMucOptions().getActualNick())) {
|
2014-06-04 06:31:19 -04:00
|
|
|
if (mXmppConnectionService.markMessage(conversation,
|
|
|
|
packet.getId(), Message.STATUS_SEND)) {
|
2014-05-16 16:46:15 -04:00
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
status = Message.STATUS_SEND;
|
|
|
|
}
|
2014-02-18 19:35:23 -05:00
|
|
|
} else {
|
|
|
|
status = Message.STATUS_RECIEVED;
|
|
|
|
}
|
2014-05-22 10:17:51 -04:00
|
|
|
String pgpBody = getPgpBody(packet);
|
2014-06-04 12:44:15 -04:00
|
|
|
conversation.setLatestMarkableMessageId(getMarkableMessageId(packet));
|
2014-06-06 12:26:40 -04:00
|
|
|
Message finishedMessage;
|
2014-06-04 06:31:19 -04:00
|
|
|
if (pgpBody == null) {
|
2014-07-11 20:36:37 -04:00
|
|
|
finishedMessage = new Message(conversation, counterPart,
|
|
|
|
packet.getBody(), Message.ENCRYPTION_NONE, status);
|
2014-05-22 10:17:51 -04:00
|
|
|
} else {
|
2014-07-11 20:36:37 -04:00
|
|
|
finishedMessage = new Message(conversation, counterPart, pgpBody,
|
2014-06-04 06:31:19 -04:00
|
|
|
Message.ENCRYPTION_PGP, status);
|
2014-05-22 10:17:51 -04:00
|
|
|
}
|
2014-06-06 12:26:40 -04:00
|
|
|
finishedMessage.setTime(getTimestamp(packet));
|
|
|
|
return finishedMessage;
|
2014-02-18 19:35:23 -05:00
|
|
|
}
|
|
|
|
|
2014-07-11 20:36:37 -04:00
|
|
|
private Message parseCarbonMessage(MessagePacket packet, Account account) {
|
2014-02-18 19:35:23 -05:00
|
|
|
int status;
|
|
|
|
String fullJid;
|
|
|
|
Element forwarded;
|
|
|
|
if (packet.hasChild("received")) {
|
2014-06-04 06:31:19 -04:00
|
|
|
forwarded = packet.findChild("received").findChild("forwarded");
|
2014-02-18 19:35:23 -05:00
|
|
|
status = Message.STATUS_RECIEVED;
|
|
|
|
} else if (packet.hasChild("sent")) {
|
2014-06-04 06:31:19 -04:00
|
|
|
forwarded = packet.findChild("sent").findChild("forwarded");
|
2014-02-18 19:35:23 -05:00
|
|
|
status = Message.STATUS_SEND;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2014-06-04 06:31:19 -04:00
|
|
|
if (forwarded == null) {
|
2014-03-31 03:45:39 -04:00
|
|
|
return null;
|
|
|
|
}
|
2014-02-18 19:35:23 -05:00
|
|
|
Element message = forwarded.findChild("message");
|
2014-07-11 08:49:06 -04:00
|
|
|
if ((message == null) || (!message.hasChild("body"))) {
|
|
|
|
if (status == Message.STATUS_RECIEVED) {
|
|
|
|
parseNormal(message, account);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2014-02-18 19:35:23 -05:00
|
|
|
if (status == Message.STATUS_RECIEVED) {
|
|
|
|
fullJid = message.getAttribute("from");
|
2014-07-11 20:36:37 -04:00
|
|
|
updateLastseen(message, account, true);
|
2014-02-18 19:35:23 -05:00
|
|
|
} else {
|
|
|
|
fullJid = message.getAttribute("to");
|
|
|
|
}
|
2014-07-16 06:42:44 -04:00
|
|
|
if (fullJid==null) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-02-18 19:35:23 -05:00
|
|
|
String[] parts = fullJid.split("/");
|
2014-06-04 06:31:19 -04:00
|
|
|
Conversation conversation = mXmppConnectionService
|
|
|
|
.findOrCreateConversation(account, parts[0], false);
|
2014-06-04 12:44:15 -04:00
|
|
|
conversation.setLatestMarkableMessageId(getMarkableMessageId(packet));
|
2014-06-04 06:31:19 -04:00
|
|
|
String pgpBody = getPgpBody(message);
|
2014-06-06 12:26:40 -04:00
|
|
|
Message finishedMessage;
|
2014-06-04 06:31:19 -04:00
|
|
|
if (pgpBody != null) {
|
2014-07-11 20:36:37 -04:00
|
|
|
finishedMessage = new Message(conversation, fullJid, pgpBody,
|
|
|
|
Message.ENCRYPTION_PGP, status);
|
2014-06-04 05:55:38 -04:00
|
|
|
} else {
|
2014-06-04 06:31:19 -04:00
|
|
|
String body = message.findChild("body").getContent();
|
2014-07-11 20:36:37 -04:00
|
|
|
finishedMessage = new Message(conversation, fullJid, body,
|
|
|
|
Message.ENCRYPTION_NONE, status);
|
2014-06-04 05:55:38 -04:00
|
|
|
}
|
2014-06-06 12:26:40 -04:00
|
|
|
finishedMessage.setTime(getTimestamp(message));
|
|
|
|
return finishedMessage;
|
2014-02-18 19:35:23 -05:00
|
|
|
}
|
2014-02-20 11:00:50 -05:00
|
|
|
|
2014-07-11 20:36:37 -04:00
|
|
|
private void parseError(MessagePacket packet, Account account) {
|
2014-04-11 03:13:56 -04:00
|
|
|
String[] fromParts = packet.getFrom().split("/");
|
2014-06-04 06:31:19 -04:00
|
|
|
mXmppConnectionService.markMessage(account, fromParts[0],
|
|
|
|
packet.getId(), Message.STATUS_SEND_FAILED);
|
2014-02-20 11:00:50 -05:00
|
|
|
}
|
2014-07-11 20:36:37 -04:00
|
|
|
|
|
|
|
private void parseNormal(Element packet, Account account) {
|
|
|
|
if (packet.hasChild("displayed", "urn:xmpp:chat-markers:0")) {
|
|
|
|
String id = packet
|
|
|
|
.findChild("displayed", "urn:xmpp:chat-markers:0")
|
|
|
|
.getAttribute("id");
|
2014-07-11 08:49:06 -04:00
|
|
|
String[] fromParts = packet.getAttribute("from").split("/");
|
2014-07-11 20:36:37 -04:00
|
|
|
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");
|
2014-07-11 08:49:06 -04:00
|
|
|
String[] fromParts = packet.getAttribute("from").split("/");
|
2014-07-11 20:36:37 -04:00
|
|
|
updateLastseen(packet, account, false);
|
|
|
|
mXmppConnectionService.markMessage(account, fromParts[0], id,
|
|
|
|
Message.STATUS_SEND_RECEIVED);
|
2014-06-06 12:49:35 -04:00
|
|
|
} else if (packet.hasChild("x")) {
|
|
|
|
Element x = packet.findChild("x");
|
|
|
|
if (x.hasChild("invite")) {
|
2014-07-12 07:42:17 -04:00
|
|
|
mXmppConnectionService
|
2014-07-11 20:36:37 -04:00
|
|
|
.findOrCreateConversation(account,
|
|
|
|
packet.getAttribute("from"), true);
|
2014-07-12 07:42:17 -04:00
|
|
|
mXmppConnectionService.updateConversationUi();
|
2014-06-06 12:49:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2014-02-27 18:22:56 -05:00
|
|
|
|
2014-06-04 12:44:15 -04:00
|
|
|
private String getPgpBody(Element message) {
|
|
|
|
Element child = message.findChild("x", "jabber:x:encrypted");
|
2014-06-04 06:31:19 -04:00
|
|
|
if (child == null) {
|
2014-05-22 10:17:51 -04:00
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return child.getContent();
|
2014-02-27 18:22:56 -05:00
|
|
|
}
|
|
|
|
}
|
2014-07-11 20:36:37 -04:00
|
|
|
|
2014-06-04 12:44:15 -04: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 12:49:35 -04:00
|
|
|
|
2014-07-11 20:36:37 -04:00
|
|
|
@Override
|
|
|
|
public void onMessagePacketReceived(Account account, MessagePacket packet) {
|
|
|
|
Message message = null;
|
|
|
|
boolean notify = true;
|
|
|
|
if (mXmppConnectionService.getPreferences().getBoolean(
|
|
|
|
"notification_grace_period_after_carbon_received", true)) {
|
|
|
|
notify = (SystemClock.elapsedRealtime() - lastCarbonMessageReceived) > XmppConnectionService.CARBON_GRACE_PERIOD;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((packet.getType() == MessagePacket.TYPE_CHAT)) {
|
|
|
|
if ((packet.getBody() != null)
|
|
|
|
&& (packet.getBody().startsWith("?OTR"))) {
|
|
|
|
message = this.parseOtrChat(packet, account);
|
|
|
|
if (message != null) {
|
|
|
|
message.markUnread();
|
|
|
|
}
|
|
|
|
} else if (packet.hasChild("body")) {
|
|
|
|
message = this.parseChat(packet, account);
|
|
|
|
message.markUnread();
|
|
|
|
} else if (packet.hasChild("received") || (packet.hasChild("sent"))) {
|
|
|
|
message = this.parseCarbonMessage(packet, account);
|
|
|
|
if (message != null) {
|
|
|
|
if (message.getStatus() == Message.STATUS_SEND) {
|
|
|
|
lastCarbonMessageReceived = SystemClock
|
|
|
|
.elapsedRealtime();
|
|
|
|
notify = false;
|
|
|
|
message.getConversation().markRead();
|
|
|
|
} else {
|
|
|
|
message.markUnread();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (packet.getType() == MessagePacket.TYPE_GROUPCHAT) {
|
|
|
|
message = this.parseGroupchat(packet, account);
|
|
|
|
if (message != null) {
|
|
|
|
if (message.getStatus() == Message.STATUS_RECIEVED) {
|
|
|
|
message.markUnread();
|
|
|
|
} else {
|
|
|
|
message.getConversation().markRead();
|
2014-07-16 06:45:56 -04:00
|
|
|
lastCarbonMessageReceived = SystemClock
|
|
|
|
.elapsedRealtime();
|
2014-07-11 20:36:37 -04:00
|
|
|
notify = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (packet.getType() == MessagePacket.TYPE_ERROR) {
|
|
|
|
this.parseError(packet, account);
|
|
|
|
return;
|
|
|
|
} else if (packet.getType() == MessagePacket.TYPE_NORMAL) {
|
|
|
|
this.parseNormal(packet, account);
|
|
|
|
}
|
|
|
|
if ((message == null) || (message.getBody() == null)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ((mXmppConnectionService.confirmMessages())
|
|
|
|
&& ((packet.getId() != null))) {
|
|
|
|
MessagePacket receivedPacket = new MessagePacket();
|
|
|
|
receivedPacket.setType(MessagePacket.TYPE_NORMAL);
|
|
|
|
receivedPacket.setTo(message.getCounterpart());
|
|
|
|
receivedPacket.setFrom(account.getFullJid());
|
|
|
|
if (packet.hasChild("markable", "urn:xmpp:chat-markers:0")) {
|
|
|
|
Element received = receivedPacket.addChild("received",
|
|
|
|
"urn:xmpp:chat-markers:0");
|
|
|
|
received.setAttribute("id", packet.getId());
|
|
|
|
account.getXmppConnection().sendMessagePacket(receivedPacket);
|
|
|
|
} else if (packet.hasChild("request", "urn:xmpp:receipts")) {
|
|
|
|
Element received = receivedPacket.addChild("received",
|
|
|
|
"urn:xmpp:receipts");
|
|
|
|
received.setAttribute("id", packet.getId());
|
|
|
|
account.getXmppConnection().sendMessagePacket(receivedPacket);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Conversation conversation = message.getConversation();
|
|
|
|
conversation.getMessages().add(message);
|
|
|
|
if (packet.getType() != MessagePacket.TYPE_ERROR) {
|
|
|
|
mXmppConnectionService.databaseBackend.createMessage(message);
|
|
|
|
}
|
2014-07-12 07:42:17 -04:00
|
|
|
mXmppConnectionService.notifyUi(conversation, notify);
|
2014-07-11 20:36:37 -04:00
|
|
|
}
|
2014-02-18 19:35:23 -05:00
|
|
|
}
|