2016-01-10 19:56:55 +00:00
|
|
|
package eu.siacs.conversations.entities;
|
|
|
|
|
2016-01-10 21:25:26 +00:00
|
|
|
import android.content.ContentValues;
|
2016-01-13 02:53:38 +00:00
|
|
|
import android.database.Cursor;
|
2021-01-18 17:26:46 +00:00
|
|
|
import androidx.annotation.NonNull;
|
2016-01-10 21:25:26 +00:00
|
|
|
import android.util.Base64;
|
2018-04-22 09:07:00 +00:00
|
|
|
import android.util.Log;
|
|
|
|
|
2020-04-20 13:53:52 +00:00
|
|
|
import com.google.common.base.Strings;
|
|
|
|
|
2016-01-10 21:25:26 +00:00
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
|
import java.lang.Comparable;
|
|
|
|
import java.security.MessageDigest;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
2016-01-10 19:56:55 +00:00
|
|
|
import java.util.ArrayList;
|
2016-01-10 21:25:26 +00:00
|
|
|
import java.util.Collections;
|
2016-02-03 16:19:05 +00:00
|
|
|
import java.util.Comparator;
|
2016-01-10 21:25:26 +00:00
|
|
|
import java.util.List;
|
2018-02-16 14:57:02 +00:00
|
|
|
|
2016-01-10 21:43:48 +00:00
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
2016-01-10 19:56:55 +00:00
|
|
|
|
2018-04-22 09:07:00 +00:00
|
|
|
import eu.siacs.conversations.Config;
|
2016-01-10 19:56:55 +00:00
|
|
|
import eu.siacs.conversations.xml.Element;
|
2018-02-16 14:57:02 +00:00
|
|
|
import eu.siacs.conversations.xml.Namespace;
|
2016-02-03 16:19:05 +00:00
|
|
|
import eu.siacs.conversations.xmpp.forms.Data;
|
2016-03-31 12:21:56 +00:00
|
|
|
import eu.siacs.conversations.xmpp.forms.Field;
|
2016-01-10 19:56:55 +00:00
|
|
|
import eu.siacs.conversations.xmpp.stanzas.IqPacket;
|
|
|
|
|
|
|
|
public class ServiceDiscoveryResult {
|
2016-01-13 02:53:38 +00:00
|
|
|
public static final String TABLENAME = "discovery_results";
|
|
|
|
public static final String HASH = "hash";
|
|
|
|
public static final String VER = "ver";
|
|
|
|
public static final String RESULT = "result";
|
|
|
|
protected final String hash;
|
|
|
|
protected final byte[] ver;
|
2016-01-10 19:56:55 +00:00
|
|
|
protected final List<String> features;
|
2016-02-03 16:19:05 +00:00
|
|
|
protected final List<Data> forms;
|
2018-04-22 09:07:00 +00:00
|
|
|
private final List<Identity> identities;
|
2016-01-10 19:56:55 +00:00
|
|
|
public ServiceDiscoveryResult(final IqPacket packet) {
|
|
|
|
this.identities = new ArrayList<>();
|
|
|
|
this.features = new ArrayList<>();
|
2016-02-03 16:19:05 +00:00
|
|
|
this.forms = new ArrayList<>();
|
2016-01-13 02:53:38 +00:00
|
|
|
this.hash = "sha-1"; // We only support sha-1 for now
|
2016-01-10 19:56:55 +00:00
|
|
|
|
|
|
|
final List<Element> elements = packet.query().getChildren();
|
|
|
|
|
|
|
|
for (final Element element : elements) {
|
|
|
|
if (element.getName().equals("identity")) {
|
|
|
|
Identity id = new Identity(element);
|
|
|
|
if (id.getType() != null && id.getCategory() != null) {
|
|
|
|
identities.add(id);
|
|
|
|
}
|
|
|
|
} else if (element.getName().equals("feature")) {
|
2016-01-10 21:25:26 +00:00
|
|
|
if (element.getAttribute("var") != null) {
|
|
|
|
features.add(element.getAttribute("var"));
|
|
|
|
}
|
2018-02-16 14:57:02 +00:00
|
|
|
} else if (element.getName().equals("x") && element.getAttribute("xmlns").equals(Namespace.DATA)) {
|
2016-02-03 16:19:05 +00:00
|
|
|
forms.add(Data.parse(element));
|
2016-01-10 19:56:55 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-13 02:53:38 +00:00
|
|
|
this.ver = this.mkCapHash();
|
|
|
|
}
|
2018-04-22 09:07:00 +00:00
|
|
|
private ServiceDiscoveryResult(String hash, byte[] ver, JSONObject o) throws JSONException {
|
2016-01-13 02:53:38 +00:00
|
|
|
this.identities = new ArrayList<>();
|
|
|
|
this.features = new ArrayList<>();
|
2016-02-03 16:19:05 +00:00
|
|
|
this.forms = new ArrayList<>();
|
2016-01-13 02:53:38 +00:00
|
|
|
this.hash = hash;
|
|
|
|
this.ver = ver;
|
|
|
|
|
2016-05-19 08:41:56 +00:00
|
|
|
JSONArray identities = o.optJSONArray("identities");
|
2016-02-03 09:40:02 +00:00
|
|
|
if (identities != null) {
|
|
|
|
for (int i = 0; i < identities.length(); i++) {
|
|
|
|
this.identities.add(new Identity(identities.getJSONObject(i)));
|
|
|
|
}
|
2016-01-13 02:53:38 +00:00
|
|
|
}
|
|
|
|
JSONArray features = o.optJSONArray("features");
|
2016-02-03 09:40:02 +00:00
|
|
|
if (features != null) {
|
|
|
|
for (int i = 0; i < features.length(); i++) {
|
|
|
|
this.features.add(features.getString(i));
|
|
|
|
}
|
2016-01-13 02:53:38 +00:00
|
|
|
}
|
2016-05-19 08:41:56 +00:00
|
|
|
JSONArray forms = o.optJSONArray("forms");
|
|
|
|
if (forms != null) {
|
2018-04-22 09:07:00 +00:00
|
|
|
for (int i = 0; i < forms.length(); i++) {
|
2016-05-19 08:41:56 +00:00
|
|
|
this.forms.add(createFormFromJSONObject(forms.getJSONObject(i)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-13 19:18:32 +00:00
|
|
|
|
|
|
|
private ServiceDiscoveryResult() {
|
|
|
|
this.hash = "sha-1";
|
|
|
|
this.features = Collections.emptyList();
|
|
|
|
this.identities = Collections.emptyList();
|
|
|
|
this.ver = null;
|
|
|
|
this.forms = Collections.emptyList();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ServiceDiscoveryResult empty() {
|
|
|
|
return new ServiceDiscoveryResult();
|
|
|
|
}
|
2016-05-19 08:41:56 +00:00
|
|
|
|
2018-04-22 09:07:00 +00:00
|
|
|
public ServiceDiscoveryResult(Cursor cursor) throws JSONException {
|
|
|
|
this(
|
|
|
|
cursor.getString(cursor.getColumnIndex(HASH)),
|
|
|
|
Base64.decode(cursor.getString(cursor.getColumnIndex(VER)), Base64.DEFAULT),
|
|
|
|
new JSONObject(cursor.getString(cursor.getColumnIndex(RESULT)))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static String clean(String s) {
|
|
|
|
return s.replace("<","<");
|
|
|
|
}
|
|
|
|
|
|
|
|
private static String blankNull(String s) {
|
|
|
|
return s == null ? "" : clean(s);
|
|
|
|
}
|
|
|
|
|
2016-05-19 08:41:56 +00:00
|
|
|
private static Data createFormFromJSONObject(JSONObject o) {
|
|
|
|
Data data = new Data();
|
|
|
|
JSONArray names = o.names();
|
2018-04-22 09:07:00 +00:00
|
|
|
for (int i = 0; i < names.length(); ++i) {
|
2016-05-19 08:41:56 +00:00
|
|
|
try {
|
|
|
|
String name = names.getString(i);
|
|
|
|
JSONArray jsonValues = o.getJSONArray(name);
|
|
|
|
ArrayList<String> values = new ArrayList<>(jsonValues.length());
|
2018-04-22 09:07:00 +00:00
|
|
|
for (int j = 0; j < jsonValues.length(); ++j) {
|
2016-05-19 08:41:56 +00:00
|
|
|
values.add(jsonValues.getString(j));
|
|
|
|
}
|
|
|
|
data.put(name, values);
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static JSONObject createJSONFromForm(Data data) {
|
|
|
|
JSONObject object = new JSONObject();
|
2018-04-22 09:07:00 +00:00
|
|
|
for (Field field : data.getFields()) {
|
2016-05-19 08:41:56 +00:00
|
|
|
try {
|
|
|
|
JSONArray jsonValues = new JSONArray();
|
2018-04-22 09:07:00 +00:00
|
|
|
for (String value : field.getValues()) {
|
2016-05-19 08:41:56 +00:00
|
|
|
jsonValues.put(value);
|
|
|
|
}
|
|
|
|
object.put(field.getFieldName(), jsonValues);
|
2018-04-22 09:07:00 +00:00
|
|
|
} catch (Exception e) {
|
2016-05-19 08:41:56 +00:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
JSONArray jsonValues = new JSONArray();
|
|
|
|
jsonValues.put(data.getFormType());
|
|
|
|
object.put(Data.FORM_TYPE, jsonValues);
|
2018-04-22 09:07:00 +00:00
|
|
|
} catch (Exception e) {
|
2016-05-19 08:41:56 +00:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return object;
|
2016-01-13 02:53:38 +00:00
|
|
|
}
|
|
|
|
|
2016-02-03 09:40:02 +00:00
|
|
|
public String getVer() {
|
2019-12-04 16:35:06 +00:00
|
|
|
return Base64.encodeToString(this.ver, Base64.NO_WRAP);
|
2016-02-03 09:40:02 +00:00
|
|
|
}
|
|
|
|
|
2016-01-10 19:56:55 +00:00
|
|
|
public List<Identity> getIdentities() {
|
|
|
|
return this.identities;
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<String> getFeatures() {
|
|
|
|
return this.features;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasIdentity(String category, String type) {
|
2018-04-22 09:07:00 +00:00
|
|
|
for (Identity id : this.getIdentities()) {
|
|
|
|
if ((category == null || id.getCategory().equals(category)) &&
|
|
|
|
(type == null || id.getType().equals(type))) {
|
2016-01-10 19:56:55 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-10 21:25:26 +00:00
|
|
|
|
2016-03-31 19:56:59 +00:00
|
|
|
public String getExtendedDiscoInformation(String formType, String name) {
|
2018-04-22 09:07:00 +00:00
|
|
|
for (Data form : this.forms) {
|
2016-03-31 19:56:59 +00:00
|
|
|
if (formType.equals(form.getFormType())) {
|
2018-04-22 09:07:00 +00:00
|
|
|
for (Field field : form.getFields()) {
|
2016-03-31 19:56:59 +00:00
|
|
|
if (name.equals(field.getFieldName())) {
|
|
|
|
return field.getValue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-04-22 09:07:00 +00:00
|
|
|
private byte[] mkCapHash() {
|
2016-01-10 21:25:26 +00:00
|
|
|
StringBuilder s = new StringBuilder();
|
|
|
|
|
|
|
|
List<Identity> identities = this.getIdentities();
|
|
|
|
Collections.sort(identities);
|
|
|
|
|
2018-04-22 09:07:00 +00:00
|
|
|
for (Identity id : identities) {
|
|
|
|
s.append(blankNull(id.getCategory()))
|
|
|
|
.append("/")
|
|
|
|
.append(blankNull(id.getType()))
|
|
|
|
.append("/")
|
|
|
|
.append(blankNull(id.getLang()))
|
|
|
|
.append("/")
|
|
|
|
.append(blankNull(id.getName()))
|
|
|
|
.append("<");
|
2016-01-10 21:25:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
List<String> features = this.getFeatures();
|
|
|
|
Collections.sort(features);
|
|
|
|
|
|
|
|
for (String feature : features) {
|
2018-04-22 09:07:00 +00:00
|
|
|
s.append(clean(feature)).append("<");
|
2016-01-10 21:25:26 +00:00
|
|
|
}
|
|
|
|
|
2018-04-22 09:07:00 +00:00
|
|
|
Collections.sort(forms, (lhs, rhs) -> lhs.getFormType().compareTo(rhs.getFormType()));
|
2016-02-03 16:19:05 +00:00
|
|
|
|
2018-04-22 09:07:00 +00:00
|
|
|
for (Data form : forms) {
|
|
|
|
s.append(clean(form.getFormType())).append("<");
|
2016-03-31 12:21:56 +00:00
|
|
|
List<Field> fields = form.getFields();
|
2020-04-20 13:53:52 +00:00
|
|
|
Collections.sort(fields, (lhs, rhs) -> Strings.nullToEmpty(lhs.getFieldName()).compareTo(Strings.nullToEmpty(rhs.getFieldName())));
|
2018-04-22 09:07:00 +00:00
|
|
|
for (Field field : fields) {
|
2020-04-20 13:53:52 +00:00
|
|
|
s.append(Strings.nullToEmpty(field.getFieldName())).append("<");
|
2016-03-31 12:21:56 +00:00
|
|
|
List<String> values = field.getValues();
|
|
|
|
Collections.sort(values);
|
2018-04-22 09:07:00 +00:00
|
|
|
for (String value : values) {
|
|
|
|
s.append(blankNull(value)).append("<");
|
2016-03-31 12:21:56 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-03 16:19:05 +00:00
|
|
|
}
|
2016-01-10 21:25:26 +00:00
|
|
|
|
|
|
|
MessageDigest md;
|
|
|
|
try {
|
|
|
|
md = MessageDigest.getInstance("SHA-1");
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return md.digest(s.toString().getBytes("UTF-8"));
|
2018-04-22 09:07:00 +00:00
|
|
|
} catch (UnsupportedEncodingException e) {
|
2016-01-10 21:25:26 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-22 09:07:00 +00:00
|
|
|
private JSONObject toJSON() {
|
2016-01-10 21:43:48 +00:00
|
|
|
try {
|
|
|
|
JSONObject o = new JSONObject();
|
|
|
|
|
|
|
|
JSONArray ids = new JSONArray();
|
2018-04-22 09:07:00 +00:00
|
|
|
for (Identity id : this.getIdentities()) {
|
2016-01-10 21:43:48 +00:00
|
|
|
ids.put(id.toJSON());
|
|
|
|
}
|
2016-05-19 08:41:56 +00:00
|
|
|
o.put("identities", ids);
|
2016-01-10 21:43:48 +00:00
|
|
|
|
|
|
|
o.put("features", new JSONArray(this.getFeatures()));
|
|
|
|
|
2016-05-19 08:41:56 +00:00
|
|
|
JSONArray forms = new JSONArray();
|
2018-04-22 09:07:00 +00:00
|
|
|
for (Data data : this.forms) {
|
2016-05-19 08:41:56 +00:00
|
|
|
forms.put(createJSONFromForm(data));
|
|
|
|
}
|
|
|
|
o.put("forms", forms);
|
|
|
|
|
2016-01-10 21:43:48 +00:00
|
|
|
return o;
|
2018-04-22 09:07:00 +00:00
|
|
|
} catch (JSONException e) {
|
2016-01-10 21:43:48 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-13 02:53:38 +00:00
|
|
|
public ContentValues getContentValues() {
|
|
|
|
final ContentValues values = new ContentValues();
|
|
|
|
values.put(HASH, this.hash);
|
2016-02-03 09:40:02 +00:00
|
|
|
values.put(VER, getVer());
|
2018-04-22 09:07:00 +00:00
|
|
|
JSONObject jsonObject = toJSON();
|
|
|
|
values.put(RESULT, jsonObject == null ? "" : jsonObject.toString());
|
2016-01-13 02:53:38 +00:00
|
|
|
return values;
|
|
|
|
}
|
2018-04-22 09:07:00 +00:00
|
|
|
|
|
|
|
public static class Identity implements Comparable {
|
|
|
|
protected final String type;
|
|
|
|
protected final String lang;
|
|
|
|
protected final String name;
|
|
|
|
final String category;
|
|
|
|
|
|
|
|
Identity(final String category, final String type, final String lang, final String name) {
|
|
|
|
this.category = category;
|
|
|
|
this.type = type;
|
|
|
|
this.lang = lang;
|
|
|
|
this.name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
Identity(final Element el) {
|
|
|
|
this(
|
|
|
|
el.getAttribute("category"),
|
|
|
|
el.getAttribute("type"),
|
|
|
|
el.getAttribute("xml:lang"),
|
|
|
|
el.getAttribute("name")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Identity(final JSONObject o) {
|
|
|
|
|
|
|
|
this(
|
|
|
|
o.optString("category", null),
|
|
|
|
o.optString("type", null),
|
|
|
|
o.optString("lang", null),
|
|
|
|
o.optString("name", null)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getCategory() {
|
|
|
|
return this.category;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getType() {
|
|
|
|
return this.type;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getLang() {
|
|
|
|
return this.lang;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getName() {
|
|
|
|
return this.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int compareTo(@NonNull Object other) {
|
|
|
|
Identity o = (Identity) other;
|
|
|
|
int r = blankNull(this.getCategory()).compareTo(blankNull(o.getCategory()));
|
|
|
|
if (r == 0) {
|
|
|
|
r = blankNull(this.getType()).compareTo(blankNull(o.getType()));
|
|
|
|
}
|
|
|
|
if (r == 0) {
|
|
|
|
r = blankNull(this.getLang()).compareTo(blankNull(o.getLang()));
|
|
|
|
}
|
|
|
|
if (r == 0) {
|
|
|
|
r = blankNull(this.getName()).compareTo(blankNull(o.getName()));
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSONObject toJSON() {
|
|
|
|
try {
|
|
|
|
JSONObject o = new JSONObject();
|
|
|
|
o.put("category", this.getCategory());
|
|
|
|
o.put("type", this.getType());
|
|
|
|
o.put("lang", this.getLang());
|
|
|
|
o.put("name", this.getName());
|
|
|
|
return o;
|
|
|
|
} catch (JSONException e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-10 19:56:55 +00:00
|
|
|
}
|