MessageGenerator.java

  1package eu.siacs.conversations.generator;
  2
  3import java.net.URL;
  4import java.text.SimpleDateFormat;
  5import java.util.ArrayList;
  6import java.util.Date;
  7import java.util.Locale;
  8import java.util.TimeZone;
  9
 10import eu.siacs.conversations.Config;
 11import eu.siacs.conversations.crypto.axolotl.AxolotlService;
 12import eu.siacs.conversations.crypto.axolotl.XmppAxolotlMessage;
 13import eu.siacs.conversations.entities.Account;
 14import eu.siacs.conversations.entities.Conversation;
 15import eu.siacs.conversations.entities.Conversational;
 16import eu.siacs.conversations.entities.Message;
 17import eu.siacs.conversations.http.P1S3UrlStreamHandler;
 18import eu.siacs.conversations.services.XmppConnectionService;
 19import eu.siacs.conversations.xml.Element;
 20import eu.siacs.conversations.xml.Namespace;
 21import eu.siacs.conversations.xmpp.chatstate.ChatState;
 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;
 26import rocks.xmpp.addr.Jid;
 27
 28public class MessageGenerator extends AbstractGenerator {
 29    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";
 30    private static final String PGP_FALLBACK_MESSAGE = "I sent you a PGP encrypted message but your client doesn’t seem to support that.";
 31
 32    public MessageGenerator(XmppConnectionService service) {
 33        super(service);
 34    }
 35
 36    private MessagePacket preparePacket(Message message) {
 37        Conversation conversation = (Conversation) message.getConversation();
 38        Account account = conversation.getAccount();
 39        MessagePacket packet = new MessagePacket();
 40        final boolean isWithSelf = conversation.getContact().isSelf();
 41        if (conversation.getMode() == Conversation.MODE_SINGLE) {
 42            packet.setTo(message.getCounterpart());
 43            packet.setType(MessagePacket.TYPE_CHAT);
 44            if (!isWithSelf) {
 45                packet.addChild("request", "urn:xmpp:receipts");
 46            }
 47        } else if (message.isPrivateMessage()) {
 48            packet.setTo(message.getCounterpart());
 49            packet.setType(MessagePacket.TYPE_CHAT);
 50            packet.addChild("x", "http://jabber.org/protocol/muc#user");
 51            packet.addChild("request", "urn:xmpp:receipts");
 52        } else {
 53            packet.setTo(message.getCounterpart().asBareJid());
 54            packet.setType(MessagePacket.TYPE_GROUPCHAT);
 55        }
 56        if (conversation.isSingleOrPrivateAndNonAnonymous() && !message.isPrivateMessage()) {
 57            packet.addChild("markable", "urn:xmpp:chat-markers:0");
 58        }
 59        packet.setFrom(account.getJid());
 60        packet.setId(message.getUuid());
 61        packet.addChild("origin-id", Namespace.STANZA_IDS).setAttribute("id", message.getUuid());
 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            Message.FileParams fileParams = message.getFileParams();
105            final URL url = fileParams.url;
106            if (P1S3UrlStreamHandler.PROTOCOL_NAME.equals(url.getProtocol())) {
107                Element x = packet.addChild("x", Namespace.P1_S3_FILE_TRANSFER);
108                final String file = url.getFile();
109                x.setAttribute("name", file.charAt(0) == '/' ? file.substring(1) : file);
110                x.setAttribute("fileid", url.getHost());
111                return packet;
112            } else {
113                content = url.toString();
114                packet.addChild("x", Namespace.OOB).addChild("url").setContent(content);
115            }
116        } else {
117            content = message.getBody();
118        }
119        packet.setBody(content);
120        return packet;
121    }
122
123    public MessagePacket generatePgpChat(Message message) {
124        MessagePacket packet = preparePacket(message);
125        if (message.hasFileOnRemoteHost()) {
126            Message.FileParams fileParams = message.getFileParams();
127            final URL url = fileParams.url;
128            if (P1S3UrlStreamHandler.PROTOCOL_NAME.equals(url.getProtocol())) {
129                Element x = packet.addChild("x", Namespace.P1_S3_FILE_TRANSFER);
130                final String file = url.getFile();
131                x.setAttribute("name", file.charAt(0) == '/' ? file.substring(1) : file);
132                x.setAttribute("fileid", url.getHost());
133            } else {
134                packet.setBody(url.toString());
135                packet.addChild("x", Namespace.OOB).addChild("url").setContent(url.toString());
136            }
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 directInvite(final Conversation conversation, final Jid contact) {
196        MessagePacket packet = new MessagePacket();
197        packet.setType(MessagePacket.TYPE_NORMAL);
198        packet.setTo(contact);
199        packet.setFrom(conversation.getAccount().getJid());
200        Element x = packet.addChild("x", "jabber:x:conference");
201        x.setAttribute("jid", conversation.getJid().asBareJid().toString());
202        String password = conversation.getMucOptions().getPassword();
203        if (password != null) {
204            x.setAttribute("password", password);
205        }
206        if (contact.isFullJid()) {
207            packet.addChild("no-store", "urn:xmpp:hints");
208            packet.addChild("no-copy", "urn:xmpp:hints");
209        }
210        return packet;
211    }
212
213    public MessagePacket invite(Conversation conversation, Jid contact) {
214        MessagePacket packet = new MessagePacket();
215        packet.setTo(conversation.getJid().asBareJid());
216        packet.setFrom(conversation.getAccount().getJid());
217        Element x = new Element("x");
218        x.setAttribute("xmlns", "http://jabber.org/protocol/muc#user");
219        Element invite = new Element("invite");
220        invite.setAttribute("to", contact.asBareJid().toString());
221        x.addChild(invite);
222        packet.addChild(x);
223        return packet;
224    }
225
226    public MessagePacket received(Account account, MessagePacket originalMessage, ArrayList<String> namespaces, int type) {
227        MessagePacket receivedPacket = new MessagePacket();
228        receivedPacket.setType(type);
229        receivedPacket.setTo(originalMessage.getFrom());
230        receivedPacket.setFrom(account.getJid());
231        for (String namespace : namespaces) {
232            receivedPacket.addChild("received", namespace).setAttribute("id", originalMessage.getId());
233        }
234        receivedPacket.addChild("store", "urn:xmpp:hints");
235        return receivedPacket;
236    }
237
238    public MessagePacket received(Account account, Jid to, String id) {
239        MessagePacket packet = new MessagePacket();
240        packet.setFrom(account.getJid());
241        packet.setTo(to);
242        packet.addChild("received", "urn:xmpp:receipts").setAttribute("id", id);
243        packet.addChild("store", "urn:xmpp:hints");
244        return packet;
245    }
246
247    public MessagePacket sessionProposal(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        packet.setId(JingleRtpConnection.JINGLE_MESSAGE_PROPOSE_ID_PREFIX + proposal.sessionId);
252        final Element propose = packet.addChild("propose", Namespace.JINGLE_MESSAGE);
253        propose.setAttribute("id", proposal.sessionId);
254        for (final Media media : proposal.media) {
255            propose.addChild("description", Namespace.JINGLE_APPS_RTP).setAttribute("media", media.toString());
256        }
257
258        packet.addChild("request", "urn:xmpp:receipts");
259        packet.addChild("store", "urn:xmpp:hints");
260        return packet;
261    }
262
263    public MessagePacket sessionRetract(final JingleConnectionManager.RtpSessionProposal proposal) {
264        final MessagePacket packet = new MessagePacket();
265        packet.setType(MessagePacket.TYPE_CHAT); //we want to carbon copy those
266        packet.setTo(proposal.with);
267        final Element propose = packet.addChild("retract", Namespace.JINGLE_MESSAGE);
268        propose.setAttribute("id", proposal.sessionId);
269        propose.addChild("description", Namespace.JINGLE_APPS_RTP);
270        packet.addChild("store", "urn:xmpp:hints");
271        return packet;
272    }
273
274    public MessagePacket sessionReject(final Jid with, final String sessionId) {
275        final MessagePacket packet = new MessagePacket();
276        packet.setType(MessagePacket.TYPE_CHAT); //we want to carbon copy those
277        packet.setTo(with);
278        final Element propose = packet.addChild("reject", Namespace.JINGLE_MESSAGE);
279        propose.setAttribute("id", sessionId);
280        propose.addChild("description", Namespace.JINGLE_APPS_RTP);
281        packet.addChild("store", "urn:xmpp:hints");
282        return packet;
283    }
284}