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