From 76a0ff9ba844899381f015ba469b0e3fc79c642a Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 25 Apr 2020 16:17:59 +0200 Subject: [PATCH] Auto delete block sender rules --- .../eu/faircode/email/EntityOperation.java | 8 ++++- .../java/eu/faircode/email/EntityRule.java | 30 ++++++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/EntityOperation.java b/app/src/main/java/eu/faircode/email/EntityOperation.java index 4e1617db01..18b620f174 100644 --- a/app/src/main/java/eu/faircode/email/EntityOperation.java +++ b/app/src/main/java/eu/faircode/email/EntityOperation.java @@ -188,7 +188,6 @@ public class EntityOperation { db.message().setMessageImportance(similar.id, null); } - EntityAccount account = db.account().getAccount(message.account); if (!"imap.gmail.com".equalsIgnoreCase(account == null ? null : account.host) || !EntityFolder.ARCHIVE.equals(source.type) || @@ -201,6 +200,13 @@ public class EntityOperation { EntityMessage.snooze(context, message.id, null); } + if (EntityFolder.JUNK.equals(source.type) && EntityFolder.INBOX.equals(target.type)) { + List rules = db.rule().getRules(target.id); + for (EntityRule rule : rules) + if (rule.isBlockingSender(message, source)) + db.rule().deleteRule(rule.id); + } + // Create copy without uid in target folder // Message with same msgid can be in archive if (message.uid != null && diff --git a/app/src/main/java/eu/faircode/email/EntityRule.java b/app/src/main/java/eu/faircode/email/EntityRule.java index dbbfe74480..364f72b29a 100644 --- a/app/src/main/java/eu/faircode/email/EntityRule.java +++ b/app/src/main/java/eu/faircode/email/EntityRule.java @@ -567,7 +567,7 @@ public class EntityRule { jcondition.put("sender", jsender); JSONObject jaction = new JSONObject(); - jaction.put("type", EntityRule.TYPE_MOVE); + jaction.put("type", TYPE_MOVE); jaction.put("target", junk.id); DB db = DB.getInstance(context); @@ -584,6 +584,34 @@ public class EntityRule { return rule; } + boolean isBlockingSender(EntityMessage message, EntityFolder junk) throws JSONException { + if (message.from == null || message.from.length == 0) + return false; + String sender = ((InternetAddress) message.from[0]).getAddress(); + if (sender == null) + return false; + int at = sender.indexOf('@'); + String domain = (at < 0 ? sender : sender.substring(at)); + + JSONObject jcondition = new JSONObject(condition); + if (!jcondition.has("sender")) + return false; + JSONObject jsender = jcondition.getJSONObject("sender"); + if (jsender.optBoolean("regex")) + return false; + String value = jsender.optString("value"); + if (!sender.equals(value) && !domain.equals(value)) + return false; + + JSONObject jaction = new JSONObject(action); + if (jaction.optInt("type", -1) != TYPE_MOVE) + return false; + if (jaction.optInt("target", -1) != junk.id) + return false; + + return true; + } + @Override public boolean equals(Object obj) { if (obj instanceof EntityRule) {