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