Search in sources :

Example 1 with OtrCryptoEngine

use of net.java.otr4j.crypto.OtrCryptoEngine in project xabber-android by redsolution.

the class OTRManager method sessionStatusChanged.

@Override
public void sessionStatusChanged(SessionID sessionID) {
    removeSMRequest(sessionID.getAccountID(), sessionID.getUserID());
    removeSMProgress(sessionID.getAccountID(), sessionID.getUserID());
    Session session = sessions.get(sessionID.getAccountID(), sessionID.getUserID());
    SessionStatus sStatus = session.getSessionStatus();
    LogManager.i(this, "session status changed " + sessionID.getUserID() + " status: " + sStatus);
    if (sStatus == SessionStatus.ENCRYPTED) {
        finished.remove(sessionID.getAccountID(), sessionID.getUserID());
        PublicKey remotePublicKey = session.getRemotePublicKey();
        String value;
        try {
            OtrCryptoEngine otrCryptoEngine = new OtrCryptoEngineImpl();
            value = otrCryptoEngine.getFingerprint(remotePublicKey);
        } catch (OtrCryptoException e) {
            LogManager.exception(this, e);
            value = null;
        }
        if (value != null) {
            actives.put(sessionID.getAccountID(), sessionID.getUserID(), value);
            if (fingerprints.get(sessionID.getAccountID(), sessionID.getUserID(), value) == null) {
                fingerprints.put(sessionID.getAccountID(), sessionID.getUserID(), value, false);
                requestToWrite(sessionID.getAccountID(), sessionID.getUserID(), value, false);
            }
        }
        newAction(sessionID.getAccountID(), sessionID.getUserID(), null, isVerified(sessionID.getAccountID(), sessionID.getUserID()) ? ChatAction.otr_verified : ChatAction.otr_encryption);
        AbstractChat chat = getChat(sessionID.getAccountID(), sessionID.getUserID());
        if (chat != null) {
            chat.sendMessages();
        }
    } else if (sStatus == SessionStatus.PLAINTEXT) {
        actives.remove(sessionID.getAccountID(), sessionID.getUserID());
        sessions.remove(sessionID.getAccountID(), sessionID.getUserID());
        finished.remove(sessionID.getAccountID(), sessionID.getUserID());
        try {
            session.endSession();
        } catch (OtrException e) {
            LogManager.exception(this, e);
        }
        newAction(sessionID.getAccountID(), sessionID.getUserID(), null, ChatAction.otr_plain);
    } else if (sStatus == SessionStatus.FINISHED) {
        actives.remove(sessionID.getAccountID(), sessionID.getUserID());
        sessions.remove(sessionID.getAccountID(), sessionID.getUserID());
        finished.put(sessionID.getAccountID(), sessionID.getUserID(), true);
        newAction(sessionID.getAccountID(), sessionID.getUserID(), null, ChatAction.otr_finish);
        // if session was finished then clear OTR-resource for this chat
        RegularChat chat = (RegularChat) getChat(sessionID.getAccountID(), sessionID.getUserID());
        if (chat != null) {
            chat.setOTRresource(null);
        }
    } else {
        throw new IllegalStateException();
    }
    onContactChanged(sessionID);
}
Also used : OtrCryptoEngine(net.java.otr4j.crypto.OtrCryptoEngine) OtrCryptoException(net.java.otr4j.crypto.OtrCryptoException) PublicKey(java.security.PublicKey) AbstractChat(com.xabber.android.data.message.AbstractChat) SessionStatus(net.java.otr4j.session.SessionStatus) OtrCryptoEngineImpl(net.java.otr4j.crypto.OtrCryptoEngineImpl) OtrException(net.java.otr4j.OtrException) RegularChat(com.xabber.android.data.message.RegularChat) Session(net.java.otr4j.session.Session)

Aggregations

AbstractChat (com.xabber.android.data.message.AbstractChat)1 RegularChat (com.xabber.android.data.message.RegularChat)1 PublicKey (java.security.PublicKey)1 OtrException (net.java.otr4j.OtrException)1 OtrCryptoEngine (net.java.otr4j.crypto.OtrCryptoEngine)1 OtrCryptoEngineImpl (net.java.otr4j.crypto.OtrCryptoEngineImpl)1 OtrCryptoException (net.java.otr4j.crypto.OtrCryptoException)1 Session (net.java.otr4j.session.Session)1 SessionStatus (net.java.otr4j.session.SessionStatus)1