Search in sources :

Example 16 with Element

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

the class IqParser method verification.

public Pair<X509Certificate[], byte[]> verification(final IqPacket packet) {
    Element item = getItem(packet);
    Element verification = item != null ? item.findChild("verification", AxolotlService.PEP_PREFIX) : null;
    Element chain = verification != null ? verification.findChild("chain") : null;
    Element signature = verification != null ? verification.findChild("signature") : null;
    if (chain != null && signature != null) {
        List<Element> certElements = chain.getChildren();
        X509Certificate[] certificates = new X509Certificate[certElements.size()];
        try {
            CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
            int i = 0;
            for (Element cert : certElements) {
                certificates[i] = (X509Certificate) certificateFactory.generateCertificate(new ByteArrayInputStream(Base64.decode(cert.getContent(), Base64.DEFAULT)));
                ++i;
            }
            return new Pair<>(certificates, Base64.decode(signature.getContent(), Base64.DEFAULT));
        } catch (CertificateException e) {
            return null;
        }
    } else {
        return null;
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Element(de.pixart.messenger.xml.Element) CertificateException(java.security.cert.CertificateException) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate) Pair(android.util.Pair)

Example 17 with Element

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

the class IqParser method deviceIds.

@NonNull
public Set<Integer> deviceIds(final Element item) {
    Set<Integer> deviceIds = new HashSet<>();
    if (item != null) {
        final Element list = item.findChild("list");
        if (list != null) {
            for (Element device : list.getChildren()) {
                if (!device.getName().equals("device")) {
                    continue;
                }
                try {
                    Integer id = Integer.valueOf(device.getAttribute("id"));
                    deviceIds.add(id);
                } catch (NumberFormatException e) {
                    Log.e(Config.LOGTAG, AxolotlService.LOGPREFIX + " : " + "Encountered invalid <device> node in PEP (" + e.getMessage() + "):" + device.toString() + ", skipping...");
                    continue;
                }
            }
        }
    }
    return deviceIds;
}
Also used : Element(de.pixart.messenger.xml.Element) HashSet(java.util.HashSet) NonNull(android.support.annotation.NonNull)

Example 18 with Element

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

the class IqParser method onIqPacketReceived.

@Override
public void onIqPacketReceived(final Account account, final IqPacket packet) {
    final boolean isGet = packet.getType() == IqPacket.TYPE.GET;
    if (packet.getType() == IqPacket.TYPE.ERROR || packet.getType() == IqPacket.TYPE.TIMEOUT) {
        return;
    } else if (packet.hasChild("query", Namespace.ROSTER) && packet.fromServer(account)) {
        final Element query = packet.findChild("query");
        // If this is in response to a query for the whole roster:
        if (packet.getType() == IqPacket.TYPE.RESULT) {
            account.getRoster().markAllAsNotInRoster();
        }
        this.rosterItems(account, query);
    } else if ((packet.hasChild("block", Namespace.BLOCKING) || packet.hasChild("blocklist", Namespace.BLOCKING)) && packet.fromServer(account)) {
        // Block list or block push.
        Log.d(Config.LOGTAG, "Received blocklist update from server");
        final Element blocklist = packet.findChild("blocklist", Namespace.BLOCKING);
        final Element block = packet.findChild("block", Namespace.BLOCKING);
        final Collection<Element> items = blocklist != null ? blocklist.getChildren() : (block != null ? block.getChildren() : null);
        // Otherwise, just update the existing blocklist.
        if (packet.getType() == IqPacket.TYPE.RESULT) {
            account.clearBlocklist();
            account.getXmppConnection().getFeatures().setBlockListRequested(true);
        }
        if (items != null) {
            final Collection<Jid> jids = new ArrayList<>(items.size());
            // Create a collection of Jids from the packet
            for (final Element item : items) {
                if (item.getName().equals("item")) {
                    final Jid jid = item.getAttributeAsJid("jid");
                    if (jid != null) {
                        jids.add(jid);
                    }
                }
            }
            account.getBlocklist().addAll(jids);
            if (packet.getType() == IqPacket.TYPE.SET) {
                boolean removed = false;
                for (Jid jid : jids) {
                    removed |= mXmppConnectionService.removeBlockedConversations(account, jid);
                }
                if (removed) {
                    mXmppConnectionService.updateConversationUi();
                }
            }
        }
        // Update the UI
        mXmppConnectionService.updateBlocklistUi(OnUpdateBlocklist.Status.BLOCKED);
        if (packet.getType() == IqPacket.TYPE.SET) {
            final IqPacket response = packet.generateResponse(IqPacket.TYPE.RESULT);
            mXmppConnectionService.sendIqPacket(account, response, null);
        }
    } else if (packet.hasChild("unblock", Namespace.BLOCKING) && packet.fromServer(account) && packet.getType() == IqPacket.TYPE.SET) {
        Log.d(Config.LOGTAG, "Received unblock update from server");
        final Collection<Element> items = packet.findChild("unblock", Namespace.BLOCKING).getChildren();
        if (items.size() == 0) {
            // No children to unblock == unblock all
            account.getBlocklist().clear();
        } else {
            final Collection<Jid> jids = new ArrayList<>(items.size());
            for (final Element item : items) {
                if (item.getName().equals("item")) {
                    final Jid jid = item.getAttributeAsJid("jid");
                    if (jid != null) {
                        jids.add(jid);
                    }
                }
            }
            account.getBlocklist().removeAll(jids);
        }
        mXmppConnectionService.updateBlocklistUi(OnUpdateBlocklist.Status.UNBLOCKED);
        final IqPacket response = packet.generateResponse(IqPacket.TYPE.RESULT);
        mXmppConnectionService.sendIqPacket(account, response, null);
    } else if (packet.hasChild("open", "http://jabber.org/protocol/ibb") || packet.hasChild("data", "http://jabber.org/protocol/ibb") || packet.hasChild("close", "http://jabber.org/protocol/ibb")) {
        mXmppConnectionService.getJingleConnectionManager().deliverIbbPacket(account, packet);
    } else if (packet.hasChild("query", "http://jabber.org/protocol/disco#info")) {
        final IqPacket response = mXmppConnectionService.getIqGenerator().discoResponse(packet);
        mXmppConnectionService.sendIqPacket(account, response, null);
    } else if (packet.hasChild("query", "jabber:iq:version") && isGet) {
        final IqPacket response = mXmppConnectionService.getIqGenerator().versionResponse(packet);
        mXmppConnectionService.sendIqPacket(account, response, null);
    } else if (packet.hasChild("ping", "urn:xmpp:ping") && isGet) {
        final IqPacket response = packet.generateResponse(IqPacket.TYPE.RESULT);
        mXmppConnectionService.sendIqPacket(account, response, null);
    } else if (packet.hasChild("time", "urn:xmpp:time") && isGet) {
        final IqPacket response;
        if (mXmppConnectionService.useTorToConnect()) {
            response = packet.generateResponse(IqPacket.TYPE.ERROR);
            final Element error = response.addChild("error");
            error.setAttribute("type", "cancel");
            error.addChild("not-allowed", "urn:ietf:params:xml:ns:xmpp-stanzas");
        } else {
            response = mXmppConnectionService.getIqGenerator().entityTimeResponse(packet);
        }
        mXmppConnectionService.sendIqPacket(account, response, null);
    } else {
        if (packet.getType() == IqPacket.TYPE.GET || packet.getType() == IqPacket.TYPE.SET) {
            final IqPacket response = packet.generateResponse(IqPacket.TYPE.ERROR);
            final Element error = response.addChild("error");
            error.setAttribute("type", "cancel");
            error.addChild("feature-not-implemented", "urn:ietf:params:xml:ns:xmpp-stanzas");
            account.getXmppConnection().sendIqPacket(response, null);
        }
    }
}
Also used : Jid(de.pixart.messenger.xmpp.jid.Jid) Element(de.pixart.messenger.xml.Element) ArrayList(java.util.ArrayList) Collection(java.util.Collection) IqPacket(de.pixart.messenger.xmpp.stanzas.IqPacket)

Example 19 with Element

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

the class PresenceParser method processConferencePresence.

private void processConferencePresence(PresencePacket packet, Conversation conversation) {
    MucOptions mucOptions = conversation.getMucOptions();
    final Jid jid = conversation.getAccount().getJid();
    final Jid from = packet.getFrom();
    if (!from.isBareJid()) {
        final String type = packet.getAttribute("type");
        final Element x = packet.findChild("x", "http://jabber.org/protocol/muc#user");
        Avatar avatar = Avatar.parsePresence(packet.findChild("x", "vcard-temp:x:update"));
        final List<String> codes = getStatusCodes(x);
        if (type == null) {
            if (x != null) {
                Element item = x.findChild("item");
                if (item != null && !from.isBareJid()) {
                    mucOptions.setError(MucOptions.Error.NONE);
                    MucOptions.User user = parseItem(conversation, item, from);
                    if (codes.contains(MucOptions.STATUS_CODE_SELF_PRESENCE) || (codes.contains(MucOptions.STATUS_CODE_ROOM_CREATED) && jid.equals(item.getAttributeAsJid("jid")))) {
                        if (mucOptions.setOnline()) {
                            mXmppConnectionService.getAvatarService().clear(mucOptions);
                        }
                        mucOptions.setSelf(user);
                        mXmppConnectionService.persistSelfNick(user);
                        invokeRenameListener(mucOptions, true);
                    }
                    boolean isNew = mucOptions.updateUser(user);
                    final AxolotlService axolotlService = conversation.getAccount().getAxolotlService();
                    Contact contact = user.getContact();
                    if (isNew && user.getRealJid() != null && mucOptions.isPrivateAndNonAnonymous() && (contact == null || !contact.mutualPresenceSubscription()) && axolotlService.hasEmptyDeviceList(user.getRealJid())) {
                        axolotlService.fetchDeviceIds(user.getRealJid());
                    }
                    if (codes.contains(MucOptions.STATUS_CODE_ROOM_CREATED) && mucOptions.autoPushConfiguration()) {
                        Log.d(Config.LOGTAG, mucOptions.getAccount().getJid().toBareJid() + ": room '" + mucOptions.getConversation().getJid().toBareJid() + "' created. pushing default configuration");
                        mXmppConnectionService.pushConferenceConfiguration(mucOptions.getConversation(), IqGenerator.defaultRoomConfiguration(), null);
                    }
                    if (mXmppConnectionService.getPgpEngine() != null) {
                        Element signed = packet.findChild("x", "jabber:x:signed");
                        if (signed != null) {
                            Element status = packet.findChild("status");
                            String msg = status == null ? "" : status.getContent();
                            long keyId = mXmppConnectionService.getPgpEngine().fetchKeyId(mucOptions.getAccount(), msg, signed.getContent());
                            if (keyId != 0) {
                                user.setPgpKeyId(keyId);
                            }
                        }
                    }
                    if (avatar != null) {
                        avatar.owner = from;
                        if (mXmppConnectionService.getFileBackend().isAvatarCached(avatar)) {
                            if (user.setAvatar(avatar)) {
                                mXmppConnectionService.getAvatarService().clear(user);
                            }
                        } else if (mXmppConnectionService.isDataSaverDisabled()) {
                            mXmppConnectionService.fetchAvatar(mucOptions.getAccount(), avatar);
                        }
                    }
                }
            }
        } else if (type.equals("unavailable")) {
            if (codes.contains(MucOptions.STATUS_CODE_SHUTDOWN) && from.equals(mucOptions.getSelf().getFullJid())) {
                mucOptions.setError(MucOptions.Error.SHUTDOWN);
            } else if (codes.contains(MucOptions.STATUS_CODE_SELF_PRESENCE)) {
                if (codes.contains(MucOptions.STATUS_CODE_KICKED)) {
                    mucOptions.setError(MucOptions.Error.KICKED);
                } else if (codes.contains(MucOptions.STATUS_CODE_BANNED)) {
                    mucOptions.setError(MucOptions.Error.BANNED);
                } else if (codes.contains(MucOptions.STATUS_CODE_LOST_MEMBERSHIP)) {
                    mucOptions.setError(MucOptions.Error.MEMBERS_ONLY);
                } else if (codes.contains(MucOptions.STATUS_CODE_AFFILIATION_CHANGE)) {
                    mucOptions.setError(MucOptions.Error.MEMBERS_ONLY);
                } else if (codes.contains(MucOptions.STATUS_CODE_SHUTDOWN)) {
                    mucOptions.setError(MucOptions.Error.SHUTDOWN);
                } else if (!codes.contains(MucOptions.STATUS_CODE_CHANGED_NICK)) {
                    mucOptions.setError(MucOptions.Error.UNKNOWN);
                    Log.d(Config.LOGTAG, "unknown error in conference: " + packet);
                }
            } else if (!from.isBareJid()) {
                Element item = x.findChild("item");
                if (item != null) {
                    mucOptions.updateUser(parseItem(conversation, item, from));
                }
                MucOptions.User user = mucOptions.deleteUser(from);
                if (user != null) {
                    mXmppConnectionService.getAvatarService().clear(user);
                }
            }
        } else if (type.equals("error")) {
            final Element error = packet.findChild("error");
            if (error == null) {
                return;
            }
            if (error.hasChild("conflict")) {
                if (mucOptions.online()) {
                    invokeRenameListener(mucOptions, false);
                } else {
                    mucOptions.setError(MucOptions.Error.NICK_IN_USE);
                }
            } else if (error.hasChild("not-authorized")) {
                mucOptions.setError(MucOptions.Error.PASSWORD_REQUIRED);
            } else if (error.hasChild("forbidden")) {
                mucOptions.setError(MucOptions.Error.BANNED);
            } else if (error.hasChild("registration-required")) {
                mucOptions.setError(MucOptions.Error.MEMBERS_ONLY);
            } else {
                final String text = error.findChildContent("text");
                if (text != null && text.contains("attribute 'to'")) {
                    if (mucOptions.online()) {
                        invokeRenameListener(mucOptions, false);
                    } else {
                        mucOptions.setError(MucOptions.Error.INVALID_NICK);
                    }
                } else {
                    mucOptions.setError(MucOptions.Error.UNKNOWN);
                    Log.d(Config.LOGTAG, "unknown error in conference: " + packet);
                }
            }
        }
    }
}
Also used : AxolotlService(de.pixart.messenger.crypto.axolotl.AxolotlService) MucOptions(de.pixart.messenger.entities.MucOptions) Jid(de.pixart.messenger.xmpp.jid.Jid) Element(de.pixart.messenger.xml.Element) Avatar(de.pixart.messenger.xmpp.pep.Avatar) Contact(de.pixart.messenger.entities.Contact)

Example 20 with Element

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

the class IqGenerator method publishAvatarMetadata.

public IqPacket publishAvatarMetadata(final Avatar avatar) {
    final Element item = new Element("item");
    item.setAttribute("id", avatar.sha1sum);
    final Element metadata = item.addChild("metadata", "urn:xmpp:avatar:metadata");
    final Element info = metadata.addChild("info");
    info.setAttribute("bytes", avatar.size);
    info.setAttribute("id", avatar.sha1sum);
    info.setAttribute("height", avatar.height);
    info.setAttribute("width", avatar.height);
    info.setAttribute("type", avatar.type);
    return publish("urn:xmpp:avatar:metadata", item);
}
Also used : Element(de.pixart.messenger.xml.Element)

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