Search in sources :

Example 36 with IqPacket

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

the class XmppConnection method sendBindRequest.

private void sendBindRequest() {
    try {
        mXmppConnectionService.restoredFromDatabaseLatch.await();
    } catch (InterruptedException e) {
        Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": interrupted while waiting for DB restore during bind");
        return;
    }
    clearIqCallbacks();
    if (account.getJid().isBareJid()) {
        account.setResource(this.createNewResource());
    }
    final IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
    final String resource = Config.USE_RANDOM_RESOURCE_ON_EVERY_BIND ? nextRandomId() : account.getResource();
    iq.addChild("bind", Namespace.BIND).addChild("resource").setContent(resource);
    this.sendUnmodifiedIqPacket(iq, (account, packet) -> {
        if (packet.getType() == IqPacket.TYPE.TIMEOUT) {
            return;
        }
        final Element bind = packet.findChild("bind");
        if (bind != null && packet.getType() == IqPacket.TYPE.RESULT) {
            isBound = true;
            final Element jid = bind.findChild("jid");
            if (jid != null && jid.getContent() != null) {
                try {
                    Jid assignedJid = Jid.fromString(jid.getContent());
                    if (!account.getJid().getDomainpart().equals(assignedJid.getDomainpart())) {
                        Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": server tried to re-assign domain to " + assignedJid.getDomainpart());
                        throw new StateChangingError(Account.State.BIND_FAILURE);
                    }
                    if (account.setJid(assignedJid)) {
                        Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": jid changed during bind. updating database");
                        mXmppConnectionService.databaseBackend.updateAccount(account);
                    }
                    if (streamFeatures.hasChild("session") && !streamFeatures.findChild("session").hasChild("optional")) {
                        sendStartSession();
                    } else {
                        sendPostBindInitialization();
                    }
                    return;
                } catch (final InvalidJidException e) {
                    Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": server reported invalid jid (" + jid.getContent() + ") on bind");
                }
            } else {
                Log.d(Config.LOGTAG, account.getJid() + ": disconnecting because of bind failure. (no jid)");
            }
        } else {
            Log.d(Config.LOGTAG, account.getJid() + ": disconnecting because of bind failure (" + packet.toString());
        }
        final Element error = packet.findChild("error");
        if (packet.getType() == IqPacket.TYPE.ERROR && error != null && error.hasChild("conflict")) {
            account.setResource(createNewResource());
        }
        throw new StateChangingError(Account.State.BIND_FAILURE);
    }, true);
}
Also used : Jid(de.pixart.messenger.xmpp.jid.Jid) Element(de.pixart.messenger.xml.Element) InvalidJidException(de.pixart.messenger.xmpp.jid.InvalidJidException) IqPacket(de.pixart.messenger.xmpp.stanzas.IqPacket)

Example 37 with IqPacket

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

the class JingleConnection method deliverPacket.

public void deliverPacket(JinglePacket packet) {
    boolean returnResult = true;
    if (packet.isAction("session-terminate")) {
        Reason reason = packet.getReason();
        if (reason != null) {
            if (reason.hasChild("cancel")) {
                this.fail();
            } else if (reason.hasChild("success")) {
                this.receiveSuccess();
            } else {
                this.fail();
            }
        } else {
            this.fail();
        }
    } else if (packet.isAction("session-accept")) {
        returnResult = receiveAccept(packet);
    } else if (packet.isAction("session-info")) {
        returnResult = true;
        Element checksum = packet.getChecksum();
        Element file = checksum == null ? null : checksum.findChild("file");
        Element hash = file == null ? null : file.findChild("hash", "urn:xmpp:hashes:2");
        if (hash != null && "sha-1".equalsIgnoreCase(hash.getAttribute("algo"))) {
            try {
                this.expectedHash = Base64.decode(hash.getContent(), Base64.DEFAULT);
            } catch (Exception e) {
                this.expectedHash = new byte[0];
            }
        }
    } else if (packet.isAction("transport-info")) {
        returnResult = receiveTransportInfo(packet);
    } else if (packet.isAction("transport-replace")) {
        if (packet.getJingleContent().hasIbbTransport()) {
            returnResult = this.receiveFallbackToIbb(packet);
        } else {
            returnResult = false;
            Log.d(Config.LOGTAG, "trying to fallback to something unknown" + packet.toString());
        }
    } else if (packet.isAction("transport-accept")) {
        returnResult = this.receiveTransportAccept(packet);
    } else {
        Log.d(Config.LOGTAG, "packet arrived in connection. action was " + packet.getAction());
        returnResult = false;
    }
    IqPacket response;
    if (returnResult) {
        response = packet.generateResponse(IqPacket.TYPE.RESULT);
    } else {
        response = packet.generateResponse(IqPacket.TYPE.ERROR);
    }
    mXmppConnectionService.sendIqPacket(account, response, null);
}
Also used : Element(de.pixart.messenger.xml.Element) Reason(de.pixart.messenger.xmpp.jingle.stanzas.Reason) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) IqPacket(de.pixart.messenger.xmpp.stanzas.IqPacket)

