From bf9eb919591891796d821afa54eef621439a357c Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 21 Apr 2024 08:38:21 +0200 Subject: [PATCH] Expression: added function blocklist --- .../java/eu/faircode/email/EntityRule.java | 41 ++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/eu/faircode/email/EntityRule.java b/app/src/main/java/eu/faircode/email/EntityRule.java index f88299f8f8..a728b62f6d 100644 --- a/app/src/main/java/eu/faircode/email/EntityRule.java +++ b/app/src/main/java/eu/faircode/email/EntityRule.java @@ -704,6 +704,41 @@ public class EntityRule { } } + public static class BlocklistFunction extends AbstractFunction { + private Context context; + private Address[] from; + private List
headers; + + BlocklistFunction(Context context, Address[] from, List
headers) { + this.context = context; + this.from = from; + this.headers = headers; + } + + @Override + public EvaluationValue evaluate( + Expression expression, Token functionToken, EvaluationValue... parameterValues) { + boolean result = false; + + try { + if (from != null) + result = Boolean.TRUE.equals(DnsBlockList.isJunk(context, Arrays.asList(from))); + + List received = new ArrayList<>(); + if (headers != null) + for (Header header : headers) + if (header.getName().equalsIgnoreCase("Received")) + received.add(header.getValue()); + result = result || Boolean.TRUE.equals(DnsBlockList.isJunk(context, received.toArray(new String[0]))); + } catch (Throwable ex) { + Log.e("EXPR", ex); + } + + Log.i("EXPR blocklist()=" + result); + return expression.convertValue(result); + } + } + @InfixOperator(precedence = OPERATOR_PRECEDENCE_COMPARISON) public static class ContainsOperator extends AbstractOperator { private boolean regex; @@ -787,6 +822,8 @@ public class EntityRule { new HeaderFunction(headers)); configuration.getFunctionDictionary().addFunction("Message", new MessageFunction(message)); + configuration.getFunctionDictionary().addFunction("Blocklist", + new BlocklistFunction(context, message == null ? null : message.from, headers)); configuration.getOperatorDictionary().addOperator("Contains", new ContainsOperator(false)); configuration.getOperatorDictionary().addOperator("Matches", @@ -818,7 +855,9 @@ public class EntityRule { for (ASTNode node : expression.getAllASTNodes()) { Token token = node.getToken(); Log.i("EXPR token=" + token.getType() + ":" + token.getValue()); - if (token.getType() == Token.TokenType.FUNCTION && "header".equalsIgnoreCase(token.getValue())) { + if (token.getType() == Token.TokenType.FUNCTION && + ("header".equalsIgnoreCase(token.getValue()) || + "blocklist".equalsIgnoreCase(token.getValue()))) { Log.i("EXPR needs headers"); return true; }