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