Search in sources :

Example 6 with OmemoCachedDeviceList

use of org.jivesoftware.smackx.omemo.internal.OmemoCachedDeviceList in project Smack by igniterealtime.

the class FileBasedOmemoStore method loadCachedDeviceList.

@Override
public OmemoCachedDeviceList loadCachedDeviceList(OmemoDevice userDevice, BareJid contact) throws IOException {
    OmemoCachedDeviceList cachedDeviceList = new OmemoCachedDeviceList();
    if (contact == null) {
        throw new IllegalArgumentException("Contact can not be null.");
    }
    // active
    File activeDevicesPath = hierarchy.getContactsActiveDevicesPath(userDevice, contact);
    Set<Integer> active = readIntegers(activeDevicesPath);
    if (active != null) {
        cachedDeviceList.getActiveDevices().addAll(active);
    }
    // inactive
    File inactiveDevicesPath = hierarchy.getContactsInactiveDevicesPath(userDevice, contact);
    Set<Integer> inactive = readIntegers(inactiveDevicesPath);
    if (inactive != null) {
        cachedDeviceList.getInactiveDevices().addAll(inactive);
    }
    return cachedDeviceList;
}
Also used : OmemoCachedDeviceList(org.jivesoftware.smackx.omemo.internal.OmemoCachedDeviceList) File(java.io.File)

Example 7 with OmemoCachedDeviceList

use of org.jivesoftware.smackx.omemo.internal.OmemoCachedDeviceList in project Smack by igniterealtime.

the class OmemoStore method mergeCachedDeviceList.

/**
 * Merge the received OmemoDeviceListElement with the one we already have. If we had none, the received one is saved.
 *
 * @param userDevice our OmemoDevice.
 * @param contact Contact we received the list from.
 * @param list    List we received.
 *
 * @throws IOException if an I/O error occurred.
 */
OmemoCachedDeviceList mergeCachedDeviceList(OmemoDevice userDevice, BareJid contact, OmemoDeviceListElement list) throws IOException {
    OmemoCachedDeviceList cached = loadCachedDeviceList(userDevice, contact);
    if (cached == null) {
        cached = new OmemoCachedDeviceList();
    }
    if (list == null) {
        return cached;
    }
    for (int devId : list.getDeviceIds()) {
        if (!cached.contains(devId)) {
            setDateOfLastDeviceIdPublication(userDevice, new OmemoDevice(contact, devId), new Date());
        }
    }
    cached.merge(list.getDeviceIds());
    storeCachedDeviceList(userDevice, contact, cached);
    return cached;
}
Also used : OmemoDevice(org.jivesoftware.smackx.omemo.internal.OmemoDevice) OmemoCachedDeviceList(org.jivesoftware.smackx.omemo.internal.OmemoCachedDeviceList) OmemoFingerprint(org.jivesoftware.smackx.omemo.trust.OmemoFingerprint) Date(java.util.Date)

Example 8 with OmemoCachedDeviceList

use of org.jivesoftware.smackx.omemo.internal.OmemoCachedDeviceList in project Smack by igniterealtime.

the class OmemoStoreTest method loadStoreCachedDeviceList.

@Test
public void loadStoreCachedDeviceList() throws IOException {
    Integer[] active = new Integer[] { 1, 5, 999, 10 };
    Integer[] inactive = new Integer[] { 6, 7, 8 };
    OmemoCachedDeviceList before = new OmemoCachedDeviceList(new HashSet<>(Arrays.asList(active)), new HashSet<>(Arrays.asList(inactive)));
    assertNotNull("Loading a non-existent cached deviceList must return an empty list.", store.loadCachedDeviceList(alice, bob.getJid()));
    store.storeCachedDeviceList(alice, bob.getJid(), before);
    OmemoCachedDeviceList after = store.loadCachedDeviceList(alice, bob.getJid());
    assertTrue("Loaded deviceList must not be empty", after.getAllDevices().size() != 0);
    assertEquals("Number of entries in active devices must match.", active.length, after.getActiveDevices().size());
    assertEquals("Number of entries in inactive devices must match.", inactive.length, after.getInactiveDevices().size());
    assertEquals("Number of total entries must match.", active.length + inactive.length, after.getAllDevices().size());
    for (Integer a : active) {
        assertTrue(after.getActiveDevices().contains(a));
        assertTrue(after.getAllDevices().contains(a));
    }
    for (Integer i : inactive) {
        assertTrue(after.getInactiveDevices().contains(i));
        assertTrue(after.getAllDevices().contains(i));
    }
    store.storeCachedDeviceList(alice, bob.getJid(), new OmemoCachedDeviceList());
    assertEquals("DeviceList must be empty after overwriting it with empty list.", 0, store.loadCachedDeviceList(alice, bob.getJid()).getAllDevices().size());
}
Also used : OmemoCachedDeviceList(org.jivesoftware.smackx.omemo.internal.OmemoCachedDeviceList) Test(org.junit.Test)

Example 9 with OmemoCachedDeviceList

