Java Code Examples for org.jxmpp.jid.impl.JidCreate#bareFrom()

The following examples show how to use org.jxmpp.jid.impl.JidCreate#bareFrom() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RosterManager.java    From mangosta-android with Apache License 2.0 6 votes vote down vote up
public void addContact(String jidString)
        throws SmackException.NotLoggedInException, InterruptedException,
        SmackException.NotConnectedException, XMPPException.XMPPErrorException,
        SmackException.NoResponseException, XmppStringprepException {
    Roster roster = Roster.getInstanceFor(XMPPSession.getInstance().getXMPPConnection());
    if (!roster.isLoaded()) {
        roster.reloadAndWait();
    }

    BareJid jid = JidCreate.bareFrom(jidString);
    String name = XMPPUtils.fromJIDToUserName(jidString);
    String[] groups = new String[]{"Buddies"};

    roster.createEntry(jid, name, groups);
    roster.sendSubscriptionRequest(jid);
}
 
Example 2
Source File: XmppConnection.java    From Zom-Android-XMPP with GNU General Public License v3.0 6 votes vote down vote up
public void initUser(long providerId, long accountId) throws ImException
{
    ContentResolver contentResolver = mContext.getContentResolver();

    Cursor cursor = contentResolver.query(Imps.ProviderSettings.CONTENT_URI, new String[]{Imps.ProviderSettings.NAME, Imps.ProviderSettings.VALUE}, Imps.ProviderSettings.PROVIDER + "=?", new String[]{Long.toString(providerId)}, null);

    if (cursor == null)
        throw new ImException("unable to query settings");

    Imps.ProviderSettings.QueryMap providerSettings = new Imps.ProviderSettings.QueryMap(
            cursor, contentResolver, providerId, false, null);

    mProviderId = providerId;
    mAccountId = accountId;
    mUser = makeUser(providerSettings, contentResolver);
    try {
        mUserJid = JidCreate.bareFrom(mUser.getAddress().getAddress());
    }
    catch (Exception e){}

    providerSettings.close();
}
 
Example 3
Source File: RosterManager.java    From mangosta-android with Apache License 2.0 6 votes vote down vote up
public void removeContact(String jidString)
        throws SmackException.NotLoggedInException, InterruptedException,
        SmackException.NotConnectedException, XMPPException.XMPPErrorException,
        SmackException.NoResponseException, XmppStringprepException {
    Roster roster = Roster.getInstanceFor(XMPPSession.getInstance().getXMPPConnection());
    if (!roster.isLoaded()) {
        roster.reloadAndWait();
    }

    BareJid jid = JidCreate.bareFrom(jidString);
    roster.removeEntry(roster.getEntry(jid));

    Presence presence = new Presence(Presence.Type.unsubscribe);
    presence.setTo(JidCreate.from(jidString));
    XMPPSession.getInstance().sendStanza(presence);
}
 
Example 4
Source File: OmemoDeviceTest.java    From Smack with Apache License 2.0 6 votes vote down vote up
/**
 * Test, if the equals() method works as intended.
 */
@Test
public void testEquals() {
    BareJid romeo, juliet, guyUnderTheBalcony;
    try {
        romeo = JidCreate.bareFrom("[email protected]");
        guyUnderTheBalcony = JidCreate.bareFrom("[email protected]/underTheBalcony");
        juliet = JidCreate.bareFrom("[email protected]");
    } catch (XmppStringprepException e) {
        Assert.fail(e.getMessage());
        return;
    }

    OmemoDevice r = new OmemoDevice(romeo, 1);
    OmemoDevice g = new OmemoDevice(guyUnderTheBalcony, 1);
    OmemoDevice r2 = new OmemoDevice(romeo, 2);
    OmemoDevice j = new OmemoDevice(juliet, 3);
    OmemoDevice j2 = new OmemoDevice(juliet, 1);

    assertTrue(r.equals(g));
    assertFalse(r.equals(r2));
    assertFalse(j.equals(j2));
    assertFalse(j2.equals(r2));
}
 
Example 5
Source File: OmemoServiceTest.java    From Smack with Apache License 2.0 6 votes vote down vote up
/**
 * Test correct functionality of isStale method.
 * @throws XmppStringprepException if the provided string is invalid.
 */
