synchronize message body changes for message correction

This commit is contained in:
Daniel Gultsch 2016-06-16 11:47:40 +02:00
parent f8b1e8098c
commit 1eb776f39c
2 changed files with 72 additions and 68 deletions

View File

@ -104,57 +104,28 @@ public class PgpDecryptionService {
} }
private void executeApi(Message message) { private void executeApi(Message message) {
Intent params = new Intent(); synchronized (message) {
params.setAction(OpenPgpApi.ACTION_DECRYPT_VERIFY); Intent params = new Intent();
if (message.getType() == Message.TYPE_TEXT) { params.setAction(OpenPgpApi.ACTION_DECRYPT_VERIFY);
InputStream is = new ByteArrayInputStream(message.getBody().getBytes()); if (message.getType() == Message.TYPE_TEXT) {
final OutputStream os = new ByteArrayOutputStream(); InputStream is = new ByteArrayInputStream(message.getBody().getBytes());
Intent result = getOpenPgpApi().executeApi(params, is, os); final OutputStream os = new ByteArrayOutputStream();
switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
case OpenPgpApi.RESULT_CODE_SUCCESS:
try {
os.flush();
message.setBody(os.toString());
message.setEncryption(Message.ENCRYPTION_DECRYPTED);
final HttpConnectionManager manager = mXmppConnectionService.getHttpConnectionManager();
if (message.trusted()
&& message.treatAsDownloadable() != Message.Decision.NEVER
&& manager.getAutoAcceptFileSize() > 0) {
manager.createNewDownloadConnection(message);
}
} catch (IOException e) {
message.setEncryption(Message.ENCRYPTION_DECRYPTION_FAILED);
}
mXmppConnectionService.updateMessage(message);
break;
case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
synchronized (PgpDecryptionService.this) {
messages.addFirst(message);
currentMessage = null;
storePendingIntent((PendingIntent) result.getParcelableExtra(OpenPgpApi.RESULT_INTENT));
}
break;
case OpenPgpApi.RESULT_CODE_ERROR:
message.setEncryption(Message.ENCRYPTION_DECRYPTION_FAILED);
mXmppConnectionService.updateMessage(message);
break;
}
} else if (message.getType() == Message.TYPE_IMAGE || message.getType() == Message.TYPE_FILE) {
try {
final DownloadableFile inputFile = mXmppConnectionService.getFileBackend().getFile(message, false);
final DownloadableFile outputFile = mXmppConnectionService.getFileBackend().getFile(message, true);
outputFile.getParentFile().mkdirs();
outputFile.createNewFile();
InputStream is = new FileInputStream(inputFile);
OutputStream os = new FileOutputStream(outputFile);
Intent result = getOpenPgpApi().executeApi(params, is, os); Intent result = getOpenPgpApi().executeApi(params, is, os);
switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) { switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
case OpenPgpApi.RESULT_CODE_SUCCESS: case OpenPgpApi.RESULT_CODE_SUCCESS:
URL url = message.getFileParams().url; try {
mXmppConnectionService.getFileBackend().updateFileParams(message,url); os.flush();
message.setEncryption(Message.ENCRYPTION_DECRYPTED); message.setBody(os.toString());
inputFile.delete(); message.setEncryption(Message.ENCRYPTION_DECRYPTED);
mXmppConnectionService.getFileBackend().updateMediaScanner(outputFile); final HttpConnectionManager manager = mXmppConnectionService.getHttpConnectionManager();
if (message.trusted()
&& message.treatAsDownloadable() != Message.Decision.NEVER
&& manager.getAutoAcceptFileSize() > 0) {
manager.createNewDownloadConnection(message);
}
} catch (IOException e) {
message.setEncryption(Message.ENCRYPTION_DECRYPTION_FAILED);
}
mXmppConnectionService.updateMessage(message); mXmppConnectionService.updateMessage(message);
break; break;
case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED: case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
@ -169,9 +140,40 @@ public class PgpDecryptionService {
mXmppConnectionService.updateMessage(message); mXmppConnectionService.updateMessage(message);
break; break;
} }
} catch (final IOException e) { } else if (message.getType() == Message.TYPE_IMAGE || message.getType() == Message.TYPE_FILE) {
message.setEncryption(Message.ENCRYPTION_DECRYPTION_FAILED); try {
mXmppConnectionService.updateMessage(message); final DownloadableFile inputFile = mXmppConnectionService.getFileBackend().getFile(message, false);
final DownloadableFile outputFile = mXmppConnectionService.getFileBackend().getFile(message, true);
outputFile.getParentFile().mkdirs();
outputFile.createNewFile();
InputStream is = new FileInputStream(inputFile);
OutputStream os = new FileOutputStream(outputFile);
Intent result = getOpenPgpApi().executeApi(params, is, os);
switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
case OpenPgpApi.RESULT_CODE_SUCCESS:
URL url = message.getFileParams().url;
mXmppConnectionService.getFileBackend().updateFileParams(message, url);
message.setEncryption(Message.ENCRYPTION_DECRYPTED);
inputFile.delete();
mXmppConnectionService.getFileBackend().updateMediaScanner(outputFile);
mXmppConnectionService.updateMessage(message);
break;
case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
synchronized (PgpDecryptionService.this) {
messages.addFirst(message);
currentMessage = null;
storePendingIntent((PendingIntent) result.getParcelableExtra(OpenPgpApi.RESULT_INTENT));
}
break;
case OpenPgpApi.RESULT_CODE_ERROR:
message.setEncryption(Message.ENCRYPTION_DECRYPTION_FAILED);
mXmppConnectionService.updateMessage(message);
break;
}
} catch (final IOException e) {
message.setEncryption(Message.ENCRYPTION_DECRYPTION_FAILED);
mXmppConnectionService.updateMessage(message);
}
} }
} }
notifyIfPending(message); notifyIfPending(message);

