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 MessagePacket generateOtrChat(Message message) {
 72		Session otrSession = message.getConversation().getOtrSession();
 73		if (otrSession == null) {
 74			return null;
 75		}
 76		MessagePacket packet = preparePacket(message);
 77		packet.addChild("private", "urn:xmpp:carbons:2");
 78		packet.addChild("no-copy", "urn:xmpp:hints");
 79		packet.addChild("no-permanent-store", "urn:xmpp:hints");
 80		packet.addChild("no-permanent-storage", "urn:xmpp:hints");
 81		try {
 82			String content;
 83			if (message.hasFileOnRemoteHost()) {
 84				content = message.getFileParams().url.toString();
 85			} else {
 86				content = message.getBody();
 87			}
 88			packet.setBody(otrSession.transformSending(content)[0]);
 89			return packet;
 90		} catch (OtrException e) {
 91			return null;
 92		}
 93	}
 94
 95	public MessagePacket generateChat(Message message) {
 96		MessagePacket packet = preparePacket(message);
 97		if (message.hasFileOnRemoteHost()) {
 98			packet.setBody(message.getFileParams().url.toString());
 99		} else {
100			packet.setBody(message.getBody());
101		}
102		return packet;
103	}
104
105	public MessagePacket generatePgpChat(Message message) {
106		MessagePacket packet = preparePacket(message);
107		packet.setBody("This is an XEP-0027 encrypted message");
108		if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
109			packet.addChild("x", "jabber:x:encrypted").setContent(message.getEncryptedBody());
110		} else if (message.getEncryption() == Message.ENCRYPTION_PGP) {
111			packet.addChild("x", "jabber:x:encrypted").setContent(message.getBody());
112		}
113		return packet;
114	}
115
116	public MessagePacket generateChatState(Conversation conversation) {
117		final Account account = conversation.getAccount();
118		MessagePacket packet = new MessagePacket();
119		packet.setType(MessagePacket.TYPE_CHAT);
120		packet.setTo(conversation.getJid().toBareJid());
121		packet.setFrom(account.getJid());
122		packet.addChild(ChatState.toElement(conversation.getOutgoingChatState()));
123		packet.addChild("no-store", "urn:xmpp:hints");
124		packet.addChild("no-storage", "urn:xmpp:hints"); //wrong! don't copy this. Its *store*
125		return packet;
126	}
127
128	public MessagePacket confirm(final Account account, final Jid to, final String id) {
129		MessagePacket packet = new MessagePacket();
130		packet.setType(MessagePacket.TYPE_CHAT);
131		packet.setTo(to);
132		packet.setFrom(account.getJid());
133		Element received = packet.addChild("displayed","urn:xmpp:chat-markers:0");
134		received.setAttribute("id", id);
135		return packet;
136	}
137
138	public MessagePacket conferenceSubject(Conversation conversation,String subject) {
139		MessagePacket packet = new MessagePacket();
140		packet.setType(MessagePacket.TYPE_GROUPCHAT);
141		packet.setTo(conversation.getJid().toBareJid());
142		Element subjectChild = new Element("subject");
143		subjectChild.setContent(subject);
144		packet.addChild(subjectChild);
145		packet.setFrom(conversation.getAccount().getJid().toBareJid());
146		return packet;
147	}
148
149	public MessagePacket directInvite(final Conversation conversation, final Jid contact) {
150		MessagePacket packet = new MessagePacket();
151		packet.setType(MessagePacket.TYPE_NORMAL);
152		packet.setTo(contact);
153		packet.setFrom(conversation.getAccount().getJid());
154		Element x = packet.addChild("x", "jabber:x:conference");
155		x.setAttribute("jid", conversation.getJid().toBareJid().toString());
156		return packet;
157	}
158
159	public MessagePacket invite(Conversation conversation, Jid contact) {
160		MessagePacket packet = new MessagePacket();
161		packet.setTo(conversation.getJid().toBareJid());
162		packet.setFrom(conversation.getAccount().getJid());
163		Element x = new Element("x");
164		x.setAttribute("xmlns", "http://jabber.org/protocol/muc#user");
165		Element invite = new Element("invite");
166		invite.setAttribute("to", contact.toBareJid().toString());
167		x.addChild(invite);
168		packet.addChild(x);
169		return packet;
170	}
171
172	public MessagePacket received(Account account, MessagePacket originalMessage, String namespace, int type) {
173		MessagePacket receivedPacket = new MessagePacket();
174		receivedPacket.setType(type);
175		receivedPacket.setTo(originalMessage.getFrom());
176		receivedPacket.setFrom(account.getJid());
177		Element received = receivedPacket.addChild("received", namespace);
178		received.setAttribute("id", originalMessage.getId());
179		return receivedPacket;
180	}
181
182	public MessagePacket generateOtrError(Jid to, String id, String errorText) {
183		MessagePacket packet = new MessagePacket();
184		packet.setType(MessagePacket.TYPE_ERROR);
185		packet.setAttribute("id",id);
186		packet.setTo(to);
187		Element error = packet.addChild("error");
188		error.setAttribute("code","406");
189		error.setAttribute("type","modify");
190		error.addChild("not-acceptable","urn:ietf:params:xml:ns:xmpp-stanzas");
191		error.addChild("text").setContent("?OTR Error:" + errorText);
192		return packet;
193	}
194}