MessageGenerator.java

  1package eu.siacs.conversations.generator;
  2
  3import java.text.SimpleDateFormat;
  4import java.util.ArrayList;
  5import java.util.Date;
  6import java.util.Locale;
  7import java.util.TimeZone;
  8
  9import eu.siacs.conversations.Config;
 10import eu.siacs.conversations.crypto.axolotl.AxolotlService;
 11import eu.siacs.conversations.crypto.axolotl.XmppAxolotlMessage;
 12import eu.siacs.conversations.entities.Account;
 13import eu.siacs.conversations.entities.Contact;
 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.xml.Namespace;
 19import eu.siacs.conversations.xmpp.chatstate.ChatState;
 20import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
 21import rocks.xmpp.addr.Jid;
 22
 23public class MessageGenerator extends AbstractGenerator {
 24	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";
 25	private static final String PGP_FALLBACK_MESSAGE = "I sent you a PGP encrypted message but your client doesn’t seem to support that.";
 26
 27	public MessageGenerator(XmppConnectionService service) {
 28		super(service);
 29	}
 30
 31	private MessagePacket preparePacket(Message message) {
 32		Conversation conversation = (Conversation) message.getConversation();
 33		Account account = conversation.getAccount();
 34		MessagePacket packet = new MessagePacket();
 35		final boolean isWithSelf = conversation.getContact().isSelf();
 36		if (conversation.getMode() == Conversation.MODE_SINGLE) {
 37			packet.setTo(message.getCounterpart());
 38			packet.setType(MessagePacket.TYPE_CHAT);
 39			if (this.mXmppConnectionService.indicateReceived() && !isWithSelf) {
 40				packet.addChild("request", "urn:xmpp:receipts");
 41			}
 42		} else if (message.getType() == Message.TYPE_PRIVATE) { //TODO files and images might be private as well
 43			packet.setTo(message.getCounterpart());
 44			packet.setType(MessagePacket.TYPE_CHAT);
 45			packet.addChild("x","http://jabber.org/protocol/muc#user");
 46			if (this.mXmppConnectionService.indicateReceived()) {
 47				packet.addChild("request", "urn:xmpp:receipts");
 48			}
 49		} else {
 50			packet.setTo(message.getCounterpart().asBareJid());
 51			packet.setType(MessagePacket.TYPE_GROUPCHAT);
 52		}
 53		if (conversation.isSingleOrPrivateAndNonAnonymous() && message.getType() != Message.TYPE_PRIVATE) {
 54			packet.addChild("markable", "urn:xmpp:chat-markers:0");
 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		packet.setBody(OMEMO_FALLBACK_MESSAGE);
 81		packet.addChild("store", "urn:xmpp:hints");
 82		packet.addChild("encryption","urn:xmpp:eme:0")
 83				.setAttribute("name","OMEMO")
 84				.setAttribute("namespace",AxolotlService.PEP_PREFIX);
 85		return packet;
 86	}
 87
 88	public MessagePacket generateKeyTransportMessage(Jid to, XmppAxolotlMessage axolotlMessage) {
 89		MessagePacket packet = new MessagePacket();
 90		packet.setType(MessagePacket.TYPE_CHAT);
 91		packet.setTo(to);
 92		packet.setAxolotlMessage(axolotlMessage.toElement());
 93		packet.addChild("store", "urn:xmpp:hints");
 94		return packet;
 95	}
 96
 97	public MessagePacket generateChat(Message message) {
 98		MessagePacket packet = preparePacket(message);
 99		String content;
100		if (message.hasFileOnRemoteHost()) {
101			Message.FileParams fileParams = message.getFileParams();
102			content = fileParams.url.toString();
103			packet.addChild("x",Namespace.OOB).addChild("url").setContent(content);
104		} else {
105			content = message.getBody();
106		}
107		packet.setBody(content);
108		return packet;
109	}
110
111	public MessagePacket generatePgpChat(Message message) {
112		MessagePacket packet = preparePacket(message);
113		if (message.hasFileOnRemoteHost()) {
114			final String url = message.getFileParams().url.toString();
115			packet.setBody(url);
116			packet.addChild("x",Namespace.OOB).addChild("url").setContent(url);
117		} else {
118			if (Config.supportUnencrypted()) {
119				packet.setBody(PGP_FALLBACK_MESSAGE);
120			}
121			if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
122				packet.addChild("x", "jabber:x:encrypted").setContent(message.getEncryptedBody());
123			} else if (message.getEncryption() == Message.ENCRYPTION_PGP) {
124				packet.addChild("x", "jabber:x:encrypted").setContent(message.getBody());
125			}
126			packet.addChild("encryption", "urn:xmpp:eme:0")
127					.setAttribute("namespace", "jabber:x:encrypted");
128		}
129		return packet;
130	}
131
132	public MessagePacket generateChatState(Conversation conversation) {
133		final Account account = conversation.getAccount();
134		MessagePacket packet = new MessagePacket();
135		packet.setType(conversation.getMode() == Conversation.MODE_MULTI ? MessagePacket.TYPE_GROUPCHAT : MessagePacket.TYPE_CHAT);
136		packet.setTo(conversation.getJid().asBareJid());
137		packet.setFrom(account.getJid());
138		packet.addChild(ChatState.toElement(conversation.getOutgoingChatState()));
139		packet.addChild("no-store", "urn:xmpp:hints");
140		packet.addChild("no-storage", "urn:xmpp:hints"); //wrong! don't copy this. Its *store*
141		return packet;
142	}
143
144	public MessagePacket confirm(final Account account, final Jid to, final String id, final Jid counterpart, final boolean groupChat) {
145		MessagePacket packet = new MessagePacket();
146		packet.setType(groupChat ? MessagePacket.TYPE_GROUPCHAT : MessagePacket.TYPE_CHAT);
147		packet.setTo(groupChat ? to.asBareJid() : to);
148		packet.setFrom(account.getJid());
149		Element displayed = packet.addChild("displayed","urn:xmpp:chat-markers:0");
150		displayed.setAttribute("id", id);
151		if (groupChat && counterpart != null) {
152			displayed.setAttribute("sender",counterpart.toString());
153		}
154		packet.addChild("store", "urn:xmpp:hints");
155		return packet;
156	}
157
158	public MessagePacket conferenceSubject(Conversation conversation,String subject) {
159		MessagePacket packet = new MessagePacket();
160		packet.setType(MessagePacket.TYPE_GROUPCHAT);
161		packet.setTo(conversation.getJid().asBareJid());
162		Element subjectChild = new Element("subject");
163		subjectChild.setContent(subject);
164		packet.addChild(subjectChild);
165		packet.setFrom(conversation.getAccount().getJid().asBareJid());
166		return packet;
167	}
168
169	public MessagePacket directInvite(final Conversation conversation, final Jid contact) {
170		MessagePacket packet = new MessagePacket();
171		packet.setType(MessagePacket.TYPE_NORMAL);
172		packet.setTo(contact);
173		packet.setFrom(conversation.getAccount().getJid());
174		Element x = packet.addChild("x", "jabber:x:conference");
175		x.setAttribute("jid", conversation.getJid().asBareJid().toString());
176		String password = conversation.getMucOptions().getPassword();
177		if (password != null) {
178			x.setAttribute("password",password);
179		}
180		return packet;
181	}
182
183	public MessagePacket invite(Conversation conversation, Jid contact) {
184		MessagePacket packet = new MessagePacket();
185		packet.setTo(conversation.getJid().asBareJid());
186		packet.setFrom(conversation.getAccount().getJid());
187		Element x = new Element("x");
188		x.setAttribute("xmlns", "http://jabber.org/protocol/muc#user");
189		Element invite = new Element("invite");
190		invite.setAttribute("to", contact.asBareJid().toString());
191		x.addChild(invite);
192		packet.addChild(x);
193		return packet;
194	}
195
196	public MessagePacket received(Account account, MessagePacket originalMessage, ArrayList<String> namespaces, int type) {
197		MessagePacket receivedPacket = new MessagePacket();
198		receivedPacket.setType(type);
199		receivedPacket.setTo(originalMessage.getFrom());
200		receivedPacket.setFrom(account.getJid());
201		for(String namespace : namespaces) {
202			receivedPacket.addChild("received", namespace).setAttribute("id", originalMessage.getId());
203		}
204		receivedPacket.addChild("store", "urn:xmpp:hints");
205		return receivedPacket;
206	}
207
208	public MessagePacket received(Account account, Jid to, String id) {
209		MessagePacket packet = new MessagePacket();
210		packet.setFrom(account.getJid());
211		packet.setTo(to);
212		packet.addChild("received","urn:xmpp:receipts").setAttribute("id",id);
213		packet.addChild("store", "urn:xmpp:hints");
214		return packet;
215	}
216}