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