MessageGenerator.java

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