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