Allow JSoup as note text

pull/213/head
M66B 1 year ago
parent 66016e386d
commit b5f3d3718a

@ -409,7 +409,7 @@ public class AdapterRule extends RecyclerView.Adapter<AdapterRule.ViewHolder> {
continue; continue;
if (rule.matches(context, message, null, null)) if (rule.matches(context, message, null, null))
if (rule.execute(context, message)) if (rule.execute(context, message, null))
applied++; applied++;
db.setTransactionSuccessful(); db.setTransactionSuccessful();

@ -3074,7 +3074,7 @@ class Core {
if (!message.content) if (!message.content)
throw new IllegalArgumentException("Message without content id=" + rule.id + ":" + rule.name); throw new IllegalArgumentException("Message without content id=" + rule.id + ":" + rule.name);
rule.execute(context, message); rule.execute(context, message, null);
} }
db.setTransactionSuccessful(); db.setTransactionSuccessful();

@ -44,6 +44,7 @@ import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element; import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
@ -135,7 +136,7 @@ public class EntityRule {
static final String EXTRA_SUBJECT = "subject"; static final String EXTRA_SUBJECT = "subject";
static final String EXTRA_RECEIVED = "received"; static final String EXTRA_RECEIVED = "received";
private static final String JSOUP_PREFIX = "jsoup:"; static final String JSOUP_PREFIX = "jsoup:";
private static final long SEND_DELAY = 5000L; // milliseconds private static final long SEND_DELAY = 5000L; // milliseconds
static boolean needsHeaders(EntityMessage message, List<EntityRule> rules) { static boolean needsHeaders(EntityMessage message, List<EntityRule> rules) {
@ -145,7 +146,7 @@ public class EntityRule {
static boolean needsBody(EntityMessage message, List<EntityRule> rules) { static boolean needsBody(EntityMessage message, List<EntityRule> rules) {
if (message.encrypt != null && !EntityMessage.ENCRYPT_NONE.equals(message.encrypt)) if (message.encrypt != null && !EntityMessage.ENCRYPT_NONE.equals(message.encrypt))
return false; return false;
return needs(rules, "body"); return needs(rules, "body") || needs(rules, "notes_jsoup");
} }
private static boolean needs(List<EntityRule> rules, String what) { private static boolean needs(List<EntityRule> rules, String what) {
@ -179,7 +180,7 @@ public class EntityRule {
if (rule.group != null && stopped.contains(rule.group)) if (rule.group != null && stopped.contains(rule.group))
continue; continue;
if (rule.matches(context, message, headers, html)) { if (rule.matches(context, message, headers, html)) {
if (rule.execute(context, message)) if (rule.execute(context, message, html))
applied++; applied++;
if (rule.stop) if (rule.stop)
if (rule.group == null) if (rule.group == null)
@ -554,8 +555,8 @@ public class EntityRule {
return matched; return matched;
} }
boolean execute(Context context, EntityMessage message) throws JSONException { boolean execute(Context context, EntityMessage message, String html) throws JSONException {
boolean executed = _execute(context, message); boolean executed = _execute(context, message, html);
if (this.id != null && executed) { if (this.id != null && executed) {
DB db = DB.getInstance(context); DB db = DB.getInstance(context);
db.rule().applyRule(id, new Date().getTime()); db.rule().applyRule(id, new Date().getTime());
@ -563,7 +564,7 @@ public class EntityRule {
return executed; return executed;
} }
private boolean _execute(Context context, EntityMessage message) throws JSONException, IllegalArgumentException { private boolean _execute(Context context, EntityMessage message, String html) throws JSONException, IllegalArgumentException {
JSONObject jaction = new JSONObject(action); JSONObject jaction = new JSONObject(action);
int type = jaction.getInt("type"); int type = jaction.getInt("type");
EntityLog.log(context, EntityLog.Type.Rules, message, EntityLog.log(context, EntityLog.Type.Rules, message,
@ -605,7 +606,7 @@ public class EntityRule {
case TYPE_LOCAL_ONLY: case TYPE_LOCAL_ONLY:
return onActionLocalOnly(context, message, jaction); return onActionLocalOnly(context, message, jaction);
case TYPE_NOTES: case TYPE_NOTES:
return onActionNotes(context, message, jaction); return onActionNotes(context, message, jaction, html);
default: default:
throw new IllegalArgumentException("Unknown rule type=" + type + " name=" + name); throw new IllegalArgumentException("Unknown rule type=" + type + " name=" + name);
} }
@ -1325,10 +1326,27 @@ public class EntityRule {
return true; return true;
} }
private boolean onActionNotes(Context context, EntityMessage message, JSONObject jargs) throws JSONException { private boolean onActionNotes(Context context, EntityMessage message, JSONObject jargs, String html) throws JSONException {
String notes = jargs.getString("notes"); String notes = jargs.getString("notes");
Integer color = (jargs.has("color") ? jargs.getInt("color") : null); Integer color = (jargs.has("color") ? jargs.getInt("color") : null);
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);
}
}
if (html != null) {
Document d = JsoupEx.parse(html);
Elements e = d.select(notes.substring(JSOUP_PREFIX.length()));
if (e.size() > 0)
notes = e.text();
}
}
DB db = DB.getInstance(context); DB db = DB.getInstance(context);
db.message().setMessageNotes(message.id, notes, color); db.message().setMessageNotes(message.id, notes, color);

@ -114,7 +114,7 @@ public class FragmentDialogRuleCheck extends FragmentDialogBase {
continue; continue;
if (rule.matches(context, message, null, null)) if (rule.matches(context, message, null, null))
if (rule.execute(context, message)) if (rule.execute(context, message, null))
applied++; applied++;
db.setTransactionSuccessful(); db.setTransactionSuccessful();

@ -1509,6 +1509,16 @@ public class FragmentRule extends FragmentBase {
JSONObject jdate = jcondition.optJSONObject("date"); JSONObject jdate = jcondition.optJSONObject("date");
JSONObject jschedule = jcondition.optJSONObject("schedule"); JSONObject jschedule = jcondition.optJSONObject("schedule");
JSONObject jaction = new JSONObject(action);
int type = jaction.getInt("type");
if (type == EntityRule.TYPE_NOTES) {
String notes = jaction.optString("notes");
if (notes.startsWith(EntityRule.JSOUP_PREFIX)) {
jcondition.put("notes_jsoup", true);
condition = jcondition.toString();
}
}
if (jsender == null && if (jsender == null &&
jrecipient == null && jrecipient == null &&
jsubject == null && jsubject == null &&

Loading…
Cancel
Save