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