2014-02-28 12:46:01 -05:00
|
|
|
package eu.siacs.conversations.utils;
|
2014-02-18 19:35:23 -05:00
|
|
|
|
|
|
|
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.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;
|
2014-02-18 19:35:23 -05:00
|
|
|
|
|
|
|
public class MessageParser {
|
|
|
|
|
|
|
|
protected static final String LOGTAG = "xmppService";
|
|
|
|
|
|
|
|
public static Message parsePlainTextChat(MessagePacket packet, Account account, XmppConnectionService service) {
|
|
|
|
String[] fromParts = packet.getFrom().split("/");
|
|
|
|
Conversation conversation = service.findOrCreateConversation(account, fromParts[0],false);
|
|
|
|
String body = packet.getBody();
|
|
|
|
return new Message(conversation, packet.getFrom(), body, Message.ENCRYPTION_NONE, Message.STATUS_RECIEVED);
|
|
|
|
}
|
|
|
|
|
2014-02-27 18:22:56 -05:00
|
|
|
public static Message parsePgpChat(String pgpBody, MessagePacket packet, Account account, XmppConnectionService service) {
|
|
|
|
String[] fromParts = packet.getFrom().split("/");
|
|
|
|
Conversation conversation = service.findOrCreateConversation(account, fromParts[0],false);
|
|
|
|
return new Message(conversation, packet.getFrom(), pgpBody, Message.ENCRYPTION_PGP, Message.STATUS_RECIEVED);
|
|
|
|
}
|
|
|
|
|
2014-02-18 19:35:23 -05:00
|
|
|
public static Message parseOtrChat(MessagePacket packet, Account account, XmppConnectionService service) {
|
2014-03-21 16:25:28 -04:00
|
|
|
boolean justStarted = false;
|
2014-03-19 11:16:40 -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("/");
|
|
|
|
Conversation conversation = service.findOrCreateConversation(account, fromParts[0],false);
|
|
|
|
String body = packet.getBody();
|
|
|
|
if (!conversation.hasValidOtrSession()) {
|
2014-03-19 11:16:40 -04:00
|
|
|
if (properlyAddressed) {
|
2014-03-21 09:58:33 -04:00
|
|
|
Log.d("xmppService","starting new otr session with "+packet.getFrom()+" because no valid otr session has been found");
|
2014-03-19 11:16:40 -04:00
|
|
|
conversation.startOtrSession(service.getApplicationContext(), fromParts[1]);
|
2014-03-21 16:25:28 -04:00
|
|
|
justStarted = true;
|
2014-03-19 11:16:40 -04:00
|
|
|
} else {
|
|
|
|
Log.d("xmppService",account.getJid()+": ignoring otr session with "+fromParts[0]);
|
|
|
|
return null;
|
|
|
|
}
|
2014-03-07 18:48:52 -05:00
|
|
|
} else {
|
2014-03-12 14:47:42 -04:00
|
|
|
String foreignPresence = conversation.getOtrSession().getSessionID().getUserID();
|
|
|
|
if (!foreignPresence.equals(fromParts[1])) {
|
2014-03-19 10:05:01 -04:00
|
|
|
conversation.resetOtrSession();
|
2014-03-19 11:16:40 -04:00
|
|
|
if (properlyAddressed) {
|
2014-03-21 09:58:33 -04:00
|
|
|
Log.d("xmppService","replacing otr session with "+packet.getFrom());
|
2014-03-19 11:16:40 -04:00
|
|
|
conversation.startOtrSession(service.getApplicationContext(), fromParts[1]);
|
2014-03-21 16:25:28 -04:00
|
|
|
justStarted = true;
|
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();
|
|
|
|
SessionStatus before = otrSession
|
|
|
|
.getSessionStatus();
|
|
|
|
body = otrSession.transformReceiving(body);
|
|
|
|
SessionStatus after = otrSession.getSessionStatus();
|
|
|
|
if ((before != after)
|
|
|
|
&& (after == SessionStatus.ENCRYPTED)) {
|
|
|
|
Log.d(LOGTAG, "otr session etablished");
|
|
|
|
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)) {
|
|
|
|
MessagePacket outPacket = service.prepareMessagePacket(
|
|
|
|
account, msg, otrSession);
|
|
|
|
msg.setStatus(Message.STATUS_SEND);
|
|
|
|
service.databaseBackend.updateMessage(msg);
|
|
|
|
account.getXmppConnection()
|
|
|
|
.sendMessagePacket(outPacket);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (service.convChangedListener!=null) {
|
|
|
|
service.convChangedListener.onConversationListChanged();
|
|
|
|
}
|
|
|
|
} else if ((before != after) && (after == SessionStatus.FINISHED)) {
|
|
|
|
conversation.resetOtrSession();
|
|
|
|
Log.d(LOGTAG,"otr session stoped");
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
2014-03-21 17:14:52 -04:00
|
|
|
if (!justStarted) {
|
2014-03-21 16:25:28 -04:00
|
|
|
conversation.resetOtrSession();
|
|
|
|
}
|
2014-02-18 19:35:23 -05:00
|
|
|
return null;
|
|
|
|
}
|
2014-03-14 14:31:15 -04:00
|
|
|
|
|
|
|
//isEmpty is a work around for some weird clients which send emtpty strings over otr
|
|
|
|
if ((body == null)||(body.isEmpty())) {
|
2014-02-18 19:35:23 -05:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return new Message(conversation, packet.getFrom(), body, Message.ENCRYPTION_OTR,Message.STATUS_RECIEVED);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Message parseGroupchat(MessagePacket packet, Account account, XmppConnectionService service) {
|
|
|
|
int status;
|
|
|
|
String[] fromParts = packet.getFrom().split("/");
|
|
|
|
Conversation conversation = service.findOrCreateConversation(account, fromParts[0],true);
|
2014-03-14 17:40:56 -04:00
|
|
|
if (packet.hasChild("subject")) {
|
|
|
|
conversation.getMucOptions().setSubject(packet.findChild("subject").getContent());
|
|
|
|
service.updateConversationInGui();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if ((fromParts.length == 1)) {
|
2014-02-18 19:35:23 -05:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
String counterPart = fromParts[1];
|
2014-03-02 23:01:02 -05:00
|
|
|
if (counterPart.equals(conversation.getMucOptions().getNick())) {
|
2014-02-18 19:35:23 -05:00
|
|
|
status = Message.STATUS_SEND;
|
|
|
|
} else {
|
|
|
|
status = Message.STATUS_RECIEVED;
|
|
|
|
}
|
|
|
|
return new Message(conversation, counterPart, packet.getBody(), Message.ENCRYPTION_NONE, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Message parseCarbonMessage(MessagePacket packet,
|
|
|
|
Account account, XmppConnectionService service) {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
int status;
|
|
|
|
String fullJid;
|
|
|
|
Element forwarded;
|
|
|
|
if (packet.hasChild("received")) {
|
|
|
|
forwarded = packet.findChild("received").findChild(
|
|
|
|
"forwarded");
|
|
|
|
status = Message.STATUS_RECIEVED;
|
|
|
|
} else if (packet.hasChild("sent")) {
|
|
|
|
forwarded = packet.findChild("sent").findChild(
|
|
|
|
"forwarded");
|
|
|
|
status = Message.STATUS_SEND;
|
|
|
|
} else {
|
|
|
|
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");
|
|
|
|
} else {
|
|
|
|
fullJid = message.getAttribute("to");
|
|
|
|
}
|
|
|
|
String[] parts = fullJid.split("/");
|
|
|
|
Conversation conversation = service.findOrCreateConversation(account, parts[0],false);
|
|
|
|
return new Message(conversation,fullJid, message.findChild("body").getContent(), Message.ENCRYPTION_NONE,status);
|
|
|
|
}
|
2014-02-20 11:00:50 -05:00
|
|
|
|
|
|
|
public static Message parseError(MessagePacket packet, Account account, XmppConnectionService service) {
|
|
|
|
|
|
|
|
String[] fromParts = packet.getFrom().split("/");
|
|
|
|
Conversation conversation = service.findOrCreateConversation(account, fromParts[0],false);
|
|
|
|
Element error = packet.findChild("error");
|
|
|
|
String errorName = error.getChildren().get(0).getName();
|
|
|
|
String displayError;
|
|
|
|
if (errorName.equals("service-unavailable")) {
|
|
|
|
displayError = "Contact is offline and does not have offline storage";
|
|
|
|
} else {
|
|
|
|
displayError = errorName.replace("-", " ");
|
|
|
|
}
|
|
|
|
return new Message(conversation, packet.getFrom(), displayError, Message.ENCRYPTION_NONE, Message.STATUS_ERROR);
|
|
|
|
}
|
2014-02-27 18:22:56 -05:00
|
|
|
|
|
|
|
public static String getPgpBody(MessagePacket packet) {
|
|
|
|
for(Element child : packet.getChildren()) {
|
|
|
|
if (child.getName().equals("x")&&child.getAttribute("xmlns").equals("jabber:x:encrypted")) {
|
|
|
|
return child.getContent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2014-02-18 19:35:23 -05:00
|
|
|
}
|