1package eu.siacs.conversations.generator;
2
3
4import android.util.Base64;
5import android.util.Log;
6
7import org.whispersystems.libaxolotl.IdentityKey;
8import org.whispersystems.libaxolotl.ecc.ECPublicKey;
9import org.whispersystems.libaxolotl.state.PreKeyRecord;
10import org.whispersystems.libaxolotl.state.SignedPreKeyRecord;
11
12import java.security.cert.CertificateEncodingException;
13import java.security.cert.X509Certificate;
14import java.util.ArrayList;
15import java.util.List;
16import java.util.Set;
17
18import eu.siacs.conversations.Config;
19import eu.siacs.conversations.crypto.axolotl.AxolotlService;
20import eu.siacs.conversations.entities.Account;
21import eu.siacs.conversations.entities.Conversation;
22import eu.siacs.conversations.entities.DownloadableFile;
23import eu.siacs.conversations.services.MessageArchiveService;
24import eu.siacs.conversations.services.XmppConnectionService;
25import eu.siacs.conversations.utils.Xmlns;
26import eu.siacs.conversations.xml.Element;
27import eu.siacs.conversations.xmpp.forms.Data;
28import eu.siacs.conversations.xmpp.jid.Jid;
29import eu.siacs.conversations.xmpp.pep.Avatar;
30import eu.siacs.conversations.xmpp.stanzas.IqPacket;
31
32public class IqGenerator extends AbstractGenerator {
33
34 public IqGenerator(final XmppConnectionService service) {
35 super(service);
36 }
37
38 public IqPacket discoResponse(final IqPacket request) {
39 final IqPacket packet = new IqPacket(IqPacket.TYPE.RESULT);
40 packet.setId(request.getId());
41 packet.setTo(request.getFrom());
42 final Element query = packet.addChild("query",
43 "http://jabber.org/protocol/disco#info");
44 query.setAttribute("node", request.query().getAttribute("node"));
45 final Element identity = query.addChild("identity");
46 identity.setAttribute("category", "client");
47 identity.setAttribute("type", IDENTITY_TYPE);
48 identity.setAttribute("name", getIdentityName());
49 for (final String feature : getFeatures()) {
50 query.addChild("feature").setAttribute("var", feature);
51 }
52 return packet;
53 }
54
55 public IqPacket versionResponse(final IqPacket request) {
56 final IqPacket packet = request.generateResponse(IqPacket.TYPE.RESULT);
57 Element query = packet.query("jabber:iq:version");
58 query.addChild("name").setContent(IDENTITY_NAME);
59 query.addChild("version").setContent(getIdentityVersion());
60 return packet;
61 }
62
63 protected IqPacket publish(final String node, final Element item) {
64 final IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
65 final Element pubsub = packet.addChild("pubsub",
66 "http://jabber.org/protocol/pubsub");
67 final Element publish = pubsub.addChild("publish");
68 publish.setAttribute("node", node);
69 publish.addChild(item);
70 return packet;
71 }
72
73 protected IqPacket retrieve(String node, Element item) {
74 final IqPacket packet = new IqPacket(IqPacket.TYPE.GET);
75 final Element pubsub = packet.addChild("pubsub",
76 "http://jabber.org/protocol/pubsub");
77 final Element items = pubsub.addChild("items");
78 items.setAttribute("node", node);
79 if (item != null) {
80 items.addChild(item);
81 }
82 return packet;
83 }
84
85 public IqPacket publishAvatar(Avatar avatar) {
86 final Element item = new Element("item");
87 item.setAttribute("id", avatar.sha1sum);
88 final Element data = item.addChild("data", "urn:xmpp:avatar:data");
89 data.setContent(avatar.image);
90 return publish("urn:xmpp:avatar:data", item);
91 }
92
93 public IqPacket publishAvatarMetadata(final Avatar avatar) {
94 final Element item = new Element("item");
95 item.setAttribute("id", avatar.sha1sum);
96 final Element metadata = item
97 .addChild("metadata", "urn:xmpp:avatar:metadata");
98 final Element info = metadata.addChild("info");
99 info.setAttribute("bytes", avatar.size);
100 info.setAttribute("id", avatar.sha1sum);
101 info.setAttribute("height", avatar.height);
102 info.setAttribute("width", avatar.height);
103 info.setAttribute("type", avatar.type);
104 return publish("urn:xmpp:avatar:metadata", item);
105 }
106
107 public IqPacket retrievePepAvatar(final Avatar avatar) {
108 final Element item = new Element("item");
109 item.setAttribute("id", avatar.sha1sum);
110 final IqPacket packet = retrieve("urn:xmpp:avatar:data", item);
111 packet.setTo(avatar.owner);
112 return packet;
113 }
114
115 public IqPacket retrieveVcardAvatar(final Avatar avatar) {
116 final IqPacket packet = new IqPacket(IqPacket.TYPE.GET);
117 packet.setTo(avatar.owner);
118 packet.addChild("vCard", "vcard-temp");
119 return packet;
120 }
121
122 public IqPacket retrieveAvatarMetaData(final Jid to) {
123 final IqPacket packet = retrieve("urn:xmpp:avatar:metadata", null);
124 if (to != null) {
125 packet.setTo(to);
126 }
127 return packet;
128 }
129
130 public IqPacket retrieveDeviceIds(final Jid to) {
131 final IqPacket packet = retrieve(AxolotlService.PEP_DEVICE_LIST, null);
132 if(to != null) {
133 packet.setTo(to);
134 }
135 return packet;
136 }
137
138 public IqPacket retrieveBundlesForDevice(final Jid to, final int deviceid) {
139 final IqPacket packet = retrieve(AxolotlService.PEP_BUNDLES+":"+deviceid, null);
140 if(to != null) {
141 packet.setTo(to);
142 }
143 return packet;
144 }
145
146 public IqPacket publishDeviceIds(final Set<Integer> ids) {
147 final Element item = new Element("item");
148 final Element list = item.addChild("list", AxolotlService.PEP_PREFIX);
149 for(Integer id:ids) {
150 final Element device = new Element("device");
151 device.setAttribute("id", id);
152 list.addChild(device);
153 }
154 return publish(AxolotlService.PEP_DEVICE_LIST, item);
155 }
156
157 public IqPacket publishBundles(final SignedPreKeyRecord signedPreKeyRecord, final IdentityKey identityKey,
158 final Set<PreKeyRecord> preKeyRecords, final int deviceId) {
159 final Element item = new Element("item");
160 final Element bundle = item.addChild("bundle", AxolotlService.PEP_PREFIX);
161 final Element signedPreKeyPublic = bundle.addChild("signedPreKeyPublic");
162 signedPreKeyPublic.setAttribute("signedPreKeyId", signedPreKeyRecord.getId());
163 ECPublicKey publicKey = signedPreKeyRecord.getKeyPair().getPublicKey();
164 signedPreKeyPublic.setContent(Base64.encodeToString(publicKey.serialize(),Base64.DEFAULT));
165 final Element signedPreKeySignature = bundle.addChild("signedPreKeySignature");
166 signedPreKeySignature.setContent(Base64.encodeToString(signedPreKeyRecord.getSignature(),Base64.DEFAULT));
167 final Element identityKeyElement = bundle.addChild("identityKey");
168 identityKeyElement.setContent(Base64.encodeToString(identityKey.serialize(), Base64.DEFAULT));
169
170 final Element prekeys = bundle.addChild("prekeys", AxolotlService.PEP_PREFIX);
171 for(PreKeyRecord preKeyRecord:preKeyRecords) {
172 final Element prekey = prekeys.addChild("preKeyPublic");
173 prekey.setAttribute("preKeyId", preKeyRecord.getId());
174 prekey.setContent(Base64.encodeToString(preKeyRecord.getKeyPair().getPublicKey().serialize(), Base64.DEFAULT));
175 }
176
177 return publish(AxolotlService.PEP_BUNDLES+":"+deviceId, item);
178 }
179
180 public IqPacket publishVerification(byte[] signature, X509Certificate[] certificates, final int deviceId) {
181 final Element item = new Element("item");
182 final Element verification = item.addChild("verification", AxolotlService.PEP_PREFIX);
183 final Element chain = verification.addChild("chain");
184 for(int i = 0; i < certificates.length; ++i) {
185 try {
186 Element certificate = chain.addChild("certificate");
187 certificate.setContent(Base64.encodeToString(certificates[i].getEncoded(), Base64.DEFAULT));
188 certificate.setAttribute("index",i);
189 } catch (CertificateEncodingException e) {
190 Log.d(Config.LOGTAG, "could not encode certificate");
191 }
192 }
193 verification.addChild("signature").setContent(Base64.encodeToString(signature, Base64.DEFAULT));
194 return publish(AxolotlService.PEP_VERIFICATION+":"+deviceId, item);
195 }
196
197 public IqPacket queryMessageArchiveManagement(final MessageArchiveService.Query mam) {
198 final IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
199 final Element query = packet.query("urn:xmpp:mam:0");
200 query.setAttribute("queryid", mam.getQueryId());
201 final Data data = new Data();
202 data.setFormType("urn:xmpp:mam:0");
203 if (mam.muc()) {
204 packet.setTo(mam.getWith());
205 } else if (mam.getWith()!=null) {
206 data.put("with", mam.getWith().toString());
207 }
208 data.put("start", getTimestamp(mam.getStart()));
209 data.put("end", getTimestamp(mam.getEnd()));
210 data.submit();
211 query.addChild(data);
212 if (mam.getPagingOrder() == MessageArchiveService.PagingOrder.REVERSE) {
213 query.addChild("set", "http://jabber.org/protocol/rsm").addChild("before").setContent(mam.getReference());
214 } else if (mam.getReference() != null) {
215 query.addChild("set", "http://jabber.org/protocol/rsm").addChild("after").setContent(mam.getReference());
216 }
217 return packet;
218 }
219 public IqPacket generateGetBlockList() {
220 final IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
221 iq.addChild("blocklist", Xmlns.BLOCKING);
222
223 return iq;
224 }
225
226 public IqPacket generateSetBlockRequest(final Jid jid) {
227 final IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
228 final Element block = iq.addChild("block", Xmlns.BLOCKING);
229 block.addChild("item").setAttribute("jid", jid.toBareJid().toString());
230 return iq;
231 }
232
233 public IqPacket generateSetUnblockRequest(final Jid jid) {
234 final IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
235 final Element block = iq.addChild("unblock", Xmlns.BLOCKING);
236 block.addChild("item").setAttribute("jid", jid.toBareJid().toString());
237 return iq;
238 }
239
240 public IqPacket generateSetPassword(final Account account, final String newPassword) {
241 final IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
242 packet.setTo(account.getServer());
243 final Element query = packet.addChild("query", Xmlns.REGISTER);
244 final Jid jid = account.getJid();
245 query.addChild("username").setContent(jid.getLocalpart());
246 query.addChild("password").setContent(newPassword);
247 return packet;
248 }
249
250 public IqPacket changeAffiliation(Conversation conference, Jid jid, String affiliation) {
251 List<Jid> jids = new ArrayList<>();
252 jids.add(jid);
253 return changeAffiliation(conference,jids,affiliation);
254 }
255
256 public IqPacket changeAffiliation(Conversation conference, List<Jid> jids, String affiliation) {
257 IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
258 packet.setTo(conference.getJid().toBareJid());
259 packet.setFrom(conference.getAccount().getJid());
260 Element query = packet.query("http://jabber.org/protocol/muc#admin");
261 for(Jid jid : jids) {
262 Element item = query.addChild("item");
263 item.setAttribute("jid", jid.toString());
264 item.setAttribute("affiliation", affiliation);
265 }
266 return packet;
267 }
268
269 public IqPacket changeRole(Conversation conference, String nick, String role) {
270 IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
271 packet.setTo(conference.getJid().toBareJid());
272 packet.setFrom(conference.getAccount().getJid());
273 Element item = packet.query("http://jabber.org/protocol/muc#admin").addChild("item");
274 item.setAttribute("nick", nick);
275 item.setAttribute("role", role);
276 return packet;
277 }
278
279 public IqPacket requestHttpUploadSlot(Jid host, DownloadableFile file, String mime) {
280 IqPacket packet = new IqPacket(IqPacket.TYPE.GET);
281 packet.setTo(host);
282 Element request = packet.addChild("request",Xmlns.HTTP_UPLOAD);
283 request.addChild("filename").setContent(file.getName());
284 request.addChild("size").setContent(String.valueOf(file.getExpectedSize()));
285 if (mime != null) {
286 request.addChild("content-type").setContent(mime);
287 }
288 return packet;
289 }
290
291 public IqPacket generateCreateAccountWithCaptcha(Account account, String id, Data data) {
292 final IqPacket register = new IqPacket(IqPacket.TYPE.SET);
293
294 register.setTo(account.getServer());
295 register.setId(id);
296 register.query("jabber:iq:register").addChild(data);
297
298 return register;
299 }
300}