Check for any/all PGP or S/MIME keys

pull/209/head
M66B 3 years ago
parent 09cc268690
commit 388be88d0f

@ -344,9 +344,9 @@ public class EditTextMultiAutoComplete extends AppCompatMultiAutoCompleteTextVie
public void run() { public void run() {
try { try {
int has = 0; int has = 0;
if (PgpHelper.hasPgpKey(context, recipient)) if (PgpHelper.hasPgpKey(context, recipient, true))
has |= 1; has |= 1;
if (SmimeHelper.hasSmimeKey(context, recipient)) if (SmimeHelper.hasSmimeKey(context, recipient, true))
has |= 2; has |= 2;
encryption.put(email, has); encryption.put(email, has);

@ -5137,13 +5137,13 @@ public class FragmentCompose extends FragmentBase {
EntityMessage.PGP_SIGNENCRYPT.equals(ref.ui_encrypt)) { EntityMessage.PGP_SIGNENCRYPT.equals(ref.ui_encrypt)) {
if (Helper.isOpenKeychainInstalled(context) && if (Helper.isOpenKeychainInstalled(context) &&
selected.sign_key != null && selected.sign_key != null &&
PgpHelper.hasPgpKey(context, recipients)) PgpHelper.hasPgpKey(context, recipients, true))
data.draft.ui_encrypt = ref.ui_encrypt; data.draft.ui_encrypt = ref.ui_encrypt;
} else if (EntityMessage.SMIME_SIGNONLY.equals(ref.ui_encrypt) || } else if (EntityMessage.SMIME_SIGNONLY.equals(ref.ui_encrypt) ||
EntityMessage.SMIME_SIGNENCRYPT.equals(ref.ui_encrypt)) { EntityMessage.SMIME_SIGNENCRYPT.equals(ref.ui_encrypt)) {
if (ActivityBilling.isPro(context) && if (ActivityBilling.isPro(context) &&
selected.sign_key_alias != null && selected.sign_key_alias != null &&
SmimeHelper.hasSmimeKey(context, recipients)) SmimeHelper.hasSmimeKey(context, recipients, true))
data.draft.ui_encrypt = ref.ui_encrypt; data.draft.ui_encrypt = ref.ui_encrypt;
} }
} }
@ -6400,8 +6400,8 @@ public class FragmentCompose extends FragmentBase {
EntityMessage.DSN_NONE.equals(draft.dsn)) && EntityMessage.DSN_NONE.equals(draft.dsn)) &&
(draft.ui_encrypt == null || (draft.ui_encrypt == null ||
EntityMessage.ENCRYPT_NONE.equals(draft.ui_encrypt))) { EntityMessage.ENCRYPT_NONE.equals(draft.ui_encrypt))) {
args.putBoolean("remind_pgp", PgpHelper.hasPgpKey(context, recipients)); args.putBoolean("remind_pgp", PgpHelper.hasPgpKey(context, recipients, false));
args.putBoolean("remind_smime", SmimeHelper.hasSmimeKey(context, recipients)); args.putBoolean("remind_smime", SmimeHelper.hasSmimeKey(context, recipients, false));
} }
if (TextUtils.isEmpty(draft.subject)) if (TextUtils.isEmpty(draft.subject))

@ -68,11 +68,11 @@ public class PgpHelper {
} }
} }
static boolean hasPgpKey(Context context, List<Address> recipients) { static boolean hasPgpKey(Context context, List<Address> recipients, boolean all) {
return hasPgpKey(context, recipients, KEY_TIMEOUT); // milliseconds return hasPgpKey(context, recipients, all, KEY_TIMEOUT); // milliseconds
} }
private static boolean hasPgpKey(Context context, List<Address> recipients, long timeout) { private static boolean hasPgpKey(Context context, List<Address> recipients, boolean all, long timeout) {
if (recipients == null || recipients.size() == 0) if (recipients == null || recipients.size() == 0)
return false; return false;
@ -90,7 +90,7 @@ public class PgpHelper {
int resultCode = result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR); int resultCode = result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR);
if (resultCode == OpenPgpApi.RESULT_CODE_SUCCESS) { if (resultCode == OpenPgpApi.RESULT_CODE_SUCCESS) {
long[] keyIds = result.getLongArrayExtra(OpenPgpApi.EXTRA_KEY_IDS); long[] keyIds = result.getLongArrayExtra(OpenPgpApi.EXTRA_KEY_IDS);
return (keyIds.length > 0); return (all ? keyIds.length == recipients.size() : keyIds.length > 0);
} }
} catch (OperationCanceledException ignored) { } catch (OperationCanceledException ignored) {
// Do nothing // Do nothing

@ -27,18 +27,19 @@ import javax.mail.Address;
import javax.mail.internet.InternetAddress; import javax.mail.internet.InternetAddress;
public class SmimeHelper { public class SmimeHelper {
static boolean hasSmimeKey(Context context, List<Address> recipients) { static boolean hasSmimeKey(Context context, List<Address> recipients, boolean all) {
if (recipients == null || recipients.size() == 0) if (recipients == null || recipients.size() == 0)
return false; return false;
int count = 0;
DB db = DB.getInstance(context); DB db = DB.getInstance(context);
for (Address address : recipients) { for (Address address : recipients) {
String email = ((InternetAddress) address).getAddress(); String email = ((InternetAddress) address).getAddress();
List<EntityCertificate> certs = db.certificate().getCertificateByEmail(email); List<EntityCertificate> certs = db.certificate().getCertificateByEmail(email);
if (certs != null && certs.size() > 0) if (certs != null && certs.size() > 0)
return true; count++;
} }
return false; return (all ? count == recipients.size() : count > 0);
} }
} }

Loading…
Cancel
Save