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