package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FairEmail is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FairEmail. If not, see .
Copyright 2018-2024 by Marcel Bokhorst (M66B)
*/
import static androidx.room.ForeignKey.CASCADE;
import static com.ezylang.evalex.operators.OperatorIfc.OPERATOR_PRECEDENCE_COMPARISON;
import android.Manifest;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.text.TextUtils;
import android.util.Patterns;
import androidx.annotation.NonNull;
import androidx.preference.PreferenceManager;
import androidx.room.Entity;
import androidx.room.ForeignKey;
import androidx.room.Index;
import androidx.room.PrimaryKey;
import com.ezylang.evalex.EvaluationException;
import com.ezylang.evalex.Expression;
import com.ezylang.evalex.config.ExpressionConfiguration;
import com.ezylang.evalex.data.EvaluationValue;
import com.ezylang.evalex.functions.AbstractFunction;
import com.ezylang.evalex.functions.FunctionParameter;
import com.ezylang.evalex.operators.AbstractOperator;
import com.ezylang.evalex.operators.InfixOperator;
import com.ezylang.evalex.parser.ASTNode;
import com.ezylang.evalex.parser.ParseException;
import com.ezylang.evalex.parser.Token;
import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
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;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
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;
import java.util.regex.Pattern;
import javax.mail.Address;
import javax.mail.Header;
import javax.mail.MessagingException;
import javax.mail.Part;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.InternetHeaders;
import javax.net.ssl.HttpsURLConnection;
@Entity(
tableName = EntityRule.TABLE_NAME,
foreignKeys = {
@ForeignKey(childColumns = "folder", entity = EntityFolder.class, parentColumns = "id", onDelete = CASCADE),
},
indices = {
@Index(value = {"folder"}),
@Index(value = {"order"})
}
)
public class EntityRule {
static final String TABLE_NAME = "rule";
@PrimaryKey(autoGenerate = true)
public Long id;
@NonNull
public String uuid = UUID.randomUUID().toString();
@NonNull
public Long folder;
@NonNull
public String name;
public String group;
@NonNull
public int order;
@NonNull
public boolean enabled;
@NonNull
public boolean daily;
@NonNull
public boolean stop;
@NonNull
public String condition;
@NonNull
public String action;
@NonNull
public Integer applied = 0;
public Long last_applied;
static final int TYPE_SEEN = 1;
static final int TYPE_UNSEEN = 2;
static final int TYPE_MOVE = 3;
static final int TYPE_ANSWER = 4;
static final int TYPE_AUTOMATION = 5;
static final int TYPE_FLAG = 6;
static final int TYPE_COPY = 7;
static final int TYPE_SNOOZE = 8;
static final int TYPE_IGNORE = 9;
static final int TYPE_NOOP = 10;
static final int TYPE_KEYWORD = 11;
static final int TYPE_HIDE = 12;
static final int TYPE_IMPORTANCE = 13;
static final int TYPE_TTS = 14;
static final int TYPE_DELETE = 15;
static final int TYPE_SOUND = 16;
static final int TYPE_LOCAL_ONLY = 17;
static final int TYPE_NOTES = 18;
static final int TYPE_URL = 19;
static final int TYPE_SILENT = 20;
static final String ACTION_AUTOMATION = BuildConfig.APPLICATION_ID + ".AUTOMATION";
static final String EXTRA_RULE = "rule";
static final String EXTRA_SENDER = "sender";
static final String EXTRA_NAME = "name";
static final String EXTRA_SUBJECT = "subject";
static final String EXTRA_RECEIVED = "received";
static final String[] EXTRA_ALL = new String[]{
EXTRA_RULE, EXTRA_SENDER, EXTRA_NAME, EXTRA_SUBJECT, EXTRA_RECEIVED
};
static final String JSOUP_PREFIX = "jsoup:";
private static final long SEND_DELAY = 5000L; // milliseconds
private static final int MAX_NOTES_LENGTH = 512; // characters
private static final int URL_TIMEOUT = 15 * 1000; // milliseconds
private static final List EXPR_VARIABLES = Collections.unmodifiableList(Arrays.asList(
"to", "from", "subject", "text", "hasAttachments"
));
static boolean needsHeaders(EntityMessage message, List rules) {
return needsHeaders(rules);
}
static boolean needsHeaders(List rules) {
return needs(rules, "header");
}
static boolean needsBody(EntityMessage message, List rules) {
if (message.encrypt != null && !EntityMessage.ENCRYPT_NONE.equals(message.encrypt))
return false;
return needsBody(rules);
}
static boolean needsBody(List rules) {
return needs(rules, "body") || needs(rules, "notes_jsoup");
}
private static boolean needs(List rules, String what) {
for (EntityRule rule : rules)
try {
JSONObject jcondition = new JSONObject(rule.condition);
if (jcondition.has(what)) {
if ("header".equals(what)) {
JSONObject jheader = jcondition.getJSONObject("header");
String value = jheader.getString("value");
boolean regex = jheader.getBoolean("regex");
if (!regex && value.startsWith("$$") && value.endsWith("$"))
continue;
}
return true;
}
if (jcondition.has("expression")) {
Expression expression = getExpression(rule, null, null, null, null);
if (expression != null) {
if ("header".equals(what) && needsHeaders(expression))
return true;
if ("body".equals(what) && needsBody(expression))
return true;
}
}
} catch (Throwable ex) {
Log.e(ex);
}
return false;
}
static int run(Context context, List rules,
EntityMessage message, List headers, String html)
throws JSONException, MessagingException, IOException {
int applied = 0;
List stopped = new ArrayList<>();
for (EntityRule rule : rules) {
if (rule.group != null && stopped.contains(rule.group))
continue;
if (rule.matches(context, message, headers, html)) {
if (rule.execute(context, message, html))
applied++;
if (rule.stop)
if (rule.group == null)
break;
else {
if (!stopped.contains(rule.group))
stopped.add(rule.group);
}
}
}
return applied;
}
boolean matches(Context context, EntityMessage message, List headers, String html) throws MessagingException {
try {
JSONObject jcondition = new JSONObject(condition);
// general
if (this.daily) {
JSONObject jgeneral = jcondition.optJSONObject("general");
if (jgeneral != null) {
int age = jgeneral.optInt("age");
if (age > 0) {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(message.received);
cal.add(Calendar.DAY_OF_MONTH, age);
if (cal.getTimeInMillis() > new Date().getTime())
return false;
}
}
}
// Sender
JSONObject jsender = jcondition.optJSONObject("sender");
if (jsender != null) {
boolean not = jsender.optBoolean("not");
String value = jsender.getString("value");
boolean regex = jsender.getBoolean("regex");
boolean known = jsender.optBoolean("known");
boolean matches = false;
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();
String personal = ia.getPersonal();
if (known) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean suggest_sent = prefs.getBoolean("suggest_sent", true);
if (suggest_sent) {
DB db = DB.getInstance(context);
EntityContact contact =
db.contact().getContact(message.account, EntityContact.TYPE_TO, email);
if (contact != null) {
Log.i(email + " is local contact");
matches = true;
break;
}
}
if (!TextUtils.isEmpty(message.avatar)) {
Log.i(email + " is Android contact");
matches = true;
break;
}
} else {
String formatted = ((personal == null ? "" : personal + " ") + "<" + email + ">");
if (matches(context, message, value, formatted, regex)) {
matches = true;
break;
}
}
}
if (matches == not)
return false;
}
// Recipient
JSONObject jrecipient = jcondition.optJSONObject("recipient");
if (jrecipient != null) {
boolean not = jrecipient.optBoolean("not");
String value = jrecipient.getString("value");
boolean regex = jrecipient.getBoolean("regex");
boolean matches = false;
List recipients = new ArrayList<>();
if (message.to != null)
recipients.addAll(Arrays.asList(message.to));
if (message.cc != null)
recipients.addAll(Arrays.asList(message.cc));
if (message.bcc != null)
recipients.addAll(Arrays.asList(message.bcc));
for (Address recipient : recipients) {
InternetAddress ia = (InternetAddress) recipient;
String personal = ia.getPersonal();
String formatted = ((personal == null ? "" : personal + " ") + "<" + ia.getAddress() + ">");
if (matches(context, message, value, formatted, regex)) {
matches = true;
break;
}
}
if (matches == not)
return false;
}
// Subject
JSONObject jsubject = jcondition.optJSONObject("subject");
if (jsubject != null) {
boolean not = jsubject.optBoolean("not");
String value = jsubject.getString("value");
boolean regex = jsubject.getBoolean("regex");
if (matches(context, message, value, message.subject, regex) == not)
return false;
}
// Attachments
if (jcondition.optBoolean("attachments")) {
DB db = DB.getInstance(context);
List attachments = db.attachment().getAttachments(message.id);
if (attachments.size() == 0)
return false;
if (jcondition.has("mimetype")) {
String mimeType = jcondition.getString("mimetype");
if (!TextUtils.isEmpty(mimeType)) {
boolean found = false;
for (EntityAttachment attachment : attachments)
if (mimeType.equalsIgnoreCase(attachment.getMimeType())) {
found = true;
break;
}
if (!found)
return false;
}
}
}
// Header
JSONObject jheader = jcondition.optJSONObject("header");
if (jheader != null) {
boolean not = jheader.optBoolean("not");
String value = jheader.getString("value");
boolean regex = jheader.getBoolean("regex");
if (!regex &&
value.startsWith("$") &&
value.endsWith("$")) {
if (matchKeywords(context, message, value) != not)
return false;
} else {
if (headers == null) {
if (message.headers == null)
throw new IllegalArgumentException(context.getString(R.string.title_rule_no_headers));
ByteArrayInputStream bis = new ByteArrayInputStream(message.headers.getBytes());
headers = Collections.list(new InternetHeaders(bis, true).getAllHeaders());
}
boolean matches = false;
for (Header header : headers) {
String formatted = header.getName() + ": " + header.getValue();
if (matches(context, message, value, formatted, regex)) {
matches = true;
break;
}
}
if (matches == not)
return false;
}
}
// Body
JSONObject jbody = jcondition.optJSONObject("body");
if (jbody != null) {
boolean not = jbody.optBoolean("not");
String value = jbody.getString("value");
boolean regex = jbody.getBoolean("regex");
boolean skip_quotes = jbody.optBoolean("skip_quotes");
boolean jsoup = value.startsWith(JSOUP_PREFIX);
if (!regex && !jsoup)
value = value.replaceAll("\\s+", " ");
if (html == null && message.content) {
File file = message.getFile(context);
try {
html = Helper.readText(file);
} catch (IOException ex) {
Log.e(ex);
}
}
if (html == null)
if (false && (message.encrypt == null || EntityMessage.ENCRYPT_NONE.equals(message.encrypt)))
throw new IllegalArgumentException(context.getString(R.string.title_rule_no_body));
else
return false;
Document d = JsoupEx.parse(html);
if (skip_quotes)
d.select("blockquote").remove();
if (jsoup) {
String selector = value.substring(JSOUP_PREFIX.length());
if (d.select(selector).isEmpty() != not)
return false;
} else {
String text = d.body().text();
if (matches(context, message, value, text, regex) == not)
return false;
}
}
// Date
JSONObject jdate = jcondition.optJSONObject("date");
if (jdate != null) {
long after = jdate.optLong("after", 0);
long before = jdate.optLong("before", 0);
if ((after != 0 && message.received < after) || (before != 0 && message.received > before))
return false;
}
// Schedule
JSONObject jschedule = jcondition.optJSONObject("schedule");
if (jschedule != null) {
boolean all = jschedule.optBoolean("all", false);
int start = jschedule.optInt("start", 0);
int end = jschedule.optInt("end", 0);
Calendar cal_start = getRelativeCalendar(all, start, message.received);
Calendar cal_end = getRelativeCalendar(all, end, message.received);
if (cal_start.getTimeInMillis() > cal_end.getTimeInMillis())
if (all)
if (cal_end.getTimeInMillis() < message.received)
cal_end.add(Calendar.DATE, 1);
else
cal_start.add(Calendar.DATE, -1);
else
cal_start.add(Calendar.HOUR_OF_DAY, -7 * 24);
if (message.received < cal_start.getTimeInMillis() ||
message.received > cal_end.getTimeInMillis())
return false;
}
// Younger
if (jcondition.has("younger")) {
int younger = jcondition.getInt("younger");
Calendar y = Calendar.getInstance();
y.add(Calendar.HOUR_OF_DAY, -younger);
if (message.received < y.getTimeInMillis())
return false;
}
// Expression
Expression expression = getExpression(this, message, headers, html, context);
if (expression != null) {
if (needsHeaders(expression) && headers == null && message.headers == null)
throw new IllegalArgumentException(context.getString(R.string.title_rule_no_headers));
Log.i("EXPR evaluating='" + jcondition.getString("expression") + "'");
Boolean result = expression.evaluate().getBooleanValue();
Log.i("EXPR evaluated=" + result);
if (!Boolean.TRUE.equals(result))
return false;
}
// Safeguard
if (jsender == null &&
jrecipient == null &&
jsubject == null &&
!jcondition.optBoolean("attachments") &&
jheader == null &&
jbody == null &&
jdate == null &&
jschedule == null &&
!jcondition.has("younger") &&
!jcondition.has("expression"))
return false;
} catch (JSONException | ParseException | EvaluationException ex) {
Log.e(ex);
return false;
}
return true;
}
private static boolean matchKeywords(Context context, EntityMessage message, String value) {
String keyword = value.substring(1, value.length() - 1);
if ("$tls".equals(keyword)) {
if (!Boolean.TRUE.equals(message.tls))
return true;
} else if ("$aligned".equals(keyword)) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean native_dkim = prefs.getBoolean("native_dkim", false);
if (!native_dkim)
return true;
if (message.signedby == null)
return true;
if (message.from == null || message.from.length != 1)
return true;
String domain = UriHelper.getEmailDomain(((InternetAddress) message.from[0]).getAddress());
if (domain == null)
return true;
boolean valid = false;
for (String signer : message.signedby.split(","))
if (Objects.equals(
UriHelper.getRootDomain(context, signer),
UriHelper.getRootDomain(context, domain))) {
valid = true;
break;
}
if (!valid)
return true;
} else if ("$dkim".equals(keyword)) {
if (!Boolean.TRUE.equals(message.dkim))
return true;
} else if ("$spf".equals(keyword)) {
if (!Boolean.TRUE.equals(message.spf))
return true;
} else if ("$dmarc".equals(keyword)) {
if (!Boolean.TRUE.equals(message.dmarc))
return true;
} else if ("$auth".equals(keyword)) {
if (!Boolean.TRUE.equals(message.auth))
return true;
} else if ("$mx".equals(keyword)) {
if (!Boolean.TRUE.equals(message.mx))
return true;
} else if ("$blocklist".equals(keyword)) {
if (!Boolean.FALSE.equals(message.blocklist))
return true;
} else if ("$replydomain".equals(keyword)) {
if (!Boolean.TRUE.equals(message.reply_domain))
return true;
} else if ("$nofrom".equals(keyword)) {
if (message.from != null && message.from.length > 0)
return true;
} else if ("$multifrom".equals(keyword)) {
if (message.from == null || message.from.length < 2)
return true;
} else if ("$automatic".equals(keyword)) {
if (!Boolean.TRUE.equals(message.auto_submitted))
return true;
} else if ("$lowpriority".equals(keyword)) {
if (!EntityMessage.PRIORITIY_LOW.equals(message.priority))
return true;
} else if ("$highpriority".equals(keyword)) {
if (!EntityMessage.PRIORITIY_HIGH.equals(message.priority))
return true;
} else if ("$signed".equals(keyword)) {
if (!message.isSigned())
return true;
} else if ("$encrypted".equals(keyword)) {
if (!message.isEncrypted())
return true;
} else {
List keywords = new ArrayList<>();
keywords.addAll(Arrays.asList(message.keywords));
if (message.ui_seen)
keywords.add("$seen");
if (message.ui_answered)
keywords.add("$answered");
if (message.ui_flagged)
keywords.add("$flagged");
if (message.ui_deleted)
keywords.add("$deleted");
if (message.infrastructure != null)
keywords.add('$' + message.infrastructure);
if (!keywords.contains(keyword))
return true;
}
return false;
}
private boolean matches(Context context, EntityMessage message, String needle, String haystack, boolean regex) {
boolean matched = false;
if (needle != null && haystack != null)
if (regex) {
Pattern pattern = Pattern.compile(needle, Pattern.DOTALL);
matched = pattern.matcher(haystack).matches();
} else
matched = haystack.toLowerCase().contains(needle.trim().toLowerCase());
if (matched)
EntityLog.log(context, EntityLog.Type.Rules, message,
"Rule=" + name + "@" + order + " matched " +
" needle=" + needle + " haystack=" + haystack + " regex=" + regex);
else
Log.i("Rule=" + name + "@" + order + " matched=" + matched +
" needle=" + needle + " haystack=" + haystack + " regex=" + regex);
return matched;
}
@FunctionParameter(name = "value")
public static class HeaderFunction extends AbstractFunction {
private List headers;
HeaderFunction(List headers) {
this.headers = headers;
}
@Override
public EvaluationValue evaluate(
Expression expression, Token functionToken, EvaluationValue... parameterValues) {
List result = new ArrayList<>();
try {
if (parameterValues.length == 1) {
String name = parameterValues[0].getStringValue();
if (name != null && headers != null)
for (Header header : headers)
if (name.equalsIgnoreCase(header.getName()))
result.add(header.getValue());
}
} catch (Throwable ex) {
Log.e("EXPR", ex);
}
Log.i("EXPR header(" + parameterValues[0] + ")=" + TextUtils.join(", ", result));
return new EvaluationValue(result, ExpressionConfiguration.defaultConfiguration());
}
}
@FunctionParameter(name = "value")
public static class MessageFunction extends AbstractFunction {
private EntityMessage message;
MessageFunction(EntityMessage message) {
this.message = message;
}
@Override
public EvaluationValue evaluate(
Expression expression, Token functionToken, EvaluationValue... parameterValues) {
List