Search in sources :

Example 41 with IqPacket

use of eu.siacs.conversations.xmpp.stanzas.IqPacket in project Conversations by siacs.

the class MessageArchiveService method execute.

private void execute(final Query query) {
    final Account account = query.getAccount();
    if (account.getStatus() == Account.State.ONLINE) {
        Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": running mam query " + query.toString());
        IqPacket packet = this.mXmppConnectionService.getIqGenerator().queryMessageArchiveManagement(query);
        this.mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() {

            @Override
            public void onIqPacketReceived(Account account, IqPacket packet) {
                Element fin = packet.findChild("fin", Namespace.MAM);
                if (packet.getType() == IqPacket.TYPE.TIMEOUT) {
                    synchronized (MessageArchiveService.this.queries) {
                        MessageArchiveService.this.queries.remove(query);
                        if (query.hasCallback()) {
                            query.callback(false);
                        }
                    }
                } else if (packet.getType() == IqPacket.TYPE.RESULT && fin != null) {
                    processFin(fin);
                } else if (packet.getType() == IqPacket.TYPE.RESULT && query.isLegacy()) {
                //do nothing
                } else {
                    Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": error executing mam: " + packet.toString());
                    finalizeQuery(query, true);
                }
            }
        });
    } else {
        synchronized (this.pendingQueries) {
            this.pendingQueries.add(query);
        }
    }
}
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 42 with IqPacket

use of eu.siacs.conversations.xmpp.stanzas.IqPacket in project Conversations by siacs.

the class IqParser method onIqPacketReceived.

@Override
public void onIqPacketReceived(final Account account, final IqPacket packet) {
    final boolean isGet = packet.getType() == IqPacket.TYPE.GET;
    if (packet.getType() == IqPacket.TYPE.ERROR || packet.getType() == IqPacket.TYPE.TIMEOUT) {
        return;
    } else if (packet.hasChild("query", Namespace.ROSTER) && packet.fromServer(account)) {
        final Element query = packet.findChild("query");
        // If this is in response to a query for the whole roster:
        if (packet.getType() == IqPacket.TYPE.RESULT) {
            account.getRoster().markAllAsNotInRoster();
        }
        this.rosterItems(account, query);
    } else if ((packet.hasChild("block", Namespace.BLOCKING) || packet.hasChild("blocklist", Namespace.BLOCKING)) && packet.fromServer(account)) {
        // Block list or block push.
        Log.d(Config.LOGTAG, "Received blocklist update from server");
        final Element blocklist = packet.findChild("blocklist", Namespace.BLOCKING);
        final Element block = packet.findChild("block", Namespace.BLOCKING);
        final Collection<Element> items = blocklist != null ? blocklist.getChildren() : (block != null ? block.getChildren() : null);
        // Otherwise, just update the existing blocklist.
        if (packet.getType() == IqPacket.TYPE.RESULT) {
            account.clearBlocklist();
            account.getXmppConnection().getFeatures().setBlockListRequested(true);
        }
        if (items != null) {
            final Collection<Jid> jids = new ArrayList<>(items.size());
            // Create a collection of Jids from the packet
            for (final Element item : items) {
                if (item.getName().equals("item")) {
                    final Jid jid = item.getAttributeAsJid("jid");
                    if (jid != null) {
                        jids.add(jid);
                    }
                }
            }
            account.getBlocklist().addAll(jids);
            if (packet.getType() == IqPacket.TYPE.SET) {
                boolean removed = false;
                for (Jid jid : jids) {
                    removed |= mXmppConnectionService.removeBlockedConversations(account, jid);
                }
                if (removed) {
                    mXmppConnectionService.updateConversationUi();
                }
            }
        }
        // Update the UI
        mXmppConnectionService.updateBlocklistUi(OnUpdateBlocklist.Status.BLOCKED);
        if (packet.getType() == IqPacket.TYPE.SET) {
            final IqPacket response = packet.generateResponse(IqPacket.TYPE.RESULT);
            mXmppConnectionService.sendIqPacket(account, response, null);
        }
    } else if (packet.hasChild("unblock", Namespace.BLOCKING) && packet.fromServer(account) && packet.getType() == IqPacket.TYPE.SET) {
        Log.d(Config.LOGTAG, "Received unblock update from server");
        final Collection<Element> items = packet.findChild("unblock", Namespace.BLOCKING).getChildren();
        if (items.size() == 0) {
            // No children to unblock == unblock all
            account.getBlocklist().clear();
        } else {
            final Collection<Jid> jids = new ArrayList<>(items.size());
            for (final Element item : items) {
                if (item.getName().equals("item")) {
                    final Jid jid = item.getAttributeAsJid("jid");
                    if (jid != null) {
                        jids.add(jid);
                    }
                }
            }
            account.getBlocklist().removeAll(jids);
        }
        mXmppConnectionService.updateBlocklistUi(OnUpdateBlocklist.Status.UNBLOCKED);
        final IqPacket response = packet.generateResponse(IqPacket.TYPE.RESULT);
        mXmppConnectionService.sendIqPacket(account, response, null);
    } else if (packet.hasChild("open", "http://jabber.org/protocol/ibb") || packet.hasChild("data", "http://jabber.org/protocol/ibb") || packet.hasChild("close", "http://jabber.org/protocol/ibb")) {
        mXmppConnectionService.getJingleConnectionManager().deliverIbbPacket(account, packet);
    } else if (packet.hasChild("query", "http://jabber.org/protocol/disco#info")) {
        final IqPacket response = mXmppConnectionService.getIqGenerator().discoResponse(packet);
        mXmppConnectionService.sendIqPacket(account, response, null);
    } else if (packet.hasChild("query", "jabber:iq:version") && isGet) {
        final IqPacket response = mXmppConnectionService.getIqGenerator().versionResponse(packet);
        mXmppConnectionService.sendIqPacket(account, response, null);
    } else if (packet.hasChild("ping", "urn:xmpp:ping") && isGet) {
        final IqPacket response = packet.generateResponse(IqPacket.TYPE.RESULT);
        mXmppConnectionService.sendIqPacket(account, response, null);
    } else if (packet.hasChild("time", "urn:xmpp:time") && isGet) {
        final IqPacket response;
        if (mXmppConnectionService.useTorToConnect()) {
            response = packet.generateResponse(IqPacket.TYPE.ERROR);
            final Element error = response.addChild("error");
            error.setAttribute("type", "cancel");
            error.addChild("not-allowed", "urn:ietf:params:xml:ns:xmpp-stanzas");
        } else {
            response = mXmppConnectionService.getIqGenerator().entityTimeResponse(packet);
        }
        mXmppConnectionService.sendIqPacket(account, response, null);
    } else {
        if (packet.getType() == IqPacket.TYPE.GET || packet.getType() == IqPacket.TYPE.SET) {
            final IqPacket response = packet.generateResponse(IqPacket.TYPE.ERROR);
            final Element error = response.addChild("error");
            error.setAttribute("type", "cancel");
            error.addChild("feature-not-implemented", "urn:ietf:params:xml:ns:xmpp-stanzas");
            account.getXmppConnection().sendIqPacket(response, null);
        }
    }
}
Also used : Jid(eu.siacs.conversations.xmpp.jid.Jid) Element(eu.siacs.conversations.xml.Element) ArrayList(java.util.ArrayList) Collection(java.util.Collection) IqPacket(eu.siacs.conversations.xmpp.stanzas.IqPacket)

