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 public ServiceDiscoveryResult(Cursor cursor) throws JSONException {
91 this(
92 cursor.getString(cursor.getColumnIndex(HASH)),
93 Base64.decode(cursor.getString(cursor.getColumnIndex(VER)), Base64.DEFAULT),
94 new JSONObject(cursor.getString(cursor.getColumnIndex(RESULT)))
95 );
96 }
97
98 private static String clean(String s) {
99 return s.replace("<","<");
100 }
101
102 private static String blankNull(String s) {
103 return s == null ? "" : clean(s);
104 }
105
106 private static Data createFormFromJSONObject(JSONObject o) {
107 Data data = new Data();
108 JSONArray names = o.names();
109 for (int i = 0; i < names.length(); ++i) {
110 try {
111 String name = names.getString(i);
112 JSONArray jsonValues = o.getJSONArray(name);
113 ArrayList<String> values = new ArrayList<>(jsonValues.length());
114 for (int j = 0; j < jsonValues.length(); ++j) {
115 values.add(jsonValues.getString(j));
116 }
117 data.put(name, values);
118 } catch (Exception e) {
119 e.printStackTrace();
120 }
121 }
122 return data;
123 }
124
125 private static JSONObject createJSONFromForm(Data data) {
126 JSONObject object = new JSONObject();
127 for (Field field : data.getFields()) {
128 try {
129 JSONArray jsonValues = new JSONArray();
130 for (String value : field.getValues()) {
131 jsonValues.put(value);
132 }
133 object.put(field.getFieldName(), jsonValues);
134 } catch (Exception e) {
135 e.printStackTrace();
136 }
137 }
138 try {
139 JSONArray jsonValues = new JSONArray();
140 jsonValues.put(data.getFormType());
141 object.put(Data.FORM_TYPE, jsonValues);
142 } catch (Exception e) {
143 e.printStackTrace();
144 }
145 return object;
146 }
147
148 public String getVer() {
149 return new String(Base64.encode(this.ver, Base64.DEFAULT)).trim();
150 }
151
152 public List<Identity> getIdentities() {
153 return this.identities;
154 }
155
156 public List<String> getFeatures() {
157 return this.features;
158 }
159
160 public boolean hasIdentity(String category, String type) {
161 for (Identity id : this.getIdentities()) {
162 if ((category == null || id.getCategory().equals(category)) &&
163 (type == null || id.getType().equals(type))) {
164 return true;
165 }
166 }
167
168 return false;
169 }
170
171 public String getExtendedDiscoInformation(String formType, String name) {
172 for (Data form : this.forms) {
173 if (formType.equals(form.getFormType())) {
174 for (Field field : form.getFields()) {
175 if (name.equals(field.getFieldName())) {
176 return field.getValue();
177 }
178 }
179 }
180 }
181 return null;
182 }
183
184 private byte[] mkCapHash() {
185 StringBuilder s = new StringBuilder();
186
187 List<Identity> identities = this.getIdentities();
188 Collections.sort(identities);
189
190 for (Identity id : identities) {
191 s.append(blankNull(id.getCategory()))
192 .append("/")
193 .append(blankNull(id.getType()))
194 .append("/")
195 .append(blankNull(id.getLang()))
196 .append("/")
197 .append(blankNull(id.getName()))
198 .append("<");
199 }
200
201 List<String> features = this.getFeatures();
202 Collections.sort(features);
203
204 for (String feature : features) {
205 s.append(clean(feature)).append("<");
206 }
207
208 Collections.sort(forms, (lhs, rhs) -> lhs.getFormType().compareTo(rhs.getFormType()));
209
210 for (Data form : forms) {
211 s.append(clean(form.getFormType())).append("<");
212 List<Field> fields = form.getFields();
213 Collections.sort(fields, (lhs, rhs) -> lhs.getFieldName().compareTo(rhs.getFieldName()));
214 for (Field field : fields) {
215 s.append(clean(field.getFieldName())).append("<");
216 List<String> values = field.getValues();
217 Collections.sort(values);
218 for (String value : values) {
219 s.append(blankNull(value)).append("<");
220 }
221 }
222 }
223
224 MessageDigest md;
225 try {
226 md = MessageDigest.getInstance("SHA-1");
227 } catch (NoSuchAlgorithmException e) {
228 return null;
229 }
230
231 try {
232 return md.digest(s.toString().getBytes("UTF-8"));
233 } catch (UnsupportedEncodingException e) {
234 return null;
235 }
236 }
237
238 private JSONObject toJSON() {
239 try {
240 JSONObject o = new JSONObject();
241
242 JSONArray ids = new JSONArray();
243 for (Identity id : this.getIdentities()) {
244 ids.put(id.toJSON());
245 }
246 o.put("identities", ids);
247
248 o.put("features", new JSONArray(this.getFeatures()));
249
250 JSONArray forms = new JSONArray();
251 for (Data data : this.forms) {
252 forms.put(createJSONFromForm(data));
253 }
254 o.put("forms", forms);
255
256 return o;
257 } catch (JSONException e) {
258 return null;
259 }
260 }
261
262 public ContentValues getContentValues() {
263 final ContentValues values = new ContentValues();
264 values.put(HASH, this.hash);
265 values.put(VER, getVer());
266 JSONObject jsonObject = toJSON();
267 values.put(RESULT, jsonObject == null ? "" : jsonObject.toString());
268 return values;
269 }
270
271 public static class Identity implements Comparable {
272 protected final String type;
273 protected final String lang;
274 protected final String name;
275 final String category;
276
277 Identity(final String category, final String type, final String lang, final String name) {
278 this.category = category;
279 this.type = type;
280 this.lang = lang;
281 this.name = name;
282 }
283
284 Identity(final Element el) {
285 this(
286 el.getAttribute("category"),
287 el.getAttribute("type"),
288 el.getAttribute("xml:lang"),
289 el.getAttribute("name")
290 );
291 }
292
293 Identity(final JSONObject o) {
294
295 this(
296 o.optString("category", null),
297 o.optString("type", null),
298 o.optString("lang", null),
299 o.optString("name", null)
300 );
301 }
302
303 public String getCategory() {
304 return this.category;
305 }
306
307 public String getType() {
308 return this.type;
309 }
310
311 public String getLang() {
312 return this.lang;
313 }
314
315 public String getName() {
316 return this.name;
317 }
318
319 public int compareTo(@NonNull Object other) {
320 Identity o = (Identity) other;
321 int r = blankNull(this.getCategory()).compareTo(blankNull(o.getCategory()));
322 if (r == 0) {
323 r = blankNull(this.getType()).compareTo(blankNull(o.getType()));
324 }
325 if (r == 0) {
326 r = blankNull(this.getLang()).compareTo(blankNull(o.getLang()));
327 }
328 if (r == 0) {
329 r = blankNull(this.getName()).compareTo(blankNull(o.getName()));
330 }
331
332 return r;
333 }
334
335 JSONObject toJSON() {
336 try {
337 JSONObject o = new JSONObject();
338 o.put("category", this.getCategory());
339 o.put("type", this.getType());
340 o.put("lang", this.getLang());
341 o.put("name", this.getName());
342 return o;
343 } catch (JSONException e) {
344 return null;
345 }
346 }
347 }
348}