Search in sources :

Example 1 with OmemoCachedDeviceList

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

the class OmemoService method deleteStaleDevices.

/**
 * Return a copy of our deviceList, but with stale devices marked as inactive.
 * Never mark our own device as stale.
 * This method ignores {@link OmemoConfiguration#getDeleteStaleDevices()}!
 *
 * In this case, a stale device is one of our devices, from which we haven't received an OMEMO message from
 * for more than {@link OmemoConfiguration#getDeleteStaleDevicesAfterHours()} hours.
 *
 * @param userDevice our OmemoDevice
 * @return our altered deviceList with stale devices marked as inactive.
 *
 * @throws IOException if an I/O error occurred.
 */
private OmemoCachedDeviceList deleteStaleDevices(OmemoDevice userDevice) throws IOException {
    OmemoCachedDeviceList deviceList = getOmemoStoreBackend().loadCachedDeviceList(userDevice);
    int maxAgeHours = OmemoConfiguration.getDeleteStaleDevicesAfterHours();
    return removeStaleDevicesFromDeviceList(userDevice, userDevice.getJid(), deviceList, maxAgeHours);
}
Also used : OmemoCachedDeviceList(org.jivesoftware.smackx.omemo.internal.OmemoCachedDeviceList) OmemoFingerprint(org.jivesoftware.smackx.omemo.trust.OmemoFingerprint)

Example 2 with OmemoCachedDeviceList

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

the class OmemoService method removeStaleDevicesFromDeviceList.

/**
 * Return a copy of the given deviceList of user contact, but with stale devices marked as inactive.
 * Never mark our own device as stale. If we haven't yet received a message from a device, store the current date
 * as last date of message receipt to allow future decisions.
 *
 * A stale device is a device, from which we haven't received an OMEMO message from for more than
 * "maxAgeMillis" milliseconds.
 *
 * @param userDevice our OmemoDevice.
 * @param contact subjects BareJid.
 * @param contactsDeviceList subjects deviceList.
 * @return copy of subjects deviceList with stale devices marked as inactive.
 *
 * @throws IOException if an I/O error occurred.
 */
private OmemoCachedDeviceList removeStaleDevicesFromDeviceList(OmemoDevice userDevice, BareJid contact, OmemoCachedDeviceList contactsDeviceList, int maxAgeHours) throws IOException {
    // Don't work on original list.
    OmemoCachedDeviceList deviceList = new OmemoCachedDeviceList(contactsDeviceList);
    // Iterate through original list, but modify copy instead
    for (int deviceId : contactsDeviceList.getActiveDevices()) {
        OmemoDevice device = new OmemoDevice(contact, deviceId);
        Date lastDeviceIdPublication = getOmemoStoreBackend().getDateOfLastDeviceIdPublication(userDevice, device);
        if (lastDeviceIdPublication == null) {
            lastDeviceIdPublication = new Date();
            getOmemoStoreBackend().setDateOfLastDeviceIdPublication(userDevice, device, lastDeviceIdPublication);
        }
        Date lastMessageReceived = getOmemoStoreBackend().getDateOfLastReceivedMessage(userDevice, device);
        if (lastMessageReceived == null) {
            lastMessageReceived = new Date();
            getOmemoStoreBackend().setDateOfLastReceivedMessage(userDevice, device, lastMessageReceived);
        }
        boolean stale = isStale(userDevice, device, lastDeviceIdPublication, maxAgeHours);
        stale &= isStale(userDevice, device, lastMessageReceived, maxAgeHours);
        if (stale) {
            deviceList.addInactiveDevice(deviceId);
        }
    }
    return deviceList;
}
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 3 with OmemoCachedDeviceList

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

the class OmemoService method refreshAndRepublishDeviceList.

/**
 * Refresh our own device list and publish it to the server.
 *
 * @param connection XMPPConnection
 * @param userDevice our OMEMO device
 *
 * @throws InterruptedException if the calling thread was interrupted.
 * @throws PubSubException.NotALeafNodeException if a PubSub leaf node operation was attempted on a non-leaf node.
 * @throws XMPPException.XMPPErrorException if there was an XMPP error returned.
 * @throws SmackException.NotConnectedException if the XMPP connection is not connected.
 * @throws SmackException.NoResponseException if there was no response from the remote entity.
 * @throws IOException if an I/O error occurred.
 */
private void refreshAndRepublishDeviceList(XMPPConnection connection, OmemoDevice userDevice) throws InterruptedException, PubSubException.NotALeafNodeException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, SmackException.NoResponseException, IOException {
    // refreshOmemoDeviceList;
    OmemoDeviceListElement publishedList;
    try {
        publishedList = fetchDeviceList(connection, userDevice.getJid());
    } catch (PubSubException.NotAPubSubNodeException e) {
        // Node is not a PubSub node. This might happen on some ejabberd servers.
        publishedList = null;
    } catch (XMPPException.XMPPErrorException e) {
        if (e.getStanzaError().getCondition() == StanzaError.Condition.item_not_found) {
            // Items not found -> items do not exist
            publishedList = null;
        } else {
            // Some other error -> throw
            throw e;
        }
    }
    if (publishedList == null) {
        publishedList = new OmemoDeviceListElement_VAxolotl(Collections.<Integer>emptySet());
    }
    getOmemoStoreBackend().mergeCachedDeviceList(userDevice, userDevice.getJid(), publishedList);
    OmemoCachedDeviceList cachedList = cleanUpDeviceList(userDevice);
    // Republish our deviceId if it is missing from the published list.
    if (!publishedList.getDeviceIds().equals(cachedList.getActiveDevices())) {
        publishDeviceList(connection, new OmemoDeviceListElement_VAxolotl(cachedList));
    }
}
Also used : OmemoDeviceListElement_VAxolotl(org.jivesoftware.smackx.omemo.element.OmemoDeviceListElement_VAxolotl) OmemoDeviceListElement(org.jivesoftware.smackx.omemo.element.OmemoDeviceListElement) PubSubException(org.jivesoftware.smackx.pubsub.PubSubException) XMPPException(org.jivesoftware.smack.XMPPException) OmemoCachedDeviceList(org.jivesoftware.smackx.omemo.internal.OmemoCachedDeviceList)

