1package eu.siacs.conversations.generator;
2
3import android.os.Bundle;
4import android.util.Base64;
5import android.util.Base64OutputStream;
6import android.util.Log;
7
8import com.cheogram.android.BobTransfer;
9
10import com.google.common.io.ByteStreams;
11
12import org.whispersystems.libsignal.IdentityKey;
13import org.whispersystems.libsignal.ecc.ECPublicKey;
14import org.whispersystems.libsignal.state.PreKeyRecord;
15import org.whispersystems.libsignal.state.SignedPreKeyRecord;
16
17import java.io.ByteArrayOutputStream;
18import java.io.FileInputStream;
19import java.io.IOException;
20import java.nio.ByteBuffer;
21import java.security.cert.CertificateEncodingException;
22import java.security.cert.X509Certificate;
23import java.util.ArrayList;
24import java.util.List;
25import java.util.Locale;
26import java.util.Set;
27import java.util.TimeZone;
28import java.util.UUID;
29
30import io.ipfs.cid.Cid;
31
32import eu.siacs.conversations.Config;
33import eu.siacs.conversations.R;
34import eu.siacs.conversations.crypto.axolotl.AxolotlService;
35import eu.siacs.conversations.entities.Account;
36import eu.siacs.conversations.entities.Bookmark;
37import eu.siacs.conversations.entities.Conversation;
38import eu.siacs.conversations.entities.DownloadableFile;
39import eu.siacs.conversations.services.MessageArchiveService;
40import eu.siacs.conversations.services.XmppConnectionService;
41import eu.siacs.conversations.xml.Element;
42import eu.siacs.conversations.xml.Namespace;
43import eu.siacs.conversations.xmpp.Jid;
44import eu.siacs.conversations.xmpp.forms.Data;
45import eu.siacs.conversations.xmpp.pep.Avatar;
46import eu.siacs.conversations.xmpp.stanzas.IqPacket;
47
48public class IqGenerator extends AbstractGenerator {
49
50 public IqGenerator(final XmppConnectionService service) {
51 super(service);
52 }
53
54 public IqPacket discoResponse(final Account account, final IqPacket request) {
55 final IqPacket packet = new IqPacket(IqPacket.TYPE.RESULT);
56 packet.setId(request.getId());
57 packet.setTo(request.getFrom());
58 final Element query = packet.addChild("query", "http://jabber.org/protocol/disco#info");
59 query.setAttribute("node", request.query().getAttribute("node"));
60 final Element identity = query.addChild("identity");
61 identity.setAttribute("category", "client");
62 identity.setAttribute("type", getIdentityType());
63 identity.setAttribute("name", getIdentityName());
64 for (final String feature : getFeatures(account)) {
65 query.addChild("feature").setAttribute("var", feature);
66 }
67 return packet;
68 }
69
70 public IqPacket versionResponse(final IqPacket request) {
71 final IqPacket packet = request.generateResponse(IqPacket.TYPE.RESULT);
72 Element query = packet.query("jabber:iq:version");
73 query.addChild("name").setContent(mXmppConnectionService.getString(R.string.app_name));
74 query.addChild("version").setContent(getIdentityVersion());
75 if ("chromium".equals(android.os.Build.BRAND)) {
76 query.addChild("os").setContent("Chrome OS");
77 } else {
78 query.addChild("os").setContent("Android");
79 }
80 return packet;
81 }
82
83 public IqPacket entityTimeResponse(IqPacket request) {
84 final IqPacket packet = request.generateResponse(IqPacket.TYPE.RESULT);
85 Element time = packet.addChild("time", "urn:xmpp:time");
86 final long now = System.currentTimeMillis();
87 time.addChild("utc").setContent(getTimestamp(now));
88 TimeZone ourTimezone = TimeZone.getDefault();
89 long offsetSeconds = ourTimezone.getOffset(now) / 1000;
90 long offsetMinutes = Math.abs((offsetSeconds % 3600) / 60);
91 long offsetHours = offsetSeconds / 3600;
92 String hours;
93 if (offsetHours < 0) {
94 hours = String.format(Locale.US, "%03d", offsetHours);
95 } else {
96 hours = String.format(Locale.US, "%02d", offsetHours);
97 }
98 String minutes = String.format(Locale.US, "%02d", offsetMinutes);
99 time.addChild("tzo").setContent(hours + ":" + minutes);
100 return packet;
101 }
102
103 public IqPacket purgeOfflineMessages() {
104 final IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
105 packet.addChild("offline", Namespace.FLEXIBLE_OFFLINE_MESSAGE_RETRIEVAL).addChild("purge");
106 return packet;
107 }
108
109 protected IqPacket publish(final String node, final Element item, final Bundle options) {
110 final IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
111 final Element pubsub = packet.addChild("pubsub", Namespace.PUBSUB);
112 final Element publish = pubsub.addChild("publish");
113 publish.setAttribute("node", node);
114 publish.addChild(item);
115 if (options != null) {
116 final Element publishOptions = pubsub.addChild("publish-options");
117 publishOptions.addChild(Data.create(Namespace.PUBSUB_PUBLISH_OPTIONS, options));
118 }
119 return packet;
120 }
121
122 protected IqPacket publish(final String node, final Element item) {
123 return publish(node, item, null);
124 }
125
126 private IqPacket retrieve(String node, Element item) {
127 final IqPacket packet = new IqPacket(IqPacket.TYPE.GET);
128 final Element pubsub = packet.addChild("pubsub", Namespace.PUBSUB);
129 final Element items = pubsub.addChild("items");
130 items.setAttribute("node", node);
131 if (item != null) {
132 items.addChild(item);
133 }
134 return packet;
135 }
136
137 public IqPacket retrieveBookmarks() {
138 return retrieve(Namespace.BOOKMARKS2, null);
139 }
140
141 public IqPacket publishNick(String nick) {
142 final Element item = new Element("item");
143 item.setAttribute("id", "current");
144 item.addChild("nick", Namespace.NICK).setContent(nick);
145 return publish(Namespace.NICK, item);
146 }
147
148 public IqPacket deleteNode(final String node) {
149 final IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
150 final Element pubsub = packet.addChild("pubsub", Namespace.PUBSUB_OWNER);
151 pubsub.addChild("delete").setAttribute("node", node);
152 return packet;
153 }
154
155 public IqPacket deleteItem(final String node, final String id) {
156 IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
157 final Element pubsub = packet.addChild("pubsub", Namespace.PUBSUB);
158 final Element retract = pubsub.addChild("retract");
159 retract.setAttribute("node", node);
160 retract.setAttribute("notify","true");
161 retract.addChild("item").setAttribute("id", id);
162 return packet;
163 }
164
165 public IqPacket publishAvatar(Avatar avatar, Bundle options) {
166 final Element item = new Element("item");
167 item.setAttribute("id", avatar.sha1sum);
168 final Element data = item.addChild("data", Namespace.AVATAR_DATA);
169 data.setContent(avatar.image);
170 return publish(Namespace.AVATAR_DATA, item, options);
171 }
172
173 public IqPacket publishElement(final String namespace, final Element element, String id, final Bundle options) {
174 final Element item = new Element("item");
175 item.setAttribute("id", id);
176 item.addChild(element);
177 return publish(namespace, item, options);
178 }
179
180 public IqPacket publishAvatarMetadata(final Avatar avatar, final Bundle options) {
181 final Element item = new Element("item");
182 item.setAttribute("id", avatar.sha1sum);
183 final Element metadata = item
184 .addChild("metadata", Namespace.AVATAR_METADATA);
185 final Element info = metadata.addChild("info");
186 info.setAttribute("bytes", avatar.size);
187 info.setAttribute("id", avatar.sha1sum);
188 info.setAttribute("height", avatar.height);
189 info.setAttribute("width", avatar.height);
190 info.setAttribute("type", avatar.type);
191 return publish(Namespace.AVATAR_METADATA, item, options);
192 }
193
194 public IqPacket retrievePepAvatar(final Avatar avatar) {
195 final Element item = new Element("item");
196 item.setAttribute("id", avatar.sha1sum);
197 final IqPacket packet = retrieve(Namespace.AVATAR_DATA, item);
198 packet.setTo(avatar.owner);
199 return packet;
200 }
201
202 public IqPacket retrieveVcardAvatar(final Avatar avatar) {
203 final IqPacket packet = new IqPacket(IqPacket.TYPE.GET);
204 packet.setTo(avatar.owner);
205 packet.addChild("vCard", "vcard-temp");
206 return packet;
207 }
208
209 public IqPacket retrieveVcardAvatar(final Jid to) {
210 final IqPacket packet = new IqPacket(IqPacket.TYPE.GET);
211 packet.setTo(to);
212 packet.addChild("vCard", "vcard-temp");
213 return packet;
214 }
215
216 public IqPacket retrieveAvatarMetaData(final Jid to) {
217 final IqPacket packet = retrieve("urn:xmpp:avatar:metadata", null);
218 if (to != null) {
219 packet.setTo(to);
220 }
221 return packet;
222 }
223
224 public IqPacket retrieveDeviceIds(final Jid to) {
225 final IqPacket packet = retrieve(AxolotlService.PEP_DEVICE_LIST, null);
226 if (to != null) {
227 packet.setTo(to);
228 }
229 return packet;
230 }
231
232 public IqPacket retrieveBundlesForDevice(final Jid to, final int deviceid) {
233 final IqPacket packet = retrieve(AxolotlService.PEP_BUNDLES + ":" + deviceid, null);
234 packet.setTo(to);
235 return packet;
236 }
237
238 public IqPacket retrieveVerificationForDevice(final Jid to, final int deviceid) {
239 final IqPacket packet = retrieve(AxolotlService.PEP_VERIFICATION + ":" + deviceid, null);
240 packet.setTo(to);
241 return packet;
242 }
243
244 public IqPacket publishDeviceIds(final Set<Integer> ids, final Bundle publishOptions) {
245 final Element item = new Element("item");
246 item.setAttribute("id", "current");
247 final Element list = item.addChild("list", AxolotlService.PEP_PREFIX);
248 for (Integer id : ids) {
249 final Element device = new Element("device");
250 device.setAttribute("id", id);
251 list.addChild(device);
252 }
253 return publish(AxolotlService.PEP_DEVICE_LIST, item, publishOptions);
254 }
255
256 public Element publishBookmarkItem(final Bookmark bookmark) {
257 final String name = bookmark.getBookmarkName();
258 final String nick = bookmark.getNick();
259 final String password = bookmark.getPassword();
260 final boolean autojoin = bookmark.autojoin();
261 final Element conference = new Element("conference", Namespace.BOOKMARKS2);
262 if (name != null) {
263 conference.setAttribute("name", name);
264 }
265 if (nick != null) {
266 conference.addChild("nick").setContent(nick);
267 }
268 if (password != null) {
269 conference.addChild("password").setContent(password);
270 }
271 conference.setAttribute("autojoin",String.valueOf(autojoin));
272 conference.addChild(bookmark.getExtensions());
273 return conference;
274 }
275
276 public IqPacket publishBundles(final SignedPreKeyRecord signedPreKeyRecord, final IdentityKey identityKey,
277 final Set<PreKeyRecord> preKeyRecords, final int deviceId, Bundle publishOptions) {
278 final Element item = new Element("item");
279 item.setAttribute("id", "current");
280 final Element bundle = item.addChild("bundle", AxolotlService.PEP_PREFIX);
281 final Element signedPreKeyPublic = bundle.addChild("signedPreKeyPublic");
282 signedPreKeyPublic.setAttribute("signedPreKeyId", signedPreKeyRecord.getId());
283 ECPublicKey publicKey = signedPreKeyRecord.getKeyPair().getPublicKey();
284 signedPreKeyPublic.setContent(Base64.encodeToString(publicKey.serialize(), Base64.NO_WRAP));
285 final Element signedPreKeySignature = bundle.addChild("signedPreKeySignature");
286 signedPreKeySignature.setContent(Base64.encodeToString(signedPreKeyRecord.getSignature(), Base64.NO_WRAP));
287 final Element identityKeyElement = bundle.addChild("identityKey");
288 identityKeyElement.setContent(Base64.encodeToString(identityKey.serialize(), Base64.NO_WRAP));
289
290 final Element prekeys = bundle.addChild("prekeys", AxolotlService.PEP_PREFIX);
291 for (PreKeyRecord preKeyRecord : preKeyRecords) {
292 final Element prekey = prekeys.addChild("preKeyPublic");
293 prekey.setAttribute("preKeyId", preKeyRecord.getId());
294 prekey.setContent(Base64.encodeToString(preKeyRecord.getKeyPair().getPublicKey().serialize(), Base64.NO_WRAP));
295 }
296
297 return publish(AxolotlService.PEP_BUNDLES + ":" + deviceId, item, publishOptions);
298 }
299
300 public IqPacket publishVerification(byte[] signature, X509Certificate[] certificates, final int deviceId) {
301 final Element item = new Element("item");
302 item.setAttribute("id", "current");
303 final Element verification = item.addChild("verification", AxolotlService.PEP_PREFIX);
304 final Element chain = verification.addChild("chain");
305 for (int i = 0; i < certificates.length; ++i) {
306 try {
307 Element certificate = chain.addChild("certificate");
308 certificate.setContent(Base64.encodeToString(certificates[i].getEncoded(), Base64.NO_WRAP));
309 certificate.setAttribute("index", i);
310 } catch (CertificateEncodingException e) {
311 Log.d(Config.LOGTAG, "could not encode certificate");
312 }
313 }
314 verification.addChild("signature").setContent(Base64.encodeToString(signature, Base64.NO_WRAP));
315 return publish(AxolotlService.PEP_VERIFICATION + ":" + deviceId, item);
316 }
317
318 public IqPacket queryMessageArchiveManagement(final MessageArchiveService.Query mam) {
319 final IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
320 final Element query = packet.query(mam.version.namespace);
321 query.setAttribute("queryid", mam.getQueryId());
322 final Data data = new Data();
323 data.setFormType(mam.version.namespace);
324 if (mam.muc()) {
325 packet.setTo(mam.getWith());
326 } else if (mam.getWith() != null) {
327 data.put("with", mam.getWith().toEscapedString());
328 }
329 final long start = mam.getStart();
330 final long end = mam.getEnd();
331 if (start != 0) {
332 data.put("start", getTimestamp(start));
333 }
334 if (end != 0) {
335 data.put("end", getTimestamp(end));
336 }
337 data.submit();
338 query.addChild(data);
339 Element set = query.addChild("set", "http://jabber.org/protocol/rsm");
340 if (mam.getPagingOrder() == MessageArchiveService.PagingOrder.REVERSE) {
341 set.addChild("before").setContent(mam.getReference());
342 } else if (mam.getReference() != null) {
343 set.addChild("after").setContent(mam.getReference());
344 }
345 set.addChild("max").setContent(String.valueOf(Config.PAGE_SIZE));
346 return packet;
347 }
348
349 public IqPacket generateGetBlockList() {
350 final IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
351 iq.addChild("blocklist", Namespace.BLOCKING);
352
353 return iq;
354 }
355
356 public IqPacket generateSetBlockRequest(final Jid jid, boolean reportSpam) {
357 final IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
358 final Element block = iq.addChild("block", Namespace.BLOCKING);
359 final Element item = block.addChild("item").setAttribute("jid", jid);
360 if (reportSpam) {
361 item.addChild("report", "urn:xmpp:reporting:0").addChild("spam");
362 }
363 Log.d(Config.LOGTAG, iq.toString());
364 return iq;
365 }
366
367 public IqPacket generateSetUnblockRequest(final Jid jid) {
368 final IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
369 final Element block = iq.addChild("unblock", Namespace.BLOCKING);
370 block.addChild("item").setAttribute("jid", jid);
371 return iq;
372 }
373
374 public IqPacket generateSetPassword(final Account account, final String newPassword) {
375 final IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
376 packet.setTo(account.getDomain());
377 final Element query = packet.addChild("query", Namespace.REGISTER);
378 final Jid jid = account.getJid();
379 query.addChild("username").setContent(jid.getLocal());
380 query.addChild("password").setContent(newPassword);
381 return packet;
382 }
383
384 public IqPacket changeAffiliation(Conversation conference, Jid jid, String affiliation) {
385 List<Jid> jids = new ArrayList<>();
386 jids.add(jid);
387 return changeAffiliation(conference, jids, affiliation);
388 }
389
390 public IqPacket changeAffiliation(Conversation conference, List<Jid> jids, String affiliation) {
391 IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
392 packet.setTo(conference.getJid().asBareJid());
393 packet.setFrom(conference.getAccount().getJid());
394 Element query = packet.query("http://jabber.org/protocol/muc#admin");
395 for (Jid jid : jids) {
396 Element item = query.addChild("item");
397 item.setAttribute("jid", jid);
398 item.setAttribute("affiliation", affiliation);
399 }
400 return packet;
401 }
402
403 public IqPacket changeRole(Conversation conference, String nick, String role) {
404 IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
405 packet.setTo(conference.getJid().asBareJid());
406 packet.setFrom(conference.getAccount().getJid());
407 Element item = packet.query("http://jabber.org/protocol/muc#admin").addChild("item");
408 item.setAttribute("nick", nick);
409 item.setAttribute("role", role);
410 return packet;
411 }
412
413 public IqPacket requestHttpUploadSlot(Jid host, DownloadableFile file, String mime) {
414 IqPacket packet = new IqPacket(IqPacket.TYPE.GET);
415 packet.setTo(host);
416 Element request = packet.addChild("request", Namespace.HTTP_UPLOAD);
417 request.setAttribute("filename", convertFilename(file.getName()));
418 request.setAttribute("size", file.getExpectedSize());
419 request.setAttribute("content-type", mime);
420 return packet;
421 }
422
423 public IqPacket requestHttpUploadLegacySlot(Jid host, DownloadableFile file, String mime) {
424 IqPacket packet = new IqPacket(IqPacket.TYPE.GET);
425 packet.setTo(host);
426 Element request = packet.addChild("request", Namespace.HTTP_UPLOAD_LEGACY);
427 request.addChild("filename").setContent(convertFilename(file.getName()));
428 request.addChild("size").setContent(String.valueOf(file.getExpectedSize()));
429 request.addChild("content-type").setContent(mime);
430 return packet;
431 }
432
433 private static String convertFilename(String name) {
434 int pos = name.indexOf('.');
435 if (pos != -1) {
436 try {
437 UUID uuid = UUID.fromString(name.substring(0, pos));
438 ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
439 bb.putLong(uuid.getMostSignificantBits());
440 bb.putLong(uuid.getLeastSignificantBits());
441 return Base64.encodeToString(bb.array(), Base64.URL_SAFE | Base64.NO_PADDING | Base64.NO_WRAP) + name.substring(pos);
442 } catch (Exception e) {
443 return name;
444 }
445 } else {
446 return name;
447 }
448 }
449
450 public IqPacket generateCreateAccountWithCaptcha(Account account, String id, Data data) {
451 final IqPacket register = new IqPacket(IqPacket.TYPE.SET);
452 register.setFrom(account.getJid().asBareJid());
453 register.setTo(account.getDomain());
454 register.setId(id);
455 Element query = register.query(Namespace.REGISTER);
456 if (data != null) {
457 query.addChild(data);
458 }
459 return register;
460 }
461
462 public IqPacket pushTokenToAppServer(Jid appServer, String token, String deviceId) {
463 return pushTokenToAppServer(appServer, token, deviceId, null);
464 }
465
466 public IqPacket pushTokenToAppServer(Jid appServer, String token, String deviceId, Jid muc) {
467 final IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
468 packet.setTo(appServer);
469 final Element command = packet.addChild("command", Namespace.COMMANDS);
470 command.setAttribute("node", "register-push-fcm");
471 command.setAttribute("action", "execute");
472 final Data data = new Data();
473 data.put("token", token);
474 data.put("android-id", deviceId);
475 if (muc != null) {
476 data.put("muc", muc.toEscapedString());
477 }
478 data.submit();
479 command.addChild(data);
480 return packet;
481 }
482
483 public IqPacket unregisterChannelOnAppServer(Jid appServer, String deviceId, String channel) {
484 final IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
485 packet.setTo(appServer);
486 final Element command = packet.addChild("command", Namespace.COMMANDS);
487 command.setAttribute("node", "unregister-push-fcm");
488 command.setAttribute("action", "execute");
489 final Data data = new Data();
490 data.put("channel", channel);
491 data.put("android-id", deviceId);
492 data.submit();
493 command.addChild(data);
494 return packet;
495 }
496
497 public IqPacket enablePush(final Jid jid, final String node, final String secret) {
498 IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
499 Element enable = packet.addChild("enable", Namespace.PUSH);
500 enable.setAttribute("jid", jid);
501 enable.setAttribute("node", node);
502 if (secret != null) {
503 Data data = new Data();
504 data.setFormType(Namespace.PUBSUB_PUBLISH_OPTIONS);
505 data.put("secret", secret);
506 data.submit();
507 enable.addChild(data);
508 }
509 return packet;
510 }
511
512 public IqPacket disablePush(final Jid jid, final String node) {
513 IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
514 Element disable = packet.addChild("disable", Namespace.PUSH);
515 disable.setAttribute("jid", jid);
516 disable.setAttribute("node", node);
517 return packet;
518 }
519
520 public IqPacket queryAffiliation(Conversation conversation, String affiliation) {
521 IqPacket packet = new IqPacket(IqPacket.TYPE.GET);
522 packet.setTo(conversation.getJid().asBareJid());
523 packet.query("http://jabber.org/protocol/muc#admin").addChild("item").setAttribute("affiliation", affiliation);
524 return packet;
525 }
526
527 public static Bundle defaultGroupChatConfiguration() {
528 Bundle options = new Bundle();
529 options.putString("muc#roomconfig_persistentroom", "1");
530 options.putString("muc#roomconfig_membersonly", "1");
531 options.putString("muc#roomconfig_publicroom", "0");
532 options.putString("muc#roomconfig_whois", "anyone");
533 options.putString("muc#roomconfig_changesubject", "0");
534 options.putString("muc#roomconfig_allowinvites", "0");
535 options.putString("muc#roomconfig_enablearchiving", "1"); //prosody
536 options.putString("mam", "1"); //ejabberd community
537 options.putString("muc#roomconfig_mam", "1"); //ejabberd saas
538 return options;
539 }
540
541 public static Bundle defaultChannelConfiguration() {
542 Bundle options = new Bundle();
543 options.putString("muc#roomconfig_persistentroom", "1");
544 options.putString("muc#roomconfig_membersonly", "0");
545 options.putString("muc#roomconfig_publicroom", "1");
546 options.putString("muc#roomconfig_whois", "moderators");
547 options.putString("muc#roomconfig_changesubject", "0");
548 options.putString("muc#roomconfig_enablearchiving", "1"); //prosody
549 options.putString("mam", "1"); //ejabberd community
550 options.putString("muc#roomconfig_mam", "1"); //ejabberd saas
551 return options;
552 }
553
554 public IqPacket requestPubsubConfiguration(Jid jid, String node) {
555 return pubsubConfiguration(jid, node, null);
556 }
557
558 public IqPacket publishPubsubConfiguration(Jid jid, String node, Data data) {
559 return pubsubConfiguration(jid, node, data);
560 }
561
562 private IqPacket pubsubConfiguration(Jid jid, String node, Data data) {
563 IqPacket packet = new IqPacket(data == null ? IqPacket.TYPE.GET : IqPacket.TYPE.SET);
564 packet.setTo(jid);
565 Element pubsub = packet.addChild("pubsub", "http://jabber.org/protocol/pubsub#owner");
566 Element configure = pubsub.addChild("configure").setAttribute("node", node);
567 if (data != null) {
568 configure.addChild(data);
569 }
570 return packet;
571 }
572
573 public IqPacket queryDiscoItems(Jid jid) {
574 IqPacket packet = new IqPacket(IqPacket.TYPE.GET);
575 packet.setTo(jid);
576 packet.query(Namespace.DISCO_ITEMS);
577 return packet;
578 }
579
580 public IqPacket queryDiscoItems(Jid jid, String node) {
581 IqPacket packet = queryDiscoItems(jid);
582 final Element query = packet.query(Namespace.DISCO_ITEMS);
583 query.setAttribute("node", node);
584 return packet;
585 }
586
587 public IqPacket queryDiscoInfo(Jid jid) {
588 IqPacket packet = new IqPacket(IqPacket.TYPE.GET);
589 packet.setTo(jid);
590 packet.addChild("query",Namespace.DISCO_INFO);
591 return packet;
592 }
593
594 public IqPacket bobResponse(IqPacket request) {
595 try {
596 String bobCid = request.findChild("data", "urn:xmpp:bob").getAttribute("cid");
597 Cid cid = BobTransfer.cid(bobCid);
598 DownloadableFile f = mXmppConnectionService.getFileForCid(cid);
599 if (f == null || !f.canRead()) {
600 throw new IOException("No such file");
601 } else if (f.getSize() > 129000) {
602 final IqPacket response = request.generateResponse(IqPacket.TYPE.ERROR);
603 final Element error = response.addChild("error");
604 error.setAttribute("type", "cancel");
605 error.addChild("policy-violation", "urn:ietf:params:xml:ns:xmpp-stanzas");
606 return response;
607 } else {
608 final IqPacket response = request.generateResponse(IqPacket.TYPE.RESULT);
609 final Element data = response.addChild("data", "urn:xmpp:bob");
610 data.setAttribute("cid", bobCid);
611 data.setAttribute("type", f.getMimeType());
612 ByteArrayOutputStream b64 = new ByteArrayOutputStream((int) f.getSize() * 2);
613 ByteStreams.copy(new FileInputStream(f), new Base64OutputStream(b64, Base64.NO_WRAP));
614 data.setContent(b64.toString("utf-8"));
615 return response;
616 }
617 } catch (final IOException | IllegalStateException e) {
618 final IqPacket response = request.generateResponse(IqPacket.TYPE.ERROR);
619 final Element error = response.addChild("error");
620 error.setAttribute("type", "cancel");
621 error.addChild("item-not-found", "urn:ietf:params:xml:ns:xmpp-stanzas");
622 return response;
623 }
624 }
625}