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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
protected String 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void parse(Uri uri) {
|
|
|
|
String scheme = uri.getScheme();
|
2015-02-18 17:23:13 +00:00
|
|
|
if ("xmpp".equalsIgnoreCase(scheme)) {
|
2014-11-16 01:10:29 +00:00
|
|
|
// sample: xmpp:jid@foo.com
|
|
|
|
muc = "join".equalsIgnoreCase(uri.getQuery());
|
|
|
|
if (uri.getAuthority() != null) {
|
|
|
|
jid = uri.getAuthority();
|
|
|
|
} else {
|
|
|
|
jid = uri.getSchemeSpecificPart().split("\\?")[0];
|
|
|
|
}
|
|
|
|
fingerprint = parseFingerprint(uri.getQuery());
|
2015-02-18 17:23:13 +00:00
|
|
|
} else if ("imto".equalsIgnoreCase(scheme)) {
|
2014-11-16 01:10:29 +00:00
|
|
|
// sample: imto://xmpp/jid@foo.com
|
|
|
|
try {
|
|
|
|
jid = URLDecoder.decode(uri.getEncodedPath(), "UTF-8").split("/")[1];
|
|
|
|
} 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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected String parseFingerprint(String query) {
|
|
|
|
if (query == null) {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
final String NEEDLE = "otr-fingerprint=";
|
|
|
|
int index = query.indexOf(NEEDLE);
|
|
|
|
if (index >= 0 && query.length() >= (NEEDLE.length() + index + 40)) {
|
2015-01-02 00:21:14 +00:00
|
|
|
return query.substring(index + NEEDLE.length(), index + NEEDLE.length() + 40);
|
2014-11-16 01:10:29 +00:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getFingerprint() {
|
|
|
|
return this.fingerprint;
|
|
|
|
}
|
|
|
|
}
|