IqGenerator.java

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