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