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