From 49a68dc1a8cd1580bf63ada855adfe20732a73ca Mon Sep 17 00:00:00 2001 From: M66B Date: Thu, 9 Jun 2022 11:06:03 +0200 Subject: [PATCH] Added option for inbound OAuth only --- .../java/eu/faircode/email/FragmentOAuth.java | 78 +++++++++++-------- app/src/main/res/layout/fragment_oauth.xml | 12 ++- app/src/main/res/values/strings.xml | 1 + 3 files changed, 57 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/FragmentOAuth.java b/app/src/main/java/eu/faircode/email/FragmentOAuth.java index 43ec962612..c29448250b 100644 --- a/app/src/main/java/eu/faircode/email/FragmentOAuth.java +++ b/app/src/main/java/eu/faircode/email/FragmentOAuth.java @@ -112,6 +112,7 @@ public class FragmentOAuth extends FragmentBase { private EditText etName; private EditText etEmail; private EditText etTenant; + private CheckBox cbInboundOnly; private CheckBox cbPop; private CheckBox cbUpdate; private Button btnOAuth; @@ -162,6 +163,7 @@ public class FragmentOAuth extends FragmentBase { etName = view.findViewById(R.id.etName); etEmail = view.findViewById(R.id.etEmail); etTenant = view.findViewById(R.id.etTenant); + cbInboundOnly = view.findViewById(R.id.cbInboundOnly); cbPop = view.findViewById(R.id.cbPop); cbUpdate = view.findViewById(R.id.cbUpdate); btnOAuth = view.findViewById(R.id.btnOAuth); @@ -229,6 +231,7 @@ public class FragmentOAuth extends FragmentBase { etName.setText(personal); etEmail.setText(address); etTenant.setText(null); + cbInboundOnly.setChecked(false); cbPop.setChecked(false); cbUpdate.setChecked(update); @@ -284,6 +287,7 @@ public class FragmentOAuth extends FragmentBase { etName.setEnabled(false); etEmail.setEnabled(false); etTenant.setEnabled(false); + cbInboundOnly.setEnabled(false); cbPop.setEnabled(false); cbUpdate.setEnabled(false); btnOAuth.setEnabled(false); @@ -426,6 +430,7 @@ public class FragmentOAuth extends FragmentBase { etName.setEnabled(true); etEmail.setEnabled(true); etTenant.setEnabled(true); + cbInboundOnly.setEnabled(true); cbPop.setEnabled(true); cbUpdate.setEnabled(true); @@ -513,6 +518,7 @@ public class FragmentOAuth extends FragmentBase { args.putBoolean("askAccount", askAccount); args.putString("personal", etName.getText().toString().trim()); args.putString("address", etEmail.getText().toString().trim()); + args.putBoolean("inbound_only", cbInboundOnly.isChecked()); args.putBoolean("pop", cbPop.isChecked()); args.putBoolean("update", cbUpdate.isChecked()); @@ -537,6 +543,7 @@ public class FragmentOAuth extends FragmentBase { boolean askAccount = args.getBoolean("askAccount", false); String personal = args.getString("personal"); String address = args.getString("address"); + boolean inbound_only = args.getBoolean("inbound_only"); boolean pop = args.getBoolean("pop"); EmailProvider provider = EmailProvider.getProvider(context, id); @@ -742,18 +749,20 @@ public class FragmentOAuth extends FragmentBase { folders = aservice.getFolders(); } - Log.i("OAuth checking SMTP provider=" + provider.id); - Long max_size; - - try (EmailService iservice = new EmailService( - context, iprotocol, null, iencryption, false, - EmailService.PURPOSE_CHECK, true)) { - iservice.connect( - provider.smtp.host, provider.smtp.port, - AUTH_TYPE_OAUTH, provider.id, - username, state, - null, null); - max_size = iservice.getMaxSize(); + Long max_size = null; + if (!inbound_only) { + Log.i("OAuth checking SMTP provider=" + provider.id); + + try (EmailService iservice = new EmailService( + context, iprotocol, null, iencryption, false, + EmailService.PURPOSE_CHECK, true)) { + iservice.connect( + provider.smtp.host, provider.smtp.port, + AUTH_TYPE_OAUTH, provider.id, + username, state, + null, null); + max_size = iservice.getMaxSize(); + } } Log.i("OAuth passed provider=" + provider.id); @@ -839,27 +848,28 @@ public class FragmentOAuth extends FragmentBase { db.account().updateAccount(account); // Create identities - for (Pair identity : identities) { - EntityIdentity ident = new EntityIdentity(); - ident.name = identity.second; - ident.email = identity.first; - ident.account = account.id; - - ident.host = provider.smtp.host; - ident.encryption = iencryption; - ident.port = provider.smtp.port; - ident.auth_type = AUTH_TYPE_OAUTH; - ident.provider = provider.id; - ident.user = username; - ident.password = state; - ident.use_ip = provider.useip; - ident.synchronize = true; - ident.primary = ident.user.equals(ident.email); - ident.max_size = max_size; - - ident.id = db.identity().insertIdentity(ident); - EntityLog.log(context, "OAuth identity=" + ident.name + " email=" + ident.email); - } + if (!inbound_only) + for (Pair identity : identities) { + EntityIdentity ident = new EntityIdentity(); + ident.name = identity.second; + ident.email = identity.first; + ident.account = account.id; + + ident.host = provider.smtp.host; + ident.encryption = iencryption; + ident.port = provider.smtp.port; + ident.auth_type = AUTH_TYPE_OAUTH; + ident.provider = provider.id; + ident.user = username; + ident.password = state; + ident.use_ip = provider.useip; + ident.synchronize = true; + ident.primary = ident.user.equals(ident.email); + ident.max_size = max_size; + + ident.id = db.identity().insertIdentity(ident); + EntityLog.log(context, "OAuth identity=" + ident.name + " email=" + ident.email); + } args.putBoolean("pop", pop); } else { @@ -912,6 +922,7 @@ public class FragmentOAuth extends FragmentBase { etName.setEnabled(true); etEmail.setEnabled(true); etTenant.setEnabled(true); + cbInboundOnly.setEnabled(true); cbPop.setEnabled(true); cbUpdate.setEnabled(true); btnOAuth.setEnabled(true); @@ -952,6 +963,7 @@ public class FragmentOAuth extends FragmentBase { etName.setEnabled(true); etEmail.setEnabled(true); etTenant.setEnabled(true); + cbInboundOnly.setEnabled(true); cbPop.setEnabled(true); cbUpdate.setEnabled(true); btnOAuth.setEnabled(true); diff --git a/app/src/main/res/layout/fragment_oauth.xml b/app/src/main/res/layout/fragment_oauth.xml index e88ff28a79..5560806254 100644 --- a/app/src/main/res/layout/fragment_oauth.xml +++ b/app/src/main/res/layout/fragment_oauth.xml @@ -90,6 +90,16 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/etTenant" /> + + + app:layout_constraintTop_toBottomOf="@id/cbInboundOnly" /> Gmail %1$s (OAuth) Other provider + Incoming email only (email cannot be sent!) POP3 account The email provider has approved OAuth only for the Play Store and GitHub version Authorizing Google accounts will work in the Play Store and GitHub version only because Android checks the digital app signature