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