Search in sources :

Example 1 with PresenceGenerator

use of eu.siacs.conversations.generator.PresenceGenerator in project Conversations by siacs.

the class PresenceParser method parseContactPresence.

private 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.getResource();
        Avatar avatar = Avatar.parsePresence(packet.findChild("x", "vcard-temp:x:update"));
        if (avatar != null && (!contact.isSelf() || account.getAvatar() == null)) {
            avatar.owner = from.asBareJid();
            if (mXmppConnectionService.getFileBackend().isAvatarCached(avatar)) {
                if (avatar.owner.equals(account.getJid().asBareJid())) {
                    account.setAvatar(avatar.getFilename());
                    mXmppConnectionService.databaseBackend.updateAccount(account);
                    mXmppConnectionService.getAvatarService().clear(account);
                    mXmppConnectionService.updateConversationUi();
                    mXmppConnectionService.updateAccountUi();
                } else {
                    contact.setAvatar(avatar);
                    mXmppConnectionService.syncRoster(account);
                    mXmppConnectionService.getAvatarService().clear(contact);
                    mXmppConnectionService.updateConversationUi();
                    mXmppConnectionService.updateRosterUi();
                }
            } else if (mXmppConnectionService.isDataSaverDisabled()) {
                mXmppConnectionService.fetchAvatar(account, avatar);
            }
        }
        if (mXmppConnectionService.isMuc(account, from)) {
            return;
        }
        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) {
            final String status = packet.findChildContent("status");
            final long keyId = pgp.fetchKeyId(account, status, x.getContent());
            if (keyId != 0 && contact.setPgpKeyId(keyId)) {
                Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": found OpenPGP key id for " + contact.getJid() + " " + OpenPgpUtils.convertKeyIdToHex(keyId));
                mXmppConnectionService.syncRoster(account);
            }
        }
        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.getResource());
        }
        if (contact.getShownStatus() == Presence.Status.OFFLINE) {
            contact.flagInactive();
        }
        mXmppConnectionService.onContactStatusChanged.onContactStatusChanged(contact, false);
    } else if (type.equals("subscribe")) {
        if (contact.setPresenceName(packet.findChildContent("nick", Namespace.NICK))) {
            mXmppConnectionService.syncRoster(account);
            mXmppConnectionService.getAvatarService().clear(contact);
        }
        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().asBareJid(), 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(eu.siacs.conversations.generator.PresenceGenerator) InvalidJid(eu.siacs.conversations.xmpp.InvalidJid) Jid(eu.siacs.conversations.xmpp.Jid) Message(eu.siacs.conversations.entities.Message) Element(eu.siacs.conversations.xml.Element) PgpEngine(eu.siacs.conversations.crypto.PgpEngine) Conversation(eu.siacs.conversations.entities.Conversation) Avatar(eu.siacs.conversations.xmpp.pep.Avatar) Contact(eu.siacs.conversations.entities.Contact) Presence(eu.siacs.conversations.entities.Presence)

Aggregations

PgpEngine (eu.siacs.conversations.crypto.PgpEngine)1 Contact (eu.siacs.conversations.entities.Contact)1 Conversation (eu.siacs.conversations.entities.Conversation)1 Message (eu.siacs.conversations.entities.Message)1 Presence (eu.siacs.conversations.entities.Presence)1 PresenceGenerator (eu.siacs.conversations.generator.PresenceGenerator)1 Element (eu.siacs.conversations.xml.Element)1 InvalidJid (eu.siacs.conversations.xmpp.InvalidJid)1 Jid (eu.siacs.conversations.xmpp.Jid)1 Avatar (eu.siacs.conversations.xmpp.pep.Avatar)1