2014-11-16 01:10:29 +00:00
|
|
|
package eu.siacs.conversations.utils;
|
|
|
|
|
|
|
|
import android.net.Uri;
|
2021-01-23 08:25:34 +00:00
|
|
|
|
2021-01-18 17:26:46 +00:00
|
|
|
import androidx.annotation.NonNull;
|
2020-01-08 09:51:18 +00:00
|
|
|
|
2021-09-27 09:15:56 +00:00
|
|
|
import com.google.common.base.CharMatcher;
|
|
|
|
import com.google.common.collect.Collections2;
|
2020-01-08 09:51:18 +00:00
|
|
|
import com.google.common.collect.ImmutableList;
|
|
|
|
import com.google.common.collect.ImmutableMap;
|
2014-11-16 01:10:29 +00:00
|
|
|
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
|
import java.net.URLDecoder;
|
2016-11-17 19:09:42 +00:00
|
|
|
import java.util.ArrayList;
|
2021-10-03 15:01:32 +00:00
|
|
|
import java.util.Arrays;
|
2020-01-08 09:51:18 +00:00
|
|
|
import java.util.Collections;
|
2016-05-29 18:44:58 +00:00
|
|
|
import java.util.List;
|
2016-11-17 19:09:42 +00:00
|
|
|
import java.util.Locale;
|
2020-01-08 09:51:18 +00:00
|
|
|
import java.util.Map;
|
2014-11-16 01:10:29 +00:00
|
|
|
|
2020-05-15 15:06:16 +00:00
|
|
|
import eu.siacs.conversations.xmpp.Jid;
|
2014-11-16 01:10:29 +00:00
|
|
|
|
|
|
|
public class XmppUri {
|
|
|
|
|
2020-01-16 17:54:56 +00:00
|
|
|
public static final String ACTION_JOIN = "join";
|
|
|
|
public static final String ACTION_MESSAGE = "message";
|
|
|
|
public static final String ACTION_REGISTER = "register";
|
|
|
|
public static final String ACTION_ROSTER = "roster";
|
2020-06-21 13:40:51 +00:00
|
|
|
public static final String PARAMETER_PRE_AUTH = "preauth";
|
|
|
|
public static final String PARAMETER_IBR = "ibr";
|
2020-01-16 17:54:56 +00:00
|
|
|
private static final String OMEMO_URI_PARAM = "omemo-sid-";
|
|
|
|
protected Uri uri;
|
|
|
|
protected String jid;
|
|
|
|
private List<Fingerprint> fingerprints = new ArrayList<>();
|
|
|
|
private Map<String, String> parameters = Collections.emptyMap();
|
|
|
|
private boolean safeSource = true;
|
|
|
|
|
2021-03-22 09:12:53 +00:00
|
|
|
public XmppUri(final String uri) {
|
2020-01-16 17:54:56 +00:00
|
|
|
try {
|
|
|
|
parse(Uri.parse(uri));
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
try {
|
2020-05-17 06:53:44 +00:00
|
|
|
jid = Jid.ofEscaped(uri).asBareJid().toEscapedString();
|
2020-01-16 17:54:56 +00:00
|
|
|
} catch (IllegalArgumentException e2) {
|
|
|
|
jid = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public XmppUri(Uri uri) {
|
|
|
|
parse(uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
public XmppUri(Uri uri, boolean safeSource) {
|
|
|
|
this.safeSource = safeSource;
|
|
|
|
parse(uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Map<String, String> parseParameters(final String query, final char seperator) {
|
|
|
|
final ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<>();
|
|
|
|
final String[] pairs = query == null ? new String[0] : query.split(String.valueOf(seperator));
|
|
|
|
for (String pair : pairs) {
|
|
|
|
final String[] parts = pair.split("=", 2);
|
|
|
|
if (parts.length == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
final String key = parts[0].toLowerCase(Locale.US);
|
|
|
|
final String value;
|
|
|
|
if (parts.length == 2) {
|
|
|
|
String decoded;
|
|
|
|
try {
|
|
|
|
decoded = URLDecoder.decode(parts[1], "UTF-8");
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
decoded = "";
|
|
|
|
}
|
|
|
|
value = decoded;
|
|
|
|
} else {
|
|
|
|
value = "";
|
|
|
|
}
|
|
|
|
builder.put(key, value);
|
|
|
|
}
|
|
|
|
return builder.build();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static List<Fingerprint> parseFingerprints(Map<String, String> parameters) {
|
|
|
|
ImmutableList.Builder<Fingerprint> builder = new ImmutableList.Builder<>();
|
|
|
|
for (Map.Entry<String, String> parameter : parameters.entrySet()) {
|
|
|
|
final String key = parameter.getKey();
|
|
|
|
final String value = parameter.getValue().toLowerCase(Locale.US);
|
|
|
|
if (key.startsWith(OMEMO_URI_PARAM)) {
|
|
|
|
try {
|
|
|
|
final int id = Integer.parseInt(key.substring(OMEMO_URI_PARAM.length()));
|
|
|
|
builder.add(new Fingerprint(FingerprintType.OMEMO, value, id));
|
|
|
|
} catch (Exception e) {
|
|
|
|
//ignoring invalid device id
|
|
|
|
}
|
|
|
|
} else if ("omemo".equals(key)) {
|
|
|
|
builder.add(new Fingerprint(FingerprintType.OMEMO, value, 0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return builder.build();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String getFingerprintUri(final String base, final List<XmppUri.Fingerprint> fingerprints, char separator) {
|
|
|
|
final StringBuilder builder = new StringBuilder(base);
|
|
|
|
builder.append('?');
|
|
|
|
for (int i = 0; i < fingerprints.size(); ++i) {
|
|
|
|
XmppUri.FingerprintType type = fingerprints.get(i).type;
|
|
|
|
if (type == XmppUri.FingerprintType.OMEMO) {
|
|
|
|
builder.append(XmppUri.OMEMO_URI_PARAM);
|
|
|
|
builder.append(fingerprints.get(i).deviceId);
|
|
|
|
}
|
|
|
|
builder.append('=');
|
|
|
|
builder.append(fingerprints.get(i).fingerprint);
|
|
|
|
if (i != fingerprints.size() - 1) {
|
|
|
|
builder.append(separator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return builder.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static String lameUrlDecode(String url) {
|
|
|
|
return url.replace("%23", "#").replace("%25", "%");
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String lameUrlEncode(String url) {
|
|
|
|
return url.replace("%", "%25").replace("#", "%23");
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isSafeSource() {
|
|
|
|
return safeSource;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void parse(final Uri uri) {
|
|
|
|
if (uri == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.uri = uri;
|
|
|
|
String scheme = uri.getScheme();
|
|
|
|
String host = uri.getHost();
|
|
|
|
List<String> segments = uri.getPathSegments();
|
|
|
|
if ("https".equalsIgnoreCase(scheme) && "conversations.im".equalsIgnoreCase(host)) {
|
|
|
|
if (segments.size() >= 2 && segments.get(1).contains("@")) {
|
|
|
|
// sample : https://conversations.im/i/foo@bar.com
|
|
|
|
try {
|
2020-05-17 06:53:44 +00:00
|
|
|
jid = Jid.ofEscaped(lameUrlDecode(segments.get(1))).toEscapedString();
|
2020-01-16 17:54:56 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
jid = null;
|
|
|
|
}
|
|
|
|
} else if (segments.size() >= 3) {
|
|
|
|
// sample : https://conversations.im/i/foo/bar.com
|
|
|
|
jid = segments.get(1) + "@" + segments.get(2);
|
|
|
|
}
|
|
|
|
if (segments.size() > 1 && "j".equalsIgnoreCase(segments.get(0))) {
|
|
|
|
this.parameters = ImmutableMap.of(ACTION_JOIN, "");
|
|
|
|
}
|
|
|
|
final Map<String, String> parameters = parseParameters(uri.getQuery(), '&');
|
|
|
|
this.fingerprints = parseFingerprints(parameters);
|
|
|
|
} else if ("xmpp".equalsIgnoreCase(scheme)) {
|
|
|
|
// sample: xmpp:foo@bar.com
|
|
|
|
this.parameters = parseParameters(uri.getQuery(), ';');
|
|
|
|
if (uri.getAuthority() != null) {
|
|
|
|
jid = uri.getAuthority();
|
|
|
|
} else {
|
|
|
|
final String[] parts = uri.getSchemeSpecificPart().split("\\?");
|
|
|
|
if (parts.length > 0) {
|
|
|
|
jid = parts[0];
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.fingerprints = parseFingerprints(parameters);
|
2021-10-03 15:01:32 +00:00
|
|
|
} else if ("imto".equalsIgnoreCase(scheme) && Arrays.asList("xmpp", "jabber").contains(uri.getHost())) {
|
2020-01-16 17:54:56 +00:00
|
|
|
// sample: imto://xmpp/foo@bar.com
|
|
|
|
try {
|
|
|
|
jid = URLDecoder.decode(uri.getEncodedPath(), "UTF-8").split("/")[1].trim();
|
|
|
|
} catch (final UnsupportedEncodingException ignored) {
|
|
|
|
jid = null;
|
|
|
|
}
|
|
|
|
} else {
|
2020-12-02 07:19:06 +00:00
|
|
|
jid = null;
|
2020-01-16 17:54:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@NonNull
|
|
|
|
public String toString() {
|
|
|
|
if (uri != null) {
|
|
|
|
return uri.toString();
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isAction(final String action) {
|
2021-09-27 09:15:56 +00:00
|
|
|
return Collections2.transform(
|
|
|
|
parameters.keySet(),
|
2021-10-03 16:51:18 +00:00
|
|
|
s -> CharMatcher.inRange('a', 'z').or(CharMatcher.inRange('A', 'Z')).retainFrom(s)
|
2021-09-27 09:15:56 +00:00
|
|
|
).contains(action);
|
2020-01-16 17:54:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public Jid getJid() {
|
|
|
|
try {
|
2020-05-17 06:53:44 +00:00
|
|
|
return this.jid == null ? null : Jid.ofEscaped(this.jid);
|
2020-01-16 17:54:56 +00:00
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isValidJid() {
|
|
|
|
if (jid == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
try {
|
2020-05-17 06:53:44 +00:00
|
|
|
Jid.ofEscaped(jid);
|
2020-01-16 17:54:56 +00:00
|
|
|
return true;
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getBody() {
|
|
|
|
return parameters.get("body");
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getName() {
|
|
|
|
return parameters.get("name");
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getParameter(String key) {
|
|
|
|
return this.parameters.get(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<Fingerprint> getFingerprints() {
|
|
|
|
return this.fingerprints;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasFingerprints() {
|
|
|
|
return fingerprints.size() > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public enum FingerprintType {
|
|
|
|
OMEMO
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class Fingerprint {
|
|
|
|
public final FingerprintType type;
|
|
|
|
public final String fingerprint;
|
|
|
|
final int deviceId;
|
|
|
|
|
|
|
|
public Fingerprint(FingerprintType type, String fingerprint, int deviceId) {
|
|
|
|
this.type = type;
|
|
|
|
this.fingerprint = fingerprint;
|
|
|
|
this.deviceId = deviceId;
|
|
|
|
}
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return type.toString() + ": " + fingerprint + (deviceId != 0 ? " " + deviceId : "");
|
|
|
|
}
|
|
|
|
}
|
2014-11-16 01:10:29 +00:00
|
|
|
}
|