diff --git a/FAQ.md b/FAQ.md index 07ca2531e5..a03b8c7796 100644 --- a/FAQ.md +++ b/FAQ.md @@ -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: diff --git a/app/src/main/java/eu/faircode/email/ExpressionHelper.java b/app/src/main/java/eu/faircode/email/ExpressionHelper.java index 754cccec41..55c17d7f5e 100644 --- a/app/src/main/java/eu/faircode/email/ExpressionHelper.java +++ b/app/src/main/java/eu/faircode/email/ExpressionHelper.java @@ -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
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; diff --git a/index.html b/index.html index 89bcfaf689..98e08785d6 100644 --- a/index.html +++ b/index.html @@ -1524,6 +1524,7 @@ X-Google-Original-From: Somebody <somebody+extra@example.org>
  • 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:

    header("X-Mailer") contains "Open-Xchange" && from matches ".*service@.*"