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(String node) {
149 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", "urn:xmpp:avatar:data");
169 data.setContent(avatar.image);
170 return publish("urn:xmpp: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", "urn:xmpp: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("urn:xmpp: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("urn:xmpp: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 retrieveAvatarMetaData(final Jid to) {
210 final IqPacket packet = retrieve("urn:xmpp:avatar:metadata", null);
211 if (to != null) {
212 packet.setTo(to);
213 }
214 return packet;
215 }
216
217 public IqPacket retrieveDeviceIds(final Jid to) {
218 final IqPacket packet = retrieve(AxolotlService.PEP_DEVICE_LIST, null);
219 if (to != null) {
220 packet.setTo(to);
221 }
222 return packet;
223 }
224
225 public IqPacket retrieveBundlesForDevice(final Jid to, final int deviceid) {
226 final IqPacket packet = retrieve(AxolotlService.PEP_BUNDLES + ":" + deviceid, null);
227 packet.setTo(to);
228 return packet;
229 }
230
231 public IqPacket retrieveVerificationForDevice(final Jid to, final int deviceid) {
232 final IqPacket packet = retrieve(AxolotlService.PEP_VERIFICATION + ":" + deviceid, null);
233 packet.setTo(to);
234 return packet;
235 }
236
237 public IqPacket publishDeviceIds(final Set<Integer> ids, final Bundle publishOptions) {
238 final Element item = new Element("item");
239 item.setAttribute("id", "current");
240 final Element list = item.addChild("list", AxolotlService.PEP_PREFIX);
241 for (Integer id : ids) {
242 final Element device = new Element("device");
243 device.setAttribute("id", id);
244 list.addChild(device);
245 }
246 return publish(AxolotlService.PEP_DEVICE_LIST, item, publishOptions);
247 }
248
249 public Element publishBookmarkItem(final Bookmark bookmark) {
250 final String name = bookmark.getBookmarkName();
251 final String nick = bookmark.getNick();
252 final boolean autojoin = bookmark.autojoin();
253 final Element conference = new Element("conference", Namespace.BOOKMARKS2);
254 if (name != null) {
255 conference.setAttribute("name", name);
256 }
257 if (nick != null) {
258 conference.addChild("nick").setContent(nick);
259 }
260 conference.setAttribute("autojoin",String.valueOf(autojoin));
261 return conference;
262 }
263
264 public IqPacket publishBundles(final SignedPreKeyRecord signedPreKeyRecord, final IdentityKey identityKey,
265 final Set<PreKeyRecord> preKeyRecords, final int deviceId, Bundle publishOptions) {
266 final Element item = new Element("item");
267 item.setAttribute("id", "current");
268 final Element bundle = item.addChild("bundle", AxolotlService.PEP_PREFIX);
269 final Element signedPreKeyPublic = bundle.addChild("signedPreKeyPublic");
270 signedPreKeyPublic.setAttribute("signedPreKeyId", signedPreKeyRecord.getId());
271 ECPublicKey publicKey = signedPreKeyRecord.getKeyPair().getPublicKey();
272 signedPreKeyPublic.setContent(Base64.encodeToString(publicKey.serialize(), Base64.NO_WRAP));
273 final Element signedPreKeySignature = bundle.addChild("signedPreKeySignature");
274 signedPreKeySignature.setContent(Base64.encodeToString(signedPreKeyRecord.getSignature(), Base64.NO_WRAP));
275 final Element identityKeyElement = bundle.addChild("identityKey");
276 identityKeyElement.setContent(Base64.encodeToString(identityKey.serialize(), Base64.NO_WRAP));
277
278 final Element prekeys = bundle.addChild("prekeys", AxolotlService.PEP_PREFIX);
279 for (PreKeyRecord preKeyRecord : preKeyRecords) {
280 final Element prekey = prekeys.addChild("preKeyPublic");
281 prekey.setAttribute("preKeyId", preKeyRecord.getId());
282 prekey.setContent(Base64.encodeToString(preKeyRecord.getKeyPair().getPublicKey().serialize(), Base64.NO_WRAP));
283 }
284
285 return publish(AxolotlService.PEP_BUNDLES + ":" + deviceId, item, publishOptions);
286 }
287
288 public IqPacket publishVerification(byte[] signature, X509Certificate[] certificates, final int deviceId) {
289 final Element item = new Element("item");
290 item.setAttribute("id", "current");
291 final Element verification = item.addChild("verification", AxolotlService.PEP_PREFIX);
292 final Element chain = verification.addChild("chain");
293 for (int i = 0; i < certificates.length; ++i) {
294 try {
295 Element certificate = chain.addChild("certificate");
296 certificate.setContent(Base64.encodeToString(certificates[i].getEncoded(), Base64.NO_WRAP));
297 certificate.setAttribute("index", i);
298 } catch (CertificateEncodingException e) {
299 Log.d(Config.LOGTAG, "could not encode certificate");
300 }
301 }
302 verification.addChild("signature").setContent(Base64.encodeToString(signature, Base64.NO_WRAP));
303 return publish(AxolotlService.PEP_VERIFICATION + ":" + deviceId, item);
304 }
305
306 public IqPacket queryMessageArchiveManagement(final MessageArchiveService.Query mam) {
307 final IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
308 final Element query = packet.query(mam.version.namespace);
309 query.setAttribute("queryid", mam.getQueryId());
310 final Data data = new Data();
311 data.setFormType(mam.version.namespace);
312 if (mam.muc()) {
313 packet.setTo(mam.getWith());
314 } else if (mam.getWith() != null) {
315 data.put("with", mam.getWith().toEscapedString());
316 }
317 final long start = mam.getStart();
318 final long end = mam.getEnd();
319 if (start != 0) {
320 data.put("start", getTimestamp(start));
321 }
322 if (end != 0) {
323 data.put("end", getTimestamp(end));
324 }
325 data.submit();
326 query.addChild(data);
327 Element set = query.addChild("set", "http://jabber.org/protocol/rsm");
328 if (mam.getPagingOrder() == MessageArchiveService.PagingOrder.REVERSE) {
329 set.addChild("before").setContent(mam.getReference());
330 } else if (mam.getReference() != null) {
331 set.addChild("after").setContent(mam.getReference());
332 }
333 set.addChild("max").setContent(String.valueOf(Config.PAGE_SIZE));
334 return packet;
335 }
336
337 public IqPacket generateGetBlockList() {
338 final IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
339 iq.addChild("blocklist", Namespace.BLOCKING);
340
341 return iq;
342 }
343
344 public IqPacket generateSetBlockRequest(final Jid jid, boolean reportSpam) {
345 final IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
346 final Element block = iq.addChild("block", Namespace.BLOCKING);
347 final Element item = block.addChild("item").setAttribute("jid", jid);
348 if (reportSpam) {
349 item.addChild("report", "urn:xmpp:reporting:0").addChild("spam");
350 }
351 Log.d(Config.LOGTAG, iq.toString());
352 return iq;
353 }
354
355 public IqPacket generateSetUnblockRequest(final Jid jid) {
356 final IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
357 final Element block = iq.addChild("unblock", Namespace.BLOCKING);
358 block.addChild("item").setAttribute("jid", jid);
359 return iq;
360 }
361
362 public IqPacket generateSetPassword(final Account account, final String newPassword) {
363 final IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
364 packet.setTo(account.getDomain());
365 final Element query = packet.addChild("query", Namespace.REGISTER);
366 final Jid jid = account.getJid();
367 query.addChild("username").setContent(jid.getLocal());
368 query.addChild("password").setContent(newPassword);
369 return packet;
370 }
371
372 public IqPacket changeAffiliation(Conversation conference, Jid jid, String affiliation) {
373 List<Jid> jids = new ArrayList<>();
374 jids.add(jid);
375 return changeAffiliation(conference, jids, affiliation);
376 }
377
378 public IqPacket changeAffiliation(Conversation conference, List<Jid> jids, String affiliation) {
379 IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
380 packet.setTo(conference.getJid().asBareJid());
381 packet.setFrom(conference.getAccount().getJid());
382 Element query = packet.query("http://jabber.org/protocol/muc#admin");
383 for (Jid jid : jids) {
384 Element item = query.addChild("item");
385 item.setAttribute("jid", jid);
386 item.setAttribute("affiliation", affiliation);
387 }
388 return packet;
389 }
390
391 public IqPacket changeRole(Conversation conference, String nick, String role) {
392 IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
393 packet.setTo(conference.getJid().asBareJid());
394 packet.setFrom(conference.getAccount().getJid());
395 Element item = packet.query("http://jabber.org/protocol/muc#admin").addChild("item");
396 item.setAttribute("nick", nick);
397 item.setAttribute("role", role);
398 return packet;
399 }
400
401 public IqPacket requestHttpUploadSlot(Jid host, DownloadableFile file, String mime) {
402 IqPacket packet = new IqPacket(IqPacket.TYPE.GET);
403 packet.setTo(host);
404 Element request = packet.addChild("request", Namespace.HTTP_UPLOAD);
405 request.setAttribute("filename", convertFilename(file.getName()));
406 request.setAttribute("size", file.getExpectedSize());
407 request.setAttribute("content-type", mime);
408 return packet;
409 }
410
411 public IqPacket requestHttpUploadLegacySlot(Jid host, DownloadableFile file, String mime) {
412 IqPacket packet = new IqPacket(IqPacket.TYPE.GET);
413 packet.setTo(host);
414 Element request = packet.addChild("request", Namespace.HTTP_UPLOAD_LEGACY);
415 request.addChild("filename").setContent(convertFilename(file.getName()));
416 request.addChild("size").setContent(String.valueOf(file.getExpectedSize()));
417 request.addChild("content-type").setContent(mime);
418 return packet;
419 }
420
421 private static String convertFilename(String name) {
422 int pos = name.indexOf('.');
423 if (pos != -1) {
424 try {
425 UUID uuid = UUID.fromString(name.substring(0, pos));
426 ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
427 bb.putLong(uuid.getMostSignificantBits());
428 bb.putLong(uuid.getLeastSignificantBits());
429 return Base64.encodeToString(bb.array(), Base64.URL_SAFE | Base64.NO_PADDING | Base64.NO_WRAP) + name.substring(pos);
430 } catch (Exception e) {
431 return name;
432 }
433 } else {
434 return name;
435 }
436 }
437
438 public IqPacket generateCreateAccountWithCaptcha(Account account, String id, Data data) {
439 final IqPacket register = new IqPacket(IqPacket.TYPE.SET);
440 register.setFrom(account.getJid().asBareJid());
441 register.setTo(account.getDomain());
442 register.setId(id);
443 Element query = register.query(Namespace.REGISTER);
444 if (data != null) {
445 query.addChild(data);
446 }
447 return register;
448 }
449
450 public IqPacket pushTokenToAppServer(Jid appServer, String token, String deviceId) {
451 return pushTokenToAppServer(appServer, token, deviceId, null);
452 }
453
454 public IqPacket pushTokenToAppServer(Jid appServer, String token, String deviceId, Jid muc) {
455 final IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
456 packet.setTo(appServer);
457 final Element command = packet.addChild("command", Namespace.COMMANDS);
458 command.setAttribute("node", "register-push-fcm");
459 command.setAttribute("action", "execute");
460 final Data data = new Data();
461 data.put("token", token);
462 data.put("android-id", deviceId);
463 if (muc != null) {
464 data.put("muc", muc.toEscapedString());
465 }
466 data.submit();
467 command.addChild(data);
468 return packet;
469 }
470
471 public IqPacket unregisterChannelOnAppServer(Jid appServer, String deviceId, String channel) {
472 final IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
473 packet.setTo(appServer);
474 final Element command = packet.addChild("command", Namespace.COMMANDS);
475 command.setAttribute("node", "unregister-push-fcm");
476 command.setAttribute("action", "execute");
477 final Data data = new Data();
478 data.put("channel", channel);
479 data.put("android-id", deviceId);
480 data.submit();
481 command.addChild(data);
482 return packet;
483 }
484
485 public IqPacket enablePush(final Jid jid, final String node, final String secret) {
486 IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
487 Element enable = packet.addChild("enable", Namespace.PUSH);
488 enable.setAttribute("jid", jid);
489 enable.setAttribute("node", node);
490 if (secret != null) {
491 Data data = new Data();
492 data.setFormType(Namespace.PUBSUB_PUBLISH_OPTIONS);
493 data.put("secret", secret);
494 data.submit();
495 enable.addChild(data);
496 }
497 return packet;
498 }
499
500 public IqPacket disablePush(final Jid jid, final String node) {
501 IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
502 Element disable = packet.addChild("disable", Namespace.PUSH);
503 disable.setAttribute("jid", jid);
504 disable.setAttribute("node", node);
505 return packet;
506 }
507
508 public IqPacket queryAffiliation(Conversation conversation, String affiliation) {
509 IqPacket packet = new IqPacket(IqPacket.TYPE.GET);
510 packet.setTo(conversation.getJid().asBareJid());
511 packet.query("http://jabber.org/protocol/muc#admin").addChild("item").setAttribute("affiliation", affiliation);
512 return packet;
513 }
514
515 public static Bundle defaultGroupChatConfiguration() {
516 Bundle options = new Bundle();
517 options.putString("muc#roomconfig_persistentroom", "1");
518 options.putString("muc#roomconfig_membersonly", "1");
519 options.putString("muc#roomconfig_publicroom", "0");
520 options.putString("muc#roomconfig_whois", "anyone");
521 options.putString("muc#roomconfig_changesubject", "0");
522 options.putString("muc#roomconfig_allowinvites", "0");
523 options.putString("muc#roomconfig_enablearchiving", "1"); //prosody
524 options.putString("mam", "1"); //ejabberd community
525 options.putString("muc#roomconfig_mam", "1"); //ejabberd saas
526 return options;
527 }
528
529 public static Bundle defaultChannelConfiguration() {
530 Bundle options = new Bundle();
531 options.putString("muc#roomconfig_persistentroom", "1");
532 options.putString("muc#roomconfig_membersonly", "0");
533 options.putString("muc#roomconfig_publicroom", "1");
534 options.putString("muc#roomconfig_whois", "moderators");
535 options.putString("muc#roomconfig_changesubject", "0");
536 options.putString("muc#roomconfig_enablearchiving", "1"); //prosody
537 options.putString("mam", "1"); //ejabberd community
538 options.putString("muc#roomconfig_mam", "1"); //ejabberd saas
539 return options;
540 }
541
542 public IqPacket requestPubsubConfiguration(Jid jid, String node) {
543 return pubsubConfiguration(jid, node, null);
544 }
545
546 public IqPacket publishPubsubConfiguration(Jid jid, String node, Data data) {
547 return pubsubConfiguration(jid, node, data);
548 }
549
550 private IqPacket pubsubConfiguration(Jid jid, String node, Data data) {
551 IqPacket packet = new IqPacket(data == null ? IqPacket.TYPE.GET : IqPacket.TYPE.SET);
552 packet.setTo(jid);
553 Element pubsub = packet.addChild("pubsub", "http://jabber.org/protocol/pubsub#owner");
554 Element configure = pubsub.addChild("configure").setAttribute("node", node);
555 if (data != null) {
556 configure.addChild(data);
557 }
558 return packet;
559 }
560
561 public IqPacket queryDiscoItems(Jid jid) {
562 IqPacket packet = new IqPacket(IqPacket.TYPE.GET);
563 packet.setTo(jid);
564 packet.query(Namespace.DISCO_ITEMS);
565 return packet;
566 }
567
568 public IqPacket queryDiscoItems(Jid jid, String node) {
569 IqPacket packet = queryDiscoItems(jid);
570 final Element query = packet.query(Namespace.DISCO_ITEMS);
571 query.setAttribute("node", node);
572 return packet;
573 }
574
575 public IqPacket queryDiscoInfo(Jid jid) {
576 IqPacket packet = new IqPacket(IqPacket.TYPE.GET);
577 packet.setTo(jid);
578 packet.addChild("query",Namespace.DISCO_INFO);
579 return packet;
580 }
581
582 public IqPacket bobResponse(IqPacket request) {
583 try {
584 String bobCid = request.findChild("data", "urn:xmpp:bob").getAttribute("cid");
585 Cid cid = BobTransfer.cid(bobCid);
586 DownloadableFile f = mXmppConnectionService.getFileForCid(cid);
587 if (f == null || !f.canRead()) {
588 throw new IOException("No such file");
589 } else if (f.getSize() > 129000) {
590 final IqPacket response = request.generateResponse(IqPacket.TYPE.ERROR);
591 final Element error = response.addChild("error");
592 error.setAttribute("type", "cancel");
593 error.addChild("policy-violation", "urn:ietf:params:xml:ns:xmpp-stanzas");
594 return response;
595 } else {
596 final IqPacket response = request.generateResponse(IqPacket.TYPE.RESULT);
597 final Element data = response.addChild("data", "urn:xmpp:bob");
598 data.setAttribute("cid", bobCid);
599 data.setAttribute("type", f.getMimeType());
600 ByteArrayOutputStream b64 = new ByteArrayOutputStream((int) f.getSize() * 2);
601 ByteStreams.copy(new FileInputStream(f), new Base64OutputStream(b64, Base64.NO_WRAP));
602 data.setContent(b64.toString("utf-8"));
603 return response;
604 }
605 } catch (final IOException | IllegalStateException e) {
606 final IqPacket response = request.generateResponse(IqPacket.TYPE.ERROR);
607 final Element error = response.addChild("error");
608 error.setAttribute("type", "cancel");
609 error.addChild("item-not-found", "urn:ietf:params:xml:ns:xmpp-stanzas");
610 return response;
611 }
612 }
613}