Search in sources :

Example 1 with PgpEngine

use of de.pixart.messenger.crypto.PgpEngine in project Pix-Art-Messenger by kriztan.

the class XmppConnectionService method attachImageToConversation.

public void attachImageToConversation(final Conversation conversation, final Uri uri, final UiCallback<Message> callback) {
    if (FileBackend.weOwnFile(this, uri)) {
        Log.d(Config.LOGTAG, "trying to attach file that belonged to us");
        callback.error(R.string.security_error_invalid_file_access, null);
        return;
    }
    final String mimeType = MimeUtils.guessMimeTypeFromUri(this, uri);
    final String compressPictures = getCompressPicturesPreference();
    if ("never".equals(compressPictures) || ("auto".equals(compressPictures) && getFileBackend().useImageAsIs(uri)) || (mimeType != null && mimeType.endsWith("/gif"))) {
        Log.d(Config.LOGTAG, conversation.getAccount().getJid().toBareJid() + ": not compressing picture. sending as file");
        attachFileToConversation(conversation, uri, mimeType, callback);
        return;
    }
    final Message message;
    if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) {
        message = new Message(conversation, "", Message.ENCRYPTION_DECRYPTED);
    } else {
        message = new Message(conversation, "", conversation.getNextEncryption());
    }
    message.setCounterpart(conversation.getNextCounterpart());
    message.setType(Message.TYPE_IMAGE);
    mFileAddingExecutor.execute(new Runnable() {

        @Override
        public void run() {
            try {
                getFileBackend().copyImageToPrivateStorage(message, uri);
                if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) {
                    final PgpEngine pgpEngine = getPgpEngine();
                    if (pgpEngine != null) {
                        pgpEngine.encrypt(message, callback);
                    } else if (callback != null) {
                        callback.error(R.string.unable_to_connect_to_keychain, null);
                    }
                } else {
                    sendMessage(message);
                    callback.success(message);
                }
            } catch (final FileBackend.FileCopyException e) {
                callback.error(e.getResId(), message);
            }
        }
    });
}
Also used : XmppAxolotlMessage(de.pixart.messenger.crypto.axolotl.XmppAxolotlMessage) Message(de.pixart.messenger.entities.Message) PgpEngine(de.pixart.messenger.crypto.PgpEngine)

Example 2 with PgpEngine

use of de.pixart.messenger.crypto.PgpEngine in project Pix-Art-Messenger by kriztan.

the class AttachFileToConversationRunnable method processAsFile.

private void processAsFile() {
    final String path = mXmppConnectionService.getFileBackend().getOriginalPath(uri);
    if (path != null && !FileBackend.isPathBlacklisted(path)) {
        message.setRelativeFilePath(path);
        mXmppConnectionService.getFileBackend().updateFileParams(message);
        if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
            mXmppConnectionService.getPgpEngine().encrypt(message, callback);
        } else {
            mXmppConnectionService.sendMessage(message);
            callback.success(message);
        }
    } else {
        try {
            mXmppConnectionService.getFileBackend().copyFileToPrivateStorage(message, uri, type);
            mXmppConnectionService.getFileBackend().updateFileParams(message);
            if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
                final PgpEngine pgpEngine = mXmppConnectionService.getPgpEngine();
                if (pgpEngine != null) {
                    pgpEngine.encrypt(message, callback);
                } else if (callback != null) {
                    callback.error(R.string.unable_to_connect_to_keychain, null);
                }
            } else {
                mXmppConnectionService.sendMessage(message);
                callback.success(message);
            }
        } catch (FileBackend.FileCopyException e) {
            callback.error(e.getResId(), message);
        }
    }
}
Also used : PgpEngine(de.pixart.messenger.crypto.PgpEngine) FileBackend(de.pixart.messenger.persistance.FileBackend)

Example 3 with PgpEngine

use of de.pixart.messenger.crypto.PgpEngine in project Pix-Art-Messenger by kriztan.

the class PresenceParser method parseContactPresence.

