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;
}
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;
}
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());
}
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;
}
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;
}
Aggregations