Expression: added known()

This commit is contained in:
M66B 2024-04-29 21:35:12 +02:00
parent 8c8337270a
commit c131de5f0e
3 changed files with 55 additions and 0 deletions

1
FAQ.md
View File

@ -2785,6 +2785,7 @@ The following extra functions are available:
* *attachments()* (returns an integer indicating number of attachments; since version 1.2179)
* *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)
* *Known()* (returns a boolean indicating that the from/reply-to address is in the Android address book or in the local contacts database)
Example conditions:

View File

@ -22,8 +22,11 @@ package eu.faircode.email;
import static com.ezylang.evalex.operators.OperatorIfc.OPERATOR_PRECEDENCE_COMPARISON;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import androidx.preference.PreferenceManager;
import com.ezylang.evalex.Expression;
import com.ezylang.evalex.config.ExpressionConfiguration;
import com.ezylang.evalex.data.EvaluationValue;
@ -53,6 +56,7 @@ import java.util.regex.Pattern;
import javax.mail.Address;
import javax.mail.Header;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.InternetHeaders;
public class ExpressionHelper {
@ -95,6 +99,7 @@ public class ExpressionHelper {
AttachmentsFunction fAttachments = new AttachmentsFunction(context, message);
JsoupFunction fJsoup = new JsoupFunction(context, message);
SizeFunction fSize = new SizeFunction();
KnownFunction fKnown = new KnownFunction(context, message);
ContainsOperator oContains = new ContainsOperator(false);
ContainsOperator oMatches = new ContainsOperator(true);
@ -109,6 +114,7 @@ public class ExpressionHelper {
configuration.getFunctionDictionary().addFunction("attachments", fAttachments);
configuration.getFunctionDictionary().addFunction("Jsoup", fJsoup);
configuration.getFunctionDictionary().addFunction("Size", fSize);
configuration.getFunctionDictionary().addFunction("Known", fKnown);
configuration.getOperatorDictionary().addOperator("Contains", oContains);
configuration.getOperatorDictionary().addOperator("Matches", oMatches);
@ -364,6 +370,53 @@ public class ExpressionHelper {
}
}
public static class KnownFunction extends AbstractFunction {
private final Context context;
private final EntityMessage message;
KnownFunction(Context context, EntityMessage message) {
this.context = context;
this.message = message;
}
@Override
public EvaluationValue evaluate(
Expression expression, Token functionToken, EvaluationValue... parameterValues) {
boolean result = false;
if (message != null)
if (message.avatar != null)
result = true;
else {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean suggest_sent = prefs.getBoolean("suggest_sent", true);
if (suggest_sent) {
DB db = DB.getInstance(context);
List<Address> senders = new ArrayList<>();
if (message.from != null)
senders.addAll(Arrays.asList(message.from));
if (message.reply != null)
senders.addAll(Arrays.asList(message.reply));
for (Address sender : senders) {
InternetAddress ia = (InternetAddress) sender;
String email = ia.getAddress();
EntityContact contact =
db.contact().getContact(message.account, EntityContact.TYPE_TO, email);
if (contact != null) {
result = true;
break;
}
}
}
}
Log.i("EXPR known()=" + result);
return expression.convertValue(result);
}
}
@InfixOperator(precedence = OPERATOR_PRECEDENCE_COMPARISON)
public static class ContainsOperator extends AbstractOperator {
private final boolean regex;

View File

@ -1524,6 +1524,7 @@ X-Google-Original-From: Somebody &lt;somebody+extra@example.org&gt;</code></pre>
<li><em>attachments()</em> (returns an integer indicating number of attachments; since version 1.2179)</li>
<li><em>Jsoup()</em> (returns an array of selected strings; since version 1.2179)</li>
<li><em>Size(array)</em> (returns the number of items in an array; since version 1.2179)</li>
<li><em>Known()</em> (returns a boolean indicating that the from/reply-to address is in the Android address book or in the local contacts database)</li>
</ul>
<p>Example conditions:</p>
<p><code>header("X-Mailer") contains "Open-Xchange" &amp;&amp; from matches ".*service@.*"</code></p>