diff --git a/FAQ.md b/FAQ.md index a3a5c2ea4d..1ea5a6b242 100644 --- a/FAQ.md +++ b/FAQ.md @@ -389,6 +389,7 @@ The low priority status bar notification shows the number of pending operations, * *subscribe*: subscribe to remote folder * *send*: send message * *exists*: check if message exists +* *rule*: execute rule on body text Operations are processed only when there is a connection to the email server or when manually synchronizing. See also [this FAQ](#user-content-faq16). diff --git a/app/src/main/java/eu/faircode/email/Core.java b/app/src/main/java/eu/faircode/email/Core.java index 85a5110949..2a39ae8074 100644 --- a/app/src/main/java/eu/faircode/email/Core.java +++ b/app/src/main/java/eu/faircode/email/Core.java @@ -381,6 +381,10 @@ class Core { onSubscribeFolder(context, jargs, folder, (IMAPFolder) ifolder); break; + case EntityOperation.RULE: + onRule(context, jargs, message); + break; + default: throw new IllegalArgumentException("Unknown operation=" + op.name); } @@ -1711,6 +1715,21 @@ class Core { Log.i(folder.name + " subscribed=" + subscribe); } + private static void onRule(Context context, JSONArray jargs, EntityMessage message) throws JSONException, IOException { + // Download message body + DB db = DB.getInstance(context); + + long id = jargs.getLong(0); + EntityRule rule = db.rule().getRule(id); + if (rule == null) + throw new IllegalArgumentException("Rule not found id=" + id); + + if (!message.content) + throw new IllegalArgumentException("Message without content id=" + rule.id + ":" + rule.name); + + rule.execute(context, message); + } + private static void onSynchronizeMessages( Context context, JSONArray jargs, EntityAccount account, final EntityFolder folder, diff --git a/app/src/main/java/eu/faircode/email/EntityOperation.java b/app/src/main/java/eu/faircode/email/EntityOperation.java index 15b727a9f9..79f055d69f 100644 --- a/app/src/main/java/eu/faircode/email/EntityOperation.java +++ b/app/src/main/java/eu/faircode/email/EntityOperation.java @@ -98,6 +98,7 @@ public class EntityOperation { static final String SUBSCRIBE = "subscribe"; static final String SEND = "send"; static final String EXISTS = "exists"; + static final String RULE = "rule"; static void queue(Context context, EntityMessage message, String name, Object... values) { DB db = DB.getInstance(context); diff --git a/app/src/main/java/eu/faircode/email/EntityRule.java b/app/src/main/java/eu/faircode/email/EntityRule.java index 1d7dcabc8a..6a9bd255f1 100644 --- a/app/src/main/java/eu/faircode/email/EntityRule.java +++ b/app/src/main/java/eu/faircode/email/EntityRule.java @@ -36,6 +36,8 @@ import androidx.room.PrimaryKey; import org.json.JSONException; import org.json.JSONObject; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; import java.io.File; import java.io.IOException; @@ -407,6 +409,12 @@ public class EntityRule { } private boolean onActionAnswer(Context context, EntityMessage message, JSONObject jargs) throws JSONException, IOException { + if (!message.content) { + EntityOperation.queue(context, message, EntityOperation.BODY); + EntityOperation.queue(context, message, EntityOperation.RULE, this.id); + return true; + } + long iid = jargs.getLong("identity"); long aid = jargs.getLong("answer"); boolean cc = (jargs.has("cc") && jargs.getBoolean("cc")); @@ -458,6 +466,22 @@ public class EntityRule { reply.id = db.message().insertMessage(reply); String body = answer.getText(message.from); + Document msg = JsoupEx.parse(body); + + Element div = msg.createElement("div"); + + Element p = msg.createElement("p"); + DateFormat DF = Helper.getDateTimeInstance(context); + p.text(DF.format(new Date(message.received)) + " " + MessageHelper.formatAddresses(message.from) + ":"); + div.appendChild(p); + + Document answering = JsoupEx.parse(message.getFile(context)); + div.appendChild(answering.body().tagName("blockquote")); + + msg.body().appendChild(div); + + body = msg.outerHtml(); + File file = reply.getFile(context); Helper.writeText(file, body); db.message().setMessageContent(reply.id,