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 jid = uri.getSchemeSpecificPart().split("\\?")[0];
75 }
76 this.fingerprints = parseFingerprints(uri.getQuery());
77 this.body = parseBody(uri.getQuery());
78 } else if ("imto".equalsIgnoreCase(scheme)) {
79 // sample: imto://xmpp/foo@bar.com
80 try {
81 jid = URLDecoder.decode(uri.getEncodedPath(), "UTF-8").split("/")[1].trim();
82 } catch (final UnsupportedEncodingException ignored) {
83 jid = null;
84 }
85 } else {
86 try {
87 jid = Jid.fromString(uri.toString()).toBareJid().toString();
88 } catch (final InvalidJidException ignored) {
89 jid = null;
90 }
91 }
92 }
93
94 protected List<Fingerprint> parseFingerprints(String query) {
95 return parseFingerprints(query,';');
96 }
97
98 protected List<Fingerprint> parseFingerprints(String query, char seperator) {
99 List<Fingerprint> fingerprints = new ArrayList<>();
100 String[] pairs = query == null ? new String[0] : query.split(String.valueOf(seperator));
101 for(String pair : pairs) {
102 String[] parts = pair.split("=",2);
103 if (parts.length == 2) {
104 String key = parts[0].toLowerCase(Locale.US);
105 String value = parts[1].toLowerCase(Locale.US);
106 if (OTR_URI_PARAM.equals(key)) {
107 fingerprints.add(new Fingerprint(FingerprintType.OTR,value));
108 }
109 if (key.startsWith(OMEMO_URI_PARAM)) {
110 try {
111 int id = Integer.parseInt(key.substring(OMEMO_URI_PARAM.length()));
112 fingerprints.add(new Fingerprint(FingerprintType.OMEMO,value,id));
113 } catch (Exception e) {
114 //ignoring invalid device id
115 }
116 }
117 }
118 }
119 return fingerprints;
120 }
121
122 protected String parseBody(String query) {
123 for(String pair : query == null ? new String[0] : query.split(";")) {
124 final String[] parts = pair.split("=",2);
125 if (parts.length == 2 && "body".equals(parts[0].toLowerCase(Locale.US))) {
126 try {
127 return URLDecoder.decode(parts[1],"UTF-8");
128 } catch (UnsupportedEncodingException e) {
129 return null;
130 }
131 }
132 }
133 return null;
134 }
135
136 protected boolean isMuc(String query) {
137 for(String pair : query == null ? new String[0] : query.split(";")) {
138 final String[] parts = pair.split("=",2);
139 if (parts.length == 1 && "join".equals(parts[0])) {
140 return true;
141 }
142 }
143 return false;
144 }
145
146 public Jid getJid() {
147 try {
148 return this.jid == null ? null :Jid.fromString(this.jid.toLowerCase());
149 } catch (InvalidJidException e) {
150 return null;
151 }
152 }
153
154 public boolean isJidValid() {
155 try {
156 Jid.fromString(jid);
157 return true;
158 } catch (InvalidJidException e) {
159 return false;
160 }
161 }
162
163 public String getBody() {
164 return body;
165 }
166
167 public List<Fingerprint> getFingerprints() {
168 return this.fingerprints;
169 }
170
171 public boolean hasFingerprints() {
172 return fingerprints.size() > 0;
173 }
174 public enum FingerprintType {
175 OMEMO,
176 OTR
177 }
178
179 public static String getFingerprintUri(String base, List<XmppUri.Fingerprint> fingerprints, char seperator) {
180 StringBuilder builder = new StringBuilder(base);
181 builder.append('?');
182 for(int i = 0; i < fingerprints.size(); ++i) {
183 XmppUri.FingerprintType type = fingerprints.get(i).type;
184 if (type == XmppUri.FingerprintType.OMEMO) {
185 builder.append(XmppUri.OMEMO_URI_PARAM);
186 builder.append(fingerprints.get(i).deviceId);
187 } else if (type == XmppUri.FingerprintType.OTR) {
188 builder.append(XmppUri.OTR_URI_PARAM);
189 }
190 builder.append('=');
191 builder.append(fingerprints.get(i).fingerprint);
192 if (i != fingerprints.size() -1) {
193 builder.append(seperator);
194 }
195 }
196 return builder.toString();
197 }
198
199 public static class Fingerprint {
200 public final FingerprintType type;
201 public final String fingerprint;
202 public final int deviceId;
203
204 public Fingerprint(FingerprintType type, String fingerprint) {
205 this(type, fingerprint, 0);
206 }
207
208 public Fingerprint(FingerprintType type, String fingerprint, int deviceId) {
209 this.type = type;
210 this.fingerprint = fingerprint;
211 this.deviceId = deviceId;
212 }
213
214 @Override
215 public String toString() {
216 return type.toString()+": "+fingerprint+(deviceId != 0 ? " "+String.valueOf(deviceId) : "");
217 }
218 }
219}