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

573 lines
21 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/>.
2020-01-05 17:32:53 +00:00
Copyright 2018-2020 by Marcel Bokhorst (M66B)
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;
2019-03-14 16:02:27 +00:00
import android.net.Uri;
2019-10-09 09:09:00 +00:00
import android.text.TextUtils;
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;
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-08-04 17:14:53 +00:00
import java.util.Calendar;
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 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;
@NonNull
public Integer applied = 0;
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;
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";
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");
2019-09-22 09:44:11 +00:00
boolean known = jsender.optBoolean("known");
2019-01-18 10:58:55 +00:00
boolean matches = false;
if (message.from != null) {
for (Address from : message.from) {
InternetAddress ia = (InternetAddress) from;
2019-09-22 09:44:11 +00:00
String email = ia.getAddress();
2019-01-18 10:58:55 +00:00
String personal = ia.getPersonal();
2019-09-22 09:44:11 +00:00
if (known) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean suggest_sent = prefs.getBoolean("suggest_sent", true);
2019-09-22 09:44:11 +00:00
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;
}
}
2020-02-02 19:17:03 +00:00
if (!TextUtils.isEmpty(message.avatar)) {
Log.i(email + " is Android contact");
matches = true;
break;
2019-09-22 09:44:11 +00:00
}
} else {
String formatted = ((personal == null ? "" : personal + " ") + "<" + email + ">");
if (matches(context, value, formatted, regex)) {
matches = true;
break;
}
2019-01-18 10:58:55 +00:00
}
}
}
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-08-04 17:14:53 +00:00
// Subject
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;
}
2019-10-13 14:47:35 +00:00
// Attachments
if (jcondition.optBoolean("attachments")) {
DB db = DB.getInstance(context);
if (db.attachment().getAttachments(message.id).size() == 0)
return false;
}
2019-08-04 17:14:53 +00:00
// Header
2019-01-18 13:04:05 +00:00
JSONObject jheader = jcondition.optJSONObject("header");
2019-05-22 07:01:25 +00:00
if (jheader != null && imessage != null) {
2019-01-18 13:04:05 +00:00
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
2019-08-04 17:14:53 +00:00
// Schedule
JSONObject jschedule = jcondition.optJSONObject("schedule");
if (jschedule != null) {
int start = jschedule.optInt("start", 0);
int end = jschedule.optInt("end", 0);
2019-08-25 06:07:02 +00:00
Calendar cal_start = getRelativeCalendar(start, message.received);
Calendar cal_end = getRelativeCalendar(end, message.received);
2019-08-21 11:32:25 +00:00
if (cal_start.getTimeInMillis() > cal_end.getTimeInMillis())
cal_start.add(Calendar.HOUR_OF_DAY, -7 * 24);
if (message.received < cal_start.getTimeInMillis() ||
message.received > cal_end.getTimeInMillis())
2019-08-04 17:14:53 +00:00
return false;
}
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 &&
jschedule == 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) {
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
2019-07-08 14:47:16 +00:00
Log.i("Rule=" + name + " matched=" + matched +
" needle=" + needle + " haystack=" + haystack + " regex=" + regex);
2019-02-04 13:01:36 +00:00
return matched;
2019-01-17 13:29:35 +00:00
}
2019-07-16 06:25:30 +00:00
boolean execute(Context context, EntityMessage message) throws JSONException, IOException {
boolean executed = _execute(context, message);
2019-10-21 07:06:02 +00:00
if (id != null && executed) {
DB db = DB.getInstance(context);
db.rule().applyRule(id);
}
return executed;
}
private boolean _execute(Context context, EntityMessage message) throws JSONException, IOException {
2019-07-16 06:25:30 +00:00
JSONObject jaction = new JSONObject(action);
int type = jaction.getInt("type");
Log.i("Executing rule=" + type + ":" + name + " message=" + message.id);
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);
case TYPE_AUTOMATION:
return onActionAutomation(context, message, jaction);
default:
throw new IllegalArgumentException("Unknown rule type=" + type);
2019-01-17 13:29:35 +00:00
}
}
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);
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);
boolean seen = jargs.optBoolean("seen");
boolean thread = jargs.optBoolean("thread");
2019-05-23 13:57:44 +00:00
DB db = DB.getInstance(context);
EntityFolder folder = db.folder().getFolder(target);
if (folder == null)
throw new IllegalArgumentException("Rule move to folder not found");
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)
EntityOperation.queue(context, threaded, EntityOperation.MOVE, target, seen);
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
}
2019-07-16 06:25:30 +00:00
private boolean onActionCopy(Context context, EntityMessage message, JSONObject jargs) throws JSONException {
2019-05-18 06:34:26 +00:00
long target = jargs.getLong("target");
2019-05-23 13:57:44 +00:00
DB db = DB.getInstance(context);
EntityFolder folder = db.folder().getFolder(target);
if (folder == null)
throw new IllegalArgumentException("Rule copy to folder not found");
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
}
2019-07-16 06:25:30 +00:00
private boolean onActionAnswer(Context context, EntityMessage message, JSONObject jargs) throws JSONException, IOException {
2019-01-18 18:40:51 +00:00
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
2019-05-18 06:49:20 +00:00
DB db = DB.getInstance(context);
2019-01-18 18:40:51 +00:00
EntityIdentity identity = db.identity().getIdentity(iid);
if (identity == null)
throw new IllegalArgumentException("Rule identity not found");
2019-09-27 17:37:34 +00:00
EntityAnswer answer = db.answer().getAnswer(aid);
if (answer == null)
2019-01-18 18:40:51 +00:00
throw new IllegalArgumentException("Rule answer not found");
2019-09-27 17:37:34 +00:00
2019-01-18 18:40:51 +00:00
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;
reply.unsubscribe = "mailto:" + identity.email;
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-09-27 17:37:34 +00:00
String body = answer.getText(message.from);
2019-03-14 07:45:13 +00:00
Helper.writeText(reply.getFile(context), body);
2019-05-04 18:52:21 +00:00
db.message().setMessageContent(reply.id,
true,
false,
HtmlHelper.getPreview(body),
null);
2019-01-18 18:40:51 +00:00
2019-05-18 06:49:20 +00:00
EntityOperation.queue(context, reply, EntityOperation.SEND);
2019-09-13 13:17:53 +00:00
2019-07-16 06:25:30 +00:00
return true;
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) {
2019-03-17 12:49:10 +00:00
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);
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
}
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));
2019-08-25 06:07:02 +00:00
Calendar cal = getRelativeCalendar(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
2019-05-18 06:49:20 +00:00
EntityOperation.queue(context, message, EntityOperation.FLAG, true, color);
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);
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");
if (TextUtils.isEmpty(keyword)) {
Log.w("Keyword empty");
return false;
}
EntityOperation.queue(context, message, EntityOperation.KEYWORD, keyword, true);
return true;
}
2019-08-25 06:07:02 +00:00
private static Calendar getRelativeCalendar(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();
2019-08-25 06:07:02 +00:00
if (reference > cal.getTimeInMillis() - 7 * 24 * 3600 * 1000L)
cal.setTimeInMillis(reference);
long time = cal.getTimeInMillis();
2019-08-23 17:50:50 +00:00
cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY + d);
2019-08-25 06:07:02 +00:00
if (cal.getTimeInMillis() < time)
2019-08-23 17:50:50 +00:00
cal.add(Calendar.HOUR_OF_DAY, 7 * 24);
cal.set(Calendar.HOUR_OF_DAY, h);
cal.set(Calendar.MINUTE, m);
cal.set(Calendar.SECOND, 0);
return cal;
}
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) &&
this.action.equals(other.action) &&
this.applied.equals(other.applied);
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();
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);
2020-02-10 07:58:23 +00:00
json.put("applied", applied);
2019-01-18 16:00:11 +00:00
return json;
}
public static EntityRule fromJSON(JSONObject json) throws JSONException {
EntityRule rule = new EntityRule();
// 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");
2020-02-10 07:58:23 +00:00
rule.applied = json.optInt("applied", 0);
2019-01-18 16:00:11 +00:00
return rule;
}
2019-01-17 13:29:35 +00:00
}