Search in sources :

Example 26 with Jid

use of de.pixart.messenger.xmpp.jid.Jid in project Pix-Art-Messenger by kriztan.

the class CryptoHelper method extractJidAndName.

public static Pair<Jid, String> extractJidAndName(X509Certificate certificate) throws CertificateEncodingException, InvalidJidException, CertificateParsingException {
    Collection<List<?>> alternativeNames = certificate.getSubjectAlternativeNames();
    List<String> emails = new ArrayList<>();
    if (alternativeNames != null) {
        for (List<?> san : alternativeNames) {
            Integer type = (Integer) san.get(0);
            if (type == 1) {
                emails.add((String) san.get(1));
            }
        }
    }
    X500Name x500name = new JcaX509CertificateHolder(certificate).getSubject();
    if (emails.size() == 0 && x500name.getRDNs(BCStyle.EmailAddress).length > 0) {
        emails.add(IETFUtils.valueToString(x500name.getRDNs(BCStyle.EmailAddress)[0].getFirst().getValue()));
    }
    String name = x500name.getRDNs(BCStyle.CN).length > 0 ? IETFUtils.valueToString(x500name.getRDNs(BCStyle.CN)[0].getFirst().getValue()) : null;
    if (emails.size() >= 1) {
        return new Pair<>(Jid.fromString(emails.get(0)), name);
    } else if (name != null) {
        try {
            Jid jid = Jid.fromString(name);
            if (jid.isBareJid() && !jid.isDomainJid()) {
                return new Pair<>(jid, null);
            }
        } catch (InvalidJidException e) {
            return null;
        }
    }
    return null;
}
Also used : Jid(de.pixart.messenger.xmpp.jid.Jid) InvalidJidException(de.pixart.messenger.xmpp.jid.InvalidJidException) ArrayList(java.util.ArrayList) X500Name(org.bouncycastle.asn1.x500.X500Name) JcaX509CertificateHolder(org.bouncycastle.cert.jcajce.JcaX509CertificateHolder) ArrayList(java.util.ArrayList) List(java.util.List) Pair(android.util.Pair)

Example 27 with Jid

use of de.pixart.messenger.xmpp.jid.Jid in project Pix-Art-Messenger by kriztan.

the class PresenceSelector method showPresenceSelectionDialog.

public static void showPresenceSelectionDialog(Activity activity, final Conversation conversation, final OnPresenceSelected listener) {
    final Contact contact = conversation.getContact();
    final Presences presences = contact.getPresences();
    AlertDialog.Builder builder = new AlertDialog.Builder(activity);
    builder.setTitle(activity.getString(R.string.choose_presence));
    final String[] resourceArray = presences.toResourceArray();
    Pair<Map<String, String>, Map<String, String>> typeAndName = presences.toTypeAndNameMap();
    final Map<String, String> resourceTypeMap = typeAndName.first;
    final Map<String, String> resourceNameMap = typeAndName.second;
    final String[] readableIdentities = new String[resourceArray.length];
    final AtomicInteger selectedResource = new AtomicInteger(0);
    for (int i = 0; i < resourceArray.length; ++i) {
        String resource = resourceArray[i];
        if (resource.equals(contact.getLastResource())) {
            selectedResource.set(i);
        }
        String type = resourceTypeMap.get(resource);
        String name = resourceNameMap.get(resource);
        if (type != null) {
            if (Collections.frequency(resourceTypeMap.values(), type) == 1) {
                readableIdentities[i] = translateType(activity, type);
            } else if (name != null) {
                if (Collections.frequency(resourceNameMap.values(), name) == 1 || CryptoHelper.UUID_PATTERN.matcher(resource).matches()) {
                    readableIdentities[i] = translateType(activity, type) + "  (" + name + ")";
                } else {
                    readableIdentities[i] = translateType(activity, type) + " (" + name + " / " + resource + ")";
                }
            } else {
                readableIdentities[i] = translateType(activity, type) + " (" + resource + ")";
            }
        } else {
            readableIdentities[i] = resource;
        }
    }
    builder.setSingleChoiceItems(readableIdentities, selectedResource.get(), (dialog, which) -> selectedResource.set(which));
    builder.setNegativeButton(R.string.cancel, null);
    builder.setPositiveButton(R.string.ok, (dialog, which) -> {
        try {
            Jid next = Jid.fromParts(contact.getJid().getLocalpart(), contact.getJid().getDomainpart(), resourceArray[selectedResource.get()]);
            conversation.setNextCounterpart(next);
        } catch (InvalidJidException e) {
            conversation.setNextCounterpart(null);
        }
        listener.onPresenceSelected();
    });
    builder.create().show();
}
Also used : AlertDialog(android.app.AlertDialog) Jid(de.pixart.messenger.xmpp.jid.Jid) Presences(de.pixart.messenger.entities.Presences) InvalidJidException(de.pixart.messenger.xmpp.jid.InvalidJidException) Contact(de.pixart.messenger.entities.Contact) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map)

