fixed #220
This commit is contained in:
parent
fafc5306d7
commit
1374fed31b
|
@ -1,14 +1,19 @@
|
||||||
package eu.siacs.conversations.generator;
|
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.OtrException;
|
||||||
import net.java.otr4j.session.Session;
|
import net.java.otr4j.session.Session;
|
||||||
import eu.siacs.conversations.entities.Account;
|
import eu.siacs.conversations.entities.Account;
|
||||||
import eu.siacs.conversations.entities.Conversation;
|
import eu.siacs.conversations.entities.Conversation;
|
||||||
import eu.siacs.conversations.entities.Message;
|
import eu.siacs.conversations.entities.Message;
|
||||||
|
import eu.siacs.conversations.xml.Element;
|
||||||
import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
|
import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
|
||||||
|
|
||||||
public class MessageGenerator {
|
public class MessageGenerator {
|
||||||
private MessagePacket preparePacket(Message message) {
|
private MessagePacket preparePacket(Message message, boolean addDelay) {
|
||||||
Conversation conversation = message.getConversation();
|
Conversation conversation = message.getConversation();
|
||||||
Account account = conversation.getAccount();
|
Account account = conversation.getAccount();
|
||||||
MessagePacket packet = new MessagePacket();
|
MessagePacket packet = new MessagePacket();
|
||||||
|
@ -21,15 +26,30 @@ public class MessageGenerator {
|
||||||
}
|
}
|
||||||
packet.setFrom(account.getFullJid());
|
packet.setFrom(account.getFullJid());
|
||||||
packet.setId(message.getUuid());
|
packet.setId(message.getUuid());
|
||||||
|
if (addDelay) {
|
||||||
|
addDelay(packet,message.getTimeSent());
|
||||||
|
}
|
||||||
return packet;
|
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 {
|
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();
|
Session otrSession = message.getConversation().getOtrSession();
|
||||||
if (otrSession==null) {
|
if (otrSession==null) {
|
||||||
throw new OtrException(null);
|
throw new OtrException(null);
|
||||||
}
|
}
|
||||||
MessagePacket packet = preparePacket(message);
|
MessagePacket packet = preparePacket(message,addDelay);
|
||||||
packet.addChild("private", "urn:xmpp:carbons:2");
|
packet.addChild("private", "urn:xmpp:carbons:2");
|
||||||
packet.addChild("no-copy", "urn:xmpp:hints");
|
packet.addChild("no-copy", "urn:xmpp:hints");
|
||||||
packet.setBody(otrSession.transformSending(message
|
packet.setBody(otrSession.transformSending(message
|
||||||
|
@ -38,13 +58,21 @@ public class MessageGenerator {
|
||||||
}
|
}
|
||||||
|
|
||||||
public MessagePacket generateChat(Message message) {
|
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());
|
packet.setBody(message.getBody());
|
||||||
return packet;
|
return packet;
|
||||||
}
|
}
|
||||||
|
|
||||||
public MessagePacket generatePgpChat(Message message) {
|
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");
|
packet.setBody("This is an XEP-0027 encryted message");
|
||||||
if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
|
if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
|
||||||
packet.addChild("x", "jabber:x:encrypted").setContent(
|
packet.addChild("x", "jabber:x:encrypted").setContent(
|
||||||
|
|
|
@ -4,6 +4,7 @@ import java.text.ParseException;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
|
import android.util.Log;
|
||||||
import eu.siacs.conversations.entities.Account;
|
import eu.siacs.conversations.entities.Account;
|
||||||
import eu.siacs.conversations.entities.Contact;
|
import eu.siacs.conversations.entities.Contact;
|
||||||
import eu.siacs.conversations.services.XmppConnectionService;
|
import eu.siacs.conversations.services.XmppConnectionService;
|
||||||
|
@ -18,19 +19,34 @@ public abstract class AbstractParser {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected long getTimestamp(Element packet) {
|
protected long getTimestamp(Element packet) {
|
||||||
|
long now = System.currentTimeMillis();
|
||||||
if (packet.hasChild("delay")) {
|
if (packet.hasChild("delay")) {
|
||||||
try {
|
try {
|
||||||
String stamp = packet.findChild("delay").getAttribute(
|
String stamp = packet.findChild("delay").getAttribute(
|
||||||
"stamp");
|
"stamp");
|
||||||
stamp = stamp.replace("Z", "+0000");
|
stamp = stamp.replace("Z", "+0000");
|
||||||
Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
|
if (stamp.contains(".")) {
|
||||||
|
Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
|
||||||
.parse(stamp);
|
.parse(stamp);
|
||||||
|
if (now<date.getTime()) {
|
||||||
|
return now;
|
||||||
|
} else {
|
||||||
return date.getTime();
|
return date.getTime();
|
||||||
} catch (ParseException e) {
|
|
||||||
return System.currentTimeMillis();
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return System.currentTimeMillis();
|
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 now;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return now;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,20 +18,24 @@ public class MessageParser extends AbstractParser {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Message parseChat(MessagePacket packet, Account account) {
|
public Message parseChat(MessagePacket packet, Account account) {
|
||||||
|
Log.d("xmppService","received message: "+packet.toString());
|
||||||
String[] fromParts = packet.getFrom().split("/");
|
String[] fromParts = packet.getFrom().split("/");
|
||||||
Conversation conversation = mXmppConnectionService
|
Conversation conversation = mXmppConnectionService
|
||||||
.findOrCreateConversation(account, fromParts[0], false);
|
.findOrCreateConversation(account, fromParts[0], false);
|
||||||
conversation.setLatestMarkableMessageId(getMarkableMessageId(packet));
|
conversation.setLatestMarkableMessageId(getMarkableMessageId(packet));
|
||||||
updateLastseen(packet, account,true);
|
updateLastseen(packet, account,true);
|
||||||
String pgpBody = getPgpBody(packet);
|
String pgpBody = getPgpBody(packet);
|
||||||
|
Message finishedMessage;
|
||||||
if (pgpBody != null) {
|
if (pgpBody != null) {
|
||||||
return new Message(conversation, packet.getFrom(), pgpBody,
|
finishedMessage = new Message(conversation, packet.getFrom(), pgpBody,
|
||||||
Message.ENCRYPTION_PGP, Message.STATUS_RECIEVED);
|
Message.ENCRYPTION_PGP, Message.STATUS_RECIEVED);
|
||||||
} else {
|
} else {
|
||||||
return new Message(conversation, packet.getFrom(),
|
finishedMessage = new Message(conversation, packet.getFrom(),
|
||||||
packet.getBody(), Message.ENCRYPTION_NONE,
|
packet.getBody(), Message.ENCRYPTION_NONE,
|
||||||
Message.STATUS_RECIEVED);
|
Message.STATUS_RECIEVED);
|
||||||
}
|
}
|
||||||
|
finishedMessage.setTime(getTimestamp(packet));
|
||||||
|
return finishedMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Message parseOtrChat(MessagePacket packet, Account account) {
|
public Message parseOtrChat(MessagePacket packet, Account account) {
|
||||||
|
|
|
@ -763,9 +763,9 @@ public class XmppConnectionService extends Service {
|
||||||
if (message.getType() == Message.TYPE_TEXT) {
|
if (message.getType() == Message.TYPE_TEXT) {
|
||||||
MessagePacket packet = null;
|
MessagePacket packet = null;
|
||||||
if (message.getEncryption() == Message.ENCRYPTION_NONE) {
|
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)) {
|
} 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) {
|
} else if (message.getEncryption() == Message.ENCRYPTION_OTR) {
|
||||||
Presences presences = message.getConversation().getContact()
|
Presences presences = message.getConversation().getContact()
|
||||||
.getPresences();
|
.getPresences();
|
||||||
|
@ -1207,7 +1207,7 @@ public class XmppConnectionService extends Service {
|
||||||
msg.setPresence(otrSession.getSessionID().getUserID());
|
msg.setPresence(otrSession.getSessionID().getUserID());
|
||||||
if (msg.getType() == Message.TYPE_TEXT) {
|
if (msg.getType() == Message.TYPE_TEXT) {
|
||||||
try {
|
try {
|
||||||
MessagePacket outPacket = mMessageGenerator.generateOtrChat(msg);
|
MessagePacket outPacket = mMessageGenerator.generateOtrChat(msg,true);
|
||||||
msg.setStatus(Message.STATUS_SEND);
|
msg.setStatus(Message.STATUS_SEND);
|
||||||
databaseBackend.updateMessage(msg);
|
databaseBackend.updateMessage(msg);
|
||||||
account.getXmppConnection().sendMessagePacket(outPacket);
|
account.getXmppConnection().sendMessagePacket(outPacket);
|
||||||
|
|
|
@ -795,6 +795,7 @@ public class XmppConnection implements Runnable {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sendMessagePacket(MessagePacket packet) {
|
public void sendMessagePacket(MessagePacket packet) {
|
||||||
|
Log.d("xmppService",packet.toString());
|
||||||
this.sendPacket(packet, null);
|
this.sendPacket(packet, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue