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