From 1374fed31b96cbe4a16ee6349afe38f9203c8ac4 Mon Sep 17 00:00:00 2001 From: iNPUTmice Date: Sun, 22 Jun 2014 18:21:04 +0200 Subject: [PATCH] fixed #220 --- .../generator/MessageGenerator.java | 36 ++++++++++++++++--- .../conversations/parser/AbstractParser.java | 26 +++++++++++--- .../conversations/parser/MessageParser.java | 8 +++-- .../services/XmppConnectionService.java | 6 ++-- .../conversations/xmpp/XmppConnection.java | 1 + 5 files changed, 63 insertions(+), 14 deletions(-) diff --git a/src/eu/siacs/conversations/generator/MessageGenerator.java b/src/eu/siacs/conversations/generator/MessageGenerator.java index 57b21fcf7..a0bb7fb8e 100644 --- a/src/eu/siacs/conversations/generator/MessageGenerator.java +++ b/src/eu/siacs/conversations/generator/MessageGenerator.java @@ -1,14 +1,19 @@ package eu.siacs.conversations.generator; +import java.text.SimpleDateFormat; +import java.util.Date; +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; +import eu.siacs.conversations.xml.Element; import eu.siacs.conversations.xmpp.stanzas.MessagePacket; public class MessageGenerator { - private MessagePacket preparePacket(Message message) { + private MessagePacket preparePacket(Message message, boolean addDelay) { Conversation conversation = message.getConversation(); Account account = conversation.getAccount(); MessagePacket packet = new MessagePacket(); @@ -21,15 +26,30 @@ public class MessageGenerator { } packet.setFrom(account.getFullJid()); packet.setId(message.getUuid()); + if (addDelay) { + addDelay(packet,message.getTimeSent()); + } return packet; } + private void addDelay(MessagePacket packet, long timestamp) { + final SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); + 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 { + 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); } - MessagePacket packet = preparePacket(message); + MessagePacket packet = preparePacket(message,addDelay); packet.addChild("private", "urn:xmpp:carbons:2"); packet.addChild("no-copy", "urn:xmpp:hints"); packet.setBody(otrSession.transformSending(message @@ -38,13 +58,21 @@ public class MessageGenerator { } public MessagePacket generateChat(Message message) { - MessagePacket packet = preparePacket(message); + 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) { - MessagePacket packet = preparePacket(message); + 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( diff --git a/src/eu/siacs/conversations/parser/AbstractParser.java b/src/eu/siacs/conversations/parser/AbstractParser.java index 2bd839f1f..c06a5cbd9 100644 --- a/src/eu/siacs/conversations/parser/AbstractParser.java +++ b/src/eu/siacs/conversations/parser/AbstractParser.java @@ -4,6 +4,7 @@ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; +import android.util.Log; import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Contact; import eu.siacs.conversations.services.XmppConnectionService; @@ -18,19 +19,34 @@ public abstract class AbstractParser { } protected long getTimestamp(Element packet) { + long now = System.currentTimeMillis(); if (packet.hasChild("delay")) { try { String stamp = packet.findChild("delay").getAttribute( "stamp"); stamp = stamp.replace("Z", "+0000"); - Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ") - .parse(stamp); - return date.getTime(); + if (stamp.contains(".")) { + Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ") + .parse(stamp); + if (now