AbstractGenerator.java

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