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