1package eu.siacs.conversations.generator;
2
3import java.text.SimpleDateFormat;
4import java.util.ArrayList;
5import java.util.Date;
6import java.util.Locale;
7import java.util.TimeZone;
8
9import eu.siacs.conversations.Config;
10import eu.siacs.conversations.crypto.axolotl.AxolotlService;
11import eu.siacs.conversations.crypto.axolotl.XmppAxolotlMessage;
12import eu.siacs.conversations.entities.Account;
13import eu.siacs.conversations.entities.Conversation;
14import eu.siacs.conversations.entities.Conversational;
15import eu.siacs.conversations.entities.Message;
16import eu.siacs.conversations.services.XmppConnectionService;
17import eu.siacs.conversations.xml.Element;
18import eu.siacs.conversations.xml.Namespace;
19import eu.siacs.conversations.xmpp.Jid;
20import eu.siacs.conversations.xmpp.chatstate.ChatState;
21import eu.siacs.conversations.xmpp.forms.Data;
22import eu.siacs.conversations.xmpp.jingle.JingleConnectionManager;
23import eu.siacs.conversations.xmpp.jingle.JingleRtpConnection;
24import eu.siacs.conversations.xmpp.jingle.Media;
25import eu.siacs.conversations.xmpp.jingle.stanzas.Reason;
26import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
27
28public class MessageGenerator extends AbstractGenerator {
29 private static final String OMEMO_FALLBACK_MESSAGE = "I sent you an OMEMO encrypted message but your client doesn’t seem to support that. Find more information on https://conversations.im/omemo";
30 private static final String PGP_FALLBACK_MESSAGE = "I sent you a PGP encrypted message but your client doesn’t seem to support that.";
31
32 public MessageGenerator(XmppConnectionService service) {
33 super(service);
34 }
35
36 private MessagePacket preparePacket(Message message, boolean legacyEncryption) {
37 Conversation conversation = (Conversation) message.getConversation();
38 Account account = conversation.getAccount();
39 MessagePacket packet = new MessagePacket();
40 final boolean isWithSelf = conversation.getContact().isSelf();
41 if (conversation.getMode() == Conversation.MODE_SINGLE) {
42 packet.setTo(message.getCounterpart());
43 packet.setType(MessagePacket.TYPE_CHAT);
44 if (!isWithSelf) {
45 packet.addChild("request", "urn:xmpp:receipts");
46 }
47 } else if (message.isPrivateMessage()) {
48 packet.setTo(message.getCounterpart());
49 packet.setType(MessagePacket.TYPE_CHAT);
50 packet.addChild("x", "http://jabber.org/protocol/muc#user");
51 packet.addChild("request", "urn:xmpp:receipts");
52 } else {
53 packet.setTo(message.getCounterpart().asBareJid());
54 packet.setType(MessagePacket.TYPE_GROUPCHAT);
55 }
56 if (conversation.isSingleOrPrivateAndNonAnonymous() && !message.isPrivateMessage()) {
57 packet.addChild("markable", "urn:xmpp:chat-markers:0");
58 }
59 packet.setFrom(account.getJid());
60 packet.setId(message.getUuid());
61 if (conversation.getMode() == Conversational.MODE_SINGLE || message.isPrivateMessage() || !conversation.getMucOptions().stableId()) {
62 packet.addChild("origin-id", Namespace.STANZA_IDS).setAttribute("id", message.getUuid());
63 }
64 if (message.edited()) {
65 packet.addChild("replace", "urn:xmpp:message-correct:0").setAttribute("id", message.getEditedIdWireFormat());
66 }
67 if (!legacyEncryption) {
68 if (message.getSubject() != null && message.getSubject().length() > 0) packet.addChild("subject").setContent(message.getSubject());
69 // Legacy encryption can't handle advanced payloads
70 for (Element el : message.getPayloads()) {
71 packet.addChild(el);
72 }
73 } else {
74 for (Element el : message.getPayloads()) {
75 if ("thread".equals(el.getName())) packet.addChild(el);
76 }
77 }
78 return packet;
79 }
80
81 public void addDelay(MessagePacket packet, long timestamp) {
82 final SimpleDateFormat mDateFormat = new SimpleDateFormat(
83 "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
84 mDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
85 Element delay = packet.addChild("delay", "urn:xmpp:delay");
86 Date date = new Date(timestamp);
87 delay.setAttribute("stamp", mDateFormat.format(date));
88 }
89
90 public MessagePacket generateAxolotlChat(Message message, XmppAxolotlMessage axolotlMessage) {
91 MessagePacket packet = preparePacket(message, true);
92 if (axolotlMessage == null) {
93 return null;
94 }
95 packet.setAxolotlMessage(axolotlMessage.toElement());
96 packet.setBody(OMEMO_FALLBACK_MESSAGE);
97 packet.addChild("store", "urn:xmpp:hints");
98 packet.addChild("encryption", "urn:xmpp:eme:0")
99 .setAttribute("name", "OMEMO")
100 .setAttribute("namespace", AxolotlService.PEP_PREFIX);
101 return packet;
102 }
103
104 public MessagePacket generateKeyTransportMessage(Jid to, XmppAxolotlMessage axolotlMessage) {
105 MessagePacket packet = new MessagePacket();
106 packet.setType(MessagePacket.TYPE_CHAT);
107 packet.setTo(to);
108 packet.setAxolotlMessage(axolotlMessage.toElement());
109 packet.addChild("store", "urn:xmpp:hints");
110 return packet;
111 }
112
113 public MessagePacket generateChat(Message message) {
114 MessagePacket packet = preparePacket(message, false);
115 if (message.hasFileOnRemoteHost()) {
116 final Message.FileParams fileParams = message.getFileParams();
117
118 if (message.getFallbacks(Namespace.OOB).isEmpty()) {
119 if (message.getBody().equals("")) {
120 message.setBody(fileParams.url);
121 packet.addChild("fallback", "urn:xmpp:fallback:0").setAttribute("for", Namespace.OOB)
122 .addChild("body", "urn:xmpp:fallback:0");
123 } else {
124 long start = message.getRawBody().codePointCount(0, message.getRawBody().length());
125 message.appendBody(fileParams.url);
126 packet.addChild("fallback", "urn:xmpp:fallback:0").setAttribute("for", Namespace.OOB)
127 .addChild("body", "urn:xmpp:fallback:0")
128 .setAttribute("start", String.valueOf(start))
129 .setAttribute("end", String.valueOf(start + fileParams.url.length()));
130 }
131 }
132
133 packet.addChild("x", Namespace.OOB).addChild("url").setContent(fileParams.url);
134 }
135 if (message.getRawBody() != null) packet.setBody(message.getRawBody());
136 return packet;
137 }
138
139 public MessagePacket generatePgpChat(Message message) {
140 MessagePacket packet = preparePacket(message, true);
141 if (message.hasFileOnRemoteHost()) {
142 Message.FileParams fileParams = message.getFileParams();
143 final String url = fileParams.url;
144 packet.setBody(url);
145 packet.addChild("x", Namespace.OOB).addChild("url").setContent(url);
146 packet.addChild("fallback", "urn:xmpp:fallback:0").setAttribute("for", Namespace.OOB)
147 .addChild("body", "urn:xmpp:fallback:0");
148 } else {
149 if (Config.supportUnencrypted()) {
150 packet.setBody(PGP_FALLBACK_MESSAGE);
151 }
152 if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
153 packet.addChild("x", "jabber:x:encrypted").setContent(message.getEncryptedBody());
154 } else if (message.getEncryption() == Message.ENCRYPTION_PGP) {
155 packet.addChild("x", "jabber:x:encrypted").setContent(message.getBody());
156 }
157 packet.addChild("encryption", "urn:xmpp:eme:0")
158 .setAttribute("namespace", "jabber:x:encrypted");
159 }
160 return packet;
161 }
162
163 public MessagePacket generateChatState(Conversation conversation) {
164 final Account account = conversation.getAccount();
165 MessagePacket packet = new MessagePacket();
166 packet.setType(conversation.getMode() == Conversation.MODE_MULTI ? MessagePacket.TYPE_GROUPCHAT : MessagePacket.TYPE_CHAT);
167 packet.setTo(conversation.getJid().asBareJid());
168 packet.setFrom(account.getJid());
169 packet.addChild(ChatState.toElement(conversation.getOutgoingChatState()));
170 packet.addChild("no-store", "urn:xmpp:hints");
171 packet.addChild("no-storage", "urn:xmpp:hints"); //wrong! don't copy this. Its *store*
172 return packet;
173 }
174
175 public MessagePacket confirm(final Message message) {
176 final boolean groupChat = message.getConversation().getMode() == Conversational.MODE_MULTI;
177 final Jid to = message.getCounterpart();
178 final MessagePacket packet = new MessagePacket();
179 packet.setType(groupChat ? MessagePacket.TYPE_GROUPCHAT : MessagePacket.TYPE_CHAT);
180 packet.setTo(groupChat ? to.asBareJid() : to);
181 final Element displayed = packet.addChild("displayed", "urn:xmpp:chat-markers:0");
182 if (groupChat) {
183 final String stanzaId = message.getServerMsgId();
184 if (stanzaId != null) {
185 displayed.setAttribute("id", stanzaId);
186 } else {
187 displayed.setAttribute("sender", to.toString());
188 displayed.setAttribute("id", message.getRemoteMsgId());
189 }
190 } else {
191 displayed.setAttribute("id", message.getRemoteMsgId());
192 }
193 packet.addChild("store", "urn:xmpp:hints");
194 return packet;
195 }
196
197 public MessagePacket conferenceSubject(Conversation conversation, String subject) {
198 MessagePacket packet = new MessagePacket();
199 packet.setType(MessagePacket.TYPE_GROUPCHAT);
200 packet.setTo(conversation.getJid().asBareJid());
201 packet.addChild("subject").setContent(subject);
202 packet.setFrom(conversation.getAccount().getJid().asBareJid());
203 return packet;
204 }
205
206 public MessagePacket requestVoice(Jid jid) {
207 MessagePacket packet = new MessagePacket();
208 packet.setType(MessagePacket.TYPE_NORMAL);
209 packet.setTo(jid.asBareJid());
210 Data form = new Data();
211 form.setFormType("http://jabber.org/protocol/muc#request");
212 form.put("muc#role", "participant");
213 form.submit();
214 packet.addChild(form);
215 return packet;
216 }
217
218 public MessagePacket directInvite(final Conversation conversation, final Jid contact) {
219 MessagePacket packet = new MessagePacket();
220 packet.setType(MessagePacket.TYPE_NORMAL);
221 packet.setTo(contact);
222 packet.setFrom(conversation.getAccount().getJid());
223 Element x = packet.addChild("x", "jabber:x:conference");
224 x.setAttribute("jid", conversation.getJid().asBareJid());
225 String password = conversation.getMucOptions().getPassword();
226 if (password != null) {
227 x.setAttribute("password", password);
228 }
229 if (contact.isFullJid()) {
230 packet.addChild("no-store", "urn:xmpp:hints");
231 packet.addChild("no-copy", "urn:xmpp:hints");
232 }
233 return packet;
234 }
235
236 public MessagePacket invite(final Conversation conversation, final Jid contact) {
237 final MessagePacket packet = new MessagePacket();
238 packet.setTo(conversation.getJid().asBareJid());
239 packet.setFrom(conversation.getAccount().getJid());
240 Element x = new Element("x");
241 x.setAttribute("xmlns", "http://jabber.org/protocol/muc#user");
242 Element invite = new Element("invite");
243 invite.setAttribute("to", contact.asBareJid());
244 x.addChild(invite);
245 packet.addChild(x);
246 return packet;
247 }
248
249 public MessagePacket received(Account account, final Jid from, final String id, ArrayList<String> namespaces, int type) {
250 final MessagePacket receivedPacket = new MessagePacket();
251 receivedPacket.setType(type);
252 receivedPacket.setTo(from);
253 receivedPacket.setFrom(account.getJid());
254 for (final String namespace : namespaces) {
255 receivedPacket.addChild("received", namespace).setAttribute("id", id);
256 }
257 receivedPacket.addChild("store", "urn:xmpp:hints");
258 return receivedPacket;
259 }
260
261 public MessagePacket received(Account account, Jid to, String id) {
262 MessagePacket packet = new MessagePacket();
263 packet.setFrom(account.getJid());
264 packet.setTo(to);
265 packet.addChild("received", "urn:xmpp:receipts").setAttribute("id", id);
266 packet.addChild("store", "urn:xmpp:hints");
267 return packet;
268 }
269
270 public MessagePacket sessionFinish(
271 final Jid with, final String sessionId, final Reason reason) {
272 final MessagePacket packet = new MessagePacket();
273 packet.setType(MessagePacket.TYPE_CHAT);
274 packet.setTo(with);
275 final Element finish = packet.addChild("finish", Namespace.JINGLE_MESSAGE);
276 finish.setAttribute("id", sessionId);
277 final Element reasonElement = finish.addChild("reason", Namespace.JINGLE);
278 reasonElement.addChild(reason.toString());
279 packet.addChild("store", "urn:xmpp:hints");
280 return packet;
281 }
282
283 public MessagePacket sessionProposal(final JingleConnectionManager.RtpSessionProposal proposal) {
284 final MessagePacket packet = new MessagePacket();
285 packet.setType(MessagePacket.TYPE_CHAT); //we want to carbon copy those
286 packet.setTo(proposal.with);
287 packet.setId(JingleRtpConnection.JINGLE_MESSAGE_PROPOSE_ID_PREFIX + proposal.sessionId);
288 final Element propose = packet.addChild("propose", Namespace.JINGLE_MESSAGE);
289 propose.setAttribute("id", proposal.sessionId);
290 for (final Media media : proposal.media) {
291 propose.addChild("description", Namespace.JINGLE_APPS_RTP).setAttribute("media", media.toString());
292 }
293 packet.addChild("request", "urn:xmpp:receipts");
294 packet.addChild("store", "urn:xmpp:hints");
295 return packet;
296 }
297
298 public MessagePacket sessionRetract(final JingleConnectionManager.RtpSessionProposal proposal) {
299 final MessagePacket packet = new MessagePacket();
300 packet.setType(MessagePacket.TYPE_CHAT); //we want to carbon copy those
301 packet.setTo(proposal.with);
302 final Element propose = packet.addChild("retract", Namespace.JINGLE_MESSAGE);
303 propose.setAttribute("id", proposal.sessionId);
304 propose.addChild("description", Namespace.JINGLE_APPS_RTP);
305 packet.addChild("store", "urn:xmpp:hints");
306 return packet;
307 }
308
309 public MessagePacket sessionReject(final Jid with, final String sessionId) {
310 final MessagePacket packet = new MessagePacket();
311 packet.setType(MessagePacket.TYPE_CHAT); //we want to carbon copy those
312 packet.setTo(with);
313 final Element propose = packet.addChild("reject", Namespace.JINGLE_MESSAGE);
314 propose.setAttribute("id", sessionId);
315 propose.addChild("description", Namespace.JINGLE_APPS_RTP);
316 packet.addChild("store", "urn:xmpp:hints");
317 return packet;
318 }
319}