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 return packet;
83 }
84
85 private static boolean recipientSupportsOmemo(Message message) {
86 Contact c = message.getContact();
87 return c != null && c.getPresences().allOrNonSupport(AxolotlService.PEP_DEVICE_LIST_NOTIFY);
88 }
89
90 public static void addMessageHints(MessagePacket packet) {
91 packet.addChild("private", "urn:xmpp:carbons:2");
92 packet.addChild("no-copy", "urn:xmpp:hints");
93 packet.addChild("no-permanent-store", "urn:xmpp:hints");
94 packet.addChild("no-permanent-storage", "urn:xmpp:hints"); //do not copy this. this is wrong. it is *store*
95 }
96
97 public MessagePacket generateOtrChat(Message message) {
98 Session otrSession = message.getConversation().getOtrSession();
99 if (otrSession == null) {
100 return null;
101 }
102 MessagePacket packet = preparePacket(message);
103 addMessageHints(packet);
104 try {
105 String content;
106 if (message.hasFileOnRemoteHost()) {
107 content = message.getFileParams().url.toString();
108 } else {
109 content = message.getBody();
110 }
111 packet.setBody(otrSession.transformSending(content)[0]);
112 return packet;
113 } catch (OtrException e) {
114 return null;
115 }
116 }
117
118 public MessagePacket generateChat(Message message) {
119 MessagePacket packet = preparePacket(message);
120 String content;
121 if (message.hasFileOnRemoteHost()) {
122 Message.FileParams fileParams = message.getFileParams();
123 content = fileParams.url.toString();
124 packet.addChild("x","jabber:x:oob").addChild("url").setContent(content);
125 } else {
126 content = message.getBody();
127 }
128 packet.setBody(content);
129 return packet;
130 }
131
132 public MessagePacket generatePgpChat(Message message) {
133 MessagePacket packet = preparePacket(message);
134 if (Config.supportUnencrypted()) {
135 packet.setBody(PGP_FALLBACK_MESSAGE);
136 }
137 if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
138 packet.addChild("x", "jabber:x:encrypted").setContent(message.getEncryptedBody());
139 } else if (message.getEncryption() == Message.ENCRYPTION_PGP) {
140 packet.addChild("x", "jabber:x:encrypted").setContent(message.getBody());
141 }
142 return packet;
143 }
144
145 public MessagePacket generateChatState(Conversation conversation) {
146 final Account account = conversation.getAccount();
147 MessagePacket packet = new MessagePacket();
148 packet.setType(MessagePacket.TYPE_CHAT);
149 packet.setTo(conversation.getJid().toBareJid());
150 packet.setFrom(account.getJid());
151 packet.addChild(ChatState.toElement(conversation.getOutgoingChatState()));
152 packet.addChild("no-store", "urn:xmpp:hints");
153 packet.addChild("no-storage", "urn:xmpp:hints"); //wrong! don't copy this. Its *store*
154 return packet;
155 }
156
157 public MessagePacket confirm(final Account account, final Jid to, final String id) {
158 MessagePacket packet = new MessagePacket();
159 packet.setType(MessagePacket.TYPE_CHAT);
160 packet.setTo(to);
161 packet.setFrom(account.getJid());
162 Element received = packet.addChild("displayed","urn:xmpp:chat-markers:0");
163 received.setAttribute("id", id);
164 packet.addChild("store", "urn:xmpp:hints");
165 return packet;
166 }
167
168 public MessagePacket conferenceSubject(Conversation conversation,String subject) {
169 MessagePacket packet = new MessagePacket();
170 packet.setType(MessagePacket.TYPE_GROUPCHAT);
171 packet.setTo(conversation.getJid().toBareJid());
172 Element subjectChild = new Element("subject");
173 subjectChild.setContent(subject);
174 packet.addChild(subjectChild);
175 packet.setFrom(conversation.getAccount().getJid().toBareJid());
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().toBareJid().toString());
186 String password = conversation.getMucOptions().getPassword();
187 if (password != null) {
188 x.setAttribute("password",password);
189 }
190 return packet;
191 }
192
193 public MessagePacket invite(Conversation conversation, Jid contact) {
194 MessagePacket packet = new MessagePacket();
195 packet.setTo(conversation.getJid().toBareJid());
196 packet.setFrom(conversation.getAccount().getJid());
197 Element x = new Element("x");
198 x.setAttribute("xmlns", "http://jabber.org/protocol/muc#user");
199 Element invite = new Element("invite");
200 invite.setAttribute("to", contact.toBareJid().toString());
201 x.addChild(invite);
202 packet.addChild(x);
203 return packet;
204 }
205
206 public MessagePacket received(Account account, MessagePacket originalMessage, ArrayList<String> namespaces, int type) {
207 MessagePacket receivedPacket = new MessagePacket();
208 receivedPacket.setType(type);
209 receivedPacket.setTo(originalMessage.getFrom());
210 receivedPacket.setFrom(account.getJid());
211 for(String namespace : namespaces) {
212 receivedPacket.addChild("received", namespace).setAttribute("id", originalMessage.getId());
213 }
214 return receivedPacket;
215 }
216
217 public MessagePacket generateOtrError(Jid to, String id, String errorText) {
218 MessagePacket packet = new MessagePacket();
219 packet.setType(MessagePacket.TYPE_ERROR);
220 packet.setAttribute("id",id);
221 packet.setTo(to);
222 Element error = packet.addChild("error");
223 error.setAttribute("code","406");
224 error.setAttribute("type","modify");
225 error.addChild("not-acceptable","urn:ietf:params:xml:ns:xmpp-stanzas");
226 error.addChild("text").setContent("?OTR Error:" + errorText);
227 return packet;
228 }
229}