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