From 04265c6a7de7117d20feee0e22fd7065cd753fb2 Mon Sep 17 00:00:00 2001 From: M66B Date: Tue, 30 Apr 2024 09:51:26 +0200 Subject: [PATCH] Added received time to expression --- FAQ.md | 5 +++-- .../main/java/eu/faircode/email/EntityRule.java | 16 ++-------------- .../java/eu/faircode/email/ExpressionHelper.java | 16 ++++++++++++++++ index.html | 5 +++-- 4 files changed, 24 insertions(+), 18 deletions(-) diff --git a/FAQ.md b/FAQ.md index 30054422ff..1f3a57ef34 100644 --- a/FAQ.md +++ b/FAQ.md @@ -2765,8 +2765,9 @@ Please [see here](https://ezylang.github.io/EvalEx/references/references.html) a The following extra variables are available: -* *from* (array) -* *to* (array) +* *received* (long, unix epoch in milliseconds; since version 1.2179) +* *from* (array of strings) +* *to* (array of strings) * *subject* (string) * *text* (string) * *hasAttachments* (boolean; deprecated, use function *attachments()* instead) diff --git a/app/src/main/java/eu/faircode/email/EntityRule.java b/app/src/main/java/eu/faircode/email/EntityRule.java index b75f0250ef..1db136a958 100644 --- a/app/src/main/java/eu/faircode/email/EntityRule.java +++ b/app/src/main/java/eu/faircode/email/EntityRule.java @@ -155,10 +155,6 @@ public class EntityRule { private static final int MAX_NOTES_LENGTH = 512; // characters private static final int URL_TIMEOUT = 15 * 1000; // milliseconds - private static final List EXPR_VARIABLES = Collections.unmodifiableList(Arrays.asList( - "to", "from", "subject", "text", "hasAttachments" - )); - static boolean needsHeaders(EntityMessage message, List rules) { return needsHeaders(rules); } @@ -691,16 +687,8 @@ public class EntityRule { void validate(Context context) throws JSONException, IllegalArgumentException { try { Expression expression = ExpressionHelper.getExpression(this, null, null, null, context); - if (expression != null) { - for (String variable : expression.getUsedVariables()) { - Log.i("EXPR variable=" + variable); - if (!EXPR_VARIABLES.contains(variable)) - throw new IllegalArgumentException("Unknown variable '" + variable + "'"); - } - Log.i("EXPR validating"); - expression.validate(); - Log.i("EXPR validated"); - } + if (expression != null) + ExpressionHelper.check(expression); } catch (ParseException | MessagingException ex) { Log.w("EXPR", ex); String message = ex.getMessage(); diff --git a/app/src/main/java/eu/faircode/email/ExpressionHelper.java b/app/src/main/java/eu/faircode/email/ExpressionHelper.java index c378d6f2cd..52e8cc86b8 100644 --- a/app/src/main/java/eu/faircode/email/ExpressionHelper.java +++ b/app/src/main/java/eu/faircode/email/ExpressionHelper.java @@ -60,6 +60,21 @@ import javax.mail.internet.InternetAddress; import javax.mail.internet.InternetHeaders; public class ExpressionHelper { + private static final List EXPR_VARIABLES = Collections.unmodifiableList(Arrays.asList( + "received", "to", "from", "subject", "text", "hasAttachments" + )); + + static void check(Expression expression) throws ParseException { + for (String variable : expression.getUsedVariables()) { + Log.i("EXPR variable=" + variable); + if (!EXPR_VARIABLES.contains(variable)) + throw new IllegalArgumentException("Unknown variable '" + variable + "'"); + } + Log.i("EXPR validating"); + expression.validate(); + Log.i("EXPR validated"); + } + static Expression getExpression(EntityRule rule, EntityMessage message, List
headers, String html, Context context) throws JSONException, ParseException, MessagingException { // https://ezylang.github.io/EvalEx/ @@ -120,6 +135,7 @@ public class ExpressionHelper { configuration.getOperatorDictionary().addOperator("Matches", oMatches); Expression expression = new Expression(eval, configuration) + .with("received", message == null ? null : message.received) .with("to", to) .with("from", from) .with("subject", message == null ? null : message.subject) diff --git a/index.html b/index.html index 294ce84da3..647d076b4e 100644 --- a/index.html +++ b/index.html @@ -1504,8 +1504,9 @@ X-Google-Original-From: Somebody <somebody+extra@example.org>

Please see here about which constants, operators and functions are available.

The following extra variables are available:

    -
  • from (array)
  • -
  • to (array)
  • +
  • received (long, unix epoch in milliseconds; since version 1.2179)
  • +
  • from (array of strings)
  • +
  • to (array of strings)
  • subject (string)
  • text (string)
  • hasAttachments (boolean; deprecated, use function attachments() instead)