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