From 68e46b00431882556043c96c1d1a6d7da2b67bca Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 2 Jun 2024 12:12:36 +0200 Subject: [PATCH] Added regex to attachments expression --- FAQ.md | 2 +- .../java/eu/faircode/email/ExpressionHelper.java | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/FAQ.md b/FAQ.md index 0fbcdc4a42..2f76cb296d 100644 --- a/FAQ.md +++ b/FAQ.md @@ -2791,7 +2791,7 @@ The following extra functions are available: * *blocklist()* (version 1.2176-1.2178; deprecated, use *onBlocklist()* instead) * *onBlocklist()* (returns a boolean indicating if the sender/server is on a DNS blocklist; since version 1.2179) * *hasMx()* (returns a boolean indicating if the from/reply-to address has an associated MX record; since version 1.2179) -* *attachments()* (returns an integer indicating number of attachments; since version 1.2179) +* *attachments(regex)* (returns an integer indicating number of attachments; since version 1.2179; optional regex since version 1.2194) * *Jsoup()* (returns an array of selected strings; since version 1.2179) * *Size(array)* (returns the number of items in an array; since version 1.2179) * *knownContact()* (returns a boolean indicating that the from/reply-to address is in the Android address book or in the local contacts database) diff --git a/app/src/main/java/eu/faircode/email/ExpressionHelper.java b/app/src/main/java/eu/faircode/email/ExpressionHelper.java index 52e8cc86b8..0c8d116ef3 100644 --- a/app/src/main/java/eu/faircode/email/ExpressionHelper.java +++ b/app/src/main/java/eu/faircode/email/ExpressionHelper.java @@ -312,6 +312,7 @@ public class ExpressionHelper { } } + @FunctionParameter(name = "value") public static class AttachmentsFunction extends AbstractFunction { private final Context context; private final EntityMessage message; @@ -325,10 +326,20 @@ public class ExpressionHelper { public EvaluationValue evaluate( Expression expression, Token functionToken, EvaluationValue... parameterValues) { int result = 0; + String regex = null; if (message != null) { DB db = DB.getInstance(context); - result = db.attachment().countAttachments(message.id); + if (parameterValues.length == 1) { + regex = parameterValues[0].getStringValue(); + Pattern p = Pattern.compile(regex); + List attachments = db.attachment().getAttachments(message.id); + if (attachments != null) + for (EntityAttachment attachment : attachments) + if (attachment.name != null && p.matcher(attachment.name).matches()) + result++; + } else + result = db.attachment().countAttachments(message.id); } Log.i("EXPR attachments()=" + result);