anotherim/src/main/java/eu/siacs/conversations/utils/XmppUri.java

253 lines
6.5 KiB
Java
Raw Normal View History

package eu.siacs.conversations.utils;
import android.net.Uri;
import android.support.annotation.NonNull;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
2016-11-17 19:09:42 +00:00
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
2016-11-17 19:09:42 +00:00
import java.util.Locale;
import java.util.Map;
2018-03-05 17:30:40 +00:00
import rocks.xmpp.addr.Jid;
public class XmppUri {
2018-02-14 15:51:31 +00:00
protected Uri uri;
protected String jid;
2018-05-04 20:07:00 +00:00
private List<Fingerprint> fingerprints = new ArrayList<>();
private Map<String,String> parameters = Collections.emptyMap();
2018-05-04 20:07:00 +00:00
private boolean safeSource = true;
2016-11-17 19:09:42 +00:00
2018-05-04 20:07:00 +00:00
private static final String OMEMO_URI_PARAM = "omemo-sid-";
2017-12-05 20:46:48 +00:00
public static final String ACTION_JOIN = "join";
public static final String ACTION_MESSAGE = "message";
public XmppUri(String uri) {
try {
parse(Uri.parse(uri));
} catch (IllegalArgumentException e) {
try {
2018-03-05 17:30:40 +00:00
jid = Jid.of(uri).asBareJid().toString();
} catch (IllegalArgumentException e2) {
jid = null;
}
}
}
public XmppUri(Uri uri) {
parse(uri);
}
public XmppUri(Uri uri, boolean safeSource) {
this.safeSource = safeSource;
parse(uri);
}
public boolean isSafeSource() {
return safeSource;
}
2018-07-06 18:16:04 +00:00
protected void parse(final Uri uri) {
if (uri == null) {
return;
}
2018-02-14 15:51:31 +00:00
this.uri = uri;
String scheme = uri.getScheme();
String host = uri.getHost();
List<String> segments = uri.getPathSegments();
2016-05-30 11:06:42 +00:00
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 {
jid = Jid.of(lameUrlDecode(segments.get(1))).toString();
2016-05-30 11:06:42 +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);
}
2017-12-05 20:46:48 +00:00
if (segments.size() > 1 && "j".equalsIgnoreCase(segments.get(0))) {
this.parameters = ImmutableMap.of(ACTION_JOIN, "");
2017-12-05 20:46:48 +00:00
}
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("\\?");
2017-12-08 17:44:08 +00:00
if (parts.length > 0) {
2017-12-08 13:24:26 +00:00
jid = parts[0];
} else {
return;
}
}
this.fingerprints = parseFingerprints(parameters);
2015-02-18 17:23:13 +00:00
} else if ("imto".equalsIgnoreCase(scheme)) {
// sample: imto://xmpp/foo@bar.com
try {
2017-02-27 21:48:25 +00:00
jid = URLDecoder.decode(uri.getEncodedPath(), "UTF-8").split("/")[1].trim();
} catch (final UnsupportedEncodingException ignored) {
jid = null;
}
} else {
try {
2018-03-05 17:30:40 +00:00
jid = Jid.of(uri.toString()).asBareJid().toString();
} catch (final IllegalArgumentException ignored) {
jid = null;
}
}
}
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;
2016-11-17 19:09:42 +00:00
if (parts.length == 2) {
String decoded;
try {
decoded = URLDecoder.decode(parts[1],"UTF-8");
} catch (UnsupportedEncodingException e) {
decoded = "";
2016-11-17 19:09:42 +00:00
}
value = decoded;
} else {
value = "";
}
builder.put(key, value);
}
return builder.build();
}
@Override
@NonNull
public String toString() {
if (uri != null) {
return uri.toString();
}
return "";
}
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
}
}
}
return builder.build();
}
2017-12-05 20:46:48 +00:00
public boolean isAction(final String action) {
return parameters.containsKey(action);
2017-12-05 20:46:48 +00:00
}
public Jid getJid() {
try {
return this.jid == null ? null : Jid.of(this.jid);
2018-03-05 17:30:40 +00:00
} catch (IllegalArgumentException e) {
return null;
}
}
2017-12-05 16:29:05 +00:00
public boolean isJidValid() {
if (jid == null) {
return false;
}
2017-12-05 16:29:05 +00:00
try {
2018-03-05 17:30:40 +00:00
Jid.of(jid);
2017-12-05 16:29:05 +00:00
return true;
2018-03-05 17:30:40 +00:00
} catch (IllegalArgumentException e) {
2017-12-05 16:29:05 +00:00
return false;
}
}
public String getBody() {
return parameters.get("body");
}
2018-02-12 19:00:42 +00:00
public String getName() {
return parameters.get("name");
2018-02-12 19:00:42 +00:00
}
2016-11-17 19:09:42 +00:00
public List<Fingerprint> getFingerprints() {
return this.fingerprints;
}
public boolean hasFingerprints() {
return fingerprints.size() > 0;
}
2016-11-17 19:09:42 +00:00
public enum FingerprintType {
2018-02-19 12:55:48 +00:00
OMEMO
2016-11-17 19:09:42 +00:00
}
public static String getFingerprintUri(String base, List<XmppUri.Fingerprint> fingerprints, char separator) {
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();
}
2016-11-17 19:09:42 +00:00
public static class Fingerprint {
public final FingerprintType type;
public final String fingerprint;
2018-05-04 20:07:00 +00:00
final int deviceId;
2016-11-17 19:09:42 +00:00
public Fingerprint(FingerprintType type, String fingerprint, int deviceId) {
this.type = type;
this.fingerprint = fingerprint;
this.deviceId = deviceId;
}
@NonNull
2016-11-17 19:09:42 +00:00
@Override
public String toString() {
return type.toString() + ": " + fingerprint + (deviceId != 0 ? " " + deviceId : "");
2016-11-17 19:09:42 +00:00
}
}
2018-05-04 20:07:00 +00:00
private static String lameUrlDecode(String url) {
return url.replace("%23", "#").replace("%25", "%");
}
public static String lameUrlEncode(String url) {
return url.replace("%", "%25").replace("#", "%23");
}
}