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