conversations-classic/src/eu/siacs/conversations/generator/MessageGenerator.java

87 lines
2.9 KiB
Java
Raw Normal View History

package eu.siacs.conversations.generator;
2014-06-22 16:21:04 +00:00
import java.text.SimpleDateFormat;
import java.util.Date;
2014-06-22 19:44:17 +00:00
import java.util.Locale;
2014-06-22 16:21:04 +00:00
import java.util.TimeZone;
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 16:21:04 +00:00
import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
public class MessageGenerator {
2014-06-22 16:21:04 +00:00
private MessagePacket preparePacket(Message message, boolean addDelay) {
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);
} else {
packet.setTo(message.getCounterpart().split("/")[0]);
packet.setType(MessagePacket.TYPE_GROUPCHAT);
}
packet.setFrom(account.getFullJid());
packet.setId(message.getUuid());
2014-06-22 16:21:04 +00:00
if (addDelay) {
addDelay(packet,message.getTimeSent());
}
return packet;
}
2014-06-22 16:21:04 +00:00
private void addDelay(MessagePacket packet, long timestamp) {
2014-06-22 19:44:17 +00:00
final SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",Locale.US);
2014-06-22 16:21:04 +00: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));
}
public MessagePacket generateOtrChat(Message message) throws OtrException {
2014-06-22 16:21:04 +00:00
return generateOtrChat(message, false);
}
public MessagePacket generateOtrChat(Message message, boolean addDelay) throws OtrException {
Session otrSession = message.getConversation().getOtrSession();
if (otrSession==null) {
throw new OtrException(null);
}
2014-06-22 16:21:04 +00:00
MessagePacket packet = preparePacket(message,addDelay);
packet.addChild("private", "urn:xmpp:carbons:2");
packet.addChild("no-copy", "urn:xmpp:hints");
packet.setBody(otrSession.transformSending(message
.getBody()));
return packet;
}
public MessagePacket generateChat(Message message) {
2014-06-22 16:21:04 +00:00
return generateChat(message, false);
}
public MessagePacket generateChat(Message message, boolean addDelay) {
MessagePacket packet = preparePacket(message,addDelay);
packet.setBody(message.getBody());
return packet;
}
public MessagePacket generatePgpChat(Message message) {
2014-06-22 16:21:04 +00:00
return generatePgpChat(message, false);
}
public MessagePacket generatePgpChat(Message message, boolean addDelay) {
MessagePacket packet = preparePacket(message,addDelay);
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;
}
}