1package eu.siacs.conversations.generator;
2
3import android.util.Base64;
4
5import java.security.MessageDigest;
6import java.security.NoSuchAlgorithmException;
7import java.text.SimpleDateFormat;
8import java.util.ArrayList;
9import java.util.Arrays;
10import java.util.Collections;
11import java.util.List;
12import java.util.Locale;
13import java.util.TimeZone;
14
15import eu.siacs.conversations.Config;
16import eu.siacs.conversations.R;
17import eu.siacs.conversations.crypto.axolotl.AxolotlService;
18import eu.siacs.conversations.services.XmppConnectionService;
19import eu.siacs.conversations.utils.PhoneHelper;
20import eu.siacs.conversations.xml.Namespace;
21import eu.siacs.conversations.xmpp.jingle.stanzas.Content;
22
23public abstract class AbstractGenerator {
24 private final String[] FEATURES = {
25 "urn:xmpp:jingle:1",
26 Content.Version.FT_3.getNamespace(),
27 Content.Version.FT_4.getNamespace(),
28 Content.Version.FT_5.getNamespace(),
29 "urn:xmpp:jingle:transports:s5b:1",
30 "urn:xmpp:jingle:transports:ibb:1",
31 "http://jabber.org/protocol/muc",
32 "jabber:x:conference",
33 Namespace.OOB,
34 "http://jabber.org/protocol/caps",
35 "http://jabber.org/protocol/disco#info",
36 "urn:xmpp:avatar:metadata+notify",
37 "http://jabber.org/protocol/nick+notify",
38 "urn:xmpp:ping",
39 "jabber:iq:version",
40 "http://jabber.org/protocol/chatstates"
41 };
42 private final String[] MESSAGE_CONFIRMATION_FEATURES = {
43 "urn:xmpp:chat-markers:0",
44 "urn:xmpp:receipts"
45 };
46 private final String[] MESSAGE_CORRECTION_FEATURES = {
47 "urn:xmpp:message-correct:0"
48 };
49 private final String[] PRIVACY_SENSITIVE = {
50 "urn:xmpp:time" //XEP-0202: Entity Time leaks time zone
51 };
52 private final String[] OTR = {
53 "urn:xmpp:otr:0"
54 };
55 private String mVersion = null;
56
57 private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
58
59 protected XmppConnectionService mXmppConnectionService;
60
61 protected AbstractGenerator(XmppConnectionService service) {
62 this.mXmppConnectionService = service;
63 }
64
65 protected String getIdentityVersion() {
66 if (mVersion == null) {
67 this.mVersion = PhoneHelper.getVersionName(mXmppConnectionService);
68 }
69 return this.mVersion;
70 }
71
72 public String getIdentityName() {
73 return mXmppConnectionService.getString(R.string.app_name) + " " + getIdentityVersion();
74 }
75
76 public String getIdentityType() {
77 if ("chromium".equals(android.os.Build.BRAND)) {
78 return "pc";
79 } else {
80 return mXmppConnectionService.getString(R.string.default_resource).toLowerCase();
81 }
82 }
83
84 public String getCapHash() {
85 StringBuilder s = new StringBuilder();
86 s.append("client/" + getIdentityType() + "//" + getIdentityName() + "<");
87 MessageDigest md;
88 try {
89 md = MessageDigest.getInstance("SHA-1");
90 } catch (NoSuchAlgorithmException e) {
91 return null;
92 }
93
94 for (String feature : getFeatures()) {
95 s.append(feature + "<");
96 }
97 byte[] sha1 = md.digest(s.toString().getBytes());
98 return new String(Base64.encode(sha1, Base64.DEFAULT)).trim();
99 }
100
101 public static String getTimestamp(long time) {
102 DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
103 return DATE_FORMAT.format(time);
104 }
105
106 public List<String> getFeatures() {
107 ArrayList<String> features = new ArrayList<>();
108 features.addAll(Arrays.asList(FEATURES));
109 if (mXmppConnectionService.confirmMessages()) {
110 features.addAll(Arrays.asList(MESSAGE_CONFIRMATION_FEATURES));
111 }
112 if (mXmppConnectionService.allowMessageCorrection()) {
113 features.addAll(Arrays.asList(MESSAGE_CORRECTION_FEATURES));
114 }
115 if (Config.supportOmemo()) {
116 features.add(AxolotlService.PEP_DEVICE_LIST_NOTIFY);
117 }
118 if (!mXmppConnectionService.useTorToConnect()) {
119 features.addAll(Arrays.asList(PRIVACY_SENSITIVE));
120 }
121 if (mXmppConnectionService.broadcastLastActivity()) {
122 features.add(Namespace.IDLE);
123 }
124 Collections.sort(features);
125 return features;
126 }
127}