2014-11-16 01:10:29 +00:00
|
|
|
package eu.siacs.conversations.utils;
|
|
|
|
|
|
|
|
import android.net.Uri;
|
|
|
|
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
|
import java.net.URLDecoder;
|
2016-11-17 19:09:42 +00:00
|
|
|
import java.util.ArrayList;
|
2016-05-29 18:44:58 +00:00
|
|
|
import java.util.List;
|
2016-11-17 19:09:42 +00:00
|
|
|
import java.util.Locale;
|
2014-11-16 01:10:29 +00:00
|
|
|
|
|
|
|
import eu.siacs.conversations.xmpp.jid.InvalidJidException;
|
|
|
|
import eu.siacs.conversations.xmpp.jid.Jid;
|
|
|
|
|
|
|
|
public class XmppUri {
|
|
|
|
|
2014-11-16 21:23:42 +00:00
|
|
|
protected String jid;
|
|
|
|
protected boolean muc;
|
2016-11-17 19:09:42 +00:00
|
|
|
protected List<Fingerprint> fingerprints = new ArrayList<>();
|
2016-11-28 14:09:02 +00:00
|
|
|
private String body;
|
2016-12-03 12:19:56 +00:00
|
|
|
protected boolean safeSource = true;
|
2016-11-17 19:09:42 +00:00
|
|
|
|
2016-11-17 21:28:45 +00:00
|
|
|
public static final String OMEMO_URI_PARAM = "omemo-sid-";
|
|
|
|
public static final String OTR_URI_PARAM = "otr-fingerprint";
|
2014-11-16 01:10:29 +00:00
|
|
|
|
|
|
|
public XmppUri(String uri) {
|
|
|
|
try {
|
|
|
|
parse(Uri.parse(uri));
|
|
|
|
} catch (IllegalArgumentException e) {
|
2015-01-05 14:08:13 +00:00
|
|
|
try {
|
|
|
|
jid = Jid.fromString(uri).toBareJid().toString();
|
|
|
|
} catch (InvalidJidException e2) {
|
|
|
|
jid = null;
|
|
|
|
}
|
2014-11-16 01:10:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public XmppUri(Uri uri) {
|
|
|
|
parse(uri);
|
|
|
|
}
|
|
|
|
|
2016-12-03 12:19:56 +00:00
|
|
|
public XmppUri(Uri uri, boolean safeSource) {
|
|
|
|
this.safeSource = safeSource;
|
|
|
|
parse(uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isSafeSource() {
|
|
|
|
return safeSource;
|
|
|
|
}
|
|
|
|
|
2014-11-16 01:10:29 +00:00
|
|
|
protected void parse(Uri uri) {
|
|
|
|
String scheme = uri.getScheme();
|
2016-05-29 18:44:58 +00:00
|
|
|
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.fromString(segments.get(1)).toString();
|
|
|
|
} 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);
|
|
|
|
}
|
2016-05-30 19:12:04 +00:00
|
|
|
muc = segments.size() > 1 && "j".equalsIgnoreCase(segments.get(0));
|
2016-12-03 12:37:26 +00:00
|
|
|
fingerprints = parseFingerprints(uri.getQuery(),'&');
|
2016-05-29 18:44:58 +00:00
|
|
|
} else if ("xmpp".equalsIgnoreCase(scheme)) {
|
|
|
|
// sample: xmpp:foo@bar.com
|
2016-11-28 14:09:02 +00:00
|
|
|
muc = isMuc(uri.getQuery());
|
2014-11-16 01:10:29 +00:00
|
|
|
if (uri.getAuthority() != null) {
|
|
|
|
jid = uri.getAuthority();
|
|
|
|
} else {
|
2017-12-08 13:24:26 +00:00
|
|
|
String[] parts = uri.getSchemeSpecificPart().split("\\?");
|
|
|
|
if (parts.length > 1) {
|
|
|
|
jid = parts[0];
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2014-11-16 01:10:29 +00:00
|
|
|
}
|
2016-11-17 19:09:42 +00:00
|
|
|
this.fingerprints = parseFingerprints(uri.getQuery());
|
2016-11-28 14:09:02 +00:00
|
|
|
this.body = parseBody(uri.getQuery());
|
2015-02-18 17:23:13 +00:00
|
|
|
} else if ("imto".equalsIgnoreCase(scheme)) {
|
2016-05-29 18:44:58 +00:00
|
|
|
// sample: imto://xmpp/foo@bar.com
|
2014-11-16 01:10:29 +00:00
|
|
|
try {
|
2017-02-27 21:48:25 +00:00
|
|
|
jid = URLDecoder.decode(uri.getEncodedPath(), "UTF-8").split("/")[1].trim();
|
2014-11-16 01:10:29 +00:00
|
|
|
} catch (final UnsupportedEncodingException ignored) {
|
2015-01-05 14:08:13 +00:00
|
|
|
jid = null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
jid = Jid.fromString(uri.toString()).toBareJid().toString();
|
|
|
|
} catch (final InvalidJidException ignored) {
|
|
|
|
jid = null;
|
2014-11-16 01:10:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-17 19:09:42 +00:00
|
|
|
protected List<Fingerprint> parseFingerprints(String query) {
|
2016-12-03 12:19:56 +00:00
|
|
|
return parseFingerprints(query,';');
|
|
|
|
}
|
|
|
|
|
|
|
|
protected List<Fingerprint> parseFingerprints(String query, char seperator) {
|
2016-11-17 19:09:42 +00:00
|
|
|
List<Fingerprint> fingerprints = new ArrayList<>();
|
2016-12-03 12:19:56 +00:00
|
|
|
String[] pairs = query == null ? new String[0] : query.split(String.valueOf(seperator));
|
2016-11-17 19:09:42 +00:00
|
|
|
for(String pair : pairs) {
|
|
|
|
String[] parts = pair.split("=",2);
|
|
|
|
if (parts.length == 2) {
|
|
|
|
String key = parts[0].toLowerCase(Locale.US);
|
2016-11-18 12:02:33 +00:00
|
|
|
String value = parts[1].toLowerCase(Locale.US);
|
2016-11-17 21:28:45 +00:00
|
|
|
if (OTR_URI_PARAM.equals(key)) {
|
2016-11-17 19:09:42 +00:00
|
|
|
fingerprints.add(new Fingerprint(FingerprintType.OTR,value));
|
|
|
|
}
|
|
|
|
if (key.startsWith(OMEMO_URI_PARAM)) {
|
|
|
|
try {
|
|
|
|
int id = Integer.parseInt(key.substring(OMEMO_URI_PARAM.length()));
|
|
|
|
fingerprints.add(new Fingerprint(FingerprintType.OMEMO,value,id));
|
|
|
|
} catch (Exception e) {
|
|
|
|
//ignoring invalid device id
|
|
|
|
}
|
|
|
|
}
|
2014-11-16 01:10:29 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-17 19:09:42 +00:00
|
|
|
return fingerprints;
|
2014-11-16 01:10:29 +00:00
|
|
|
}
|
|
|
|
|
2016-11-28 14:09:02 +00:00
|
|
|
protected String parseBody(String query) {
|
|
|
|
for(String pair : query == null ? new String[0] : query.split(";")) {
|
|
|
|
final String[] parts = pair.split("=",2);
|
|
|
|
if (parts.length == 2 && "body".equals(parts[0].toLowerCase(Locale.US))) {
|
|
|
|
try {
|
|
|
|
return URLDecoder.decode(parts[1],"UTF-8");
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected boolean isMuc(String query) {
|
|
|
|
for(String pair : query == null ? new String[0] : query.split(";")) {
|
|
|
|
final String[] parts = pair.split("=",2);
|
|
|
|
if (parts.length == 1 && "join".equals(parts[0])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-16 01:10:29 +00:00
|
|
|
public Jid getJid() {
|
|
|
|
try {
|
2015-02-18 17:23:13 +00:00
|
|
|
return this.jid == null ? null :Jid.fromString(this.jid.toLowerCase());
|
2014-11-16 01:10:29 +00:00
|
|
|
} catch (InvalidJidException e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-05 16:29:05 +00:00
|
|
|
public boolean isJidValid() {
|
|
|
|
try {
|
|
|
|
Jid.fromString(jid);
|
|
|
|
return true;
|
|
|
|
} catch (InvalidJidException e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-28 14:09:02 +00:00
|
|
|
public String getBody() {
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
|
2016-11-17 19:09:42 +00:00
|
|
|
public List<Fingerprint> getFingerprints() {
|
|
|
|
return this.fingerprints;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasFingerprints() {
|
|
|
|
return fingerprints.size() > 0;
|
|
|
|
}
|
|
|
|
public enum FingerprintType {
|
|
|
|
OMEMO,
|
|
|
|
OTR
|
|
|
|
}
|
|
|
|
|
2016-12-03 12:37:26 +00:00
|
|
|
public static String getFingerprintUri(String base, List<XmppUri.Fingerprint> fingerprints, char seperator) {
|
|
|
|
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);
|
|
|
|
} else if (type == XmppUri.FingerprintType.OTR) {
|
|
|
|
builder.append(XmppUri.OTR_URI_PARAM);
|
|
|
|
}
|
|
|
|
builder.append('=');
|
|
|
|
builder.append(fingerprints.get(i).fingerprint);
|
|
|
|
if (i != fingerprints.size() -1) {
|
|
|
|
builder.append(seperator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return builder.toString();
|
|
|
|
}
|
|
|
|
|
2016-11-17 19:09:42 +00:00
|
|
|
public static class Fingerprint {
|
|
|
|
public final FingerprintType type;
|
|
|
|
public final String fingerprint;
|
|
|
|
public final int deviceId;
|
|
|
|
|
|
|
|
public Fingerprint(FingerprintType type, String fingerprint) {
|
|
|
|
this(type, fingerprint, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Fingerprint(FingerprintType type, String fingerprint, int deviceId) {
|
|
|
|
this.type = type;
|
|
|
|
this.fingerprint = fingerprint;
|
|
|
|
this.deviceId = deviceId;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return type.toString()+": "+fingerprint+(deviceId != 0 ? " "+String.valueOf(deviceId) : "");
|
|
|
|
}
|
2014-11-16 01:10:29 +00:00
|
|
|
}
|
|
|
|
}
|