use of org.jivesoftware.smackx.omemo.internal.OmemoCachedDeviceList in project Smack by igniterealtime.

the class OmemoService method cleanUpDeviceList.

/**
 * Add our load the deviceList of the user from cache, delete stale devices if needed, add the users device
 * back if necessary, store the refurbished list in cache and return it.
 *
 * @param userDevice our own OMEMO device
 * @return cleaned device list
 *
 * @throws IOException if an I/O error occurred.
 */
OmemoCachedDeviceList cleanUpDeviceList(OmemoDevice userDevice) throws IOException {
    OmemoCachedDeviceList cachedDeviceList;
    // Delete stale devices if allowed and necessary
    if (OmemoConfiguration.getDeleteStaleDevices()) {
        cachedDeviceList = deleteStaleDevices(userDevice);
    } else {
        cachedDeviceList = getOmemoStoreBackend().loadCachedDeviceList(userDevice);
    }
    // Add back our device if necessary
    if (!cachedDeviceList.getActiveDevices().contains(userDevice.getDeviceId())) {
        cachedDeviceList.addDevice(userDevice.getDeviceId());
    }
    getOmemoStoreBackend().storeCachedDeviceList(userDevice, userDevice.getJid(), cachedDeviceList);
    return cachedDeviceList;
}
Also used : OmemoCachedDeviceList(org.jivesoftware.smackx.omemo.internal.OmemoCachedDeviceList)

Example 10 with OmemoCachedDeviceList

use of org.jivesoftware.smackx.omemo.internal.OmemoCachedDeviceList in project Smack by igniterealtime.

the class OmemoManager method getActiveFingerprints.

/**
 * Return all OmemoFingerprints of active devices of a contact.
 * TODO: Make more fail-safe
 *
 * @param contact contact
 * @return Map of all active devices of the contact and their fingerprints.
 *
 * @throws SmackException.NotLoggedInException if the XMPP connection is not authenticated.
 * @throws CorruptedOmemoKeyException if the OMEMO key is corrupted.
 * @throws CannotEstablishOmemoSessionException if no OMEMO session could be established.
 * @throws SmackException.NotConnectedException if the XMPP connection is not connected.
 * @throws InterruptedException if the calling thread was interrupted.
 * @throws SmackException.NoResponseException if there was no response from the remote entity.
 * @throws IOException if an I/O error occurred.
 */
public synchronized HashMap<OmemoDevice, OmemoFingerprint> getActiveFingerprints(BareJid contact) throws SmackException.NotLoggedInException, CorruptedOmemoKeyException, CannotEstablishOmemoSessionException, SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException, IOException {
    if (getOwnJid() == null) {
        throw new SmackException.NotLoggedInException();
    }
    HashMap<OmemoDevice, OmemoFingerprint> fingerprints = new HashMap<>();
    OmemoCachedDeviceList deviceList = getOmemoService().getOmemoStoreBackend().loadCachedDeviceList(getOwnDevice(), contact);
    for (int id : deviceList.getActiveDevices()) {
        OmemoDevice device = new OmemoDevice(contact, id);
        OmemoFingerprint fingerprint = getFingerprint(device);
        if (fingerprint != null) {
            fingerprints.put(device, fingerprint);
        }
    }
    return fingerprints;
}
Also used : OmemoDevice(org.jivesoftware.smackx.omemo.internal.OmemoDevice) HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap) OmemoFingerprint(org.jivesoftware.smackx.omemo.trust.OmemoFingerprint) OmemoCachedDeviceList(org.jivesoftware.smackx.omemo.internal.OmemoCachedDeviceList) OmemoFingerprint(org.jivesoftware.smackx.omemo.trust.OmemoFingerprint) StoreHint(org.jivesoftware.smackx.hints.element.StoreHint)

Aggregations

OmemoCachedDeviceList (org.jivesoftware.smackx.omemo.internal.OmemoCachedDeviceList)12 OmemoFingerprint (org.jivesoftware.smackx.omemo.trust.OmemoFingerprint)6 OmemoDevice (org.jivesoftware.smackx.omemo.internal.OmemoDevice)4 StoreHint (org.jivesoftware.smackx.hints.element.StoreHint)3 Date (java.util.Date)2 HashSet (java.util.HashSet)2 XMPPException (org.jivesoftware.smack.XMPPException)2 PubSubException (org.jivesoftware.smackx.pubsub.PubSubException)2 Test (org.junit.Test)2 File (java.io.File)1 IOException (java.io.IOException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 WeakHashMap (java.util.WeakHashMap)1 SmackException (org.jivesoftware.smack.SmackException)1 NotConnectedException (org.jivesoftware.smack.SmackException.NotConnectedException)1 OmemoDeviceListElement (org.jivesoftware.smackx.omemo.element.OmemoDeviceListElement)1 OmemoDeviceListElement_VAxolotl (org.jivesoftware.smackx.omemo.element.OmemoDeviceListElement_VAxolotl)1 CannotEstablishOmemoSessionException (org.jivesoftware.smackx.omemo.exceptions.CannotEstablishOmemoSessionException)1