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