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