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			if (this.mXmppConnectionService.indicateReceived()) {
 43				packet.addChild("request", "urn:xmpp:receipts");
 44			}
 45		} else if (message.getType() == Message.TYPE_PRIVATE) {
 46			packet.setTo(message.getCounterpart());
 47			packet.setType(MessagePacket.TYPE_CHAT);
 48			packet.addChild("x","http://jabber.org/protocol/muc#user");
 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		if (conversation.getMode() == Conversation.MODE_SINGLE ||
 57				(conversation.getMucOptions().nonanonymous() && conversation.getMucOptions().membersOnly() && message.getType() != Message.TYPE_PRIVATE)) {
 58			packet.addChild("markable", "urn:xmpp:chat-markers:0");
 59		}
 60		packet.setFrom(account.getJid());
 61		packet.setId(message.getUuid());
 62		packet.addChild("origin-id", Namespace.STANZA_IDS).setAttribute("id",message.getUuid());
 63		if (message.edited()) {
 64			packet.addChild("replace","urn:xmpp:message-correct:0").setAttribute("id",message.getEditedId());
 65		}
 66		return packet;
 67	}
 68
 69	public void addDelay(MessagePacket packet, long timestamp) {
 70		final SimpleDateFormat mDateFormat = new SimpleDateFormat(
 71				"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
 72		mDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
 73		Element delay = packet.addChild("delay", "urn:xmpp:delay");
 74		Date date = new Date(timestamp);
 75		delay.setAttribute("stamp", mDateFormat.format(date));
 76	}
 77
 78	public MessagePacket generateAxolotlChat(Message message, XmppAxolotlMessage axolotlMessage) {
 79		MessagePacket packet = preparePacket(message);
 80		if (axolotlMessage == null) {
 81			return null;
 82		}
 83		packet.setAxolotlMessage(axolotlMessage.toElement());
 84		if (Config.supportUnencrypted() && !recipientSupportsOmemo(message)) {
 85			packet.setBody(OMEMO_FALLBACK_MESSAGE);
 86		}
 87		packet.addChild("store", "urn:xmpp:hints");
 88		packet.addChild("encryption","urn:xmpp:eme:0")
 89				.setAttribute("name","OMEMO")
 90				.setAttribute("namespace",AxolotlService.PEP_PREFIX);
 91		return packet;
 92	}
 93
 94	public MessagePacket generateKeyTransportMessage(Jid to, XmppAxolotlMessage axolotlMessage) {
 95		MessagePacket packet = new MessagePacket();
 96		packet.setType(MessagePacket.TYPE_CHAT);
 97		packet.setTo(to);
 98		packet.setAxolotlMessage(axolotlMessage.toElement());
 99		packet.addChild("store", "urn:xmpp:hints");
100		return packet;
101	}
102
103	private static boolean recipientSupportsOmemo(Message message) {
104		Contact c = message.getContact();
105		return c != null && c.getPresences().allOrNonSupport(AxolotlService.PEP_DEVICE_LIST_NOTIFY);
106	}
107
108	public static void addMessageHints(MessagePacket packet) {
109		packet.addChild("private", "urn:xmpp:carbons:2");
110		packet.addChild("no-copy", "urn:xmpp:hints");
111		packet.addChild("no-permanent-store", "urn:xmpp:hints");
112		packet.addChild("no-permanent-storage", "urn:xmpp:hints"); //do not copy this. this is wrong. it is *store*
113	}
114
115	public MessagePacket generateOtrChat(Message message) {
116		Session otrSession = message.getConversation().getOtrSession();
117		if (otrSession == null) {
118			return null;
119		}
120		MessagePacket packet = preparePacket(message);
121		addMessageHints(packet);
122		try {
123			String content;
124			if (message.hasFileOnRemoteHost()) {
125				content = message.getFileParams().url.toString();
126			} else {
127				content = message.getBody();
128			}
129			packet.setBody(otrSession.transformSending(content)[0]);
130			packet.addChild("encryption","urn:xmpp:eme:0")
131					.setAttribute("namespace","urn:xmpp:otr:0");
132			return packet;
133		} catch (OtrException e) {
134			return null;
135		}
136	}
137
138	public MessagePacket generateChat(Message message) {
139		MessagePacket packet = preparePacket(message);
140		String content;
141		if (message.hasFileOnRemoteHost()) {
142			Message.FileParams fileParams = message.getFileParams();
143			content = fileParams.url.toString();
144			packet.addChild("x",Namespace.OOB).addChild("url").setContent(content);
145		} else {
146			content = message.getBody();
147		}
148		packet.setBody(content);
149		return packet;
150	}
151
152	public MessagePacket generatePgpChat(Message message) {
153		MessagePacket packet = preparePacket(message);
154		if (message.hasFileOnRemoteHost()) {
155			final String url = message.getFileParams().url.toString();
156			packet.setBody(url);
157			packet.addChild("x",Namespace.OOB).addChild("url").setContent(url);
158		} else {
159			if (Config.supportUnencrypted()) {
160				packet.setBody(PGP_FALLBACK_MESSAGE);
161			}
162			if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
163				packet.addChild("x", "jabber:x:encrypted").setContent(message.getEncryptedBody());
164			} else if (message.getEncryption() == Message.ENCRYPTION_PGP) {
165				packet.addChild("x", "jabber:x:encrypted").setContent(message.getBody());
166			}
167			packet.addChild("encryption", "urn:xmpp:eme:0")
168					.setAttribute("namespace", "jabber:x:encrypted");
169		}
170		return packet;
171	}
172
173	public MessagePacket generateChatState(Conversation conversation) {
174		final Account account = conversation.getAccount();
175		MessagePacket packet = new MessagePacket();
176		packet.setType(conversation.getMode() == Conversation.MODE_MULTI ? MessagePacket.TYPE_GROUPCHAT : MessagePacket.TYPE_CHAT);
177		packet.setTo(conversation.getJid().toBareJid());
178		packet.setFrom(account.getJid());
179		packet.addChild(ChatState.toElement(conversation.getOutgoingChatState()));
180		packet.addChild("no-store", "urn:xmpp:hints");
181		packet.addChild("no-storage", "urn:xmpp:hints"); //wrong! don't copy this. Its *store*
182		return packet;
183	}
184
185	public MessagePacket confirm(final Account account, final Jid to, final String id, final Jid counterpart, final boolean groupChat) {
186		MessagePacket packet = new MessagePacket();
187		packet.setType(groupChat ? MessagePacket.TYPE_GROUPCHAT : MessagePacket.TYPE_CHAT);
188		packet.setTo(groupChat ? to.toBareJid() : to);
189		packet.setFrom(account.getJid());
190		Element displayed = packet.addChild("displayed","urn:xmpp:chat-markers:0");
191		displayed.setAttribute("id", id);
192		if (groupChat && counterpart != null) {
193			displayed.setAttribute("sender",counterpart.toPreppedString());
194		}
195		packet.addChild("store", "urn:xmpp:hints");
196		return packet;
197	}
198
199	public MessagePacket conferenceSubject(Conversation conversation,String subject) {
200		MessagePacket packet = new MessagePacket();
201		packet.setType(MessagePacket.TYPE_GROUPCHAT);
202		packet.setTo(conversation.getJid().toBareJid());
203		Element subjectChild = new Element("subject");
204		subjectChild.setContent(subject);
205		packet.addChild(subjectChild);
206		packet.setFrom(conversation.getAccount().getJid().toBareJid());
207		return packet;
208	}
209
210	public MessagePacket directInvite(final Conversation conversation, final Jid contact) {
211		MessagePacket packet = new MessagePacket();
212		packet.setType(MessagePacket.TYPE_NORMAL);
213		packet.setTo(contact);
214		packet.setFrom(conversation.getAccount().getJid());
215		Element x = packet.addChild("x", "jabber:x:conference");
216		x.setAttribute("jid", conversation.getJid().toBareJid().toString());
217		String password = conversation.getMucOptions().getPassword();
218		if (password != null) {
219			x.setAttribute("password",password);
220		}
221		return packet;
222	}
223
224	public MessagePacket invite(Conversation conversation, Jid contact) {
225		MessagePacket packet = new MessagePacket();
226		packet.setTo(conversation.getJid().toBareJid());
227		packet.setFrom(conversation.getAccount().getJid());
228		Element x = new Element("x");
229		x.setAttribute("xmlns", "http://jabber.org/protocol/muc#user");
230		Element invite = new Element("invite");
231		invite.setAttribute("to", contact.toBareJid().toString());
232		x.addChild(invite);
233		packet.addChild(x);
234		return packet;
235	}
236
237	public MessagePacket received(Account account, MessagePacket originalMessage, ArrayList<String> namespaces, int type) {
238		MessagePacket receivedPacket = new MessagePacket();
239		receivedPacket.setType(type);
240		receivedPacket.setTo(originalMessage.getFrom());
241		receivedPacket.setFrom(account.getJid());
242		for(String namespace : namespaces) {
243			receivedPacket.addChild("received", namespace).setAttribute("id", originalMessage.getId());
244		}
245		return receivedPacket;
246	}
247
248	public MessagePacket generateOtrError(Jid to, String id, String errorText) {
249		MessagePacket packet = new MessagePacket();
250		packet.setType(MessagePacket.TYPE_ERROR);
251		packet.setAttribute("id",id);
252		packet.setTo(to);
253		Element error = packet.addChild("error");
254		error.setAttribute("code","406");
255		error.setAttribute("type","modify");
256		error.addChild("not-acceptable","urn:ietf:params:xml:ns:xmpp-stanzas");
257		error.addChild("text").setContent("?OTR Error:" + errorText);
258		return packet;
259	}
260}