@Test
public void isStaleDeviceTest() throws XmppStringprepException {
    OmemoDevice user = new OmemoDevice(JidCreate.bareFrom("[email protected]"), 123);
    OmemoDevice other = new OmemoDevice(JidCreate.bareFrom("[email protected]"), 444);

    Date now = new Date();
    Date deleteMe = new Date(now.getTime() - ((DELETE_STALE + 1) * ONE_HOUR));

    // Devices one hour "older" than max ages are stale
    assertTrue(OmemoService.isStale(user, other, deleteMe, DELETE_STALE));

    // Own device is never stale, no matter how old
    assertFalse(OmemoService.isStale(user, user, deleteMe, DELETE_STALE));

    // Always return false if date is null.
    assertFalse(OmemoService.isStale(user, other, null, DELETE_STALE));
}
 
Example 6
Source File: OmemoExceptionsTest.java    From Smack with Apache License 2.0 6 votes vote down vote up
@Test
public void cannotEstablishOmemoSessionExceptionTest() throws XmppStringprepException {
    OmemoDevice alice1 = new OmemoDevice(JidCreate.bareFrom("[email protected]"), 1234);
    OmemoDevice alice2 = new OmemoDevice(JidCreate.bareFrom("[email protected]"), 2345);
    OmemoDevice bob = new OmemoDevice(JidCreate.bareFrom("[email protected]"), 5678);

    CannotEstablishOmemoSessionException c = new CannotEstablishOmemoSessionException(alice1, null);
    assertEquals(1, c.getFailures().size());
    assertTrue(c.getFailures().containsKey(alice1.getJid()));

    c.addSuccess(alice2);
    assertFalse(c.requiresThrowing());

    c.addFailures(new CannotEstablishOmemoSessionException(bob, null));
    assertTrue(c.requiresThrowing());
    assertEquals(1, c.getSuccesses().size());
    assertEquals(2, c.getFailures().size());

    c.getSuccesses().remove(alice2.getJid());
    c.addFailures(new CannotEstablishOmemoSessionException(alice2, null));
    assertEquals(2, c.getFailures().size());
}
 
Example 7
Source File: OmemoExceptionsTest.java    From Smack with Apache License 2.0 6 votes vote down vote up
@Test
public void undecidedOmemoIdentityExceptionTest() throws XmppStringprepException {
    OmemoDevice alice = new OmemoDevice(JidCreate.bareFrom("[email protected]"), 1234);
    OmemoDevice bob = new OmemoDevice(JidCreate.bareFrom("[email protected]"), 5678);
    OmemoDevice mallory = new OmemoDevice(JidCreate.bareFrom("[email protected]"), 9876);

    UndecidedOmemoIdentityException u = new UndecidedOmemoIdentityException(alice);
    assertTrue(u.getUndecidedDevices().contains(alice));
    assertTrue(u.getUndecidedDevices().size() == 1);

    UndecidedOmemoIdentityException v = new UndecidedOmemoIdentityException(bob);
    v.getUndecidedDevices().add(mallory);
    assertTrue(v.getUndecidedDevices().size() == 2);
    assertTrue(v.getUndecidedDevices().contains(bob));
    assertTrue(v.getUndecidedDevices().contains(mallory));

    u.getUndecidedDevices().add(bob);
    u.join(v);
    assertTrue(u.getUndecidedDevices().size() == 3);
}
 
Example 8
Source File: PresenceManager.java    From Spark with Apache License 2.0 5 votes vote down vote up
/**
 * Returns true if the user is online and their status is available or free to chat.
 *
 * @param jidString the jid of the user.
 * @return true if the user is online and available.
 * @deprecated use {@link #isAvailable(BareJid)} instead.
 */
@Deprecated
public static boolean isAvailable(String jidString) {
    BareJid jid;
    try {
        jid = JidCreate.bareFrom(jidString);
    } catch (XmppStringprepException e) {
        throw new IllegalStateException(e);
    }
    return isAvailable(jid);
}
 
Example 9
Source File: SignalOmemoStoreConnector.java    From Smack with Apache License 2.0 5 votes vote down vote up
@Override
public void deleteAllSessions(String s) {
    BareJid jid;
    try {
        jid = JidCreate.bareFrom(s);
    } catch (XmppStringprepException e) {
        throw new AssertionError(e);
    }

    omemoStore.removeAllRawSessionsOf(getOurDevice(), jid);
}
 
Example 10
Source File: BareJidEncoder.java    From Smack with Apache License 2.0 5 votes vote down vote up
@Override
public BareJid decode(String string) {
    try {
        return JidCreate.bareFrom(string);
    } catch (XmppStringprepException e) {
        throw new IllegalArgumentException("BareJid cannot be decoded.", e);
    }
}
 
