1package eu.siacs.conversations.utils;
2
3import android.net.Uri;
4
5import java.io.UnsupportedEncodingException;
6import java.net.URLDecoder;
7
8import eu.siacs.conversations.utils.CryptoHelper;
9import eu.siacs.conversations.xmpp.jid.InvalidJidException;
10import eu.siacs.conversations.xmpp.jid.Jid;
11
12public class XmppUri {
13
14 protected String jid;
15 protected boolean muc;
16 protected String fingerprint;
17
18 public XmppUri(String uri) {
19 try {
20 parse(Uri.parse(uri));
21 } catch (IllegalArgumentException e) {
22 jid = null;
23 }
24 }
25
26 public XmppUri(Uri uri) {
27 parse(uri);
28 }
29
30 protected void parse(Uri uri) {
31 String scheme = uri.getScheme();
32 if ("xmpp".equals(scheme)) {
33 // sample: xmpp:jid@foo.com
34 muc = "join".equalsIgnoreCase(uri.getQuery());
35 if (uri.getAuthority() != null) {
36 jid = uri.getAuthority();
37 } else {
38 jid = uri.getSchemeSpecificPart().split("\\?")[0];
39 }
40 fingerprint = parseFingerprint(uri.getQuery());
41 } else if ("imto".equals(scheme)) {
42 // sample: imto://xmpp/jid@foo.com
43 try {
44 jid = URLDecoder.decode(uri.getEncodedPath(), "UTF-8").split("/")[1];
45 } catch (final UnsupportedEncodingException ignored) {
46 }
47 }
48 }
49
50 protected String parseFingerprint(String query) {
51 if (query == null) {
52 return null;
53 } else {
54 final String NEEDLE = "otr-fingerprint=";
55 int index = query.indexOf(NEEDLE);
56 if (index >= 0 && query.length() >= (NEEDLE.length() + index + 40)) {
57 return CryptoHelper.prettifyFingerprint(query.substring(index + NEEDLE.length(), index + NEEDLE.length() + 40));
58 } else {
59 return null;
60 }
61 }
62 }
63
64 public Jid getJid() {
65 try {
66 return Jid.fromString(this.jid);
67 } catch (InvalidJidException e) {
68 return null;
69 }
70 }
71
72 public String getFingerprint() {
73 return this.fingerprint;
74 }
75
76 public boolean isMuc() {
77 return this.muc;
78 }
79}