mirror of
https://github.com/moparisthebest/Conversations
synced 2024-12-26 09:19:07 -05:00
fixed #220
This commit is contained in:
parent
fafc5306d7
commit
1374fed31b
@ -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(
|
||||
|
@ -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<date.getTime()) {
|
||||
return now;
|
||||
} else {
|
||||
return date.getTime();
|
||||
}
|
||||
} else {
|
||||
Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
|
||||
.parse(stamp);
|
||||
if (now<date.getTime()) {
|
||||
return now;
|
||||
} else {
|
||||
return date.getTime();
|
||||
}
|
||||
}
|
||||
} catch (ParseException e) {
|
||||
return System.currentTimeMillis();
|
||||
return now;
|
||||
}
|
||||
} else {
|
||||
return System.currentTimeMillis();
|
||||
return now;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -18,20 +18,24 @@ public class MessageParser extends AbstractParser {
|
||||
}
|
||||
|
||||
public Message parseChat(MessagePacket packet, Account account) {
|
||||
Log.d("xmppService","received message: "+packet.toString());
|
||||
String[] fromParts = packet.getFrom().split("/");
|
||||
Conversation conversation = mXmppConnectionService
|
||||
.findOrCreateConversation(account, fromParts[0], false);
|
||||
conversation.setLatestMarkableMessageId(getMarkableMessageId(packet));
|
||||
updateLastseen(packet, account,true);
|
||||
String pgpBody = getPgpBody(packet);
|
||||
Message finishedMessage;
|
||||
if (pgpBody != null) {
|
||||
return new Message(conversation, packet.getFrom(), pgpBody,
|
||||
finishedMessage = new Message(conversation, packet.getFrom(), pgpBody,
|
||||
Message.ENCRYPTION_PGP, Message.STATUS_RECIEVED);
|
||||
} else {
|
||||
return new Message(conversation, packet.getFrom(),
|
||||
finishedMessage = new Message(conversation, packet.getFrom(),
|
||||
packet.getBody(), Message.ENCRYPTION_NONE,
|
||||
Message.STATUS_RECIEVED);
|
||||
}
|
||||
finishedMessage.setTime(getTimestamp(packet));
|
||||
return finishedMessage;
|
||||
}
|
||||
|
||||
public Message parseOtrChat(MessagePacket packet, Account account) {
|
||||
|
@ -763,9 +763,9 @@ public class XmppConnectionService extends Service {
|
||||
if (message.getType() == Message.TYPE_TEXT) {
|
||||
MessagePacket packet = null;
|
||||
if (message.getEncryption() == Message.ENCRYPTION_NONE) {
|
||||
packet = mMessageGenerator.generateChat(message);
|
||||
packet = mMessageGenerator.generateChat(message,true);
|
||||
} else if ((message.getEncryption() == Message.ENCRYPTION_DECRYPTED)||(message.getEncryption() == Message.ENCRYPTION_PGP)) {
|
||||
packet = mMessageGenerator.generatePgpChat(message);
|
||||
packet = mMessageGenerator.generatePgpChat(message,true);
|
||||
} else if (message.getEncryption() == Message.ENCRYPTION_OTR) {
|
||||
Presences presences = message.getConversation().getContact()
|
||||
.getPresences();
|
||||
@ -1207,7 +1207,7 @@ public class XmppConnectionService extends Service {
|
||||
msg.setPresence(otrSession.getSessionID().getUserID());
|
||||
if (msg.getType() == Message.TYPE_TEXT) {
|
||||
try {
|
||||
MessagePacket outPacket = mMessageGenerator.generateOtrChat(msg);
|
||||
MessagePacket outPacket = mMessageGenerator.generateOtrChat(msg,true);
|
||||
msg.setStatus(Message.STATUS_SEND);
|
||||
databaseBackend.updateMessage(msg);
|
||||
account.getXmppConnection().sendMessagePacket(outPacket);
|
||||
|
@ -795,6 +795,7 @@ public class XmppConnection implements Runnable {
|
||||
}
|
||||
|
||||
public void sendMessagePacket(MessagePacket packet) {
|
||||
Log.d("xmppService",packet.toString());
|
||||
this.sendPacket(packet, null);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user