Conversations/src/eu/siacs/conversations/parser/MessageParser.java

244 lines
8.5 KiB
Java
Raw Normal View History

2014-05-14 06:56:34 -04:00
package eu.siacs.conversations.parser;
import java.util.List;
import net.java.otr4j.session.Session;
import net.java.otr4j.session.SessionStatus;
import android.util.Log;
2014-02-28 12:46:01 -05:00
import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Contact;
2014-02-28 12:46:01 -05:00
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 14:22:13 -04:00
import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
public class MessageParser extends AbstractParser {
2014-06-04 06:31:19 -04:00
2014-05-14 06:56:34 -04:00
public MessageParser(XmppConnectionService service) {
super(service);
2014-05-14 06:56:34 -04:00
}
2014-06-04 06:31:19 -04:00
2014-06-04 05:55:38 -04:00
public Message parseChat(MessagePacket packet, Account account) {
String[] fromParts = packet.getFrom().split("/");
2014-06-04 06:31:19 -04:00
Conversation conversation = mXmppConnectionService
.findOrCreateConversation(account, fromParts[0], false);
conversation.setLatestMarkableMessageId(getMarkableMessageId(packet));
2014-06-06 12:49:35 -04:00
updateLastseen(packet, account,true);
2014-06-04 05:55:38 -04:00
String pgpBody = getPgpBody(packet);
2014-06-04 06:31:19 -04:00
if (pgpBody != null) {
return new Message(conversation, packet.getFrom(), pgpBody,
Message.ENCRYPTION_PGP, Message.STATUS_RECIEVED);
2014-06-04 05:55:38 -04:00
} else {
2014-06-04 06:31:19 -04:00
return new Message(conversation, packet.getFrom(),
packet.getBody(), Message.ENCRYPTION_NONE,
Message.STATUS_RECIEVED);
2014-06-04 05:55:38 -04:00
}
2014-02-27 18:22:56 -05:00
}
2014-06-04 05:55:38 -04:00
2014-05-14 06:56:34 -04:00
public Message parseOtrChat(MessagePacket packet, Account account) {
2014-06-04 06:31:19 -04:00
boolean properlyAddressed = (packet.getTo().split("/").length == 2)
|| (account.countPresences() == 1);
String[] fromParts = packet.getFrom().split("/");
2014-06-04 06:31:19 -04:00
Conversation conversation = mXmppConnectionService
.findOrCreateConversation(account, fromParts[0], false);
2014-06-06 12:49:35 -04:00
updateLastseen(packet, account,true);
String body = packet.getBody();
if (!conversation.hasValidOtrSession()) {
if (properlyAddressed) {
2014-06-04 06:31:19 -04: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);
} else {
2014-06-04 06:31:19 -04:00
Log.d("xmppService", account.getJid()
+ ": ignoring otr session with " + fromParts[0]);
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-03-19 10:05:01 -04:00
conversation.resetOtrSession();
if (properlyAddressed) {
2014-06-04 06:31:19 -04:00
Log.d("xmppService",
"replacing otr session with " + packet.getFrom());
conversation.startOtrSession(
mXmppConnectionService.getApplicationContext(),
fromParts[1], false);
} else {
return null;
}
2014-03-07 18:48:52 -05:00
}
}
try {
Session otrSession = conversation.getOtrSession();
2014-06-04 06:31:19 -04:00
SessionStatus before = otrSession.getSessionStatus();
body = otrSession.transformReceiving(body);
SessionStatus after = otrSession.getSessionStatus();
2014-06-04 06:31:19 -04:00
if ((before != after) && (after == SessionStatus.ENCRYPTED)) {
List<Message> messages = conversation.getMessages();
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 06:31:19 -04:00
MessagePacket outPacket = mXmppConnectionService
.prepareMessagePacket(account, msg, otrSession);
msg.setStatus(Message.STATUS_SEND);
2014-06-04 06:31:19 -04:00
mXmppConnectionService.databaseBackend
.updateMessage(msg);
account.getXmppConnection()
.sendMessagePacket(outPacket);
}
}
2014-05-14 06:56:34 -04:00
mXmppConnectionService.updateUi(conversation, false);
} else if ((before != after) && (after == SessionStatus.FINISHED)) {
conversation.resetOtrSession();
}
2014-06-04 06:31:19 -04:00
// isEmpty is a work around for some weird clients which send emtpty
// strings over otr
if ((body == null) || (body.isEmpty())) {
return null;
}
conversation.setLatestMarkableMessageId(getMarkableMessageId(packet));
Message finishedMessage = new Message(conversation, packet.getFrom(), body,
2014-06-04 06:31:19 -04:00
Message.ENCRYPTION_OTR, Message.STATUS_RECIEVED);
finishedMessage.setTime(getTimestamp(packet));
return finishedMessage;
} catch (Exception e) {
2014-03-21 18:09:14 -04:00
conversation.resetOtrSession();
return null;
}
}
2014-06-04 06:31:19 -04:00
2014-05-14 06:56:34 -04:00
public Message parseGroupchat(MessagePacket packet, Account account) {
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-05-14 06:56:34 -04:00
mXmppConnectionService.updateUi(conversation, false);
2014-03-14 17:40:56 -04:00
return null;
}
if ((fromParts.length == 1)) {
return null;
}
String counterPart = fromParts[1];
2014-03-02 23:01:02 -05:00
if (counterPart.equals(conversation.getMucOptions().getNick())) {
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;
}
} else {
status = Message.STATUS_RECIEVED;
}
String pgpBody = getPgpBody(packet);
conversation.setLatestMarkableMessageId(getMarkableMessageId(packet));
Message finishedMessage;
2014-06-04 06:31:19 -04:00
if (pgpBody == null) {
finishedMessage = new Message(conversation, counterPart, packet.getBody(),
2014-06-04 06:31:19 -04:00
Message.ENCRYPTION_NONE, status);
} else {
finishedMessage= new Message(conversation, counterPart, pgpBody,
2014-06-04 06:31:19 -04:00
Message.ENCRYPTION_PGP, status);
}
finishedMessage.setTime(getTimestamp(packet));
return finishedMessage;
}
2014-06-04 06:31:19 -04:00
public Message parseCarbonMessage(MessagePacket packet, Account account) {
int status;
String fullJid;
Element forwarded;
if (packet.hasChild("received")) {
2014-06-04 06:31:19 -04:00
forwarded = packet.findChild("received").findChild("forwarded");
status = Message.STATUS_RECIEVED;
} else if (packet.hasChild("sent")) {
2014-06-04 06:31:19 -04:00
forwarded = packet.findChild("sent").findChild("forwarded");
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;
}
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 12:49:35 -04:00
updateLastseen(message, account,true);
} else {
fullJid = message.getAttribute("to");
}
String[] parts = fullJid.split("/");
2014-06-04 06:31:19 -04:00
Conversation conversation = mXmppConnectionService
.findOrCreateConversation(account, parts[0], false);
conversation.setLatestMarkableMessageId(getMarkableMessageId(packet));
2014-06-04 06:31:19 -04:00
String pgpBody = getPgpBody(message);
Message finishedMessage;
2014-06-04 06:31:19 -04:00
if (pgpBody != null) {
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();
finishedMessage= new Message(conversation, fullJid, body,Message.ENCRYPTION_NONE, status);
2014-06-04 05:55:38 -04:00
}
finishedMessage.setTime(getTimestamp(message));
return finishedMessage;
}
2014-05-14 06:56:34 -04:00
public 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-06-06 12:49:35 -04: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 18:22:56 -05: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) {
return null;
} else {
return child.getContent();
2014-02-27 18:22:56 -05: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
}