1package eu.siacs.conversations.entities;
2
3import android.content.ContentValues;
4import android.database.Cursor;
5import android.util.Base64;
6
7import androidx.annotation.NonNull;
8
9import com.google.common.base.Strings;
10
11import org.json.JSONArray;
12import org.json.JSONException;
13import org.json.JSONObject;
14
15import java.nio.charset.StandardCharsets;
16import java.security.MessageDigest;
17import java.security.NoSuchAlgorithmException;
18import java.util.ArrayList;
19import java.util.Collections;
20import java.util.Comparator;
21import java.util.List;
22
23import eu.siacs.conversations.xml.Element;
24import eu.siacs.conversations.xml.Namespace;
25import eu.siacs.conversations.xmpp.forms.Data;
26import eu.siacs.conversations.xmpp.forms.Field;
27import eu.siacs.conversations.xmpp.stanzas.IqPacket;
28
29public class ServiceDiscoveryResult {
30 public static final String TABLENAME = "discovery_results";
31 public static final String HASH = "hash";
32 public static final String VER = "ver";
33 public static final String RESULT = "result";
34 protected final String hash;
35 protected final byte[] ver;
36 protected final List<String> features;
37 protected final List<Data> forms;
38 private final List<Identity> identities;
39 public ServiceDiscoveryResult(final IqPacket packet) {
40 this.identities = new ArrayList<>();
41 this.features = new ArrayList<>();
42 this.forms = new ArrayList<>();
43 this.hash = "sha-1"; // We only support sha-1 for now
44
45 final List<Element> elements = packet.query().getChildren();
46
47 for (final Element element : elements) {
48 if (element.getName().equals("identity")) {
49 Identity id = new Identity(element);
50 if (id.getType() != null && id.getCategory() != null) {
51 identities.add(id);
52 }
53 } else if (element.getName().equals("feature")) {
54 if (element.getAttribute("var") != null) {
55 features.add(element.getAttribute("var"));
56 }
57 } else if (element.getName().equals("x") && element.getAttribute("xmlns").equals(Namespace.DATA)) {
58 forms.add(Data.parse(element));
59 }
60 }
61 this.ver = this.mkCapHash();
62 }
63 private ServiceDiscoveryResult(String hash, byte[] ver, JSONObject o) throws JSONException {
64 this.identities = new ArrayList<>();
65 this.features = new ArrayList<>();
66 this.forms = new ArrayList<>();
67 this.hash = hash;
68 this.ver = ver;
69
70 JSONArray identities = o.optJSONArray("identities");
71 if (identities != null) {
72 for (int i = 0; i < identities.length(); i++) {
73 this.identities.add(new Identity(identities.getJSONObject(i)));
74 }
75 }
76 JSONArray features = o.optJSONArray("features");
77 if (features != null) {
78 for (int i = 0; i < features.length(); i++) {
79 this.features.add(features.getString(i));
80 }
81 }
82 JSONArray forms = o.optJSONArray("forms");
83 if (forms != null) {
84 for (int i = 0; i < forms.length(); i++) {
85 this.forms.add(createFormFromJSONObject(forms.getJSONObject(i)));
86 }
87 }
88 }
89
90 private ServiceDiscoveryResult() {
91 this.hash = "sha-1";
92 this.features = Collections.emptyList();
93 this.identities = Collections.emptyList();
94 this.ver = null;
95 this.forms = Collections.emptyList();
96 }
97
98 public static ServiceDiscoveryResult empty() {
99 return new ServiceDiscoveryResult();
100 }
101
102 public ServiceDiscoveryResult(Cursor cursor) throws JSONException {
103 this(
104 cursor.getString(cursor.getColumnIndexOrThrow(HASH)),
105 Base64.decode(cursor.getString(cursor.getColumnIndexOrThrow(VER)), Base64.DEFAULT),
106 new JSONObject(cursor.getString(cursor.getColumnIndexOrThrow(RESULT)))
107 );
108 }
109
110 private static String clean(String s) {
111 return s.replace("<","<");
112 }
113
114 private static String blankNull(String s) {
115 return s == null ? "" : clean(s);
116 }
117
118 private static Data createFormFromJSONObject(JSONObject o) {
119 Data data = new Data();
120 JSONArray names = o.names();
121 for (int i = 0; i < names.length(); ++i) {
122 try {
123 String name = names.getString(i);
124 JSONArray jsonValues = o.getJSONArray(name);
125 ArrayList<String> values = new ArrayList<>(jsonValues.length());
126 for (int j = 0; j < jsonValues.length(); ++j) {
127 values.add(jsonValues.getString(j));
128 }
129 data.put(name, values);
130 } catch (Exception e) {
131 e.printStackTrace();
132 }
133 }
134 return data;
135 }
136
137 private static JSONObject createJSONFromForm(Data data) {
138 JSONObject object = new JSONObject();
139 for (Field field : data.getFields()) {
140 try {
141 JSONArray jsonValues = new JSONArray();
142 for (String value : field.getValues()) {
143 jsonValues.put(value);
144 }
145 object.put(field.getFieldName(), jsonValues);
146 } catch (Exception e) {
147 e.printStackTrace();
148 }
149 }
150 try {
151 JSONArray jsonValues = new JSONArray();
152 jsonValues.put(data.getFormType());
153 object.put(Data.FORM_TYPE, jsonValues);
154 } catch (Exception e) {
155 e.printStackTrace();
156 }
157 return object;
158 }
159
160 public String getVer() {
161 return Base64.encodeToString(this.ver, Base64.NO_WRAP);
162 }
163
164 public List<Identity> getIdentities() {
165 return this.identities;
166 }
167
168 public List<String> getFeatures() {
169 return this.features;
170 }
171
172 public boolean hasIdentity(String category, String type) {
173 for (Identity id : this.getIdentities()) {
174 if ((category == null || id.getCategory().equals(category)) &&
175 (type == null || id.getType().equals(type))) {
176 return true;
177 }
178 }
179
180 return false;
181 }
182
183 public String getExtendedDiscoInformation(String formType, String name) {
184 for (Data form : this.forms) {
185 if (formType.equals(form.getFormType())) {
186 for (Field field : form.getFields()) {
187 if (name.equals(field.getFieldName())) {
188 return field.getValue();
189 }
190 }
191 }
192 }
193 return null;
194 }
195
196 private byte[] mkCapHash() {
197 StringBuilder s = new StringBuilder();
198
199 List<Identity> identities = this.getIdentities();
200 Collections.sort(identities);
201
202 for (Identity id : identities) {
203 s.append(blankNull(id.getCategory()))
204 .append("/")
205 .append(blankNull(id.getType()))
206 .append("/")
207 .append(blankNull(id.getLang()))
208 .append("/")
209 .append(blankNull(id.getName()))
210 .append("<");
211 }
212
213 List<String> features = this.getFeatures();
214 Collections.sort(features);
215
216 for (String feature : features) {
217 s.append(clean(feature)).append("<");
218 }
219
220 Collections.sort(forms, Comparator.comparing(Data::getFormType));
221
222 for (Data form : forms) {
223 s.append(clean(form.getFormType())).append("<");
224 List<Field> fields = form.getFields();
225 Collections.sort(
226 fields, Comparator.comparing(lhs -> Strings.nullToEmpty(lhs.getFieldName())));
227 for (Field field : fields) {
228 s.append(Strings.nullToEmpty(field.getFieldName())).append("<");
229 final List<String> values = field.getValues();
230 Collections.sort(values, Comparator.comparing(ServiceDiscoveryResult::blankNull));
231 for (final String value : values) {
232 s.append(blankNull(value)).append("<");
233 }
234 }
235 }
236
237 MessageDigest md;
238 try {
239 md = MessageDigest.getInstance("SHA-1");
240 } catch (NoSuchAlgorithmException e) {
241 return null;
242 }
243
244 return md.digest(s.toString().getBytes(StandardCharsets.UTF_8));
245 }
246
247 private JSONObject toJSON() {
248 try {
249 JSONObject o = new JSONObject();
250
251 JSONArray ids = new JSONArray();
252 for (Identity id : this.getIdentities()) {
253 ids.put(id.toJSON());
254 }
255 o.put("identities", ids);
256
257 o.put("features", new JSONArray(this.getFeatures()));
258
259 JSONArray forms = new JSONArray();
260 for (Data data : this.forms) {
261 forms.put(createJSONFromForm(data));
262 }
263 o.put("forms", forms);
264
265 return o;
266 } catch (JSONException e) {
267 return null;
268 }
269 }
270
271 public ContentValues getContentValues() {
272 final ContentValues values = new ContentValues();
273 values.put(HASH, this.hash);
274 values.put(VER, getVer());
275 JSONObject jsonObject = toJSON();
276 values.put(RESULT, jsonObject == null ? "" : jsonObject.toString());
277 return values;
278 }
279
280 public static class Identity implements Comparable {
281 protected final String type;
282 protected final String lang;
283 protected final String name;
284 final String category;
285
286 Identity(final String category, final String type, final String lang, final String name) {
287 this.category = category;
288 this.type = type;
289 this.lang = lang;
290 this.name = name;
291 }
292
293 Identity(final Element el) {
294 this(
295 el.getAttribute("category"),
296 el.getAttribute("type"),
297 el.getAttribute("xml:lang"),
298 el.getAttribute("name")
299 );
300 }
301
302 Identity(final JSONObject o) {
303
304 this(
305 o.optString("category", null),
306 o.optString("type", null),
307 o.optString("lang", null),
308 o.optString("name", null)
309 );
310 }
311
312 public String getCategory() {
313 return this.category;
314 }
315
316 public String getType() {
317 return this.type;
318 }
319
320 public String getLang() {
321 return this.lang;
322 }
323
324 public String getName() {
325 return this.name;
326 }
327
328 public int compareTo(@NonNull Object other) {
329 Identity o = (Identity) other;
330 int r = blankNull(this.getCategory()).compareTo(blankNull(o.getCategory()));
331 if (r == 0) {
332 r = blankNull(this.getType()).compareTo(blankNull(o.getType()));
333 }
334 if (r == 0) {
335 r = blankNull(this.getLang()).compareTo(blankNull(o.getLang()));
336 }
337 if (r == 0) {
338 r = blankNull(this.getName()).compareTo(blankNull(o.getName()));
339 }
340
341 return r;
342 }
343
344 JSONObject toJSON() {
345 try {
346 JSONObject o = new JSONObject();
347 o.put("category", this.getCategory());
348 o.put("type", this.getType());
349 o.put("lang", this.getLang());
350 o.put("name", this.getName());
351 return o;
352 } catch (JSONException e) {
353 return null;
354 }
355 }
356 }
357}