Example 43 with IqPacket

use of eu.siacs.conversations.xmpp.stanzas.IqPacket in project Conversations by siacs.

the class XmppConnectionService method changeAffiliationsInConference.

public void changeAffiliationsInConference(final Conversation conference, MucOptions.Affiliation before, MucOptions.Affiliation after) {
    List<Jid> jids = new ArrayList<>();
    for (MucOptions.User user : conference.getMucOptions().getUsers()) {
        if (user.getAffiliation() == before && user.getRealJid() != null) {
            jids.add(user.getRealJid());
        }
    }
    IqPacket request = this.mIqGenerator.changeAffiliation(conference, jids, after.toString());
    sendIqPacket(conference.getAccount(), request, mDefaultIqHandler);
}
Also used : MucOptions(eu.siacs.conversations.entities.MucOptions) Jid(eu.siacs.conversations.xmpp.jid.Jid) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) IqPacket(eu.siacs.conversations.xmpp.stanzas.IqPacket)

Example 44 with IqPacket

use of eu.siacs.conversations.xmpp.stanzas.IqPacket in project Conversations by siacs.

the class XmppConnectionService method updateAccountPasswordOnServer.

public void updateAccountPasswordOnServer(final Account account, final String newPassword, final OnAccountPasswordChanged callback) {
    final IqPacket iq = getIqGenerator().generateSetPassword(account, newPassword);
    sendIqPacket(account, iq, new OnIqPacketReceived() {

        @Override
        public void onIqPacketReceived(final Account account, final IqPacket packet) {
            if (packet.getType() == IqPacket.TYPE.RESULT) {
                account.setPassword(newPassword);
                account.setOption(Account.OPTION_MAGIC_CREATE, false);
                databaseBackend.updateAccount(account);
                callback.onPasswordChangeSucceeded();
            } else {
                callback.onPasswordChangeFailed();
            }
        }
    });
}
Also used : Account(eu.siacs.conversations.entities.Account) OnIqPacketReceived(eu.siacs.conversations.xmpp.OnIqPacketReceived) IqPacket(eu.siacs.conversations.xmpp.stanzas.IqPacket)

