Search in sources :

Example 6 with Element

use of eu.siacs.conversations.xml.Element in project Conversations by siacs.

the class XmppConnectionService method fetchAvatarPep.

private void fetchAvatarPep(Account account, final Avatar avatar, final UiCallback<Avatar> callback) {
    IqPacket packet = this.mIqGenerator.retrievePepAvatar(avatar);
    sendIqPacket(account, packet, new OnIqPacketReceived() {

        @Override
        public void onIqPacketReceived(Account account, IqPacket result) {
            synchronized (mInProgressAvatarFetches) {
                mInProgressAvatarFetches.remove(generateFetchKey(account, avatar));
            }
            final String ERROR = account.getJid().toBareJid() + ": fetching avatar for " + avatar.owner + " failed ";
            if (result.getType() == IqPacket.TYPE.RESULT) {
                avatar.image = mIqParser.avatarData(result);
                if (avatar.image != null) {
                    if (getFileBackend().save(avatar)) {
                        if (account.getJid().toBareJid().equals(avatar.owner)) {
                            if (account.setAvatar(avatar.getFilename())) {
                                databaseBackend.updateAccount(account);
                            }
                            getAvatarService().clear(account);
                            updateConversationUi();
                            updateAccountUi();
                        } else {
                            Contact contact = account.getRoster().getContact(avatar.owner);
                            contact.setAvatar(avatar);
                            getAvatarService().clear(contact);
                            updateConversationUi();
                            updateRosterUi();
                        }
                        if (callback != null) {
                            callback.success(avatar);
                        }
                        Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": successfully fetched pep avatar for " + avatar.owner);
                        return;
                    }
                } else {
                    Log.d(Config.LOGTAG, ERROR + "(parsing error)");
                }
            } else {
                Element error = result.findChild("error");
                if (error == null) {
                    Log.d(Config.LOGTAG, ERROR + "(server error)");
                } else {
                    Log.d(Config.LOGTAG, ERROR + error.toString());
                }
            }
            if (callback != null) {
                callback.error(0, null);
            }
        }
    });
}
Also used : Account(eu.siacs.conversations.entities.Account) OnIqPacketReceived(eu.siacs.conversations.xmpp.OnIqPacketReceived) Element(eu.siacs.conversations.xml.Element) IqPacket(eu.siacs.conversations.xmpp.stanzas.IqPacket) Contact(eu.siacs.conversations.entities.Contact)

Example 7 with Element

use of eu.siacs.conversations.xml.Element in project Conversations by siacs.

the class XmppConnectionService method joinMuc.

private void joinMuc(Conversation conversation, final OnConferenceJoined onConferenceJoined, final boolean followedInvite) {
    Account account = conversation.getAccount();
    account.pendingConferenceJoins.remove(conversation);
    account.pendingConferenceLeaves.remove(conversation);
    if (account.getStatus() == Account.State.ONLINE) {
        conversation.resetMucOptions();
        if (onConferenceJoined != null) {
            conversation.getMucOptions().flagNoAutoPushConfiguration();
        }
        conversation.setHasMessagesLeftOnServer(false);
        fetchConferenceConfiguration(conversation, new OnConferenceConfigurationFetched() {

            private void join(Conversation conversation) {
                Account account = conversation.getAccount();
                final MucOptions mucOptions = conversation.getMucOptions();
                final Jid joinJid = mucOptions.getSelf().getFullJid();
                Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": joining conversation " + joinJid.toString());
                PresencePacket packet = mPresenceGenerator.selfPresence(account, Presence.Status.ONLINE, mucOptions.nonanonymous());
                packet.setTo(joinJid);
                Element x = packet.addChild("x", "http://jabber.org/protocol/muc");
                if (conversation.getMucOptions().getPassword() != null) {
                    x.addChild("password").setContent(mucOptions.getPassword());
                }
                if (mucOptions.mamSupport()) {
                    // Use MAM instead of the limited muc history to get history
                    x.addChild("history").setAttribute("maxchars", "0");
                } else {
                    // Fallback to muc history
                    x.addChild("history").setAttribute("since", PresenceGenerator.getTimestamp(conversation.getLastMessageTransmitted()));
                }
                sendPresencePacket(account, packet);
                if (onConferenceJoined != null) {
                    onConferenceJoined.onConferenceJoined(conversation);
                }
                if (!joinJid.equals(conversation.getJid())) {
                    conversation.setContactJid(joinJid);
                    databaseBackend.updateConversation(conversation);
                }
                if (mucOptions.mamSupport()) {
                    getMessageArchiveService().catchupMUC(conversation);
                }
                if (mucOptions.membersOnly() && mucOptions.nonanonymous()) {
                    fetchConferenceMembers(conversation);
                    if (followedInvite && conversation.getBookmark() == null) {
                        saveConversationAsBookmark(conversation, null);
                    }
                }
                sendUnsentMessages(conversation);
            }

            @Override
            public void onConferenceConfigurationFetched(Conversation conversation) {
                join(conversation);
            }

            @Override
            public void onFetchFailed(final Conversation conversation, Element error) {
                if (error != null && "remote-server-not-found".equals(error.getName())) {
                    conversation.getMucOptions().setError(MucOptions.Error.SERVER_NOT_FOUND);
                } else {
                    join(conversation);
                    fetchConferenceConfiguration(conversation);
                }
            }
        });
        updateConversationUi();
    } else {
        account.pendingConferenceJoins.add(conversation);
        conversation.resetMucOptions();
        conversation.setHasMessagesLeftOnServer(false);
        updateConversationUi();
    }
}
Also used : Account(eu.siacs.conversations.entities.Account) MucOptions(eu.siacs.conversations.entities.MucOptions) Jid(eu.siacs.conversations.xmpp.jid.Jid) Element(eu.siacs.conversations.xml.Element) Conversation(eu.siacs.conversations.entities.Conversation) PresencePacket(eu.siacs.conversations.xmpp.stanzas.PresencePacket)

