Search in sources :

Example 31 with IqPacket

use of de.pixart.messenger.xmpp.stanzas.IqPacket in project Pix-Art-Messenger by kriztan.

the class IqGenerator method enablePush.

public IqPacket enablePush(Jid jid, String node, String secret) {
    IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
    Element enable = packet.addChild("enable", "urn:xmpp:push:0");
    enable.setAttribute("jid", jid.toString());
    enable.setAttribute("node", node);
    Data data = new Data();
    data.setFormType("http://jabber.org/protocol/pubsub#publish-options");
    data.put("secret", secret);
    data.submit();
    enable.addChild(data);
    return packet;
}
Also used : Element(de.pixart.messenger.xml.Element) Data(de.pixart.messenger.xmpp.forms.Data) IqPacket(de.pixart.messenger.xmpp.stanzas.IqPacket)

Example 32 with IqPacket

use of de.pixart.messenger.xmpp.stanzas.IqPacket in project Pix-Art-Messenger by kriztan.

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) {
        try {
            callback.onIqPacketReceived(account, failurePacket);
        } catch (StateChangingError error) {
            Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": caught StateChangingError(" + error.state.toString() + ") while clearing callbacks");
        // ignore
        }
    }
    Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": done clearing iq callbacks. " + this.packetCallbacks.size() + " left");
}
Also used : ArrayList(java.util.ArrayList) IqPacket(de.pixart.messenger.xmpp.stanzas.IqPacket) Pair(android.util.Pair)

Example 33 with IqPacket

use of de.pixart.messenger.xmpp.stanzas.IqPacket in project Pix-Art-Messenger by kriztan.

the class XmppConnection method sendRegistryRequest.

private void sendRegistryRequest() {
    final IqPacket register = new IqPacket(IqPacket.TYPE.GET);
    register.query("jabber:iq:register");
    register.setTo(account.getServer());
    sendUnmodifiedIqPacket(register, new OnIqPacketReceived() {

        @Override
        public void onIqPacketReceived(final Account account, final IqPacket packet) {
            if (packet.getType() == IqPacket.TYPE.TIMEOUT) {
                return;
            }
            if (packet.getType() == IqPacket.TYPE.ERROR) {
                throw new StateChangingError(Account.State.REGISTRATION_FAILED);
            }
            final Element query = packet.query("jabber:iq:register");
            if (query.hasChild("username") && (query.hasChild("password"))) {
                final IqPacket register = new IqPacket(IqPacket.TYPE.SET);
                final Element username = new Element("username").setContent(account.getUsername());
                final Element password = new Element("password").setContent(account.getPassword());
                register.query("jabber:iq:register").addChild(username);
                register.query().addChild(password);
                register.setFrom(account.getJid().toBareJid());
                sendUnmodifiedIqPacket(register, registrationResponseListener, true);
            } else if (query.hasChild("x", Namespace.DATA)) {
                final Data data = Data.parse(query.findChild("x", Namespace.DATA));
                final Element blob = query.findChild("data", "urn:xmpp:bob");
                final String id = packet.getId();
                InputStream is;
                if (blob != null) {
                    try {
                        final String base64Blob = blob.getContent();
                        final byte[] strBlob = Base64.decode(base64Blob, Base64.DEFAULT);
                        is = new ByteArrayInputStream(strBlob);
                    } catch (Exception e) {
                        is = null;
                    }
                } else {
                    try {
                        Field field = data.getFieldByName("url");
                        URL url = field != null && field.getValue() != null ? new URL(field.getValue()) : null;
                        is = url != null ? url.openStream() : null;
                    } catch (IOException e) {
                        is = null;
                    }
                }
                if (is != null) {
                    Bitmap captcha = BitmapFactory.decodeStream(is);
                    try {
                        if (mXmppConnectionService.displayCaptchaRequest(account, id, data, captcha)) {
                            return;
                        }
                    } catch (Exception e) {
                        throw new StateChangingError(Account.State.REGISTRATION_FAILED);
                    }
                }
                throw new StateChangingError(Account.State.REGISTRATION_FAILED);
            } else if (query.hasChild("instructions") || query.hasChild("x", Namespace.OOB)) {
                final String instructions = query.findChildContent("instructions");
                final Element oob = query.findChild("x", Namespace.OOB);
                final String url = oob == null ? null : oob.findChildContent("url");
                if (url != null) {
                    setAccountCreationFailed(url);
                } else if (instructions != null) {
                    Matcher matcher = Patterns.AUTOLINK_WEB_URL.matcher(instructions);
                    if (matcher.find()) {
                        setAccountCreationFailed(instructions.substring(matcher.start(), matcher.end()));
                    }
                }
                throw new StateChangingError(Account.State.REGISTRATION_FAILED);
            }
        }
    }, true);
}
Also used : Account(de.pixart.messenger.entities.Account) Matcher(java.util.regex.Matcher) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Element(de.pixart.messenger.xml.Element) Data(de.pixart.messenger.xmpp.forms.Data) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidJidException(de.pixart.messenger.xmpp.jid.InvalidJidException) ConnectException(java.net.ConnectException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) URL(java.net.URL) IqPacket(de.pixart.messenger.xmpp.stanzas.IqPacket) Field(de.pixart.messenger.xmpp.forms.Field) Bitmap(android.graphics.Bitmap) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 34 with IqPacket

