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