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.xmpp.chatstate.ChatState;
22import eu.siacs.conversations.xmpp.jid.Jid;
23import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
24
25public class MessageGenerator extends AbstractGenerator {
26 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";
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 = message.getConversation();
36 Account account = conversation.getAccount();
37 MessagePacket packet = new MessagePacket();
38 if (conversation.getMode() == Conversation.MODE_SINGLE) {
39 packet.setTo(message.getCounterpart());
40 packet.setType(MessagePacket.TYPE_CHAT);
41 packet.addChild("markable", "urn:xmpp:chat-markers:0");
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 if (this.mXmppConnectionService.indicateReceived()) {
49 packet.addChild("request", "urn:xmpp:receipts");
50 }
51 } else {
52 packet.setTo(message.getCounterpart().toBareJid());
53 packet.setType(MessagePacket.TYPE_GROUPCHAT);
54 }
55 packet.setFrom(account.getJid());
56 packet.setId(message.getUuid());
57 if (message.edited()) {
58 packet.addChild("replace","urn:xmpp:message-correct:0").setAttribute("id",message.getEditedId());
59 }
60 return packet;
61 }
62
63 public void addDelay(MessagePacket packet, long timestamp) {
64 final SimpleDateFormat mDateFormat = new SimpleDateFormat(
65 "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
66 mDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
67 Element delay = packet.addChild("delay", "urn:xmpp:delay");
68 Date date = new Date(timestamp);
69 delay.setAttribute("stamp", mDateFormat.format(date));
70 }
71
72 public MessagePacket generateAxolotlChat(Message message, XmppAxolotlMessage axolotlMessage) {
73 MessagePacket packet = preparePacket(message);
74 if (axolotlMessage == null) {
75 return null;
76 }
77 packet.setAxolotlMessage(axolotlMessage.toElement());
78 if (Config.supportUnencrypted() && !recipientSupportsOmemo(message)) {
79 packet.setBody(OMEMO_FALLBACK_MESSAGE);
80 }
81 packet.addChild("store", "urn:xmpp:hints");
82 packet.addChild("encryption","urn:xmpp:eme:0")
83 .setAttribute("name","OMEMO")
84 .setAttribute("namespace",AxolotlService.PEP_PREFIX);
85 return packet;
86 }
87
88 private static boolean recipientSupportsOmemo(Message message) {
89 Contact c = message.getContact();
90 return c != null && c.getPresences().allOrNonSupport(AxolotlService.PEP_DEVICE_LIST_NOTIFY);
91 }
92
93 public static void addMessageHints(MessagePacket packet) {
94 packet.addChild("private", "urn:xmpp:carbons:2");
95 packet.addChild("no-copy", "urn:xmpp:hints");
96 packet.addChild("no-permanent-store", "urn:xmpp:hints");
97 packet.addChild("no-permanent-storage", "urn:xmpp:hints"); //do not copy this. this is wrong. it is *store*
98 }
99
100 public MessagePacket generateOtrChat(Message message) {
101 Session otrSession = message.getConversation().getOtrSession();
102 if (otrSession == null) {
103 return null;
104 }
105 MessagePacket packet = preparePacket(message);
106 addMessageHints(packet);
107 try {
108 String content;
109 if (message.hasFileOnRemoteHost()) {
110 content = message.getFileParams().url.toString();
111 } else {
112 content = message.getBody();
113 }
114 packet.setBody(otrSession.transformSending(content)[0]);
115 packet.addChild("encryption","urn:xmpp:eme:0")
116 .setAttribute("namespace","urn:xmpp:otr:0");
117 return packet;
118 } catch (OtrException e) {
119 return null;
120 }
121 }
122
123 public MessagePacket generateChat(Message message) {
124 MessagePacket packet = preparePacket(message);
125 String content;
126 if (message.hasFileOnRemoteHost()) {
127 Message.FileParams fileParams = message.getFileParams();
128 content = fileParams.url.toString();
129 packet.addChild("x","jabber:x:oob").addChild("url").setContent(content);
130 } else {
131 content = message.getBody();
132 }
133 packet.setBody(content);
134 return packet;
135 }
136
137 public MessagePacket generatePgpChat(Message message) {
138 MessagePacket packet = preparePacket(message);
139 if (Config.supportUnencrypted()) {
140 packet.setBody(PGP_FALLBACK_MESSAGE);
141 }
142 if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
143 packet.addChild("x", "jabber:x:encrypted").setContent(message.getEncryptedBody());
144 } else if (message.getEncryption() == Message.ENCRYPTION_PGP) {
145 packet.addChild("x", "jabber:x:encrypted").setContent(message.getBody());
146 }
147 packet.addChild("encryption","urn:xmpp:eme:0")
148 .setAttribute("namespace","jabber:x:encrypted");
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(MessagePacket.TYPE_CHAT);
156 packet.setTo(conversation.getJid().toBareJid());
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 Account account, final Jid to, final String id) {
165 MessagePacket packet = new MessagePacket();
166 packet.setType(MessagePacket.TYPE_CHAT);
167 packet.setTo(to);
168 packet.setFrom(account.getJid());
169 Element received = packet.addChild("displayed","urn:xmpp:chat-markers:0");
170 received.setAttribute("id", id);
171 packet.addChild("store", "urn:xmpp:hints");
172 return packet;
173 }
174
175 public MessagePacket conferenceSubject(Conversation conversation,String subject) {
176 MessagePacket packet = new MessagePacket();
177 packet.setType(MessagePacket.TYPE_GROUPCHAT);
178 packet.setTo(conversation.getJid().toBareJid());
179 Element subjectChild = new Element("subject");
180 subjectChild.setContent(subject);
181 packet.addChild(subjectChild);
182 packet.setFrom(conversation.getAccount().getJid().toBareJid());
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().toBareJid().toString());
193 String password = conversation.getMucOptions().getPassword();
194 if (password != null) {
195 x.setAttribute("password",password);
196 }
197 return packet;
198 }
199
200 public MessagePacket invite(Conversation conversation, Jid contact) {
201 MessagePacket packet = new MessagePacket();
202 packet.setTo(conversation.getJid().toBareJid());
203 packet.setFrom(conversation.getAccount().getJid());
204 Element x = new Element("x");
205 x.setAttribute("xmlns", "http://jabber.org/protocol/muc#user");
206 Element invite = new Element("invite");
207 invite.setAttribute("to", contact.toBareJid().toString());
208 x.addChild(invite);
209 packet.addChild(x);
210 return packet;
211 }
212
213 public MessagePacket received(Account account, MessagePacket originalMessage, ArrayList<String> namespaces, int type) {
214 MessagePacket receivedPacket = new MessagePacket();
215 receivedPacket.setType(type);
216 receivedPacket.setTo(originalMessage.getFrom());
217 receivedPacket.setFrom(account.getJid());
218 for(String namespace : namespaces) {
219 receivedPacket.addChild("received", namespace).setAttribute("id", originalMessage.getId());
220 }
221 return receivedPacket;
222 }
223
224 public MessagePacket generateOtrError(Jid to, String id, String errorText) {
225 MessagePacket packet = new MessagePacket();
226 packet.setType(MessagePacket.TYPE_ERROR);
227 packet.setAttribute("id",id);
228 packet.setTo(to);
229 Element error = packet.addChild("error");
230 error.setAttribute("code","406");
231 error.setAttribute("type","modify");
232 error.addChild("not-acceptable","urn:ietf:params:xml:ns:xmpp-stanzas");
233 error.addChild("text").setContent("?OTR Error:" + errorText);
234 return packet;
235 }
236}