Block all domains

pull/194/merge
M66B 3 years ago
parent 5729580d75
commit 69df4b9759

@ -4765,7 +4765,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
aargs.putInt("protocol", message.accountProtocol); aargs.putInt("protocol", message.accountProtocol);
aargs.putLong("folder", message.folder); aargs.putLong("folder", message.folder);
aargs.putString("type", message.folderType); aargs.putString("type", message.folderType);
aargs.putString("from", MessageHelper.formatAddresses(message.from)); aargs.putString("from", DB.Converters.encodeAddresses(message.from));
aargs.putBoolean("inJunk", EntityFolder.JUNK.equals(message.folderType)); aargs.putBoolean("inJunk", EntityFolder.JUNK.equals(message.folderType));
aargs.putBoolean("canBlock", canBlock); aargs.putBoolean("canBlock", canBlock);

@ -948,16 +948,20 @@ public class EntityRule {
return cal; return cal;
} }
static EntityRule blockSender(Context context, EntityMessage message, EntityFolder junk, boolean block_domain) throws JSONException { @NonNull
if (message.from == null || message.from.length == 0) static List<EntityRule> blockSender(Context context, EntityMessage message, EntityFolder junk, boolean block_domain) throws JSONException {
return null; List<EntityRule> rules = new ArrayList<>();
String sender = ((InternetAddress) message.from[0]).getAddress(); if (message.from == null)
String name = MessageHelper.formatAddresses(new Address[]{message.from[0]}); return rules;
for (Address from : message.from) {
String sender = ((InternetAddress) from).getAddress();
String name = MessageHelper.formatAddresses(new Address[]{from});
if (TextUtils.isEmpty(sender) || if (TextUtils.isEmpty(sender) ||
!Helper.EMAIL_ADDRESS.matcher(sender).matches()) !Helper.EMAIL_ADDRESS.matcher(sender).matches())
return null; continue;
boolean regex = false; boolean regex = false;
if (block_domain) { if (block_domain) {
@ -989,7 +993,10 @@ public class EntityRule {
rule.condition = jcondition.toString(); rule.condition = jcondition.toString();
rule.action = jaction.toString(); rule.action = jaction.toString();
return rule; rules.add(rule);
}
return rules;
} }
boolean isBlockingSender(EntityMessage message, EntityFolder junk) throws JSONException { boolean isBlockingSender(EntityMessage message, EntityFolder junk) throws JSONException {

@ -49,6 +49,7 @@ import androidx.preference.PreferenceManager;
import org.json.JSONObject; import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@ -64,7 +65,7 @@ public class FragmentDialogJunk extends FragmentDialogBase {
final int protocol = args.getInt("protocol"); final int protocol = args.getInt("protocol");
final long folder = args.getLong("folder"); final long folder = args.getLong("folder");
final String type = args.getString("type"); final String type = args.getString("type");
final String from = args.getString("from"); final Address[] froms = DB.Converters.decodeAddresses(args.getString("from"));
final boolean inJunk = args.getBoolean("inJunk"); final boolean inJunk = args.getBoolean("inJunk");
final boolean canBlock = args.getBoolean("canBlock"); final boolean canBlock = args.getBoolean("canBlock");
@ -305,23 +306,32 @@ public class FragmentDialogJunk extends FragmentDialogBase {
} }
}); });
String domain = null;
try {
boolean common = false; boolean common = false;
Address[] froms = MessageHelper.parseAddresses(context, from); List<String> domains = new ArrayList<>();
String email = (froms.length == 0 ? null : ((InternetAddress) froms[0]).getAddress()); if (froms != null)
for (Address from : froms) {
String email = ((InternetAddress) from).getAddress();
int at = (email == null ? -1 : email.indexOf('@')); int at = (email == null ? -1 : email.indexOf('@'));
domain = (at > 0 ? email.substring(at + 1).toLowerCase(Locale.ROOT) : null); String domain = (at < 0 ? null : email.substring(at + 1).toLowerCase(Locale.ROOT));
if (TextUtils.isEmpty(domain))
continue;
domains.add(domain);
if (domain != null) { for (String d : EmailProvider.getDomainNames(context))
List<String> domains = EmailProvider.getDomainNames(context); if (domain.matches(d))
for (String d : domains)
if (domain.matches(d)) {
common = true; common = true;
break;
}
} }
// Initialize
tvMessage.setText(inJunk
? getString(R.string.title_folder_junk)
: getString(R.string.title_ask_spam_who, MessageHelper.formatAddresses(froms)));
cbBlockSender.setEnabled(canBlock);
cbBlockDomain.setEnabled(false);
cbBlockSender.setChecked(canBlock);
cbBlockDomain.setText(getString(R.string.title_block_sender_domain, TextUtils.join(",", domains)));
if (common) { if (common) {
int dp6 = Helper.dp2pixels(context, 6); int dp6 = Helper.dp2pixels(context, 6);
int colorWarning = Helper.resolveColor(context, R.attr.colorWarning); int colorWarning = Helper.resolveColor(context, R.attr.colorWarning);
@ -331,19 +341,8 @@ public class FragmentDialogJunk extends FragmentDialogBase {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
cbBlockDomain.setCompoundDrawableTintList(ColorStateList.valueOf(colorWarning)); cbBlockDomain.setCompoundDrawableTintList(ColorStateList.valueOf(colorWarning));
} }
} catch (Throwable ex) {
Log.e(ex);
}
// Initialize cbBlockDomain.setVisibility(domains.size() > 0 ? View.VISIBLE : View.GONE);
tvMessage.setText(inJunk
? getString(R.string.title_folder_junk)
: getString(R.string.title_ask_spam_who, from));
cbBlockSender.setEnabled(canBlock);
cbBlockDomain.setEnabled(false);
cbBlockSender.setChecked(canBlock);
cbBlockDomain.setText(getString(R.string.title_block_sender_domain, domain));
cbBlockDomain.setVisibility(domain == null ? View.GONE : View.VISIBLE);
ibMore.setImageLevel(1); ibMore.setImageLevel(1);
cbBlocklist.setChecked(check_blocklist && use_blocklist); cbBlocklist.setChecked(check_blocklist && use_blocklist);
tvBlocklist.setText(TextUtils.join(", ", DnsBlockList.getNamesEnabled(context))); tvBlocklist.setText(TextUtils.join(", ", DnsBlockList.getNamesEnabled(context)));

@ -2580,7 +2580,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
aargs.putInt("protocol", message.accountProtocol); aargs.putInt("protocol", message.accountProtocol);
aargs.putLong("folder", message.folder); aargs.putLong("folder", message.folder);
aargs.putString("type", message.folderType); aargs.putString("type", message.folderType);
aargs.putString("from", MessageHelper.formatAddresses(message.from)); aargs.putString("from", DB.Converters.encodeAddresses(message.from));
aargs.putBoolean("inJunk", EntityFolder.JUNK.equals(message.folderType)); aargs.putBoolean("inJunk", EntityFolder.JUNK.equals(message.folderType));
aargs.putBoolean("canBlock", canBlock); aargs.putBoolean("canBlock", canBlock);
@ -8185,7 +8185,6 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
long id = args.getLong("id"); long id = args.getLong("id");
boolean block_sender = args.getBoolean("block_sender"); boolean block_sender = args.getBoolean("block_sender");
boolean block_domain = args.getBoolean("block_domain"); boolean block_domain = args.getBoolean("block_domain");
List<String> whitelist = EmailProvider.getDomainNames(context);
DB db = DB.getInstance(context); DB db = DB.getInstance(context);
try { try {
@ -8208,19 +8207,17 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
EntityContact.TYPE_JUNK, message.received); EntityContact.TYPE_JUNK, message.received);
if (block_domain) { if (block_domain) {
EntityRule rule = EntityRule.blockSender(context, message, junk, block_domain); List<EntityRule> rules = EntityRule.blockSender(context, message, junk, block_domain);
if (rule != null) { for (EntityRule rule : rules) {
if (message.folder.equals(junk.id)) { if (message.folder.equals(junk.id)) {
EntityFolder inbox = db.folder().getFolderByType(message.account, EntityFolder.INBOX); EntityFolder inbox = db.folder().getFolderByType(message.account, EntityFolder.INBOX);
if (inbox == null) if (inbox == null)
rule = null; continue;
else
rule.folder = inbox.id; rule.folder = inbox.id;
} }
}
if (rule != null)
rule.id = db.rule().insertRule(rule); rule.id = db.rule().insertRule(rule);
} }
}
db.setTransactionSuccessful(); db.setTransactionSuccessful();
} finally { } finally {

Loading…
Cancel
Save