1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-22 06:12:05 +01:00

[extensions] Deprecate old-style PepManager PEP listeners

This commit is contained in:
Florian Schmaus 2020-05-25 20:32:47 +02:00
parent 15499ad1f8
commit ccc785062e
4 changed files with 48 additions and 94 deletions

View file

@ -253,7 +253,10 @@ public final class PepManager extends Manager {
* *
* @param pepListener a roster exchange listener. * @param pepListener a roster exchange listener.
* @return true if pepListener was added. * @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) { public boolean addPepListener(PepListener pepListener) {
return pepListeners.add(pepListener); return pepListeners.add(pepListener);
} }
@ -263,7 +266,10 @@ public final class PepManager extends Manager {
* *
* @param pepListener a roster exchange listener. * @param pepListener a roster exchange listener.
* @return true, if pepListener was removed. * @return true, if pepListener was removed.
* @deprecated use {@link #removePepEventListener(PepEventListener)} instead.
*/ */
// TODO: Remove in Smack 4.5.
@Deprecated
public boolean removePepListener(PepListener pepListener) { public boolean removePepListener(PepListener pepListener) {
return pepListeners.remove(pepListener); return pepListeners.remove(pepListener);
} }

View file

@ -17,7 +17,6 @@
package org.jivesoftware.smackx.omemo; 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.OMEMO_NAMESPACE_V_AXOLOTL;
import static org.jivesoftware.smackx.omemo.util.OmemoConstants.PEP_NODE_DEVICE_LIST_NOTIFY;
import java.io.IOException; import java.io.IOException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
@ -39,7 +38,6 @@ import org.jivesoftware.smack.Manager;
import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.MessageBuilder; import org.jivesoftware.smack.packet.MessageBuilder;
import org.jivesoftware.smack.packet.Stanza; 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.OmemoTrustCallback;
import org.jivesoftware.smackx.omemo.trust.TrustState; import org.jivesoftware.smackx.omemo.trust.TrustState;
import org.jivesoftware.smackx.omemo.util.MessageOrOmemoMessage; 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.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.PubSubException;
import org.jivesoftware.smackx.pubsub.packet.PubSub; import org.jivesoftware.smackx.pubsub.packet.PubSub;
@ -101,6 +98,8 @@ public final class OmemoManager extends Manager {
private final HashSet<OmemoMessageListener> omemoMessageListeners = new HashSet<>(); private final HashSet<OmemoMessageListener> omemoMessageListeners = new HashSet<>();
private final HashSet<OmemoMucMessageListener> omemoMucMessageListeners = new HashSet<>(); private final HashSet<OmemoMucMessageListener> omemoMucMessageListeners = new HashSet<>();
private final PepManager pepManager;
private OmemoTrustCallback trustCallback; private OmemoTrustCallback trustCallback;
private BareJid ownJid; private BareJid ownJid;
@ -116,6 +115,7 @@ public final class OmemoManager extends Manager {
super(connection); super(connection);
service = OmemoService.getInstance(); service = OmemoService.getInstance();
pepManager = PepManager.getInstanceFor(connection);
this.deviceId = deviceId; this.deviceId = deviceId;
@ -134,9 +134,6 @@ public final class OmemoManager extends Manager {
// StanzaListeners // StanzaListeners
resumeStanzaAndPEPListeners(); 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)); 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. * after {@link #stopStanzaAndPEPListeners()} was called.
*/ */
public void resumeStanzaAndPEPListeners() { public void resumeStanzaAndPEPListeners() {
PepManager pepManager = PepManager.getInstanceFor(connection());
CarbonManager carbonManager = CarbonManager.getInstanceFor(connection()); CarbonManager carbonManager = CarbonManager.getInstanceFor(connection());
// Remove listeners to avoid them getting added twice // Remove listeners to avoid them getting added twice
connection().removeAsyncStanzaListener(this::internalOmemoMessageStanzaListener); connection().removeAsyncStanzaListener(this::internalOmemoMessageStanzaListener);
carbonManager.removeCarbonCopyReceivedListener(this::internalOmemoCarbonCopyListener); carbonManager.removeCarbonCopyReceivedListener(this::internalOmemoCarbonCopyListener);
pepManager.removePepListener(this::deviceListUpdateListener);
// Add listeners // Add listeners
pepManager.addPepListener(this::deviceListUpdateListener); pepManager.addPepEventListener(OmemoConstants.PEP_NODE_DEVICE_LIST, OmemoDeviceListElement.class, pepOmemoDeviceListEventListener);
connection().addAsyncStanzaListener(this::internalOmemoMessageStanzaListener, OmemoManager::isOmemoMessage); connection().addAsyncStanzaListener(this::internalOmemoMessageStanzaListener, OmemoManager::isOmemoMessage);
carbonManager.addCarbonCopyReceivedListener(this::internalOmemoCarbonCopyListener); carbonManager.addCarbonCopyReceivedListener(this::internalOmemoCarbonCopyListener);
} }
@ -907,7 +901,7 @@ public final class OmemoManager extends Manager {
* Remove active stanza listeners needed for OMEMO. * Remove active stanza listeners needed for OMEMO.
*/ */
public void stopStanzaAndPEPListeners() { public void stopStanzaAndPEPListeners() {
PepManager.getInstanceFor(connection()).removePepListener(this::deviceListUpdateListener); pepManager.removePepEventListener(pepOmemoDeviceListEventListener);
connection().removeAsyncStanzaListener(this::internalOmemoMessageStanzaListener); connection().removeAsyncStanzaListener(this::internalOmemoMessageStanzaListener);
CarbonManager.getInstanceFor(connection()).removeCarbonCopyReceivedListener(this::internalOmemoCarbonCopyListener); CarbonManager.getInstanceFor(connection()).removeCarbonCopyReceivedListener(this::internalOmemoCarbonCopyListener);
} }
@ -992,71 +986,45 @@ public final class OmemoManager extends Manager {
}); });
} }
/** @SuppressWarnings("UnnecessaryLambda")
* PEPListener that listens for OMEMO deviceList updates. private final PepEventListener<OmemoDeviceListElement> pepOmemoDeviceListEventListener =
*/ (from, receivedDeviceList, id, message) -> {
private void deviceListUpdateListener(EntityBareJid from, EventElement event, Message message) { // Device List <list>
// Unknown sender, no more work to do. OmemoCachedDeviceList deviceList;
if (from == null) { try {
// TODO: This DOES happen for some reason. Figure out when... getOmemoService().getOmemoStoreBackend().mergeCachedDeviceList(getOwnDevice(), from,
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 <list>
OmemoCachedDeviceList deviceList;
OmemoDeviceListElement receivedDeviceList = (OmemoDeviceListElement) payloadItem.getPayload();
try {
getOmemoService().getOmemoStoreBackend().mergeCachedDeviceList(getOwnDevice(), from,
receivedDeviceList); receivedDeviceList);
if (!from.asBareJid().equals(getOwnJid())) { if (!from.asBareJid().equals(getOwnJid())) {
continue; return;
} }
deviceList = getOmemoService().cleanUpDeviceList(getOwnDevice()); deviceList = getOmemoService().cleanUpDeviceList(getOwnDevice());
} catch (IOException e) { } catch (IOException e) {
LOGGER.log(Level.SEVERE, LOGGER.log(Level.SEVERE,
"IOException while processing OMEMO PEP device updates. Message: " + message, "IOException while processing OMEMO PEP device updates. Message: " + message,
e); e);
continue; return;
} }
final OmemoDeviceListElement_VAxolotl newDeviceList = new OmemoDeviceListElement_VAxolotl(deviceList); final OmemoDeviceListElement_VAxolotl newDeviceList = new OmemoDeviceListElement_VAxolotl(deviceList);
if (!newDeviceList.copyDeviceIds().equals(receivedDeviceList.copyDeviceIds())) { if (!newDeviceList.copyDeviceIds().equals(receivedDeviceList.copyDeviceIds())) {
LOGGER.log(Level.FINE, "Republish deviceList due to changes:" + LOGGER.log(Level.FINE, "Republish deviceList due to changes:" +
" Received: " + Arrays.toString(receivedDeviceList.copyDeviceIds().toArray()) + " Received: " + Arrays.toString(receivedDeviceList.copyDeviceIds().toArray()) +
" Published: " + Arrays.toString(newDeviceList.copyDeviceIds().toArray())); " Published: " + Arrays.toString(newDeviceList.copyDeviceIds().toArray()));
Async.go(new Runnable() { Async.go(new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
OmemoService.publishDeviceList(connection(), newDeviceList); OmemoService.publishDeviceList(connection(), newDeviceList);
} catch (InterruptedException | XMPPException.XMPPErrorException | } catch (InterruptedException | XMPPException.XMPPErrorException |
SmackException.NotConnectedException | SmackException.NoResponseException | PubSubException.NotALeafNodeException e) { 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. * StanzaFilter that filters messages containing a OMEMO element.

View file

@ -31,7 +31,6 @@ public final class OmemoConstants {
// PubSub Node names // PubSub Node names
public static final String PEP_NODE_DEVICE_LIST = OMEMO_NAMESPACE_V_AXOLOTL + ".devicelist"; 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"; public static final String PEP_NODE_BUNDLES = OMEMO_NAMESPACE_V_AXOLOTL + ".bundles";
/** /**

View file

@ -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.store.definition.OpenPgpTrustStore;
import org.jivesoftware.smackx.ox.util.OpenPgpPubSubUtil; import org.jivesoftware.smackx.ox.util.OpenPgpPubSubUtil;
import org.jivesoftware.smackx.ox.util.SecretKeyBackupHelper; import org.jivesoftware.smackx.ox.util.SecretKeyBackupHelper;
import org.jivesoftware.smackx.pep.PepEventListener;
import org.jivesoftware.smackx.pep.PepListener; import org.jivesoftware.smackx.pep.PepListener;
import org.jivesoftware.smackx.pep.PepManager; 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.LeafNode;
import org.jivesoftware.smackx.pubsub.PayloadItem;
import org.jivesoftware.smackx.pubsub.PubSubException; import org.jivesoftware.smackx.pubsub.PubSubException;
import org.jivesoftware.smackx.pubsub.PubSubFeature; import org.jivesoftware.smackx.pubsub.PubSubFeature;
@ -172,6 +170,9 @@ public final class OpenPgpManager extends Manager {
private final Set<SignElementReceivedListener> signElementReceivedListeners = new HashSet<>(); private final Set<SignElementReceivedListener> signElementReceivedListeners = new HashSet<>();
private final Set<CryptElementReceivedListener> cryptElementReceivedListeners = new HashSet<>(); private final Set<CryptElementReceivedListener> cryptElementReceivedListeners = new HashSet<>();
@SuppressWarnings("UnnecessaryLambda")
private final PepEventListener<PublicKeysListElement> pepPublicKeyListElementListener = (from, listElement, id, message) -> processPublicKeysListElement(from, listElement);;
/** /**
* Private constructor to avoid instantiation without putting the object into {@code INSTANCES}. * 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); publishPublicKey(pepManager, pubkeyElement, primaryFingerprint);
// Subscribe to public key changes // Subscribe to public key changes
PepManager.getInstanceFor(connection()).addPepListener(this::metadataListener); pepManager.addPepEventListener(PEP_NODE_PUBLIC_KEYS, PublicKeysListElement.class, pepPublicKeyListElementListener);
ServiceDiscoveryManager.getInstanceFor(connection()) ServiceDiscoveryManager.getInstanceFor(connection())
.addFeature(PEP_NODE_PUBLIC_KEYS_NOTIFY); .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. * Remove the metadata listener. This method is mainly used in tests.
*/ */
public void stopMetadataListener() { public void stopMetadataListener() {
PepManager.getInstanceFor(connection()).removePepListener(this::metadataListener); pepManager.removePepEventListener(pepPublicKeyListElementListener);
} }
/** /**
@ -496,26 +497,6 @@ public final class OpenPgpManager extends Manager {
Private stuff. Private stuff.
*/ */
/**
* {@link PepListener} that listens for changes to the OX public keys metadata node.
*
* @see <a href="https://xmpp.org/extensions/xep-0373.html#pubsub-notifications">XEP-0373 §4.4</a>
*/
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) { private void processPublicKeysListElement(BareJid contact, PublicKeysListElement listElement) {
OpenPgpContact openPgpContact = getOpenPgpContact(contact.asEntityBareJidIfPossible()); OpenPgpContact openPgpContact = getOpenPgpContact(contact.asEntityBareJidIfPossible());
try { try {