Example 11
Source File: UserManager.java    From Spark with Apache License 2.0 5 votes vote down vote up
/**
 * @deprecated use {@link #getUserNicknameFromJID(BareJid)} instead.
 */
@Deprecated
public String getUserNicknameFromJID(CharSequence jid) {
    BareJid bareJid;
    try {
        bareJid = JidCreate.bareFrom(jid);
    } catch (XmppStringprepException e) {
        throw new IllegalStateException(e);
    }
    return getUserNicknameFromJID(bareJid);
}
 
Example 12
Source File: Jid.java    From Conversations with GNU General Public License v3.0 5 votes vote down vote up
static Jid ofLocalAndDomain(CharSequence local, CharSequence domain) {
    try {
        return new WrappedJid(
                JidCreate.bareFrom(
                        Localpart.fromUnescaped(local.toString()),
                        Domainpart.from(domain.toString())
                )
        );
    } catch (XmppStringprepException e) {
        throw new IllegalArgumentException(e);
    }
}
 
Example 13
Source File: OmemoServiceTest.java    From Smack with Apache License 2.0 5 votes vote down vote up
@Test
public void removeOurDeviceTest() throws XmppStringprepException {
    OmemoDevice a = new OmemoDevice(JidCreate.bareFrom("[email protected]"), 123);
    OmemoDevice b = new OmemoDevice(JidCreate.bareFrom("[email protected]"), 124);

    HashSet<OmemoDevice> devices = new HashSet<>();
    devices.add(a); devices.add(b);

    assertTrue(devices.contains(a));
    assertTrue(devices.contains(b));
    OmemoService.removeOurDevice(a, devices);

    assertFalse(devices.contains(a));
    assertTrue(devices.contains(b));
}
 
Example 14
Source File: Jid.java    From Conversations with GNU General Public License v3.0 5 votes vote down vote up
static Jid ofLocalAndDomainEscaped(CharSequence local, CharSequence domain) {
    try {
        return new WrappedJid(
                JidCreate.bareFrom(
                        Localpart.from(local.toString()),
                        Domainpart.from(domain.toString())
                )
        );
    } catch (XmppStringprepException e) {
        throw new IllegalArgumentException(e);
    }
}
 
Example 15
Source File: Jid.java    From Pix-Art-Messenger with GNU General Public License v3.0 5 votes vote down vote up
static Jid ofLocalAndDomainEscaped(CharSequence local, CharSequence domain) {
    try {
        return new WrappedJid(
                JidCreate.bareFrom(
                        Localpart.from(local.toString()),
                        Domainpart.from(domain.toString())
                )
        );
    } catch (XmppStringprepException e) {
        throw new IllegalArgumentException(e);
    }
}
 
Example 16
Source File: Jid.java    From Pix-Art-Messenger with GNU General Public License v3.0 5 votes vote down vote up
static Jid ofLocalAndDomain(CharSequence local, CharSequence domain) {
    try {
        return new WrappedJid(
                JidCreate.bareFrom(
                        Localpart.fromUnescaped(local.toString()),
                        Domainpart.from(domain.toString())
                )
        );
    } catch (XmppStringprepException e) {
        throw new IllegalArgumentException(e);
    }
}
 
Example 17
Source File: UserManager.java    From Spark with Apache License 2.0 5 votes vote down vote up
public static String unescapeJID(CharSequence jid) {
    BareJid bareJid;
    try {
        bareJid = JidCreate.bareFrom(jid);
    } catch (XmppStringprepException e) {
        throw new IllegalStateException(e);
    }
    return unescapeJID(bareJid);
}
 
Example 18
Source File: OmemoStoreTest.java    From Smack with Apache License 2.0 4 votes vote down vote up
OmemoStoreTest(OmemoStore<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_Sess, T_Addr, T_ECPub, T_Bundle, T_Ciph> store)
        throws XmppStringprepException {
    this.store = store;
    alice = new OmemoDevice(JidCreate.bareFrom("[email protected]"), 123);
    bob = new OmemoDevice(JidCreate.bareFrom("[email protected]"), 987);
}
 