Example 45 with IqPacket

use of eu.siacs.conversations.xmpp.stanzas.IqPacket in project Conversations by siacs.

the class XmppConnectionService method fetchConferenceMembers.

private void fetchConferenceMembers(final Conversation conversation) {
    final Account account = conversation.getAccount();
    final String[] affiliations = { "member", "admin", "owner" };
    OnIqPacketReceived callback = new OnIqPacketReceived() {

        private int i = 0;

        private boolean success = true;

        @Override
        public void onIqPacketReceived(Account account, IqPacket packet) {
            Element query = packet.query("http://jabber.org/protocol/muc#admin");
            if (packet.getType() == IqPacket.TYPE.RESULT && query != null) {
                for (Element child : query.getChildren()) {
                    if ("item".equals(child.getName())) {
                        MucOptions.User user = AbstractParser.parseItem(conversation, child);
                        if (!user.realJidMatchesAccount()) {
                            conversation.getMucOptions().updateUser(user);
                        }
                    }
                }
            } else {
                success = false;
                Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": could not request affiliation " + affiliations[i] + " in " + conversation.getJid().toBareJid());
            }
            ++i;
            if (i >= affiliations.length) {
                List<Jid> members = conversation.getMucOptions().getMembers();
                if (success) {
                    List<Jid> cryptoTargets = conversation.getAcceptedCryptoTargets();
                    boolean changed = false;
                    for (ListIterator<Jid> iterator = cryptoTargets.listIterator(); iterator.hasNext(); ) {
                        Jid jid = iterator.next();
                        if (!members.contains(jid)) {
                            iterator.remove();
                            Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": removed " + jid + " from crypto targets of " + conversation.getName());
                            changed = true;
                        }
                    }
                    if (changed) {
                        conversation.setAcceptedCryptoTargets(cryptoTargets);
                        updateConversation(conversation);
                    }
                }
                Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": retrieved members for " + conversation.getJid().toBareJid() + ": " + conversation.getMucOptions().getMembers());
                getAvatarService().clear(conversation);
                updateMucRosterUi();
                updateConversationUi();
            }
        }
    };
    for (String affiliation : affiliations) {
        sendIqPacket(account, mIqGenerator.queryAffiliation(conversation, affiliation), callback);
    }
    Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": fetching members for " + conversation.getName());
}
Also used : Account(eu.siacs.conversations.entities.Account) MucOptions(eu.siacs.conversations.entities.MucOptions) OnIqPacketReceived(eu.siacs.conversations.xmpp.OnIqPacketReceived) Jid(eu.siacs.conversations.xmpp.jid.Jid) Element(eu.siacs.conversations.xml.Element) IqPacket(eu.siacs.conversations.xmpp.stanzas.IqPacket)

Aggregations

IqPacket (eu.siacs.conversations.xmpp.stanzas.IqPacket)78 Element (eu.siacs.conversations.xml.Element)43 Account (eu.siacs.conversations.entities.Account)41 OnIqPacketReceived (eu.siacs.conversations.xmpp.OnIqPacketReceived)33 Jid (eu.siacs.conversations.xmpp.jid.Jid)14 Data (eu.siacs.conversations.xmpp.forms.Data)6 InvalidJidException (eu.siacs.conversations.xmpp.jid.InvalidJidException)6 ArrayList (java.util.ArrayList)6 JinglePacket (eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket)4 HashSet (java.util.HashSet)4 InvalidKeyException (org.whispersystems.libaxolotl.InvalidKeyException)4 PreKeyBundle (org.whispersystems.libaxolotl.state.PreKeyBundle)4 Pair (android.util.Pair)3 Conversation (eu.siacs.conversations.entities.Conversation)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 InvalidKeyIdException (org.whispersystems.libaxolotl.InvalidKeyIdException)3 Bundle (android.os.Bundle)2 Bookmark (eu.siacs.conversations.entities.Bookmark)2 Contact (eu.siacs.conversations.entities.Contact)2