1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
package de.pixart.messenger.utils;
import android.net.Uri;
import androidx.annotation.NonNull;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Pattern;
import rocks.xmpp.addr.Jid;
public class XmppUri {
public static final String ACTION_JOIN = "join";
public static final String ACTION_MESSAGE = "message";
public static final String ACTION_REGISTER = "register";
public static final String ACTION_ROSTER = "roster";
private static final String OMEMO_URI_PARAM = "omemo-sid-";
private static final String OTR_URI_PARAM = "otr-fingerprint";
public static Pattern XMPP_URI = Patterns.XMPP_PATTERN;
protected Uri uri;
protected String jid;
private List<Fingerprint> fingerprints = new ArrayList<>();
private Map<String, String> parameters = Collections.emptyMap();
private boolean safeSource = true;
public XmppUri(String uri) {
try {
parse(Uri.parse(uri));
} catch (IllegalArgumentException e) {
try {
jid = Jid.of(uri).asBareJid().toString();
} catch (IllegalArgumentException e2) {
jid = null;
}
}
}
public XmppUri(Uri uri) {
parse(uri);
}
public XmppUri(Uri uri, boolean safeSource) {
this.safeSource = safeSource;
parse(uri);
}
private static Map<String, String> parseParameters(final String query, final char seperator) {
final ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<>();
final String[] pairs = query == null ? new String[0] : query.split(String.valueOf(seperator));
for (String pair : pairs) {
final String[] parts = pair.split("=", 2);
if (parts.length == 0) {
continue;
}
final String key = parts[0].toLowerCase(Locale.US);
final String value;
if (parts.length == 2) {
String decoded;
try {
decoded = URLDecoder.decode(parts[1], "UTF-8");
} catch (UnsupportedEncodingException e) {
decoded = "";
}
value = decoded;
} else {
value = "";
}
builder.put(key, value);
}
return builder.build();
}
private static List<Fingerprint> parseFingerprints(Map<String, String> parameters) {
ImmutableList.Builder<Fingerprint> builder = new ImmutableList.Builder<>();
for (Map.Entry<String, String> parameter : parameters.entrySet()) {
final String key = parameter.getKey();
final String value = parameter.getValue().toLowerCase(Locale.US);
if (key.startsWith(OMEMO_URI_PARAM)) {
try {
final int id = Integer.parseInt(key.substring(OMEMO_URI_PARAM.length()));
builder.add(new Fingerprint(FingerprintType.OMEMO, value, id));
} catch (Exception e) {
//ignoring invalid device id
}
} else if ("omemo".equals(key)) {
builder.add(new Fingerprint(FingerprintType.OMEMO, value, 0));
}
}
return builder.build();
}
public static String getFingerprintUri(final String base, final List<XmppUri.Fingerprint> fingerprints, char separator) {
StringBuilder builder = new StringBuilder(base);
builder.append('?');
for (int i = 0; i < fingerprints.size(); ++i) {
XmppUri.FingerprintType type = fingerprints.get(i).type;
if (type == XmppUri.FingerprintType.OMEMO) {
builder.append(XmppUri.OMEMO_URI_PARAM);
builder.append(fingerprints.get(i).deviceId);
} else if (type == XmppUri.FingerprintType.OTR) {
builder.append(XmppUri.OTR_URI_PARAM);
}
builder.append('=');
builder.append(fingerprints.get(i).fingerprint);
if (i != fingerprints.size() - 1) {
builder.append(separator);
}
}
return builder.toString();
}
private static String lameUrlDecode(String url) {
return url.replace("%23", "#").replace("%25", "%");
}
public static String lameUrlEncode(String url) {
return url.replace("%", "%25").replace("#", "%23");
}
public boolean isSafeSource() {
return safeSource;
}
protected void parse(final Uri uri) {
if (uri == null) {
return;
}
this.uri = uri;
String scheme = uri.getScheme();
String host = uri.getHost();
List<String> segments = uri.getPathSegments();
if ("https".equalsIgnoreCase(scheme) && "jabber.pix-art.de".equalsIgnoreCase(host)) {
if (segments.size() >= 2 && segments.get(1).contains("@")) {
// sample : https://conversations.im/i/foo@bar.com
try {
jid = Jid.of(lameUrlDecode(segments.get(1))).toString();
} catch (Exception e) {
jid = null;
}
} else if (segments.size() >= 3) {
// sample : https://conversations.im/i/foo/bar.com
jid = segments.get(1) + "@" + segments.get(2);
}
if (segments.size() > 1 && "j".equalsIgnoreCase(segments.get(0))) {
this.parameters = ImmutableMap.of(ACTION_JOIN, "");
}
final Map<String, String> parameters = parseParameters(uri.getQuery(), '&');
this.fingerprints = parseFingerprints(parameters);
} else if ("xmpp".equalsIgnoreCase(scheme)) {
// sample: xmpp:foo@bar.com
this.parameters = parseParameters(uri.getQuery(), ';');
if (uri.getAuthority() != null) {
jid = uri.getAuthority();
} else {
final String[] parts = uri.getSchemeSpecificPart().split("\\?");
if (parts.length > 0) {
jid = parts[0];
} else {
return;
}
}
this.fingerprints = parseFingerprints(parameters);
} else if ("imto".equalsIgnoreCase(scheme)) {
// sample: imto://xmpp/foo@bar.com
try {
jid = URLDecoder.decode(uri.getEncodedPath(), "UTF-8").split("/")[1].trim();
} catch (final UnsupportedEncodingException ignored) {
jid = null;
}
} else {
try {
jid = Jid.of(uri.toString()).asBareJid().toString();
} catch (final IllegalArgumentException ignored) {
jid = null;
}
}
}
@Override
@NonNull
public String toString() {
if (uri != null) {
return uri.toString();
}
return "";
}
public boolean isAction(final String action) {
return parameters.containsKey(action);
}
public Jid getJid() {
try {
return this.jid == null ? null : Jid.of(this.jid);
} catch (IllegalArgumentException e) {
return null;
}
}
public boolean isValidJid() {
if (jid == null) {
return false;
}
try {
Jid.of(jid);
return true;
} catch (IllegalArgumentException e) {
return false;
}
}
public String getBody() {
return parameters.get("body");
}
public String getName() {
return parameters.get("name");
}
public String getParameter(String key) {
return this.parameters.get(key);
}
public List<Fingerprint> getFingerprints() {
return this.fingerprints;
}
public boolean hasFingerprints() {
return fingerprints.size() > 0;
}
public enum FingerprintType {
OMEMO,
OTR
}
public static class Fingerprint {
public final FingerprintType type;
public final String fingerprint;
final int deviceId;
public Fingerprint(FingerprintType type, String fingerprint) {
this(type, fingerprint, 0);
}
public Fingerprint(FingerprintType type, String fingerprint, int deviceId) {
this.type = type;
this.fingerprint = fingerprint;
this.deviceId = deviceId;
}
@NonNull
@Override
public String toString() {
return type.toString() + ": " + fingerprint + (deviceId != 0 ? " " + deviceId : "");
}
}
}
|