Example 38 with IqPacket

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

the class JingleInbandTransport method sendNextBlock.

private void sendNextBlock() {
    byte[] buffer = new byte[this.blockSize];
    try {
        int count = fileInputStream.read(buffer);
        if (count == -1) {
            sendClose();
            file.setSha1Sum(digest.digest());
            this.onFileTransmissionStatusChanged.onFileTransmitted(file);
            fileInputStream.close();
            return;
        } else if (count != buffer.length) {
            int rem = fileInputStream.read(buffer, count, buffer.length - count);
            if (rem > 0) {
                count += rem;
            }
        }
        this.remainingSize -= count;
        this.digest.update(buffer, 0, count);
        String base64 = Base64.encodeToString(buffer, 0, count, Base64.NO_WRAP);
        IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
        iq.setTo(this.counterpart);
        Element data = iq.addChild("data", "http://jabber.org/protocol/ibb");
        data.setAttribute("seq", Integer.toString(this.seq));
        data.setAttribute("block-size", Integer.toString(this.blockSize));
        data.setAttribute("sid", this.sessionId);
        data.setContent(base64);
        this.account.getXmppConnection().sendIqPacket(iq, this.onAckReceived);
        // don't fill up stanza queue too much
        this.account.getXmppConnection().r();
        this.seq++;
        if (this.remainingSize > 0) {
            connection.updateProgress((int) ((((double) (this.fileSize - this.remainingSize)) / this.fileSize) * 100));
        } else {
            sendClose();
            file.setSha1Sum(digest.digest());
            this.onFileTransmissionStatusChanged.onFileTransmitted(file);
            fileInputStream.close();
        }
    } catch (IOException e) {
        Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": io exception during sendNextBlock() " + e.getMessage());
        FileBackend.close(fileInputStream);
        this.onFileTransmissionStatusChanged.onFileTransferAborted();
    }
}
Also used : Element(de.pixart.messenger.xml.Element) IOException(java.io.IOException) IqPacket(de.pixart.messenger.xmpp.stanzas.IqPacket)

Example 39 with IqPacket

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

the class JingleInbandTransport method sendClose.

private void sendClose() {
    IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
    iq.setTo(this.counterpart);
    Element close = iq.addChild("close", "http://jabber.org/protocol/ibb");
    close.setAttribute("sid", this.sessionId);
    this.account.getXmppConnection().sendIqPacket(iq, null);
}
Also used : Element(de.pixart.messenger.xml.Element) IqPacket(de.pixart.messenger.xmpp.stanzas.IqPacket)

Example 40 with IqPacket

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

the class AxolotlService method publishDeviceIdsAndRefineAccessModel.

private void publishDeviceIdsAndRefineAccessModel(final Set<Integer> ids, final boolean firstAttempt) {
    final Bundle publishOptions = account.getXmppConnection().getFeatures().pepPublishOptions() ? PublishOptions.openAccess() : null;
    IqPacket publish = mXmppConnectionService.getIqGenerator().publishDeviceIds(ids, publishOptions);
    mXmppConnectionService.sendIqPacket(account, publish, new OnIqPacketReceived() {

        @Override
        public void onIqPacketReceived(Account account, IqPacket packet) {
            Element error = packet.getType() == IqPacket.TYPE.ERROR ? packet.findChild("error") : null;
            if (firstAttempt && error != null && error.hasChild("precondition-not-met", Namespace.PUBSUB_ERROR)) {
                Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": precondition wasn't met for device list. pushing node configuration");
                mXmppConnectionService.pushNodeConfiguration(account, AxolotlService.PEP_DEVICE_LIST, publishOptions, new XmppConnectionService.OnConfigurationPushed() {

                    @Override
                    public void onPushSucceeded() {
                        publishDeviceIdsAndRefineAccessModel(ids, false);
                    }

                    @Override
                    public void onPushFailed() {
                        publishDeviceIdsAndRefineAccessModel(ids, false);
                    }
                });
            } else {
                if (AxolotlService.this.changeAccessMode.compareAndSet(true, false)) {
                    Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": done changing access mode");
                    account.setOption(Account.OPTION_REQUIRES_ACCESS_MODE_CHANGE, false);
                    mXmppConnectionService.databaseBackend.updateAccount(account);
                }
                if (packet.getType() == IqPacket.TYPE.ERROR) {
                    pepBroken = true;
                    Log.d(Config.LOGTAG, getLogprefix(account) + "Error received while publishing own device id" + packet.findChild("error"));
                }
            }
        }
    });
}
Also used : Account(de.pixart.messenger.entities.Account) OnIqPacketReceived(de.pixart.messenger.xmpp.OnIqPacketReceived) Bundle(android.os.Bundle) PreKeyBundle(org.whispersystems.libsignal.state.PreKeyBundle) Element(de.pixart.messenger.xml.Element) 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