Example 28 with Jid

use of de.pixart.messenger.xmpp.jid.Jid 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 29 with Jid

use of de.pixart.messenger.xmpp.jid.Jid in project Pix-Art-Messenger by kriztan.

the class JingleConnection method upgradeNamespace.

private void upgradeNamespace() {
    Jid jid = this.message.getCounterpart();
    String resource = jid != null ? jid.getResourcepart() : null;
    if (resource != null) {
        Presence presence = this.account.getRoster().getContact(jid).getPresences().getPresences().get(resource);
        ServiceDiscoveryResult result = presence != null ? presence.getServiceDiscoveryResult() : null;
        if (result != null) {
            List<String> features = result.getFeatures();
            if (features.contains(Content.Version.FT_5.getNamespace())) {
                this.ftVersion = Content.Version.FT_5;
            } else if (features.contains(Content.Version.FT_4.getNamespace())) {
                this.ftVersion = Content.Version.FT_4;
            }
        }
    }
}
Also used : Jid(de.pixart.messenger.xmpp.jid.Jid) ServiceDiscoveryResult(de.pixart.messenger.entities.ServiceDiscoveryResult) Presence(de.pixart.messenger.entities.Presence)

Example 30 with Jid

use of de.pixart.messenger.xmpp.jid.Jid in project Pix-Art-Messenger by kriztan.

the class OtrService method sendOtrErrorMessage.

public void sendOtrErrorMessage(SessionID session, String errorText) {
    try {
        Jid jid = Jid.fromSessionID(session);
        Conversation conversation = mXmppConnectionService.find(account, jid);
        String id = conversation == null ? null : conversation.getLastReceivedOtrMessageId();
        if (id != null) {
            MessagePacket packet = mXmppConnectionService.getMessageGenerator().generateOtrError(jid, id, errorText);
            packet.setFrom(account.getJid());
            mXmppConnectionService.sendMessagePacket(account, packet);
            Log.d(Config.LOGTAG, packet.toString());
            Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": unreadable OTR message in " + conversation.getName());
        }
    } catch (InvalidJidException e) {
        return;
    }
}
Also used : MessagePacket(de.pixart.messenger.xmpp.stanzas.MessagePacket) Jid(de.pixart.messenger.xmpp.jid.Jid) InvalidJidException(de.pixart.messenger.xmpp.jid.InvalidJidException) Conversation(de.pixart.messenger.entities.Conversation)

Aggregations

Jid (de.pixart.messenger.xmpp.jid.Jid)75 InvalidJidException (de.pixart.messenger.xmpp.jid.InvalidJidException)26 Account (de.pixart.messenger.entities.Account)24 Element (de.pixart.messenger.xml.Element)19 Conversation (de.pixart.messenger.entities.Conversation)18 IqPacket (de.pixart.messenger.xmpp.stanzas.IqPacket)15 Contact (de.pixart.messenger.entities.Contact)14 MucOptions (de.pixart.messenger.entities.MucOptions)11 ArrayList (java.util.ArrayList)11 OnIqPacketReceived (de.pixart.messenger.xmpp.OnIqPacketReceived)8 Message (de.pixart.messenger.entities.Message)7 Intent (android.content.Intent)6 AxolotlService (de.pixart.messenger.crypto.axolotl.AxolotlService)6 Bookmark (de.pixart.messenger.entities.Bookmark)6 MessagePacket (de.pixart.messenger.xmpp.stanzas.MessagePacket)4 Map (java.util.Map)4 SuppressLint (android.annotation.SuppressLint)3 AlertDialog (android.support.v7.app.AlertDialog)3 XmppAxolotlMessage (de.pixart.messenger.crypto.axolotl.XmppAxolotlMessage)3 HashMap (java.util.HashMap)3