public void parseContactPresence(final PresencePacket packet, final Account account) {
    final PresenceGenerator mPresenceGenerator = mXmppConnectionService.getPresenceGenerator();
    final Jid from = packet.getFrom();
    if (from == null || from.equals(account.getJid())) {
        return;
    }
    final String type = packet.getAttribute("type");
    final Contact contact = account.getRoster().getContact(from);
    if (type == null) {
        final String resource = from.isBareJid() ? "" : from.getResourcepart();
        if (contact.setPresenceName(packet.findChildContent("nick", Namespace.NICK))) {
            mXmppConnectionService.getAvatarService().clear(contact);
        }
        Avatar avatar = Avatar.parsePresence(packet.findChild("x", "vcard-temp:x:update"));
        if (avatar != null && (!contact.isSelf() || account.getAvatar() == null)) {
            avatar.owner = from.toBareJid();
            if (mXmppConnectionService.getFileBackend().isAvatarCached(avatar)) {
                if (avatar.owner.equals(account.getJid().toBareJid())) {
                    account.setAvatar(avatar.getFilename());
                    mXmppConnectionService.databaseBackend.updateAccount(account);
                    mXmppConnectionService.getAvatarService().clear(account);
                    mXmppConnectionService.updateConversationUi();
                    mXmppConnectionService.updateAccountUi();
                } else if (contact.setAvatar(avatar)) {
                    mXmppConnectionService.getAvatarService().clear(contact);
                    mXmppConnectionService.updateConversationUi();
                    mXmppConnectionService.updateRosterUi();
                }
            } else if (mXmppConnectionService.isDataSaverDisabled()) {
                mXmppConnectionService.fetchAvatar(account, avatar);
            }
        }
        int sizeBefore = contact.getPresences().size();
        final String show = packet.findChildContent("show");
        final Element caps = packet.findChild("c", "http://jabber.org/protocol/caps");
        final String message = packet.findChildContent("status");
        final Presence presence = Presence.parse(show, caps, message);
        contact.updatePresence(resource, presence);
        if (presence.hasCaps()) {
            mXmppConnectionService.fetchCaps(account, from, presence);
        }
        final Element idle = packet.findChild("idle", Namespace.IDLE);
        if (idle != null) {
            try {
                final String since = idle.getAttribute("since");
                contact.setLastseen(AbstractParser.parseTimestamp(since));
                contact.flagInactive();
            } catch (Throwable throwable) {
                if (contact.setLastseen(AbstractParser.parseTimestamp(packet))) {
                    contact.flagActive();
                }
            }
        } else {
            if (contact.setLastseen(AbstractParser.parseTimestamp(packet))) {
                contact.flagActive();
            }
        }
        PgpEngine pgp = mXmppConnectionService.getPgpEngine();
        Element x = packet.findChild("x", "jabber:x:signed");
        if (pgp != null && x != null) {
            Element status = packet.findChild("status");
            String msg = status != null ? status.getContent() : "";
            contact.setPgpKeyId(pgp.fetchKeyId(account, msg, x.getContent()));
        }
        boolean online = sizeBefore < contact.getPresences().size();
        mXmppConnectionService.onContactStatusChanged.onContactStatusChanged(contact, online);
    } else if (type.equals("unavailable")) {
        if (contact.setLastseen(AbstractParser.parseTimestamp(packet, 0L, true))) {
            contact.flagInactive();
        }
        if (from.isBareJid()) {
            contact.clearPresences();
        } else {
            contact.removePresence(from.getResourcepart());
        }
        mXmppConnectionService.onContactStatusChanged.onContactStatusChanged(contact, false);
    } else if (type.equals("subscribe")) {
        if (contact.getOption(Contact.Options.PREEMPTIVE_GRANT)) {
            mXmppConnectionService.sendPresencePacket(account, mPresenceGenerator.sendPresenceUpdatesTo(contact));
        } else {
            contact.setOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST);
            final Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, contact.getJid().toBareJid(), false, false);
            final String statusMessage = packet.findChildContent("status");
            if (statusMessage != null && !statusMessage.isEmpty() && conversation.countMessages() == 0) {
                conversation.add(new Message(conversation, statusMessage, Message.ENCRYPTION_NONE, Message.STATUS_RECEIVED));
            }
        }
    }
    mXmppConnectionService.updateRosterUi();
}
Also used : PresenceGenerator(de.pixart.messenger.generator.PresenceGenerator) Jid(de.pixart.messenger.xmpp.jid.Jid) Message(de.pixart.messenger.entities.Message) Element(de.pixart.messenger.xml.Element) PgpEngine(de.pixart.messenger.crypto.PgpEngine) Conversation(de.pixart.messenger.entities.Conversation) Avatar(de.pixart.messenger.xmpp.pep.Avatar) Contact(de.pixart.messenger.entities.Contact) Presence(de.pixart.messenger.entities.Presence)

Aggregations

PgpEngine (de.pixart.messenger.crypto.PgpEngine)3 Message (de.pixart.messenger.entities.Message)2 XmppAxolotlMessage (de.pixart.messenger.crypto.axolotl.XmppAxolotlMessage)1 Contact (de.pixart.messenger.entities.Contact)1 Conversation (de.pixart.messenger.entities.Conversation)1 Presence (de.pixart.messenger.entities.Presence)1 PresenceGenerator (de.pixart.messenger.generator.PresenceGenerator)1 FileBackend (de.pixart.messenger.persistance.FileBackend)1 Element (de.pixart.messenger.xml.Element)1 Jid (de.pixart.messenger.xmpp.jid.Jid)1 Avatar (de.pixart.messenger.xmpp.pep.Avatar)1