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