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.services.XmppConnectionService;
16import eu.siacs.conversations.utils.PhoneHelper;
17
18public abstract class AbstractGenerator {
19 private final String[] FEATURES = {
20 "urn:xmpp:jingle:1",
21 "urn:xmpp:jingle:apps:file-transfer:3",
22 "urn:xmpp:jingle:transports:s5b:1",
23 "urn:xmpp:jingle:transports:ibb:1",
24 "http://jabber.org/protocol/muc",
25 "jabber:x:conference",
26 "http://jabber.org/protocol/caps",
27 "http://jabber.org/protocol/disco#info",
28 "urn:xmpp:avatar:metadata+notify",
29 "urn:xmpp:ping",
30 "jabber:iq:version"};
31 private final String[] MESSAGE_CONFIRMATION_FEATURES = {
32 "urn:xmpp:chat-markers:0",
33 "urn:xmpp:receipts"
34 };
35 private String mVersion = null;
36 public final String IDENTITY_NAME = "Conversations";
37 public final String IDENTITY_TYPE = "phone";
38
39 private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
40
41 protected XmppConnectionService mXmppConnectionService;
42
43 protected AbstractGenerator(XmppConnectionService service) {
44 this.mXmppConnectionService = service;
45 }
46
47 protected String getIdentityVersion() {
48 if (mVersion == null) {
49 this.mVersion = PhoneHelper.getVersionName(mXmppConnectionService);
50 }
51 return this.mVersion;
52 }
53
54 protected String getIdentityName() {
55 return IDENTITY_NAME + " " + getIdentityVersion();
56 }
57
58 public String getCapHash() {
59 StringBuilder s = new StringBuilder();
60 s.append("client/" + IDENTITY_TYPE + "//" + getIdentityName() + "<");
61 MessageDigest md;
62 try {
63 md = MessageDigest.getInstance("SHA-1");
64 } catch (NoSuchAlgorithmException e) {
65 return null;
66 }
67
68 for (String feature : getFeatures()) {
69 s.append(feature + "<");
70 }
71 byte[] sha1 = md.digest(s.toString().getBytes());
72 return new String(Base64.encode(sha1, Base64.DEFAULT)).trim();
73 }
74
75 public static String getTimestamp(long time) {
76 DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
77 return DATE_FORMAT.format(time);
78 }
79
80 public List<String> getFeatures() {
81 ArrayList<String> features = new ArrayList<>();
82 features.addAll(Arrays.asList(FEATURES));
83 if (mXmppConnectionService.confirmMessages()) {
84 features.addAll(Arrays.asList(MESSAGE_CONFIRMATION_FEATURES));
85 }
86 Collections.sort(features);
87 return features;
88 }
89}