diff --git a/app/src/main/java/eu/faircode/email/Core.java b/app/src/main/java/eu/faircode/email/Core.java index 164be5e651..b4ef1a916b 100644 --- a/app/src/main/java/eu/faircode/email/Core.java +++ b/app/src/main/java/eu/faircode/email/Core.java @@ -98,6 +98,7 @@ import java.util.Calendar; import java.util.Collections; import java.util.Comparator; import java.util.Date; +import java.util.Enumeration; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; @@ -114,6 +115,7 @@ import javax.mail.Flags; import javax.mail.Folder; import javax.mail.FolderClosedException; import javax.mail.FolderNotFoundException; +import javax.mail.Header; import javax.mail.Message; import javax.mail.MessageRemovedException; import javax.mail.MessagingException; @@ -2796,6 +2798,9 @@ class Core { // No MX check + List
headers = + (EntityRule.needsHeaders(rules) ? helper.getAllHeaders() : null); + try { db.beginTransaction(); @@ -2814,7 +2819,7 @@ class Core { attachment.id = db.attachment().insertAttachment(attachment); } - runRules(context, imessage, account, folder, message, rules); + runRules(context, headers, account, folder, message, rules); reportNewMessage(context, account, folder, message); db.setTransactionSuccessful(); @@ -3649,6 +3654,9 @@ class Core { } } + List
headers = + (EntityRule.needsHeaders(rules) ? helper.getAllHeaders() : null); + if (message == null) { Long sent = helper.getSent(); @@ -3860,7 +3868,7 @@ class Core { attachment.id = db.attachment().insertAttachment(attachment); } - runRules(context, imessage, account, folder, message, rules); + runRules(context, headers, account, folder, message, rules); if (message.blocklist != null && message.blocklist) { boolean use_blocklist = prefs.getBoolean("use_blocklist", false); @@ -4070,7 +4078,7 @@ class Core { db.message().updateMessage(message); if (process) - runRules(context, imessage, account, folder, message, rules); + runRules(context, headers, account, folder, message, rules); db.setTransactionSuccessful(); } finally { @@ -4233,7 +4241,7 @@ class Core { } private static void runRules( - Context context, Message imessage, + Context context, List
headers, EntityAccount account, EntityFolder folder, EntityMessage message, List rules) { @@ -4246,7 +4254,7 @@ class Core { try { boolean executed = false; for (EntityRule rule : rules) - if (rule.matches(context, message, imessage)) { + if (rule.matches(context, message, headers)) { rule.execute(context, message); executed = true; if (rule.stop) diff --git a/app/src/main/java/eu/faircode/email/EntityRule.java b/app/src/main/java/eu/faircode/email/EntityRule.java index 80ef27dbe3..3c65eceb61 100644 --- a/app/src/main/java/eu/faircode/email/EntityRule.java +++ b/app/src/main/java/eu/faircode/email/EntityRule.java @@ -51,8 +51,8 @@ import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; +import java.util.Collections; import java.util.Date; -import java.util.Enumeration; import java.util.List; import java.util.Locale; import java.util.Objects; @@ -61,7 +61,6 @@ import java.util.regex.Pattern; import javax.mail.Address; import javax.mail.Header; -import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; @@ -125,7 +124,20 @@ public class EntityRule { private static ExecutorService executor = Helper.getBackgroundExecutor(1, "rule"); - boolean matches(Context context, EntityMessage message, Message imessage) throws MessagingException { + static boolean needsHeaders(List rules) { + for (EntityRule rule : rules) + try { + JSONObject jcondition = new JSONObject(rule.condition); + if (jcondition.has("header")) + return true; + } catch (Throwable ex) { + Log.e(ex); + } + + return false; + } + + boolean matches(Context context, EntityMessage message, List
headers) throws MessagingException { try { JSONObject jcondition = new JSONObject(condition); @@ -262,16 +274,14 @@ public class EntityRule { return false; } else { boolean matches = false; - Enumeration
headers; - if (imessage != null) - headers = imessage.getAllHeaders(); - else if (message.headers != null) { + if (headers == null) { + if (message.headers == null) + throw new IllegalArgumentException(context.getString(R.string.title_rule_no_headers)); + ByteArrayInputStream bis = new ByteArrayInputStream(message.headers.getBytes()); - headers = new InternetHeaders(bis).getAllHeaders(); - } else - throw new IllegalArgumentException(context.getString(R.string.title_rule_no_headers)); - while (headers.hasMoreElements()) { - Header header = headers.nextElement(); + headers = Collections.list(new InternetHeaders(bis).getAllHeaders()); + } + for (Header header : headers) { String formatted = header.getName() + ": " + header.getValue(); if (matches(context, message, value, formatted, regex)) { matches = true; diff --git a/app/src/main/java/eu/faircode/email/MessageHelper.java b/app/src/main/java/eu/faircode/email/MessageHelper.java index 2ae2e483d2..806342fde0 100644 --- a/app/src/main/java/eu/faircode/email/MessageHelper.java +++ b/app/src/main/java/eu/faircode/email/MessageHelper.java @@ -1045,6 +1045,11 @@ public class MessageHelper { return (header == null ? null : MimeUtility.unfold(header)); } + List
getAllHeaders() throws MessagingException { + ensureHeaders(); + return Collections.list(imessage.getAllHeaders()); + } + String[] getReferences() throws MessagingException { ensureHeaders();