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