Search in sources :

Example 1 with PlainTextMessage

use of net.java.otr4j.io.messages.PlainTextMessage in project xabber-android by redsolution.

the class MamManager method getMessageItems.

private List<MessageItem> getMessageItems(org.jivesoftware.smackx.mam.MamManager.MamQueryResult mamQueryResult, AbstractChat chat) {
    List<MessageItem> messageItems = new ArrayList<>();
    for (Forwarded forwarded : mamQueryResult.forwardedMessages) {
        if (!(forwarded.getForwardedStanza() instanceof Message)) {
            continue;
        }
        Message message = (Message) forwarded.getForwardedStanza();
        DelayInformation delayInformation = forwarded.getDelayInformation();
        DelayInformation messageDelay = DelayInformation.from(message);
        String body = message.getBody();
        net.java.otr4j.io.messages.AbstractMessage otrMessage;
        try {
            otrMessage = SerializationUtils.toMessage(body);
        } catch (IOException e) {
            continue;
        }
        boolean encrypted = false;
        if (otrMessage != null) {
            if (otrMessage.messageType != net.java.otr4j.io.messages.AbstractMessage.MESSAGE_PLAINTEXT) {
                encrypted = true;
                try {
                    // this transforming just decrypt message if have keys. No action as injectMessage or something else
                    body = OTRManager.getInstance().transformReceivingIfSessionExist(chat.getAccount(), chat.getUser(), body);
                    if (OTRManager.getInstance().isEncrypted(body)) {
                        continue;
                    }
                } catch (Exception e) {
                    continue;
                }
            } else
                body = ((PlainTextMessage) otrMessage).cleanText;
        }
        boolean incoming = message.getFrom().asBareJid().equals(chat.getUser().getJid().asBareJid());
        MessageItem messageItem = new MessageItem();
        messageItem.setAccount(chat.getAccount());
        messageItem.setUser(chat.getUser());
        messageItem.setResource(chat.getUser().getJid().getResourceOrNull());
        messageItem.setText(body);
        messageItem.setTimestamp(delayInformation.getStamp().getTime());
        if (messageDelay != null) {
            messageItem.setDelayTimestamp(messageDelay.getStamp().getTime());
        }
        messageItem.setIncoming(incoming);
        messageItem.setStanzaId(message.getStanzaId());
        messageItem.setReceivedFromMessageArchive(true);
        messageItem.setRead(true);
        messageItem.setSent(true);
        messageItem.setEncrypted(encrypted);
        FileManager.processFileMessage(messageItem);
        messageItems.add(messageItem);
    }
    return messageItems;
}
Also used : MessageItem(com.xabber.android.data.database.messagerealm.MessageItem) PlainTextMessage(net.java.otr4j.io.messages.PlainTextMessage) Message(org.jivesoftware.smack.packet.Message) ArrayList(java.util.ArrayList) IOException(java.io.IOException) SmackException(org.jivesoftware.smack.SmackException) IOException(java.io.IOException) XMPPException(org.jivesoftware.smack.XMPPException) DelayInformation(org.jivesoftware.smackx.delay.packet.DelayInformation) PlainTextMessage(net.java.otr4j.io.messages.PlainTextMessage) Forwarded(org.jivesoftware.smackx.forward.packet.Forwarded)

Example 2 with PlainTextMessage

use of net.java.otr4j.io.messages.PlainTextMessage in project Zom-Android by zom.

the class SerializationUtils method toString.

