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