Refactoring

This commit is contained in:
M66B 2020-04-19 13:21:07 +02:00
parent 89dc0a68c8
commit e178a39762
1 changed files with 62 additions and 53 deletions

View File

@ -194,6 +194,7 @@ import java.util.Objects;
import java.util.Properties; import java.util.Properties;
import javax.mail.Address; import javax.mail.Address;
import javax.mail.MessagingException;
import javax.mail.Session; import javax.mail.Session;
import javax.mail.internet.InternetAddress; import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage;
@ -5419,11 +5420,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
if (message == null) if (message == null)
return null; return null;
InputStream is = null;
X509Certificate result = null; X509Certificate result = null;
String alias = args.getString("alias");
boolean duplicate = args.getBoolean("duplicate");
if (EntityMessage.SMIME_SIGNONLY.equals(type)) { if (EntityMessage.SMIME_SIGNONLY.equals(type)) {
// Get content/signature // Get content/signature
boolean sdata = false; boolean sdata = false;
@ -5452,6 +5449,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
throw new IllegalArgumentException("Signature missing"); throw new IllegalArgumentException("Signature missing");
// Build signed data // Build signed data
InputStream is = null;
FileInputStream fis = new FileInputStream(signature); FileInputStream fis = new FileInputStream(signature);
CMSSignedData signedData; CMSSignedData signedData;
if (sdata) { if (sdata) {
@ -5638,8 +5636,12 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
if (result != null) if (result != null)
break; break;
} }
if (is != null)
decodeMessage(context, is, message, args);
} else { } else {
// Check alias // Check alias
String alias = args.getString("alias");
if (alias == null) if (alias == null)
throw new IllegalArgumentException("Key alias missing"); throw new IllegalArgumentException("Key alias missing");
@ -5678,7 +5680,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
Collection<RecipientInformation> recipients = envelopedData.getRecipientInfos().getRecipients(); // KeyTransRecipientInformation Collection<RecipientInformation> recipients = envelopedData.getRecipientInfos().getRecipients(); // KeyTransRecipientInformation
// Find recipient // Find recipient
is = null; InputStream is = null;
if (chain[0].getSerialNumber() != null) if (chain[0].getSerialNumber() != null)
for (RecipientInformation recipientInfo : recipients) { for (RecipientInformation recipientInfo : recipients) {
KeyTransRecipientId recipientId = (KeyTransRecipientId) recipientInfo.getRID(); KeyTransRecipientId recipientId = (KeyTransRecipientId) recipientInfo.getRID();
@ -5704,55 +5706,8 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
db.identity().setIdentitySignKeyAlias(message.identity, null); db.identity().setIdentitySignKeyAlias(message.identity, null);
throw new IllegalArgumentException(context.getString(R.string.title_unknown_key)); throw new IllegalArgumentException(context.getString(R.string.title_unknown_key));
} }
}
if (is != null) { decodeMessage(context, is, message, args);
// Decode message
Properties props = MessageHelper.getSessionProperties();
Session isession = Session.getInstance(props, null);
MimeMessage imessage = new MimeMessage(isession, is);
MessageHelper helper = new MessageHelper(imessage);
MessageHelper.MessageParts parts = helper.getMessageParts(context);
try {
db.beginTransaction();
// Write decrypted body
String html = parts.getHtml(context);
Helper.writeText(message.getFile(context), html);
Log.i("s/mime html=" + (html == null ? null : html.length()));
// Remove existing attachments
db.attachment().deleteAttachments(message.id);
// Add decrypted attachments
List<EntityAttachment> remotes = parts.getAttachments();
for (int index = 0; index < remotes.size(); index++) {
EntityAttachment remote = remotes.get(index);
remote.message = message.id;
remote.sequence = index + 1;
remote.id = db.attachment().insertAttachment(remote);
try {
parts.downloadAttachment(context, index, remote);
} catch (Throwable ex) {
Log.e(ex);
}
Log.i("s/mime attachment=" + remote);
}
db.message().setMessageEncrypt(message.id, parts.getEncryption());
db.message().setMessageStored(message.id, new Date().getTime());
db.message().setMessageFts(message.id, false);
if (alias != null && !duplicate && message.identity != null)
db.identity().setIdentitySignKeyAlias(message.identity, alias);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
WorkerFts.init(context, false);
} }
return result; return result;
@ -5907,6 +5862,60 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
else else
Log.unexpectedError(getParentFragmentManager(), ex); Log.unexpectedError(getParentFragmentManager(), ex);
} }
private void decodeMessage(Context context, InputStream is, EntityMessage message, Bundle args) throws MessagingException, IOException {
String alias = args.getString("alias");
boolean duplicate = args.getBoolean("duplicate");
// Decode message
Properties props = MessageHelper.getSessionProperties();
Session isession = Session.getInstance(props, null);
MimeMessage imessage = new MimeMessage(isession, is);
MessageHelper helper = new MessageHelper(imessage);
MessageHelper.MessageParts parts = helper.getMessageParts(context);
DB db = DB.getInstance(context);
try {
db.beginTransaction();
// Write decrypted body
String html = parts.getHtml(context);
Helper.writeText(message.getFile(context), html);
Log.i("s/mime html=" + (html == null ? null : html.length()));
// Remove existing attachments
db.attachment().deleteAttachments(message.id);
// Add decrypted attachments
List<EntityAttachment> remotes = parts.getAttachments();
for (int index = 0; index < remotes.size(); index++) {
EntityAttachment remote = remotes.get(index);
remote.message = message.id;
remote.sequence = index + 1;
remote.id = db.attachment().insertAttachment(remote);
try {
parts.downloadAttachment(context, index, remote);
} catch (Throwable ex) {
Log.e(ex);
}
Log.i("s/mime attachment=" + remote);
}
db.message().setMessageEncrypt(message.id, parts.getEncryption());
db.message().setMessageStored(message.id, new Date().getTime());
db.message().setMessageFts(message.id, false);
if (alias != null && !duplicate && message.identity != null)
db.identity().setIdentitySignKeyAlias(message.identity, alias);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
WorkerFts.init(context, false);
}
}.execute(this, args, "decrypt:s/mime"); }.execute(this, args, "decrypt:s/mime");
} }