aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/eu
diff options
context:
space:
mode:
authorDaniel Gultsch <daniel@gultsch.de>2015-07-05 11:59:38 +0200
committerDaniel Gultsch <daniel@gultsch.de>2015-07-05 11:59:38 +0200
commitbef731a3c8ec2d4cefb70e6a7fe4232825a1d876 (patch)
treeeb4043314cb5573f672c1c21d7f03a66132e6039 /src/main/java/eu
parent148955a7136b65b6a070706e5cf561db68fa6616 (diff)
refactored sendMessage and merged with resendMessage
Diffstat (limited to 'src/main/java/eu')
-rw-r--r--src/main/java/eu/siacs/conversations/entities/Message.java20
-rw-r--r--src/main/java/eu/siacs/conversations/services/XmppConnectionService.java245
2 files changed, 117 insertions, 148 deletions
diff --git a/src/main/java/eu/siacs/conversations/entities/Message.java b/src/main/java/eu/siacs/conversations/entities/Message.java
index ac5a9d560..078a45659 100644
--- a/src/main/java/eu/siacs/conversations/entities/Message.java
+++ b/src/main/java/eu/siacs/conversations/entities/Message.java
@@ -435,6 +435,26 @@ public class Message extends AbstractEntity {
return (status > STATUS_RECEIVED || (contact != null && contact.trusted()));
}
+ public boolean fixCounterpart() {
+ Presences presences = conversation.getContact().getPresences();
+ if (counterpart != null && presences.has(counterpart.getResourcepart())) {
+ return true;
+ } else if (presences.size() >= 1) {
+ try {
+ counterpart = Jid.fromParts(conversation.getJid().getLocalpart(),
+ conversation.getJid().getDomainpart(),
+ presences.asStringArray()[0]);
+ return true;
+ } catch (InvalidJidException e) {
+ counterpart = null;
+ return false;
+ }
+ } else {
+ counterpart = null;
+ return false;
+ }
+ }
+
public enum Decision {
MUST,
SHOULD,
diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java
index 3b64872e5..2fead2380 100644
--- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java
+++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java
@@ -28,6 +28,7 @@ import android.util.LruCache;
import net.java.otr4j.OtrException;
import net.java.otr4j.session.Session;
import net.java.otr4j.session.SessionID;
+import net.java.otr4j.session.SessionImpl;
import net.java.otr4j.session.SessionStatus;
import org.openintents.openpgp.util.OpenPgpApi;
@@ -684,113 +685,126 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
}
public void sendMessage(final Message message) {
+ sendMessage(message,false);
+ }
+
+ private void sendMessage(final Message message, final boolean resend) {
final Account account = message.getConversation().getAccount();
+ final Conversation conversation = message.getConversation();
account.deactivateGracePeriod();
- final Conversation conv = message.getConversation();
MessagePacket packet = null;
boolean saveInDb = true;
- boolean send = false;
- if (account.getStatus() == Account.State.ONLINE
- && account.getXmppConnection() != null) {
- if (message.needsUploading()) {
- if (message.getCounterpart() != null || account.httpUploadAvailable()) {
- if (message.getEncryption() == Message.ENCRYPTION_OTR) {
- if (!conv.hasValidOtrSession()) {
- conv.startOtrSession(message.getCounterpart().getResourcepart(),true);
- message.setStatus(Message.STATUS_WAITING);
- } else if (conv.hasValidOtrSession()
- && conv.getOtrSession().getSessionStatus() == SessionStatus.ENCRYPTED) {
- mJingleConnectionManager.createNewConnection(message);
+ message.setStatus(Message.STATUS_WAITING);
+
+ if (!resend && message.getEncryption() != Message.ENCRYPTION_OTR) {
+ message.getConversation().endOtrIfNeeded();
+ message.getConversation().findUnsentMessagesWithOtrEncryption(new Conversation.OnMessageFound() {
+ @Override
+ public void onMessageFound(Message message) {
+ markMessage(message,Message.STATUS_SEND_FAILED);
+ }
+ });
+ }
+
+ if (account.isOnlineAndConnected()) {
+ switch (message.getEncryption()) {
+ case Message.ENCRYPTION_NONE:
+ if (message.needsUploading()) {
+ if (account.httpUploadAvailable() || message.fixCounterpart()) {
+ this.sendFileMessage(message);
+ } else {
+ break;
}
} else {
- this.sendFileMessage(message);
-
+ packet = mMessageGenerator.generateChat(message,resend);
}
- } else {
- if (message.getEncryption() == Message.ENCRYPTION_OTR) {
- conv.startOtrIfNeeded();
- }
- message.setStatus(Message.STATUS_WAITING);
- }
- } else {
- if (message.getEncryption() == Message.ENCRYPTION_OTR) {
- if (!conv.hasValidOtrSession() && (message.getCounterpart() != null)) {
- conv.startOtrSession(message.getCounterpart().getResourcepart(), true);
- message.setStatus(Message.STATUS_WAITING);
- } else if (conv.hasValidOtrSession()) {
- if (conv.getOtrSession().getSessionStatus() == SessionStatus.ENCRYPTED) {
- packet = mMessageGenerator.generateOtrChat(message);
- send = true;
+ break;
+ case Message.ENCRYPTION_PGP:
+ case Message.ENCRYPTION_DECRYPTED:
+ if (message.needsUploading()) {
+ if (account.httpUploadAvailable() || message.fixCounterpart()) {
+ this.sendFileMessage(message);
} else {
- message.setStatus(Message.STATUS_WAITING);
- conv.startOtrIfNeeded();
+ break;
}
} else {
- message.setStatus(Message.STATUS_WAITING);
+ packet = mMessageGenerator.generatePgpChat(message,resend);
}
- } else if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
- message.getConversation().endOtrIfNeeded();
- message.getConversation().findUnsentMessagesWithOtrEncryption(new Conversation.OnMessageFound() {
- @Override
- public void onMessageFound(Message message) {
- markMessage(message,Message.STATUS_SEND_FAILED);
+ break;
+ case Message.ENCRYPTION_OTR:
+ SessionImpl otrSession = conversation.getOtrSession();
+ if (otrSession != null && otrSession.getSessionStatus() == SessionStatus.ENCRYPTED) {
+ try {
+ message.setCounterpart(Jid.fromSessionID(otrSession.getSessionID()));
+ } catch (InvalidJidException e) {
+ break;
}
- });
- packet = mMessageGenerator.generatePgpChat(message);
- send = true;
- } else {
- message.getConversation().endOtrIfNeeded();
- message.getConversation().findUnsentMessagesWithOtrEncryption(new Conversation.OnMessageFound() {
- @Override
- public void onMessageFound(Message message) {
- markMessage(message,Message.STATUS_SEND_FAILED);
+ if (message.needsUploading()) {
+ mJingleConnectionManager.createNewConnection(message);
+ } else {
+ packet = mMessageGenerator.generateOtrChat(message,resend);
}
- });
- packet = mMessageGenerator.generateChat(message);
- send = true;
+ } else if (otrSession == null) {
+ if (message.fixCounterpart()) {
+ conversation.startOtrSession(message.getCounterpart().getResourcepart(), true);
+ } else {
+ break;
+ }
+ }
+ break;
+ }
+ if (packet != null) {
+ if (account.getXmppConnection().getFeatures().sm() || conversation.getMode() == Conversation.MODE_MULTI) {
+ message.setStatus(Message.STATUS_UNSEND);
+ } else {
+ message.setStatus(Message.STATUS_SEND);
}
}
- if (!account.getXmppConnection().getFeatures().sm()
- && conv.getMode() != Conversation.MODE_MULTI) {
- message.setStatus(Message.STATUS_SEND);
- }
} else {
- message.setStatus(Message.STATUS_WAITING);
- if (message.getType() == Message.TYPE_TEXT) {
- if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
- String pgpBody = message.getEncryptedBody();
- String decryptedBody = message.getBody();
- message.setBody(pgpBody);
- message.setEncryption(Message.ENCRYPTION_PGP);
- databaseBackend.createMessage(message);
- saveInDb = false;
- message.setBody(decryptedBody);
- message.setEncryption(Message.ENCRYPTION_DECRYPTED);
- } else if (message.getEncryption() == Message.ENCRYPTION_OTR) {
- if (!conv.hasValidOtrSession()
- && message.getCounterpart() != null) {
- conv.startOtrSession(message.getCounterpart().getResourcepart(), false);
- }
- }
+ switch(message.getEncryption()) {
+ case Message.ENCRYPTION_DECRYPTED:
+ if (!message.needsUploading()) {
+ String pgpBody = message.getEncryptedBody();
+ String decryptedBody = message.getBody();
+ message.setBody(pgpBody);
+ message.setEncryption(Message.ENCRYPTION_PGP);
+ databaseBackend.createMessage(message);
+ saveInDb = false;
+ message.setBody(decryptedBody);
+ message.setEncryption(Message.ENCRYPTION_DECRYPTED);
+ }
+ break;
+ case Message.ENCRYPTION_OTR:
+ if (!conversation.hasValidOtrSession() && message.getCounterpart() != null) {
+ conversation.startOtrSession(message.getCounterpart().getResourcepart(), false);
+ }
+ break;
}
-
}
- conv.add(message);
- if (saveInDb) {
- if (message.getEncryption() == Message.ENCRYPTION_NONE
- || saveEncryptedMessages()) {
+
+ if (resend) {
+ if (packet != null) {
+ if (account.getXmppConnection().getFeatures().sm() || conversation.getMode() == Conversation.MODE_MULTI) {
+ markMessage(message,Message.STATUS_UNSEND);
+ } else {
+ markMessage(message,Message.STATUS_SEND);
+ }
+ }
+ } else {
+ conversation.add(message);
+ if (saveInDb && (message.getEncryption() == Message.ENCRYPTION_NONE || saveEncryptedMessages())) {
databaseBackend.createMessage(message);
- }
+ }
+ updateConversationUi();
}
- if ((send) && (packet != null)) {
- if (conv.setOutgoingChatState(Config.DEFAULT_CHATSTATE)) {
+ if (packet != null) {
+ if (conversation.setOutgoingChatState(Config.DEFAULT_CHATSTATE)) {
if (this.sendChatStates()) {
- packet.addChild(ChatState.toElement(conv.getOutgoingChatState()));
+ packet.addChild(ChatState.toElement(conversation.getOutgoingChatState()));
}
}
sendMessagePacket(account, packet);
}
- updateConversationUi();
}
private void sendUnsentMessages(final Conversation conversation) {
@@ -804,72 +818,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
}
public void resendMessage(final Message message) {
- Account account = message.getConversation().getAccount();
- MessagePacket packet = null;
- if (message.getEncryption() == Message.ENCRYPTION_OTR) {
- Presences presences = message.getConversation().getContact().getPresences();
- if (!message.getConversation().hasValidOtrSession()) {
- if ((message.getCounterpart() != null)
- && (presences.has(message.getCounterpart().getResourcepart()))) {
- message.getConversation().startOtrSession(message.getCounterpart().getResourcepart(), true);
- } else {
- if (presences.size() == 1) {
- String presence = presences.asStringArray()[0];
- message.getConversation().startOtrSession(presence, true);
- }
- }
- } else {
- if (message.getConversation().getOtrSession().getSessionStatus() == SessionStatus.ENCRYPTED) {
- try {
- message.setCounterpart(Jid.fromSessionID(message.getConversation().getOtrSession().getSessionID()));
- if (message.needsUploading()) {
- mJingleConnectionManager.createNewConnection(message);
- } else {
- packet = mMessageGenerator.generateOtrChat(message, true);
- }
- } catch (final InvalidJidException ignored) {
-
- }
- }
- }
- } else if (message.needsUploading()) {
- Contact contact = message.getConversation().getContact();
- Presences presences = contact.getPresences();
- if (account.httpUploadAvailable() || (message.getCounterpart() != null && presences.has(message.getCounterpart().getResourcepart()))) {
- this.sendFileMessage(message);
- } else {
- if (presences.size() == 1) {
- String presence = presences.asStringArray()[0];
- try {
- message.setCounterpart(Jid.fromParts(contact.getJid().getLocalpart(), contact.getJid().getDomainpart(), presence));
- } catch (InvalidJidException e) {
- return;
- }
- this.sendFileMessage(message);
- }
- }
- } else {
- if (message.getEncryption() == Message.ENCRYPTION_NONE) {
- packet = mMessageGenerator.generateChat(message, true);
- } else if ((message.getEncryption() == Message.ENCRYPTION_DECRYPTED)
- || (message.getEncryption() == Message.ENCRYPTION_PGP)) {
- packet = mMessageGenerator.generatePgpChat(message, true);
- }
- }
- if (packet != null) {
- if (!account.getXmppConnection().getFeatures().sm()
- && message.getConversation().getMode() != Conversation.MODE_MULTI) {
- markMessage(message, Message.STATUS_SEND);
- } else {
- markMessage(message, Message.STATUS_UNSEND);
- }
- if (message.getConversation().setOutgoingChatState(Config.DEFAULT_CHATSTATE)) {
- if (this.sendChatStates()) {
- packet.addChild(ChatState.toElement(message.getConversation().getOutgoingChatState()));
- }
- }
- sendMessagePacket(account, packet);
- }
+ sendMessage(message,true);
}
public void fetchRosterFromServer(final Account account) {