// Message IO.
public static String toString(AbstractMessage m) throws IOException {
    StringWriter writer = new StringWriter();
    writer.write(SerializationConstants.HEAD);
    switch(m.messageType) {
        case AbstractMessage.MESSAGE_ERROR:
            ErrorMessage error = (ErrorMessage) m;
            writer.write(SerializationConstants.HEAD_ERROR);
            writer.write(error.error);
            break;
        case AbstractMessage.MESSAGE_PLAINTEXT:
            PlainTextMessage plaintxt = (PlainTextMessage) m;
            writer.write(plaintxt.cleanText);
            if (plaintxt.versions != null && plaintxt.versions.size() > 0) {
                writer.write(" \\t  \\t\\t\\t\\t \\t \\t \\t  ");
                for (int version : plaintxt.versions) {
                    if (version == 1)
                        writer.write("  \\t\\t  \\t ");
                    if (version == 2)
                        writer.write(" \\t \\t  \\t ");
                }
            }
            break;
        case AbstractMessage.MESSAGE_QUERY:
            QueryMessage query = (QueryMessage) m;
            if (query.versions.size() == 1 && query.versions.get(0) == 1) {
                writer.write(SerializationConstants.HEAD_QUERY_Q);
            } else {
                writer.write(SerializationConstants.HEAD_QUERY_V);
                for (int version : query.versions) writer.write(String.valueOf(version));
                writer.write(SerializationConstants.HEAD_QUERY_Q);
            }
            writer.write(OtrConstants.CommonRequest);
            break;
        case AbstractEncodedMessage.MESSAGE_DHKEY:
        case AbstractEncodedMessage.MESSAGE_REVEALSIG:
        case AbstractEncodedMessage.MESSAGE_SIGNATURE:
        case AbstractEncodedMessage.MESSAGE_DH_COMMIT:
        case AbstractEncodedMessage.MESSAGE_DATA:
            ByteArrayOutputStream o = new ByteArrayOutputStream();
            OtrOutputStream s = new OtrOutputStream(o);
            switch(m.messageType) {
                case AbstractEncodedMessage.MESSAGE_DHKEY:
                    DHKeyMessage dhkey = (DHKeyMessage) m;
                    s.writeShort(dhkey.protocolVersion);
                    s.writeByte(dhkey.messageType);
                    s.writeDHPublicKey(dhkey.dhPublicKey);
                    break;
                case AbstractEncodedMessage.MESSAGE_REVEALSIG:
                    RevealSignatureMessage revealsig = (RevealSignatureMessage) m;
                    s.writeShort(revealsig.protocolVersion);
                    s.writeByte(revealsig.messageType);
                    s.writeData(revealsig.revealedKey);
                    s.writeData(revealsig.xEncrypted);
                    s.writeMac(revealsig.xEncryptedMAC);
                    break;
                case AbstractEncodedMessage.MESSAGE_SIGNATURE:
                    SignatureMessage sig = (SignatureMessage) m;
                    s.writeShort(sig.protocolVersion);
                    s.writeByte(sig.messageType);
                    s.writeData(sig.xEncrypted);
                    s.writeMac(sig.xEncryptedMAC);
                    break;
                case AbstractEncodedMessage.MESSAGE_DH_COMMIT:
                    DHCommitMessage dhcommit = (DHCommitMessage) m;
                    s.writeShort(dhcommit.protocolVersion);
                    s.writeByte(dhcommit.messageType);
                    s.writeData(dhcommit.dhPublicKeyEncrypted);
                    s.writeData(dhcommit.dhPublicKeyHash);
                    break;
                case AbstractEncodedMessage.MESSAGE_DATA:
                    DataMessage data = (DataMessage) m;
                    s.writeShort(data.protocolVersion);
                    s.writeByte(data.messageType);
                    s.writeByte(data.flags);
                    s.writeInt(data.senderKeyID);
                    s.writeInt(data.recipientKeyID);
                    s.writeDHPublicKey(data.nextDH);
                    s.writeCtr(data.ctr);
                    s.writeData(data.encryptedMessage);
                    s.writeMac(data.mac);
                    s.writeData(data.oldMACKeys);
                    break;
            }
            writer.write(SerializationConstants.HEAD_ENCODED);
            writer.write(Base64.encodeToString(o.toByteArray(), Base64.NO_WRAP));
            writer.write(".");
            break;
        default:
            throw new IOException("Illegal message type.");
    }
    return writer.toString();
}
Also used : QueryMessage(net.java.otr4j.io.messages.QueryMessage) RevealSignatureMessage(net.java.otr4j.io.messages.RevealSignatureMessage) SignatureMessage(net.java.otr4j.io.messages.SignatureMessage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RevealSignatureMessage(net.java.otr4j.io.messages.RevealSignatureMessage) IOException(java.io.IOException) DHCommitMessage(net.java.otr4j.io.messages.DHCommitMessage) StringWriter(java.io.StringWriter) PlainTextMessage(net.java.otr4j.io.messages.PlainTextMessage) DataMessage(net.java.otr4j.io.messages.DataMessage) DHKeyMessage(net.java.otr4j.io.messages.DHKeyMessage) ErrorMessage(net.java.otr4j.io.messages.ErrorMessage)

Example 3 with PlainTextMessage

use of net.java.otr4j.io.messages.PlainTextMessage in project Zom-Android by zom.

the class SerializationUtils method toMessage.

public static AbstractMessage toMessage(String s) throws IOException {
    if (s == null || s.length() <= 1)
        return null;
    if (s.indexOf(SerializationConstants.HEAD) != 0 || s.length() <= SerializationConstants.HEAD.length()) {
        // Try to detect whitespace tag.
        final Matcher matcher = patternWhitespace.matcher(s);
        boolean v1 = false;
        boolean v2 = false;
        while (matcher.find()) {
            if (!v1 && matcher.start(2) > -1)
                v1 = true;
            if (!v2 && matcher.start(3) > -1)
                v2 = true;
            if (v1 && v2)
                break;
        }
        String cleanText = matcher.replaceAll("");
        List<Integer> versions;
        if (v1 && v2) {
            versions = new Vector<Integer>(2);
            versions.add(0, 1);
            versions.add(0, 2);
        } else if (v1) {
            versions = new Vector<Integer>(1);
            versions.add(0, 1);
        } else if (v2) {
            versions = new Vector<Integer>(1);
            versions.add(2);
        } else
            versions = null;
        return new PlainTextMessage(versions, cleanText);
    } else {
        char contentType = s.charAt(SerializationConstants.HEAD.length());
        String content = s.substring(SerializationConstants.HEAD.length() + 1);
        switch(contentType) {
            case SerializationConstants.HEAD_ENCODED:
                ByteArrayInputStream bin = new ByteArrayInputStream(Base64.decode(content, Base64.NO_WRAP));
                OtrInputStream otr = new OtrInputStream(bin);
                // We have an encoded message.
                int protocolVersion = otr.readShort();
                int messageType = otr.readByte();
                switch(messageType) {
                    case AbstractEncodedMessage.MESSAGE_DATA:
                        int flags = otr.readByte();
                        int senderKeyID = otr.readInt();
                        int recipientKeyID = otr.readInt();
                        DHPublicKey nextDH = otr.readDHPublicKey();
                        byte[] ctr = otr.readCtr();
                        byte[] encryptedMessage = otr.readData();
                        byte[] mac = otr.readMac();
                        byte[] oldMacKeys = otr.readMac();
                        return new DataMessage(protocolVersion, flags, senderKeyID, recipientKeyID, nextDH, ctr, encryptedMessage, mac, oldMacKeys);
                    case AbstractEncodedMessage.MESSAGE_DH_COMMIT:
                        byte[] dhPublicKeyEncrypted = otr.readData();
                        byte[] dhPublicKeyHash = otr.readData();
                        return new DHCommitMessage(protocolVersion, dhPublicKeyHash, dhPublicKeyEncrypted);
                    case AbstractEncodedMessage.MESSAGE_DHKEY:
                        DHPublicKey dhPublicKey = otr.readDHPublicKey();
                        return new DHKeyMessage(protocolVersion, dhPublicKey);
                    case AbstractEncodedMessage.MESSAGE_REVEALSIG:
                        {
                            byte[] revealedKey = otr.readData();
                            byte[] xEncrypted = otr.readData();
                            byte[] xEncryptedMac = otr.readMac();
                            return new RevealSignatureMessage(protocolVersion, xEncrypted, xEncryptedMac, revealedKey);
                        }
                    case AbstractEncodedMessage.MESSAGE_SIGNATURE:
                        {
                            byte[] xEncryted = otr.readData();
                            byte[] xEncryptedMac = otr.readMac();
                            return new SignatureMessage(protocolVersion, xEncryted, xEncryptedMac);
                        }
                    default:
                        throw new IOException("Illegal message type.");
                }
            case SerializationConstants.HEAD_MESSAGE:
                return new ErrorMessage(AbstractMessage.MESSAGE_ERROR, content);
            case SerializationConstants.HEAD_QUERY_V:
            case SerializationConstants.HEAD_QUERY_Q:
                List<Integer> versions = new Vector<Integer>();
                String versionString = null;
                if (SerializationConstants.HEAD_QUERY_Q == contentType) {
                    versions.add(1);
                    if (content.charAt(0) == 'v') {
                        versionString = content.substring(1, content.indexOf('?'));
                    }
                } else if (SerializationConstants.HEAD_QUERY_V == contentType) {
                    versionString = content.substring(0, content.indexOf('?'));
                }
                if (versionString != null) {
                    StringReader sr = new StringReader(versionString);
                    int c;
                    while ((c = sr.read()) != -1) if (!versions.contains(c))
                        versions.add(Integer.parseInt(String.valueOf((char) c)));
                }
                QueryMessage query = new QueryMessage(versions);
                return query;
            default:
                throw new IOException("Unknown message type.");
        }
    }
}
Also used : QueryMessage(net.java.otr4j.io.messages.QueryMessage) Matcher(java.util.regex.Matcher) DHPublicKey(javax.crypto.interfaces.DHPublicKey) RevealSignatureMessage(net.java.otr4j.io.messages.RevealSignatureMessage) SignatureMessage(net.java.otr4j.io.messages.SignatureMessage) RevealSignatureMessage(net.java.otr4j.io.messages.RevealSignatureMessage) IOException(java.io.IOException) BigInteger(java.math.BigInteger) DHCommitMessage(net.java.otr4j.io.messages.DHCommitMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) PlainTextMessage(net.java.otr4j.io.messages.PlainTextMessage) DataMessage(net.java.otr4j.io.messages.DataMessage) StringReader(java.io.StringReader) DHKeyMessage(net.java.otr4j.io.messages.DHKeyMessage) ErrorMessage(net.java.otr4j.io.messages.ErrorMessage) Vector(java.util.Vector)

Example 4 with PlainTextMessage

use of net.java.otr4j.io.messages.PlainTextMessage in project xabber-android by redsolution.

the class NextMamManager method parseMessage.

@Nullable
private MessageItem parseMessage(AccountItem accountItem, AccountJid account, UserJid user, Forwarded forwarded, String prevID) {
    if (!(forwarded.getForwardedStanza() instanceof Message)) {
        return null;
    }
    Message message = (Message) forwarded.getForwardedStanza();
    DelayInformation delayInformation = forwarded.getDelayInformation();
    DelayInformation messageDelay = DelayInformation.from(message);
    String body = message.getBody();
    net.java.otr4j.io.messages.AbstractMessage otrMessage;
    try {
        otrMessage = SerializationUtils.toMessage(body);
    } catch (IOException e) {
        return null;
    }
    boolean encrypted = false;
    if (otrMessage != null) {
        if (otrMessage.messageType != net.java.otr4j.io.messages.AbstractMessage.MESSAGE_PLAINTEXT) {
            encrypted = true;
            try {
                // this transforming just decrypt message if have keys. No action as injectMessage or something else
                body = OTRManager.getInstance().transformReceivingIfSessionExist(account, user, body);
                if (OTRManager.getInstance().isEncrypted(body)) {
                    return null;
                }
            } catch (Exception e) {
                return null;
            }
        } else
            body = ((PlainTextMessage) otrMessage).cleanText;
    }
    // forward comment (to support previous forwarded xep)
    String forwardComment = ForwardManager.parseForwardComment(message);
    if (forwardComment != null)
        body = forwardComment;
    // modify body with references
    Pair<String, String> bodies = ReferencesManager.modifyBodyWithReferences(message, body);
    body = bodies.first;
    String markupBody = bodies.second;
    boolean incoming = message.getFrom().asBareJid().equals(user.getJid().asBareJid());
    String uid = UUID.randomUUID().toString();
    MessageItem messageItem = new MessageItem(uid);
    messageItem.setPreviousId(prevID);
    String archivedId = ArchivedHelper.getArchivedId(forwarded.getForwardedStanza());
    if (archivedId != null)
        messageItem.setArchivedId(archivedId);
    long timestamp = delayInformation.getStamp().getTime();
    messageItem.setAccount(account);
    messageItem.setUser(user);
    messageItem.setResource(user.getJid().getResourceOrNull());
    messageItem.setText(body);
    if (markupBody != null)
        messageItem.setMarkupText(markupBody);
    messageItem.setTimestamp(timestamp);
    if (messageDelay != null) {
        messageItem.setDelayTimestamp(messageDelay.getStamp().getTime());
    }
    messageItem.setIncoming(incoming);
    messageItem.setStanzaId(AbstractChat.getStanzaId(message));
    messageItem.setPacketId(message.getStanzaId());
    messageItem.setReceivedFromMessageArchive(true);
    messageItem.setRead(timestamp <= accountItem.getStartHistoryTimestamp());
    messageItem.setSent(true);
    messageItem.setEncrypted(encrypted);
    // attachments
    FileManager.processFileMessage(messageItem);
    RealmList<Attachment> attachments = HttpFileUploadManager.parseFileMessage(message);
    if (attachments.size() > 0)
        messageItem.setAttachments(attachments);
    // forwarded
    messageItem.setOriginalStanza(message.toXML().toString());
    messageItem.setOriginalFrom(message.getFrom().toString());
    // groupchat
    RefUser groupchatUser = ReferencesManager.getGroupchatUserFromReferences(message);
    if (groupchatUser != null) {
        GroupchatUserManager.getInstance().saveGroupchatUser(groupchatUser, timestamp);
        messageItem.setGroupchatUserId(groupchatUser.getId());
    }
    return messageItem;
}
Also used : MessageItem(com.xabber.android.data.database.messagerealm.MessageItem) RefUser(com.xabber.android.data.extension.references.RefUser) PlainTextMessage(net.java.otr4j.io.messages.PlainTextMessage) Message(org.jivesoftware.smack.packet.Message) Attachment(com.xabber.android.data.database.messagerealm.Attachment) IOException(java.io.IOException) SmackException(org.jivesoftware.smack.SmackException) XMPPException(org.jivesoftware.smack.XMPPException) IOException(java.io.IOException) DelayInformation(org.jivesoftware.smackx.delay.packet.DelayInformation) PlainTextMessage(net.java.otr4j.io.messages.PlainTextMessage) Nullable(androidx.annotation.Nullable)

Aggregations

IOException (java.io.IOException)4 PlainTextMessage (net.java.otr4j.io.messages.PlainTextMessage)4 MessageItem (com.xabber.android.data.database.messagerealm.MessageItem)2 DHCommitMessage (net.java.otr4j.io.messages.DHCommitMessage)2 DHKeyMessage (net.java.otr4j.io.messages.DHKeyMessage)2 DataMessage (net.java.otr4j.io.messages.DataMessage)2 ErrorMessage (net.java.otr4j.io.messages.ErrorMessage)2 QueryMessage (net.java.otr4j.io.messages.QueryMessage)2 RevealSignatureMessage (net.java.otr4j.io.messages.RevealSignatureMessage)2 SignatureMessage (net.java.otr4j.io.messages.SignatureMessage)2 SmackException (org.jivesoftware.smack.SmackException)2 XMPPException (org.jivesoftware.smack.XMPPException)2 Message (org.jivesoftware.smack.packet.Message)2 DelayInformation (org.jivesoftware.smackx.delay.packet.DelayInformation)2 Nullable (androidx.annotation.Nullable)1 Attachment (com.xabber.android.data.database.messagerealm.Attachment)1 RefUser (com.xabber.android.data.extension.references.RefUser)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 StringReader (java.io.StringReader)1