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 "http://jabber.org/protocol/chatstates"};
32 private final String[] MESSAGE_CONFIRMATION_FEATURES = {
33 "urn:xmpp:chat-markers:0",
34 "urn:xmpp:receipts"
35 };
36 private String mVersion = null;
37 public final String IDENTITY_NAME = "Conversations";
38 public final String IDENTITY_TYPE = "phone";
39
40 private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
41
42 protected XmppConnectionService mXmppConnectionService;
43
44 protected AbstractGenerator(XmppConnectionService service) {
45 this.mXmppConnectionService = service;
46 }
47
48 protected String getIdentityVersion() {
49 if (mVersion == null) {
50 this.mVersion = PhoneHelper.getVersionName(mXmppConnectionService);
51 }
52 return this.mVersion;
53 }
54
55 protected String getIdentityName() {
56 return IDENTITY_NAME + " " + getIdentityVersion();
57 }
58
59 public String getCapHash() {
60 StringBuilder s = new StringBuilder();
61 s.append("client/" + IDENTITY_TYPE + "//" + getIdentityName() + "<");
62 MessageDigest md;
63 try {
64 md = MessageDigest.getInstance("SHA-1");
65 } catch (NoSuchAlgorithmException e) {
66 return null;
67 }
68
69 for (String feature : getFeatures()) {
70 s.append(feature + "<");
71 }
72 byte[] sha1 = md.digest(s.toString().getBytes());
73 return new String(Base64.encode(sha1, Base64.DEFAULT)).trim();
74 }
75
76 public static String getTimestamp(long time) {
77 DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
78 return DATE_FORMAT.format(time);
79 }
80
81 public List<String> getFeatures() {
82 ArrayList<String> features = new ArrayList<>();
83 features.addAll(Arrays.asList(FEATURES));
84 if (mXmppConnectionService.confirmMessages()) {
85 features.addAll(Arrays.asList(MESSAGE_CONFIRMATION_FEATURES));
86 }
87 Collections.sort(features);
88 return features;
89 }
90}