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