From b42d6bbb84e5e05567f4c9f399da65333000c0fb Mon Sep 17 00:00:00 2001 From: M66B Date: Thu, 19 Nov 2020 18:54:44 +0100 Subject: [PATCH] Added rule validation --- .../java/eu/faircode/email/EntityRule.java | 71 +++++++++++++++++-- .../java/eu/faircode/email/FragmentRule.java | 2 + 2 files changed, 66 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/EntityRule.java b/app/src/main/java/eu/faircode/email/EntityRule.java index 48f7e77330..43a62f5540 100644 --- a/app/src/main/java/eu/faircode/email/EntityRule.java +++ b/app/src/main/java/eu/faircode/email/EntityRule.java @@ -307,7 +307,7 @@ public class EntityRule { return executed; } - private boolean _execute(Context context, EntityMessage message) throws JSONException { + private boolean _execute(Context context, EntityMessage message) throws JSONException, IllegalArgumentException { JSONObject jaction = new JSONObject(action); int type = jaction.getInt("type"); Log.i("Executing rule=" + type + ":" + name + " message=" + message.id); @@ -346,6 +346,65 @@ public class EntityRule { } } + void validate(Context context) throws JSONException, IllegalArgumentException { + 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: + String keyword = jargs.getString("keyword"); + if (TextUtils.isEmpty(keyword)) + throw new IllegalArgumentException("Keyword missing"); + case TYPE_MOVE: + case TYPE_COPY: + long target = jargs.optLong("target", -1); + if (target < 0) + throw new IllegalArgumentException("Folder missing"); + 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) + throw new IllegalArgumentException("Identity missing"); + EntityIdentity identity = db.identity().getIdentity(iid); + if (identity == null) + throw new IllegalArgumentException("Identity not found"); + + long aid = jargs.optLong("answer", -1); + if (aid < 0) + throw new IllegalArgumentException("Answer missing"); + EntityAnswer answer = db.answer().getAnswer(aid); + if (answer == null) + throw new IllegalArgumentException("Answer not found"); + return; + case TYPE_TTS: + return; + case TYPE_AUTOMATION: + return; + default: + throw new IllegalArgumentException("Unknown rule type=" + type); + } + } + private boolean onActionSeen(Context context, EntityMessage message, boolean seen) { EntityOperation.queue(context, message, EntityOperation.SEEN, seen); @@ -443,7 +502,7 @@ public class EntityRule { try { answer(context, EntityRule.this, message, jargs); } catch (Throwable ex) { - Log.e(ex); + Log.w(ex); } } }); @@ -596,7 +655,7 @@ public class EntityRule { try { speak(context, EntityRule.this, message); } catch (Throwable ex) { - Log.e(ex); + Log.w(ex); } } }); @@ -691,10 +750,8 @@ public class EntityRule { 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; - } + if (TextUtils.isEmpty(keyword)) + throw new IllegalArgumentException("Keyword missing rule=" + name); EntityOperation.queue(context, message, EntityOperation.KEYWORD, keyword, true); diff --git a/app/src/main/java/eu/faircode/email/FragmentRule.java b/app/src/main/java/eu/faircode/email/FragmentRule.java index c9a88160fb..7ade185ff1 100644 --- a/app/src/main/java/eu/faircode/email/FragmentRule.java +++ b/app/src/main/java/eu/faircode/email/FragmentRule.java @@ -1051,6 +1051,7 @@ public class FragmentRule extends FragmentBase { rule.stop = stop; rule.condition = condition; rule.action = action; + rule.validate(context); rule.id = db.rule().insertRule(rule); } else { EntityRule rule = db.rule().getRule(id); @@ -1061,6 +1062,7 @@ public class FragmentRule extends FragmentBase { rule.stop = stop; rule.condition = condition; rule.action = action; + rule.validate((context)); db.rule().updateRule(rule); }