From 538a630c31a45925a7b3c03d7ddbecadec33e7a9 Mon Sep 17 00:00:00 2001 From: M66B Date: Wed, 17 Apr 2024 22:03:48 +0200 Subject: [PATCH] Expression: message properties --- .../java/eu/faircode/email/EntityRule.java | 34 +++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/EntityRule.java b/app/src/main/java/eu/faircode/email/EntityRule.java index 6c56fbd540..32cac7a188 100644 --- a/app/src/main/java/eu/faircode/email/EntityRule.java +++ b/app/src/main/java/eu/faircode/email/EntityRule.java @@ -64,6 +64,8 @@ import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; import java.net.URL; import java.nio.charset.StandardCharsets; import java.text.DateFormat; @@ -73,9 +75,11 @@ import java.util.Arrays; import java.util.Calendar; import java.util.Collections; import java.util.Date; +import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Locale; +import java.util.Map; import java.util.Objects; import java.util.UUID; import java.util.regex.Matcher; @@ -166,7 +170,7 @@ public class EntityRule { private static final int URL_TIMEOUT = 15 * 1000; // milliseconds private static final List EXPR_VARIABLES = Collections.unmodifiableList(Arrays.asList( - "to", "from", "subject", "text" + "to", "from", "subject", "text", "message" )); static boolean needsHeaders(EntityMessage message, List rules) { @@ -733,6 +737,31 @@ public class EntityRule { headers = Collections.list(new InternetHeaders(bis, true).getAllHeaders()); } + Map props = new HashMap<>(); + if (message != null) { + for (Field field : message.getClass().getDeclaredFields()) + try { + int modifiers = field.getModifiers(); + if (!Modifier.isPublic(modifiers) || Modifier.isStatic(modifiers)) + continue; + Class type = field.getType(); + if (type.isArray()) + continue; + if (type.isPrimitive() || + Boolean.class.isAssignableFrom(type) || + Integer.class.isAssignableFrom(type) || + Long.class.isAssignableFrom(type) || + String.class.isAssignableFrom(type)) { + field.setAccessible(true); + Object value = field.get(message); + props.put(field.getName(), value); + } else + Log.i("EXPR skipping non primitive=" + field.getName()); + } catch (Throwable ex) { + Log.w(ex); + } + } + ExpressionConfiguration configuration = ExpressionConfiguration.defaultConfiguration(); configuration.getFunctionDictionary().addFunction("Header", new HeaderFunction(headers)); @@ -745,7 +774,8 @@ public class EntityRule { .with("to", to) .with("from", from) .with("subject", message == null ? null : Arrays.asList(message.subject)) - .with("text", doc == null ? null : Arrays.asList(doc.text())); + .with("text", doc == null ? null : Arrays.asList(doc.text())) + .with("message", props); if (message != null) { boolean hasAttachments = false;