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