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,48 +948,55 @@ 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;
if (TextUtils.isEmpty(sender) || for (Address from : message.from) {
!Helper.EMAIL_ADDRESS.matcher(sender).matches()) String sender = ((InternetAddress) from).getAddress();
return null; String name = MessageHelper.formatAddresses(new Address[]{from});
boolean regex = false; if (TextUtils.isEmpty(sender) ||
if (block_domain) { !Helper.EMAIL_ADDRESS.matcher(sender).matches())
int at = sender.indexOf('@'); continue;
if (at > 0) {
regex = true; boolean regex = false;
sender = ".*@.*" + sender.substring(at + 1) + ".*"; if (block_domain) {
int at = sender.indexOf('@');
if (at > 0) {
regex = true;
sender = ".*@.*" + sender.substring(at + 1) + ".*";
}
} }
}
JSONObject jsender = new JSONObject(); JSONObject jsender = new JSONObject();
jsender.put("value", sender); jsender.put("value", sender);
jsender.put("regex", regex); jsender.put("regex", regex);
JSONObject jcondition = new JSONObject(); JSONObject jcondition = new JSONObject();
jcondition.put("sender", jsender); jcondition.put("sender", jsender);
JSONObject jaction = new JSONObject(); JSONObject jaction = new JSONObject();
jaction.put("type", TYPE_MOVE); jaction.put("type", TYPE_MOVE);
jaction.put("target", junk.id); jaction.put("target", junk.id);
jaction.put("seen", true); jaction.put("seen", true);
EntityRule rule = new EntityRule(); EntityRule rule = new EntityRule();
rule.folder = message.folder; rule.folder = message.folder;
rule.name = context.getString(R.string.title_block, name); rule.name = context.getString(R.string.title_block, name);
rule.order = 1000; rule.order = 1000;
rule.enabled = true; rule.enabled = true;
rule.stop = true; rule.stop = true;
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,45 +306,43 @@ public class FragmentDialogJunk extends FragmentDialogBase {
} }
}); });
String domain = null; boolean common = false;
try { List<String> domains = new ArrayList<>();
boolean common = false; if (froms != null)
Address[] froms = MessageHelper.parseAddresses(context, from); for (Address from : froms) {
String email = (froms.length == 0 ? null : ((InternetAddress) froms[0]).getAddress()); 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))
if (domain != null) { continue;
List<String> domains = EmailProvider.getDomainNames(context);
for (String d : domains) domains.add(domain);
if (domain.matches(d)) {
for (String d : EmailProvider.getDomainNames(context))
if (domain.matches(d))
common = true; 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 // Initialize
tvMessage.setText(inJunk tvMessage.setText(inJunk
? getString(R.string.title_folder_junk) ? 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); cbBlockSender.setEnabled(canBlock);
cbBlockDomain.setEnabled(false); cbBlockDomain.setEnabled(false);
cbBlockSender.setChecked(canBlock); 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); 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,18 +8207,16 @@ 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();

Loading…
Cancel
Save