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 addMessageHints(MessagePacket packet) {
72 packet.addChild("private", "urn:xmpp:carbons:2");
73 packet.addChild("no-copy", "urn:xmpp:hints");
74 packet.addChild("no-permanent-store", "urn:xmpp:hints");
75 packet.addChild("no-permanent-storage", "urn:xmpp:hints"); //do not copy this. this is wrong. it is *store*
76 }
77
78 public MessagePacket generateOtrChat(Message message) {
79 Session otrSession = message.getConversation().getOtrSession();
80 if (otrSession == null) {
81 return null;
82 }
83 MessagePacket packet = preparePacket(message);
84 addMessageHints(packet);
85 try {
86 String content;
87 if (message.hasFileOnRemoteHost()) {
88 content = message.getFileParams().url.toString();
89 } else {
90 content = message.getBody();
91 }
92 packet.setBody(otrSession.transformSending(content)[0]);
93 return packet;
94 } catch (OtrException e) {
95 return null;
96 }
97 }
98
99 public MessagePacket generateChat(Message message) {
100 MessagePacket packet = preparePacket(message);
101 if (message.hasFileOnRemoteHost()) {
102 packet.setBody(message.getFileParams().url.toString());
103 } else {
104 packet.setBody(message.getBody());
105 }
106 return packet;
107 }
108
109 public MessagePacket generatePgpChat(Message message) {
110 MessagePacket packet = preparePacket(message);
111 packet.setBody("This is an XEP-0027 encrypted message");
112 if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
113 packet.addChild("x", "jabber:x:encrypted").setContent(message.getEncryptedBody());
114 } else if (message.getEncryption() == Message.ENCRYPTION_PGP) {
115 packet.addChild("x", "jabber:x:encrypted").setContent(message.getBody());
116 }
117 return packet;
118 }
119
120 public MessagePacket generateChatState(Conversation conversation) {
121 final Account account = conversation.getAccount();
122 MessagePacket packet = new MessagePacket();
123 packet.setType(MessagePacket.TYPE_CHAT);
124 packet.setTo(conversation.getJid().toBareJid());
125 packet.setFrom(account.getJid());
126 packet.addChild(ChatState.toElement(conversation.getOutgoingChatState()));
127 packet.addChild("no-store", "urn:xmpp:hints");
128 packet.addChild("no-storage", "urn:xmpp:hints"); //wrong! don't copy this. Its *store*
129 return packet;
130 }
131
132 public MessagePacket confirm(final Account account, final Jid to, final String id) {
133 MessagePacket packet = new MessagePacket();
134 packet.setType(MessagePacket.TYPE_CHAT);
135 packet.setTo(to);
136 packet.setFrom(account.getJid());
137 Element received = packet.addChild("displayed","urn:xmpp:chat-markers:0");
138 received.setAttribute("id", id);
139 return packet;
140 }
141
142 public MessagePacket conferenceSubject(Conversation conversation,String subject) {
143 MessagePacket packet = new MessagePacket();
144 packet.setType(MessagePacket.TYPE_GROUPCHAT);
145 packet.setTo(conversation.getJid().toBareJid());
146 Element subjectChild = new Element("subject");
147 subjectChild.setContent(subject);
148 packet.addChild(subjectChild);
149 packet.setFrom(conversation.getAccount().getJid().toBareJid());
150 return packet;
151 }
152
153 public MessagePacket directInvite(final Conversation conversation, final Jid contact) {
154 MessagePacket packet = new MessagePacket();
155 packet.setType(MessagePacket.TYPE_NORMAL);
156 packet.setTo(contact);
157 packet.setFrom(conversation.getAccount().getJid());
158 Element x = packet.addChild("x", "jabber:x:conference");
159 x.setAttribute("jid", conversation.getJid().toBareJid().toString());
160 return packet;
161 }
162
163 public MessagePacket invite(Conversation conversation, Jid contact) {
164 MessagePacket packet = new MessagePacket();
165 packet.setTo(conversation.getJid().toBareJid());
166 packet.setFrom(conversation.getAccount().getJid());
167 Element x = new Element("x");
168 x.setAttribute("xmlns", "http://jabber.org/protocol/muc#user");
169 Element invite = new Element("invite");
170 invite.setAttribute("to", contact.toBareJid().toString());
171 x.addChild(invite);
172 packet.addChild(x);
173 return packet;
174 }
175
176 public MessagePacket received(Account account, MessagePacket originalMessage, String namespace, int type) {
177 MessagePacket receivedPacket = new MessagePacket();
178 receivedPacket.setType(type);
179 receivedPacket.setTo(originalMessage.getFrom());
180 receivedPacket.setFrom(account.getJid());
181 Element received = receivedPacket.addChild("received", namespace);
182 received.setAttribute("id", originalMessage.getId());
183 return receivedPacket;
184 }
185
186 public MessagePacket generateOtrError(Jid to, String id, String errorText) {
187 MessagePacket packet = new MessagePacket();
188 packet.setType(MessagePacket.TYPE_ERROR);
189 packet.setAttribute("id",id);
190 packet.setTo(to);
191 Element error = packet.addChild("error");
192 error.setAttribute("code","406");
193 error.setAttribute("type","modify");
194 error.addChild("not-acceptable","urn:ietf:params:xml:ns:xmpp-stanzas");
195 error.addChild("text").setContent("?OTR Error:" + errorText);
196 return packet;
197 }
198}