Added message language to rules

master
M66B 3 weeks ago
parent 91144624bf
commit 2d03388fba

@ -3028,6 +3028,7 @@ The following extra operators are available:
The following extra functions are available: The following extra functions are available:
* *header("name")* (returns an array of header values for the named header) * *header("name")* (returns an array of header values for the named header)
* *message("language")* returns the two letter [ISO language code](https://en.wikipedia.org/wiki/List_of_ISO_639_language_codes) of a message; note that it cannot always be determined and that it can be incorrect (since version 1.2330)
* *blocklist()* (version 1.2176-1.2178; deprecated, use *onBlocklist()* instead) * *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) * *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) * *hasMx()* (returns a boolean indicating if the from/reply-to address has an associated MX record; since version 1.2179)
@ -3050,6 +3051,8 @@ Example conditions:
```AI("Is the message below a scam? Answer in English with just yes or no.") contains "yes"``` ```AI("Is the message below a scam? Answer in English with just yes or no.") contains "yes"```
```COALESCE(message("language")) == "en"```
<br> <br>
**Actions** **Actions**

@ -108,7 +108,7 @@ public class ExpressionHelper {
} }
HeaderFunction fHeader = new HeaderFunction(headers); HeaderFunction fHeader = new HeaderFunction(headers);
MessageFunction fMessage = new MessageFunction(message); MessageFunction fMessage = new MessageFunction(context, message, doc);
BlocklistFunction fBlocklist = new BlocklistFunction(context, message, headers); BlocklistFunction fBlocklist = new BlocklistFunction(context, message, headers);
MxFunction fMx = new MxFunction(context, message); MxFunction fMx = new MxFunction(context, message);
AttachmentsFunction fAttachments = new AttachmentsFunction(context, message); AttachmentsFunction fAttachments = new AttachmentsFunction(context, message);
@ -189,7 +189,7 @@ public class ExpressionHelper {
Token token = node.getToken(); Token token = node.getToken();
Log.i("EXPR token=" + token.getType() + ":" + token.getValue()); Log.i("EXPR token=" + token.getType() + ":" + token.getValue());
if (token.getType() == Token.TokenType.FUNCTION && if (token.getType() == Token.TokenType.FUNCTION &&
"AI".equalsIgnoreCase(token.getValue())) { ("AI".equalsIgnoreCase(token.getValue()) || "message".equalsIgnoreCase(token.getValue()))) {
Log.i("EXPR needs body"); Log.i("EXPR needs body");
return true; return true;
} }
@ -232,10 +232,14 @@ public class ExpressionHelper {
@FunctionParameter(name = "value") @FunctionParameter(name = "value")
public static class MessageFunction extends AbstractFunction { public static class MessageFunction extends AbstractFunction {
private Context context;
private final EntityMessage message; private final EntityMessage message;
private final Document doc;
MessageFunction(EntityMessage message) { MessageFunction(Context context, EntityMessage message, Document doc) {
this.context = context;
this.message = message; this.message = message;
this.doc = doc;
} }
@Override @Override
@ -246,6 +250,16 @@ public class ExpressionHelper {
try { try {
if (parameterValues.length == 1) { if (parameterValues.length == 1) {
String name = parameterValues[0].getStringValue(); String name = parameterValues[0].getStringValue();
if ("language".equalsIgnoreCase(name)) {
String language;
if (message.language == null)
language = (doc == null ? null : HtmlHelper.getLanguage(context, message.subject, doc.text()));
else
language = message.language;
Log.i("EXPR language=" + language);
if (language != null)
result.add(language);
} else {
if (name != null && message != null) { if (name != null && message != null) {
Field field = message.getClass().getField(name); Field field = message.getClass().getField(name);
field.setAccessible(true); field.setAccessible(true);
@ -254,12 +268,14 @@ public class ExpressionHelper {
result.add(value); result.add(value);
} }
} }
}
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e("EXPR", ex); Log.e("EXPR", ex);
} }
Log.i("EXPR message(" + parameterValues[0] + ")=" + TextUtils.join(", ", result)); EvaluationValue val = EvaluationValue.of(result, ExpressionConfiguration.defaultConfiguration());
return EvaluationValue.of(result, ExpressionConfiguration.defaultConfiguration()); Log.i("EXPR message(" + parameterValues[0] + ")=" + TextUtils.join(", ", result) + " val=" + val);
return val;
} }
} }

@ -1658,6 +1658,7 @@ X-Google-Original-From: Somebody &lt;somebody+extra@example.org&gt;</code></pre>
<p>The following extra functions are available:</p> <p>The following extra functions are available:</p>
<ul> <ul>
<li><em>header(“name”)</em> (returns an array of header values for the named header)</li> <li><em>header(“name”)</em> (returns an array of header values for the named header)</li>
<li><em>message(“language”)</em> returns the two letter <a href="https://en.wikipedia.org/wiki/List_of_ISO_639_language_codes">ISO language code</a> of a message; note that it cannot always be determined and that it can be incorrect (since version 1.2330)</li>
<li><em>blocklist()</em> (version 1.2176-1.2178; deprecated, use <em>onBlocklist()</em> instead)</li> <li><em>blocklist()</em> (version 1.2176-1.2178; deprecated, use <em>onBlocklist()</em> instead)</li>
<li><em>onBlocklist()</em> (returns a boolean indicating if the sender/server is on a DNS blocklist; since version 1.2179)</li> <li><em>onBlocklist()</em> (returns a boolean indicating if the sender/server is on a DNS blocklist; since version 1.2179)</li>
<li><em>hasMx()</em> (returns a boolean indicating if the from/reply-to address has an associated MX record; since version 1.2179)</li> <li><em>hasMx()</em> (returns a boolean indicating if the from/reply-to address has an associated MX record; since version 1.2179)</li>
@ -1674,6 +1675,7 @@ X-Google-Original-From: Somebody &lt;somebody+extra@example.org&gt;</code></pre>
<p><code>!onBlocklist() &amp;&amp; hasMx() &amp;&amp; attachments() &gt; 0</code></p> <p><code>!onBlocklist() &amp;&amp; hasMx() &amp;&amp; attachments() &gt; 0</code></p>
<p><code>(received + 7*24*60*60*1000) &lt; DT_DATE_TO_EPOCH(DT_NOW())</code></p> <p><code>(received + 7*24*60*60*1000) &lt; DT_DATE_TO_EPOCH(DT_NOW())</code></p>
<p><code>AI("Is the message below a scam? Answer in English with just yes or no.") contains "yes"</code></p> <p><code>AI("Is the message below a scam? Answer in English with just yes or no.") contains "yes"</code></p>
<p><code>COALESCE(message("language")) == "en"</code></p>
<p><br></p> <p><br></p>
<p><strong>Actions</strong></p> <p><strong>Actions</strong></p>
<p>You can select one of these actions to apply to matching messages:</p> <p>You can select one of these actions to apply to matching messages:</p>

Loading…
Cancel
Save