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