Search in sources :

Example 71 with IqPacket

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

the class XmppConnection method clearIqCallbacks.

private void clearIqCallbacks() {
    final IqPacket failurePacket = new IqPacket(IqPacket.TYPE.TIMEOUT);
    final ArrayList<OnIqPacketReceived> callbacks = new ArrayList<>();
    synchronized (this.packetCallbacks) {
        if (this.packetCallbacks.size() == 0) {
            return;
        }
        Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": clearing " + this.packetCallbacks.size() + " iq callbacks");
        final Iterator<Pair<IqPacket, OnIqPacketReceived>> iterator = this.packetCallbacks.values().iterator();
        while (iterator.hasNext()) {
            Pair<IqPacket, OnIqPacketReceived> entry = iterator.next();
            callbacks.add(entry.second);
            iterator.remove();
        }
    }
    for (OnIqPacketReceived callback : callbacks) {
        callback.onIqPacketReceived(account, failurePacket);
    }
    Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": done clearing iq callbacks. " + this.packetCallbacks.size() + " left");
}
Also used : ArrayList(java.util.ArrayList) IqPacket(eu.siacs.conversations.xmpp.stanzas.IqPacket) Pair(android.util.Pair)

Example 72 with IqPacket

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

the class XmppConnection method processPacket.

private Element processPacket(final Tag currentTag, final int packetType) throws XmlPullParserException, IOException {
    Element element;
    switch(packetType) {
        case PACKET_IQ:
            element = new IqPacket();
            break;
        case PACKET_MESSAGE:
            element = new MessagePacket();
            break;
        case PACKET_PRESENCE:
            element = new PresencePacket();
            break;
        default:
            return null;
    }
    element.setAttributes(currentTag.getAttributes());
    Tag nextTag = tagReader.readTag();
    if (nextTag == null) {
        throw new IOException("interrupted mid tag");
    }
    while (!nextTag.isEnd(element.getName())) {
        if (!nextTag.isNo()) {
            final Element child = tagReader.readElement(nextTag);
            final String type = currentTag.getAttribute("type");
            if (packetType == PACKET_IQ && "jingle".equals(child.getName()) && ("set".equalsIgnoreCase(type) || "get".equalsIgnoreCase(type))) {
                element = new JinglePacket();
                element.setAttributes(currentTag.getAttributes());
            }
            element.addChild(child);
        }
        nextTag = tagReader.readTag();
        if (nextTag == null) {
            throw new IOException("interrupted mid tag");
        }
    }
    if (stanzasReceived == Integer.MAX_VALUE) {
        resetStreamId();
        throw new IOException("time to restart the session. cant handle >2 billion pcks");
    }
    ++stanzasReceived;
    lastPacketReceived = SystemClock.elapsedRealtime();
    if (Config.BACKGROUND_STANZA_LOGGING && mXmppConnectionService.checkListeners()) {
        Log.d(Config.LOGTAG, "[background stanza] " + element);
    }
    return element;
}
Also used : MessagePacket(eu.siacs.conversations.xmpp.stanzas.MessagePacket) JinglePacket(eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket) Element(eu.siacs.conversations.xml.Element) Tag(eu.siacs.conversations.xml.Tag) IOException(java.io.IOException) PresencePacket(eu.siacs.conversations.xmpp.stanzas.PresencePacket) IqPacket(eu.siacs.conversations.xmpp.stanzas.IqPacket)

Example 73 with IqPacket

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

the class XmppConnection method sendServiceDiscoveryInfo.

