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/>.
|
|
|
|
|
|
|
|
Copyright 2018-2019 by Marcel Bokhorst (M66B)
|
|
|
|
*/
|
|
|
|
|
|
|
|
import android.content.Context;
|
2019-03-17 12:49:10 +00:00
|
|
|
import android.content.Intent;
|
2019-03-14 16:02:27 +00:00
|
|
|
import android.net.Uri;
|
2019-01-17 13:29:35 +00:00
|
|
|
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
2019-01-18 18:40:51 +00:00
|
|
|
import java.io.IOException;
|
2019-03-17 11:11:12 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
2019-01-18 18:40:51 +00:00
|
|
|
import java.util.Date;
|
2019-01-18 13:04:05 +00:00
|
|
|
import java.util.Enumeration;
|
2019-03-17 11:11:12 +00:00
|
|
|
import java.util.List;
|
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.Message;
|
|
|
|
import javax.mail.MessagingException;
|
2019-01-18 10:58:55 +00:00
|
|
|
import javax.mail.internet.InternetAddress;
|
|
|
|
|
2019-01-17 13:29:35 +00:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.room.Entity;
|
|
|
|
import androidx.room.ForeignKey;
|
|
|
|
import androidx.room.Index;
|
|
|
|
import androidx.room.PrimaryKey;
|
|
|
|
|
|
|
|
import static androidx.room.ForeignKey.CASCADE;
|
|
|
|
|
|
|
|
@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 Long folder;
|
|
|
|
@NonNull
|
|
|
|
public String name;
|
|
|
|
@NonNull
|
|
|
|
public int order;
|
|
|
|
@NonNull
|
2019-01-17 21:25:22 +00:00
|
|
|
public boolean enabled;
|
|
|
|
@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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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_SUBJECT = "subject";
|
2019-01-17 13:29:35 +00:00
|
|
|
|
2019-01-18 13:04:05 +00:00
|
|
|
boolean matches(Context context, EntityMessage message, Message imessage) throws MessagingException {
|
2019-01-17 21:41:00 +00:00
|
|
|
try {
|
|
|
|
JSONObject jcondition = new JSONObject(condition);
|
|
|
|
|
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) {
|
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");
|
|
|
|
|
|
|
|
boolean matches = false;
|
|
|
|
if (message.from != null) {
|
|
|
|
for (Address from : message.from) {
|
|
|
|
InternetAddress ia = (InternetAddress) from;
|
|
|
|
String personal = ia.getPersonal();
|
|
|
|
String formatted = ((personal == null ? "" : personal + " ") + "<" + ia.getAddress() + ">");
|
2019-02-04 13:01:36 +00:00
|
|
|
if (matches(context, value, formatted, regex)) {
|
2019-01-18 10:58:55 +00:00
|
|
|
matches = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!matches)
|
|
|
|
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) {
|
|
|
|
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));
|
|
|
|
for (Address recipient : recipients) {
|
|
|
|
InternetAddress ia = (InternetAddress) recipient;
|
|
|
|
String personal = ia.getPersonal();
|
|
|
|
String formatted = ((personal == null ? "" : personal + " ") + "<" + ia.getAddress() + ">");
|
|
|
|
if (matches(context, value, formatted, regex)) {
|
|
|
|
matches = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!matches)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-18 10:58:55 +00:00
|
|
|
JSONObject jsubject = jcondition.optJSONObject("subject");
|
|
|
|
if (jsubject != null) {
|
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");
|
|
|
|
|
2019-02-04 13:01:36 +00:00
|
|
|
if (!matches(context, value, message.subject, regex))
|
2019-01-18 13:04:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSONObject jheader = jcondition.optJSONObject("header");
|
|
|
|
if (jheader != null) {
|
|
|
|
String value = jheader.getString("value");
|
|
|
|
boolean regex = jheader.getBoolean("regex");
|
|
|
|
|
|
|
|
boolean matches = false;
|
|
|
|
Enumeration<Header> headers = imessage.getAllHeaders();
|
|
|
|
while (headers.hasMoreElements()) {
|
|
|
|
Header header = headers.nextElement();
|
|
|
|
String formatted = header.getName() + ": " + header.getValue();
|
2019-02-04 13:01:36 +00:00
|
|
|
if (matches(context, value, formatted, regex)) {
|
2019-01-18 13:04:05 +00:00
|
|
|
matches = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!matches)
|
2019-01-18 10:58:55 +00:00
|
|
|
return false;
|
2019-01-17 21:41:00 +00:00
|
|
|
}
|
2019-01-18 10:58:55 +00:00
|
|
|
|
|
|
|
// Safeguard
|
2019-03-17 11:11:12 +00:00
|
|
|
if (jsender == null && jrecipient == null && jsubject == null && jheader == null)
|
2019-01-18 10:58:55 +00:00
|
|
|
return false;
|
2019-01-17 21:41:00 +00:00
|
|
|
} catch (JSONException ex) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-02-04 13:01:36 +00:00
|
|
|
private boolean matches(Context context, String needle, String haystack, boolean regex) {
|
|
|
|
boolean matched = false;
|
|
|
|
if (needle != null && haystack != null)
|
|
|
|
if (regex) {
|
|
|
|
Pattern pattern = Pattern.compile(needle);
|
|
|
|
matched = pattern.matcher(haystack).matches();
|
|
|
|
} else
|
|
|
|
matched = haystack.toLowerCase().contains(needle.trim().toLowerCase());
|
|
|
|
|
|
|
|
EntityLog.log(context, "Rule=" + name + " matched=" + matched +
|
|
|
|
" needle=" + needle + " haystack=" + haystack + " regex=" + regex);
|
|
|
|
return matched;
|
2019-01-17 13:29:35 +00:00
|
|
|
}
|
|
|
|
|
2019-01-18 18:40:51 +00:00
|
|
|
void execute(Context context, DB db, EntityMessage message) throws IOException {
|
2019-01-17 21:41:00 +00:00
|
|
|
try {
|
|
|
|
JSONObject jargs = new JSONObject(action);
|
|
|
|
int type = jargs.getInt("type");
|
2019-01-18 11:26:02 +00:00
|
|
|
Log.i("Executing rule=" + type + ":" + name + " message=" + message.id);
|
2019-01-17 21:41:00 +00:00
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case TYPE_SEEN:
|
|
|
|
onActionSeen(context, db, message, true);
|
|
|
|
break;
|
|
|
|
case TYPE_UNSEEN:
|
|
|
|
onActionSeen(context, db, message, false);
|
|
|
|
break;
|
|
|
|
case TYPE_MOVE:
|
|
|
|
onActionMove(context, db, message, jargs);
|
|
|
|
break;
|
2019-01-18 18:40:51 +00:00
|
|
|
case TYPE_ANSWER:
|
|
|
|
onActionAnswer(context, db, message, jargs);
|
|
|
|
break;
|
2019-03-17 12:49:10 +00:00
|
|
|
case TYPE_AUTOMATION:
|
|
|
|
onActionAutomation(context, db, message, jargs);
|
|
|
|
break;
|
2019-01-17 21:41:00 +00:00
|
|
|
}
|
|
|
|
} catch (JSONException ex) {
|
|
|
|
Log.e(ex);
|
2019-01-17 13:29:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onActionSeen(Context context, DB db, EntityMessage message, boolean seen) {
|
|
|
|
EntityOperation.queue(context, db, message, EntityOperation.SEEN, seen);
|
2019-01-17 21:41:00 +00:00
|
|
|
message.seen = seen;
|
2019-01-17 13:29:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void onActionMove(Context context, DB db, EntityMessage message, JSONObject jargs) throws JSONException {
|
|
|
|
long target = jargs.getLong("target");
|
2019-01-18 08:43:29 +00:00
|
|
|
EntityOperation.queue(context, db, message, EntityOperation.MOVE, target, false);
|
2019-01-17 13:29:35 +00:00
|
|
|
}
|
|
|
|
|
2019-01-18 18:40:51 +00:00
|
|
|
private void onActionAnswer(Context context, DB db, EntityMessage message, JSONObject jargs) throws JSONException, IOException {
|
|
|
|
long iid = jargs.getLong("identity");
|
|
|
|
long aid = jargs.getLong("answer");
|
2019-02-18 09:19:12 +00:00
|
|
|
boolean cc = (jargs.has("cc") && jargs.getBoolean("cc"));
|
2019-01-18 18:40:51 +00:00
|
|
|
|
|
|
|
EntityIdentity identity = db.identity().getIdentity(iid);
|
|
|
|
if (identity == null)
|
|
|
|
throw new IllegalArgumentException("Rule identity not found");
|
|
|
|
|
|
|
|
String body = EntityAnswer.getAnswerText(db, aid, message.from);
|
|
|
|
if (body == null)
|
|
|
|
throw new IllegalArgumentException("Rule answer not found");
|
|
|
|
|
|
|
|
EntityMessage reply = new EntityMessage();
|
|
|
|
reply.account = message.account;
|
|
|
|
reply.folder = db.folder().getOutbox().id;
|
|
|
|
reply.identity = identity.id;
|
|
|
|
reply.msgid = EntityMessage.generateMessageId();
|
2019-01-21 16:45:05 +00:00
|
|
|
reply.references = (message.references == null ? "" : message.references + " ") + message.msgid;
|
|
|
|
reply.inreplyto = message.msgid;
|
2019-01-18 18:40:51 +00:00
|
|
|
reply.thread = message.thread;
|
|
|
|
reply.to = (message.reply == null || message.reply.length == 0 ? message.from : message.reply);
|
|
|
|
reply.from = new InternetAddress[]{new InternetAddress(identity.email, identity.name)};
|
2019-02-18 09:19:12 +00:00
|
|
|
if (cc)
|
|
|
|
reply.cc = message.cc;
|
2019-01-18 18:40:51 +00:00
|
|
|
reply.subject = context.getString(R.string.title_subject_reply, message.subject == null ? "" : message.subject);
|
|
|
|
reply.received = new Date().getTime();
|
2019-03-14 16:02:27 +00:00
|
|
|
|
|
|
|
reply.sender = MessageHelper.getSortKey(reply.from);
|
2019-04-05 06:33:43 +00:00
|
|
|
Uri lookupUri = ContactInfo.getLookupUri(context, 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-03-14 07:45:13 +00:00
|
|
|
Helper.writeText(reply.getFile(context), body);
|
2019-03-02 10:49:58 +00:00
|
|
|
db.message().setMessageContent(reply.id, true, HtmlHelper.getPreview(body), null);
|
2019-01-18 18:40:51 +00:00
|
|
|
|
2019-04-05 08:02:29 +00:00
|
|
|
Core.updateMessageSize(context, reply.id);
|
|
|
|
|
2019-01-18 18:40:51 +00:00
|
|
|
EntityOperation.queue(context, db, reply, EntityOperation.SEND);
|
|
|
|
}
|
|
|
|
|
2019-03-17 12:49:10 +00:00
|
|
|
private void onActionAutomation(Context context, DB db, EntityMessage message, JSONObject jargs) throws JSONException {
|
|
|
|
String sender = (message.from == null || message.from.length == 0
|
|
|
|
? null : ((InternetAddress) message.from[0]).getAddress());
|
|
|
|
|
|
|
|
Intent automation = new Intent(ACTION_AUTOMATION);
|
|
|
|
automation.putExtra(EXTRA_RULE, name);
|
|
|
|
automation.putExtra(EXTRA_SENDER, sender);
|
|
|
|
automation.putExtra(EXTRA_SUBJECT, message.subject);
|
|
|
|
|
2019-03-17 13:57:01 +00:00
|
|
|
Log.i("Sending " + automation);
|
|
|
|
try {
|
|
|
|
context.sendBroadcast(automation);
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
2019-03-17 12:49:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-17 13:29:35 +00:00
|
|
|
@Override
|
|
|
|
public boolean equals(Object obj) {
|
|
|
|
if (obj instanceof EntityRule) {
|
|
|
|
EntityRule other = (EntityRule) obj;
|
|
|
|
return this.folder.equals(other.folder) &&
|
|
|
|
this.name.equals(other.name) &&
|
2019-01-17 21:25:22 +00:00
|
|
|
this.order == other.order &&
|
|
|
|
this.enabled == other.enabled &&
|
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) &&
|
2019-01-17 21:25:22 +00:00
|
|
|
this.action.equals(other.action);
|
2019-01-17 13:29:35 +00:00
|
|
|
} else
|
|
|
|
return false;
|
|
|
|
}
|
2019-01-18 16:00:11 +00:00
|
|
|
|
|
|
|
public JSONObject toJSON() throws JSONException {
|
|
|
|
JSONObject json = new JSONObject();
|
2019-02-07 08:29:39 +00:00
|
|
|
json.put("id", id);
|
2019-01-18 16:00:11 +00:00
|
|
|
json.put("name", name);
|
|
|
|
json.put("order", order);
|
|
|
|
json.put("enabled", enabled);
|
|
|
|
json.put("stop", stop);
|
|
|
|
json.put("condition", condition);
|
|
|
|
json.put("action", action);
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static EntityRule fromJSON(JSONObject json) throws JSONException {
|
|
|
|
EntityRule rule = new EntityRule();
|
2019-02-07 08:29:39 +00:00
|
|
|
// id
|
2019-01-18 16:00:11 +00:00
|
|
|
rule.name = json.getString("name");
|
|
|
|
rule.order = json.getInt("order");
|
|
|
|
rule.enabled = json.getBoolean("enabled");
|
|
|
|
rule.stop = json.getBoolean("stop");
|
|
|
|
rule.condition = json.getString("condition");
|
|
|
|
rule.action = json.getString("action");
|
|
|
|
return rule;
|
|
|
|
}
|
2019-01-17 13:29:35 +00:00
|
|
|
}
|