Search in sources :

Example 36 with Element

use of de.pixart.messenger.xml.Element in project Pix-Art-Messenger by kriztan.

the class XmppConnection method sendPostBindInitialization.

private void sendPostBindInitialization() {
    smVersion = 0;
    if (streamFeatures.hasChild("sm", "urn:xmpp:sm:3")) {
        smVersion = 3;
    } else if (streamFeatures.hasChild("sm", "urn:xmpp:sm:2")) {
        smVersion = 2;
    }
    if (smVersion != 0) {
        synchronized (this.mStanzaQueue) {
            final EnablePacket enable = new EnablePacket(smVersion);
            tagWriter.writeStanzaAsync(enable);
            stanzasSent = 0;
            mStanzaQueue.clear();
        }
    }
    features.carbonsEnabled = false;
    features.blockListRequested = false;
    synchronized (this.disco) {
        this.disco.clear();
    }
    Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": starting service discovery");
    mPendingServiceDiscoveries.set(0);
    if (smVersion == 0 || Patches.DISCO_EXCEPTIONS.contains(account.getJid().getDomainpart())) {
        Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": do not wait for service discovery");
        mWaitForDisco.set(false);
    } else {
        mWaitForDisco.set(true);
    }
    lastDiscoStarted = SystemClock.elapsedRealtime();
    mXmppConnectionService.scheduleWakeUpCall(Config.CONNECT_DISCO_TIMEOUT, account.getUuid().hashCode());
    Element caps = streamFeatures.findChild("c");
    final String hash = caps == null ? null : caps.getAttribute("hash");
    final String ver = caps == null ? null : caps.getAttribute("ver");
    ServiceDiscoveryResult discoveryResult = null;
    if (hash != null && ver != null) {
        discoveryResult = mXmppConnectionService.getCachedServiceDiscoveryResult(new Pair<>(hash, ver));
    }
    final boolean requestDiscoItemsFirst = !account.isOptionSet(Account.OPTION_LOGGED_IN_SUCCESSFULLY);
    if (requestDiscoItemsFirst) {
        sendServiceDiscoveryItems(account.getServer());
    }
    if (discoveryResult == null) {
        sendServiceDiscoveryInfo(account.getServer());
    } else {
        Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": server caps came from cache");
        disco.put(account.getServer(), discoveryResult);
    }
    sendServiceDiscoveryInfo(account.getJid().toBareJid());
    if (!requestDiscoItemsFirst) {
        sendServiceDiscoveryItems(account.getServer());
    }
    if (!mWaitForDisco.get()) {
        finalizeBind();
    }
    this.lastSessionStarted = SystemClock.elapsedRealtime();
}
Also used : EnablePacket(de.pixart.messenger.xmpp.stanzas.streammgmt.EnablePacket) ServiceDiscoveryResult(de.pixart.messenger.entities.ServiceDiscoveryResult) Element(de.pixart.messenger.xml.Element) Pair(android.util.Pair)

Example 37 with Element

use of de.pixart.messenger.xml.Element 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)

Example 38 with Element

use of de.pixart.messenger.xml.Element 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 39 with Element

use of de.pixart.messenger.xml.Element in project Pix-Art-Messenger by kriztan.

the class JingleCandidate method toElement.

public Element toElement() {
    Element element = new Element("candidate");
    element.setAttribute("cid", this.getCid());
    element.setAttribute("host", this.getHost());
    element.setAttribute("port", Integer.toString(this.getPort()));
    element.setAttribute("jid", this.getJid().toString());
    element.setAttribute("priority", Integer.toString(this.getPriority()));
    if (this.getType() == TYPE_DIRECT) {
        element.setAttribute("type", "direct");
    } else if (this.getType() == TYPE_PROXY) {
        element.setAttribute("type", "proxy");
    }
    return element;
}
Also used : Element(de.pixart.messenger.xml.Element)

Example 40 with Element

use of de.pixart.messenger.xml.Element 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)

Aggregations

Element (de.pixart.messenger.xml.Element)100 IqPacket (de.pixart.messenger.xmpp.stanzas.IqPacket)48 Account (de.pixart.messenger.entities.Account)23 Jid (de.pixart.messenger.xmpp.jid.Jid)19 OnIqPacketReceived (de.pixart.messenger.xmpp.OnIqPacketReceived)17 Contact (de.pixart.messenger.entities.Contact)9 MessagePacket (de.pixart.messenger.xmpp.stanzas.MessagePacket)8 ArrayList (java.util.ArrayList)7 Conversation (de.pixart.messenger.entities.Conversation)6 IOException (java.io.IOException)6 Data (de.pixart.messenger.xmpp.forms.Data)5 Avatar (de.pixart.messenger.xmpp.pep.Avatar)5 HashSet (java.util.HashSet)5 AxolotlService (de.pixart.messenger.crypto.axolotl.AxolotlService)4 MucOptions (de.pixart.messenger.entities.MucOptions)4 ECPublicKey (org.whispersystems.libsignal.ecc.ECPublicKey)4 PreKeyBundle (org.whispersystems.libsignal.state.PreKeyBundle)4 Pair (android.util.Pair)3 Bookmark (de.pixart.messenger.entities.Bookmark)3 Message (de.pixart.messenger.entities.Message)3