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