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