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 String content;
106 if (message.hasFileOnRemoteHost()) {
107 final Message.FileParams fileParams = message.getFileParams();
108 content = fileParams.url;
109 packet.addChild("x", Namespace.OOB).addChild("url").setContent(content);
110 packet.addChild("fallback", "urn:xmpp:fallback:0").setAttribute("for", Namespace.OOB)
111 .addChild("body", "urn:xmpp:fallback:0");
112 message.setBody(content);
113 } else {
114 content = message.getBody();
115 }
116 packet.setBody(content);
117 return packet;
118 }
119
120 public MessagePacket generatePgpChat(Message message) {
121 MessagePacket packet = preparePacket(message);
122 if (message.hasFileOnRemoteHost()) {
123 Message.FileParams fileParams = message.getFileParams();
124 final String url = fileParams.url;
125 packet.setBody(url);
126 packet.addChild("x", Namespace.OOB).addChild("url").setContent(url);
127 packet.addChild("fallback", "urn:xmpp:fallback:0").setAttribute("for", Namespace.OOB)
128 .addChild("body", "urn:xmpp:fallback:0");
129 } else {
130 if (Config.supportUnencrypted()) {
131 packet.setBody(PGP_FALLBACK_MESSAGE);
132 }
133 if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
134 packet.addChild("x", "jabber:x:encrypted").setContent(message.getEncryptedBody());
135 } else if (message.getEncryption() == Message.ENCRYPTION_PGP) {
136 packet.addChild("x", "jabber:x:encrypted").setContent(message.getBody());
137 }
138 packet.addChild("encryption", "urn:xmpp:eme:0")
139 .setAttribute("namespace", "jabber:x:encrypted");
140 }
141 return packet;
142 }
143
144 public MessagePacket generateChatState(Conversation conversation) {
145 final Account account = conversation.getAccount();
146 MessagePacket packet = new MessagePacket();
147 packet.setType(conversation.getMode() == Conversation.MODE_MULTI ? MessagePacket.TYPE_GROUPCHAT : MessagePacket.TYPE_CHAT);
148 packet.setTo(conversation.getJid().asBareJid());
149 packet.setFrom(account.getJid());
150 packet.addChild(ChatState.toElement(conversation.getOutgoingChatState()));
151 packet.addChild("no-store", "urn:xmpp:hints");
152 packet.addChild("no-storage", "urn:xmpp:hints"); //wrong! don't copy this. Its *store*
153 return packet;
154 }
155
156 public MessagePacket confirm(final Message message) {
157 final boolean groupChat = message.getConversation().getMode() == Conversational.MODE_MULTI;
158 final Jid to = message.getCounterpart();
159 final MessagePacket packet = new MessagePacket();
160 packet.setType(groupChat ? MessagePacket.TYPE_GROUPCHAT : MessagePacket.TYPE_CHAT);
161 packet.setTo(groupChat ? to.asBareJid() : to);
162 final Element displayed = packet.addChild("displayed", "urn:xmpp:chat-markers:0");
163 if (groupChat) {
164 final String stanzaId = message.getServerMsgId();
165 if (stanzaId != null) {
166 displayed.setAttribute("id", stanzaId);
167 } else {
168 displayed.setAttribute("sender", to.toString());
169 displayed.setAttribute("id", message.getRemoteMsgId());
170 }
171 } else {
172 displayed.setAttribute("id", message.getRemoteMsgId());
173 }
174 packet.addChild("store", "urn:xmpp:hints");
175 return packet;
176 }
177
178 public MessagePacket conferenceSubject(Conversation conversation, String subject) {
179 MessagePacket packet = new MessagePacket();
180 packet.setType(MessagePacket.TYPE_GROUPCHAT);
181 packet.setTo(conversation.getJid().asBareJid());
182 packet.addChild("subject").setContent(subject);
183 packet.setFrom(conversation.getAccount().getJid().asBareJid());
184 return packet;
185 }
186
187 public MessagePacket directInvite(final Conversation conversation, final Jid contact) {
188 MessagePacket packet = new MessagePacket();
189 packet.setType(MessagePacket.TYPE_NORMAL);
190 packet.setTo(contact);
191 packet.setFrom(conversation.getAccount().getJid());
192 Element x = packet.addChild("x", "jabber:x:conference");
193 x.setAttribute("jid", conversation.getJid().asBareJid());
194 String password = conversation.getMucOptions().getPassword();
195 if (password != null) {
196 x.setAttribute("password", password);
197 }
198 if (contact.isFullJid()) {
199 packet.addChild("no-store", "urn:xmpp:hints");
200 packet.addChild("no-copy", "urn:xmpp:hints");
201 }
202 return packet;
203 }
204
205 public MessagePacket invite(Conversation conversation, Jid contact) {
206 MessagePacket packet = new MessagePacket();
207 packet.setTo(conversation.getJid().asBareJid());
208 packet.setFrom(conversation.getAccount().getJid());
209 Element x = new Element("x");
210 x.setAttribute("xmlns", "http://jabber.org/protocol/muc#user");
211 Element invite = new Element("invite");
212 invite.setAttribute("to", contact.asBareJid());
213 x.addChild(invite);
214 packet.addChild(x);
215 return packet;
216 }
217
218 public MessagePacket received(Account account, final Jid from, final String id, ArrayList<String> namespaces, int type) {
219 final MessagePacket receivedPacket = new MessagePacket();
220 receivedPacket.setType(type);
221 receivedPacket.setTo(from);
222 receivedPacket.setFrom(account.getJid());
223 for (final String namespace : namespaces) {
224 receivedPacket.addChild("received", namespace).setAttribute("id", id);
225 }
226 receivedPacket.addChild("store", "urn:xmpp:hints");
227 return receivedPacket;
228 }
229
230 public MessagePacket received(Account account, Jid to, String id) {
231 MessagePacket packet = new MessagePacket();
232 packet.setFrom(account.getJid());
233 packet.setTo(to);
234 packet.addChild("received", "urn:xmpp:receipts").setAttribute("id", id);
235 packet.addChild("store", "urn:xmpp:hints");
236 return packet;
237 }
238
239 public MessagePacket sessionProposal(final JingleConnectionManager.RtpSessionProposal proposal) {
240 final MessagePacket packet = new MessagePacket();
241 packet.setType(MessagePacket.TYPE_CHAT); //we want to carbon copy those
242 packet.setTo(proposal.with);
243 packet.setId(JingleRtpConnection.JINGLE_MESSAGE_PROPOSE_ID_PREFIX + proposal.sessionId);
244 final Element propose = packet.addChild("propose", Namespace.JINGLE_MESSAGE);
245 propose.setAttribute("id", proposal.sessionId);
246 for (final Media media : proposal.media) {
247 propose.addChild("description", Namespace.JINGLE_APPS_RTP).setAttribute("media", media.toString());
248 }
249
250 packet.addChild("request", "urn:xmpp:receipts");
251 packet.addChild("store", "urn:xmpp:hints");
252 return packet;
253 }
254
255 public MessagePacket sessionRetract(final JingleConnectionManager.RtpSessionProposal proposal) {
256 final MessagePacket packet = new MessagePacket();
257 packet.setType(MessagePacket.TYPE_CHAT); //we want to carbon copy those
258 packet.setTo(proposal.with);
259 final Element propose = packet.addChild("retract", Namespace.JINGLE_MESSAGE);
260 propose.setAttribute("id", proposal.sessionId);
261 propose.addChild("description", Namespace.JINGLE_APPS_RTP);
262 packet.addChild("store", "urn:xmpp:hints");
263 return packet;
264 }
265
266 public MessagePacket sessionReject(final Jid with, final String sessionId) {
267 final MessagePacket packet = new MessagePacket();
268 packet.setType(MessagePacket.TYPE_CHAT); //we want to carbon copy those
269 packet.setTo(with);
270 final Element propose = packet.addChild("reject", Namespace.JINGLE_MESSAGE);
271 propose.setAttribute("id", sessionId);
272 propose.addChild("description", Namespace.JINGLE_APPS_RTP);
273 packet.addChild("store", "urn:xmpp:hints");
274 return packet;
275 }
276}