mirror of
https://github.com/moparisthebest/Conversations
synced 2024-11-24 17:52:17 -05:00
moved message packet creation into sperate class
This commit is contained in:
parent
f559bd14dd
commit
fafc5306d7
57
src/eu/siacs/conversations/generator/MessageGenerator.java
Normal file
57
src/eu/siacs/conversations/generator/MessageGenerator.java
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
package eu.siacs.conversations.generator;
|
||||||
|
|
||||||
|
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.xmpp.stanzas.MessagePacket;
|
||||||
|
|
||||||
|
public class MessageGenerator {
|
||||||
|
private MessagePacket preparePacket(Message message) {
|
||||||
|
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());
|
||||||
|
return packet;
|
||||||
|
}
|
||||||
|
|
||||||
|
public MessagePacket generateOtrChat(Message message) throws OtrException {
|
||||||
|
Session otrSession = message.getConversation().getOtrSession();
|
||||||
|
if (otrSession==null) {
|
||||||
|
throw new OtrException(null);
|
||||||
|
}
|
||||||
|
MessagePacket packet = preparePacket(message);
|
||||||
|
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) {
|
||||||
|
MessagePacket packet = preparePacket(message);
|
||||||
|
packet.setBody(message.getBody());
|
||||||
|
return packet;
|
||||||
|
}
|
||||||
|
|
||||||
|
public MessagePacket generatePgpChat(Message message) {
|
||||||
|
MessagePacket packet = preparePacket(message);
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
@ -21,6 +21,7 @@ import eu.siacs.conversations.entities.Message;
|
|||||||
import eu.siacs.conversations.entities.MucOptions;
|
import eu.siacs.conversations.entities.MucOptions;
|
||||||
import eu.siacs.conversations.entities.MucOptions.OnRenameListener;
|
import eu.siacs.conversations.entities.MucOptions.OnRenameListener;
|
||||||
import eu.siacs.conversations.entities.Presences;
|
import eu.siacs.conversations.entities.Presences;
|
||||||
|
import eu.siacs.conversations.generator.MessageGenerator;
|
||||||
import eu.siacs.conversations.parser.MessageParser;
|
import eu.siacs.conversations.parser.MessageParser;
|
||||||
import eu.siacs.conversations.parser.PresenceParser;
|
import eu.siacs.conversations.parser.PresenceParser;
|
||||||
import eu.siacs.conversations.persistance.DatabaseBackend;
|
import eu.siacs.conversations.persistance.DatabaseBackend;
|
||||||
@ -88,6 +89,7 @@ public class XmppConnectionService extends Service {
|
|||||||
|
|
||||||
private MessageParser mMessageParser = new MessageParser(this);
|
private MessageParser mMessageParser = new MessageParser(this);
|
||||||
private PresenceParser mPresenceParser = new PresenceParser(this);
|
private PresenceParser mPresenceParser = new PresenceParser(this);
|
||||||
|
private MessageGenerator mMessageGenerator = new MessageGenerator();
|
||||||
|
|
||||||
private List<Account> accounts;
|
private List<Account> accounts;
|
||||||
private List<Conversation> conversations = null;
|
private List<Conversation> conversations = null;
|
||||||
@ -685,42 +687,41 @@ public class XmppConnectionService extends Service {
|
|||||||
message.setStatus(Message.STATUS_WAITING);
|
message.setStatus(Message.STATUS_WAITING);
|
||||||
} else if (conv.hasValidOtrSession()
|
} else if (conv.hasValidOtrSession()
|
||||||
&& conv.getOtrSession().getSessionStatus() == SessionStatus.ENCRYPTED) {
|
&& conv.getOtrSession().getSessionStatus() == SessionStatus.ENCRYPTED) {
|
||||||
packet = prepareMessagePacket(account, message,
|
message.setPresence(conv.getOtrSession().getSessionID().getUserID());
|
||||||
conv.getOtrSession());
|
try {
|
||||||
|
packet = mMessageGenerator.generateOtrChat(message);
|
||||||
send = true;
|
send = true;
|
||||||
message.setStatus(Message.STATUS_SEND);
|
message.setStatus(Message.STATUS_SEND);
|
||||||
|
} catch (OtrException e) {
|
||||||
|
Log.e(LOGTAG,"error generating otr packet");
|
||||||
|
packet = null;
|
||||||
|
}
|
||||||
} else if (message.getPresence() == null) {
|
} else if (message.getPresence() == null) {
|
||||||
message.setStatus(Message.STATUS_WAITING);
|
message.setStatus(Message.STATUS_WAITING);
|
||||||
}
|
}
|
||||||
} else if (message.getEncryption() == Message.ENCRYPTION_PGP) {
|
} else if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
|
||||||
message.getConversation().endOtrIfNeeded();
|
message.getConversation().endOtrIfNeeded();
|
||||||
packet = prepareMessagePacket(account, message, null);
|
packet = mMessageGenerator.generatePgpChat(message);
|
||||||
packet.setBody("This is an XEP-0027 encryted message");
|
|
||||||
packet.addChild("x", "jabber:x:encrypted").setContent(
|
|
||||||
message.getEncryptedBody());
|
|
||||||
message.setStatus(Message.STATUS_SEND);
|
message.setStatus(Message.STATUS_SEND);
|
||||||
message.setEncryption(Message.ENCRYPTION_DECRYPTED);
|
|
||||||
send = true;
|
send = true;
|
||||||
} else {
|
} else {
|
||||||
message.getConversation().endOtrIfNeeded();
|
message.getConversation().endOtrIfNeeded();
|
||||||
// don't encrypt
|
|
||||||
if (message.getConversation().getMode() == Conversation.MODE_SINGLE) {
|
if (message.getConversation().getMode() == Conversation.MODE_SINGLE) {
|
||||||
message.setStatus(Message.STATUS_SEND);
|
message.setStatus(Message.STATUS_SEND);
|
||||||
}
|
}
|
||||||
packet = prepareMessagePacket(account, message, null);
|
packet = mMessageGenerator.generateChat(message);
|
||||||
send = true;
|
send = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
message.setStatus(Message.STATUS_WAITING);
|
message.setStatus(Message.STATUS_WAITING);
|
||||||
if (message.getType() == Message.TYPE_TEXT) {
|
if (message.getType() == Message.TYPE_TEXT) {
|
||||||
if (message.getEncryption() == Message.ENCRYPTION_PGP) {
|
if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
|
||||||
String pgpBody = message.getEncryptedBody();
|
String pgpBody = message.getEncryptedBody();
|
||||||
String decryptedBody = message.getBody();
|
String decryptedBody = message.getBody();
|
||||||
message.setBody(pgpBody);
|
message.setBody(pgpBody);
|
||||||
databaseBackend.createMessage(message);
|
databaseBackend.createMessage(message);
|
||||||
saveInDb = false;
|
saveInDb = false;
|
||||||
message.setEncryption(Message.ENCRYPTION_DECRYPTED);
|
|
||||||
message.setBody(decryptedBody);
|
message.setBody(decryptedBody);
|
||||||
} else if (message.getEncryption() == Message.ENCRYPTION_OTR) {
|
} else if (message.getEncryption() == Message.ENCRYPTION_OTR) {
|
||||||
if (conv.hasValidOtrSession()) {
|
if (conv.hasValidOtrSession()) {
|
||||||
@ -762,21 +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 = prepareMessagePacket(account, message, null);
|
packet = mMessageGenerator.generateChat(message);
|
||||||
} else if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
|
} else if ((message.getEncryption() == Message.ENCRYPTION_DECRYPTED)||(message.getEncryption() == Message.ENCRYPTION_PGP)) {
|
||||||
packet = prepareMessagePacket(account, message, null);
|
packet = mMessageGenerator.generatePgpChat(message);
|
||||||
packet.setBody("This is an XEP-0027 encryted message");
|
|
||||||
if (message.getEncryptedBody() == null) {
|
|
||||||
markMessage(message, Message.STATUS_SEND_FAILED);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
packet.addChild("x", "jabber:x:encrypted").setContent(
|
|
||||||
message.getEncryptedBody());
|
|
||||||
} else if (message.getEncryption() == Message.ENCRYPTION_PGP) {
|
|
||||||
packet = prepareMessagePacket(account, message, null);
|
|
||||||
packet.setBody("This is an XEP-0027 encryted message");
|
|
||||||
packet.addChild("x", "jabber:x:encrypted").setContent(
|
|
||||||
message.getBody());
|
|
||||||
} 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();
|
||||||
@ -817,41 +806,6 @@ public class XmppConnectionService extends Service {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public MessagePacket prepareMessagePacket(Account account, Message message,
|
|
||||||
Session otrSession) {
|
|
||||||
MessagePacket packet = new MessagePacket();
|
|
||||||
if (message.getConversation().getMode() == Conversation.MODE_SINGLE) {
|
|
||||||
packet.setType(MessagePacket.TYPE_CHAT);
|
|
||||||
packet.setFrom(account.getFullJid());
|
|
||||||
if (otrSession != null) {
|
|
||||||
try {
|
|
||||||
packet.setBody(otrSession.transformSending(message
|
|
||||||
.getBody()));
|
|
||||||
} catch (OtrException e) {
|
|
||||||
Log.d(LOGTAG,
|
|
||||||
account.getJid()
|
|
||||||
+ ": could not encrypt message to "
|
|
||||||
+ message.getCounterpart());
|
|
||||||
}
|
|
||||||
packet.addChild("private", "urn:xmpp:carbons:2");
|
|
||||||
packet.addChild("no-copy", "urn:xmpp:hints");
|
|
||||||
packet.setTo(otrSession.getSessionID().getAccountID() + "/"
|
|
||||||
+ otrSession.getSessionID().getUserID());
|
|
||||||
} else {
|
|
||||||
packet.setBody(message.getBody());
|
|
||||||
packet.setTo(message.getCounterpart());
|
|
||||||
}
|
|
||||||
packet.addChild("markable", "urn:xmpp:chat-markers:0");
|
|
||||||
} else if (message.getConversation().getMode() == Conversation.MODE_MULTI) {
|
|
||||||
packet.setType(MessagePacket.TYPE_GROUPCHAT);
|
|
||||||
packet.setBody(message.getBody());
|
|
||||||
packet.setTo(message.getCounterpart().split("/")[0]);
|
|
||||||
packet.setFrom(account.getJid());
|
|
||||||
}
|
|
||||||
packet.setId(message.getUuid());
|
|
||||||
return packet;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void fetchRosterFromServer(Account account) {
|
public void fetchRosterFromServer(Account account) {
|
||||||
IqPacket iqPacket = new IqPacket(IqPacket.TYPE_GET);
|
IqPacket iqPacket = new IqPacket(IqPacket.TYPE_GET);
|
||||||
if (!"".equals(account.getRosterVersion())) {
|
if (!"".equals(account.getRosterVersion())) {
|
||||||
@ -1252,11 +1206,14 @@ public class XmppConnectionService extends Service {
|
|||||||
&& (msg.getEncryption() == Message.ENCRYPTION_OTR)) {
|
&& (msg.getEncryption() == Message.ENCRYPTION_OTR)) {
|
||||||
msg.setPresence(otrSession.getSessionID().getUserID());
|
msg.setPresence(otrSession.getSessionID().getUserID());
|
||||||
if (msg.getType() == Message.TYPE_TEXT) {
|
if (msg.getType() == Message.TYPE_TEXT) {
|
||||||
MessagePacket outPacket = prepareMessagePacket(account,
|
try {
|
||||||
msg, otrSession);
|
MessagePacket outPacket = mMessageGenerator.generateOtrChat(msg);
|
||||||
msg.setStatus(Message.STATUS_SEND);
|
msg.setStatus(Message.STATUS_SEND);
|
||||||
databaseBackend.updateMessage(msg);
|
databaseBackend.updateMessage(msg);
|
||||||
account.getXmppConnection().sendMessagePacket(outPacket);
|
account.getXmppConnection().sendMessagePacket(outPacket);
|
||||||
|
} catch (OtrException e) {
|
||||||
|
Log.e(LOGTAG,"error creating otr packet");
|
||||||
|
}
|
||||||
} else if (msg.getType() == Message.TYPE_IMAGE) {
|
} else if (msg.getType() == Message.TYPE_IMAGE) {
|
||||||
mJingleConnectionManager.createNewConnection(msg);
|
mJingleConnectionManager.createNewConnection(msg);
|
||||||
}
|
}
|
||||||
|
@ -967,6 +967,7 @@ public class ConversationActivity extends XmppActivity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void success(Message message) {
|
public void success(Message message) {
|
||||||
|
message.setEncryption(Message.ENCRYPTION_DECRYPTED);
|
||||||
xmppConnectionService.sendMessage(message);
|
xmppConnectionService.sendMessage(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user