2014-05-14 12:56:34 +02:00
|
|
|
package eu.siacs.conversations.parser;
|
2014-02-19 01:35:23 +01:00
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import net.java.otr4j.session.Session;
|
|
|
|
import net.java.otr4j.session.SessionStatus;
|
|
|
|
import android.util.Log;
|
2014-02-28 18:46:01 +01: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;
|
|
|
|
import eu.siacs.conversations.xml.Element;
|
2014-03-10 19:22:13 +01:00
|
|
|
import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
|
2014-02-19 01:35:23 +01:00
|
|
|
|
|
|
|
public class MessageParser {
|
2014-06-04 12:31:19 +02:00
|
|
|
|
2014-02-19 01:35:23 +01:00
|
|
|
protected static final String LOGTAG = "xmppService";
|
2014-05-14 12:56:34 +02:00
|
|
|
private XmppConnectionService mXmppConnectionService;
|
2014-06-04 12:31:19 +02:00
|
|
|
|
2014-05-14 12:56:34 +02:00
|
|
|
public MessageParser(XmppConnectionService service) {
|
|
|
|
this.mXmppConnectionService = service;
|
|
|
|
}
|
2014-06-04 12:31:19 +02:00
|
|
|
|
2014-06-04 11:55:38 +02:00
|
|
|
public Message parseChat(MessagePacket packet, Account account) {
|
2014-02-19 01:35:23 +01:00
|
|
|
String[] fromParts = packet.getFrom().split("/");
|
2014-06-04 12:31:19 +02:00
|
|
|
Conversation conversation = mXmppConnectionService
|
|
|
|
.findOrCreateConversation(account, fromParts[0], false);
|
2014-06-04 11:55:38 +02:00
|
|
|
String pgpBody = getPgpBody(packet);
|
2014-06-04 12:31:19 +02:00
|
|
|
if (pgpBody != null) {
|
|
|
|
return new Message(conversation, packet.getFrom(), pgpBody,
|
|
|
|
Message.ENCRYPTION_PGP, Message.STATUS_RECIEVED);
|
2014-06-04 11:55:38 +02:00
|
|
|
} else {
|
2014-06-04 12:31:19 +02:00
|
|
|
return new Message(conversation, packet.getFrom(),
|
|
|
|
packet.getBody(), Message.ENCRYPTION_NONE,
|
|
|
|
Message.STATUS_RECIEVED);
|
2014-06-04 11:55:38 +02:00
|
|
|
}
|
2014-02-28 00:22:56 +01:00
|
|
|
}
|
2014-06-04 11:55:38 +02:00
|
|
|
|
2014-05-14 12:56:34 +02:00
|
|
|
public Message parseOtrChat(MessagePacket packet, Account account) {
|
2014-06-04 12:31:19 +02:00
|
|
|
boolean properlyAddressed = (packet.getTo().split("/").length == 2)
|
|
|
|
|| (account.countPresences() == 1);
|
2014-02-19 01:35:23 +01:00
|
|
|
String[] fromParts = packet.getFrom().split("/");
|
2014-06-04 12:31:19 +02:00
|
|
|
Conversation conversation = mXmppConnectionService
|
|
|
|
.findOrCreateConversation(account, fromParts[0], false);
|
2014-02-19 01:35:23 +01:00
|
|
|
String body = packet.getBody();
|
|
|
|
if (!conversation.hasValidOtrSession()) {
|
2014-03-19 16:16:40 +01:00
|
|
|
if (properlyAddressed) {
|
2014-06-04 12:31:19 +02:00
|
|
|
Log.d("xmppService",
|
|
|
|
"starting new otr session with "
|
|
|
|
+ packet.getFrom()
|
|
|
|
+ " because no valid otr session has been found");
|
|
|
|
conversation.startOtrSession(
|
|
|
|
mXmppConnectionService.getApplicationContext(),
|
|
|
|
fromParts[1], false);
|
2014-03-19 16:16:40 +01:00
|
|
|
} else {
|
2014-06-04 12:31:19 +02:00
|
|
|
Log.d("xmppService", account.getJid()
|
|
|
|
+ ": ignoring otr session with " + fromParts[0]);
|
2014-03-19 16:16:40 +01:00
|
|
|
return null;
|
|
|
|
}
|
2014-03-08 00:48:52 +01:00
|
|
|
} else {
|
2014-06-04 12:31:19 +02:00
|
|
|
String foreignPresence = conversation.getOtrSession()
|
|
|
|
.getSessionID().getUserID();
|
2014-03-12 19:47:42 +01:00
|
|
|
if (!foreignPresence.equals(fromParts[1])) {
|
2014-03-19 15:05:01 +01:00
|
|
|
conversation.resetOtrSession();
|
2014-03-19 16:16:40 +01:00
|
|
|
if (properlyAddressed) {
|
2014-06-04 12:31:19 +02:00
|
|
|
Log.d("xmppService",
|
|
|
|
"replacing otr session with " + packet.getFrom());
|
|
|
|
conversation.startOtrSession(
|
|
|
|
mXmppConnectionService.getApplicationContext(),
|
|
|
|
fromParts[1], false);
|
2014-03-19 16:16:40 +01:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2014-03-08 00:48:52 +01:00
|
|
|
}
|
2014-02-19 01:35:23 +01:00
|
|
|
}
|
|
|
|
try {
|
|
|
|
Session otrSession = conversation.getOtrSession();
|
2014-06-04 12:31:19 +02:00
|
|
|
SessionStatus before = otrSession.getSessionStatus();
|
2014-02-19 01:35:23 +01:00
|
|
|
body = otrSession.transformReceiving(body);
|
|
|
|
SessionStatus after = otrSession.getSessionStatus();
|
2014-06-04 12:31:19 +02:00
|
|
|
if ((before != after) && (after == SessionStatus.ENCRYPTED)) {
|
2014-02-19 01:35:23 +01:00
|
|
|
Log.d(LOGTAG, "otr session etablished");
|
2014-06-04 12:31:19 +02:00
|
|
|
List<Message> messages = conversation.getMessages();
|
2014-02-19 01:35:23 +01:00
|
|
|
for (int i = 0; i < messages.size(); ++i) {
|
|
|
|
Message msg = messages.get(i);
|
|
|
|
if ((msg.getStatus() == Message.STATUS_UNSEND)
|
|
|
|
&& (msg.getEncryption() == Message.ENCRYPTION_OTR)) {
|
2014-06-04 12:31:19 +02:00
|
|
|
MessagePacket outPacket = mXmppConnectionService
|
|
|
|
.prepareMessagePacket(account, msg, otrSession);
|
2014-02-19 01:35:23 +01:00
|
|
|
msg.setStatus(Message.STATUS_SEND);
|
2014-06-04 12:31:19 +02:00
|
|
|
mXmppConnectionService.databaseBackend
|
|
|
|
.updateMessage(msg);
|
2014-02-19 01:35:23 +01:00
|
|
|
account.getXmppConnection()
|
|
|
|
.sendMessagePacket(outPacket);
|
|
|
|
}
|
|
|
|
}
|
2014-05-14 12:56:34 +02:00
|
|
|
mXmppConnectionService.updateUi(conversation, false);
|
2014-02-19 01:35:23 +01:00
|
|
|
} else if ((before != after) && (after == SessionStatus.FINISHED)) {
|
|
|
|
conversation.resetOtrSession();
|
2014-06-04 12:31:19 +02:00
|
|
|
Log.d(LOGTAG, "otr session stoped");
|
2014-02-19 01:35:23 +01:00
|
|
|
}
|
2014-06-04 12:31:19 +02:00
|
|
|
// isEmpty is a work around for some weird clients which send emtpty
|
|
|
|
// strings over otr
|
|
|
|
if ((body == null) || (body.isEmpty())) {
|
2014-03-23 14:15:14 +01:00
|
|
|
return null;
|
|
|
|
}
|
2014-06-04 12:31:19 +02:00
|
|
|
return new Message(conversation, packet.getFrom(), body,
|
|
|
|
Message.ENCRYPTION_OTR, Message.STATUS_RECIEVED);
|
2014-02-19 01:35:23 +01:00
|
|
|
} catch (Exception e) {
|
2014-03-21 23:09:14 +01:00
|
|
|
conversation.resetOtrSession();
|
2014-02-19 01:35:23 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2014-06-04 12:31:19 +02:00
|
|
|
|
2014-05-14 12:56:34 +02:00
|
|
|
public Message parseGroupchat(MessagePacket packet, Account account) {
|
2014-02-19 01:35:23 +01:00
|
|
|
int status;
|
|
|
|
String[] fromParts = packet.getFrom().split("/");
|
2014-06-04 12:31:19 +02:00
|
|
|
Conversation conversation = mXmppConnectionService
|
|
|
|
.findOrCreateConversation(account, fromParts[0], true);
|
2014-03-14 22:40:56 +01:00
|
|
|
if (packet.hasChild("subject")) {
|
2014-06-04 12:31:19 +02:00
|
|
|
conversation.getMucOptions().setSubject(
|
|
|
|
packet.findChild("subject").getContent());
|
2014-05-14 12:56:34 +02:00
|
|
|
mXmppConnectionService.updateUi(conversation, false);
|
2014-03-14 22:40:56 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if ((fromParts.length == 1)) {
|
2014-02-19 01:35:23 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
String counterPart = fromParts[1];
|
2014-03-03 05:01:02 +01:00
|
|
|
if (counterPart.equals(conversation.getMucOptions().getNick())) {
|
2014-06-04 12:31:19 +02:00
|
|
|
if (mXmppConnectionService.markMessage(conversation,
|
|
|
|
packet.getId(), Message.STATUS_SEND)) {
|
2014-05-16 22:46:15 +02:00
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
status = Message.STATUS_SEND;
|
|
|
|
}
|
2014-02-19 01:35:23 +01:00
|
|
|
} else {
|
|
|
|
status = Message.STATUS_RECIEVED;
|
|
|
|
}
|
2014-05-22 16:17:51 +02:00
|
|
|
String pgpBody = getPgpBody(packet);
|
2014-06-04 12:31:19 +02:00
|
|
|
if (pgpBody == null) {
|
|
|
|
return new Message(conversation, counterPart, packet.getBody(),
|
|
|
|
Message.ENCRYPTION_NONE, status);
|
2014-05-22 16:17:51 +02:00
|
|
|
} else {
|
2014-06-04 12:31:19 +02:00
|
|
|
return new Message(conversation, counterPart, pgpBody,
|
|
|
|
Message.ENCRYPTION_PGP, status);
|
2014-05-22 16:17:51 +02:00
|
|
|
}
|
2014-02-19 01:35:23 +01:00
|
|
|
}
|
|
|
|
|
2014-06-04 12:31:19 +02:00
|
|
|
public Message parseCarbonMessage(MessagePacket packet, Account account) {
|
2014-02-19 01:35:23 +01:00
|
|
|
int status;
|
|
|
|
String fullJid;
|
|
|
|
Element forwarded;
|
|
|
|
if (packet.hasChild("received")) {
|
2014-06-04 12:31:19 +02:00
|
|
|
forwarded = packet.findChild("received").findChild("forwarded");
|
2014-02-19 01:35:23 +01:00
|
|
|
status = Message.STATUS_RECIEVED;
|
|
|
|
} else if (packet.hasChild("sent")) {
|
2014-06-04 12:31:19 +02:00
|
|
|
forwarded = packet.findChild("sent").findChild("forwarded");
|
2014-02-19 01:35:23 +01:00
|
|
|
status = Message.STATUS_SEND;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2014-06-04 12:31:19 +02:00
|
|
|
if (forwarded == null) {
|
2014-03-31 09:45:39 +02:00
|
|
|
return null;
|
|
|
|
}
|
2014-02-19 01:35:23 +01: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");
|
|
|
|
} else {
|
|
|
|
fullJid = message.getAttribute("to");
|
|
|
|
}
|
|
|
|
String[] parts = fullJid.split("/");
|
2014-06-04 12:31:19 +02:00
|
|
|
Conversation conversation = mXmppConnectionService
|
|
|
|
.findOrCreateConversation(account, parts[0], false);
|
|
|
|
String pgpBody = getPgpBody(message);
|
|
|
|
if (pgpBody != null) {
|
|
|
|
return new Message(conversation, fullJid, pgpBody,
|
|
|
|
Message.ENCRYPTION_PGP, status);
|
2014-06-04 11:55:38 +02:00
|
|
|
} else {
|
2014-06-04 12:31:19 +02:00
|
|
|
String body = message.findChild("body").getContent();
|
|
|
|
return new Message(conversation, fullJid, body,
|
|
|
|
Message.ENCRYPTION_NONE, status);
|
2014-06-04 11:55:38 +02:00
|
|
|
}
|
2014-02-19 01:35:23 +01:00
|
|
|
}
|
2014-02-20 17:00:50 +01:00
|
|
|
|
2014-05-14 12:56:34 +02:00
|
|
|
public void parseError(MessagePacket packet, Account account) {
|
2014-04-11 09:13:56 +02:00
|
|
|
String[] fromParts = packet.getFrom().split("/");
|
2014-06-04 12:31:19 +02:00
|
|
|
mXmppConnectionService.markMessage(account, fromParts[0],
|
|
|
|
packet.getId(), Message.STATUS_SEND_FAILED);
|
2014-02-20 17:00:50 +01:00
|
|
|
}
|
2014-02-28 00:22:56 +01:00
|
|
|
|
2014-06-04 12:31:19 +02:00
|
|
|
private String getPgpBody(Element packet) {
|
2014-05-22 16:17:51 +02:00
|
|
|
Element child = packet.findChild("x", "jabber:x:encrypted");
|
2014-06-04 12:31:19 +02:00
|
|
|
if (child == null) {
|
2014-05-22 16:17:51 +02:00
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return child.getContent();
|
2014-02-28 00:22:56 +01:00
|
|
|
}
|
|
|
|
}
|
2014-02-19 01:35:23 +01:00
|
|
|
}
|