MessageGenerator.java

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