package de.thedevstack.conversationsplus.crypto.otr; import net.java.otr4j.session.Session; import net.java.otr4j.session.SessionStatus; import de.thedevstack.conversationsplus.crypto.OtrService; import de.thedevstack.conversationsplus.entities.Conversation; import de.thedevstack.conversationsplus.entities.Message; import de.thedevstack.conversationsplus.utils.CryptoHelper; import de.thedevstack.conversationsplus.utils.UiUpdateHelper; import de.thedevstack.conversationsplus.utils.XmppConnectionServiceAccessor; import de.thedevstack.conversationsplus.xmpp.jid.Jid; /** */ public class OtrMessageParser { public static Message parseOtrChat(String body, Jid from, String id, Conversation conversation) { String presence; if (from.isBareJid()) { presence = ""; } else { presence = from.getResourcepart(); } if (body.matches("^\\?OTRv\\d{1,2}\\?.*")) { conversation.endOtrIfNeeded(); } if (!conversation.hasValidOtrSession()) { conversation.startOtrSession(presence,false); } else { String foreignPresence = conversation.getOtrSession().getSessionID().getUserID(); if (!foreignPresence.equals(presence)) { conversation.endOtrIfNeeded(); conversation.startOtrSession(presence, false); } } try { conversation.setLastReceivedOtrMessageId(id); Session otrSession = conversation.getOtrSession(); body = otrSession.transformReceiving(body); SessionStatus status = otrSession.getSessionStatus(); if (body == null && status == SessionStatus.ENCRYPTED) { XmppConnectionServiceAccessor.xmppConnectionService.onOtrSessionEstablished(conversation); return null; } else if (body == null && status == SessionStatus.FINISHED) { conversation.resetOtrSession(); UiUpdateHelper.updateConversationUi(); return null; } else 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; } final OtrService otrService = conversation.getAccount().getOtrService(); Message finishedMessage = new Message(conversation, body, Message.ENCRYPTION_OTR); finishedMessage.setFingerprint(otrService.getFingerprint(otrSession.getRemotePublicKey())); conversation.setLastReceivedOtrMessageId(null); return finishedMessage; } catch (Exception e) { conversation.resetOtrSession(); return null; } } }