1package eu.siacs.conversations.generator;
2
3import java.net.URL;
4import java.text.SimpleDateFormat;
5import java.util.ArrayList;
6import java.util.Date;
7import java.util.Locale;
8import java.util.TimeZone;
9
10import eu.siacs.conversations.Config;
11import eu.siacs.conversations.crypto.axolotl.AxolotlService;
12import eu.siacs.conversations.crypto.axolotl.XmppAxolotlMessage;
13import eu.siacs.conversations.entities.Account;
14import eu.siacs.conversations.entities.Conversation;
15import eu.siacs.conversations.entities.Conversational;
16import eu.siacs.conversations.entities.Message;
17import eu.siacs.conversations.http.P1S3UrlStreamHandler;
18import eu.siacs.conversations.services.XmppConnectionService;
19import eu.siacs.conversations.xml.Element;
20import eu.siacs.conversations.xml.Namespace;
21import eu.siacs.conversations.xmpp.Jid;
22import eu.siacs.conversations.xmpp.chatstate.ChatState;
23import eu.siacs.conversations.xmpp.jingle.JingleConnectionManager;
24import eu.siacs.conversations.xmpp.jingle.JingleRtpConnection;
25import eu.siacs.conversations.xmpp.jingle.Media;
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) {
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 return packet;
68 }
69
70 public void addDelay(MessagePacket packet, long timestamp) {
71 final SimpleDateFormat mDateFormat = new SimpleDateFormat(
72 "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
73 mDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
74 Element delay = packet.addChild("delay", "urn:xmpp:delay");
75 Date date = new Date(timestamp);
76 delay.setAttribute("stamp", mDateFormat.format(date));
77 }
78
79 public MessagePacket generateAxolotlChat(Message message, XmppAxolotlMessage axolotlMessage) {
80 MessagePacket packet = preparePacket(message);
81 if (axolotlMessage == null) {
82 return null;
83 }
84 packet.setAxolotlMessage(axolotlMessage.toElement());
85 packet.setBody(OMEMO_FALLBACK_MESSAGE);
86 packet.addChild("store", "urn:xmpp:hints");
87 packet.addChild("encryption", "urn:xmpp:eme:0")
88 .setAttribute("name", "OMEMO")
89 .setAttribute("namespace", AxolotlService.PEP_PREFIX);
90 return packet;
91 }
92
93 public MessagePacket generateKeyTransportMessage(Jid to, XmppAxolotlMessage axolotlMessage) {
94 MessagePacket packet = new MessagePacket();
95 packet.setType(MessagePacket.TYPE_CHAT);
96 packet.setTo(to);
97 packet.setAxolotlMessage(axolotlMessage.toElement());
98 packet.addChild("store", "urn:xmpp:hints");
99 return packet;
100 }
101
102 public MessagePacket generateChat(Message message) {
103 MessagePacket packet = preparePacket(message);
104 String content;
105 if (message.hasFileOnRemoteHost()) {
106 Message.FileParams fileParams = message.getFileParams();
107 final URL url = fileParams.url;
108 if (P1S3UrlStreamHandler.PROTOCOL_NAME.equals(url.getProtocol())) {
109 Element x = packet.addChild("x", Namespace.P1_S3_FILE_TRANSFER);
110 final String file = url.getFile();
111 x.setAttribute("name", file.charAt(0) == '/' ? file.substring(1) : file);
112 x.setAttribute("fileid", url.getHost());
113 return packet;
114 } else {
115 content = url.toString();
116 packet.addChild("x", Namespace.OOB).addChild("url").setContent(content);
117 }
118 } else {
119 content = message.getBody();
120 }
121 packet.setBody(content);
122 return packet;
123 }
124
125 public MessagePacket generatePgpChat(Message message) {
126 MessagePacket packet = preparePacket(message);
127 if (message.hasFileOnRemoteHost()) {
128 Message.FileParams fileParams = message.getFileParams();
129 final URL url = fileParams.url;
130 if (P1S3UrlStreamHandler.PROTOCOL_NAME.equals(url.getProtocol())) {
131 Element x = packet.addChild("x", Namespace.P1_S3_FILE_TRANSFER);
132 final String file = url.getFile();
133 x.setAttribute("name", file.charAt(0) == '/' ? file.substring(1) : file);
134 x.setAttribute("fileid", url.getHost());
135 } else {
136 packet.setBody(url.toString());
137 packet.addChild("x", Namespace.OOB).addChild("url").setContent(url.toString());
138 }
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 directInvite(final Conversation conversation, final Jid contact) {
198 MessagePacket packet = new MessagePacket();
199 packet.setType(MessagePacket.TYPE_NORMAL);
200 packet.setTo(contact);
201 packet.setFrom(conversation.getAccount().getJid());
202 Element x = packet.addChild("x", "jabber:x:conference");
203 x.setAttribute("jid", conversation.getJid().asBareJid());
204 String password = conversation.getMucOptions().getPassword();
205 if (password != null) {
206 x.setAttribute("password", password);
207 }
208 if (contact.isFullJid()) {
209 packet.addChild("no-store", "urn:xmpp:hints");
210 packet.addChild("no-copy", "urn:xmpp:hints");
211 }
212 return packet;
213 }
214
215 public MessagePacket invite(Conversation conversation, Jid contact) {
216 MessagePacket packet = new MessagePacket();
217 packet.setTo(conversation.getJid().asBareJid());
218 packet.setFrom(conversation.getAccount().getJid());
219 Element x = new Element("x");
220 x.setAttribute("xmlns", "http://jabber.org/protocol/muc#user");
221 Element invite = new Element("invite");
222 invite.setAttribute("to", contact.asBareJid());
223 x.addChild(invite);
224 packet.addChild(x);
225 return packet;
226 }
227
228 public MessagePacket received(Account account, final Jid from, final String id, ArrayList<String> namespaces, int type) {
229 final MessagePacket receivedPacket = new MessagePacket();
230 receivedPacket.setType(type);
231 receivedPacket.setTo(from);
232 receivedPacket.setFrom(account.getJid());
233 for (final String namespace : namespaces) {
234 receivedPacket.addChild("received", namespace).setAttribute("id", id);
235 }
236 receivedPacket.addChild("store", "urn:xmpp:hints");
237 return receivedPacket;
238 }
239
240 public MessagePacket received(Account account, Jid to, String id) {
241 MessagePacket packet = new MessagePacket();
242 packet.setFrom(account.getJid());
243 packet.setTo(to);
244 packet.addChild("received", "urn:xmpp:receipts").setAttribute("id", id);
245 packet.addChild("store", "urn:xmpp:hints");
246 return packet;
247 }
248
249 public MessagePacket sessionProposal(final JingleConnectionManager.RtpSessionProposal proposal) {
250 final MessagePacket packet = new MessagePacket();
251 packet.setType(MessagePacket.TYPE_CHAT); //we want to carbon copy those
252 packet.setTo(proposal.with);
253 packet.setId(JingleRtpConnection.JINGLE_MESSAGE_PROPOSE_ID_PREFIX + proposal.sessionId);
254 final Element propose = packet.addChild("propose", Namespace.JINGLE_MESSAGE);
255 propose.setAttribute("id", proposal.sessionId);
256 for (final Media media : proposal.media) {
257 propose.addChild("description", Namespace.JINGLE_APPS_RTP).setAttribute("media", media.toString());
258 }
259
260 packet.addChild("request", "urn:xmpp:receipts");
261 packet.addChild("store", "urn:xmpp:hints");
262 return packet;
263 }
264
265 public MessagePacket sessionRetract(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 final Element propose = packet.addChild("retract", Namespace.JINGLE_MESSAGE);
270 propose.setAttribute("id", proposal.sessionId);
271 propose.addChild("description", Namespace.JINGLE_APPS_RTP);
272 packet.addChild("store", "urn:xmpp:hints");
273 return packet;
274 }
275
276 public MessagePacket sessionReject(final Jid with, final String sessionId) {
277 final MessagePacket packet = new MessagePacket();
278 packet.setType(MessagePacket.TYPE_CHAT); //we want to carbon copy those
279 packet.setTo(with);
280 final Element propose = packet.addChild("reject", Namespace.JINGLE_MESSAGE);
281 propose.setAttribute("id", sessionId);
282 propose.addChild("description", Namespace.JINGLE_APPS_RTP);
283 packet.addChild("store", "urn:xmpp:hints");
284 return packet;
285 }
286}