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