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