2014-06-22 11:24:47 -04:00
|
|
|
package eu.siacs.conversations.generator;
|
|
|
|
|
2014-06-22 12:21:04 -04:00
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.Date;
|
2014-06-22 15:44:17 -04:00
|
|
|
import java.util.Locale;
|
2014-06-22 12:21:04 -04:00
|
|
|
import java.util.TimeZone;
|
|
|
|
|
2014-06-22 11:24:47 -04:00
|
|
|
import net.java.otr4j.OtrException;
|
|
|
|
import net.java.otr4j.session.Session;
|
|
|
|
import eu.siacs.conversations.entities.Account;
|
|
|
|
import eu.siacs.conversations.entities.Conversation;
|
|
|
|
import eu.siacs.conversations.entities.Message;
|
2014-06-22 12:21:04 -04:00
|
|
|
import eu.siacs.conversations.xml.Element;
|
2014-06-22 11:24:47 -04:00
|
|
|
import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
|
|
|
|
|
|
|
|
public class MessageGenerator {
|
2014-06-22 12:21:04 -04:00
|
|
|
private MessagePacket preparePacket(Message message, boolean addDelay) {
|
2014-06-22 11:24:47 -04:00
|
|
|
Conversation conversation = message.getConversation();
|
|
|
|
Account account = conversation.getAccount();
|
|
|
|
MessagePacket packet = new MessagePacket();
|
|
|
|
if (conversation.getMode() == Conversation.MODE_SINGLE) {
|
|
|
|
packet.setTo(message.getCounterpart());
|
|
|
|
packet.setType(MessagePacket.TYPE_CHAT);
|
2014-06-23 17:29:26 -04:00
|
|
|
packet.addChild("markable", "urn:xmpp:chat-markers:0");
|
2014-08-10 09:27:44 -04:00
|
|
|
} else if (message.getType() == Message.TYPE_PRIVATE) {
|
|
|
|
packet.setTo(message.getCounterpart());
|
|
|
|
packet.setType(MessagePacket.TYPE_CHAT);
|
2014-06-22 11:24:47 -04:00
|
|
|
} else {
|
|
|
|
packet.setTo(message.getCounterpart().split("/")[0]);
|
|
|
|
packet.setType(MessagePacket.TYPE_GROUPCHAT);
|
|
|
|
}
|
|
|
|
packet.setFrom(account.getFullJid());
|
|
|
|
packet.setId(message.getUuid());
|
2014-06-22 12:21:04 -04:00
|
|
|
if (addDelay) {
|
|
|
|
addDelay(packet,message.getTimeSent());
|
|
|
|
}
|
2014-06-22 11:24:47 -04:00
|
|
|
return packet;
|
|
|
|
}
|
|
|
|
|
2014-06-22 12:21:04 -04:00
|
|
|
private void addDelay(MessagePacket packet, long timestamp) {
|
2014-06-22 15:44:17 -04:00
|
|
|
final SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",Locale.US);
|
2014-06-22 12:21:04 -04:00
|
|
|
mDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
|
|
|
|
Element delay = packet.addChild("delay", "urn:xmpp:delay");
|
|
|
|
Date date = new Date(timestamp);
|
|
|
|
delay.setAttribute("stamp", mDateFormat.format(date));
|
|
|
|
}
|
|
|
|
|
2014-07-11 21:44:23 -04:00
|
|
|
public MessagePacket generateOtrChat(Message message) {
|
2014-06-22 12:21:04 -04:00
|
|
|
return generateOtrChat(message, false);
|
|
|
|
}
|
|
|
|
|
2014-06-24 10:14:18 -04:00
|
|
|
public MessagePacket generateOtrChat(Message message, boolean addDelay) {
|
2014-06-22 11:24:47 -04:00
|
|
|
Session otrSession = message.getConversation().getOtrSession();
|
|
|
|
if (otrSession==null) {
|
2014-06-24 10:14:18 -04:00
|
|
|
return null;
|
2014-06-22 11:24:47 -04:00
|
|
|
}
|
2014-06-22 12:21:04 -04:00
|
|
|
MessagePacket packet = preparePacket(message,addDelay);
|
2014-06-22 11:24:47 -04:00
|
|
|
packet.addChild("private", "urn:xmpp:carbons:2");
|
|
|
|
packet.addChild("no-copy", "urn:xmpp:hints");
|
2014-06-24 10:14:18 -04:00
|
|
|
try {
|
|
|
|
packet.setBody(otrSession.transformSending(message
|
|
|
|
.getBody()));
|
|
|
|
return packet;
|
|
|
|
} catch (OtrException e) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-06-22 11:24:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public MessagePacket generateChat(Message message) {
|
2014-06-22 12:21:04 -04:00
|
|
|
return generateChat(message, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public MessagePacket generateChat(Message message, boolean addDelay) {
|
|
|
|
MessagePacket packet = preparePacket(message,addDelay);
|
2014-06-22 11:24:47 -04:00
|
|
|
packet.setBody(message.getBody());
|
|
|
|
return packet;
|
|
|
|
}
|
|
|
|
|
|
|
|
public MessagePacket generatePgpChat(Message message) {
|
2014-06-22 12:21:04 -04:00
|
|
|
return generatePgpChat(message, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public MessagePacket generatePgpChat(Message message, boolean addDelay) {
|
|
|
|
MessagePacket packet = preparePacket(message,addDelay);
|
2014-06-22 11:24:47 -04:00
|
|
|
packet.setBody("This is an XEP-0027 encryted message");
|
|
|
|
if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
|
|
|
|
packet.addChild("x", "jabber:x:encrypted").setContent(
|
|
|
|
message.getEncryptedBody());
|
|
|
|
} else if (message.getEncryption() == Message.ENCRYPTION_PGP) {
|
|
|
|
packet.setBody(message.getBody());
|
|
|
|
}
|
|
|
|
return packet;
|
|
|
|
}
|
2014-06-26 10:42:24 -04:00
|
|
|
|
|
|
|
public MessagePacket generateNotAcceptable(MessagePacket origin) {
|
|
|
|
MessagePacket packet = generateError(origin);
|
|
|
|
Element error = packet.addChild("error");
|
|
|
|
error.setAttribute("type", "modify");
|
|
|
|
error.setAttribute("code", "406");
|
|
|
|
error.addChild("not-acceptable");
|
|
|
|
return packet;
|
|
|
|
}
|
|
|
|
|
|
|
|
private MessagePacket generateError(MessagePacket origin) {
|
|
|
|
MessagePacket packet = new MessagePacket();
|
|
|
|
packet.setId(origin.getId());
|
|
|
|
packet.setTo(origin.getFrom());
|
|
|
|
packet.setBody(origin.getBody());
|
|
|
|
packet.setType(MessagePacket.TYPE_ERROR);
|
|
|
|
return packet;
|
|
|
|
}
|
2014-07-11 21:44:23 -04:00
|
|
|
|
|
|
|
public MessagePacket confirm(Account account, String to, String id) {
|
|
|
|
MessagePacket packet = new MessagePacket();
|
|
|
|
packet.setType(MessagePacket.TYPE_NORMAL);
|
|
|
|
packet.setTo(to);
|
|
|
|
packet.setFrom(account.getFullJid());
|
|
|
|
Element received = packet.addChild("displayed",
|
|
|
|
"urn:xmpp:chat-markers:0");
|
|
|
|
received.setAttribute("id", id);
|
|
|
|
return packet;
|
|
|
|
}
|
|
|
|
|
2014-07-12 05:13:18 -04:00
|
|
|
public MessagePacket conferenceSubject(Conversation conversation,String subject) {
|
2014-07-11 21:44:23 -04:00
|
|
|
MessagePacket packet = new MessagePacket();
|
|
|
|
packet.setType(MessagePacket.TYPE_GROUPCHAT);
|
|
|
|
packet.setTo(conversation.getContactJid().split("/")[0]);
|
|
|
|
Element subjectChild = new Element("subject");
|
|
|
|
subjectChild.setContent(subject);
|
|
|
|
packet.addChild(subjectChild);
|
|
|
|
packet.setFrom(conversation.getAccount().getJid());
|
|
|
|
return packet;
|
|
|
|
}
|
2014-07-21 10:04:53 -04:00
|
|
|
|
2014-07-21 13:55:00 -04:00
|
|
|
public MessagePacket directInvite(Conversation conversation, String contact) {
|
2014-07-21 10:04:53 -04:00
|
|
|
MessagePacket packet = new MessagePacket();
|
|
|
|
packet.setType(MessagePacket.TYPE_NORMAL);
|
|
|
|
packet.setTo(contact);
|
|
|
|
packet.setFrom(conversation.getAccount().getFullJid());
|
|
|
|
Element x = packet.addChild("x", "jabber:x:conference");
|
|
|
|
x.setAttribute("jid", conversation.getContactJid().split("/")[0]);
|
|
|
|
return packet;
|
|
|
|
}
|
2014-07-21 13:55:00 -04:00
|
|
|
|
|
|
|
public MessagePacket invite(Conversation conversation, String contact) {
|
|
|
|
MessagePacket packet = new MessagePacket();
|
|
|
|
packet.setTo(conversation.getContactJid().split("/")[0]);
|
|
|
|
packet.setFrom(conversation.getAccount().getFullJid());
|
|
|
|
Element x = new Element("x");
|
|
|
|
x.setAttribute("xmlns", "http://jabber.org/protocol/muc#user");
|
|
|
|
Element invite = new Element("invite");
|
|
|
|
invite.setAttribute("to", contact);
|
|
|
|
x.addChild(invite);
|
|
|
|
packet.addChild(x);
|
|
|
|
return packet;
|
|
|
|
}
|
2014-07-23 19:04:25 -04:00
|
|
|
|
|
|
|
public MessagePacket received(Account account, MessagePacket originalMessage, String namespace) {
|
|
|
|
MessagePacket receivedPacket = new MessagePacket();
|
|
|
|
receivedPacket.setType(MessagePacket.TYPE_NORMAL);
|
|
|
|
receivedPacket.setTo(originalMessage.getFrom());
|
|
|
|
receivedPacket.setFrom(account.getFullJid());
|
|
|
|
Element received = receivedPacket.addChild("received",namespace);
|
|
|
|
received.setAttribute("id", originalMessage.getId());
|
|
|
|
return receivedPacket;
|
|
|
|
}
|
2014-06-22 11:24:47 -04:00
|
|
|
}
|