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