From ccc785062e9b04513302c898e3015c2ff2d7fb91 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Mon, 25 May 2020 20:32:47 +0200 Subject: [PATCH] [extensions] Deprecate old-style PepManager PEP listeners --- .../jivesoftware/smackx/pep/PepManager.java | 6 + .../smackx/omemo/OmemoManager.java | 104 ++++++------------ .../smackx/omemo/util/OmemoConstants.java | 1 - .../smackx/ox/OpenPgpManager.java | 31 +----- 4 files changed, 48 insertions(+), 94 deletions(-) diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/pep/PepManager.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/pep/PepManager.java index 20b009287..404da53fb 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/pep/PepManager.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/pep/PepManager.java @@ -253,7 +253,10 @@ public final class PepManager extends Manager { * * @param pepListener a roster exchange listener. * @return true if pepListener was added. + * @deprecated use {@link #addPepEventListener(String, Class, PepEventListener)} instead. */ + // TODO: Remove in Smack 4.5 + @Deprecated public boolean addPepListener(PepListener pepListener) { return pepListeners.add(pepListener); } @@ -263,7 +266,10 @@ public final class PepManager extends Manager { * * @param pepListener a roster exchange listener. * @return true, if pepListener was removed. + * @deprecated use {@link #removePepEventListener(PepEventListener)} instead. */ + // TODO: Remove in Smack 4.5. + @Deprecated public boolean removePepListener(PepListener pepListener) { return pepListeners.remove(pepListener); } diff --git a/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/OmemoManager.java b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/OmemoManager.java index 88971cae8..35f7f407e 100644 --- a/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/OmemoManager.java +++ b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/OmemoManager.java @@ -17,7 +17,6 @@ package org.jivesoftware.smackx.omemo; import static org.jivesoftware.smackx.omemo.util.OmemoConstants.OMEMO_NAMESPACE_V_AXOLOTL; -import static org.jivesoftware.smackx.omemo.util.OmemoConstants.PEP_NODE_DEVICE_LIST_NOTIFY; import java.io.IOException; import java.security.NoSuchAlgorithmException; @@ -39,7 +38,6 @@ import org.jivesoftware.smack.Manager; import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPException; -import org.jivesoftware.smack.packet.ExtensionElement; import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.MessageBuilder; import org.jivesoftware.smack.packet.Stanza; @@ -71,10 +69,9 @@ import org.jivesoftware.smackx.omemo.trust.OmemoFingerprint; import org.jivesoftware.smackx.omemo.trust.OmemoTrustCallback; import org.jivesoftware.smackx.omemo.trust.TrustState; import org.jivesoftware.smackx.omemo.util.MessageOrOmemoMessage; +import org.jivesoftware.smackx.omemo.util.OmemoConstants; +import org.jivesoftware.smackx.pep.PepEventListener; import org.jivesoftware.smackx.pep.PepManager; -import org.jivesoftware.smackx.pubsub.EventElement; -import org.jivesoftware.smackx.pubsub.ItemsExtension; -import org.jivesoftware.smackx.pubsub.PayloadItem; import org.jivesoftware.smackx.pubsub.PubSubException; import org.jivesoftware.smackx.pubsub.packet.PubSub; @@ -101,6 +98,8 @@ public final class OmemoManager extends Manager { private final HashSet omemoMessageListeners = new HashSet<>(); private final HashSet omemoMucMessageListeners = new HashSet<>(); + private final PepManager pepManager; + private OmemoTrustCallback trustCallback; private BareJid ownJid; @@ -116,6 +115,7 @@ public final class OmemoManager extends Manager { super(connection); service = OmemoService.getInstance(); + pepManager = PepManager.getInstanceFor(connection); this.deviceId = deviceId; @@ -134,9 +134,6 @@ public final class OmemoManager extends Manager { // StanzaListeners resumeStanzaAndPEPListeners(); - - // Announce OMEMO support - ServiceDiscoveryManager.getInstanceFor(connection).addFeature(PEP_NODE_DEVICE_LIST_NOTIFY); } /** @@ -247,7 +244,6 @@ public final class OmemoManager extends Manager { } getOmemoService().init(new LoggedInOmemoManager(this)); - ServiceDiscoveryManager.getInstanceFor(connection()).addFeature(PEP_NODE_DEVICE_LIST_NOTIFY); } /** @@ -889,16 +885,14 @@ public final class OmemoManager extends Manager { * after {@link #stopStanzaAndPEPListeners()} was called. */ public void resumeStanzaAndPEPListeners() { - PepManager pepManager = PepManager.getInstanceFor(connection()); CarbonManager carbonManager = CarbonManager.getInstanceFor(connection()); // Remove listeners to avoid them getting added twice connection().removeAsyncStanzaListener(this::internalOmemoMessageStanzaListener); carbonManager.removeCarbonCopyReceivedListener(this::internalOmemoCarbonCopyListener); - pepManager.removePepListener(this::deviceListUpdateListener); // Add listeners - pepManager.addPepListener(this::deviceListUpdateListener); + pepManager.addPepEventListener(OmemoConstants.PEP_NODE_DEVICE_LIST, OmemoDeviceListElement.class, pepOmemoDeviceListEventListener); connection().addAsyncStanzaListener(this::internalOmemoMessageStanzaListener, OmemoManager::isOmemoMessage); carbonManager.addCarbonCopyReceivedListener(this::internalOmemoCarbonCopyListener); } @@ -907,7 +901,7 @@ public final class OmemoManager extends Manager { * Remove active stanza listeners needed for OMEMO. */ public void stopStanzaAndPEPListeners() { - PepManager.getInstanceFor(connection()).removePepListener(this::deviceListUpdateListener); + pepManager.removePepEventListener(pepOmemoDeviceListEventListener); connection().removeAsyncStanzaListener(this::internalOmemoMessageStanzaListener); CarbonManager.getInstanceFor(connection()).removeCarbonCopyReceivedListener(this::internalOmemoCarbonCopyListener); } @@ -992,71 +986,45 @@ public final class OmemoManager extends Manager { }); } - /** - * PEPListener that listens for OMEMO deviceList updates. - */ - private void deviceListUpdateListener(EntityBareJid from, EventElement event, Message message) { - // Unknown sender, no more work to do. - if (from == null) { - // TODO: This DOES happen for some reason. Figure out when... - return; - } - - for (ExtensionElement items : event.getExtensions()) { - if (!(items instanceof ItemsExtension)) { - continue; - } - - for (ExtensionElement item : ((ItemsExtension) items).getExtensions()) { - if (!(item instanceof PayloadItem)) { - continue; - } - - PayloadItem payloadItem = (PayloadItem) item; - - if (!(payloadItem.getPayload() instanceof OmemoDeviceListElement)) { - continue; - } - - // Device List - OmemoCachedDeviceList deviceList; - OmemoDeviceListElement receivedDeviceList = (OmemoDeviceListElement) payloadItem.getPayload(); - try { - getOmemoService().getOmemoStoreBackend().mergeCachedDeviceList(getOwnDevice(), from, + @SuppressWarnings("UnnecessaryLambda") + private final PepEventListener pepOmemoDeviceListEventListener = + (from, receivedDeviceList, id, message) -> { + // Device List + OmemoCachedDeviceList deviceList; + try { + getOmemoService().getOmemoStoreBackend().mergeCachedDeviceList(getOwnDevice(), from, receivedDeviceList); - if (!from.asBareJid().equals(getOwnJid())) { - continue; - } + if (!from.asBareJid().equals(getOwnJid())) { + return; + } - deviceList = getOmemoService().cleanUpDeviceList(getOwnDevice()); - } catch (IOException e) { - LOGGER.log(Level.SEVERE, + deviceList = getOmemoService().cleanUpDeviceList(getOwnDevice()); + } catch (IOException e) { + LOGGER.log(Level.SEVERE, "IOException while processing OMEMO PEP device updates. Message: " + message, - e); - continue; - } - final OmemoDeviceListElement_VAxolotl newDeviceList = new OmemoDeviceListElement_VAxolotl(deviceList); + e); + return; + } + final OmemoDeviceListElement_VAxolotl newDeviceList = new OmemoDeviceListElement_VAxolotl(deviceList); - if (!newDeviceList.copyDeviceIds().equals(receivedDeviceList.copyDeviceIds())) { - LOGGER.log(Level.FINE, "Republish deviceList due to changes:" + + if (!newDeviceList.copyDeviceIds().equals(receivedDeviceList.copyDeviceIds())) { + LOGGER.log(Level.FINE, "Republish deviceList due to changes:" + " Received: " + Arrays.toString(receivedDeviceList.copyDeviceIds().toArray()) + " Published: " + Arrays.toString(newDeviceList.copyDeviceIds().toArray())); - Async.go(new Runnable() { - @Override - public void run() { - try { - OmemoService.publishDeviceList(connection(), newDeviceList); - } catch (InterruptedException | XMPPException.XMPPErrorException | + Async.go(new Runnable() { + @Override + public void run() { + try { + OmemoService.publishDeviceList(connection(), newDeviceList); + } catch (InterruptedException | XMPPException.XMPPErrorException | SmackException.NotConnectedException | SmackException.NoResponseException | PubSubException.NotALeafNodeException e) { - LOGGER.log(Level.WARNING, "Could not publish our deviceList upon an received update.", e); - } - } - }); + LOGGER.log(Level.WARNING, "Could not publish our deviceList upon an received update.", e); + } } - } + }); } - } + }; /** * StanzaFilter that filters messages containing a OMEMO element. diff --git a/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/util/OmemoConstants.java b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/util/OmemoConstants.java index f91dc199f..352853450 100644 --- a/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/util/OmemoConstants.java +++ b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/util/OmemoConstants.java @@ -31,7 +31,6 @@ public final class OmemoConstants { // PubSub Node names public static final String PEP_NODE_DEVICE_LIST = OMEMO_NAMESPACE_V_AXOLOTL + ".devicelist"; - public static final String PEP_NODE_DEVICE_LIST_NOTIFY = PEP_NODE_DEVICE_LIST + "+notify"; public static final String PEP_NODE_BUNDLES = OMEMO_NAMESPACE_V_AXOLOTL + ".bundles"; /** diff --git a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OpenPgpManager.java b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OpenPgpManager.java index 9cf2783ee..a3a8a7c1b 100644 --- a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OpenPgpManager.java +++ b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OpenPgpManager.java @@ -68,12 +68,10 @@ import org.jivesoftware.smackx.ox.store.definition.OpenPgpStore; import org.jivesoftware.smackx.ox.store.definition.OpenPgpTrustStore; import org.jivesoftware.smackx.ox.util.OpenPgpPubSubUtil; import org.jivesoftware.smackx.ox.util.SecretKeyBackupHelper; +import org.jivesoftware.smackx.pep.PepEventListener; import org.jivesoftware.smackx.pep.PepListener; import org.jivesoftware.smackx.pep.PepManager; -import org.jivesoftware.smackx.pubsub.EventElement; -import org.jivesoftware.smackx.pubsub.ItemsExtension; import org.jivesoftware.smackx.pubsub.LeafNode; -import org.jivesoftware.smackx.pubsub.PayloadItem; import org.jivesoftware.smackx.pubsub.PubSubException; import org.jivesoftware.smackx.pubsub.PubSubFeature; @@ -172,6 +170,9 @@ public final class OpenPgpManager extends Manager { private final Set signElementReceivedListeners = new HashSet<>(); private final Set cryptElementReceivedListeners = new HashSet<>(); + @SuppressWarnings("UnnecessaryLambda") + private final PepEventListener pepPublicKeyListElementListener = (from, listElement, id, message) -> processPublicKeysListElement(from, listElement);; + /** * Private constructor to avoid instantiation without putting the object into {@code INSTANCES}. * @@ -278,7 +279,7 @@ public final class OpenPgpManager extends Manager { publishPublicKey(pepManager, pubkeyElement, primaryFingerprint); // Subscribe to public key changes - PepManager.getInstanceFor(connection()).addPepListener(this::metadataListener); + pepManager.addPepEventListener(PEP_NODE_PUBLIC_KEYS, PublicKeysListElement.class, pepPublicKeyListElementListener); ServiceDiscoveryManager.getInstanceFor(connection()) .addFeature(PEP_NODE_PUBLIC_KEYS_NOTIFY); } @@ -380,7 +381,7 @@ public final class OpenPgpManager extends Manager { * Remove the metadata listener. This method is mainly used in tests. */ public void stopMetadataListener() { - PepManager.getInstanceFor(connection()).removePepListener(this::metadataListener); + pepManager.removePepEventListener(pepPublicKeyListElementListener); } /** @@ -496,26 +497,6 @@ public final class OpenPgpManager extends Manager { Private stuff. */ - /** - * {@link PepListener} that listens for changes to the OX public keys metadata node. - * - * @see XEP-0373 ยง4.4 - */ - private void metadataListener(final EntityBareJid from, final EventElement event, final Message message) { - if (PEP_NODE_PUBLIC_KEYS.equals(event.getEvent().getNode())) { - Async.go(new Runnable() { - @Override - public void run() { - ItemsExtension items = (ItemsExtension) event.getExtensions().get(0); - PayloadItem payload = (PayloadItem) items.getItems().get(0); - PublicKeysListElement listElement = (PublicKeysListElement) payload.getPayload(); - - processPublicKeysListElement(from, listElement); - } - }, "ProcessOXMetadata"); - } - } - private void processPublicKeysListElement(BareJid contact, PublicKeysListElement listElement) { OpenPgpContact openPgpContact = getOpenPgpContact(contact.asEntityBareJidIfPossible()); try {