Example 8 with Element

use of eu.siacs.conversations.xml.Element in project Conversations by siacs.

the class XmppConnectionService method deleteContactOnServer.

public void deleteContactOnServer(Contact contact) {
    contact.resetOption(Contact.Options.PREEMPTIVE_GRANT);
    contact.resetOption(Contact.Options.DIRTY_PUSH);
    contact.setOption(Contact.Options.DIRTY_DELETE);
    Account account = contact.getAccount();
    if (account.getStatus() == Account.State.ONLINE) {
        IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
        Element item = iq.query(Namespace.ROSTER).addChild("item");
        item.setAttribute("jid", contact.getJid().toString());
        item.setAttribute("subscription", "remove");
        account.getXmppConnection().sendIqPacket(iq, mDefaultIqHandler);
    }
}
Also used : Account(eu.siacs.conversations.entities.Account) Element(eu.siacs.conversations.xml.Element) IqPacket(eu.siacs.conversations.xmpp.stanzas.IqPacket)

Example 9 with Element

use of eu.siacs.conversations.xml.Element in project Conversations by siacs.

the class XmppConnectionService method fetchMamPreferences.

public void fetchMamPreferences(Account account, final OnMamPreferencesFetched callback) {
    final boolean legacy = account.getXmppConnection().getFeatures().mamLegacy();
    IqPacket request = new IqPacket(IqPacket.TYPE.GET);
    request.addChild("prefs", legacy ? Namespace.MAM_LEGACY : Namespace.MAM);
    sendIqPacket(account, request, new OnIqPacketReceived() {

        @Override
        public void onIqPacketReceived(Account account, IqPacket packet) {
            Element prefs = packet.findChild("prefs", legacy ? Namespace.MAM_LEGACY : Namespace.MAM);
            if (packet.getType() == IqPacket.TYPE.RESULT && prefs != null) {
                callback.onPreferencesFetched(prefs);
            } else {
                callback.onPreferencesFetchFailed();
            }
        }
    });
}
Also used : Account(eu.siacs.conversations.entities.Account) OnIqPacketReceived(eu.siacs.conversations.xmpp.OnIqPacketReceived) Element(eu.siacs.conversations.xml.Element) IqPacket(eu.siacs.conversations.xmpp.stanzas.IqPacket)

Example 10 with Element

use of eu.siacs.conversations.xml.Element in project Conversations by siacs.

the class IqParser method verification.

public Pair<X509Certificate[], byte[]> verification(final IqPacket packet) {
    Element item = getItem(packet);
    Element verification = item != null ? item.findChild("verification", AxolotlService.PEP_PREFIX) : null;
    Element chain = verification != null ? verification.findChild("chain") : null;
    Element signature = verification != null ? verification.findChild("signature") : null;
    if (chain != null && signature != null) {
        List<Element> certElements = chain.getChildren();
        X509Certificate[] certificates = new X509Certificate[certElements.size()];
        try {
            CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
            int i = 0;
            for (Element cert : certElements) {
                certificates[i] = (X509Certificate) certificateFactory.generateCertificate(new ByteArrayInputStream(Base64.decode(cert.getContent(), Base64.DEFAULT)));
                ++i;
            }
            return new Pair<>(certificates, Base64.decode(signature.getContent(), Base64.DEFAULT));
        } catch (CertificateException e) {
            return null;
        }
    } else {
        return null;
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Element(eu.siacs.conversations.xml.Element) CertificateException(java.security.cert.CertificateException) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate) Pair(android.util.Pair)

Aggregations

Element (eu.siacs.conversations.xml.Element)93 IqPacket (eu.siacs.conversations.xmpp.stanzas.IqPacket)43 Account (eu.siacs.conversations.entities.Account)21 Jid (eu.siacs.conversations.xmpp.jid.Jid)17 OnIqPacketReceived (eu.siacs.conversations.xmpp.OnIqPacketReceived)16 MessagePacket (eu.siacs.conversations.xmpp.stanzas.MessagePacket)8 Contact (eu.siacs.conversations.entities.Contact)7 Conversation (eu.siacs.conversations.entities.Conversation)6 ArrayList (java.util.ArrayList)6 Data (eu.siacs.conversations.xmpp.forms.Data)5 Avatar (eu.siacs.conversations.xmpp.pep.Avatar)5 IOException (java.io.IOException)5 MucOptions (eu.siacs.conversations.entities.MucOptions)4 PresencePacket (eu.siacs.conversations.xmpp.stanzas.PresencePacket)4 Pair (android.util.Pair)3 Bookmark (eu.siacs.conversations.entities.Bookmark)3 Message (eu.siacs.conversations.entities.Message)3 InvalidJidException (eu.siacs.conversations.xmpp.jid.InvalidJidException)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3