private void sendServiceDiscoveryInfo(final Jid jid) {
    mPendingServiceDiscoveries.incrementAndGet();
    final IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
    iq.setTo(jid);
    iq.query("http://jabber.org/protocol/disco#info");
    this.sendIqPacket(iq, new OnIqPacketReceived() {

        @Override
        public void onIqPacketReceived(final Account account, final IqPacket packet) {
            if (packet.getType() == IqPacket.TYPE.RESULT) {
                boolean advancedStreamFeaturesLoaded;
                synchronized (XmppConnection.this.disco) {
                    ServiceDiscoveryResult result = new ServiceDiscoveryResult(packet);
                    if (jid.equals(account.getServer())) {
                        mXmppConnectionService.databaseBackend.insertDiscoveryResult(result);
                    }
                    disco.put(jid, result);
                    advancedStreamFeaturesLoaded = disco.containsKey(account.getServer()) && disco.containsKey(account.getJid().toBareJid());
                }
                if (advancedStreamFeaturesLoaded && (jid.equals(account.getServer()) || jid.equals(account.getJid().toBareJid()))) {
                    enableAdvancedStreamFeatures();
                }
            } else {
                Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": could not query disco info for " + jid.toString());
            }
            if (packet.getType() != IqPacket.TYPE.TIMEOUT) {
                if (mPendingServiceDiscoveries.decrementAndGet() == 0 && mWaitForDisco.compareAndSet(true, false)) {
                    finalizeBind();
                }
            }
        }
    });
}
Also used : Account(eu.siacs.conversations.entities.Account) ServiceDiscoveryResult(eu.siacs.conversations.entities.ServiceDiscoveryResult) IqPacket(eu.siacs.conversations.xmpp.stanzas.IqPacket)

Example 74 with IqPacket

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

the class XmppConnection method sendServiceDiscoveryItems.

private void sendServiceDiscoveryItems(final Jid server) {
    mPendingServiceDiscoveries.incrementAndGet();
    final IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
    iq.setTo(server.toDomainJid());
    iq.query("http://jabber.org/protocol/disco#items");
    this.sendIqPacket(iq, new OnIqPacketReceived() {

        @Override
        public void onIqPacketReceived(final Account account, final IqPacket packet) {
            if (packet.getType() == IqPacket.TYPE.RESULT) {
                final List<Element> elements = packet.query().getChildren();
                for (final Element element : elements) {
                    if (element.getName().equals("item")) {
                        final Jid jid = element.getAttributeAsJid("jid");
                        if (jid != null && !jid.equals(account.getServer())) {
                            sendServiceDiscoveryInfo(jid);
                        }
                    }
                }
            } else {
                Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": could not query disco items of " + server);
            }
            if (packet.getType() != IqPacket.TYPE.TIMEOUT) {
                if (mPendingServiceDiscoveries.decrementAndGet() == 0 && mWaitForDisco.compareAndSet(true, false)) {
                    finalizeBind();
                }
            }
        }
    });
}
Also used : Account(eu.siacs.conversations.entities.Account) Jid(eu.siacs.conversations.xmpp.jid.Jid) Element(eu.siacs.conversations.xml.Element) List(java.util.List) ArrayList(java.util.ArrayList) IqPacket(eu.siacs.conversations.xmpp.stanzas.IqPacket)

Example 75 with IqPacket

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

the class XmppConnection method processIq.

private void processIq(final Tag currentTag) throws XmlPullParserException, IOException {
    final IqPacket packet = (IqPacket) processPacket(currentTag, PACKET_IQ);
    if (packet.getId() == null) {
        // an iq packet without id is definitely invalid
        return;
    }
    if (packet instanceof JinglePacket) {
        if (this.jingleListener != null) {
            this.jingleListener.onJinglePacketReceived(account, (JinglePacket) packet);
        }
    } else {
        OnIqPacketReceived callback = null;
        synchronized (this.packetCallbacks) {
            if (packetCallbacks.containsKey(packet.getId())) {
                final Pair<IqPacket, OnIqPacketReceived> packetCallbackDuple = packetCallbacks.get(packet.getId());
                // Packets to the server should have responses from the server
                if (packetCallbackDuple.first.toServer(account)) {
                    if (packet.fromServer(account)) {
                        callback = packetCallbackDuple.second;
                        packetCallbacks.remove(packet.getId());
                    } else {
                        Log.e(Config.LOGTAG, account.getJid().toBareJid().toString() + ": ignoring spoofed iq packet");
                    }
                } else {
                    if (packet.getFrom().equals(packetCallbackDuple.first.getTo())) {
                        callback = packetCallbackDuple.second;
                        packetCallbacks.remove(packet.getId());
                    } else {
                        Log.e(Config.LOGTAG, account.getJid().toBareJid().toString() + ": ignoring spoofed iq packet");
                    }
                }
            } else if (packet.getType() == IqPacket.TYPE.GET || packet.getType() == IqPacket.TYPE.SET) {
                callback = this.unregisteredIqListener;
            }
        }
        if (callback != null) {
            callback.onIqPacketReceived(account, packet);
        }
    }
}
Also used : JinglePacket(eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket) 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