From 7219f42ad2e79a2069c112d4f33a6f54cdfc4cfc Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Wed, 5 Dec 2018 19:11:40 +0100 Subject: [PATCH] check mam preference for mam:2 namespace and purge offline only if set --- .../services/XmppConnectionService.java | 2 +- .../conversations/xmpp/XmppConnection.java | 19 +++++++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index d53be0dd7..dad6c558f 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -307,7 +307,7 @@ public class XmppConnectionService extends Service { } final boolean flexible = account.getXmppConnection().getFeatures().flexibleOfflineMessageRetrieval(); final boolean catchup = getMessageArchiveService().inCatchup(account); - if (flexible && catchup) { + if (flexible && catchup && account.getXmppConnection().isMamPreferenceAlways()) { sendIqPacket(account, mIqGenerator.purgeOfflineMessages(), (acc, packet) -> { if (packet.getType() == IqPacket.TYPE.RESULT) { Log.d(Config.LOGTAG, acc.getJid().asBareJid() + ": successfully purged offline messages"); diff --git a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java index 19d0283de..94833a3b3 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java +++ b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java @@ -47,7 +47,6 @@ import java.util.regex.Matcher; import javax.net.ssl.KeyManager; import javax.net.ssl.SSLContext; -import javax.net.ssl.SSLSession; import javax.net.ssl.SSLSocket; import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.X509KeyManager; @@ -75,7 +74,6 @@ import eu.siacs.conversations.services.MessageArchiveService; import eu.siacs.conversations.services.NotificationService; import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.utils.CryptoHelper; -import eu.siacs.conversations.utils.IP; import eu.siacs.conversations.utils.Patterns; import eu.siacs.conversations.utils.Resolver; import eu.siacs.conversations.utils.SSLSocketHelper; @@ -157,6 +155,7 @@ public class XmppConnection implements Runnable { private long lastConnect = 0; private long lastSessionStarted = 0; private long lastDiscoStarted = 0; + private boolean isMamPreferenceAlways = false; private AtomicInteger mPendingServiceDiscoveries = new AtomicInteger(0); private AtomicBoolean mWaitForDisco = new AtomicBoolean(true); private AtomicBoolean mWaitingForSmCatchup = new AtomicBoolean(false); @@ -1170,6 +1169,7 @@ public class XmppConnection implements Runnable { Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": server caps came from cache"); disco.put(Jid.of(account.getServer()), discoveryResult); } + discoverMamPreferences(); sendServiceDiscoveryInfo(account.getJid().asBareJid()); if (!requestDiscoItemsFirst) { sendServiceDiscoveryItems(Jid.of(account.getServer())); @@ -1213,6 +1213,21 @@ public class XmppConnection implements Runnable { }); } + private void discoverMamPreferences() { + IqPacket request = new IqPacket(IqPacket.TYPE.GET); + request.addChild("prefs", MessageArchiveService.Version.MAM_2.namespace); + sendIqPacket(request, (account, response) -> { + if (response.getType() == IqPacket.TYPE.RESULT) { + Element prefs = response.findChild("prefs", MessageArchiveService.Version.MAM_2.namespace); + isMamPreferenceAlways = "always".equals(prefs == null ? null : prefs.getAttribute("default")); + } + }); + } + + public boolean isMamPreferenceAlways() { + return isMamPreferenceAlways; + } + private void finalizeBind() { Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": online with resource " + account.getResource()); if (bindListener != null) {