Search in sources :

Example 11 with Session

use of net.java.otr4j.session.Session in project Zom-Android by zom.

the class OtrChatManager method sessionStatusChanged.

@Override
public void sessionStatusChanged(final SessionID sessionID) {
    SessionStatus sStatus = mOtrEngine.getSessionStatus(sessionID);
    OtrDebugLogger.log("session status changed: " + sStatus);
    final Session session = mOtrEngine.getSession(sessionID);
    OtrSm otrSm = mOtrSms.get(sessionID.toString());
    if (sStatus == SessionStatus.ENCRYPTED) {
        PublicKey remoteKey = mOtrEngine.getRemotePublicKey(sessionID);
        mOtrEngineHost.storeRemoteKey(sessionID, remoteKey);
        if (otrSm == null) {
            // SMP handler - make sure we only add this once per session!
            otrSm = new OtrSm(session, mOtrEngineHost.getKeyManager(), sessionID, OtrChatManager.this);
            session.addTlvHandler(otrSm);
            mOtrSms.put(sessionID.toString(), otrSm);
        }
    } else if (sStatus == SessionStatus.PLAINTEXT) {
        if (otrSm != null) {
            session.removeTlvHandler(otrSm);
            mOtrSms.remove(sessionID.toString());
        }
        mOtrEngineHost.removeSessionResource(sessionID);
    } else if (sStatus == SessionStatus.FINISHED) {
    // Do nothing.  The user must take affirmative action to
    // restart or end the session, so that they don't send
    // plaintext by mistake.
    }
}
Also used : PublicKey(java.security.PublicKey) SessionStatus(net.java.otr4j.session.SessionStatus) OtrSm(net.java.otr4j.session.OtrSm) Session(net.java.otr4j.session.Session)

Example 12 with Session

use of net.java.otr4j.session.Session 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)

Example 13 with Session

use of net.java.otr4j.session.Session in project xabber-android by redsolution.

the class OTRManager method getOrCreateSession.

private Session getOrCreateSession(String account, String user) {
    Session session = sessions.get(account, user);
    if (session != null) {
        LogManager.i(this, "Found session with id " + session.getSessionID() + " with status " + session.getSessionStatus() + " for user " + user);
        return session;
    }
    LogManager.i(this, "Creating new session for " + user);
    session = new SessionImpl(new SessionID(account, user, "xmpp"), this);
    session.addOtrEngineListener(this);
    sessions.put(account, user, session);
    return session;
}
Also used : SessionImpl(net.java.otr4j.session.SessionImpl) SessionID(net.java.otr4j.session.SessionID) Session(net.java.otr4j.session.Session)

Example 14 with Session

use of net.java.otr4j.session.Session in project xabber-android by redsolution.

the class OTRManager method transformReceiving.

/**
 * Transform incoming message after receiving.
 */
public String transformReceiving(AccountJid account, UserJid user, String content) throws OtrException {
    LogManager.i(this, "transform incoming message... " + content, "transform incoming message... ***");
    Session session = getOrCreateSession(account.toString(), user.toString());
    try {
        String s = session.transformReceiving(content);
        LogManager.i(this, "transformed incoming message: " + s + " session status: " + session.getSessionStatus(), "transformed incoming message: " + "***" + " session status: " + session.getSessionStatus());
        return s;
    } catch (UnsupportedOperationException e) {
        throw new OtrException(e);
    }
}
Also used : OtrException(net.java.otr4j.OtrException) Session(net.java.otr4j.session.Session)

Example 15 with Session

use of net.java.otr4j.session.Session in project xabber-android by redsolution.

the class OTRManager method onContactUnAvailable.

public void onContactUnAvailable(String account, String user) {
    Session session = sessions.get(account, user);
    if (session == null) {
        return;
    }
    if (session.getSessionStatus() == SessionStatus.ENCRYPTED) {
        try {
            LogManager.i(this, "onContactUnAvailable. Refresh session for " + user);
            session.refreshSession();
        } catch (OtrException e) {
            LogManager.exception(this, e);
        }
    }
}
Also used : OtrException(net.java.otr4j.OtrException) Session(net.java.otr4j.session.Session)

Aggregations

Session (net.java.otr4j.session.Session)20 OtrException (net.java.otr4j.OtrException)13 SessionID (net.java.otr4j.session.SessionID)4 SessionStatus (net.java.otr4j.session.SessionStatus)4 MessagePacket (de.pixart.messenger.xmpp.stanzas.MessagePacket)3 MessagePacket (eu.siacs.conversations.xmpp.stanzas.MessagePacket)3 SessionImpl (net.java.otr4j.session.SessionImpl)3 XmppAxolotlMessage (de.pixart.messenger.crypto.axolotl.XmppAxolotlMessage)2 Account (de.pixart.messenger.entities.Account)2 Message (de.pixart.messenger.entities.Message)2 XmppAxolotlMessage (eu.siacs.conversations.crypto.axolotl.XmppAxolotlMessage)2 Account (eu.siacs.conversations.entities.Account)2 Message (eu.siacs.conversations.entities.Message)2 PublicKey (java.security.PublicKey)2 AbstractChat (com.xabber.android.data.message.AbstractChat)1 RegularChat (com.xabber.android.data.message.RegularChat)1 OtrService (de.pixart.messenger.crypto.OtrService)1 Conversation (de.pixart.messenger.entities.Conversation)1 InvalidJidException (de.pixart.messenger.xmpp.jid.InvalidJidException)1 OtrService (eu.siacs.conversations.crypto.OtrService)1