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

383 lines
14 KiB
Java
Raw Normal View History

2014-05-14 06:56:34 -04:00
package eu.siacs.conversations.parser;
import android.util.Log;
import android.util.Pair;
import net.java.otr4j.session.Session;
import net.java.otr4j.session.SessionStatus;
import eu.siacs.conversations.Config;
2014-02-28 12:46:01 -05:00
import eu.siacs.conversations.entities.Account;
2014-08-05 16:58:46 -04:00
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.entities.MucOptions;
import eu.siacs.conversations.http.HttpConnectionManager;
2014-12-13 06:25:52 -05:00
import eu.siacs.conversations.services.MessageArchiveService;
2014-02-28 12:46:01 -05:00
import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.utils.CryptoHelper;
2014-02-28 12:46:01 -05:00
import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xmpp.OnMessagePacketReceived;
import eu.siacs.conversations.xmpp.chatstate.ChatState;
2014-11-06 14:10:03 -05:00
import eu.siacs.conversations.xmpp.jid.Jid;
2014-08-05 16:58:46 -04:00
import eu.siacs.conversations.xmpp.pep.Avatar;
2014-03-10 14:22:13 -04:00
import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
public class MessageParser extends AbstractParser implements
OnMessagePacketReceived {
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
private boolean extractChatState(Conversation conversation, final MessagePacket packet) {
ChatState state = ChatState.parse(packet);
if (state != null && conversation != null) {
final Account account = conversation.getAccount();
Jid from = packet.getFrom();
if (from.toBareJid().equals(account.getJid().toBareJid())) {
conversation.setOutgoingChatState(state);
return false;
} else {
return conversation.setIncomingChatState(state);
}
}
return false;
}
private Message parseOtrChat(String body, Jid from, String id, Conversation conversation) {
String presence;
2014-11-06 14:10:03 -05:00
if (from.isBareJid()) {
presence = "";
} else {
2014-11-06 14:10:03 -05:00
presence = from.getResourcepart();
}
if (body.matches("^\\?OTRv\\d{1,2}\\?.*")) {
conversation.endOtrIfNeeded();
}
if (!conversation.hasValidOtrSession()) {
conversation.startOtrSession(presence,false);
2014-03-07 18:48:52 -05:00
} else {
String foreignPresence = conversation.getOtrSession().getSessionID().getUserID();
if (!foreignPresence.equals(presence)) {
conversation.endOtrIfNeeded();
conversation.startOtrSession(presence, false);
2014-03-07 18:48:52 -05:00
}
}
try {
conversation.setLastReceivedOtrMessageId(id);
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)) {
conversation.setNextEncryption(Message.ENCRYPTION_OTR);
2014-06-11 15:53:25 -04:00
mXmppConnectionService.onOtrSessionEstablished(conversation);
} else if ((before != after) && (after == SessionStatus.FINISHED)) {
conversation.setNextEncryption(Message.ENCRYPTION_NONE);
conversation.resetOtrSession();
mXmppConnectionService.updateConversationUi();
}
2014-06-04 06:31:19 -04: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;
}
Message finishedMessage = new Message(conversation, body, Message.ENCRYPTION_OTR, Message.STATUS_RECEIVED);
conversation.setLastReceivedOtrMessageId(null);
return finishedMessage;
} catch (Exception e) {
conversation.resetOtrSession();
return null;
}
}
2014-06-04 06:31:19 -04:00
2015-04-21 12:36:11 -04:00
private class Invite {
Jid jid;
String password;
Invite(Jid jid, String password) {
this.jid = jid;
this.password = password;
2015-01-07 10:45:44 -05:00
}
2015-04-21 12:36:11 -04:00
}
private Invite extractInvite(Element message) {
Element x = message.findChild("x", "http://jabber.org/protocol/muc#user");
2015-04-21 12:36:11 -04:00
if (x != null) {
Element invite = x.findChild("invite");
if (invite != null) {
Element pw = x.findChild("password");
return new Invite(message.getAttributeAsJid("from"), pw != null ? pw.getContent(): null);
}
2015-01-07 10:45:44 -05:00
} else {
2015-04-21 12:36:11 -04:00
x = message.findChild("x","jabber:x:conference");
if (x != null) {
return new Invite(x.getAttributeAsJid("jid"),x.getAttribute("password"));
}
2014-06-06 12:49:35 -04:00
}
2015-04-21 12:36:11 -04:00
return null;
2014-06-06 12:49:35 -04:00
}
2014-02-27 18:22:56 -05:00
2014-11-06 14:10:03 -05:00
private void parseEvent(final Element event, final Jid from, final Account account) {
2014-08-04 19:36:17 -04:00
Element items = event.findChild("items");
2014-11-20 12:42:47 -05:00
if (items == null) {
return;
}
2014-08-04 19:36:17 -04:00
String node = items.getAttribute("node");
2014-11-20 12:42:47 -05:00
if (node == null) {
return;
}
if (node.equals("urn:xmpp:avatar:metadata")) {
Avatar avatar = Avatar.parseMetadata(items);
if (avatar != null) {
avatar.owner = from;
if (mXmppConnectionService.getFileBackend().isAvatarCached(
avatar)) {
if (account.getJid().toBareJid().equals(from)) {
if (account.setAvatar(avatar.getFilename())) {
mXmppConnectionService.databaseBackend
.updateAccount(account);
2014-08-15 07:11:33 -04:00
}
2014-11-20 12:42:47 -05:00
mXmppConnectionService.getAvatarService().clear(
account);
mXmppConnectionService.updateConversationUi();
mXmppConnectionService.updateAccountUi();
2014-08-05 16:58:46 -04:00
} else {
2014-11-20 12:42:47 -05:00
Contact contact = account.getRoster().getContact(
from);
contact.setAvatar(avatar);
2014-11-20 12:42:47 -05:00
mXmppConnectionService.getAvatarService().clear(
contact);
mXmppConnectionService.updateConversationUi();
mXmppConnectionService.updateRosterUi();
2014-08-05 16:58:46 -04:00
}
2014-11-20 12:42:47 -05:00
} else {
mXmppConnectionService.fetchAvatar(account, avatar);
2014-08-05 16:58:46 -04:00
}
2014-11-20 12:42:47 -05:00
}
} else if (node.equals("http://jabber.org/protocol/nick")) {
Element item = items.findChild("item");
if (item != null) {
Element nick = item.findChild("nick",
"http://jabber.org/protocol/nick");
if (nick != null) {
if (from != null) {
Contact contact = account.getRoster().getContact(
from);
contact.setPresenceName(nick.getContent());
mXmppConnectionService.getAvatarService().clear(account);
mXmppConnectionService.updateConversationUi();
mXmppConnectionService.updateAccountUi();
}
}
2014-08-05 16:58:46 -04:00
}
2014-08-04 19:36:17 -04:00
}
}
@Override
public void onMessagePacketReceived(Account account, MessagePacket original) {
final MessagePacket packet;
Long timestamp = null;
final boolean isForwarded;
boolean carbon = false; //live carbons or mam-sub
if (original.fromServer(account)) {
Pair<MessagePacket, Long> f;
f = original.getForwardedMessagePacket("received", "urn:xmpp:carbons:2");
f = f == null ? original.getForwardedMessagePacket("sent", "urn:xmpp:carbons:2") : f;
f = f == null ? original.getForwardedMessagePacket("result", "urn:xmpp:mam:0") : f;
packet = f != null ? f.first : original;
timestamp = f != null ? f.second : null;
isForwarded = f != null;
carbon = original.hasChild("received", "urn:xmpp:carbons:2") || original.hasChild("received", "urn:xmpp:carbons:2");
Element fin = packet.findChild("fin", "urn:xmpp:mam:0");
if (fin != null) {
mXmppConnectionService.getMessageArchiveService().processFin(fin);
return;
}
} else {
packet = original;
isForwarded = false;
}
if (timestamp == null) {
timestamp = AbstractParser.getTimestamp(packet, System.currentTimeMillis());
}
final String body = packet.getBody();
final String encrypted = packet.findChildContent("x", "jabber:x:encrypted");
int status;
final Jid to = packet.getTo();
final Jid from = packet.getFrom();
final Jid counterpart;
final String id = packet.getId();
boolean isTypeGroupChat = packet.getType() == MessagePacket.TYPE_GROUPCHAT;
boolean properlyAddressed = !to.isBareJid() || account.countPresences() == 1;
if (packet.fromAccount(account)) {
status = Message.STATUS_SEND;
counterpart = to;
} else {
status = Message.STATUS_RECEIVED;
counterpart = from;
2014-02-27 18:22:56 -05:00
}
if (from == null || to == null) {
Log.d(Config.LOGTAG,"no to or from in: "+packet.toString());
return;
}
2014-06-06 12:49:35 -04:00
Invite invite = extractInvite(packet);
if (invite != null && invite.jid != null) {
Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, invite.jid, true);
if (!conversation.getMucOptions().online()) {
conversation.getMucOptions().setPassword(invite.password);
mXmppConnectionService.databaseBackend.updateConversation(conversation);
mXmppConnectionService.joinMuc(conversation);
mXmppConnectionService.updateConversationUi();
}
return;
}
if (extractChatState(mXmppConnectionService.find(account,from), packet)) {
mXmppConnectionService.updateConversationUi();
}
if (body != null || encrypted != null) {
Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, counterpart.toBareJid(), isTypeGroupChat);
if (isTypeGroupChat) {
if (counterpart.getResourcepart().equals(conversation.getMucOptions().getActualNick())) {
status = Message.STATUS_SEND;
if (mXmppConnectionService.markMessage(conversation, id, Message.STATUS_SEND_RECEIVED)) {
return;
} else if (id == null) {
Message message = conversation.findSentMessageWithBody(packet.getBody());
if (message != null) {
mXmppConnectionService.markMessage(message, Message.STATUS_SEND_RECEIVED);
return;
}
}
} else {
status = Message.STATUS_RECEIVED;
}
}
Message message;
if (body != null && body.startsWith("?OTR")) {
if (!isForwarded && !isTypeGroupChat && properlyAddressed) {
message = parseOtrChat(body, from, id, conversation);
if (message == null) {
return;
}
} else {
message = new Message(conversation, body, Message.ENCRYPTION_NONE, status);
2014-12-04 19:54:16 -05:00
}
} else if (encrypted != null) {
message = new Message(conversation, encrypted, Message.ENCRYPTION_PGP, status);
} else {
message = new Message(conversation, body, Message.ENCRYPTION_NONE, status);
}
message.setCounterpart(counterpart);
message.setRemoteMsgId(id);
message.setTime(timestamp);
message.markable = packet.hasChild("markable", "urn:xmpp:chat-markers:0");
if (conversation.getMode() == Conversation.MODE_MULTI) {
message.setTrueCounterpart(conversation.getMucOptions().getTrueCounterpart(counterpart.getResourcepart()));
if (!isTypeGroupChat) {
message.setType(Message.TYPE_PRIVATE);
}
}
updateLastseen(packet,account,true);
conversation.add(message);
if (carbon || status == Message.STATUS_RECEIVED) {
mXmppConnectionService.markRead(conversation);
account.activateGracePeriod();
} else if (!isForwarded) {
message.markUnread();
}
if (mXmppConnectionService.confirmMessages() && id != null && !packet.fromAccount(account)) {
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);
}
}
if (account.getXmppConnection() != null && account.getXmppConnection().getFeatures().advancedStreamFeaturesLoaded()) {
if (conversation.setLastMessageTransmitted(System.currentTimeMillis())) {
mXmppConnectionService.updateConversation(conversation);
}
}
if (message.getStatus() == Message.STATUS_RECEIVED
&& conversation.getOtrSession() != null
&& !conversation.getOtrSession().getSessionID().getUserID()
.equals(message.getCounterpart().getResourcepart())) {
conversation.endOtrIfNeeded();
}
if (message.getEncryption() == Message.ENCRYPTION_NONE
|| mXmppConnectionService.saveEncryptedMessages()) {
mXmppConnectionService.databaseBackend.createMessage(message);
}
final HttpConnectionManager manager = this.mXmppConnectionService.getHttpConnectionManager();
if (message.trusted() && message.bodyContainsDownloadable() && manager.getAutoAcceptFileSize() > 0) {
manager.createNewConnection(message);
} else if (!message.isRead()) {
mXmppConnectionService.getNotificationService().push(message);
}
mXmppConnectionService.updateConversationUi();
} else {
if (packet.hasChild("subject") && isTypeGroupChat) {
Conversation conversation = mXmppConnectionService.find(account, from.toBareJid());
if (conversation != null && conversation.getMode() == Conversation.MODE_MULTI) {
conversation.setHasMessagesLeftOnServer(true);
conversation.getMucOptions().setSubject(packet.findChildContent("subject"));
mXmppConnectionService.updateConversationUi();
return;
}
}
}
Element received = packet.findChild("received", "urn:xmpp:chat-markers:0");
if (received == null) {
received = packet.findChild("received", "urn:xmpp:receipts");
}
if (received != null && !packet.fromAccount(account)) {
mXmppConnectionService.markMessage(account, from.toBareJid(), received.getAttribute("id"), Message.STATUS_SEND_RECEIVED);
}
Element displayed = packet.findChild("displayed", "urn:xmpp:chat-markers:0");
if (displayed != null) {
if (packet.fromAccount(account)) {
Conversation conversation = mXmppConnectionService.find(account,counterpart.toBareJid());
mXmppConnectionService.markRead(conversation);
} else {
updateLastseen(packet, account, true);
final Message displayedMessage = mXmppConnectionService.markMessage(account, from.toBareJid(), displayed.getAttribute("id"), Message.STATUS_SEND_DISPLAYED);
Message message = displayedMessage == null ? null : displayedMessage.prev();
while (message != null
&& message.getStatus() == Message.STATUS_SEND_RECEIVED
&& message.getTimeSent() < displayedMessage.getTimeSent()) {
mXmppConnectionService.markMessage(message, Message.STATUS_SEND_DISPLAYED);
message = message.prev();
}
}
2014-09-28 09:21:56 -04:00
}
2014-08-04 19:36:17 -04:00
Element event = packet.findChild("event", "http://jabber.org/protocol/pubsub#event");
if (event != null) {
parseEvent(event, from, account);
2014-08-04 19:36:17 -04:00
}
String nick = packet.findChildContent("nick", "http://jabber.org/protocol/nick");
if (nick != null) {
Contact contact = account.getRoster().getContact(from);
contact.setPresenceName(nick);
}
}
}