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.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);

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

@ -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,23 +306,32 @@ 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());
List<String> domains = new ArrayList<>();
if (froms != null)
for (Address from : froms) {
String email = ((InternetAddress) from).getAddress();
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) {
List<String> domains = EmailProvider.getDomainNames(context);
for (String d : domains)
if (domain.matches(d)) {
for (String d : EmailProvider.getDomainNames(context))
if (domain.matches(d))
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) {
int dp6 = Helper.dp2pixels(context, 6);
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)
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));
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.setVisibility(domains.size() > 0 ? View.VISIBLE : View.GONE);
ibMore.setImageLevel(1);
cbBlocklist.setChecked(check_blocklist && use_blocklist);
tvBlocklist.setText(TextUtils.join(", ", DnsBlockList.getNamesEnabled(context)));

@ -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,19 +8207,17 @@ 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
continue;
rule.folder = inbox.id;
}
}
if (rule != null)
rule.id = db.rule().insertRule(rule);
}
}
db.setTransactionSuccessful();
} finally {

Loading…
Cancel
Save