IqGenerator.java

  1package eu.siacs.conversations.generator;
  2
  3import android.os.Bundle;
  4import android.util.Base64;
  5import android.util.Log;
  6import eu.siacs.conversations.Config;
  7import eu.siacs.conversations.crypto.axolotl.AxolotlService;
  8import eu.siacs.conversations.entities.Account;
  9import eu.siacs.conversations.entities.Conversation;
 10import eu.siacs.conversations.services.MessageArchiveService;
 11import eu.siacs.conversations.services.XmppConnectionService;
 12import eu.siacs.conversations.xml.Element;
 13import eu.siacs.conversations.xml.Namespace;
 14import eu.siacs.conversations.xmpp.Jid;
 15import eu.siacs.conversations.xmpp.forms.Data;
 16import eu.siacs.conversations.xmpp.pep.Avatar;
 17import im.conversations.android.xmpp.model.stanza.Iq;
 18import java.security.cert.CertificateEncodingException;
 19import java.security.cert.X509Certificate;
 20import java.util.ArrayList;
 21import java.util.List;
 22import java.util.Set;
 23import org.whispersystems.libsignal.IdentityKey;
 24import org.whispersystems.libsignal.ecc.ECPublicKey;
 25import org.whispersystems.libsignal.state.PreKeyRecord;
 26import org.whispersystems.libsignal.state.SignedPreKeyRecord;
 27
 28public class IqGenerator extends AbstractGenerator {
 29
 30    public IqGenerator(final XmppConnectionService service) {
 31        super(service);
 32    }
 33
 34    public static Iq purgeOfflineMessages() {
 35        final Iq packet = new Iq(Iq.Type.SET);
 36        packet.addChild("offline", Namespace.FLEXIBLE_OFFLINE_MESSAGE_RETRIEVAL).addChild("purge");
 37        return packet;
 38    }
 39
 40    protected Iq publish(final String node, final Element item, final Bundle options) {
 41        final var packet = new Iq(Iq.Type.SET);
 42        final Element pubsub = packet.addChild("pubsub", Namespace.PUBSUB);
 43        final Element publish = pubsub.addChild("publish");
 44        publish.setAttribute("node", node);
 45        publish.addChild(item);
 46        if (options != null) {
 47            final Element publishOptions = pubsub.addChild("publish-options");
 48            publishOptions.addChild(Data.create(Namespace.PUBSUB_PUBLISH_OPTIONS, options));
 49        }
 50        return packet;
 51    }
 52
 53    protected Iq publish(final String node, final Element item) {
 54        return publish(node, item, null);
 55    }
 56
 57    private Iq retrieve(String node, Element item) {
 58        final var packet = new Iq(Iq.Type.GET);
 59        final Element pubsub = packet.addChild("pubsub", Namespace.PUBSUB);
 60        final Element items = pubsub.addChild("items");
 61        items.setAttribute("node", node);
 62        if (item != null) {
 63            items.addChild(item);
 64        }
 65        return packet;
 66    }
 67
 68    public Iq deleteNode(final String node) {
 69        final var packet = new Iq(Iq.Type.SET);
 70        final Element pubsub = packet.addChild("pubsub", Namespace.PUBSUB_OWNER);
 71        pubsub.addChild("delete").setAttribute("node", node);
 72        return packet;
 73    }
 74
 75    public Iq retrievePepAvatar(final Avatar avatar) {
 76        final Element item = new Element("item");
 77        item.setAttribute("id", avatar.sha1sum);
 78        final var packet = retrieve(Namespace.AVATAR_DATA, item);
 79        packet.setTo(avatar.owner);
 80        return packet;
 81    }
 82
 83    public Iq retrieveVcardAvatar(final Avatar avatar) {
 84        final Iq packet = new Iq(Iq.Type.GET);
 85        packet.setTo(avatar.owner);
 86        packet.addChild("vCard", "vcard-temp");
 87        return packet;
 88    }
 89
 90    public Iq retrieveVcardAvatar(final Jid to) {
 91        final Iq packet = new Iq(Iq.Type.GET);
 92        packet.setTo(to);
 93        packet.addChild("vCard", "vcard-temp");
 94        return packet;
 95    }
 96
 97    public Iq retrieveAvatarMetaData(final Jid to) {
 98        final Iq packet = retrieve("urn:xmpp:avatar:metadata", null);
 99        if (to != null) {
100            packet.setTo(to);
101        }
102        return packet;
103    }
104
105    public Iq retrieveDeviceIds(final Jid to) {
106        final var packet = retrieve(AxolotlService.PEP_DEVICE_LIST, null);
107        if (to != null) {
108            packet.setTo(to);
109        }
110        return packet;
111    }
112
113    public Iq retrieveBundlesForDevice(final Jid to, final int deviceid) {
114        final var packet = retrieve(AxolotlService.PEP_BUNDLES + ":" + deviceid, null);
115        packet.setTo(to);
116        return packet;
117    }
118
119    public Iq retrieveVerificationForDevice(final Jid to, final int deviceid) {
120        final var packet = retrieve(AxolotlService.PEP_VERIFICATION + ":" + deviceid, null);
121        packet.setTo(to);
122        return packet;
123    }
124
125    public Iq publishDeviceIds(final Set<Integer> ids, final Bundle publishOptions) {
126        final Element item = new Element("item");
127        item.setAttribute("id", "current");
128        final Element list = item.addChild("list", AxolotlService.PEP_PREFIX);
129        for (Integer id : ids) {
130            final Element device = new Element("device");
131            device.setAttribute("id", id);
132            list.addChild(device);
133        }
134        return publish(AxolotlService.PEP_DEVICE_LIST, item, publishOptions);
135    }
136
137    public Iq publishBundles(
138            final SignedPreKeyRecord signedPreKeyRecord,
139            final IdentityKey identityKey,
140            final Set<PreKeyRecord> preKeyRecords,
141            final int deviceId,
142            Bundle publishOptions) {
143        final Element item = new Element("item");
144        item.setAttribute("id", "current");
145        final Element bundle = item.addChild("bundle", AxolotlService.PEP_PREFIX);
146        final Element signedPreKeyPublic = bundle.addChild("signedPreKeyPublic");
147        signedPreKeyPublic.setAttribute("signedPreKeyId", signedPreKeyRecord.getId());
148        ECPublicKey publicKey = signedPreKeyRecord.getKeyPair().getPublicKey();
149        signedPreKeyPublic.setContent(Base64.encodeToString(publicKey.serialize(), Base64.NO_WRAP));
150        final Element signedPreKeySignature = bundle.addChild("signedPreKeySignature");
151        signedPreKeySignature.setContent(
152                Base64.encodeToString(signedPreKeyRecord.getSignature(), Base64.NO_WRAP));
153        final Element identityKeyElement = bundle.addChild("identityKey");
154        identityKeyElement.setContent(
155                Base64.encodeToString(identityKey.serialize(), Base64.NO_WRAP));
156
157        final Element prekeys = bundle.addChild("prekeys", AxolotlService.PEP_PREFIX);
158        for (PreKeyRecord preKeyRecord : preKeyRecords) {
159            final Element prekey = prekeys.addChild("preKeyPublic");
160            prekey.setAttribute("preKeyId", preKeyRecord.getId());
161            prekey.setContent(
162                    Base64.encodeToString(
163                            preKeyRecord.getKeyPair().getPublicKey().serialize(), Base64.NO_WRAP));
164        }
165
166        return publish(AxolotlService.PEP_BUNDLES + ":" + deviceId, item, publishOptions);
167    }
168
169    public Iq publishVerification(
170            byte[] signature, X509Certificate[] certificates, final int deviceId) {
171        final Element item = new Element("item");
172        item.setAttribute("id", "current");
173        final Element verification = item.addChild("verification", AxolotlService.PEP_PREFIX);
174        final Element chain = verification.addChild("chain");
175        for (int i = 0; i < certificates.length; ++i) {
176            try {
177                Element certificate = chain.addChild("certificate");
178                certificate.setContent(
179                        Base64.encodeToString(certificates[i].getEncoded(), Base64.NO_WRAP));
180                certificate.setAttribute("index", i);
181            } catch (CertificateEncodingException e) {
182                Log.d(Config.LOGTAG, "could not encode certificate");
183            }
184        }
185        verification
186                .addChild("signature")
187                .setContent(Base64.encodeToString(signature, Base64.NO_WRAP));
188        return publish(AxolotlService.PEP_VERIFICATION + ":" + deviceId, item);
189    }
190
191    public Iq queryMessageArchiveManagement(final MessageArchiveService.Query mam) {
192        final Iq packet = new Iq(Iq.Type.SET);
193        final Element query = packet.query(mam.version.namespace);
194        query.setAttribute("queryid", mam.getQueryId());
195        final Data data = new Data();
196        data.setFormType(mam.version.namespace);
197        if (mam.muc()) {
198            packet.setTo(mam.getWith());
199        } else if (mam.getWith() != null) {
200            data.put("with", mam.getWith().toString());
201        }
202        final long start = mam.getStart();
203        final long end = mam.getEnd();
204        if (start != 0) {
205            data.put("start", getTimestamp(start));
206        }
207        if (end != 0) {
208            data.put("end", getTimestamp(end));
209        }
210        data.submit();
211        query.addChild(data);
212        Element set = query.addChild("set", "http://jabber.org/protocol/rsm");
213        if (mam.getPagingOrder() == MessageArchiveService.PagingOrder.REVERSE) {
214            set.addChild("before").setContent(mam.getReference());
215        } else if (mam.getReference() != null) {
216            set.addChild("after").setContent(mam.getReference());
217        }
218        set.addChild("max").setContent(String.valueOf(Config.PAGE_SIZE));
219        return packet;
220    }
221
222    public Iq generateSetPassword(final Account account, final String newPassword) {
223        final Iq packet = new Iq(Iq.Type.SET);
224        packet.setTo(account.getDomain());
225        final Element query = packet.addChild("query", Namespace.REGISTER);
226        final Jid jid = account.getJid();
227        query.addChild("username").setContent(jid.getLocal());
228        query.addChild("password").setContent(newPassword);
229        return packet;
230    }
231
232    public Iq changeAffiliation(Conversation conference, Jid jid, String affiliation) {
233        List<Jid> jids = new ArrayList<>();
234        jids.add(jid);
235        return changeAffiliation(conference, jids, affiliation);
236    }
237
238    public Iq changeAffiliation(Conversation conference, List<Jid> jids, String affiliation) {
239        final Iq packet = new Iq(Iq.Type.SET);
240        packet.setTo(conference.getJid().asBareJid());
241        packet.setFrom(conference.getAccount().getJid());
242        Element query = packet.query("http://jabber.org/protocol/muc#admin");
243        for (Jid jid : jids) {
244            Element item = query.addChild("item");
245            item.setAttribute("jid", jid);
246            item.setAttribute("affiliation", affiliation);
247        }
248        return packet;
249    }
250
251    public Iq changeRole(Conversation conference, String nick, String role) {
252        final Iq packet = new Iq(Iq.Type.SET);
253        packet.setTo(conference.getJid().asBareJid());
254        packet.setFrom(conference.getAccount().getJid());
255        Element item = packet.query("http://jabber.org/protocol/muc#admin").addChild("item");
256        item.setAttribute("nick", nick);
257        item.setAttribute("role", role);
258        return packet;
259    }
260
261    public static Iq generateCreateAccountWithCaptcha(
262            final Account account, final String id, final Data data) {
263        final Iq register = new Iq(Iq.Type.SET);
264        register.setFrom(account.getJid().asBareJid());
265        register.setTo(account.getDomain());
266        register.setId(id);
267        Element query = register.query(Namespace.REGISTER);
268        if (data != null) {
269            query.addChild(data);
270        }
271        return register;
272    }
273
274    public Iq pushTokenToAppServer(Jid appServer, String token, String deviceId) {
275        return pushTokenToAppServer(appServer, token, deviceId, null);
276    }
277
278    public Iq pushTokenToAppServer(Jid appServer, String token, String deviceId, Jid muc) {
279        final Iq packet = new Iq(Iq.Type.SET);
280        packet.setTo(appServer);
281        final Element command = packet.addChild("command", Namespace.COMMANDS);
282        command.setAttribute("node", "register-push-fcm");
283        command.setAttribute("action", "execute");
284        final Data data = new Data();
285        data.put("token", token);
286        data.put("android-id", deviceId);
287        if (muc != null) {
288            data.put("muc", muc.toString());
289        }
290        data.submit();
291        command.addChild(data);
292        return packet;
293    }
294
295    public Iq unregisterChannelOnAppServer(Jid appServer, String deviceId, String channel) {
296        final Iq packet = new Iq(Iq.Type.SET);
297        packet.setTo(appServer);
298        final Element command = packet.addChild("command", Namespace.COMMANDS);
299        command.setAttribute("node", "unregister-push-fcm");
300        command.setAttribute("action", "execute");
301        final Data data = new Data();
302        data.put("channel", channel);
303        data.put("android-id", deviceId);
304        data.submit();
305        command.addChild(data);
306        return packet;
307    }
308
309    public Iq enablePush(final Jid jid, final String node, final String secret) {
310        final Iq packet = new Iq(Iq.Type.SET);
311        Element enable = packet.addChild("enable", Namespace.PUSH);
312        enable.setAttribute("jid", jid);
313        enable.setAttribute("node", node);
314        if (secret != null) {
315            Data data = new Data();
316            data.setFormType(Namespace.PUBSUB_PUBLISH_OPTIONS);
317            data.put("secret", secret);
318            data.submit();
319            enable.addChild(data);
320        }
321        return packet;
322    }
323
324    public Iq disablePush(final Jid jid, final String node) {
325        Iq packet = new Iq(Iq.Type.SET);
326        Element disable = packet.addChild("disable", Namespace.PUSH);
327        disable.setAttribute("jid", jid);
328        disable.setAttribute("node", node);
329        return packet;
330    }
331
332    public Iq queryAffiliation(Conversation conversation, String affiliation) {
333        final Iq packet = new Iq(Iq.Type.GET);
334        packet.setTo(conversation.getJid().asBareJid());
335        packet.query("http://jabber.org/protocol/muc#admin")
336                .addChild("item")
337                .setAttribute("affiliation", affiliation);
338        return packet;
339    }
340
341    public static Bundle defaultGroupChatConfiguration() {
342        Bundle options = new Bundle();
343        options.putString("muc#roomconfig_persistentroom", "1");
344        options.putString("muc#roomconfig_membersonly", "1");
345        options.putString("muc#roomconfig_publicroom", "0");
346        options.putString("muc#roomconfig_whois", "anyone");
347        options.putString("muc#roomconfig_changesubject", "0");
348        options.putString("muc#roomconfig_allowinvites", "0");
349        options.putString("muc#roomconfig_enablearchiving", "1"); // prosody
350        options.putString("mam", "1"); // ejabberd community
351        options.putString("muc#roomconfig_mam", "1"); // ejabberd saas
352        return options;
353    }
354
355    public static Bundle defaultChannelConfiguration() {
356        Bundle options = new Bundle();
357        options.putString("muc#roomconfig_persistentroom", "1");
358        options.putString("muc#roomconfig_membersonly", "0");
359        options.putString("muc#roomconfig_publicroom", "1");
360        options.putString("muc#roomconfig_whois", "moderators");
361        options.putString("muc#roomconfig_changesubject", "0");
362        options.putString("muc#roomconfig_enablearchiving", "1"); // prosody
363        options.putString("mam", "1"); // ejabberd community
364        options.putString("muc#roomconfig_mam", "1"); // ejabberd saas
365        return options;
366    }
367
368    public Iq requestPubsubConfiguration(Jid jid, String node) {
369        return pubsubConfiguration(jid, node, null);
370    }
371
372    public Iq publishPubsubConfiguration(Jid jid, String node, Data data) {
373        return pubsubConfiguration(jid, node, data);
374    }
375
376    private Iq pubsubConfiguration(Jid jid, String node, Data data) {
377        final Iq packet = new Iq(data == null ? Iq.Type.GET : Iq.Type.SET);
378        packet.setTo(jid);
379        Element pubsub = packet.addChild("pubsub", "http://jabber.org/protocol/pubsub#owner");
380        Element configure = pubsub.addChild("configure").setAttribute("node", node);
381        if (data != null) {
382            configure.addChild(data);
383        }
384        return packet;
385    }
386}