IqGenerator.java

  1package eu.siacs.conversations.generator;
  2
  3
  4import android.os.Bundle;
  5import android.util.Base64;
  6import android.util.Log;
  7
  8import org.whispersystems.libsignal.IdentityKey;
  9import org.whispersystems.libsignal.ecc.ECPublicKey;
 10import org.whispersystems.libsignal.state.PreKeyRecord;
 11import org.whispersystems.libsignal.state.SignedPreKeyRecord;
 12
 13import java.nio.ByteBuffer;
 14import java.security.cert.CertificateEncodingException;
 15import java.security.cert.X509Certificate;
 16import java.util.ArrayList;
 17import java.util.List;
 18import java.util.Locale;
 19import java.util.Set;
 20import java.util.TimeZone;
 21import java.util.UUID;
 22
 23import eu.siacs.conversations.Config;
 24import eu.siacs.conversations.R;
 25import eu.siacs.conversations.crypto.axolotl.AxolotlService;
 26import eu.siacs.conversations.entities.Account;
 27import eu.siacs.conversations.entities.Conversation;
 28import eu.siacs.conversations.entities.DownloadableFile;
 29import eu.siacs.conversations.services.MessageArchiveService;
 30import eu.siacs.conversations.services.XmppConnectionService;
 31import eu.siacs.conversations.xml.Namespace;
 32import eu.siacs.conversations.xml.Element;
 33import eu.siacs.conversations.xmpp.forms.Data;
 34import eu.siacs.conversations.xmpp.pep.Avatar;
 35import eu.siacs.conversations.xmpp.stanzas.IqPacket;
 36import rocks.xmpp.addr.Jid;
 37
 38public class IqGenerator extends AbstractGenerator {
 39
 40	public IqGenerator(final XmppConnectionService service) {
 41		super(service);
 42	}
 43
 44	public IqPacket discoResponse(final Account account, final IqPacket request) {
 45		final IqPacket packet = new IqPacket(IqPacket.TYPE.RESULT);
 46		packet.setId(request.getId());
 47		packet.setTo(request.getFrom());
 48		final Element query = packet.addChild("query", "http://jabber.org/protocol/disco#info");
 49		query.setAttribute("node", request.query().getAttribute("node"));
 50		final Element identity = query.addChild("identity");
 51		identity.setAttribute("category", "client");
 52		identity.setAttribute("type", getIdentityType());
 53		identity.setAttribute("name", getIdentityName());
 54		for (final String feature : getFeatures(account)) {
 55			query.addChild("feature").setAttribute("var", feature);
 56		}
 57		return packet;
 58	}
 59
 60	public IqPacket versionResponse(final IqPacket request) {
 61		final IqPacket packet = request.generateResponse(IqPacket.TYPE.RESULT);
 62		Element query = packet.query("jabber:iq:version");
 63		query.addChild("name").setContent(mXmppConnectionService.getString(R.string.app_name));
 64		query.addChild("version").setContent(getIdentityVersion());
 65		if ("chromium".equals(android.os.Build.BRAND)) {
 66			query.addChild("os").setContent("Chrome OS");
 67		} else {
 68			query.addChild("os").setContent("Android");
 69		}
 70		return packet;
 71	}
 72
 73	public IqPacket entityTimeResponse(IqPacket request) {
 74		final IqPacket packet = request.generateResponse(IqPacket.TYPE.RESULT);
 75		Element time = packet.addChild("time", "urn:xmpp:time");
 76		final long now = System.currentTimeMillis();
 77		time.addChild("utc").setContent(getTimestamp(now));
 78		TimeZone ourTimezone = TimeZone.getDefault();
 79		long offsetSeconds = ourTimezone.getOffset(now) / 1000;
 80		long offsetMinutes = Math.abs((offsetSeconds % 3600) / 60);
 81		long offsetHours = offsetSeconds / 3600;
 82		String hours;
 83		if (offsetHours < 0) {
 84			hours = String.format(Locale.US, "%03d", offsetHours);
 85		} else {
 86			hours = String.format(Locale.US, "%02d", offsetHours);
 87		}
 88		String minutes = String.format(Locale.US, "%02d", offsetMinutes);
 89		time.addChild("tzo").setContent(hours + ":" + minutes);
 90		return packet;
 91	}
 92
 93	public IqPacket purgeOfflineMessages() {
 94		final IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
 95		packet.addChild("offline", Namespace.FLEXIBLE_OFFLINE_MESSAGE_RETRIEVAL).addChild("purge");
 96		return packet;
 97	}
 98
 99	protected IqPacket publish(final String node, final Element item, final Bundle options) {
100		final IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
101		final Element pubsub = packet.addChild("pubsub", Namespace.PUBSUB);
102		final Element publish = pubsub.addChild("publish");
103		publish.setAttribute("node", node);
104		publish.addChild(item);
105		if (options != null) {
106			final Element publishOptions = pubsub.addChild("publish-options");
107			publishOptions.addChild(Data.create(Namespace.PUBSUB_PUBLISH_OPTIONS, options));
108		}
109		return packet;
110	}
111
112	protected IqPacket publish(final String node, final Element item) {
113		return publish(node, item, null);
114	}
115
116	private IqPacket retrieve(String node, Element item) {
117		final IqPacket packet = new IqPacket(IqPacket.TYPE.GET);
118		final Element pubsub = packet.addChild("pubsub", Namespace.PUBSUB);
119		final Element items = pubsub.addChild("items");
120		items.setAttribute("node", node);
121		if (item != null) {
122			items.addChild(item);
123		}
124		return packet;
125	}
126
127	public IqPacket publishNick(String nick) {
128		final Element item = new Element("item");
129		item.addChild("nick", "http://jabber.org/protocol/nick").setContent(nick);
130		return publish("http://jabber.org/protocol/nick", item);
131	}
132
133	public IqPacket publishAvatar(Avatar avatar) {
134		final Element item = new Element("item");
135		item.setAttribute("id", avatar.sha1sum);
136		final Element data = item.addChild("data", "urn:xmpp:avatar:data");
137		data.setContent(avatar.image);
138		return publish("urn:xmpp:avatar:data", item);
139	}
140
141	public IqPacket publishAvatarMetadata(final Avatar avatar) {
142		final Element item = new Element("item");
143		item.setAttribute("id", avatar.sha1sum);
144		final Element metadata = item
145				.addChild("metadata", "urn:xmpp:avatar:metadata");
146		final Element info = metadata.addChild("info");
147		info.setAttribute("bytes", avatar.size);
148		info.setAttribute("id", avatar.sha1sum);
149		info.setAttribute("height", avatar.height);
150		info.setAttribute("width", avatar.height);
151		info.setAttribute("type", avatar.type);
152		return publish("urn:xmpp:avatar:metadata", item);
153	}
154
155	public IqPacket retrievePepAvatar(final Avatar avatar) {
156		final Element item = new Element("item");
157		item.setAttribute("id", avatar.sha1sum);
158		final IqPacket packet = retrieve("urn:xmpp:avatar:data", item);
159		packet.setTo(avatar.owner);
160		return packet;
161	}
162
163	public IqPacket retrieveVcardAvatar(final Avatar avatar) {
164		final IqPacket packet = new IqPacket(IqPacket.TYPE.GET);
165		packet.setTo(avatar.owner);
166		packet.addChild("vCard", "vcard-temp");
167		return packet;
168	}
169
170	public IqPacket retrieveAvatarMetaData(final Jid to) {
171		final IqPacket packet = retrieve("urn:xmpp:avatar:metadata", null);
172		if (to != null) {
173			packet.setTo(to);
174		}
175		return packet;
176	}
177
178	public IqPacket retrieveDeviceIds(final Jid to) {
179		final IqPacket packet = retrieve(AxolotlService.PEP_DEVICE_LIST, null);
180		if (to != null) {
181			packet.setTo(to);
182		}
183		return packet;
184	}
185
186	public IqPacket retrieveBundlesForDevice(final Jid to, final int deviceid) {
187		final IqPacket packet = retrieve(AxolotlService.PEP_BUNDLES + ":" + deviceid, null);
188		packet.setTo(to);
189		return packet;
190	}
191
192	public IqPacket retrieveVerificationForDevice(final Jid to, final int deviceid) {
193		final IqPacket packet = retrieve(AxolotlService.PEP_VERIFICATION + ":" + deviceid, null);
194		packet.setTo(to);
195		return packet;
196	}
197
198	public IqPacket publishDeviceIds(final Set<Integer> ids, final Bundle publishOptions) {
199		final Element item = new Element("item");
200		final Element list = item.addChild("list", AxolotlService.PEP_PREFIX);
201		for (Integer id : ids) {
202			final Element device = new Element("device");
203			device.setAttribute("id", id);
204			list.addChild(device);
205		}
206		return publish(AxolotlService.PEP_DEVICE_LIST, item, publishOptions);
207	}
208
209	public IqPacket publishBundles(final SignedPreKeyRecord signedPreKeyRecord, final IdentityKey identityKey,
210	                               final Set<PreKeyRecord> preKeyRecords, final int deviceId, Bundle publishOptions) {
211		final Element item = new Element("item");
212		final Element bundle = item.addChild("bundle", AxolotlService.PEP_PREFIX);
213		final Element signedPreKeyPublic = bundle.addChild("signedPreKeyPublic");
214		signedPreKeyPublic.setAttribute("signedPreKeyId", signedPreKeyRecord.getId());
215		ECPublicKey publicKey = signedPreKeyRecord.getKeyPair().getPublicKey();
216		signedPreKeyPublic.setContent(Base64.encodeToString(publicKey.serialize(), Base64.DEFAULT));
217		final Element signedPreKeySignature = bundle.addChild("signedPreKeySignature");
218		signedPreKeySignature.setContent(Base64.encodeToString(signedPreKeyRecord.getSignature(), Base64.DEFAULT));
219		final Element identityKeyElement = bundle.addChild("identityKey");
220		identityKeyElement.setContent(Base64.encodeToString(identityKey.serialize(), Base64.DEFAULT));
221
222		final Element prekeys = bundle.addChild("prekeys", AxolotlService.PEP_PREFIX);
223		for (PreKeyRecord preKeyRecord : preKeyRecords) {
224			final Element prekey = prekeys.addChild("preKeyPublic");
225			prekey.setAttribute("preKeyId", preKeyRecord.getId());
226			prekey.setContent(Base64.encodeToString(preKeyRecord.getKeyPair().getPublicKey().serialize(), Base64.DEFAULT));
227		}
228
229		return publish(AxolotlService.PEP_BUNDLES + ":" + deviceId, item, publishOptions);
230	}
231
232	public IqPacket publishVerification(byte[] signature, X509Certificate[] certificates, final int deviceId) {
233		final Element item = new Element("item");
234		final Element verification = item.addChild("verification", AxolotlService.PEP_PREFIX);
235		final Element chain = verification.addChild("chain");
236		for (int i = 0; i < certificates.length; ++i) {
237			try {
238				Element certificate = chain.addChild("certificate");
239				certificate.setContent(Base64.encodeToString(certificates[i].getEncoded(), Base64.DEFAULT));
240				certificate.setAttribute("index", i);
241			} catch (CertificateEncodingException e) {
242				Log.d(Config.LOGTAG, "could not encode certificate");
243			}
244		}
245		verification.addChild("signature").setContent(Base64.encodeToString(signature, Base64.DEFAULT));
246		return publish(AxolotlService.PEP_VERIFICATION + ":" + deviceId, item);
247	}
248
249	public IqPacket queryMessageArchiveManagement(final MessageArchiveService.Query mam) {
250		final IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
251		final Element query = packet.query(mam.version.namespace);
252		query.setAttribute("queryid", mam.getQueryId());
253		final Data data = new Data();
254		data.setFormType(mam.version.namespace);
255		if (mam.muc()) {
256			packet.setTo(mam.getWith());
257		} else if (mam.getWith() != null) {
258			data.put("with", mam.getWith().toString());
259		}
260		final long start = mam.getStart();
261		final long end = mam.getEnd();
262		if (start != 0) {
263			data.put("start", getTimestamp(start));
264		}
265		if (end != 0) {
266			data.put("end", getTimestamp(end));
267		}
268		data.submit();
269		query.addChild(data);
270		Element set = query.addChild("set", "http://jabber.org/protocol/rsm");
271		if (mam.getPagingOrder() == MessageArchiveService.PagingOrder.REVERSE) {
272			set.addChild("before").setContent(mam.getReference());
273		} else if (mam.getReference() != null) {
274			set.addChild("after").setContent(mam.getReference());
275		}
276		set.addChild("max").setContent(String.valueOf(Config.PAGE_SIZE));
277		return packet;
278	}
279
280	public IqPacket generateGetBlockList() {
281		final IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
282		iq.addChild("blocklist", Namespace.BLOCKING);
283
284		return iq;
285	}
286
287	public IqPacket generateSetBlockRequest(final Jid jid, boolean reportSpam) {
288		final IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
289		final Element block = iq.addChild("block", Namespace.BLOCKING);
290		final Element item = block.addChild("item").setAttribute("jid", jid.asBareJid().toString());
291		if (reportSpam) {
292			item.addChild("report", "urn:xmpp:reporting:0").addChild("spam");
293		}
294		Log.d(Config.LOGTAG, iq.toString());
295		return iq;
296	}
297
298	public IqPacket generateSetUnblockRequest(final Jid jid) {
299		final IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
300		final Element block = iq.addChild("unblock", Namespace.BLOCKING);
301		block.addChild("item").setAttribute("jid", jid.asBareJid().toString());
302		return iq;
303	}
304
305	public IqPacket generateSetPassword(final Account account, final String newPassword) {
306		final IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
307		packet.setTo(Jid.of(account.getServer()));
308		final Element query = packet.addChild("query", Namespace.REGISTER);
309		final Jid jid = account.getJid();
310		query.addChild("username").setContent(jid.getLocal());
311		query.addChild("password").setContent(newPassword);
312		return packet;
313	}
314
315	public IqPacket changeAffiliation(Conversation conference, Jid jid, String affiliation) {
316		List<Jid> jids = new ArrayList<>();
317		jids.add(jid);
318		return changeAffiliation(conference, jids, affiliation);
319	}
320
321	public IqPacket changeAffiliation(Conversation conference, List<Jid> jids, String affiliation) {
322		IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
323		packet.setTo(conference.getJid().asBareJid());
324		packet.setFrom(conference.getAccount().getJid());
325		Element query = packet.query("http://jabber.org/protocol/muc#admin");
326		for (Jid jid : jids) {
327			Element item = query.addChild("item");
328			item.setAttribute("jid", jid.toString());
329			item.setAttribute("affiliation", affiliation);
330		}
331		return packet;
332	}
333
334	public IqPacket changeRole(Conversation conference, String nick, String role) {
335		IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
336		packet.setTo(conference.getJid().asBareJid());
337		packet.setFrom(conference.getAccount().getJid());
338		Element item = packet.query("http://jabber.org/protocol/muc#admin").addChild("item");
339		item.setAttribute("nick", nick);
340		item.setAttribute("role", role);
341		return packet;
342	}
343
344	public IqPacket requestHttpUploadSlot(Jid host, DownloadableFile file, String mime) {
345		IqPacket packet = new IqPacket(IqPacket.TYPE.GET);
346		packet.setTo(host);
347		Element request = packet.addChild("request", Namespace.HTTP_UPLOAD);
348		request.setAttribute("filename", convertFilename(file.getName()));
349		request.setAttribute("size", file.getExpectedSize());
350		request.setAttribute("content-type", mime);
351		return packet;
352	}
353
354	public IqPacket requestHttpUploadLegacySlot(Jid host, DownloadableFile file, String mime) {
355		IqPacket packet = new IqPacket(IqPacket.TYPE.GET);
356		packet.setTo(host);
357		Element request = packet.addChild("request", Namespace.HTTP_UPLOAD_LEGACY);
358		request.addChild("filename").setContent(convertFilename(file.getName()));
359		request.addChild("size").setContent(String.valueOf(file.getExpectedSize()));
360		request.addChild("content-type").setContent(mime);
361		return packet;
362	}
363
364	public IqPacket requestP1S3Slot(Jid host, String md5) {
365		IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
366		packet.setTo(host);
367		packet.query(Namespace.P1_S3_FILE_TRANSFER).setAttribute("md5", md5);
368		return packet;
369	}
370
371	public IqPacket requestP1S3Url(Jid host, String fileId) {
372		IqPacket packet = new IqPacket(IqPacket.TYPE.GET);
373		packet.setTo(host);
374		packet.query(Namespace.P1_S3_FILE_TRANSFER).setAttribute("fileid", fileId);
375		return packet;
376	}
377
378	private static String convertFilename(String name) {
379		int pos = name.indexOf('.');
380		if (pos != -1) {
381			try {
382				UUID uuid = UUID.fromString(name.substring(0, pos));
383				ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
384				bb.putLong(uuid.getMostSignificantBits());
385				bb.putLong(uuid.getLeastSignificantBits());
386				return Base64.encodeToString(bb.array(), Base64.URL_SAFE | Base64.NO_PADDING | Base64.NO_WRAP) + name.substring(pos, name.length());
387			} catch (Exception e) {
388				return name;
389			}
390		} else {
391			return name;
392		}
393	}
394
395	public IqPacket generateCreateAccountWithCaptcha(Account account, String id, Data data) {
396		final IqPacket register = new IqPacket(IqPacket.TYPE.SET);
397		register.setFrom(account.getJid().asBareJid());
398		register.setTo(Jid.of(account.getServer()));
399		register.setId(id);
400		Element query = register.query("jabber:iq:register");
401		if (data != null) {
402			query.addChild(data);
403		}
404		return register;
405	}
406
407	public IqPacket pushTokenToAppServer(Jid appServer, String token, String deviceId) {
408		IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
409		packet.setTo(appServer);
410		Element command = packet.addChild("command", "http://jabber.org/protocol/commands");
411		command.setAttribute("node", "register-push-fcm");
412		command.setAttribute("action", "execute");
413		Data data = new Data();
414		data.put("token", token);
415		data.put("android-id", deviceId);
416		data.submit();
417		command.addChild(data);
418		return packet;
419	}
420
421	public IqPacket enablePush(Jid jid, String node, String secret) {
422		IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
423		Element enable = packet.addChild("enable", "urn:xmpp:push:0");
424		enable.setAttribute("jid", jid.toString());
425		enable.setAttribute("node", node);
426		Data data = new Data();
427		data.setFormType(Namespace.PUBSUB_PUBLISH_OPTIONS);
428		data.put("secret", secret);
429		data.submit();
430		enable.addChild(data);
431		return packet;
432	}
433
434	public IqPacket queryAffiliation(Conversation conversation, String affiliation) {
435		IqPacket packet = new IqPacket(IqPacket.TYPE.GET);
436		packet.setTo(conversation.getJid().asBareJid());
437		packet.query("http://jabber.org/protocol/muc#admin").addChild("item").setAttribute("affiliation", affiliation);
438		return packet;
439	}
440
441	public static Bundle defaultRoomConfiguration() {
442		Bundle options = new Bundle();
443		options.putString("muc#roomconfig_persistentroom", "1");
444		options.putString("muc#roomconfig_membersonly", "1");
445		options.putString("muc#roomconfig_publicroom", "0");
446		options.putString("muc#roomconfig_whois", "anyone");
447		options.putString("muc#roomconfig_enablearchiving", "1");
448		options.putString("mam", "1");
449		return options;
450	}
451
452	public IqPacket requestPubsubConfiguration(Jid jid, String node) {
453		return pubsubConfiguration(jid, node, null);
454	}
455
456	public IqPacket publishPubsubConfiguration(Jid jid, String node, Data data) {
457		return pubsubConfiguration(jid, node, data);
458	}
459
460	private IqPacket pubsubConfiguration(Jid jid, String node, Data data) {
461		IqPacket packet = new IqPacket(data == null ? IqPacket.TYPE.GET : IqPacket.TYPE.SET);
462		packet.setTo(jid);
463		Element pubsub = packet.addChild("pubsub", "http://jabber.org/protocol/pubsub#owner");
464		Element configure = pubsub.addChild("configure").setAttribute("node", node);
465		if (data != null) {
466			configure.addChild(data);
467		}
468		return packet;
469	}
470}