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