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