IqGenerator.java

  1package eu.siacs.conversations.generator;
  2
  3
  4import android.util.Base64;
  5
  6import org.whispersystems.libaxolotl.IdentityKey;
  7import org.whispersystems.libaxolotl.ecc.ECPublicKey;
  8import org.whispersystems.libaxolotl.state.PreKeyRecord;
  9import org.whispersystems.libaxolotl.state.SignedPreKeyRecord;
 10
 11import java.util.ArrayList;
 12import java.util.List;
 13
 14import eu.siacs.conversations.crypto.axolotl.AxolotlService;
 15import eu.siacs.conversations.entities.Account;
 16import eu.siacs.conversations.entities.Conversation;
 17import eu.siacs.conversations.entities.DownloadableFile;
 18import eu.siacs.conversations.services.MessageArchiveService;
 19import eu.siacs.conversations.services.XmppConnectionService;
 20import eu.siacs.conversations.utils.PhoneHelper;
 21import eu.siacs.conversations.utils.Xmlns;
 22import eu.siacs.conversations.xml.Element;
 23import eu.siacs.conversations.xmpp.forms.Data;
 24import eu.siacs.conversations.xmpp.jid.Jid;
 25import eu.siacs.conversations.xmpp.pep.Avatar;
 26import eu.siacs.conversations.xmpp.stanzas.IqPacket;
 27
 28public class IqGenerator extends AbstractGenerator {
 29
 30	public IqGenerator(final XmppConnectionService service) {
 31		super(service);
 32	}
 33
 34	public IqPacket discoResponse(final IqPacket request) {
 35		final IqPacket packet = new IqPacket(IqPacket.TYPE.RESULT);
 36		packet.setId(request.getId());
 37		packet.setTo(request.getFrom());
 38		final Element query = packet.addChild("query",
 39				"http://jabber.org/protocol/disco#info");
 40		query.setAttribute("node", request.query().getAttribute("node"));
 41		final Element identity = query.addChild("identity");
 42		identity.setAttribute("category", "client");
 43		identity.setAttribute("type", IDENTITY_TYPE);
 44		identity.setAttribute("name", getIdentityName());
 45		for (final String feature : getFeatures()) {
 46			query.addChild("feature").setAttribute("var", feature);
 47		}
 48		return packet;
 49	}
 50
 51	public IqPacket versionResponse(final IqPacket request) {
 52		final IqPacket packet = request.generateResponse(IqPacket.TYPE.RESULT);
 53		Element query = packet.query("jabber:iq:version");
 54		query.addChild("name").setContent(IDENTITY_NAME);
 55		query.addChild("version").setContent(getIdentityVersion());
 56		return packet;
 57	}
 58
 59	protected IqPacket publish(final String node, final Element item) {
 60		final IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
 61		final Element pubsub = packet.addChild("pubsub",
 62				"http://jabber.org/protocol/pubsub");
 63		final Element publish = pubsub.addChild("publish");
 64		publish.setAttribute("node", node);
 65		publish.addChild(item);
 66		return packet;
 67	}
 68
 69	protected IqPacket retrieve(String node, Element item) {
 70		final IqPacket packet = new IqPacket(IqPacket.TYPE.GET);
 71		final Element pubsub = packet.addChild("pubsub",
 72				"http://jabber.org/protocol/pubsub");
 73		final Element items = pubsub.addChild("items");
 74		items.setAttribute("node", node);
 75		if (item != null) {
 76			items.addChild(item);
 77		}
 78		return packet;
 79	}
 80
 81	public IqPacket publishAvatar(Avatar avatar) {
 82		final Element item = new Element("item");
 83		item.setAttribute("id", avatar.sha1sum);
 84		final Element data = item.addChild("data", "urn:xmpp:avatar:data");
 85		data.setContent(avatar.image);
 86		return publish("urn:xmpp:avatar:data", item);
 87	}
 88
 89	public IqPacket publishAvatarMetadata(final Avatar avatar) {
 90		final Element item = new Element("item");
 91		item.setAttribute("id", avatar.sha1sum);
 92		final Element metadata = item
 93			.addChild("metadata", "urn:xmpp:avatar:metadata");
 94		final Element info = metadata.addChild("info");
 95		info.setAttribute("bytes", avatar.size);
 96		info.setAttribute("id", avatar.sha1sum);
 97		info.setAttribute("height", avatar.height);
 98		info.setAttribute("width", avatar.height);
 99		info.setAttribute("type", avatar.type);
100		return publish("urn:xmpp:avatar:metadata", item);
101	}
102
103	public IqPacket retrievePepAvatar(final Avatar avatar) {
104		final Element item = new Element("item");
105		item.setAttribute("id", avatar.sha1sum);
106		final IqPacket packet = retrieve("urn:xmpp:avatar:data", item);
107		packet.setTo(avatar.owner);
108		return packet;
109	}
110
111	public IqPacket retrieveVcardAvatar(final Avatar avatar) {
112		final IqPacket packet = new IqPacket(IqPacket.TYPE.GET);
113		packet.setTo(avatar.owner);
114		packet.addChild("vCard", "vcard-temp");
115		return packet;
116	}
117
118	public IqPacket retrieveAvatarMetaData(final Jid to) {
119		final IqPacket packet = retrieve("urn:xmpp:avatar:metadata", null);
120		if (to != null) {
121			packet.setTo(to);
122		}
123		return packet;
124	}
125
126	public IqPacket retrieveDeviceIds(final Jid to) {
127		final IqPacket packet = retrieve(AxolotlService.PEP_DEVICE_LIST, null);
128		if(to != null) {
129			packet.setTo(to);
130		}
131		return packet;
132	}
133
134	public IqPacket retrieveBundleForDevice(final Jid to, final int deviceid) {
135		final IqPacket packet = retrieve(AxolotlService.PEP_BUNDLE+":"+deviceid, null);
136		if(to != null) {
137			packet.setTo(to);
138		}
139		return packet;
140	}
141
142	public IqPacket retrievePreKeysForDevice(final Jid to, final int deviceId) {
143		final IqPacket packet = retrieve(AxolotlService.PEP_PREKEYS+":"+deviceId, null);
144		if(to != null) {
145			packet.setTo(to);
146		}
147		return packet;
148	}
149
150	public IqPacket publishDeviceIds(final List<Integer> ids) {
151		final Element item = new Element("item");
152		final Element list = item.addChild("list", AxolotlService.PEP_PREFIX);
153		for(Integer id:ids) {
154			final Element device = new Element("device");
155			device.setAttribute("id", id);
156			list.addChild(device);
157		}
158		return publish(AxolotlService.PEP_DEVICE_LIST, item);
159	}
160
161	public IqPacket publishBundle(final SignedPreKeyRecord signedPreKeyRecord, IdentityKey identityKey, final int deviceId) {
162		final Element item = new Element("item");
163		final Element bundle = item.addChild("bundle", AxolotlService.PEP_PREFIX);
164		final Element signedPreKeyPublic = bundle.addChild("signedPreKeyPublic");
165		signedPreKeyPublic.setAttribute("signedPreKeyId", signedPreKeyRecord.getId());
166		ECPublicKey publicKey = signedPreKeyRecord.getKeyPair().getPublicKey();
167		signedPreKeyPublic.setContent(Base64.encodeToString(publicKey.serialize(),Base64.DEFAULT));
168		final Element signedPreKeySignature = bundle.addChild("signedPreKeySignature");
169		signedPreKeySignature.setContent(Base64.encodeToString(signedPreKeyRecord.getSignature(),Base64.DEFAULT));
170		final Element identityKeyElement = bundle.addChild("identityKey");
171		identityKeyElement.setContent(Base64.encodeToString(identityKey.serialize(), Base64.DEFAULT));
172
173		return publish(AxolotlService.PEP_BUNDLE+":"+deviceId, item);
174	}
175
176	public IqPacket publishPreKeys(final List<PreKeyRecord> prekeyList, final int deviceId) {
177		final Element item = new Element("item");
178		final Element prekeys = item.addChild("prekeys", AxolotlService.PEP_PREFIX);
179		for(PreKeyRecord preKeyRecord:prekeyList) {
180			final Element prekey = prekeys.addChild("preKeyPublic");
181			prekey.setAttribute("preKeyId", preKeyRecord.getId());
182			prekey.setContent(Base64.encodeToString(preKeyRecord.getKeyPair().getPublicKey().serialize(), Base64.DEFAULT));
183		}
184
185		return publish(AxolotlService.PEP_PREKEYS+":"+deviceId, item);
186	}
187
188	public IqPacket queryMessageArchiveManagement(final MessageArchiveService.Query mam) {
189		final IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
190		final Element query = packet.query("urn:xmpp:mam:0");
191		query.setAttribute("queryid",mam.getQueryId());
192		final Data data = new Data();
193		data.setFormType("urn:xmpp:mam:0");
194		if (mam.muc()) {
195			packet.setTo(mam.getWith());
196		} else if (mam.getWith()!=null) {
197			data.put("with", mam.getWith().toString());
198		}
199		data.put("start",getTimestamp(mam.getStart()));
200		data.put("end",getTimestamp(mam.getEnd()));
201		query.addChild(data);
202		if (mam.getPagingOrder() == MessageArchiveService.PagingOrder.REVERSE) {
203			query.addChild("set", "http://jabber.org/protocol/rsm").addChild("before").setContent(mam.getReference());
204		} else if (mam.getReference() != null) {
205			query.addChild("set", "http://jabber.org/protocol/rsm").addChild("after").setContent(mam.getReference());
206		}
207		return packet;
208	}
209	public IqPacket generateGetBlockList() {
210		final IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
211		iq.addChild("blocklist", Xmlns.BLOCKING);
212
213		return iq;
214	}
215
216	public IqPacket generateSetBlockRequest(final Jid jid) {
217		final IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
218		final Element block = iq.addChild("block", Xmlns.BLOCKING);
219		block.addChild("item").setAttribute("jid", jid.toBareJid().toString());
220		return iq;
221	}
222
223	public IqPacket generateSetUnblockRequest(final Jid jid) {
224		final IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
225		final Element block = iq.addChild("unblock", Xmlns.BLOCKING);
226		block.addChild("item").setAttribute("jid", jid.toBareJid().toString());
227		return iq;
228	}
229
230	public IqPacket generateSetPassword(final Account account, final String newPassword) {
231		final IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
232		packet.setTo(account.getServer());
233		final Element query = packet.addChild("query", Xmlns.REGISTER);
234		final Jid jid = account.getJid();
235		query.addChild("username").setContent(jid.getLocalpart());
236		query.addChild("password").setContent(newPassword);
237		return packet;
238	}
239
240	public IqPacket changeAffiliation(Conversation conference, Jid jid, String affiliation) {
241		List<Jid> jids = new ArrayList<>();
242		jids.add(jid);
243		return changeAffiliation(conference,jids,affiliation);
244	}
245
246	public IqPacket changeAffiliation(Conversation conference, List<Jid> jids, String affiliation) {
247		IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
248		packet.setTo(conference.getJid().toBareJid());
249		packet.setFrom(conference.getAccount().getJid());
250		Element query = packet.query("http://jabber.org/protocol/muc#admin");
251		for(Jid jid : jids) {
252			Element item = query.addChild("item");
253			item.setAttribute("jid", jid.toString());
254			item.setAttribute("affiliation", affiliation);
255		}
256		return packet;
257	}
258
259	public IqPacket changeRole(Conversation conference, String nick, String role) {
260		IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
261		packet.setTo(conference.getJid().toBareJid());
262		packet.setFrom(conference.getAccount().getJid());
263		Element item = packet.query("http://jabber.org/protocol/muc#admin").addChild("item");
264		item.setAttribute("nick", nick);
265		item.setAttribute("role", role);
266		return packet;
267	}
268
269	public IqPacket requestHttpUploadSlot(Jid host, DownloadableFile file) {
270		IqPacket packet = new IqPacket(IqPacket.TYPE.GET);
271		packet.setTo(host);
272		Element request = packet.addChild("request",Xmlns.HTTP_UPLOAD);
273		request.addChild("filename").setContent(file.getName());
274		request.addChild("size").setContent(String.valueOf(file.getExpectedSize()));
275		return packet;
276	}
277}