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