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