From d7bc205420bc07578aee49087b3988429e22372b Mon Sep 17 00:00:00 2001 From: Christian Schneppe Date: Tue, 1 Aug 2017 13:19:11 +0200 Subject: use publish-options instead of always pushing node configuration --- .../messenger/crypto/axolotl/AxolotlService.java | 86 ++++++++++++++-------- 1 file changed, 57 insertions(+), 29 deletions(-) (limited to 'src/main/java/de/pixart/messenger/crypto/axolotl') diff --git a/src/main/java/de/pixart/messenger/crypto/axolotl/AxolotlService.java b/src/main/java/de/pixart/messenger/crypto/axolotl/AxolotlService.java index 340f050c5..5dc37e4c9 100644 --- a/src/main/java/de/pixart/messenger/crypto/axolotl/AxolotlService.java +++ b/src/main/java/de/pixart/messenger/crypto/axolotl/AxolotlService.java @@ -46,6 +46,7 @@ import de.pixart.messenger.entities.Message; import de.pixart.messenger.parser.IqParser; import de.pixart.messenger.services.XmppConnectionService; import de.pixart.messenger.utils.CryptoHelper; +import de.pixart.messenger.utils.Namespace; import de.pixart.messenger.utils.SerialSingleThreadExecutor; import de.pixart.messenger.xml.Element; import de.pixart.messenger.xmpp.OnAdvancedStreamFeaturesLoaded; @@ -213,7 +214,6 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded { putDevicesForJid(account.getJid().toBareJid().toPreppedString(), deviceIds, store); for (String address : store.getKnownAddresses()) { deviceIds = store.getSubDeviceSessions(address); - Log.d(Config.LOGTAG, account.getJid().toBareJid() + " adding device ids for " + address + " " + deviceIds); putDevicesForJid(address, deviceIds, store); } @@ -434,16 +434,7 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded { } Set deviceIds = new HashSet<>(); deviceIds.add(getOwnDeviceId()); - IqPacket publish = mXmppConnectionService.getIqGenerator().publishDeviceIds(deviceIds); - Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Wiping all other devices from Pep:" + publish); - mXmppConnectionService.sendIqPacket(account, publish, new OnIqPacketReceived() { - @Override - public void onIqPacketReceived(Account account, IqPacket packet) { - if (packet.getType() == IqPacket.TYPE.RESULT) { - mXmppConnectionService.pushNodeConfiguration(account, AxolotlService.PEP_DEVICE_LIST, PublishOptions.openAccess(), null); - } - } - }); + publishDeviceIdsAndRefineAccessModel(deviceIds); } public void distrustFingerprint(final String fingerprint) { @@ -510,17 +501,40 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded { numPublishTriesOnEmptyPep = 0; } deviceIdsCopy.add(getOwnDeviceId()); - IqPacket publish = mXmppConnectionService.getIqGenerator().publishDeviceIds(deviceIdsCopy); + publishDeviceIdsAndRefineAccessModel(deviceIdsCopy); + } + + private void publishDeviceIdsAndRefineAccessModel(Set ids) { + publishDeviceIdsAndRefineAccessModel(ids, true); + } + + private void publishDeviceIdsAndRefineAccessModel(final Set ids, final boolean firstAttempt) { + final Bundle publishOptions = account.getXmppConnection().getFeatures().pepPublishOptions() ? PublishOptions.openAccess() : null; + IqPacket publish = mXmppConnectionService.getIqGenerator().publishDeviceIds(ids, publishOptions); ownPushPending.set(true); mXmppConnectionService.sendIqPacket(account, publish, new OnIqPacketReceived() { @Override public void onIqPacketReceived(Account account, IqPacket packet) { - ownPushPending.set(false); - if (packet.getType() == IqPacket.TYPE.ERROR) { - pepBroken = true; - Log.d(Config.LOGTAG, getLogprefix(account) + "Error received while publishing own device id" + packet.findChild("error")); - } else if (packet.getType() != IqPacket.TYPE.TIMEOUT) { - mXmppConnectionService.pushNodeConfiguration(account, AxolotlService.PEP_DEVICE_LIST, PublishOptions.openAccess(), null); + Element error = packet.getType() == IqPacket.TYPE.ERROR ? packet.findChild("error") : null; + if (firstAttempt && error != null && error.hasChild("precondition-not-met", Namespace.PUBSUB_ERROR)) { + Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": precondition wasn't met for device list. pushing node configuration"); + mXmppConnectionService.pushNodeConfiguration(account, AxolotlService.PEP_DEVICE_LIST, publishOptions, new XmppConnectionService.OnConfigurationPushed() { + @Override + public void onPushSucceeded() { + publishDeviceIdsAndRefineAccessModel(ids, false); + } + + @Override + public void onPushFailed() { + publishDeviceIdsAndRefineAccessModel(ids, false); + } + }); + } else { + ownPushPending.set(false); + if (packet.getType() == IqPacket.TYPE.ERROR) { + pepBroken = true; + Log.d(Config.LOGTAG, getLogprefix(account) + "Error received while publishing own device id" + packet.findChild("error")); + } } } }); @@ -680,32 +694,46 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded { Set preKeyRecords, final boolean announceAfter, final boolean wipe) { + publishDeviceBundle(signedPreKeyRecord, preKeyRecords, announceAfter, wipe, true); + } + + private void publishDeviceBundle(final SignedPreKeyRecord signedPreKeyRecord, + final Set preKeyRecords, + final boolean announceAfter, + final boolean wipe, + final boolean firstAttempt) { + final Bundle publishOptions = account.getXmppConnection().getFeatures().pepPublishOptions() ? PublishOptions.openAccess() : null; IqPacket publish = mXmppConnectionService.getIqGenerator().publishBundles( signedPreKeyRecord, axolotlStore.getIdentityKeyPair().getPublicKey(), - preKeyRecords, getOwnDeviceId()); + preKeyRecords, getOwnDeviceId(), publishOptions); Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + ": Bundle " + getOwnDeviceId() + " in PEP not current. Publishing..."); mXmppConnectionService.sendIqPacket(account, publish, new OnIqPacketReceived() { @Override public void onIqPacketReceived(final Account account, IqPacket packet) { - if (packet.getType() == IqPacket.TYPE.RESULT) { + Element error = packet.getType() == IqPacket.TYPE.ERROR ? packet.findChild("error") : null; + if (firstAttempt && error != null && error.hasChild("precondition-not-met", Namespace.PUBSUB_ERROR)) { + Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": precondition wasn't met for bundle. pushing node configuration"); final String node = AxolotlService.PEP_BUNDLES + ":" + getOwnDeviceId(); - mXmppConnectionService.pushNodeConfiguration(account, node, PublishOptions.openAccess(), new XmppConnectionService.OnConfigurationPushed() { + mXmppConnectionService.pushNodeConfiguration(account, node, publishOptions, new XmppConnectionService.OnConfigurationPushed() { @Override public void onPushSucceeded() { - Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Successfully published bundle. "); - if (wipe) { - wipeOtherPepDevices(); - } else if (announceAfter) { - Log.d(Config.LOGTAG, getLogprefix(account) + "Announcing device " + getOwnDeviceId()); - publishOwnDeviceIdIfNeeded(); - } + publishDeviceBundle(signedPreKeyRecord, preKeyRecords, announceAfter, wipe, false); } @Override public void onPushFailed() { - Log.d(Config.LOGTAG, "unable to change access model for pubsub node"); + publishDeviceBundle(signedPreKeyRecord, preKeyRecords, announceAfter, wipe, false); } }); + } + if (packet.getType() == IqPacket.TYPE.RESULT) { + Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Successfully published bundle. "); + if (wipe) { + wipeOtherPepDevices(); + } else if (announceAfter) { + Log.d(Config.LOGTAG, getLogprefix(account) + "Announcing device " + getOwnDeviceId()); + publishOwnDeviceIdIfNeeded(); + } } else if (packet.getType() == IqPacket.TYPE.ERROR) { pepBroken = true; Log.d(Config.LOGTAG, getLogprefix(account) + "Error received while publishing bundle: " + packet.findChild("error")); -- cgit v1.2.3