FairEmail/app/src/main/java/eu/faircode/email/EntityRule.java

1941 lines
77 KiB
Java
Raw Normal View History

2019-01-17 13:29:35 +00:00
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 <http://www.gnu.org/licenses/>.
2024-01-01 07:50:49 +00:00
Copyright 2018-2024 by Marcel Bokhorst (M66B)
2019-01-17 13:29:35 +00:00
*/
2021-08-16 11:40:42 +00:00
import static androidx.room.ForeignKey.CASCADE;
2024-04-17 10:53:07 +00:00
import static com.ezylang.evalex.operators.OperatorIfc.OPERATOR_PRECEDENCE_COMPARISON;
2023-01-09 19:04:11 +00:00
import android.Manifest;
import android.content.ContentResolver;
2019-01-17 13:29:35 +00:00
import android.content.Context;
2019-03-17 12:49:10 +00:00
import android.content.Intent;
2019-09-22 09:44:11 +00:00
import android.content.SharedPreferences;
2020-07-03 14:44:59 +00:00
import android.content.res.Configuration;
import android.content.res.Resources;
2023-01-09 19:04:11 +00:00
import android.database.Cursor;
2019-03-14 16:02:27 +00:00
import android.net.Uri;
2023-01-09 19:04:11 +00:00
import android.provider.ContactsContract;
2019-10-09 09:09:00 +00:00
import android.text.TextUtils;
2023-10-02 16:45:50 +00:00
import android.util.Patterns;
2019-01-17 13:29:35 +00:00
import androidx.annotation.NonNull;
2019-09-22 09:44:11 +00:00
import androidx.preference.PreferenceManager;
import androidx.room.Entity;
import androidx.room.ForeignKey;
import androidx.room.Index;
import androidx.room.PrimaryKey;
2024-04-17 10:53:07 +00:00
import com.ezylang.evalex.EvaluationException;
import com.ezylang.evalex.Expression;
import com.ezylang.evalex.config.ExpressionConfiguration;
import com.ezylang.evalex.data.EvaluationValue;
2024-04-17 14:27:33 +00:00
import com.ezylang.evalex.functions.AbstractFunction;
import com.ezylang.evalex.functions.FunctionParameter;
2024-04-17 10:53:07 +00:00
import com.ezylang.evalex.operators.AbstractOperator;
import com.ezylang.evalex.operators.InfixOperator;
2024-04-17 14:27:33 +00:00
import com.ezylang.evalex.parser.ASTNode;
2024-04-17 10:53:07 +00:00
import com.ezylang.evalex.parser.ParseException;
import com.ezylang.evalex.parser.Token;
2019-01-17 13:29:35 +00:00
import org.json.JSONException;
import org.json.JSONObject;
2020-07-13 14:34:55 +00:00
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
2019-01-17 13:29:35 +00:00
import java.io.ByteArrayInputStream;
2020-02-20 09:35:01 +00:00
import java.io.File;
2019-01-18 18:40:51 +00:00
import java.io.IOException;
2023-10-03 14:11:42 +00:00
import java.io.InputStream;
2023-10-02 16:45:50 +00:00
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
2019-03-17 11:11:12 +00:00
import java.util.ArrayList;
import java.util.Arrays;
2019-08-04 17:14:53 +00:00
import java.util.Calendar;
2021-12-08 11:18:38 +00:00
import java.util.Collections;
2019-01-18 18:40:51 +00:00
import java.util.Date;
2022-04-01 06:49:40 +00:00
import java.util.Iterator;
2019-03-17 11:11:12 +00:00
import java.util.List;
2020-07-03 14:44:59 +00:00
import java.util.Locale;
2020-12-24 07:30:03 +00:00
import java.util.Objects;
2022-06-27 06:57:35 +00:00
import java.util.UUID;
2023-08-15 13:00:56 +00:00
import java.util.regex.Matcher;
2019-01-17 13:29:35 +00:00
import java.util.regex.Pattern;
2019-01-18 10:58:55 +00:00
import javax.mail.Address;
2019-01-18 13:04:05 +00:00
import javax.mail.Header;
import javax.mail.MessagingException;
import javax.mail.Part;
import javax.mail.internet.AddressException;
2019-01-18 10:58:55 +00:00
import javax.mail.internet.InternetAddress;
import javax.mail.internet.InternetHeaders;
2023-12-06 14:31:05 +00:00
import javax.net.ssl.HttpsURLConnection;
2019-01-18 10:58:55 +00:00
2019-01-17 13:29:35 +00:00
@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
2022-06-27 06:57:35 +00:00
public String uuid = UUID.randomUUID().toString();
@NonNull
2019-01-17 13:29:35 +00:00
public Long folder;
@NonNull
public String name;
2023-04-01 16:46:05 +00:00
public String group;
2019-01-17 13:29:35 +00:00
@NonNull
public int order;
@NonNull
2019-01-17 21:25:22 +00:00
public boolean enabled;
@NonNull
2022-12-23 13:06:25 +00:00
public boolean daily;
@NonNull
2019-01-18 10:58:55 +00:00
public boolean stop;
@NonNull
2019-01-17 13:29:35 +00:00
public String condition;
@NonNull
public String action;
@NonNull
public Integer applied = 0;
2020-12-24 07:30:03 +00:00
public Long last_applied;
2019-01-17 13:29:35 +00:00
static final int TYPE_SEEN = 1;
static final int TYPE_UNSEEN = 2;
static final int TYPE_MOVE = 3;
2019-01-18 18:40:51 +00:00
static final int TYPE_ANSWER = 4;
2019-03-17 12:49:10 +00:00
static final int TYPE_AUTOMATION = 5;
2019-05-15 10:52:39 +00:00
static final int TYPE_FLAG = 6;
2019-05-18 06:34:26 +00:00
static final int TYPE_COPY = 7;
2019-06-01 13:27:21 +00:00
static final int TYPE_SNOOZE = 8;
2019-09-22 09:54:29 +00:00
static final int TYPE_IGNORE = 9;
2019-10-01 10:40:54 +00:00
static final int TYPE_NOOP = 10;
2019-10-09 09:09:00 +00:00
static final int TYPE_KEYWORD = 11;
2019-10-12 09:09:54 +00:00
static final int TYPE_HIDE = 12;
2020-02-01 12:51:32 +00:00
static final int TYPE_IMPORTANCE = 13;
2020-07-03 13:58:49 +00:00
static final int TYPE_TTS = 14;
2021-12-28 07:59:08 +00:00
static final int TYPE_DELETE = 15;
2022-01-04 08:11:51 +00:00
static final int TYPE_SOUND = 16;
static final int TYPE_LOCAL_ONLY = 17;
2023-08-14 07:39:53 +00:00
static final int TYPE_NOTES = 18;
2023-10-02 16:45:50 +00:00
static final int TYPE_URL = 19;
static final int TYPE_SILENT = 20;
2019-03-17 12:49:10 +00:00
static final String ACTION_AUTOMATION = BuildConfig.APPLICATION_ID + ".AUTOMATION";
static final String EXTRA_RULE = "rule";
static final String EXTRA_SENDER = "sender";
2023-06-05 20:09:10 +00:00
static final String EXTRA_NAME = "name";
2019-03-17 12:49:10 +00:00
static final String EXTRA_SUBJECT = "subject";
static final String EXTRA_RECEIVED = "received";
2019-01-17 13:29:35 +00:00
2023-10-03 14:17:18 +00:00
static final String[] EXTRA_ALL = new String[]{
EXTRA_RULE, EXTRA_SENDER, EXTRA_NAME, EXTRA_SUBJECT, EXTRA_RECEIVED
};
2023-08-14 08:58:36 +00:00
static final String JSOUP_PREFIX = "jsoup:";
2020-02-21 16:38:46 +00:00
private static final long SEND_DELAY = 5000L; // milliseconds
2023-08-14 09:26:45 +00:00
private static final int MAX_NOTES_LENGTH = 512; // characters
2023-10-02 16:45:50 +00:00
private static final int URL_TIMEOUT = 15 * 1000; // milliseconds
2020-02-21 16:38:46 +00:00
2024-04-17 10:53:07 +00:00
private static final List<String> EXPR_VARIABLES = Collections.unmodifiableList(Arrays.asList(
2024-04-17 14:27:33 +00:00
"to", "from", "subject", "text"
2024-04-17 10:53:07 +00:00
));
static boolean needsHeaders(EntityMessage message, List<EntityRule> rules) {
2023-08-14 15:11:24 +00:00
return needsHeaders(rules);
}
static boolean needsHeaders(List<EntityRule> rules) {
2021-12-08 19:10:19 +00:00
return needs(rules, "header");
}
static boolean needsBody(EntityMessage message, List<EntityRule> rules) {
if (message.encrypt != null && !EntityMessage.ENCRYPT_NONE.equals(message.encrypt))
return false;
2023-08-14 15:11:24 +00:00
return needsBody(rules);
}
static boolean needsBody(List<EntityRule> rules) {
2023-08-14 08:58:36 +00:00
return needs(rules, "body") || needs(rules, "notes_jsoup");
}
2021-12-08 19:10:19 +00:00
private static boolean needs(List<EntityRule> rules, String what) {
2021-12-08 11:18:38 +00:00
for (EntityRule rule : rules)
try {
JSONObject jcondition = new JSONObject(rule.condition);
2024-04-17 14:27:33 +00:00
2023-06-04 15:12:50 +00:00
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;
}
2021-12-08 11:18:38 +00:00
return true;
2023-06-04 15:12:50 +00:00
}
2024-04-17 14:27:33 +00:00
2024-04-17 10:53:07 +00:00
if (jcondition.has("expression")) {
2024-04-17 18:01:33 +00:00
Expression expression = getExpression(rule, null, null, null, null);
2024-04-17 14:27:33 +00:00
if (expression != null) {
if ("header".equals(what) && needsHeaders(expression))
return true;
2024-04-17 17:23:33 +00:00
if ("body".equals(what) && needsBody(expression))
return true;
2024-04-17 14:27:33 +00:00
}
2024-04-17 10:53:07 +00:00
}
2021-12-08 11:18:38 +00:00
} catch (Throwable ex) {
Log.e(ex);
}
return false;
}
2023-04-01 18:15:46 +00:00
static int run(Context context, List<EntityRule> rules,
EntityMessage message, List<Header> headers, String html)
2023-10-02 16:45:50 +00:00
throws JSONException, MessagingException, IOException {
2023-04-01 18:15:46 +00:00
int applied = 0;
List<String> stopped = new ArrayList<>();
for (EntityRule rule : rules) {
if (rule.group != null && stopped.contains(rule.group))
continue;
if (rule.matches(context, message, headers, html)) {
2023-08-14 08:58:36 +00:00
if (rule.execute(context, message, html))
2023-04-01 18:15:46 +00:00
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<Header> headers, String html) throws MessagingException {
2019-01-17 21:41:00 +00:00
try {
JSONObject jcondition = new JSONObject(condition);
2022-12-23 15:06:40 +00:00
// 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;
}
}
}
2019-03-17 11:11:12 +00:00
// Sender
2019-01-18 10:58:55 +00:00
JSONObject jsender = jcondition.optJSONObject("sender");
if (jsender != null) {
2024-04-14 20:53:05 +00:00
boolean not = jsender.optBoolean("not");
2019-01-18 13:04:05 +00:00
String value = jsender.getString("value");
2019-01-18 10:58:55 +00:00
boolean regex = jsender.getBoolean("regex");
2019-09-22 09:44:11 +00:00
boolean known = jsender.optBoolean("known");
2019-01-18 10:58:55 +00:00
boolean matches = false;
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();
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");
2019-09-22 09:44:11 +00:00
matches = true;
break;
}
2019-01-18 10:58:55 +00:00
}
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;
}
2019-01-18 10:58:55 +00:00
}
}
2024-04-14 20:53:05 +00:00
if (matches == not)
2019-01-18 10:58:55 +00:00
return false;
2019-01-17 21:41:00 +00:00
}
2019-03-17 11:11:12 +00:00
// Recipient
JSONObject jrecipient = jcondition.optJSONObject("recipient");
if (jrecipient != null) {
2024-04-14 20:53:05 +00:00
boolean not = jrecipient.optBoolean("not");
2019-03-17 11:11:12 +00:00
String value = jrecipient.getString("value");
boolean regex = jrecipient.getBoolean("regex");
boolean matches = false;
List<Address> 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));
2019-03-17 11:11:12 +00:00
for (Address recipient : recipients) {
InternetAddress ia = (InternetAddress) recipient;
String personal = ia.getPersonal();
String formatted = ((personal == null ? "" : personal + " ") + "<" + ia.getAddress() + ">");
2021-08-16 11:40:42 +00:00
if (matches(context, message, value, formatted, regex)) {
2019-03-17 11:11:12 +00:00
matches = true;
break;
}
}
2024-04-14 20:53:05 +00:00
if (matches == not)
2019-03-17 11:11:12 +00:00
return false;
}
2019-08-04 17:14:53 +00:00
// Subject
2019-01-18 10:58:55 +00:00
JSONObject jsubject = jcondition.optJSONObject("subject");
if (jsubject != null) {
2024-04-14 20:53:05 +00:00
boolean not = jsubject.optBoolean("not");
2019-01-18 13:04:05 +00:00
String value = jsubject.getString("value");
2019-01-18 10:58:55 +00:00
boolean regex = jsubject.getBoolean("regex");
2024-04-14 20:53:05 +00:00
if (matches(context, message, value, message.subject, regex) == not)
2019-01-18 13:04:05 +00:00
return false;
}
2019-10-13 14:47:35 +00:00
// Attachments
if (jcondition.optBoolean("attachments")) {
DB db = DB.getInstance(context);
List<EntityAttachment> attachments = db.attachment().getAttachments(message.id);
if (attachments.size() == 0)
2019-10-13 14:47:35 +00:00
return false;
if (jcondition.has("mimetype")) {
String mimeType = jcondition.getString("mimetype");
2020-11-28 14:17:56 +00:00
if (!TextUtils.isEmpty(mimeType)) {
boolean found = false;
for (EntityAttachment attachment : attachments)
if (mimeType.equalsIgnoreCase(attachment.getMimeType())) {
found = true;
break;
}
2020-11-28 14:17:56 +00:00
if (!found)
return false;
}
}
2019-10-13 14:47:35 +00:00
}
2019-08-04 17:14:53 +00:00
// Header
2019-01-18 13:04:05 +00:00
JSONObject jheader = jcondition.optJSONObject("header");
if (jheader != null) {
2024-04-15 07:11:36 +00:00
boolean not = jheader.optBoolean("not");
2019-01-18 13:04:05 +00:00
String value = jheader.getString("value");
boolean regex = jheader.getBoolean("regex");
2021-11-10 15:01:19 +00:00
if (!regex &&
value.startsWith("$") &&
value.endsWith("$")) {
2024-04-15 07:11:36 +00:00
if (matchKeywords(context, message, value) != not)
return false;
2021-11-10 15:01:19 +00:00
} else {
2021-12-08 11:18:38 +00:00
if (headers == null) {
if (message.headers == null)
throw new IllegalArgumentException(context.getString(R.string.title_rule_no_headers));
2021-11-10 15:01:19 +00:00
ByteArrayInputStream bis = new ByteArrayInputStream(message.headers.getBytes());
2022-08-13 12:03:41 +00:00
headers = Collections.list(new InternetHeaders(bis, true).getAllHeaders());
2021-12-08 11:18:38 +00:00
}
2021-12-08 21:56:39 +00:00
boolean matches = false;
2021-12-08 11:18:38 +00:00
for (Header header : headers) {
2021-11-10 15:01:19 +00:00
String formatted = header.getName() + ": " + header.getValue();
if (matches(context, message, value, formatted, regex)) {
matches = true;
break;
}
2019-01-18 13:04:05 +00:00
}
2024-04-15 07:11:36 +00:00
if (matches == not)
2021-11-10 15:01:19 +00:00
return false;
2019-01-18 13:04:05 +00:00
}
2019-01-17 21:41:00 +00:00
}
2019-01-18 10:58:55 +00:00
// Body
2022-07-01 15:54:42 +00:00
JSONObject jbody = jcondition.optJSONObject("body");
if (jbody != null) {
2024-04-14 20:53:05 +00:00
boolean not = jbody.optBoolean("not");
String value = jbody.getString("value");
boolean regex = jbody.getBoolean("regex");
boolean skip_quotes = jbody.optBoolean("skip_quotes");
2022-10-31 11:16:27 +00:00
boolean jsoup = value.startsWith(JSOUP_PREFIX);
if (!regex && !jsoup)
2021-12-09 10:05:06 +00:00
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)
2022-11-01 12:28:14 +00:00
if (false && (message.encrypt == null || EntityMessage.ENCRYPT_NONE.equals(message.encrypt)))
2022-07-02 06:44:55 +00:00
throw new IllegalArgumentException(context.getString(R.string.title_rule_no_body));
else
return false;
2022-01-09 09:53:22 +00:00
Document d = JsoupEx.parse(html);
if (skip_quotes)
d.select("blockquote").remove();
2022-10-31 11:16:27 +00:00
if (jsoup) {
String selector = value.substring(JSOUP_PREFIX.length());
2024-04-14 20:53:05 +00:00
if (d.select(selector).isEmpty() != not)
2022-10-31 11:16:27 +00:00
return false;
} else {
String text = d.body().text();
2024-04-14 20:53:05 +00:00
if (matches(context, message, value, text, regex) == not)
2022-10-31 11:16:27 +00:00
return false;
}
}
2021-03-27 10:51:57 +00:00
// 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;
}
2019-08-04 17:14:53 +00:00
// Schedule
JSONObject jschedule = jcondition.optJSONObject("schedule");
if (jschedule != null) {
2023-02-01 20:13:43 +00:00
boolean all = jschedule.optBoolean("all", false);
2019-08-04 17:14:53 +00:00
int start = jschedule.optInt("start", 0);
int end = jschedule.optInt("end", 0);
2023-02-01 20:13:43 +00:00
Calendar cal_start = getRelativeCalendar(all, start, message.received);
Calendar cal_end = getRelativeCalendar(all, end, message.received);
2019-08-21 11:32:25 +00:00
if (cal_start.getTimeInMillis() > cal_end.getTimeInMillis())
2023-02-01 20:13:43 +00:00
if (all)
2024-02-02 08:01:39 +00:00
if (cal_end.getTimeInMillis() < message.received)
cal_end.add(Calendar.DATE, 1);
else
cal_start.add(Calendar.DATE, -1);
2023-02-01 20:13:43 +00:00
else
cal_start.add(Calendar.HOUR_OF_DAY, -7 * 24);
2019-08-21 11:32:25 +00:00
if (message.received < cal_start.getTimeInMillis() ||
message.received > cal_end.getTimeInMillis())
2019-08-04 17:14:53 +00:00
return false;
}
2024-04-17 18:01:33 +00:00
// Younger
2023-04-19 19:54:10 +00:00
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;
}
2024-04-17 14:27:33 +00:00
// Expression
2024-04-17 18:01:33 +00:00
Expression expression = getExpression(this, message, headers, html, context);
2024-04-17 10:53:07 +00:00
if (expression != null) {
2024-04-17 17:51:52 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean experiments = prefs.getBoolean("experiments", false);
if (experiments) {
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;
}
2024-04-17 10:53:07 +00:00
}
2019-01-18 10:58:55 +00:00
// Safeguard
2019-10-13 14:47:35 +00:00
if (jsender == null &&
jrecipient == null &&
jsubject == null &&
!jcondition.optBoolean("attachments") &&
jheader == null &&
jbody == null &&
2021-03-27 10:51:57 +00:00
jdate == null &&
2023-04-19 19:54:10 +00:00
jschedule == null &&
2024-04-17 10:53:07 +00:00
!jcondition.has("younger") &&
!jcondition.has("expression"))
2019-01-18 10:58:55 +00:00
return false;
2024-04-17 10:53:07 +00:00
} catch (JSONException | ParseException | EvaluationException ex) {
2019-01-17 21:41:00 +00:00
Log.e(ex);
2019-01-18 10:58:55 +00:00
return false;
2019-01-17 13:29:35 +00:00
}
2019-01-18 10:58:55 +00:00
return true;
2019-01-17 13:29:35 +00:00
}
2024-04-15 07:11:36 +00:00
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<String> 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;
}
2021-08-16 11:40:42 +00:00
private boolean matches(Context context, EntityMessage message, String needle, String haystack, boolean regex) {
2019-02-04 13:01:36 +00:00
boolean matched = false;
if (needle != null && haystack != null)
if (regex) {
2019-09-02 19:52:33 +00:00
Pattern pattern = Pattern.compile(needle, Pattern.DOTALL);
2019-02-04 13:01:36 +00:00
matched = pattern.matcher(haystack).matches();
} else
2020-02-15 14:53:11 +00:00
matched = haystack.toLowerCase().contains(needle.trim().toLowerCase());
2019-02-04 13:01:36 +00:00
2020-03-22 09:47:32 +00:00
if (matched)
2021-08-16 11:40:42 +00:00
EntityLog.log(context, EntityLog.Type.Rules, message,
2022-12-24 10:00:50 +00:00
"Rule=" + name + "@" + order + " matched " +
2021-08-16 11:40:42 +00:00
" needle=" + needle + " haystack=" + haystack + " regex=" + regex);
2020-03-22 09:47:32 +00:00
else
2022-12-24 10:00:50 +00:00
Log.i("Rule=" + name + "@" + order + " matched=" + matched +
2020-03-22 09:47:32 +00:00
" needle=" + needle + " haystack=" + haystack + " regex=" + regex);
2019-02-04 13:01:36 +00:00
return matched;
2019-01-17 13:29:35 +00:00
}
2024-04-17 14:27:33 +00:00
@FunctionParameter(name = "value")
public static class HeaderFunction extends AbstractFunction {
private List<Header> headers;
HeaderFunction(List<Header> headers) {
this.headers = headers;
}
2024-04-17 10:53:07 +00:00
@Override
public EvaluationValue evaluate(
2024-04-17 14:27:33 +00:00
Expression expression, Token functionToken, EvaluationValue... parameterValues) {
2024-04-17 17:27:07 +00:00
List<String> result = new ArrayList<>();
2024-04-17 14:27:33 +00:00
String name = parameterValues[0].getStringValue();
2024-04-17 17:27:07 +00:00
if (name != null && headers != null) {
2024-04-17 14:27:33 +00:00
for (Header header : headers)
if (name.equalsIgnoreCase(header.getName()))
result.add(header.getValue());
2024-04-17 17:27:07 +00:00
Log.i("EXPR " + name + "=" + TextUtils.join(", ", result));
}
2024-04-17 14:27:33 +00:00
return new EvaluationValue(result, ExpressionConfiguration.defaultConfiguration());
2024-04-17 10:53:07 +00:00
}
}
@InfixOperator(precedence = OPERATOR_PRECEDENCE_COMPARISON)
2024-04-17 14:27:33 +00:00
public static class ContainsOperator extends AbstractOperator {
private boolean regex;
ContainsOperator(boolean regex) {
this.regex = regex;
}
2024-04-17 10:53:07 +00:00
@Override
public EvaluationValue evaluate(
Expression expression, Token operatorToken, EvaluationValue... operands) {
2024-04-17 14:27:33 +00:00
Log.i("EXPR " + operands[0] + (regex ? " MATCHES " : " CONTAINS ") + operands[1] + " regex=" + regex);
2024-04-17 17:30:11 +00:00
2024-04-17 14:27:33 +00:00
String condition = operands[1].getStringValue();
List<EvaluationValue> array = operands[0].getArrayValue();
2024-04-17 17:30:11 +00:00
if (TextUtils.isEmpty(condition) || array == null || array.isEmpty())
return expression.convertValue(false);
Pattern p = (regex ? Pattern.compile(condition, Pattern.DOTALL) : null);
for (EvaluationValue item : array) {
String value = item.getStringValue();
if (!TextUtils.isEmpty(value))
if (p == null) {
if (value.toLowerCase().contains(condition.toLowerCase()))
return expression.convertValue(true);
} else {
if (p.matcher(value).matches())
return expression.convertValue(true);
}
2024-04-17 14:27:33 +00:00
}
2024-04-17 17:30:11 +00:00
2024-04-17 14:27:33 +00:00
return expression.convertValue(false);
2024-04-17 10:53:07 +00:00
}
}
2024-04-17 18:01:33 +00:00
static Expression getExpression(EntityRule rule, EntityMessage message, List<Header> headers, String html, Context context) throws JSONException, ParseException, MessagingException {
2024-04-17 10:53:07 +00:00
// https://ezylang.github.io/EvalEx/
JSONObject jcondition = new JSONObject(rule.condition);
if (!jcondition.has("expression"))
return null;
2024-04-17 17:42:14 +00:00
String eval = jcondition.getString("expression");
2024-04-17 10:53:07 +00:00
2024-04-17 14:27:33 +00:00
List<String> to = new ArrayList<>();
if (message != null && message.to != null)
for (Address a : message.to)
to.add(MessageHelper.formatAddresses(new Address[]{a}));
List<String> from = new ArrayList<>();
if (message != null && message.from != null)
for (Address a : message.from)
from.add(MessageHelper.formatAddresses(new Address[]{a}));
2024-04-17 18:01:33 +00:00
if (html == null && message != null && message.content)
2024-04-17 14:27:33 +00:00
try {
2024-04-17 18:01:33 +00:00
html = Helper.readText(message.getFile(context));
2024-04-17 14:27:33 +00:00
} catch (IOException ex) {
Log.e(ex);
}
2024-04-17 18:01:33 +00:00
Document doc = (html == null ? null : JsoupEx.parse(html));
2024-04-17 14:27:33 +00:00
if (headers == null && message != null && message.headers != null) {
ByteArrayInputStream bis = new ByteArrayInputStream(message.headers.getBytes());
headers = Collections.list(new InternetHeaders(bis, true).getAllHeaders());
2024-04-17 10:53:07 +00:00
}
ExpressionConfiguration configuration = ExpressionConfiguration.defaultConfiguration();
2024-04-17 14:27:33 +00:00
configuration.getFunctionDictionary().addFunction("Header",
new HeaderFunction(headers));
configuration.getOperatorDictionary().addOperator("Contains",
new ContainsOperator(false));
configuration.getOperatorDictionary().addOperator("Matches",
new ContainsOperator(true));
2024-04-17 10:53:07 +00:00
2024-04-17 17:42:14 +00:00
Expression expression = new Expression(eval, configuration)
2024-04-17 14:27:33 +00:00
.with("to", to)
.with("from", from)
.with("subject", message == null ? null : Arrays.asList(message.subject))
2024-04-17 17:31:50 +00:00
.with("text", doc == null ? null : Arrays.asList(doc.text()));
2024-04-17 17:42:14 +00:00
if (message != null) {
boolean hasAttachments = false;
for (String variable : expression.getUsedVariables())
if (!hasAttachments && "attachments".equals(variable)) {
hasAttachments = true;
DB db = DB.getInstance(context);
List<EntityAttachment> attachments = db.attachment().getAttachments(message.id);
expression.with("attachments", attachments == null ? 0 : attachments.size());
}
}
return expression;
2024-04-17 14:27:33 +00:00
}
static boolean needsHeaders(Expression expression) {
try {
expression.validate();
for (ASTNode node : expression.getAllASTNodes()) {
Token token = node.getToken();
Log.i("EXPR token=" + token.getType() + ":" + token.getValue());
if (token.getType() == Token.TokenType.FUNCTION && "header".equalsIgnoreCase(token.getValue())) {
Log.i("EXPR needs headers");
return true;
}
}
} catch (Throwable ex) {
Log.e(ex);
}
return false;
2024-04-17 10:53:07 +00:00
}
2024-04-17 17:23:33 +00:00
static boolean needsBody(Expression expression) {
try {
for (String variable : expression.getUsedVariables())
if ("text".equalsIgnoreCase(variable))
return true;
} catch (Throwable ex) {
Log.e(ex);
}
return false;
}
2023-10-02 16:45:50 +00:00
boolean execute(Context context, EntityMessage message, String html) throws JSONException, IOException {
2023-08-14 08:58:36 +00:00
boolean executed = _execute(context, message, html);
2022-10-15 15:40:18 +00:00
if (this.id != null && executed) {
DB db = DB.getInstance(context);
2020-12-24 07:30:03 +00:00
db.rule().applyRule(id, new Date().getTime());
}
return executed;
}
2023-10-02 16:45:50 +00:00
private boolean _execute(Context context, EntityMessage message, String html) throws JSONException, IllegalArgumentException, IOException {
2019-07-16 06:25:30 +00:00
JSONObject jaction = new JSONObject(action);
int type = jaction.getInt("type");
2022-12-24 10:00:50 +00:00
EntityLog.log(context, EntityLog.Type.Rules, message,
"Executing rule=" + type + ":" + this.name + "@" + this.order);
2019-07-16 06:25:30 +00:00
switch (type) {
2019-10-01 10:40:54 +00:00
case TYPE_NOOP:
return true;
2019-07-16 06:25:30 +00:00
case TYPE_SEEN:
return onActionSeen(context, message, true);
case TYPE_UNSEEN:
return onActionSeen(context, message, false);
2019-10-12 09:09:54 +00:00
case TYPE_HIDE:
return onActionHide(context, message);
2019-09-22 09:54:29 +00:00
case TYPE_IGNORE:
return onActionIgnore(context, message, jaction);
2019-07-16 06:25:30 +00:00
case TYPE_SNOOZE:
return onActionSnooze(context, message, jaction);
case TYPE_FLAG:
return onActionFlag(context, message, jaction);
2020-02-01 12:51:32 +00:00
case TYPE_IMPORTANCE:
return onActionImportance(context, message, jaction);
2019-10-09 09:09:00 +00:00
case TYPE_KEYWORD:
return onActionKeyword(context, message, jaction);
2019-07-16 06:25:30 +00:00
case TYPE_MOVE:
return onActionMove(context, message, jaction);
case TYPE_COPY:
return onActionCopy(context, message, jaction);
case TYPE_ANSWER:
return onActionAnswer(context, message, jaction);
2020-07-03 13:58:49 +00:00
case TYPE_TTS:
return onActionTts(context, message, jaction);
2019-07-16 06:25:30 +00:00
case TYPE_AUTOMATION:
return onActionAutomation(context, message, jaction);
2021-12-28 07:59:08 +00:00
case TYPE_DELETE:
return onActionDelete(context, message, jaction);
2022-01-04 08:11:51 +00:00
case TYPE_SOUND:
return onActionSound(context, message, jaction);
case TYPE_LOCAL_ONLY:
return onActionLocalOnly(context, message, jaction);
2023-08-14 07:39:53 +00:00
case TYPE_NOTES:
2023-08-14 08:58:36 +00:00
return onActionNotes(context, message, jaction, html);
2023-10-02 16:45:50 +00:00
case TYPE_URL:
return onActionUrl(context, message, jaction, html);
case TYPE_SILENT:
return onActionSilent(context, message, jaction);
2019-07-16 06:25:30 +00:00
default:
2020-07-09 15:06:29 +00:00
throw new IllegalArgumentException("Unknown rule type=" + type + " name=" + name);
2019-01-17 13:29:35 +00:00
}
}
2020-11-19 17:54:44 +00:00
void validate(Context context) throws JSONException, IllegalArgumentException {
2024-04-17 14:27:33 +00:00
try {
2024-04-17 18:01:33 +00:00
Expression expression = getExpression(this, null, null, null, context);
2024-04-17 14:27:33 +00:00
if (expression != null) {
2024-04-17 10:53:07 +00:00
for (String variable : expression.getUsedVariables()) {
Log.i("EXPR variable=" + variable);
if (!EXPR_VARIABLES.contains(variable))
throw new IllegalArgumentException("Unknown variable '" + variable + "'");
}
Log.i("EXPR validating");
expression.validate();
Log.i("EXPR validated");
}
2024-04-17 14:27:33 +00:00
} catch (ParseException | MessagingException ex) {
Log.w("EXPR", ex);
String message = ex.getMessage();
if (TextUtils.isEmpty(message))
message = "Invalid expression";
throw new IllegalArgumentException(message, ex);
}
2024-04-17 10:53:07 +00:00
2020-11-19 17:54:44 +00:00
JSONObject jargs = new JSONObject(action);
int type = jargs.getInt("type");
DB db = DB.getInstance(context);
switch (type) {
case TYPE_NOOP:
return;
case TYPE_SEEN:
return;
case TYPE_UNSEEN:
return;
case TYPE_HIDE:
return;
case TYPE_IGNORE:
return;
case TYPE_SNOOZE:
return;
case TYPE_FLAG:
return;
case TYPE_IMPORTANCE:
return;
case TYPE_KEYWORD:
2021-07-29 07:46:27 +00:00
String keyword = jargs.optString("keyword");
2020-11-19 17:54:44 +00:00
if (TextUtils.isEmpty(keyword))
2020-11-19 18:10:14 +00:00
throw new IllegalArgumentException(context.getString(R.string.title_rule_keyword_missing));
2020-11-19 18:33:45 +00:00
return;
2020-11-19 17:54:44 +00:00
case TYPE_MOVE:
case TYPE_COPY:
long target = jargs.optLong("target", -1);
if (target < 0)
2020-11-19 18:10:14 +00:00
throw new IllegalArgumentException(context.getString(R.string.title_rule_folder_missing));
2020-11-19 17:54:44 +00:00
EntityFolder folder = db.folder().getFolder(target);
if (folder == null)
throw new IllegalArgumentException("Folder not found");
return;
case TYPE_ANSWER:
long iid = jargs.optLong("identity", -1);
if (iid < 0)
2020-11-19 18:10:14 +00:00
throw new IllegalArgumentException(context.getString(R.string.title_rule_identity_missing));
2020-11-19 17:54:44 +00:00
EntityIdentity identity = db.identity().getIdentity(iid);
if (identity == null)
throw new IllegalArgumentException("Identity not found");
long aid = jargs.optLong("answer", -1);
2021-04-01 16:35:52 +00:00
if (aid < 0) {
String to = jargs.optString("to");
if (TextUtils.isEmpty(to))
throw new IllegalArgumentException(context.getString(R.string.title_rule_answer_missing));
2022-04-03 09:11:16 +00:00
else
try {
InternetAddress[] addresses = MessageHelper.parseAddresses(context, to);
if (addresses == null || addresses.length == 0)
throw new IllegalArgumentException(context.getString(R.string.title_no_email));
} catch (AddressException ex) {
throw new IllegalArgumentException(context.getString(R.string.title_email_invalid, to));
}
2021-04-01 16:35:52 +00:00
} else {
EntityAnswer answer = db.answer().getAnswer(aid);
if (answer == null)
throw new IllegalArgumentException("Template not found");
}
2020-11-19 17:54:44 +00:00
return;
case TYPE_TTS:
return;
case TYPE_AUTOMATION:
return;
2021-12-28 07:59:08 +00:00
case TYPE_DELETE:
return;
2022-01-04 08:11:51 +00:00
case TYPE_SOUND:
return;
case TYPE_LOCAL_ONLY:
return;
2023-08-14 07:39:53 +00:00
case TYPE_NOTES:
String notes = jargs.optString("notes");
if (TextUtils.isEmpty(notes))
throw new IllegalArgumentException(context.getString(R.string.title_rule_notes_missing));
return;
2023-10-02 16:45:50 +00:00
case TYPE_URL:
String url = jargs.optString("url");
if (TextUtils.isEmpty(url) || !Patterns.WEB_URL.matcher(url).matches())
throw new IllegalArgumentException(context.getString(R.string.title_rule_url_missing));
return;
case TYPE_SILENT:
return;
2020-11-19 17:54:44 +00:00
default:
throw new IllegalArgumentException("Unknown rule type=" + type);
}
}
2019-07-16 06:25:30 +00:00
private boolean onActionSeen(Context context, EntityMessage message, boolean seen) {
2019-05-18 06:49:20 +00:00
EntityOperation.queue(context, message, EntityOperation.SEEN, seen);
2019-09-13 13:17:53 +00:00
2019-09-10 16:05:46 +00:00
message.ui_seen = seen;
message.ui_ignored = true;
2019-09-13 13:17:53 +00:00
2019-07-16 06:25:30 +00:00
return true;
2019-01-17 13:29:35 +00:00
}
2019-10-12 09:09:54 +00:00
private boolean onActionHide(Context context, EntityMessage message) {
DB db = DB.getInstance(context);
2023-04-19 05:24:57 +00:00
EntityFolder folder = db.folder().getFolder(message.folder);
if (EntityFolder.DRAFTS.equals(folder.type))
return false;
2019-10-12 09:09:54 +00:00
db.message().setMessageSnoozed(message.id, Long.MAX_VALUE);
db.message().setMessageUiIgnored(message.id, true);
EntityMessage.snooze(context, message.id, Long.MAX_VALUE);
message.ui_snoozed = Long.MAX_VALUE;
message.ui_ignored = true;
return true;
}
2019-09-22 09:54:29 +00:00
private boolean onActionIgnore(Context context, EntityMessage message, JSONObject jargs) {
DB db = DB.getInstance(context);
db.message().setMessageUiIgnored(message.id, true);
message.ui_ignored = true;
return true;
}
private boolean onActionMove(Context context, EntityMessage message, JSONObject jargs) {
2019-09-13 09:30:09 +00:00
long target = jargs.optLong("target", -1);
2022-09-16 08:06:16 +00:00
String create = jargs.optString("create");
boolean seen = jargs.optBoolean("seen");
boolean thread = jargs.optBoolean("thread");
2019-05-23 13:57:44 +00:00
DB db = DB.getInstance(context);
2022-09-16 08:06:16 +00:00
2019-05-23 13:57:44 +00:00
EntityFolder folder = db.folder().getFolder(target);
if (folder == null)
2020-07-09 15:06:29 +00:00
throw new IllegalArgumentException("Rule move to folder not found name=" + name);
2019-05-23 13:57:44 +00:00
2022-09-16 08:06:16 +00:00
if (!TextUtils.isEmpty(create)) {
Calendar calendar = Calendar.getInstance();
2023-01-29 11:36:30 +00:00
calendar.setTimeInMillis(message.received);
2022-09-16 08:06:16 +00:00
String year = String.format(Locale.ROOT, "%04d", calendar.get(Calendar.YEAR));
String month = String.format(Locale.ROOT, "%02d", calendar.get(Calendar.MONTH) + 1);
String week = String.format(Locale.ROOT, "%02d", calendar.get(Calendar.WEEK_OF_YEAR));
2023-01-09 15:44:53 +00:00
String day = String.format(Locale.ROOT, "%02d", calendar.get(Calendar.DAY_OF_MONTH));
2022-09-16 08:06:16 +00:00
create = create.replace("$year$", year);
create = create.replace("$month$", month);
create = create.replace("$week$", week);
2023-01-09 15:44:53 +00:00
create = create.replace("$day$", day);
2022-09-16 08:06:16 +00:00
2022-09-16 12:16:03 +00:00
String domain = null;
2022-09-16 08:06:16 +00:00
if (message.from != null &&
message.from.length > 0 &&
message.from[0] instanceof InternetAddress) {
InternetAddress from = (InternetAddress) message.from[0];
domain = UriHelper.getEmailDomain(from.getAddress());
2022-09-16 08:06:16 +00:00
}
2022-09-16 12:16:03 +00:00
create = create.replace("$domain$", domain == null ? "" : domain);
2022-09-16 08:06:16 +00:00
2023-01-09 19:04:11 +00:00
if (create.contains("$group$")) {
2023-01-10 12:18:56 +00:00
EntityContact local = null;
if (message.from != null && message.from.length == 1) {
String email = ((InternetAddress) message.from[0]).getAddress();
if (!TextUtils.isEmpty(email))
local = db.contact().getContact(message.account, EntityContact.TYPE_FROM, email);
}
2023-01-09 19:04:11 +00:00
2023-01-10 12:18:56 +00:00
if (local != null && !TextUtils.isEmpty(local.group)) {
Log.i(this.name + " local group=" + local.group);
create = create.replace("$group$", local.group);
} else {
if (!Helper.hasPermission(context, Manifest.permission.READ_CONTACTS))
2023-01-09 19:04:11 +00:00
return false;
2023-01-10 12:18:56 +00:00
Log.i(this.name + " lookup=" + message.avatar);
if (message.avatar == null)
return false;
ContentResolver resolver = context.getContentResolver();
try (Cursor contact = resolver.query(Uri.parse(message.avatar),
new String[]{ContactsContract.Contacts._ID}, null, null, null)) {
Log.i(this.name + " contacts=" + contact.getCount());
if (!contact.moveToNext())
return false;
2023-01-09 19:04:11 +00:00
2023-01-10 12:18:56 +00:00
long contactId = contact.getLong(0);
Log.i(this.name + " contactId=" + contactId);
try (Cursor membership = resolver.query(ContactsContract.Data.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID},
ContactsContract.Data.MIMETYPE + "= ? AND " +
ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID + "= ?",
new String[]{
ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE,
Long.toString((contactId))
},
null)) {
Log.i(this.name + " membership=" + membership.getCount());
int count = 0;
String groupName = null;
while (membership.moveToNext()) {
long groupId = membership.getLong(0);
try (Cursor groups = resolver.query(ContactsContract.Groups.CONTENT_URI,
new String[]{
ContactsContract.Groups.TITLE,
ContactsContract.Groups.AUTO_ADD,
ContactsContract.Groups.GROUP_VISIBLE,
},
ContactsContract.Groups._ID + " = ?",
new String[]{Long.toString(groupId)},
ContactsContract.Groups.TITLE)) {
while (groups.moveToNext()) {
groupName = groups.getString(0);
int auto_add = groups.getInt(1);
int visible = groups.getInt(2);
if (auto_add == 0)
count++;
Log.i(this.name + " membership groupId=" + groupId +
" name=" + groupName + " auto_add=" + auto_add + " visible=" + visible);
}
2023-01-09 21:46:42 +00:00
}
}
2023-01-10 12:18:56 +00:00
Log.i(this.name + " name=" + groupName + " count=" + count);
if (count == 1)
create = create.replace("$group$", groupName);
else
return false;
2023-01-09 19:04:11 +00:00
}
}
}
}
2022-09-16 12:16:03 +00:00
String name = folder.name + (folder.separator == null ? "" : folder.separator) + create;
2022-09-23 20:26:56 +00:00
EntityFolder created = db.folder().getFolderByName(folder.account, name);
2022-09-16 08:06:16 +00:00
if (created == null) {
created = new EntityFolder();
created.tbc = true;
created.account = folder.account;
created.namespace = folder.namespace;
created.separator = folder.separator;
created.name = name;
created.type = EntityFolder.USER;
created.subscribed = true;
created.setProperties();
EntityAccount account = db.account().getAccount(folder.account);
created.setSpecials(account);
created.synchronize = folder.synchronize;
created.poll = folder.poll;
created.poll_factor = folder.poll_factor;
created.download = folder.download;
created.auto_classify_source = folder.auto_classify_source;
created.auto_classify_target = folder.auto_classify_target;
created.sync_days = folder.sync_days;
created.keep_days = folder.keep_days;
created.unified = folder.unified;
created.navigation = folder.navigation;
created.notify = folder.notify;
created.id = db.folder().insertFolder(created);
}
target = created.id;
}
2019-07-25 13:38:14 +00:00
List<EntityMessage> messages = db.message().getMessagesByThread(
message.account, message.thread, thread ? null : message.id, message.folder);
2019-06-20 17:09:33 +00:00
for (EntityMessage threaded : messages)
2022-09-16 08:06:16 +00:00
EntityOperation.queue(context, threaded, EntityOperation.MOVE, target,
seen, null, true, false, !TextUtils.isEmpty(create));
2020-05-05 18:39:38 +00:00
message.ui_hide = true;
2019-09-13 13:17:53 +00:00
if (seen) {
message.ui_seen = true;
message.ui_ignored = true;
}
2019-07-16 06:25:30 +00:00
return true;
2019-01-17 13:29:35 +00:00
}
2020-11-19 17:20:18 +00:00
private boolean onActionCopy(Context context, EntityMessage message, JSONObject jargs) {
2020-07-10 12:35:31 +00:00
long target = jargs.optLong("target", -1);
2019-05-23 13:57:44 +00:00
DB db = DB.getInstance(context);
EntityFolder folder = db.folder().getFolder(target);
if (folder == null)
2020-07-09 15:06:29 +00:00
throw new IllegalArgumentException("Rule copy to folder not found name=" + name);
2019-05-23 13:57:44 +00:00
2019-05-18 06:49:20 +00:00
EntityOperation.queue(context, message, EntityOperation.COPY, target, false);
2019-09-13 13:17:53 +00:00
2019-07-16 06:25:30 +00:00
return true;
2019-05-18 06:34:26 +00:00
}
2020-11-19 17:20:18 +00:00
private boolean onActionAnswer(Context context, EntityMessage message, JSONObject jargs) {
DB db = DB.getInstance(context);
2021-09-29 17:44:27 +00:00
String to = jargs.optString("to");
2022-04-04 08:56:57 +00:00
boolean resend = jargs.optBoolean("resend");
boolean attached = jargs.optBoolean("attached");
boolean attachments = jargs.optBoolean("attachments");
2019-01-18 18:40:51 +00:00
2021-09-29 17:44:27 +00:00
if (TextUtils.isEmpty(to) &&
message.auto_submitted != null && message.auto_submitted) {
2021-08-16 11:40:42 +00:00
EntityLog.log(context, EntityLog.Type.Rules, message,
"Auto submitted rule=" + name);
2020-10-02 13:54:45 +00:00
return false;
}
2021-12-27 08:43:25 +00:00
boolean complete = true;
if (!message.content) {
complete = false;
EntityOperation.queue(context, message, EntityOperation.BODY);
2021-12-27 08:43:25 +00:00
}
if (attachments)
for (EntityAttachment attachment : db.attachment().getAttachments(message.id))
if (!attachment.available) {
complete = false;
EntityOperation.queue(context, message, EntityOperation.ATTACHMENT, attachment.id);
}
2022-04-04 08:56:57 +00:00
if (resend && message.headers == null) {
complete = false;
EntityOperation.queue(context, message, EntityOperation.HEADERS);
}
if (!resend && attached && !Boolean.TRUE.equals(message.raw)) {
complete = false;
EntityOperation.queue(context, message, EntityOperation.RAW);
}
2022-10-15 15:40:18 +00:00
if (!complete && this.id != null) {
EntityOperation.queue(context, message, EntityOperation.RULE, this.id);
2022-10-15 15:40:18 +00:00
return true;
}
2019-05-18 06:49:20 +00:00
2022-12-13 09:52:39 +00:00
Helper.getSerialExecutor().submit(new Runnable() {
2020-11-17 07:30:43 +00:00
@Override
public void run() {
try {
answer(context, EntityRule.this, message, jargs);
} catch (Throwable ex) {
2020-11-19 18:14:46 +00:00
db.message().setMessageError(message.id, Log.formatThrowable(ex));
2020-11-19 17:54:44 +00:00
Log.w(ex);
2020-11-17 07:30:43 +00:00
}
}
});
return true;
}
private static void answer(Context context, EntityRule rule, EntityMessage message, JSONObject jargs) throws JSONException, AddressException, IOException {
Log.i("Answering name=" + rule.name);
DB db = DB.getInstance(context);
long iid = jargs.getLong("identity");
long aid = jargs.getLong("answer");
boolean answer_subject = jargs.optBoolean("answer_subject", false);
boolean original_text = jargs.optBoolean("original_text", true);
2022-04-04 07:55:44 +00:00
boolean attachments = jargs.optBoolean("attachments");
2020-11-17 07:30:43 +00:00
String to = jargs.optString("to");
2022-04-04 08:56:57 +00:00
boolean resend = jargs.optBoolean("resend");
boolean attached = jargs.optBoolean("attached");
2020-11-17 07:30:43 +00:00
boolean cc = jargs.optBoolean("cc");
2021-12-24 13:00:12 +00:00
boolean isReply = TextUtils.isEmpty(to);
2021-06-10 13:48:17 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean separate_reply = prefs.getBoolean("separate_reply", false);
boolean extended_reply = prefs.getBoolean("extended_reply", false);
boolean quote_reply = prefs.getBoolean("quote_reply", true);
2021-12-24 13:00:12 +00:00
boolean quote = (quote_reply && isReply);
2021-06-10 13:48:17 +00:00
2019-01-18 18:40:51 +00:00
EntityIdentity identity = db.identity().getIdentity(iid);
if (identity == null)
2020-11-17 07:30:43 +00:00
throw new IllegalArgumentException("Rule identity not found name=" + rule.name);
2019-01-18 18:40:51 +00:00
2021-04-01 16:35:52 +00:00
EntityAnswer answer;
2022-04-04 08:56:57 +00:00
if (aid < 0 || resend) {
2021-12-24 13:00:12 +00:00
if (isReply)
2021-04-01 16:35:52 +00:00
throw new IllegalArgumentException("Rule template missing name=" + rule.name);
answer = new EntityAnswer();
answer.name = message.subject;
2021-04-01 16:35:52 +00:00
answer.text = "";
} else {
answer = db.answer().getAnswer(aid);
if (answer == null)
throw new IllegalArgumentException("Rule template not found name=" + rule.name);
}
2019-01-18 18:40:51 +00:00
2023-09-04 13:38:05 +00:00
EntityFolder outbox = EntityFolder.getOutbox(context);
2020-11-05 16:32:16 +00:00
Address[] from = new InternetAddress[]{new InternetAddress(identity.email, identity.name, StandardCharsets.UTF_8.name())};
2020-05-25 13:01:30 +00:00
// Prevent loop
if (isReply) {
List<EntityMessage> messages = db.message().getMessagesByThread(
message.account, message.thread, null, null);
for (EntityMessage threaded : messages)
if (!threaded.id.equals(message.id) &&
MessageHelper.equal(threaded.from, from)) {
EntityLog.log(context, EntityLog.Type.Rules, message,
"Answer loop" +
" name=" + answer.name +
" from=" + MessageHelper.formatAddresses(from));
return;
}
}
2019-09-27 17:37:34 +00:00
2019-01-18 18:40:51 +00:00
EntityMessage reply = new EntityMessage();
reply.account = message.account;
2020-11-05 16:32:16 +00:00
reply.folder = outbox.id;
2019-01-18 18:40:51 +00:00
reply.identity = identity.id;
reply.msgid = EntityMessage.generateMessageId();
2021-12-24 13:00:12 +00:00
if (isReply) {
reply.references = (message.references == null ? "" : message.references + " ") + message.msgid;
reply.inreplyto = message.msgid;
reply.thread = message.thread;
reply.to = (message.reply == null || message.reply.length == 0 ? message.from : message.reply);
} else {
2022-04-04 08:56:57 +00:00
if (resend) {
reply.resend = true;
reply.headers = message.headers;
} else
reply.wasforwardedfrom = message.msgid;
reply.thread = reply.msgid; // new thread
2021-07-04 19:44:02 +00:00
reply.to = MessageHelper.parseAddresses(context, to);
}
2020-05-25 13:01:30 +00:00
reply.from = from;
2019-02-18 09:19:12 +00:00
if (cc)
reply.cc = message.cc;
2022-04-04 08:56:57 +00:00
if (isReply)
reply.unsubscribe = "mailto:" + identity.email;
2020-10-02 13:54:45 +00:00
reply.auto_submitted = true;
2022-04-04 08:56:57 +00:00
if (resend)
reply.subject = message.subject;
else
reply.subject = EntityMessage.getSubject(context,
message.language,
answer_subject ? answer.name : message.subject,
!isReply);
2019-01-18 18:40:51 +00:00
reply.received = new Date().getTime();
2019-03-14 16:02:27 +00:00
reply.sender = MessageHelper.getSortKey(reply.from);
2021-12-24 13:00:12 +00:00
2020-05-06 20:30:47 +00:00
Uri lookupUri = ContactInfo.getLookupUri(reply.from);
2019-03-14 16:02:27 +00:00
reply.avatar = (lookupUri == null ? null : lookupUri.toString());
2019-01-18 18:40:51 +00:00
reply.id = db.message().insertMessage(reply);
2019-09-27 17:37:34 +00:00
2022-04-04 08:56:57 +00:00
String body;
if (resend)
body = Helper.readText(message.getFile(context));
else {
2022-08-21 06:50:42 +00:00
body = answer.getHtml(context, message.from);
2020-07-13 14:34:55 +00:00
2022-04-04 08:56:57 +00:00
if (original_text) {
Document msg = JsoupEx.parse(body);
2020-07-13 14:34:55 +00:00
2022-04-04 08:56:57 +00:00
Element div = msg.createElement("div");
2020-07-13 14:34:55 +00:00
2022-04-04 08:56:57 +00:00
Element p = message.getReplyHeader(context, msg, separate_reply, extended_reply);
div.appendChild(p);
2020-07-13 14:34:55 +00:00
2022-04-04 08:56:57 +00:00
Document answering = JsoupEx.parse(message.getFile(context));
Element e = answering.body();
if (quote) {
String style = e.attr("style");
style = HtmlHelper.mergeStyles(style, HtmlHelper.getQuoteStyle(e));
e.tagName("blockquote").attr("style", style);
} else
e.tagName("p");
div.appendChild(e);
2022-04-04 08:56:57 +00:00
msg.body().appendChild(div);
2020-07-13 14:34:55 +00:00
2022-04-04 08:56:57 +00:00
body = msg.outerHtml();
}
}
2020-07-13 14:34:55 +00:00
2020-02-20 09:35:01 +00:00
File file = reply.getFile(context);
Helper.writeText(file, body);
2021-01-19 10:57:59 +00:00
String text = HtmlHelper.getFullText(body);
2021-01-19 11:18:03 +00:00
reply.preview = HtmlHelper.getPreview(text);
2021-01-25 13:49:56 +00:00
reply.language = HtmlHelper.getLanguage(context, reply.subject, text);
2019-05-04 18:52:21 +00:00
db.message().setMessageContent(reply.id,
true,
2021-01-19 11:18:03 +00:00
reply.language,
2022-02-13 10:36:38 +00:00
0,
2021-01-19 11:18:03 +00:00
reply.preview,
2019-05-04 18:52:21 +00:00
null);
2019-01-18 18:40:51 +00:00
2022-04-04 08:56:57 +00:00
if (attachments || resend)
EntityAttachment.copy(context, message.id, reply.id);
if (!resend && attached) {
EntityAttachment attachment = new EntityAttachment();
attachment.message = reply.id;
2022-04-20 12:38:42 +00:00
attachment.sequence = db.attachment().getAttachmentSequence(reply.id) + 1;
attachment.name = "email.eml";
attachment.type = "message/rfc822";
attachment.disposition = Part.ATTACHMENT;
attachment.progress = 0;
attachment.id = db.attachment().insertAttachment(attachment);
File source = message.getRawFile(context);
File target = attachment.getFile(context);
Helper.copy(source, target);
db.attachment().setDownloaded(attachment.id, target.length());
}
2019-05-18 06:49:20 +00:00
EntityOperation.queue(context, reply, EntityOperation.SEND);
2019-09-13 13:17:53 +00:00
2020-02-21 16:38:46 +00:00
// Batch send operations, wait until after commit
ServiceSend.schedule(context, SEND_DELAY);
2019-01-18 18:40:51 +00:00
}
2019-07-16 06:25:30 +00:00
private boolean onActionAutomation(Context context, EntityMessage message, JSONObject jargs) {
2023-06-05 20:09:10 +00:00
InternetAddress iaddr =
(message.from == null || message.from.length == 0
? null : ((InternetAddress) message.from[0]));
2019-03-17 12:49:10 +00:00
// ISO 8601
DateFormat DTF = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
DTF.setTimeZone(java.util.TimeZone.getTimeZone("Zulu"));
2019-03-17 12:49:10 +00:00
Intent automation = new Intent(ACTION_AUTOMATION);
automation.putExtra(EXTRA_RULE, name);
2023-06-05 20:09:10 +00:00
automation.putExtra(EXTRA_SENDER, iaddr == null ? null : iaddr.getAddress());
automation.putExtra(EXTRA_NAME, iaddr == null ? null : iaddr.getPersonal());
2019-03-17 12:49:10 +00:00
automation.putExtra(EXTRA_SUBJECT, message.subject);
automation.putExtra(EXTRA_RECEIVED, DTF.format(message.received));
2019-03-17 12:49:10 +00:00
List<String> extras = Log.getExtras(automation.getExtras());
2021-08-16 11:40:42 +00:00
EntityLog.log(context, EntityLog.Type.Rules, message,
"Sending " + automation + " " + TextUtils.join(" ", extras));
2019-07-16 06:25:30 +00:00
context.sendBroadcast(automation);
2019-09-13 13:17:53 +00:00
2019-07-16 06:25:30 +00:00
return true;
2019-03-17 12:49:10 +00:00
}
2020-11-19 17:20:18 +00:00
private boolean onActionTts(Context context, EntityMessage message, JSONObject jargs) {
2020-11-19 18:14:46 +00:00
DB db = DB.getInstance(context);
2022-01-10 17:18:40 +00:00
if (message.ui_seen)
return false;
2022-10-15 15:40:18 +00:00
if (!message.content && this.id != null) {
2020-07-13 14:39:23 +00:00
EntityOperation.queue(context, message, EntityOperation.BODY);
EntityOperation.queue(context, message, EntityOperation.RULE, this.id);
return true;
}
2023-01-01 10:43:43 +00:00
Helper.getMediaTaskExecutor().submit(new Runnable() {
2020-11-17 07:30:43 +00:00
@Override
public void run() {
try {
2022-07-30 21:02:17 +00:00
if (MediaPlayerHelper.isInCall(context) || MediaPlayerHelper.isDnd(context))
2022-01-10 17:18:40 +00:00
return;
2020-11-17 07:30:43 +00:00
speak(context, EntityRule.this, message);
} catch (Throwable ex) {
2020-11-19 18:14:46 +00:00
db.message().setMessageError(message.id, Log.formatThrowable(ex));
2020-11-19 17:54:44 +00:00
Log.w(ex);
2020-11-17 07:30:43 +00:00
}
}
});
return true;
}
private static void speak(Context context, EntityRule rule, EntityMessage message) throws IOException {
Log.i("Speaking name=" + rule.name);
2020-12-14 07:44:20 +00:00
2022-01-04 08:11:51 +00:00
if (message.ui_seen)
2020-12-14 07:44:20 +00:00
return;
2020-07-03 14:44:59 +00:00
Locale locale = (message.language == null ? Locale.getDefault() : new Locale(message.language));
Configuration configuration = new Configuration(context.getResources().getConfiguration());
configuration.setLocale(locale);
Resources res = context.createConfigurationContext(configuration).getResources();
2020-07-03 13:58:49 +00:00
StringBuilder sb = new StringBuilder();
2020-07-03 19:39:54 +00:00
sb.append(res.getString(R.string.title_rule_tts_prefix)).append(". ");
2020-07-03 13:58:49 +00:00
if (message.from != null && message.from.length > 0)
2020-07-03 14:44:59 +00:00
sb.append(res.getString(R.string.title_rule_tts_from))
.append(' ').append(MessageHelper.formatAddressesShort(message.from)).append(". ");
2020-07-03 13:58:49 +00:00
if (!TextUtils.isEmpty(message.subject))
2020-07-03 14:44:59 +00:00
sb.append(res.getString(R.string.title_rule_tts_subject))
.append(' ').append(message.subject).append(". ");
2020-07-03 13:58:49 +00:00
2020-07-13 14:39:23 +00:00
String body = Helper.readText(message.getFile(context));
2021-01-19 10:57:59 +00:00
String text = HtmlHelper.getFullText(body);
2021-01-19 11:18:03 +00:00
String preview = HtmlHelper.getPreview(text);
2021-01-19 10:57:59 +00:00
2020-07-13 14:39:23 +00:00
if (!TextUtils.isEmpty(preview))
sb.append(res.getString(R.string.title_rule_tts_content))
.append(' ').append(preview);
2020-07-03 14:44:59 +00:00
TTSHelper.speak(context, "rule:" + message.id, sb.toString(), locale);
2020-07-03 13:58:49 +00:00
}
2019-07-16 06:25:30 +00:00
private boolean onActionSnooze(Context context, EntityMessage message, JSONObject jargs) throws JSONException {
2019-06-01 13:27:21 +00:00
int duration = jargs.getInt("duration");
boolean schedule_end = jargs.optBoolean("schedule_end", false);
2019-11-09 07:55:42 +00:00
boolean seen = jargs.optBoolean("seen", false);
long wakeup;
if (schedule_end) {
JSONObject jcondition = new JSONObject(condition);
JSONObject jschedule = jcondition.optJSONObject("schedule");
int end = (jschedule == null ? 0 : jschedule.optInt("end", 0));
2023-02-01 20:13:43 +00:00
Calendar cal = getRelativeCalendar(false, end, message.received);
2019-08-20 19:12:45 +00:00
wakeup = cal.getTimeInMillis() + duration * 3600 * 1000L;
} else
wakeup = message.received + duration * 3600 * 1000L;
2019-06-01 13:27:21 +00:00
2019-06-23 07:48:38 +00:00
if (wakeup < new Date().getTime())
2019-07-16 06:25:30 +00:00
return false;
2019-06-23 07:48:38 +00:00
2019-06-01 13:27:21 +00:00
DB db = DB.getInstance(context);
db.message().setMessageSnoozed(message.id, wakeup);
2019-11-04 08:00:14 +00:00
db.message().setMessageUiIgnored(message.id, true);
2019-06-01 13:27:21 +00:00
EntityMessage.snooze(context, message.id, wakeup);
2019-09-13 13:17:53 +00:00
message.ui_snoozed = wakeup;
2019-11-09 07:55:42 +00:00
if (seen)
onActionSeen(context, message, true);
2019-07-16 06:25:30 +00:00
return true;
2019-06-01 13:27:21 +00:00
}
2019-07-16 06:25:30 +00:00
private boolean onActionFlag(Context context, EntityMessage message, JSONObject jargs) throws JSONException {
2019-05-15 10:52:39 +00:00
Integer color = (jargs.has("color") && !jargs.isNull("color")
? jargs.getInt("color") : null);
2019-09-13 13:17:53 +00:00
2022-09-22 18:09:11 +00:00
EntityOperation.queue(context, message, EntityOperation.FLAG, true, color, false);
2019-09-13 13:17:53 +00:00
message.ui_flagged = true;
message.color = color;
2019-07-16 06:25:30 +00:00
return true;
2019-05-15 10:52:39 +00:00
}
2020-02-01 12:51:32 +00:00
private boolean onActionImportance(Context context, EntityMessage message, JSONObject jargs) throws JSONException {
Integer importance = jargs.getInt("value");
2020-02-01 13:09:58 +00:00
if (EntityMessage.PRIORITIY_NORMAL.equals(importance))
2020-02-01 12:51:32 +00:00
importance = null;
DB db = DB.getInstance(context);
db.message().setMessageImportance(message.id, importance);
2021-12-29 08:31:46 +00:00
EntityOperation.queue(context, message, EntityOperation.KEYWORD,
MessageHelper.FLAG_LOW_IMPORTANCE, EntityMessage.PRIORITIY_LOW.equals(importance));
EntityOperation.queue(context, message, EntityOperation.KEYWORD,
MessageHelper.FLAG_HIGH_IMPORTANCE, EntityMessage.PRIORITIY_HIGH.equals(importance));
2020-02-01 12:51:32 +00:00
message.importance = importance;
return true;
}
2019-10-09 09:09:00 +00:00
private boolean onActionKeyword(Context context, EntityMessage message, JSONObject jargs) throws JSONException {
String keyword = jargs.getString("keyword");
2023-03-13 16:31:44 +00:00
boolean set = jargs.optBoolean("set", true);
2020-11-19 17:54:44 +00:00
if (TextUtils.isEmpty(keyword))
throw new IllegalArgumentException("Keyword missing rule=" + name);
2019-10-09 09:09:00 +00:00
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(message.received);
String year = String.format(Locale.ROOT, "%04d", calendar.get(Calendar.YEAR));
String month = String.format(Locale.ROOT, "%02d", calendar.get(Calendar.MONTH) + 1);
String week = String.format(Locale.ROOT, "%02d", calendar.get(Calendar.WEEK_OF_YEAR));
String day = String.format(Locale.ROOT, "%02d", calendar.get(Calendar.DAY_OF_MONTH));
keyword = keyword.replace("$year$", year);
keyword = keyword.replace("$month$", month);
keyword = keyword.replace("$week$", week);
keyword = keyword.replace("$day$", day);
2023-03-13 16:31:44 +00:00
EntityOperation.queue(context, message, EntityOperation.KEYWORD, keyword, set);
2019-10-09 09:09:00 +00:00
return true;
}
2021-12-28 07:59:08 +00:00
private boolean onActionDelete(Context context, EntityMessage message, JSONObject jargs) {
EntityOperation.queue(context, message, EntityOperation.DELETE);
return true;
}
2022-01-04 08:11:51 +00:00
private boolean onActionSound(Context context, EntityMessage message, JSONObject jargs) throws JSONException {
if (message.ui_seen)
return false;
2022-11-23 07:49:14 +00:00
Uri uri = (jargs.has("uri") ? Uri.parse(jargs.getString("uri")) : null);
2022-01-04 08:11:51 +00:00
boolean alarm = jargs.getBoolean("alarm");
2022-01-04 18:40:27 +00:00
int duration = jargs.optInt("duration", MediaPlayerHelper.DEFAULT_ALARM_DURATION);
2022-11-23 07:49:14 +00:00
Log.i("Sound uri=" + uri + " alarm=" + alarm + " duration=" + duration);
2022-01-04 08:11:51 +00:00
DB db = DB.getInstance(context);
message.ui_silent = true;
db.message().setMessageUiSilent(message.id, message.ui_silent);
2022-11-23 07:49:14 +00:00
if (uri != null)
MediaPlayerHelper.queue(context, uri, alarm, duration);
2022-01-04 08:11:51 +00:00
return true;
}
private boolean onActionLocalOnly(Context context, EntityMessage message, JSONObject jargs) throws JSONException {
if (message.ui_seen)
return false;
DB db = DB.getInstance(context);
message.ui_local_only = true;
db.message().setMessageUiLocalOnly(message.id, message.ui_local_only);
return true;
}
2023-08-14 08:58:36 +00:00
private boolean onActionNotes(Context context, EntityMessage message, JSONObject jargs, String html) throws JSONException {
2023-08-14 07:39:53 +00:00
String notes = jargs.getString("notes");
Integer color = (jargs.has("color") ? jargs.getInt("color") : null);
2023-08-14 08:58:36 +00:00
if (notes.startsWith(JSOUP_PREFIX)) {
if (html == null && message.content) {
File file = message.getFile(context);
try {
html = Helper.readText(file);
} catch (IOException ex) {
Log.e(ex);
}
}
2023-08-17 06:04:50 +00:00
2023-08-14 08:58:36 +00:00
if (html != null) {
Document d = JsoupEx.parse(html);
2023-08-14 09:39:17 +00:00
String selector = notes.substring(JSOUP_PREFIX.length());
2023-08-15 13:00:56 +00:00
String regex = null;
if (selector.endsWith(("}"))) {
int b = selector.lastIndexOf('{');
if (b > 0) {
regex = selector.substring(b + 1, selector.length() - 1);
selector = selector.substring(0, b);
}
}
2023-08-14 09:39:17 +00:00
Element e = d.select(selector).first();
2023-08-17 07:55:36 +00:00
if (e == null) {
2023-08-17 06:04:50 +00:00
notes = null;
2023-08-17 07:55:36 +00:00
Log.w("Nothing selected Jsoup=" + selector);
} else {
2023-08-14 09:39:17 +00:00
notes = e.ownText();
2023-08-15 13:00:56 +00:00
if (!TextUtils.isEmpty(regex)) {
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(notes);
if (m.matches() && m.groupCount() > 0)
notes = m.group(1);
2023-08-17 07:55:36 +00:00
else
Log.w("Nothing selected regex=" + regex + " value=" + notes);
2023-08-15 13:00:56 +00:00
}
}
2023-08-14 08:58:36 +00:00
}
}
2023-08-14 09:26:45 +00:00
if (TextUtils.isEmpty(notes))
notes = null;
else if (notes.length() > MAX_NOTES_LENGTH)
notes = notes.substring(0, MAX_NOTES_LENGTH);
2023-08-14 07:39:53 +00:00
DB db = DB.getInstance(context);
db.message().setMessageNotes(message.id, notes, color);
return true;
}
2023-10-02 16:45:50 +00:00
private boolean onActionUrl(Context context, EntityMessage message, JSONObject jargs, String html) throws JSONException, IOException {
String url = jargs.getString("url");
2023-10-03 14:11:42 +00:00
String method = jargs.optString("method");
if (TextUtils.isEmpty(method))
method = "GET";
2023-10-02 16:45:50 +00:00
2023-10-02 19:12:02 +00:00
InternetAddress iaddr =
(message.from == null || message.from.length == 0
? null : ((InternetAddress) message.from[0]));
String address = (iaddr == null ? null : iaddr.getAddress());
String personal = (iaddr == null ? null : iaddr.getPersonal());
// ISO 8601
DateFormat DTF = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
DTF.setTimeZone(java.util.TimeZone.getTimeZone("Zulu"));
url = url.replace("$" + EXTRA_RULE + "$", Uri.encode(name == null ? "" : name));
url = url.replace("$" + EXTRA_SENDER + "$", Uri.encode(address == null ? "" : address));
url = url.replace("$" + EXTRA_NAME + "$", Uri.encode(personal == null ? "" : personal));
url = url.replace("$" + EXTRA_SUBJECT + "$", Uri.encode(message.subject == null ? "" : message.subject));
url = url.replace("$" + EXTRA_RECEIVED + "$", Uri.encode(DTF.format(message.received)));
2023-10-03 14:11:42 +00:00
String body = null;
if ("POST".equals(method) || "PUT".equals(method)) {
Uri u = Uri.parse(url);
body = u.getQuery();
url = u.buildUpon().clearQuery().build().toString();
}
2023-10-02 16:45:50 +00:00
Log.i("GET " + url);
2023-12-06 14:31:05 +00:00
HttpsURLConnection connection = null;
2023-10-02 16:45:50 +00:00
try {
2023-12-06 14:31:05 +00:00
connection = (HttpsURLConnection) new URL(url).openConnection();
2023-10-03 14:11:42 +00:00
connection.setRequestMethod(method);
connection.setDoOutput(body != null);
2023-10-02 16:45:50 +00:00
connection.setReadTimeout(URL_TIMEOUT);
connection.setConnectTimeout(URL_TIMEOUT);
connection.setInstanceFollowRedirects(true);
ConnectionHelper.setUserAgent(context, connection);
connection.connect();
2023-10-03 14:11:42 +00:00
if (body != null)
connection.getOutputStream().write(body.getBytes());
int status = connection.getResponseCode();
if (status < 200 || status > 299) {
String error = "Error " + status + ": " + connection.getResponseMessage();
try {
InputStream is = connection.getErrorStream();
if (is != null)
error += "\n" + Helper.readStream(is);
} catch (Throwable ex) {
Log.w(ex);
}
2023-11-12 20:06:56 +00:00
throw new IOException(error);
2023-10-03 14:11:42 +00:00
}
2023-10-02 16:45:50 +00:00
} finally {
if (connection != null)
connection.disconnect();
}
return true;
}
private boolean onActionSilent(Context context, EntityMessage message, JSONObject jargs) {
DB db = DB.getInstance(context);
db.message().setMessageUiSilent(message.id, true);
message.ui_silent = true;
return true;
}
2023-02-01 20:13:43 +00:00
private static Calendar getRelativeCalendar(boolean all, int minutes, long reference) {
2019-08-23 17:50:50 +00:00
int d = minutes / (24 * 60);
int h = minutes / 60 % 24;
int m = minutes % 60;
Calendar cal = Calendar.getInstance();
2023-02-01 20:13:43 +00:00
if (all)
2019-08-25 06:07:02 +00:00
cal.setTimeInMillis(reference);
2023-02-01 20:13:43 +00:00
else {
if (reference > cal.getTimeInMillis() - 7 * 24 * 3600 * 1000L)
cal.setTimeInMillis(reference);
long time = cal.getTimeInMillis();
2019-08-23 17:50:50 +00:00
2023-02-01 20:13:43 +00:00
cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY + d);
if (cal.getTimeInMillis() < time)
cal.add(Calendar.HOUR_OF_DAY, 7 * 24);
}
2019-08-23 17:50:50 +00:00
cal.set(Calendar.HOUR_OF_DAY, h);
cal.set(Calendar.MINUTE, m);
cal.set(Calendar.SECOND, 0);
return cal;
}
2021-12-17 14:03:19 +00:00
@NonNull
static List<EntityRule> blockSender(Context context, EntityMessage message, EntityFolder junk, boolean block_domain) throws JSONException {
List<EntityRule> rules = new ArrayList<>();
if (message.from == null)
return rules;
2022-01-30 16:26:07 +00:00
List<String> domains = new ArrayList<>();
2021-12-17 14:03:19 +00:00
for (Address from : message.from) {
String sender = ((InternetAddress) from).getAddress();
String name = MessageHelper.formatAddresses(new Address[]{from});
2023-04-18 13:06:45 +00:00
if (TextUtils.isEmpty(sender))
2021-12-17 14:03:19 +00:00
continue;
boolean regex = false;
if (block_domain) {
2022-01-30 16:26:07 +00:00
String domain = UriHelper.getEmailDomain(sender);
2023-04-18 16:37:05 +00:00
if (domain != null)
domain = domain.trim();
2022-01-30 16:26:07 +00:00
if (!TextUtils.isEmpty(domain) && !domains.contains(domain)) {
2023-04-16 06:02:47 +00:00
String parent = UriHelper.getParentDomain(context, domain);
if (parent != null)
domain = parent;
2022-01-30 16:26:07 +00:00
domains.add(domain);
2021-12-17 14:03:19 +00:00
regex = true;
2022-01-30 16:26:07 +00:00
sender = ".*@.*" + Pattern.quote(domain) + ".*";
2021-12-17 14:03:19 +00:00
}
2020-04-19 14:57:40 +00:00
}
2021-12-17 14:03:19 +00:00
JSONObject jsender = new JSONObject();
jsender.put("value", sender);
jsender.put("regex", regex);
2021-12-17 14:03:19 +00:00
JSONObject jcondition = new JSONObject();
jcondition.put("sender", jsender);
2021-12-17 14:03:19 +00:00
JSONObject jaction = new JSONObject();
jaction.put("type", TYPE_MOVE);
jaction.put("target", junk.id);
jaction.put("seen", true);
2021-12-17 14:03:19 +00:00
EntityRule rule = new EntityRule();
rule.folder = message.folder;
rule.name = context.getString(R.string.title_block, name);
rule.order = 1000;
rule.enabled = true;
rule.stop = true;
rule.condition = jcondition.toString();
rule.action = jaction.toString();
rules.add(rule);
}
2021-12-17 14:03:19 +00:00
return rules;
}
2020-04-25 14:17:59 +00:00
boolean isBlockingSender(EntityMessage message, EntityFolder junk) throws JSONException {
if (message.from == null || message.from.length == 0)
return false;
2020-09-21 21:03:13 +00:00
2020-04-25 14:17:59 +00:00
String sender = ((InternetAddress) message.from[0]).getAddress();
if (sender == null)
return false;
JSONObject jcondition = new JSONObject(condition);
if (!jcondition.has("sender"))
return false;
JSONObject jsender = jcondition.getJSONObject("sender");
String value = jsender.optString("value");
2020-09-21 21:03:13 +00:00
boolean regex = jsender.optBoolean("regex");
if (regex) {
Pattern pattern = Pattern.compile(value, Pattern.DOTALL);
if (!pattern.matcher(sender).matches())
return false;
} else {
int at = sender.indexOf('@');
String domain = (at < 0 ? sender : sender.substring(at));
if (!sender.equals(value) && !domain.equals(value))
return false;
}
2020-04-25 14:17:59 +00:00
JSONObject jaction = new JSONObject(action);
if (jaction.optInt("type", -1) != TYPE_MOVE)
return false;
2020-09-08 12:00:04 +00:00
if (jaction.optLong("target", -1) != junk.id)
2020-04-25 14:17:59 +00:00
return false;
return true;
}
2019-01-17 13:29:35 +00:00
@Override
public boolean equals(Object obj) {
if (obj instanceof EntityRule) {
EntityRule other = (EntityRule) obj;
2022-06-27 06:57:35 +00:00
return Objects.equals(this.uuid, other.uuid) &&
this.folder.equals(other.folder) &&
2019-01-17 13:29:35 +00:00
this.name.equals(other.name) &&
2023-04-01 16:46:05 +00:00
Objects.equals(this.group, other.group) &&
2019-01-17 21:25:22 +00:00
this.order == other.order &&
this.enabled == other.enabled &&
2022-12-23 13:06:25 +00:00
this.daily == other.daily &&
2019-01-18 10:58:55 +00:00
this.stop == other.stop &&
2019-01-17 13:29:35 +00:00
this.condition.equals(other.condition) &&
this.action.equals(other.action) &&
2020-12-24 07:30:03 +00:00
this.applied.equals(other.applied) &&
Objects.equals(this.last_applied, other.last_applied);
2019-01-17 13:29:35 +00:00
} else
return false;
}
2019-01-18 16:00:11 +00:00
2022-04-01 06:49:40 +00:00
boolean matches(String query) {
if (this.name.toLowerCase().contains(query))
return true;
try {
JSONObject jcondition = new JSONObject(this.condition);
JSONObject jaction = new JSONObject(this.action);
JSONObject jmerged = new JSONObject();
jmerged.put("condition", jcondition);
jmerged.put("action", jaction);
return contains(jmerged, query);
} catch (JSONException ex) {
Log.e(ex);
}
return false;
}
private boolean contains(JSONObject jobject, String query) throws JSONException {
Iterator<String> keys = jobject.keys();
while (keys.hasNext()) {
String key = keys.next();
Object value = jobject.get(key);
if (value instanceof JSONObject) {
if (contains((JSONObject) value, query))
return true;
} else {
if (value.toString().toLowerCase().contains(query))
return true;
}
}
return false;
}
2019-01-18 16:00:11 +00:00
public JSONObject toJSON() throws JSONException {
JSONObject json = new JSONObject();
json.put("id", id);
2022-06-27 06:57:35 +00:00
json.put("uuid", uuid);
2019-01-18 16:00:11 +00:00
json.put("name", name);
2023-04-01 16:46:05 +00:00
json.put("group", group);
2019-01-18 16:00:11 +00:00
json.put("order", order);
json.put("enabled", enabled);
2022-12-23 13:06:25 +00:00
json.put("daily", daily);
2019-01-18 16:00:11 +00:00
json.put("stop", stop);
json.put("condition", condition);
json.put("action", action);
2020-02-10 07:58:23 +00:00
json.put("applied", applied);
2020-12-24 07:30:03 +00:00
json.put("last_applied", last_applied);
2019-01-18 16:00:11 +00:00
return json;
}
public static EntityRule fromJSON(JSONObject json) throws JSONException {
EntityRule rule = new EntityRule();
// id
2022-06-27 06:57:35 +00:00
if (json.has("uuid"))
rule.uuid = json.getString("uuid");
2019-01-18 16:00:11 +00:00
rule.name = json.getString("name");
2023-04-11 20:01:06 +00:00
if (json.has("group") && !json.isNull("group"))
rule.group = json.getString("group");
2019-01-18 16:00:11 +00:00
rule.order = json.getInt("order");
rule.enabled = json.getBoolean("enabled");
2022-12-23 13:06:25 +00:00
rule.daily = json.optBoolean("daily");
2019-01-18 16:00:11 +00:00
rule.stop = json.getBoolean("stop");
rule.condition = json.getString("condition");
rule.action = json.getString("action");
2020-02-10 07:58:23 +00:00
rule.applied = json.optInt("applied", 0);
2020-12-24 07:30:03 +00:00
if (json.has("last_applied") && !json.isNull("last_applied"))
rule.last_applied = json.getLong("last_applied");
2019-01-18 16:00:11 +00:00
return rule;
}
2019-01-17 13:29:35 +00:00
}