Search in sources :

Example 6 with TLV

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

the class OtrChatListener method onIncomingMessage.

@Override
public boolean onIncomingMessage(ChatSession session, Message msg, boolean notifyUser) {
    OtrDebugLogger.log("processing incoming message: " + msg.getID());
    boolean result = false;
    if (mOtrChatManager == null || msg.getType() != Imps.MessageType.INCOMING) {
        if (!TextUtils.isEmpty(msg.getBody())) {
            result = mMessageListener.onIncomingMessage(session, msg, notifyUser);
        } else {
            OtrDebugLogger.log("incoming body was empty");
        }
        return result;
    } else {
        // Do the OTR decryption thing
        String body = msg.getBody();
        String remoteAddress = msg.getFrom().getAddress();
        String localAddress = msg.getTo().getAddress();
        SessionID sessionID = mOtrChatManager.getSessionId(localAddress, remoteAddress);
        SessionStatus otrStatus = mOtrChatManager.getSessionStatus(sessionID);
        if (otrStatus == SessionStatus.ENCRYPTED) {
            boolean verified = mOtrChatManager.getKeyManager().isVerified(sessionID);
            if (verified) {
                msg.setType(Imps.MessageType.INCOMING_ENCRYPTED_VERIFIED);
            } else {
                msg.setType(Imps.MessageType.INCOMING_ENCRYPTED);
            }
        }
        List<TLV> tlvs = new ArrayList<TLV>();
        try {
            body = mOtrChatManager.decryptMessage(localAddress, remoteAddress, body, tlvs);
            if (!TextUtils.isEmpty(body)) {
                msg.setBody(body);
                result = mMessageListener.onIncomingMessage(session, msg, notifyUser);
            } else {
                OtrDebugLogger.log("Decrypted incoming body was null (otrdata?)");
            }
            for (TLV tlv : tlvs) {
                if (tlv.getType() == TLV_DATA_REQUEST) {
                    OtrDebugLogger.log("Got a TLV Data Request: " + new String(tlv.getValue()));
                    mMessageListener.onIncomingDataRequest(session, msg, tlv.getValue());
                    result = true;
                } else if (tlv.getType() == TLV_DATA_RESPONSE) {
                    OtrDebugLogger.log("Got a TLV Data Response: " + new String(tlv.getValue()));
                    mMessageListener.onIncomingDataResponse(session, msg, tlv.getValue());
                    result = true;
                }
            }
        } catch (OtrException oe) {
            OtrDebugLogger.log("error decrypting message: " + msg.getID());
            // mOtrChatManager.refreshSession(sessionID.getLocalUserId(),sessionID.getRemoteUserId());
            // msg.setBody("[" + "You received an unreadable encrypted message" + "]");
            // mMessageListener.onIncomingMessage(session, msg);
            mOtrChatManager.injectMessage(sessionID, "");
        }
        SessionStatus newStatus = mOtrChatManager.getSessionStatus(sessionID.getLocalUserId(), sessionID.getRemoteUserId());
        if (newStatus != otrStatus) {
            OtrDebugLogger.log("OTR status changed from: " + otrStatus + " to " + newStatus);
            mMessageListener.onStatusChanged(session, newStatus);
        }
        return result;
    }
}
Also used : SessionStatus(net.java.otr4j.session.SessionStatus) ArrayList(java.util.ArrayList) OtrException(net.java.otr4j.OtrException) SessionID(net.java.otr4j.session.SessionID) TLV(net.java.otr4j.session.TLV)

Example 7 with TLV

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

the class OtrChatManager method initSmp.

public void initSmp(SessionID sessionID, String question, String secret) throws OtrException {
    OtrSm otrSm = mOtrSms.get(sessionID.toString());
    List<TLV> tlvs;
    if (otrSm == null) {
        showError(sessionID, "Could not perform verification because conversation is not encrypted");
        return;
    }
    tlvs = otrSm.initRespondSmp(question, secret, true);
    String encrypted = mOtrEngine.transformSending(sessionID, "", tlvs);
    mOtrEngineHost.injectMessage(sessionID, encrypted);
}
Also used : OtrSm(net.java.otr4j.session.OtrSm) TLV(net.java.otr4j.session.TLV)

Aggregations

TLV (net.java.otr4j.session.TLV)7 OtrSm (net.java.otr4j.session.OtrSm)4 SessionID (net.java.otr4j.session.SessionID)3 ArrayList (java.util.ArrayList)2 OtrException (net.java.otr4j.OtrException)2 SessionStatus (net.java.otr4j.session.SessionStatus)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1