From 71ad18beb952e883478f434ad10e73f1dd626bfd Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Tue, 7 Feb 2017 19:32:12 +0100 Subject: [PATCH] support omemo:// style urls --- .../eu/siacs/conversations/entities/Message.java | 8 ++++++-- .../http/OmemoURLStreamHandler.java | 15 +++++++++++---- .../http/OmemoURLStreamHandlerFactory.java | 16 ++++++++++++---- .../services/XmppConnectionService.java | 6 ++++++ 4 files changed, 35 insertions(+), 10 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/entities/Message.java b/src/main/java/eu/siacs/conversations/entities/Message.java index ba947c61e..6cc72ce51 100644 --- a/src/main/java/eu/siacs/conversations/entities/Message.java +++ b/src/main/java/eu/siacs/conversations/entities/Message.java @@ -657,6 +657,12 @@ public class Message extends AbstractEntity { } try { URL url = new URL(body); + String ref = url.getRef(); + final String protocol = url.getProtocol(); + final boolean encrypted = ref != null && ref.matches("([A-Fa-f0-9]{2}){48}"); + if ("omemo".equalsIgnoreCase(protocol) && encrypted) { + return Decision.MUST; + } if (!url.getProtocol().equalsIgnoreCase("http") && !url.getProtocol().equalsIgnoreCase("https")) { return Decision.NEVER; } else if (oob) { @@ -666,8 +672,6 @@ public class Message extends AbstractEntity { if (extension == null) { return Decision.NEVER; } - String ref = url.getRef(); - boolean encrypted = ref != null && ref.matches("([A-Fa-f0-9]{2}){48}"); if (encrypted) { return Decision.MUST; diff --git a/src/main/java/eu/siacs/conversations/http/OmemoURLStreamHandler.java b/src/main/java/eu/siacs/conversations/http/OmemoURLStreamHandler.java index 96673d41a..07f28b9e0 100644 --- a/src/main/java/eu/siacs/conversations/http/OmemoURLStreamHandler.java +++ b/src/main/java/eu/siacs/conversations/http/OmemoURLStreamHandler.java @@ -1,7 +1,14 @@ package eu.siacs.conversations.http; -/** - * Created by daniel on 2/7/17. - */ -public class OmemoURLStreamHandler { +import java.io.IOException; +import java.net.URL; +import java.net.URLConnection; +import java.net.URLStreamHandler; + + +public class OmemoURLStreamHandler extends URLStreamHandler { + @Override + protected URLConnection openConnection(URL url) throws IOException { + return new URL("https"+url.toString().substring(5)).openConnection(); + } } diff --git a/src/main/java/eu/siacs/conversations/http/OmemoURLStreamHandlerFactory.java b/src/main/java/eu/siacs/conversations/http/OmemoURLStreamHandlerFactory.java index c6cdc5223..86421b51b 100644 --- a/src/main/java/eu/siacs/conversations/http/OmemoURLStreamHandlerFactory.java +++ b/src/main/java/eu/siacs/conversations/http/OmemoURLStreamHandlerFactory.java @@ -1,7 +1,15 @@ package eu.siacs.conversations.http; -/** - * Created by daniel on 2/7/17. - */ -public class OmemoURLStreamHandlerFactory { +import java.net.URLStreamHandler; +import java.net.URLStreamHandlerFactory; + +public class OmemoURLStreamHandlerFactory implements URLStreamHandlerFactory { + @Override + public URLStreamHandler createURLStreamHandler(String protocol) { + if ("omemo".equals(protocol)) { + return new OmemoURLStreamHandler(); + } else { + return null; + } + } } diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index 6b91e1cc6..05bf8cb46 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -48,6 +48,7 @@ import org.openintents.openpgp.util.OpenPgpServiceConnection; import java.io.FileDescriptor; import java.io.FileNotFoundException; import java.math.BigInteger; +import java.net.URL; import java.security.SecureRandom; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; @@ -95,6 +96,7 @@ import eu.siacs.conversations.generator.IqGenerator; import eu.siacs.conversations.generator.MessageGenerator; import eu.siacs.conversations.generator.PresenceGenerator; import eu.siacs.conversations.http.HttpConnectionManager; +import eu.siacs.conversations.http.OmemoURLStreamHandlerFactory; import eu.siacs.conversations.parser.AbstractParser; import eu.siacs.conversations.parser.IqParser; import eu.siacs.conversations.parser.MessageParser; @@ -142,6 +144,10 @@ import me.leolin.shortcutbadger.ShortcutBadger; public class XmppConnectionService extends Service { + static { + URL.setURLStreamHandlerFactory(new OmemoURLStreamHandlerFactory()); + } + public static final String ACTION_REPLY_TO_CONVERSATION = "reply_to_conversations"; public static final String ACTION_CLEAR_NOTIFICATION = "clear_notification"; public static final String ACTION_DISABLE_FOREGROUND = "disable_foreground";