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