Block all domains

This commit is contained in:
M66B 2021-12-17 15:03:19 +01:00
parent 5729580d75
commit 69df4b9759
4 changed files with 79 additions and 76 deletions

View File

@ -4765,7 +4765,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
aargs.putInt("protocol", message.accountProtocol);
aargs.putLong("folder", message.folder);
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("canBlock", canBlock);

View File

@ -948,48 +948,55 @@ public class EntityRule {
return cal;
}
static EntityRule blockSender(Context context, EntityMessage message, EntityFolder junk, boolean block_domain) throws JSONException {
if (message.from == null || message.from.length == 0)
return null;
@NonNull
static List<EntityRule> blockSender(Context context, EntityMessage message, EntityFolder junk, boolean block_domain) throws JSONException {
List<EntityRule> rules = new ArrayList<>();
String sender = ((InternetAddress) message.from[0]).getAddress();
String name = MessageHelper.formatAddresses(new Address[]{message.from[0]});
if (message.from == null)
return rules;
if (TextUtils.isEmpty(sender) ||
!Helper.EMAIL_ADDRESS.matcher(sender).matches())
return null;
for (Address from : message.from) {
String sender = ((InternetAddress) from).getAddress();
String name = MessageHelper.formatAddresses(new Address[]{from});
boolean regex = false;
if (block_domain) {
int at = sender.indexOf('@');
if (at > 0) {
regex = true;
sender = ".*@.*" + sender.substring(at + 1) + ".*";
if (TextUtils.isEmpty(sender) ||
!Helper.EMAIL_ADDRESS.matcher(sender).matches())
continue;
boolean regex = false;
if (block_domain) {
int at = sender.indexOf('@');
if (at > 0) {
regex = true;
sender = ".*@.*" + sender.substring(at + 1) + ".*";
}
}
JSONObject jsender = new JSONObject();
jsender.put("value", sender);
jsender.put("regex", regex);
JSONObject jcondition = new JSONObject();
jcondition.put("sender", jsender);
JSONObject jaction = new JSONObject();
jaction.put("type", TYPE_MOVE);
jaction.put("target", junk.id);
jaction.put("seen", true);
EntityRule rule = new EntityRule();
rule.folder = message.folder;
rule.name = context.getString(R.string.title_block, name);
rule.order = 1000;
rule.enabled = true;
rule.stop = true;
rule.condition = jcondition.toString();
rule.action = jaction.toString();
rules.add(rule);
}
JSONObject jsender = new JSONObject();
jsender.put("value", sender);
jsender.put("regex", regex);
JSONObject jcondition = new JSONObject();
jcondition.put("sender", jsender);
JSONObject jaction = new JSONObject();
jaction.put("type", TYPE_MOVE);
jaction.put("target", junk.id);
jaction.put("seen", true);
EntityRule rule = new EntityRule();
rule.folder = message.folder;
rule.name = context.getString(R.string.title_block, name);
rule.order = 1000;
rule.enabled = true;
rule.stop = true;
rule.condition = jcondition.toString();
rule.action = jaction.toString();
return rule;
return rules;
}
boolean isBlockingSender(EntityMessage message, EntityFolder junk) throws JSONException {

View File

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

View File

@ -2580,7 +2580,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
aargs.putInt("protocol", message.accountProtocol);
aargs.putLong("folder", message.folder);
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("canBlock", canBlock);
@ -8185,7 +8185,6 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
long id = args.getLong("id");
boolean block_sender = args.getBoolean("block_sender");
boolean block_domain = args.getBoolean("block_domain");
List<String> whitelist = EmailProvider.getDomainNames(context);
DB db = DB.getInstance(context);
try {
@ -8208,18 +8207,16 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
EntityContact.TYPE_JUNK, message.received);
if (block_domain) {
EntityRule rule = EntityRule.blockSender(context, message, junk, block_domain);
if (rule != null) {
List<EntityRule> rules = EntityRule.blockSender(context, message, junk, block_domain);
for (EntityRule rule : rules) {
if (message.folder.equals(junk.id)) {
EntityFolder inbox = db.folder().getFolderByType(message.account, EntityFolder.INBOX);
if (inbox == null)
rule = null;
else
rule.folder = inbox.id;
continue;
rule.folder = inbox.id;
}
}
if (rule != null)
rule.id = db.rule().insertRule(rule);
}
}
db.setTransactionSuccessful();