View File

@ -468,23 +468,25 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
&& replacedMessage.getTrueCounterpart().equals(message.getTrueCounterpart()); && replacedMessage.getTrueCounterpart().equals(message.getTrueCounterpart());
if (fingerprintsMatch && (trueCountersMatch || !conversationMultiMode)) { if (fingerprintsMatch && (trueCountersMatch || !conversationMultiMode)) {
Log.d(Config.LOGTAG, "replaced message '" + replacedMessage.getBody() + "' with '" + message.getBody() + "'"); Log.d(Config.LOGTAG, "replaced message '" + replacedMessage.getBody() + "' with '" + message.getBody() + "'");
final String uuid = replacedMessage.getUuid(); synchronized (replacedMessage) {
replacedMessage.setUuid(UUID.randomUUID().toString()); final String uuid = replacedMessage.getUuid();
replacedMessage.setBody(message.getBody()); replacedMessage.setUuid(UUID.randomUUID().toString());
replacedMessage.setEdited(replacedMessage.getRemoteMsgId()); replacedMessage.setBody(message.getBody());
replacedMessage.setRemoteMsgId(remoteMsgId); replacedMessage.setEdited(replacedMessage.getRemoteMsgId());
replacedMessage.setEncryption(message.getEncryption()); replacedMessage.setRemoteMsgId(remoteMsgId);
if (replacedMessage.getStatus() == Message.STATUS_RECEIVED) { replacedMessage.setEncryption(message.getEncryption());
replacedMessage.markUnread(); if (replacedMessage.getStatus() == Message.STATUS_RECEIVED) {
} replacedMessage.markUnread();
mXmppConnectionService.updateMessage(replacedMessage, uuid); }
mXmppConnectionService.getNotificationService().updateNotification(false); mXmppConnectionService.updateMessage(replacedMessage, uuid);
if (mXmppConnectionService.confirmMessages() && remoteMsgId != null && !isForwarded && !isTypeGroupChat) { mXmppConnectionService.getNotificationService().updateNotification(false);
sendMessageReceipts(account, packet); if (mXmppConnectionService.confirmMessages() && remoteMsgId != null && !isForwarded && !isTypeGroupChat) {
} sendMessageReceipts(account, packet);
if (replacedMessage.getEncryption() == Message.ENCRYPTION_PGP) { }
conversation.getAccount().getPgpDecryptionService().discard(replacedMessage); if (replacedMessage.getEncryption() == Message.ENCRYPTION_PGP) {
conversation.getAccount().getPgpDecryptionService().decrypt(replacedMessage, false); conversation.getAccount().getPgpDecryptionService().discard(replacedMessage);
conversation.getAccount().getPgpDecryptionService().decrypt(replacedMessage, false);
}
} }
return; return;
} else { } else {