use of de.pixart.messenger.xmpp.stanzas.IqPacket in project Pix-Art-Messenger by kriztan.

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() != null && 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) {
            try {
                callback.onIqPacketReceived(account, packet);
            } catch (StateChangingError error) {
                throw new StateChangingException(error.state);
            }
        }
    }
}
Also used : JinglePacket(de.pixart.messenger.xmpp.jingle.stanzas.JinglePacket) IqPacket(de.pixart.messenger.xmpp.stanzas.IqPacket)

Example 35 with IqPacket

use of de.pixart.messenger.xmpp.stanzas.IqPacket in project Pix-Art-Messenger by kriztan.

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");
    }
    if (inSmacksSession) {
        ++stanzasReceived;
    } else if (features.sm()) {
        Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": not counting stanza(" + element.getClass().getSimpleName() + "). Not in smacks session.");
    }
    lastPacketReceived = SystemClock.elapsedRealtime();
    if (Config.BACKGROUND_STANZA_LOGGING && mXmppConnectionService.checkListeners()) {
        Log.d(Config.LOGTAG, "[background stanza] " + element);
    }
    return element;
}
Also used : MessagePacket(de.pixart.messenger.xmpp.stanzas.MessagePacket) JinglePacket(de.pixart.messenger.xmpp.jingle.stanzas.JinglePacket) Element(de.pixart.messenger.xml.Element) Tag(de.pixart.messenger.xml.Tag) IOException(java.io.IOException) PresencePacket(de.pixart.messenger.xmpp.stanzas.PresencePacket) IqPacket(de.pixart.messenger.xmpp.stanzas.IqPacket)

Aggregations

IqPacket (de.pixart.messenger.xmpp.stanzas.IqPacket)81 Element (de.pixart.messenger.xml.Element)48 Account (de.pixart.messenger.entities.Account)39 OnIqPacketReceived (de.pixart.messenger.xmpp.OnIqPacketReceived)31 Jid (de.pixart.messenger.xmpp.jid.Jid)15 ArrayList (java.util.ArrayList)7 Data (de.pixart.messenger.xmpp.forms.Data)6 InvalidJidException (de.pixart.messenger.xmpp.jid.InvalidJidException)6 PreKeyBundle (org.whispersystems.libsignal.state.PreKeyBundle)5 JinglePacket (de.pixart.messenger.xmpp.jingle.stanzas.JinglePacket)4 IOException (java.io.IOException)4 InvalidKeyException (org.whispersystems.libsignal.InvalidKeyException)4 Bundle (android.os.Bundle)3 Pair (android.util.Pair)3 Contact (de.pixart.messenger.entities.Contact)3 Conversation (de.pixart.messenger.entities.Conversation)3 FileNotFoundException (java.io.FileNotFoundException)3 InputStream (java.io.InputStream)3 HashSet (java.util.HashSet)3 List (java.util.List)3