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 private String body;
20 protected boolean safeSource = true;
21
22 public static final String OMEMO_URI_PARAM = "omemo-sid-";
23 public static final String OTR_URI_PARAM = "otr-fingerprint";
24
25 public XmppUri(String uri) {
26 try {
27 parse(Uri.parse(uri));
28 } catch (IllegalArgumentException e) {
29 try {
30 jid = Jid.fromString(uri).toBareJid().toString();
31 } catch (InvalidJidException e2) {
32 jid = null;
33 }
34 }
35 }
36
37 public XmppUri(Uri uri) {
38 parse(uri);
39 }
40
41 public XmppUri(Uri uri, boolean safeSource) {
42 this.safeSource = safeSource;
43 parse(uri);
44 }
45
46 public boolean isSafeSource() {
47 return safeSource;
48 }
49
50 protected void parse(Uri uri) {
51 String scheme = uri.getScheme();
52 String host = uri.getHost();
53 List<String> segments = uri.getPathSegments();
54 if ("https".equalsIgnoreCase(scheme) && "conversations.im".equalsIgnoreCase(host)) {
55 if (segments.size() >= 2 && segments.get(1).contains("@")) {
56 // sample : https://conversations.im/i/foo@bar.com
57 try {
58 jid = Jid.fromString(segments.get(1)).toString();
59 } catch (Exception e) {
60 jid = null;
61 }
62 } else if (segments.size() >= 3) {
63 // sample : https://conversations.im/i/foo/bar.com
64 jid = segments.get(1) + "@" + segments.get(2);
65 }
66 muc = segments.size() > 1 && "j".equalsIgnoreCase(segments.get(0));
67 fingerprints = parseFingerprints(uri.getQuery(),'&');
68 } else if ("xmpp".equalsIgnoreCase(scheme)) {
69 // sample: xmpp:foo@bar.com
70 muc = isMuc(uri.getQuery());
71 if (uri.getAuthority() != null) {
72 jid = uri.getAuthority();
73 } else {
74 String[] parts = uri.getSchemeSpecificPart().split("\\?");
75 if (parts.length > 1) {
76 jid = parts[0];
77 } else {
78 return;
79 }
80 }
81 this.fingerprints = parseFingerprints(uri.getQuery());
82 this.body = parseBody(uri.getQuery());
83 } else if ("imto".equalsIgnoreCase(scheme)) {
84 // sample: imto://xmpp/foo@bar.com
85 try {
86 jid = URLDecoder.decode(uri.getEncodedPath(), "UTF-8").split("/")[1].trim();
87 } catch (final UnsupportedEncodingException ignored) {
88 jid = null;
89 }
90 } else {
91 try {
92 jid = Jid.fromString(uri.toString()).toBareJid().toString();
93 } catch (final InvalidJidException ignored) {
94 jid = null;
95 }
96 }
97 }
98
99 protected List<Fingerprint> parseFingerprints(String query) {
100 return parseFingerprints(query,';');
101 }
102
103 protected List<Fingerprint> parseFingerprints(String query, char seperator) {
104 List<Fingerprint> fingerprints = new ArrayList<>();
105 String[] pairs = query == null ? new String[0] : query.split(String.valueOf(seperator));
106 for(String pair : pairs) {
107 String[] parts = pair.split("=",2);
108 if (parts.length == 2) {
109 String key = parts[0].toLowerCase(Locale.US);
110 String value = parts[1].toLowerCase(Locale.US);
111 if (OTR_URI_PARAM.equals(key)) {
112 fingerprints.add(new Fingerprint(FingerprintType.OTR,value));
113 }
114 if (key.startsWith(OMEMO_URI_PARAM)) {
115 try {
116 int id = Integer.parseInt(key.substring(OMEMO_URI_PARAM.length()));
117 fingerprints.add(new Fingerprint(FingerprintType.OMEMO,value,id));
118 } catch (Exception e) {
119 //ignoring invalid device id
120 }
121 }
122 }
123 }
124 return fingerprints;
125 }
126
127 protected String parseBody(String query) {
128 for(String pair : query == null ? new String[0] : query.split(";")) {
129 final String[] parts = pair.split("=",2);
130 if (parts.length == 2 && "body".equals(parts[0].toLowerCase(Locale.US))) {
131 try {
132 return URLDecoder.decode(parts[1],"UTF-8");
133 } catch (UnsupportedEncodingException e) {
134 return null;
135 }
136 }
137 }
138 return null;
139 }
140
141 protected boolean isMuc(String query) {
142 for(String pair : query == null ? new String[0] : query.split(";")) {
143 final String[] parts = pair.split("=",2);
144 if (parts.length == 1 && "join".equals(parts[0])) {
145 return true;
146 }
147 }
148 return false;
149 }
150
151 public Jid getJid() {
152 try {
153 return this.jid == null ? null :Jid.fromString(this.jid.toLowerCase());
154 } catch (InvalidJidException e) {
155 return null;
156 }
157 }
158
159 public boolean isJidValid() {
160 try {
161 Jid.fromString(jid);
162 return true;
163 } catch (InvalidJidException e) {
164 return false;
165 }
166 }
167
168 public String getBody() {
169 return body;
170 }
171
172 public List<Fingerprint> getFingerprints() {
173 return this.fingerprints;
174 }
175
176 public boolean hasFingerprints() {
177 return fingerprints.size() > 0;
178 }
179 public enum FingerprintType {
180 OMEMO,
181 OTR
182 }
183
184 public static String getFingerprintUri(String base, List<XmppUri.Fingerprint> fingerprints, char seperator) {
185 StringBuilder builder = new StringBuilder(base);
186 builder.append('?');
187 for(int i = 0; i < fingerprints.size(); ++i) {
188 XmppUri.FingerprintType type = fingerprints.get(i).type;
189 if (type == XmppUri.FingerprintType.OMEMO) {
190 builder.append(XmppUri.OMEMO_URI_PARAM);
191 builder.append(fingerprints.get(i).deviceId);
192 } else if (type == XmppUri.FingerprintType.OTR) {
193 builder.append(XmppUri.OTR_URI_PARAM);
194 }
195 builder.append('=');
196 builder.append(fingerprints.get(i).fingerprint);
197 if (i != fingerprints.size() -1) {
198 builder.append(seperator);
199 }
200 }
201 return builder.toString();
202 }
203
204 public static class Fingerprint {
205 public final FingerprintType type;
206 public final String fingerprint;
207 public final int deviceId;
208
209 public Fingerprint(FingerprintType type, String fingerprint) {
210 this(type, fingerprint, 0);
211 }
212
213 public Fingerprint(FingerprintType type, String fingerprint, int deviceId) {
214 this.type = type;
215 this.fingerprint = fingerprint;
216 this.deviceId = deviceId;
217 }
218
219 @Override
220 public String toString() {
221 return type.toString()+": "+fingerprint+(deviceId != 0 ? " "+String.valueOf(deviceId) : "");
222 }
223 }
224}