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