diff options
author | iNPUTmice <daniel@gultsch.de> | 2014-06-22 17:24:47 +0200 |
---|---|---|
committer | iNPUTmice <daniel@gultsch.de> | 2014-06-22 17:24:47 +0200 |
commit | fafc5306d7856cb258dcacecf91366dfedf85f92 (patch) | |
tree | 2912c3f49f7d25631f7de3554ac9f7f06de48b53 /src/eu/siacs/conversations | |
parent | f559bd14dda81d3881e9d45c4a737b3dcdfb67f1 (diff) |
moved message packet creation into sperate class
Diffstat (limited to '')
3 files changed, 84 insertions, 69 deletions
diff --git a/src/eu/siacs/conversations/generator/MessageGenerator.java b/src/eu/siacs/conversations/generator/MessageGenerator.java new file mode 100644 index 00000000..57b21fcf --- /dev/null +++ b/src/eu/siacs/conversations/generator/MessageGenerator.java @@ -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; + } +} diff --git a/src/eu/siacs/conversations/services/XmppConnectionService.java b/src/eu/siacs/conversations/services/XmppConnectionService.java index 69a40d4c..b3cb8430 100644 --- a/src/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/eu/siacs/conversations/services/XmppConnectionService.java @@ -21,6 +21,7 @@ import eu.siacs.conversations.entities.Message; import eu.siacs.conversations.entities.MucOptions; import eu.siacs.conversations.entities.MucOptions.OnRenameListener; import eu.siacs.conversations.entities.Presences; +import eu.siacs.conversations.generator.MessageGenerator; import eu.siacs.conversations.parser.MessageParser; import eu.siacs.conversations.parser.PresenceParser; import eu.siacs.conversations.persistance.DatabaseBackend; @@ -88,6 +89,7 @@ public class XmppConnectionService extends Service { private MessageParser mMessageParser = new MessageParser(this); private PresenceParser mPresenceParser = new PresenceParser(this); + private MessageGenerator mMessageGenerator = new MessageGenerator(); private List<Account> accounts; private List<Conversation> conversations = null; @@ -685,42 +687,41 @@ public class XmppConnectionService extends Service { message.setStatus(Message.STATUS_WAITING); } else if (conv.hasValidOtrSession() && conv.getOtrSession().getSessionStatus() == SessionStatus.ENCRYPTED) { - packet = prepareMessagePacket(account, message, - conv.getOtrSession()); - send = true; - message.setStatus(Message.STATUS_SEND); + message.setPresence(conv.getOtrSession().getSessionID().getUserID()); + try { + packet = mMessageGenerator.generateOtrChat(message); + send = true; + message.setStatus(Message.STATUS_SEND); + } catch (OtrException e) { + Log.e(LOGTAG,"error generating otr packet"); + packet = null; + } } else if (message.getPresence() == null) { message.setStatus(Message.STATUS_WAITING); } - } else if (message.getEncryption() == Message.ENCRYPTION_PGP) { + } else if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) { message.getConversation().endOtrIfNeeded(); - packet = prepareMessagePacket(account, message, null); - packet.setBody("This is an XEP-0027 encryted message"); - packet.addChild("x", "jabber:x:encrypted").setContent( - message.getEncryptedBody()); + packet = mMessageGenerator.generatePgpChat(message); message.setStatus(Message.STATUS_SEND); - message.setEncryption(Message.ENCRYPTION_DECRYPTED); send = true; } else { message.getConversation().endOtrIfNeeded(); - // don't encrypt if (message.getConversation().getMode() == Conversation.MODE_SINGLE) { message.setStatus(Message.STATUS_SEND); } - packet = prepareMessagePacket(account, message, null); + packet = mMessageGenerator.generateChat(message); send = true; } } } else { message.setStatus(Message.STATUS_WAITING); if (message.getType() == Message.TYPE_TEXT) { - if (message.getEncryption() == Message.ENCRYPTION_PGP) { + if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) { String pgpBody = message.getEncryptedBody(); String decryptedBody = message.getBody(); message.setBody(pgpBody); databaseBackend.createMessage(message); saveInDb = false; - message.setEncryption(Message.ENCRYPTION_DECRYPTED); message.setBody(decryptedBody); } else if (message.getEncryption() == Message.ENCRYPTION_OTR) { if (conv.hasValidOtrSession()) { @@ -762,21 +763,9 @@ public class XmppConnectionService extends Service { if (message.getType() == Message.TYPE_TEXT) { MessagePacket packet = null; if (message.getEncryption() == Message.ENCRYPTION_NONE) { - packet = prepareMessagePacket(account, message, null); - } else if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) { - packet = prepareMessagePacket(account, message, null); - 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()); + packet = mMessageGenerator.generateChat(message); + } else if ((message.getEncryption() == Message.ENCRYPTION_DECRYPTED)||(message.getEncryption() == Message.ENCRYPTION_PGP)) { + packet = mMessageGenerator.generatePgpChat(message); } else if (message.getEncryption() == Message.ENCRYPTION_OTR) { Presences presences = message.getConversation().getContact() .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) { IqPacket iqPacket = new IqPacket(IqPacket.TYPE_GET); if (!"".equals(account.getRosterVersion())) { @@ -1252,11 +1206,14 @@ public class XmppConnectionService extends Service { && (msg.getEncryption() == Message.ENCRYPTION_OTR)) { msg.setPresence(otrSession.getSessionID().getUserID()); if (msg.getType() == Message.TYPE_TEXT) { - MessagePacket outPacket = prepareMessagePacket(account, - msg, otrSession); - msg.setStatus(Message.STATUS_SEND); - databaseBackend.updateMessage(msg); - account.getXmppConnection().sendMessagePacket(outPacket); + try { + MessagePacket outPacket = mMessageGenerator.generateOtrChat(msg); + msg.setStatus(Message.STATUS_SEND); + databaseBackend.updateMessage(msg); + account.getXmppConnection().sendMessagePacket(outPacket); + } catch (OtrException e) { + Log.e(LOGTAG,"error creating otr packet"); + } } else if (msg.getType() == Message.TYPE_IMAGE) { mJingleConnectionManager.createNewConnection(msg); } diff --git a/src/eu/siacs/conversations/ui/ConversationActivity.java b/src/eu/siacs/conversations/ui/ConversationActivity.java index 4e264df7..1e806d31 100644 --- a/src/eu/siacs/conversations/ui/ConversationActivity.java +++ b/src/eu/siacs/conversations/ui/ConversationActivity.java @@ -967,6 +967,7 @@ public class ConversationActivity extends XmppActivity { @Override public void success(Message message) { + message.setEncryption(Message.ENCRYPTION_DECRYPTED); xmppConnectionService.sendMessage(message); } |