1package eu.siacs.conversations.entities;
2
3import android.content.ContentValues;
4import android.database.Cursor;
5import android.support.annotation.NonNull;
6import android.util.Base64;
7import android.util.Log;
8
9import java.io.UnsupportedEncodingException;
10import java.lang.Comparable;
11import java.security.MessageDigest;
12import java.security.NoSuchAlgorithmException;
13import java.util.ArrayList;
14import java.util.Collections;
15import java.util.Comparator;
16import java.util.List;
17
18import org.json.JSONArray;
19import org.json.JSONException;
20import org.json.JSONObject;
21
22import eu.siacs.conversations.Config;
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.getColumnIndex(HASH)),
105 Base64.decode(cursor.getString(cursor.getColumnIndex(VER)), Base64.DEFAULT),
106 new JSONObject(cursor.getString(cursor.getColumnIndex(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, (lhs, rhs) -> lhs.getFormType().compareTo(rhs.getFormType()));
221
222 for (Data form : forms) {
223 s.append(clean(form.getFormType())).append("<");
224 List<Field> fields = form.getFields();
225 Collections.sort(fields, (lhs, rhs) -> lhs.getFieldName().compareTo(rhs.getFieldName()));
226 for (Field field : fields) {
227 s.append(clean(field.getFieldName())).append("<");
228 List<String> values = field.getValues();
229 Collections.sort(values);
230 for (String value : values) {
231 s.append(blankNull(value)).append("<");
232 }
233 }
234 }
235
236 MessageDigest md;
237 try {
238 md = MessageDigest.getInstance("SHA-1");
239 } catch (NoSuchAlgorithmException e) {
240 return null;
241 }
242
243 try {
244 return md.digest(s.toString().getBytes("UTF-8"));
245 } catch (UnsupportedEncodingException e) {
246 return null;
247 }
248 }
249
250 private JSONObject toJSON() {
251 try {
252 JSONObject o = new JSONObject();
253
254 JSONArray ids = new JSONArray();
255 for (Identity id : this.getIdentities()) {
256 ids.put(id.toJSON());
257 }
258 o.put("identities", ids);
259
260 o.put("features", new JSONArray(this.getFeatures()));
261
262 JSONArray forms = new JSONArray();
263 for (Data data : this.forms) {
264 forms.put(createJSONFromForm(data));
265 }
266 o.put("forms", forms);
267
268 return o;
269 } catch (JSONException e) {
270 return null;
271 }
272 }
273
274 public ContentValues getContentValues() {
275 final ContentValues values = new ContentValues();
276 values.put(HASH, this.hash);
277 values.put(VER, getVer());
278 JSONObject jsonObject = toJSON();
279 values.put(RESULT, jsonObject == null ? "" : jsonObject.toString());
280 return values;
281 }
282
283 public static class Identity implements Comparable {
284 protected final String type;
285 protected final String lang;
286 protected final String name;
287 final String category;
288
289 Identity(final String category, final String type, final String lang, final String name) {
290 this.category = category;
291 this.type = type;
292 this.lang = lang;
293 this.name = name;
294 }
295
296 Identity(final Element el) {
297 this(
298 el.getAttribute("category"),
299 el.getAttribute("type"),
300 el.getAttribute("xml:lang"),
301 el.getAttribute("name")
302 );
303 }
304
305 Identity(final JSONObject o) {
306
307 this(
308 o.optString("category", null),
309 o.optString("type", null),
310 o.optString("lang", null),
311 o.optString("name", null)
312 );
313 }
314
315 public String getCategory() {
316 return this.category;
317 }
318
319 public String getType() {
320 return this.type;
321 }
322
323 public String getLang() {
324 return this.lang;
325 }
326
327 public String getName() {
328 return this.name;
329 }
330
331 public int compareTo(@NonNull Object other) {
332 Identity o = (Identity) other;
333 int r = blankNull(this.getCategory()).compareTo(blankNull(o.getCategory()));
334 if (r == 0) {
335 r = blankNull(this.getType()).compareTo(blankNull(o.getType()));
336 }
337 if (r == 0) {
338 r = blankNull(this.getLang()).compareTo(blankNull(o.getLang()));
339 }
340 if (r == 0) {
341 r = blankNull(this.getName()).compareTo(blankNull(o.getName()));
342 }
343
344 return r;
345 }
346
347 JSONObject toJSON() {
348 try {
349 JSONObject o = new JSONObject();
350 o.put("category", this.getCategory());
351 o.put("type", this.getType());
352 o.put("lang", this.getLang());
353 o.put("name", this.getName());
354 return o;
355 } catch (JSONException e) {
356 return null;
357 }
358 }
359 }
360}