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 retrieveDeviceIds(final Jid to) {
 91        final var packet = retrieve(AxolotlService.PEP_DEVICE_LIST, null);
 92        if (to != null) {
 93            packet.setTo(to);
 94        }
 95        return packet;
 96    }
 97
 98    public Iq retrieveBundlesForDevice(final Jid to, final int deviceid) {
 99        final var packet = retrieve(AxolotlService.PEP_BUNDLES + ":" + deviceid, null);
100        packet.setTo(to);
101        return packet;
102    }
103
104    public Iq retrieveVerificationForDevice(final Jid to, final int deviceid) {
105        final var packet = retrieve(AxolotlService.PEP_VERIFICATION + ":" + deviceid, null);
106        packet.setTo(to);
107        return packet;
108    }
109
110    public Iq publishDeviceIds(final Set<Integer> ids, final Bundle publishOptions) {
111        final Element item = new Element("item");
112        item.setAttribute("id", "current");
113        final Element list = item.addChild("list", AxolotlService.PEP_PREFIX);
114        for (Integer id : ids) {
115            final Element device = new Element("device");
116            device.setAttribute("id", id);
117            list.addChild(device);
118        }
119        return publish(AxolotlService.PEP_DEVICE_LIST, item, publishOptions);
120    }
121
122    public Iq publishBundles(
123            final SignedPreKeyRecord signedPreKeyRecord,
124            final IdentityKey identityKey,
125            final Set<PreKeyRecord> preKeyRecords,
126            final int deviceId,
127            Bundle publishOptions) {
128        final Element item = new Element("item");
129        item.setAttribute("id", "current");
130        final Element bundle = item.addChild("bundle", AxolotlService.PEP_PREFIX);
131        final Element signedPreKeyPublic = bundle.addChild("signedPreKeyPublic");
132        signedPreKeyPublic.setAttribute("signedPreKeyId", signedPreKeyRecord.getId());
133        ECPublicKey publicKey = signedPreKeyRecord.getKeyPair().getPublicKey();
134        signedPreKeyPublic.setContent(Base64.encodeToString(publicKey.serialize(), Base64.NO_WRAP));
135        final Element signedPreKeySignature = bundle.addChild("signedPreKeySignature");
136        signedPreKeySignature.setContent(
137                Base64.encodeToString(signedPreKeyRecord.getSignature(), Base64.NO_WRAP));
138        final Element identityKeyElement = bundle.addChild("identityKey");
139        identityKeyElement.setContent(
140                Base64.encodeToString(identityKey.serialize(), Base64.NO_WRAP));
141
142        final Element prekeys = bundle.addChild("prekeys", AxolotlService.PEP_PREFIX);
143        for (PreKeyRecord preKeyRecord : preKeyRecords) {
144            final Element prekey = prekeys.addChild("preKeyPublic");
145            prekey.setAttribute("preKeyId", preKeyRecord.getId());
146            prekey.setContent(
147                    Base64.encodeToString(
148                            preKeyRecord.getKeyPair().getPublicKey().serialize(), Base64.NO_WRAP));
149        }
150
151        return publish(AxolotlService.PEP_BUNDLES + ":" + deviceId, item, publishOptions);
152    }
153
154    public Iq publishVerification(
155            byte[] signature, X509Certificate[] certificates, final int deviceId) {
156        final Element item = new Element("item");
157        item.setAttribute("id", "current");
158        final Element verification = item.addChild("verification", AxolotlService.PEP_PREFIX);
159        final Element chain = verification.addChild("chain");
160        for (int i = 0; i < certificates.length; ++i) {
161            try {
162                Element certificate = chain.addChild("certificate");
163                certificate.setContent(
164                        Base64.encodeToString(certificates[i].getEncoded(), Base64.NO_WRAP));
165                certificate.setAttribute("index", i);
166            } catch (CertificateEncodingException e) {
167                Log.d(Config.LOGTAG, "could not encode certificate");
168            }
169        }
170        verification
171                .addChild("signature")
172                .setContent(Base64.encodeToString(signature, Base64.NO_WRAP));
173        return publish(AxolotlService.PEP_VERIFICATION + ":" + deviceId, item);
174    }
175
176    public Iq queryMessageArchiveManagement(final MessageArchiveService.Query mam) {
177        final Iq packet = new Iq(Iq.Type.SET);
178        final Element query = packet.query(mam.version.namespace);
179        query.setAttribute("queryid", mam.getQueryId());
180        final Data data = new Data();
181        data.setFormType(mam.version.namespace);
182        if (mam.muc()) {
183            packet.setTo(mam.getWith());
184        } else if (mam.getWith() != null) {
185            data.put("with", mam.getWith().toString());
186        }
187        final long start = mam.getStart();
188        final long end = mam.getEnd();
189        if (start != 0) {
190            data.put("start", getTimestamp(start));
191        }
192        if (end != 0) {
193            data.put("end", getTimestamp(end));
194        }
195        data.submit();
196        query.addChild(data);
197        Element set = query.addChild("set", "http://jabber.org/protocol/rsm");
198        if (mam.getPagingOrder() == MessageArchiveService.PagingOrder.REVERSE) {
199            set.addChild("before").setContent(mam.getReference());
200        } else if (mam.getReference() != null) {
201            set.addChild("after").setContent(mam.getReference());
202        }
203        set.addChild("max").setContent(String.valueOf(Config.PAGE_SIZE));
204        return packet;
205    }
206
207    public Iq generateSetPassword(final Account account, final String newPassword) {
208        final Iq packet = new Iq(Iq.Type.SET);
209        packet.setTo(account.getDomain());
210        final Element query = packet.addChild("query", Namespace.REGISTER);
211        final Jid jid = account.getJid();
212        query.addChild("username").setContent(jid.getLocal());
213        query.addChild("password").setContent(newPassword);
214        return packet;
215    }
216
217    public Iq changeAffiliation(Conversation conference, Jid jid, String affiliation) {
218        List<Jid> jids = new ArrayList<>();
219        jids.add(jid);
220        return changeAffiliation(conference, jids, affiliation);
221    }
222
223    public Iq changeAffiliation(Conversation conference, List<Jid> jids, String affiliation) {
224        final Iq packet = new Iq(Iq.Type.SET);
225        packet.setTo(conference.getJid().asBareJid());
226        packet.setFrom(conference.getAccount().getJid());
227        Element query = packet.query("http://jabber.org/protocol/muc#admin");
228        for (Jid jid : jids) {
229            Element item = query.addChild("item");
230            item.setAttribute("jid", jid);
231            item.setAttribute("affiliation", affiliation);
232        }
233        return packet;
234    }
235
236    public Iq changeRole(Conversation conference, String nick, String role) {
237        final Iq packet = new Iq(Iq.Type.SET);
238        packet.setTo(conference.getJid().asBareJid());
239        packet.setFrom(conference.getAccount().getJid());
240        Element item = packet.query("http://jabber.org/protocol/muc#admin").addChild("item");
241        item.setAttribute("nick", nick);
242        item.setAttribute("role", role);
243        return packet;
244    }
245
246    public static Iq generateCreateAccountWithCaptcha(
247            final Account account, final String id, final Data data) {
248        final Iq register = new Iq(Iq.Type.SET);
249        register.setFrom(account.getJid().asBareJid());
250        register.setTo(account.getDomain());
251        register.setId(id);
252        Element query = register.query(Namespace.REGISTER);
253        if (data != null) {
254            query.addChild(data);
255        }
256        return register;
257    }
258
259    public Iq pushTokenToAppServer(Jid appServer, String token, String deviceId) {
260        return pushTokenToAppServer(appServer, token, deviceId, null);
261    }
262
263    public Iq pushTokenToAppServer(Jid appServer, String token, String deviceId, Jid muc) {
264        final Iq packet = new Iq(Iq.Type.SET);
265        packet.setTo(appServer);
266        final Element command = packet.addChild("command", Namespace.COMMANDS);
267        command.setAttribute("node", "register-push-fcm");
268        command.setAttribute("action", "execute");
269        final Data data = new Data();
270        data.put("token", token);
271        data.put("android-id", deviceId);
272        if (muc != null) {
273            data.put("muc", muc.toString());
274        }
275        data.submit();
276        command.addChild(data);
277        return packet;
278    }
279
280    public Iq unregisterChannelOnAppServer(Jid appServer, String deviceId, String channel) {
281        final Iq packet = new Iq(Iq.Type.SET);
282        packet.setTo(appServer);
283        final Element command = packet.addChild("command", Namespace.COMMANDS);
284        command.setAttribute("node", "unregister-push-fcm");
285        command.setAttribute("action", "execute");
286        final Data data = new Data();
287        data.put("channel", channel);
288        data.put("android-id", deviceId);
289        data.submit();
290        command.addChild(data);
291        return packet;
292    }
293
294    public Iq enablePush(final Jid jid, final String node, final String secret) {
295        final Iq packet = new Iq(Iq.Type.SET);
296        Element enable = packet.addChild("enable", Namespace.PUSH);
297        enable.setAttribute("jid", jid);
298        enable.setAttribute("node", node);
299        if (secret != null) {
300            Data data = new Data();
301            data.setFormType(Namespace.PUBSUB_PUBLISH_OPTIONS);
302            data.put("secret", secret);
303            data.submit();
304            enable.addChild(data);
305        }
306        return packet;
307    }
308
309    public Iq disablePush(final Jid jid, final String node) {
310        Iq packet = new Iq(Iq.Type.SET);
311        Element disable = packet.addChild("disable", Namespace.PUSH);
312        disable.setAttribute("jid", jid);
313        disable.setAttribute("node", node);
314        return packet;
315    }
316
317    public Iq queryAffiliation(Conversation conversation, String affiliation) {
318        final Iq packet = new Iq(Iq.Type.GET);
319        packet.setTo(conversation.getJid().asBareJid());
320        packet.query("http://jabber.org/protocol/muc#admin")
321                .addChild("item")
322                .setAttribute("affiliation", affiliation);
323        return packet;
324    }
325
326    public static Bundle defaultGroupChatConfiguration() {
327        Bundle options = new Bundle();
328        options.putString("muc#roomconfig_persistentroom", "1");
329        options.putString("muc#roomconfig_membersonly", "1");
330        options.putString("muc#roomconfig_publicroom", "0");
331        options.putString("muc#roomconfig_whois", "anyone");
332        options.putString("muc#roomconfig_changesubject", "0");
333        options.putString("muc#roomconfig_allowinvites", "0");
334        options.putString("muc#roomconfig_enablearchiving", "1"); // prosody
335        options.putString("mam", "1"); // ejabberd community
336        options.putString("muc#roomconfig_mam", "1"); // ejabberd saas
337        return options;
338    }
339
340    public static Bundle defaultChannelConfiguration() {
341        Bundle options = new Bundle();
342        options.putString("muc#roomconfig_persistentroom", "1");
343        options.putString("muc#roomconfig_membersonly", "0");
344        options.putString("muc#roomconfig_publicroom", "1");
345        options.putString("muc#roomconfig_whois", "moderators");
346        options.putString("muc#roomconfig_changesubject", "0");
347        options.putString("muc#roomconfig_enablearchiving", "1"); // prosody
348        options.putString("mam", "1"); // ejabberd community
349        options.putString("muc#roomconfig_mam", "1"); // ejabberd saas
350        return options;
351    }
352
353    public Iq requestPubsubConfiguration(Jid jid, String node) {
354        return pubsubConfiguration(jid, node, null);
355    }
356
357    public Iq publishPubsubConfiguration(Jid jid, String node, Data data) {
358        return pubsubConfiguration(jid, node, data);
359    }
360
361    private Iq pubsubConfiguration(Jid jid, String node, Data data) {
362        final Iq packet = new Iq(data == null ? Iq.Type.GET : Iq.Type.SET);
363        packet.setTo(jid);
364        Element pubsub = packet.addChild("pubsub", "http://jabber.org/protocol/pubsub#owner");
365        Element configure = pubsub.addChild("configure").setAttribute("node", node);
366        if (data != null) {
367            configure.addChild(data);
368        }
369        return packet;
370    }
371}