aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/crypto/otr/OtrUtil.java
blob: 9707545509f96b6bfc3843eb7a5cef3c08f6a0a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package de.thedevstack.conversationsplus.crypto.otr;

import net.java.otr4j.session.SessionID;
import net.java.otr4j.session.SessionImpl;

import de.thedevstack.conversationsplus.entities.Account;
import de.thedevstack.conversationsplus.entities.Conversation;
import de.thedevstack.conversationsplus.xmpp.Forwarded;
import de.thedevstack.conversationsplus.xmpp.jid.Jid;
import de.thedevstack.conversationsplus.xmpp.stanzas.MessagePacket;

/**
 */
public class OtrUtil {
    public static boolean isOtrSessionActive(Conversation conversation) {
        return null != conversation && null != conversation.getOtrSession();
    }

    public static boolean isCounterpartOfActiveOtrSession(Conversation conversation, Jid counterpart) {
        SessionImpl otrSession = (null != conversation) ? conversation.getOtrSession() : null;
        SessionID sessionID = (null != otrSession) ? otrSession.getSessionID() : null;
        String userID = (null != sessionID) ? sessionID.getUserID() : null;

        return null != userID && null != counterpart && userID.equals(counterpart.getResourcepart());
    }

    public static boolean isValidOtrMessagePacket(MessagePacket messagePacket, Account account) {
        Jid to = messagePacket.getTo();
        boolean isProperlyAddressed = (to != null) && (!to.isBareJid() || account.countPresences() <= 1);
        boolean isNotTypeGroupChat = messagePacket.getType() != MessagePacket.TYPE_GROUPCHAT;
        boolean isNotForwarded = !messagePacket.hasChildRecursive(Forwarded.FORWARDED);

        return isProperlyAddressed && isNotTypeGroupChat && isNotForwarded;
    }

    public static boolean isOtrBody(String body) {
        return body != null && body.startsWith("?OTR");
    }
}