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