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