Search in sources :

Example 46 with Element

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

the class Avatar method parseMetadata.

public static Avatar parseMetadata(Element items) {
    Element item = items.findChild("item");
    if (item == null) {
        return null;
    }
    Element metadata = item.findChild("metadata");
    if (metadata == null) {
        return null;
    }
    String primaryId = item.getAttribute("id");
    if (primaryId == null) {
        return null;
    }
    for (Element child : metadata.getChildren()) {
        if (child.getName().equals("info") && primaryId.equals(child.getAttribute("id"))) {
            Avatar avatar = new Avatar();
            String height = child.getAttribute("height");
            String width = child.getAttribute("width");
            String size = child.getAttribute("bytes");
            try {
                if (height != null) {
                    avatar.height = Integer.parseInt(height);
                }
                if (width != null) {
                    avatar.width = Integer.parseInt(width);
                }
                if (size != null) {
                    avatar.size = Long.parseLong(size);
                }
            } catch (NumberFormatException e) {
                return null;
            }
            avatar.type = child.getAttribute("type");
            String hash = child.getAttribute("id");
            if (!isValidSHA1(hash)) {
                return null;
            }
            avatar.sha1sum = hash;
            avatar.origin = Origin.PEP;
            return avatar;
        }
    }
    return null;
}
Also used : Element(de.pixart.messenger.xml.Element)

Example 47 with Element

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

the class MessagePacket method setBody.

public void setBody(String text) {
    this.children.remove(findChild("body"));
    Element body = new Element("body");
    body.setContent(text);
    this.children.add(0, body);
}
Also used : Element(de.pixart.messenger.xml.Element)

Example 48 with Element

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

the class MessagePacket method getForwardedMessagePacket.

public Pair<MessagePacket, Long> getForwardedMessagePacket(String name, String namespace) {
    Element wrapper = findChild(name, namespace);
    if (wrapper == null) {
        return null;
    }
    Element forwarded = wrapper.findChild("forwarded", "urn:xmpp:forward:0");
    if (forwarded == null) {
        return null;
    }
    MessagePacket packet = create(forwarded.findChild("message"));
    if (packet == null) {
        return null;
    }
    Long timestamp = AbstractParser.parseTimestamp(forwarded, null);
    return new Pair(packet, timestamp);
}
Also used : Element(de.pixart.messenger.xml.Element) Pair(android.util.Pair)

Example 49 with Element

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

Example 50 with Element

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

the class AxolotlService method publishDeviceBundle.

private void publishDeviceBundle(final SignedPreKeyRecord signedPreKeyRecord, final Set<PreKeyRecord> preKeyRecords, final boolean announceAfter, final boolean wipe, final boolean firstAttempt) {
    final Bundle publishOptions = account.getXmppConnection().getFeatures().pepPublishOptions() ? PublishOptions.openAccess() : null;
    IqPacket publish = mXmppConnectionService.getIqGenerator().publishBundles(signedPreKeyRecord, axolotlStore.getIdentityKeyPair().getPublicKey(), preKeyRecords, getOwnDeviceId(), publishOptions);
    Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + ": Bundle " + getOwnDeviceId() + " in PEP not current. Publishing...");
    mXmppConnectionService.sendIqPacket(account, publish, new OnIqPacketReceived() {

        @Override
        public void onIqPacketReceived(final 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 bundle. pushing node configuration");
                final String node = AxolotlService.PEP_BUNDLES + ":" + getOwnDeviceId();
                mXmppConnectionService.pushNodeConfiguration(account, node, publishOptions, new XmppConnectionService.OnConfigurationPushed() {

                    @Override
                    public void onPushSucceeded() {
                        publishDeviceBundle(signedPreKeyRecord, preKeyRecords, announceAfter, wipe, false);
                    }

                    @Override
                    public void onPushFailed() {
                        publishDeviceBundle(signedPreKeyRecord, preKeyRecords, announceAfter, wipe, false);
                    }
                });
            } else if (packet.getType() == IqPacket.TYPE.RESULT) {
                Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Successfully published bundle. ");
                if (wipe) {
                    wipeOtherPepDevices();
                } else if (announceAfter) {
                    Log.d(Config.LOGTAG, getLogprefix(account) + "Announcing device " + getOwnDeviceId());
                    publishOwnDeviceIdIfNeeded();
                }
            } else if (packet.getType() == IqPacket.TYPE.ERROR) {
                pepBroken = true;
                Log.d(Config.LOGTAG, getLogprefix(account) + "Error received while publishing bundle: " + 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

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