From caa16a85172a5e3e8ff4e9fe2d9657dca7e8a104 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Wed, 15 Feb 2017 16:42:35 +0100 Subject: [PATCH] experimental mam legacy compat layer --- .../conversations/entities/MucOptions.java | 6 +++++- .../conversations/generator/IqGenerator.java | 5 ++--- .../conversations/parser/MessageParser.java | 10 ++++++++-- .../services/MessageArchiveService.java | 19 ++++++++++++++++++- .../services/XmppConnectionService.java | 5 +++-- .../eu/siacs/conversations/utils/Xmlns.java | 1 + .../conversations/xmpp/XmppConnection.java | 8 +++++++- 7 files changed, 44 insertions(+), 10 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/entities/MucOptions.java b/src/main/java/eu/siacs/conversations/entities/MucOptions.java index aed90e21c..03d491061 100644 --- a/src/main/java/eu/siacs/conversations/entities/MucOptions.java +++ b/src/main/java/eu/siacs/conversations/entities/MucOptions.java @@ -374,7 +374,11 @@ public class MucOptions { } public boolean mamSupport() { - return hasFeature(Xmlns.MAM); + return hasFeature(Xmlns.MAM) || hasFeature(Xmlns.MAM_LAGECY); + } + + public boolean mamLegacy() { + return hasFeature(Xmlns.MAM_LAGECY) && !hasFeature(Xmlns.MAM); } public boolean nonanonymous() { diff --git a/src/main/java/eu/siacs/conversations/generator/IqGenerator.java b/src/main/java/eu/siacs/conversations/generator/IqGenerator.java index c15480c81..2d371ab6f 100644 --- a/src/main/java/eu/siacs/conversations/generator/IqGenerator.java +++ b/src/main/java/eu/siacs/conversations/generator/IqGenerator.java @@ -10,7 +10,6 @@ import org.whispersystems.libaxolotl.ecc.ECPublicKey; import org.whispersystems.libaxolotl.state.PreKeyRecord; import org.whispersystems.libaxolotl.state.SignedPreKeyRecord; -import java.math.BigInteger; import java.nio.ByteBuffer; import java.security.cert.CertificateEncodingException; import java.security.cert.X509Certificate; @@ -230,10 +229,10 @@ public class IqGenerator extends AbstractGenerator { public IqPacket queryMessageArchiveManagement(final MessageArchiveService.Query mam) { final IqPacket packet = new IqPacket(IqPacket.TYPE.SET); - final Element query = packet.query(Xmlns.MAM); + final Element query = packet.query(mam.isLegacy() ? Xmlns.MAM_LAGECY : Xmlns.MAM); query.setAttribute("queryid", mam.getQueryId()); final Data data = new Data(); - data.setFormType(Xmlns.MAM); + data.setFormType(mam.isLegacy() ? Xmlns.MAM_LAGECY : Xmlns.MAM); if (mam.muc()) { packet.setTo(mam.getWith()); } else if (mam.getWith()!=null) { diff --git a/src/main/java/eu/siacs/conversations/parser/MessageParser.java b/src/main/java/eu/siacs/conversations/parser/MessageParser.java index c0dfac809..c0b29098c 100644 --- a/src/main/java/eu/siacs/conversations/parser/MessageParser.java +++ b/src/main/java/eu/siacs/conversations/parser/MessageParser.java @@ -300,10 +300,16 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece final boolean isForwarded; boolean isCarbon = false; String serverMsgId = null; - final Element result = original.findChild("result",Xmlns.MAM); + final Element fin = original.findChild("fin", Xmlns.MAM_LAGECY); + if (fin != null) { + mXmppConnectionService.getMessageArchiveService().processFinLagecy(fin,original.getFrom()); + return; + } + final boolean mamLagecy = original.hasChild("result",Xmlns.MAM_LAGECY); + final Element result = original.findChild("result",mamLagecy ? Xmlns.MAM_LAGECY : Xmlns.MAM); final MessageArchiveService.Query query = result == null ? null : mXmppConnectionService.getMessageArchiveService().findQuery(result.getAttribute("queryid")); if (query != null && query.validFrom(original.getFrom())) { - Pair f = original.getForwardedMessagePacket("result", Xmlns.MAM); + Pair f = original.getForwardedMessagePacket("result", mamLagecy ? Xmlns.MAM_LAGECY : Xmlns.MAM); if (f == null) { return; } diff --git a/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java b/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java index 83c50300f..ca6791644 100644 --- a/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java +++ b/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java @@ -164,8 +164,10 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded { query.callback(false); } } - } else if (packet.getType() == IqPacket.TYPE.RESULT && fin != null) { + } else if (packet.getType() == IqPacket.TYPE.RESULT && fin != null ) { processFin(fin); + } else if (packet.getType() == IqPacket.TYPE.RESULT && query.isLegacy()) { + //do nothing } else { Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": error executing mam: " + packet.toString()); finalizeQuery(query, true); @@ -219,6 +221,13 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded { return queryInProgress(conversation, null); } + public void processFinLagecy(Element fin, Jid from) { + Query query = findQuery(fin.getAttribute("queryid")); + if (query != null && query.validFrom(from)) { + processFin(fin); + } + } + public void processFin(Element fin) { Query query = findQuery(fin.getAttribute("queryid")); if (query == null) { @@ -319,6 +328,14 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded { return query; } + public boolean isLegacy() { + if (conversation == null || conversation.getMode() == Conversation.MODE_SINGLE) { + return account.getXmppConnection().getFeatures().mamLegacy(); + } else { + return conversation.getMucOptions().mamLegacy(); + } + } + public Query next(String reference) { Query query = page(reference); query.pagingOrder = PagingOrder.NORMAL; diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index 9f6a8f7ba..81adc00c3 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -3711,12 +3711,13 @@ public class XmppConnectionService extends Service { } public void fetchMamPreferences(Account account, final OnMamPreferencesFetched callback) { + final boolean lagecy = account.getXmppConnection().getFeatures().mamLegacy(); IqPacket request = new IqPacket(IqPacket.TYPE.GET); - request.addChild("prefs",Xmlns.MAM); + request.addChild("prefs",lagecy ? Xmlns.MAM_LAGECY : Xmlns.MAM); sendIqPacket(account, request, new OnIqPacketReceived() { @Override public void onIqPacketReceived(Account account, IqPacket packet) { - Element prefs = packet.findChild("prefs",Xmlns.MAM); + Element prefs = packet.findChild("prefs",lagecy ? Xmlns.MAM_LAGECY : Xmlns.MAM); if (packet.getType() == IqPacket.TYPE.RESULT && prefs != null) { callback.onPreferencesFetched(prefs); } else { diff --git a/src/main/java/eu/siacs/conversations/utils/Xmlns.java b/src/main/java/eu/siacs/conversations/utils/Xmlns.java index ead2e45cd..e0c501c78 100644 --- a/src/main/java/eu/siacs/conversations/utils/Xmlns.java +++ b/src/main/java/eu/siacs/conversations/utils/Xmlns.java @@ -8,4 +8,5 @@ public final class Xmlns { public static final String HTTP_UPLOAD = "urn:xmpp:http:upload"; public static final String STANZA_IDS = "urn:xmpp:sid:0"; public static final String MAM = "urn:xmpp:mam:1"; + public static final String MAM_LAGECY = "urn:xmpp:mam:0"; } diff --git a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java index af4ca9b79..192fa8c5a 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java +++ b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java @@ -1628,7 +1628,13 @@ public class XmppConnection implements Runnable { } public boolean mam() { - return hasDiscoFeature(account.getJid().toBareJid(), Xmlns.MAM); + return hasDiscoFeature(account.getJid().toBareJid(), Xmlns.MAM) + || hasDiscoFeature(account.getJid().toBareJid(), Xmlns.MAM_LAGECY); + } + + public boolean mamLegacy() { + return !hasDiscoFeature(account.getJid().toBareJid(),Xmlns.MAM) + && hasDiscoFeature(account.getJid().toBareJid(),Xmlns.MAM_LAGECY); } public boolean push() {