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;
14import org.json.JSONArray;
15import org.json.JSONException;
16import org.json.JSONObject;
17
18import eu.siacs.conversations.xml.Element;
19import eu.siacs.conversations.xmpp.forms.Data;
20import eu.siacs.conversations.xmpp.forms.Field;
21import eu.siacs.conversations.xmpp.stanzas.IqPacket;
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
29 protected static String blankNull(String s) {
30 return s == null ? "" : s;
31 }
32
33 public static class Identity implements Comparable {
34 protected final String category;
35 protected final String type;
36 protected final String lang;
37 protected final String name;
38
39 public Identity(final String category, final String type, final String lang, final String name) {
40 this.category = category;
41 this.type = type;
42 this.lang = lang;
43 this.name = name;
44 }
45
46 public Identity(final Element el) {
47 this(
48 el.getAttribute("category"),
49 el.getAttribute("type"),
50 el.getAttribute("xml:lang"),
51 el.getAttribute("name")
52 );
53 }
54
55 public Identity(final JSONObject o) {
56 this(
57 o.optString("category", null),
58 o.optString("type", null),
59 o.optString("lang", null),
60 o.optString("name", null)
61 );
62 }
63
64 public String getCategory() {
65 return this.category;
66 }
67
68 public String getType() {
69 return this.type;
70 }
71
72 public String getLang() {
73 return this.lang;
74 }
75
76 public String getName() {
77 return this.name;
78 }
79
80 public int compareTo(Object other) {
81 Identity o = (Identity)other;
82 int r = blankNull(this.getCategory()).compareTo(blankNull(o.getCategory()));
83 if(r == 0) {
84 r = blankNull(this.getType()).compareTo(blankNull(o.getType()));
85 }
86 if(r == 0) {
87 r = blankNull(this.getLang()).compareTo(blankNull(o.getLang()));
88 }
89 if(r == 0) {
90 r = blankNull(this.getName()).compareTo(blankNull(o.getName()));
91 }
92
93 return r;
94 }
95
96 public JSONObject toJSON() {
97 try {
98 JSONObject o = new JSONObject();
99 o.put("category", this.getCategory());
100 o.put("type", this.getType());
101 o.put("lang", this.getLang());
102 o.put("name", this.getName());
103 return o;
104 } catch(JSONException e) {
105 return null;
106 }
107 }
108 }
109
110 protected final String hash;
111 protected final byte[] ver;
112 protected final List<Identity> identities;
113 protected final List<String> features;
114 protected final List<Data> forms;
115
116 public ServiceDiscoveryResult(final IqPacket packet) {
117 this.identities = new ArrayList<>();
118 this.features = new ArrayList<>();
119 this.forms = new ArrayList<>();
120 this.hash = "sha-1"; // We only support sha-1 for now
121
122 final List<Element> elements = packet.query().getChildren();
123
124 for (final Element element : elements) {
125 if (element.getName().equals("identity")) {
126 Identity id = new Identity(element);
127 if (id.getType() != null && id.getCategory() != null) {
128 identities.add(id);
129 }
130 } else if (element.getName().equals("feature")) {
131 if (element.getAttribute("var") != null) {
132 features.add(element.getAttribute("var"));
133 }
134 } else if (element.getName().equals("x") && "jabber:x:data".equals(element.getAttribute("xmlns"))) {
135 forms.add(Data.parse(element));
136 }
137 }
138 this.ver = this.mkCapHash();
139 }
140
141 public ServiceDiscoveryResult(String hash, byte[] ver, JSONObject o) throws JSONException {
142 this.identities = new ArrayList<>();
143 this.features = new ArrayList<>();
144 this.forms = new ArrayList<>();
145 this.hash = hash;
146 this.ver = ver;
147
148 JSONArray identities = o.optJSONArray("identities");
149 if (identities != null) {
150 for (int i = 0; i < identities.length(); i++) {
151 this.identities.add(new Identity(identities.getJSONObject(i)));
152 }
153 }
154 JSONArray features = o.optJSONArray("features");
155 if (features != null) {
156 for (int i = 0; i < features.length(); i++) {
157 this.features.add(features.getString(i));
158 }
159 }
160 }
161
162 public String getVer() {
163 return new String(Base64.encode(this.ver, Base64.DEFAULT)).trim();
164 }
165
166 public ServiceDiscoveryResult(Cursor cursor) throws JSONException {
167 this(
168 cursor.getString(cursor.getColumnIndex(HASH)),
169 Base64.decode(cursor.getString(cursor.getColumnIndex(VER)), Base64.DEFAULT),
170 new JSONObject(cursor.getString(cursor.getColumnIndex(RESULT)))
171 );
172 }
173
174 public List<Identity> getIdentities() {
175 return this.identities;
176 }
177
178 public List<String> getFeatures() {
179 return this.features;
180 }
181
182 public boolean hasIdentity(String category, String type) {
183 for(Identity id : this.getIdentities()) {
184 if((category == null || id.getCategory().equals(category)) &&
185 (type == null || id.getType().equals(type))) {
186 return true;
187 }
188 }
189
190 return false;
191 }
192
193 protected byte[] mkCapHash() {
194 StringBuilder s = new StringBuilder();
195
196 List<Identity> identities = this.getIdentities();
197 Collections.sort(identities);
198
199 for(Identity id : identities) {
200 s.append(
201 blankNull(id.getCategory()) + "/" +
202 blankNull(id.getType()) + "/" +
203 blankNull(id.getLang()) + "/" +
204 blankNull(id.getName()) + "<"
205 );
206 }
207
208 List<String> features = this.getFeatures();
209 Collections.sort(features);
210
211 for (String feature : features) {
212 s.append(feature + "<");
213 }
214
215 Collections.sort(forms, new Comparator<Data>() {
216 @Override
217 public int compare(Data lhs, Data rhs) {
218 return lhs.getFormType().compareTo(rhs.getFormType());
219 }
220 });
221
222 for(Data form : forms) {
223 s.append(form.getFormType() + "<");
224 List<Field> fields = form.getFields();
225 Collections.sort(fields, new Comparator<Field>() {
226 @Override
227 public int compare(Field lhs, Field rhs) {
228 return lhs.getFieldName().compareTo(rhs.getFieldName());
229 }
230 });
231 for(Field field : fields) {
232 s.append(field.getFieldName()+"<");
233 List<String> values = field.getValues();
234 Collections.sort(values);
235 for(String value : values) {
236 s.append(value+"<");
237 }
238 }
239 }
240
241 MessageDigest md;
242 try {
243 md = MessageDigest.getInstance("SHA-1");
244 } catch (NoSuchAlgorithmException e) {
245 return null;
246 }
247
248 try {
249 return md.digest(s.toString().getBytes("UTF-8"));
250 } catch(UnsupportedEncodingException e) {
251 return null;
252 }
253 }
254
255 public JSONObject toJSON() {
256 try {
257 JSONObject o = new JSONObject();
258
259 JSONArray ids = new JSONArray();
260 for(Identity id : this.getIdentities()) {
261 ids.put(id.toJSON());
262 }
263 o.put("identites", ids);
264
265 o.put("features", new JSONArray(this.getFeatures()));
266
267 return o;
268 } catch(JSONException e) {
269 return null;
270 }
271 }
272
273 public ContentValues getContentValues() {
274 final ContentValues values = new ContentValues();
275 values.put(HASH, this.hash);
276 values.put(VER, getVer());
277 values.put(RESULT, this.toJSON().toString());
278 return values;
279 }
280}