Example 19
Source File: SubscriptionPreApprovalTest.java    From Smack with Apache License 2.0 4 votes vote down vote up
@Test
public void testPreApproveAndCreate() throws Throwable {
    final BareJid contactJID = JidCreate.bareFrom("[email protected]");
    final String contactName = "PreApproval";
    final String[] contactGroup = {};
    connection.enableStreamFeature(SubscriptionPreApproval.INSTANCE);

    final PreApproveAndCreateEntryResponder serverSimulator = new PreApproveAndCreateEntryResponder() {
        @Override
        void verifyRosterUpdateRequest(final RosterPacket updateRequest) {
            final Item item = updateRequest.getRosterItems().iterator().next();
            assertSame("The provided JID doesn't match the requested!",
                    contactJID,
                    item.getJid());
            assertSame("The provided name doesn't match the requested!",
                    contactName,
                    item.getName());
            assertSame("The provided group number doesn't match the requested!",
                    0,
                    item.getGroupNames().size());
        }

        @Override
        void verifyPreApprovalRequest(Presence preApproval) {
            assertSame("The provided name doesn't match the requested!",
                    contactJID,
                    preApproval.getTo());
            assertSame("The provided presence type is incorrect!",
                    Presence.Type.subscribed,
                    preApproval.getType());
        }
    };
    serverSimulator.start();
    roster.preApproveAndCreateEntry(contactJID, contactName, contactGroup);
    serverSimulator.join();

    // Check if an error occurred within the simulator
    final Throwable exception = serverSimulator.getException();
    if (exception != null) {
        throw exception;
    }
    rosterListener.waitUntilInvocationOrTimeout();

    // Verify the roster entry of the new contact
    final RosterEntry addedEntry = roster.getEntry(contactJID);
    assertNotNull("The new contact wasn't added to the roster!", addedEntry);
    assertTrue("The roster listener wasn't invoked for the new contact!",
            rosterListener.getAddedAddresses().contains(contactJID));
    assertSame("Setup wrong name for the new contact!",
            contactName,
            addedEntry.getName());
    assertSame("Setup wrong default subscription status!",
            ItemType.none,
            addedEntry.getType());
    assertSame("The new contact should be member of exactly one group!",
            0,
            addedEntry.getGroups().size());
}
 
Example 20
Source File: XmppConnection.java    From Zom-Android-XMPP with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void approveSubscriptionRequest(final Contact contact) {


    try {

        BareJid bareJid = JidCreate.bareFrom(contact.getAddress().getBareAddress());
        RosterEntry entry = mRoster.getEntry(bareJid);
        if (entry == null)
            mRoster.createEntry(bareJid,contact.getName(),null);

        entry = mRoster.getEntry(bareJid);

        int subType = Imps.Contacts.SUBSCRIPTION_TYPE_BOTH;
        int subStatus = Imps.Contacts.SUBSCRIPTION_STATUS_NONE;

        org.jivesoftware.smack.packet.Presence response = new org.jivesoftware.smack.packet.Presence(
                org.jivesoftware.smack.packet.Presence.Type.subscribed);
        response.setTo(bareJid);

        //send now, or queue
        if (mConnection != null && mConnection.isAuthenticated())
            mConnection.sendStanza(response);
        else
            sendPacket(response);

        /**
        if (!entry.canSeeHisPresence()) {

            //send now, or queue
            if (mConnection != null && mConnection.isAuthenticated())
                mRoster.sendSubscriptionRequest(bareJid);
            else {
                org.jivesoftware.smack.packet.Presence request = new org.jivesoftware.smack.packet.Presence(
                        org.jivesoftware.smack.packet.Presence.Type.subscribe);
                request.setTo(bareJid);
                sendPacket(request);
            }

            subType = Imps.Contacts.SUBSCRIPTION_TYPE_FROM;
        }**/

        contact.setSubscriptionStatus(subStatus);
        contact.setSubscriptionType(subType);

        mContactListManager.getSubscriptionRequestListener().onSubscriptionApproved(contact, mProviderId, mAccountId);
        getSubscriptionRequestListener().onSubScriptionChanged(contact,mProviderId,mAccountId,subType,subStatus);

        ChatSession session = findOrCreateSession(contact.getAddress().toString(), false, true);

        if (session != null)
            session.setSubscribed(true);

        if (entry != null && entry.canSeeHisPresence()) {

            requestPresenceRefresh(contact.getAddress().getBareAddress());
            qAvatar.put(contact.getAddress().getAddress(),"");

            if (getOmemo().getManager().contactSupportsOmemo(bareJid)) {
                getOmemo().getManager().requestDeviceListUpdateFor(bareJid);
                getOmemo().getManager().buildSessionsWith(bareJid);
            }
        }


    }
    catch (Exception e) {
        debug (TAG, "error responding to subscription approval: " + e.getLocalizedMessage());

    }

}