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

392 lines
14 KiB
Java
Raw Normal View History

2014-05-14 12:56:34 +02:00
package eu.siacs.conversations.parser;
import android.os.SystemClock;
2014-08-05 01:36:17 +02:00
import android.util.Log;
import net.java.otr4j.session.Session;
import net.java.otr4j.session.SessionStatus;
2014-02-28 18:46:01 +01:00
import eu.siacs.conversations.entities.Account;
2014-08-05 22:58:46 +02:00
import eu.siacs.conversations.entities.Contact;
2014-02-28 18:46:01 +01:00
import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.utils.CryptoHelper;
2014-02-28 18:46:01 +01:00
import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xmpp.OnMessagePacketReceived;
2014-08-05 22:58:46 +02:00
import eu.siacs.conversations.xmpp.pep.Avatar;
2014-03-10 19:22:13 +01:00
import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
public class MessageParser extends AbstractParser implements
OnMessagePacketReceived {
private long lastCarbonMessageReceived = -XmppConnectionService.CARBON_GRACE_PERIOD;
2014-06-04 12:31:19 +02:00
2014-05-14 12:56:34 +02:00
public MessageParser(XmppConnectionService service) {
super(service);
2014-05-14 12:56:34 +02:00
}
2014-06-04 12:31:19 +02:00
private Message parseChat(MessagePacket packet, Account account) {
String[] fromParts = packet.getFrom().split("/");
2014-06-04 12:31:19 +02:00
Conversation conversation = mXmppConnectionService
.findOrCreateConversation(account, fromParts[0], false);
conversation.setLatestMarkableMessageId(getMarkableMessageId(packet));
updateLastseen(packet, account, true);
2014-06-04 11:55:38 +02:00
String pgpBody = getPgpBody(packet);
2014-06-22 18:21:04 +02:00
Message finishedMessage;
2014-06-04 12:31:19 +02:00
if (pgpBody != null) {
finishedMessage = new Message(conversation, packet.getFrom(),
pgpBody, Message.ENCRYPTION_PGP, Message.STATUS_RECIEVED);
2014-06-04 11:55:38 +02:00
} else {
2014-06-22 18:21:04 +02:00
finishedMessage = new Message(conversation, packet.getFrom(),
2014-06-04 12:31:19 +02:00
packet.getBody(), Message.ENCRYPTION_NONE,
Message.STATUS_RECIEVED);
2014-06-04 11:55:38 +02:00
}
2014-06-22 18:21:04 +02:00
finishedMessage.setTime(getTimestamp(packet));
return finishedMessage;
2014-02-28 00:22:56 +01:00
}
2014-06-04 11:55:38 +02:00
private Message parseOtrChat(MessagePacket packet, Account account) {
2014-06-04 12:31:19 +02:00
boolean properlyAddressed = (packet.getTo().split("/").length == 2)
|| (account.countPresences() == 1);
String[] fromParts = packet.getFrom().split("/");
2014-06-04 12:31:19 +02:00
Conversation conversation = mXmppConnectionService
.findOrCreateConversation(account, fromParts[0], false);
updateLastseen(packet, account, true);
String body = packet.getBody();
if (!conversation.hasValidOtrSession()) {
if (properlyAddressed) {
2014-06-04 12:31:19 +02:00
conversation.startOtrSession(
mXmppConnectionService.getApplicationContext(),
fromParts[1], false);
} else {
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])) {
conversation.endOtrIfNeeded();
if (properlyAddressed) {
2014-06-04 12:31:19 +02:00
conversation.startOtrSession(
mXmppConnectionService.getApplicationContext(),
fromParts[1], false);
} else {
return null;
}
2014-03-08 00:48:52 +01:00
}
}
try {
Session otrSession = conversation.getOtrSession();
2014-06-04 12:31:19 +02:00
SessionStatus before = otrSession.getSessionStatus();
body = otrSession.transformReceiving(body);
SessionStatus after = otrSession.getSessionStatus();
2014-06-04 12:31:19 +02:00
if ((before != after) && (after == SessionStatus.ENCRYPTED)) {
2014-06-11 21:53:25 +02:00
mXmppConnectionService.onOtrSessionEstablished(conversation);
} else if ((before != after) && (after == SessionStatus.FINISHED)) {
conversation.resetOtrSession();
mXmppConnectionService.updateConversationUi();
}
2014-06-04 12:31:19 +02:00
if ((body == null) || (body.isEmpty())) {
return null;
}
if (body.startsWith(CryptoHelper.FILETRANSFER)) {
String key = body.substring(CryptoHelper.FILETRANSFER.length());
conversation.setSymmetricKey(CryptoHelper.hexToBytes(key));
return null;
}
conversation
.setLatestMarkableMessageId(getMarkableMessageId(packet));
Message finishedMessage = new Message(conversation,
packet.getFrom(), body, Message.ENCRYPTION_OTR,
Message.STATUS_RECIEVED);
finishedMessage.setTime(getTimestamp(packet));
return finishedMessage;
} catch (Exception e) {
String receivedId = packet.getId();
if (receivedId != null) {
mXmppConnectionService.replyWithNotAcceptable(account, packet);
}
conversation.endOtrIfNeeded();
return null;
}
}
2014-06-04 12:31:19 +02:00
private Message parseGroupchat(MessagePacket packet, Account account) {
int status;
String[] fromParts = packet.getFrom().split("/");
if (mXmppConnectionService.find(account.pendingConferenceLeaves,account,fromParts[0]) != null) {
return null;
}
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-07-12 13:42:17 +02:00
mXmppConnectionService.updateConversationUi();
2014-03-14 22:40:56 +01:00
return null;
}
if ((fromParts.length == 1)) {
return null;
}
String counterPart = fromParts[1];
if (counterPart.equals(conversation.getMucOptions().getActualNick())) {
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;
}
} else {
status = Message.STATUS_RECIEVED;
}
String pgpBody = getPgpBody(packet);
conversation.setLatestMarkableMessageId(getMarkableMessageId(packet));
Message finishedMessage;
2014-06-04 12:31:19 +02:00
if (pgpBody == null) {
finishedMessage = new Message(conversation, counterPart,
packet.getBody(), Message.ENCRYPTION_NONE, status);
} else {
finishedMessage = new Message(conversation, counterPart, pgpBody,
2014-06-04 12:31:19 +02:00
Message.ENCRYPTION_PGP, status);
}
finishedMessage.setTime(getTimestamp(packet));
if (status == Message.STATUS_RECIEVED) {
finishedMessage.setTrueCounterpart(conversation.getMucOptions().getTrueCounterpart(counterPart));
}
return finishedMessage;
}
private Message parseCarbonMessage(MessagePacket packet, Account account) {
int status;
String fullJid;
Element forwarded;
if (packet.hasChild("received")) {
2014-06-04 12:31:19 +02:00
forwarded = packet.findChild("received").findChild("forwarded");
status = Message.STATUS_RECIEVED;
} else if (packet.hasChild("sent")) {
2014-06-04 12:31:19 +02:00
forwarded = packet.findChild("sent").findChild("forwarded");
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;
}
Element message = forwarded.findChild("message");
if ((message == null) || (!message.hasChild("body"))) {
if (status == Message.STATUS_RECIEVED) {
parseNormal(message, account);
}
return null;
}
if (status == Message.STATUS_RECIEVED) {
fullJid = message.getAttribute("from");
2014-07-27 22:31:33 +02:00
if (fullJid == null ) {
return null;
} else {
updateLastseen(message, account, true);
}
} else {
fullJid = message.getAttribute("to");
2014-07-27 22:31:33 +02:00
if (fullJid == null) {
return null;
}
2014-07-16 12:42:44 +02:00
}
String[] parts = fullJid.split("/");
2014-06-04 12:31:19 +02:00
Conversation conversation = mXmppConnectionService
.findOrCreateConversation(account, parts[0], false);
conversation.setLatestMarkableMessageId(getMarkableMessageId(packet));
2014-06-04 12:31:19 +02:00
String pgpBody = getPgpBody(message);
Message finishedMessage;
2014-06-04 12:31:19 +02:00
if (pgpBody != null) {
finishedMessage = 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();
finishedMessage = new Message(conversation, fullJid, body,
Message.ENCRYPTION_NONE, status);
2014-06-04 11:55:38 +02:00
}
finishedMessage.setTime(getTimestamp(message));
return finishedMessage;
}
private 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);
}
private void parseNormal(Element packet, Account account) {
2014-08-05 01:36:17 +02:00
if (packet.hasChild("event","http://jabber.org/protocol/pubsub#event")) {
Element event = packet.findChild("event","http://jabber.org/protocol/pubsub#event");
parseEvent(event,packet.getAttribute("from"),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.getAttribute("from").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.getAttribute("from").split("/");
updateLastseen(packet, account, false);
mXmppConnectionService.markMessage(account, fromParts[0], id,
Message.STATUS_SEND_RECEIVED);
2014-07-21 16:04:53 +02:00
} else if (packet.hasChild("x","http://jabber.org/protocol/muc#user")) {
Element x = packet.findChild("x","http://jabber.org/protocol/muc#user");
2014-06-06 18:49:35 +02:00
if (x.hasChild("invite")) {
Conversation conversation = mXmppConnectionService
.findOrCreateConversation(account,
packet.getAttribute("from"), true);
if (!conversation.getMucOptions().online()) {
mXmppConnectionService.joinMuc(conversation);
mXmppConnectionService.updateConversationUi();
}
2014-06-06 18:49:35 +02:00
}
2014-07-21 16:04:53 +02:00
} else if (packet.hasChild("x", "jabber:x:conference")) {
Element x = packet.findChild("x", "jabber:x:conference");
String jid = x.getAttribute("jid");
if (jid!=null) {
Conversation conversation = mXmppConnectionService
.findOrCreateConversation(account,jid, true);
if (!conversation.getMucOptions().online()) {
mXmppConnectionService.joinMuc(conversation);
mXmppConnectionService.updateConversationUi();
}
}
2014-06-06 18:49:35 +02:00
}
}
2014-02-28 00:22:56 +01:00
2014-08-05 01:36:17 +02:00
private void parseEvent(Element event, String from, Account account) {
Element items = event.findChild("items");
String node = items.getAttribute("node");
if (node!=null) {
2014-08-05 22:58:46 +02:00
if (node.equals("urn:xmpp:avatar:metadata")) {
Avatar avatar = Avatar.parseMetadata(items);
avatar.owner = from;
if (mXmppConnectionService.getFileBackend().isAvatarCached(avatar)) {
if (account.getJid().equals(from)) {
account.setAvatar(avatar.getFilename());
} else {
Contact contact = account.getRoster().getContact(from);
contact.setAvatar(avatar.getFilename());
}
} else {
mXmppConnectionService.fetchAvatar(account, avatar);
}
} else {
Log.d("xmppService",account.getJid()+": "+node+" from "+from);
}
2014-08-05 01:36:17 +02:00
} else {
Log.d("xmppService",event.toString());
}
}
private String getPgpBody(Element message) {
Element child = message.findChild("x", "jabber:x:encrypted");
2014-06-04 12:31:19 +02:00
if (child == null) {
return null;
} else {
return child.getContent();
2014-02-28 00:22:56 +01: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 18:49:35 +02: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 {
parseNormal(packet, account);
}
} 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 12:45:56 +02:00
lastCarbonMessageReceived = SystemClock
.elapsedRealtime();
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);
return;
2014-08-05 01:36:17 +02:00
} else if (packet.getType() == MessagePacket.TYPE_HEADLINE) {
this.parseHeadline(packet, account);
return;
}
if ((message == null) || (message.getBody() == null)) {
return;
}
if ((mXmppConnectionService.confirmMessages())
&& ((packet.getId() != null))) {
if (packet.hasChild("markable", "urn:xmpp:chat-markers:0")) {
MessagePacket receipt = mXmppConnectionService.getMessageGenerator().received(account, packet, "urn:xmpp:chat-markers:0");
mXmppConnectionService.sendMessagePacket(account, receipt);
}
if (packet.hasChild("request", "urn:xmpp:receipts")) {
MessagePacket receipt = mXmppConnectionService.getMessageGenerator().received(account, packet, "urn:xmpp:receipts");
mXmppConnectionService.sendMessagePacket(account, receipt);
}
}
Conversation conversation = message.getConversation();
conversation.getMessages().add(message);
if (packet.getType() != MessagePacket.TYPE_ERROR) {
mXmppConnectionService.databaseBackend.createMessage(message);
}
2014-07-12 13:42:17 +02:00
mXmppConnectionService.notifyUi(conversation, notify);
}
2014-08-05 01:36:17 +02:00
private void parseHeadline(MessagePacket packet, Account account) {
if (packet.hasChild("event","http://jabber.org/protocol/pubsub#event")) {
Element event = packet.findChild("event","http://jabber.org/protocol/pubsub#event");
parseEvent(event,packet.getFrom(),account);
}
}
}