1package eu.siacs.conversations.utils;
2
3import android.net.Uri;
4
5import java.io.UnsupportedEncodingException;
6import java.net.URLDecoder;
7import java.util.ArrayList;
8import java.util.List;
9import java.util.Locale;
10
11import eu.siacs.conversations.xmpp.jid.InvalidJidException;
12import eu.siacs.conversations.xmpp.jid.Jid;
13
14public class XmppUri {
15
16 protected String jid;
17 protected boolean muc;
18 protected List<Fingerprint> fingerprints = new ArrayList<>();
19
20 private static final String OMEMO_URI_PARAM = "omemo-sid-";
21
22 public XmppUri(String uri) {
23 try {
24 parse(Uri.parse(uri));
25 } catch (IllegalArgumentException e) {
26 try {
27 jid = Jid.fromString(uri).toBareJid().toString();
28 } catch (InvalidJidException e2) {
29 jid = null;
30 }
31 }
32 }
33
34 public XmppUri(Uri uri) {
35 parse(uri);
36 }
37
38 protected void parse(Uri uri) {
39 String scheme = uri.getScheme();
40 String host = uri.getHost();
41 List<String> segments = uri.getPathSegments();
42 if ("https".equalsIgnoreCase(scheme) && "conversations.im".equalsIgnoreCase(host)) {
43 if (segments.size() >= 2 && segments.get(1).contains("@")) {
44 // sample : https://conversations.im/i/foo@bar.com
45 try {
46 jid = Jid.fromString(segments.get(1)).toString();
47 } catch (Exception e) {
48 jid = null;
49 }
50 } else if (segments.size() >= 3) {
51 // sample : https://conversations.im/i/foo/bar.com
52 jid = segments.get(1) + "@" + segments.get(2);
53 }
54 muc = segments.size() > 1 && "j".equalsIgnoreCase(segments.get(0));
55 } else if ("xmpp".equalsIgnoreCase(scheme)) {
56 // sample: xmpp:foo@bar.com
57 muc = "join".equalsIgnoreCase(uri.getQuery());
58 if (uri.getAuthority() != null) {
59 jid = uri.getAuthority();
60 } else {
61 jid = uri.getSchemeSpecificPart().split("\\?")[0];
62 }
63 this.fingerprints = parseFingerprints(uri.getQuery());
64 } else if ("imto".equalsIgnoreCase(scheme)) {
65 // sample: imto://xmpp/foo@bar.com
66 try {
67 jid = URLDecoder.decode(uri.getEncodedPath(), "UTF-8").split("/")[1];
68 } catch (final UnsupportedEncodingException ignored) {
69 jid = null;
70 }
71 } else {
72 try {
73 jid = Jid.fromString(uri.toString()).toBareJid().toString();
74 } catch (final InvalidJidException ignored) {
75 jid = null;
76 }
77 }
78 }
79
80 protected List<Fingerprint> parseFingerprints(String query) {
81 List<Fingerprint> fingerprints = new ArrayList<>();
82 String[] pairs = query == null ? new String[0] : query.split(";");
83 for(String pair : pairs) {
84 String[] parts = pair.split("=",2);
85 if (parts.length == 2) {
86 String key = parts[0].toLowerCase(Locale.US);
87 String value = parts[1];
88 if ("otr-fingerprint".equals(key)) {
89 fingerprints.add(new Fingerprint(FingerprintType.OTR,value));
90 }
91 if (key.startsWith(OMEMO_URI_PARAM)) {
92 try {
93 int id = Integer.parseInt(key.substring(OMEMO_URI_PARAM.length()));
94 fingerprints.add(new Fingerprint(FingerprintType.OMEMO,value,id));
95 } catch (Exception e) {
96 //ignoring invalid device id
97 }
98 }
99 }
100 }
101 return fingerprints;
102 }
103
104 public Jid getJid() {
105 try {
106 return this.jid == null ? null :Jid.fromString(this.jid.toLowerCase());
107 } catch (InvalidJidException e) {
108 return null;
109 }
110 }
111
112 public List<Fingerprint> getFingerprints() {
113 return this.fingerprints;
114 }
115
116 public boolean hasFingerprints() {
117 return fingerprints.size() > 0;
118 }
119 public enum FingerprintType {
120 OMEMO,
121 OTR
122 }
123
124 public static class Fingerprint {
125 public final FingerprintType type;
126 public final String fingerprint;
127 public final int deviceId;
128
129 public Fingerprint(FingerprintType type, String fingerprint) {
130 this(type, fingerprint, 0);
131 }
132
133 public Fingerprint(FingerprintType type, String fingerprint, int deviceId) {
134 this.type = type;
135 this.fingerprint = fingerprint;
136 this.deviceId = deviceId;
137 }
138
139 @Override
140 public String toString() {
141 return type.toString()+": "+fingerprint+(deviceId != 0 ? " "+String.valueOf(deviceId) : "");
142 }
143 }
144}