1package eu.siacs.conversations.generator;
2
3import net.java.otr4j.OtrException;
4import net.java.otr4j.session.Session;
5
6import java.text.SimpleDateFormat;
7import java.util.ArrayList;
8import java.util.Date;
9import java.util.Locale;
10import java.util.TimeZone;
11
12import eu.siacs.conversations.Config;
13import eu.siacs.conversations.crypto.axolotl.AxolotlService;
14import eu.siacs.conversations.crypto.axolotl.XmppAxolotlMessage;
15import eu.siacs.conversations.entities.Account;
16import eu.siacs.conversations.entities.Contact;
17import eu.siacs.conversations.entities.Conversation;
18import eu.siacs.conversations.entities.Message;
19import eu.siacs.conversations.services.XmppConnectionService;
20import eu.siacs.conversations.xml.Element;
21import eu.siacs.conversations.xml.Namespace;
22import eu.siacs.conversations.xmpp.chatstate.ChatState;
23import eu.siacs.conversations.xmpp.jid.Jid;
24import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
25
26public class MessageGenerator extends AbstractGenerator {
27 public static final String OTR_FALLBACK_MESSAGE = "I would like to start a private (OTR encrypted) conversation but your client doesn’t seem to support that";
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 = message.getConversation();
37 Account account = conversation.getAccount();
38 MessagePacket packet = new MessagePacket();
39 if (conversation.getMode() == Conversation.MODE_SINGLE) {
40 packet.setTo(message.getCounterpart());
41 packet.setType(MessagePacket.TYPE_CHAT);
42 if (this.mXmppConnectionService.indicateReceived()) {
43 packet.addChild("request", "urn:xmpp:receipts");
44 }
45 } else if (message.getType() == Message.TYPE_PRIVATE) {
46 packet.setTo(message.getCounterpart());
47 packet.setType(MessagePacket.TYPE_CHAT);
48 packet.addChild("x","http://jabber.org/protocol/muc#user");
49 if (this.mXmppConnectionService.indicateReceived()) {
50 packet.addChild("request", "urn:xmpp:receipts");
51 }
52 } else {
53 packet.setTo(message.getCounterpart().toBareJid());
54 packet.setType(MessagePacket.TYPE_GROUPCHAT);
55 }
56 if (conversation.getMode() == Conversation.MODE_SINGLE ||
57 (conversation.getMucOptions().nonanonymous() && conversation.getMucOptions().membersOnly() && message.getType() != Message.TYPE_PRIVATE)) {
58 packet.addChild("markable", "urn:xmpp:chat-markers:0");
59 }
60 packet.setFrom(account.getJid());
61 packet.setId(message.getUuid());
62 packet.addChild("origin-id", Namespace.STANZA_IDS).setAttribute("id",message.getUuid());
63 if (message.edited()) {
64 packet.addChild("replace","urn:xmpp:message-correct:0").setAttribute("id",message.getEditedId());
65 }
66 return packet;
67 }
68
69 public void addDelay(MessagePacket packet, long timestamp) {
70 final SimpleDateFormat mDateFormat = new SimpleDateFormat(
71 "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
72 mDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
73 Element delay = packet.addChild("delay", "urn:xmpp:delay");
74 Date date = new Date(timestamp);
75 delay.setAttribute("stamp", mDateFormat.format(date));
76 }
77
78 public MessagePacket generateAxolotlChat(Message message, XmppAxolotlMessage axolotlMessage) {
79 MessagePacket packet = preparePacket(message);
80 if (axolotlMessage == null) {
81 return null;
82 }
83 packet.setAxolotlMessage(axolotlMessage.toElement());
84 if (Config.supportUnencrypted() && !recipientSupportsOmemo(message)) {
85 packet.setBody(OMEMO_FALLBACK_MESSAGE);
86 }
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 private static boolean recipientSupportsOmemo(Message message) {
95 Contact c = message.getContact();
96 return c != null && c.getPresences().allOrNonSupport(AxolotlService.PEP_DEVICE_LIST_NOTIFY);
97 }
98
99 public static void addMessageHints(MessagePacket packet) {
100 packet.addChild("private", "urn:xmpp:carbons:2");
101 packet.addChild("no-copy", "urn:xmpp:hints");
102 packet.addChild("no-permanent-store", "urn:xmpp:hints");
103 packet.addChild("no-permanent-storage", "urn:xmpp:hints"); //do not copy this. this is wrong. it is *store*
104 }
105
106 public MessagePacket generateOtrChat(Message message) {
107 Session otrSession = message.getConversation().getOtrSession();
108 if (otrSession == null) {
109 return null;
110 }
111 MessagePacket packet = preparePacket(message);
112 addMessageHints(packet);
113 try {
114 String content;
115 if (message.hasFileOnRemoteHost()) {
116 content = message.getFileParams().url.toString();
117 } else {
118 content = message.getBody();
119 }
120 packet.setBody(otrSession.transformSending(content)[0]);
121 packet.addChild("encryption","urn:xmpp:eme:0")
122 .setAttribute("namespace","urn:xmpp:otr:0");
123 return packet;
124 } catch (OtrException e) {
125 return null;
126 }
127 }
128
129 public MessagePacket generateChat(Message message) {
130 MessagePacket packet = preparePacket(message);
131 String content;
132 if (message.hasFileOnRemoteHost()) {
133 Message.FileParams fileParams = message.getFileParams();
134 content = fileParams.url.toString();
135 packet.addChild("x",Namespace.OOB).addChild("url").setContent(content);
136 } else {
137 content = message.getBody();
138 }
139 packet.setBody(content);
140 return packet;
141 }
142
143 public MessagePacket generatePgpChat(Message message) {
144 MessagePacket packet = preparePacket(message);
145 if (message.hasFileOnRemoteHost()) {
146 final String url = message.getFileParams().url.toString();
147 packet.setBody(url);
148 packet.addChild("x",Namespace.OOB).addChild("url").setContent(url);
149 } else {
150 if (Config.supportUnencrypted()) {
151 packet.setBody(PGP_FALLBACK_MESSAGE);
152 }
153 if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
154 packet.addChild("x", "jabber:x:encrypted").setContent(message.getEncryptedBody());
155 } else if (message.getEncryption() == Message.ENCRYPTION_PGP) {
156 packet.addChild("x", "jabber:x:encrypted").setContent(message.getBody());
157 }
158 packet.addChild("encryption", "urn:xmpp:eme:0")
159 .setAttribute("namespace", "jabber:x:encrypted");
160 }
161 return packet;
162 }
163
164 public MessagePacket generateChatState(Conversation conversation) {
165 final Account account = conversation.getAccount();
166 MessagePacket packet = new MessagePacket();
167 packet.setType(conversation.getMode() == Conversation.MODE_MULTI ? MessagePacket.TYPE_GROUPCHAT : MessagePacket.TYPE_CHAT);
168 packet.setTo(conversation.getJid().toBareJid());
169 packet.setFrom(account.getJid());
170 packet.addChild(ChatState.toElement(conversation.getOutgoingChatState()));
171 packet.addChild("no-store", "urn:xmpp:hints");
172 packet.addChild("no-storage", "urn:xmpp:hints"); //wrong! don't copy this. Its *store*
173 return packet;
174 }
175
176 public MessagePacket confirm(final Account account, final Jid to, final String id, final Jid counterpart, final boolean groupChat) {
177 MessagePacket packet = new MessagePacket();
178 packet.setType(groupChat ? MessagePacket.TYPE_GROUPCHAT : MessagePacket.TYPE_CHAT);
179 packet.setTo(groupChat ? to.toBareJid() : to);
180 packet.setFrom(account.getJid());
181 Element displayed = packet.addChild("displayed","urn:xmpp:chat-markers:0");
182 displayed.setAttribute("id", id);
183 if (groupChat && counterpart != null) {
184 displayed.setAttribute("sender",counterpart.toPreppedString());
185 }
186 packet.addChild("store", "urn:xmpp:hints");
187 return packet;
188 }
189
190 public MessagePacket conferenceSubject(Conversation conversation,String subject) {
191 MessagePacket packet = new MessagePacket();
192 packet.setType(MessagePacket.TYPE_GROUPCHAT);
193 packet.setTo(conversation.getJid().toBareJid());
194 Element subjectChild = new Element("subject");
195 subjectChild.setContent(subject);
196 packet.addChild(subjectChild);
197 packet.setFrom(conversation.getAccount().getJid().toBareJid());
198 return packet;
199 }
200
201 public MessagePacket directInvite(final Conversation conversation, final Jid contact) {
202 MessagePacket packet = new MessagePacket();
203 packet.setType(MessagePacket.TYPE_NORMAL);
204 packet.setTo(contact);
205 packet.setFrom(conversation.getAccount().getJid());
206 Element x = packet.addChild("x", "jabber:x:conference");
207 x.setAttribute("jid", conversation.getJid().toBareJid().toString());
208 String password = conversation.getMucOptions().getPassword();
209 if (password != null) {
210 x.setAttribute("password",password);
211 }
212 return packet;
213 }
214
215 public MessagePacket invite(Conversation conversation, Jid contact) {
216 MessagePacket packet = new MessagePacket();
217 packet.setTo(conversation.getJid().toBareJid());
218 packet.setFrom(conversation.getAccount().getJid());
219 Element x = new Element("x");
220 x.setAttribute("xmlns", "http://jabber.org/protocol/muc#user");
221 Element invite = new Element("invite");
222 invite.setAttribute("to", contact.toBareJid().toString());
223 x.addChild(invite);
224 packet.addChild(x);
225 return packet;
226 }
227
228 public MessagePacket received(Account account, MessagePacket originalMessage, ArrayList<String> namespaces, int type) {
229 MessagePacket receivedPacket = new MessagePacket();
230 receivedPacket.setType(type);
231 receivedPacket.setTo(originalMessage.getFrom());
232 receivedPacket.setFrom(account.getJid());
233 for(String namespace : namespaces) {
234 receivedPacket.addChild("received", namespace).setAttribute("id", originalMessage.getId());
235 }
236 return receivedPacket;
237 }
238
239 public MessagePacket generateOtrError(Jid to, String id, String errorText) {
240 MessagePacket packet = new MessagePacket();
241 packet.setType(MessagePacket.TYPE_ERROR);
242 packet.setAttribute("id",id);
243 packet.setTo(to);
244 Element error = packet.addChild("error");
245 error.setAttribute("code","406");
246 error.setAttribute("type","modify");
247 error.addChild("not-acceptable","urn:ietf:params:xml:ns:xmpp-stanzas");
248 error.addChild("text").setContent("?OTR Error:" + errorText);
249 return packet;
250 }
251}