Example 4 with OmemoCachedDeviceList

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

the class OmemoManager method purgeEverything.

public List<Exception> purgeEverything() throws NotConnectedException, InterruptedException, IOException {
    List<Exception> exceptions = new ArrayList<>(5);
    PubSubManager pm = PubSubManager.getInstanceFor(getConnection(), getOwnJid());
    try {
        requestDeviceListUpdateFor(getOwnJid());
    } catch (SmackException.NoResponseException | PubSubException.NotALeafNodeException | XMPPException.XMPPErrorException e) {
        exceptions.add(e);
    }
    OmemoCachedDeviceList deviceList = OmemoService.getInstance().getOmemoStoreBackend().loadCachedDeviceList(getOwnDevice(), getOwnJid());
    for (int id : deviceList.getAllDevices()) {
        try {
            pm.getLeafNode(OmemoConstants.PEP_NODE_BUNDLE_FROM_DEVICE_ID(id)).deleteAllItems();
        } catch (SmackException.NoResponseException | PubSubException.NotALeafNodeException | XMPPException.XMPPErrorException | PubSubException.NotAPubSubNodeException e) {
            exceptions.add(e);
        }
        try {
            pm.deleteNode(OmemoConstants.PEP_NODE_BUNDLE_FROM_DEVICE_ID(id));
        } catch (SmackException.NoResponseException | XMPPException.XMPPErrorException e) {
            exceptions.add(e);
        }
    }
    try {
        pm.getLeafNode(OmemoConstants.PEP_NODE_DEVICE_LIST).deleteAllItems();
    } catch (SmackException.NoResponseException | PubSubException.NotALeafNodeException | XMPPException.XMPPErrorException | PubSubException.NotAPubSubNodeException e) {
        exceptions.add(e);
    }
    try {
        pm.deleteNode(OmemoConstants.PEP_NODE_DEVICE_LIST);
    } catch (SmackException.NoResponseException | XMPPException.XMPPErrorException e) {
        exceptions.add(e);
    }
    return exceptions;
}
Also used : ArrayList(java.util.ArrayList) OmemoCachedDeviceList(org.jivesoftware.smackx.omemo.internal.OmemoCachedDeviceList) SmackException(org.jivesoftware.smack.SmackException) NoRawSessionException(org.jivesoftware.smackx.omemo.exceptions.NoRawSessionException) UndecidedOmemoIdentityException(org.jivesoftware.smackx.omemo.exceptions.UndecidedOmemoIdentityException) PubSubException(org.jivesoftware.smackx.pubsub.PubSubException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) XMPPException(org.jivesoftware.smack.XMPPException) NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) NoOmemoSupportException(org.jivesoftware.smackx.omemo.exceptions.NoOmemoSupportException) IOException(java.io.IOException) CryptoFailedException(org.jivesoftware.smackx.omemo.exceptions.CryptoFailedException) CorruptedOmemoKeyException(org.jivesoftware.smackx.omemo.exceptions.CorruptedOmemoKeyException) CannotEstablishOmemoSessionException(org.jivesoftware.smackx.omemo.exceptions.CannotEstablishOmemoSessionException) OmemoFingerprint(org.jivesoftware.smackx.omemo.trust.OmemoFingerprint) StoreHint(org.jivesoftware.smackx.hints.element.StoreHint) PubSubManager(org.jivesoftware.smackx.pubsub.PubSubManager)

Example 5 with OmemoCachedDeviceList

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

the class OmemoManager method getDevicesOf.

/**
 * Return a set of all OMEMO capable devices of a contact.
 * Note, that this method does not explicitly refresh the device list of the contact, so it might be outdated.
 *
 * @see #requestDeviceListUpdateFor(BareJid)
 *
 * @param contact contact we want to get a set of device of.
 * @return set of known devices of that contact.
 *
 * @throws IOException if an I/O error occurred.
 */
public Set<OmemoDevice> getDevicesOf(BareJid contact) throws IOException {
    OmemoCachedDeviceList list = getOmemoService().getOmemoStoreBackend().loadCachedDeviceList(getOwnDevice(), contact);
    HashSet<OmemoDevice> devices = new HashSet<>();
    for (int deviceId : list.getActiveDevices()) {
        devices.add(new OmemoDevice(contact, deviceId));
    }
    return devices;
}
Also used : OmemoDevice(org.jivesoftware.smackx.omemo.internal.OmemoDevice) OmemoCachedDeviceList(org.jivesoftware.smackx.omemo.internal.OmemoCachedDeviceList) OmemoFingerprint(org.jivesoftware.smackx.omemo.trust.OmemoFingerprint) StoreHint(org.jivesoftware.smackx.hints.element.StoreHint) HashSet(java.util.HashSet)

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