|
|
@ -51,6 +51,7 @@ import java.util.Date;
|
|
|
|
import java.util.Enumeration;
|
|
|
|
import java.util.Enumeration;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Locale;
|
|
|
|
import java.util.Locale;
|
|
|
|
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
|
|
|
|
import javax.mail.Address;
|
|
|
|
import javax.mail.Address;
|
|
|
@ -117,6 +118,8 @@ public class EntityRule {
|
|
|
|
|
|
|
|
|
|
|
|
private static final long SEND_DELAY = 5000L; // milliseconds
|
|
|
|
private static final long SEND_DELAY = 5000L; // milliseconds
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static ExecutorService executor = Helper.getBackgroundExecutor(1, "rule");
|
|
|
|
|
|
|
|
|
|
|
|
boolean matches(Context context, EntityMessage message, Message imessage) throws MessagingException {
|
|
|
|
boolean matches(Context context, EntityMessage message, Message imessage) throws MessagingException {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
JSONObject jcondition = new JSONObject(condition);
|
|
|
|
JSONObject jcondition = new JSONObject(condition);
|
|
|
@ -411,11 +414,6 @@ public class EntityRule {
|
|
|
|
|
|
|
|
|
|
|
|
private boolean onActionAnswer(Context context, EntityMessage message, JSONObject jargs) throws JSONException, IOException, AddressException {
|
|
|
|
private boolean onActionAnswer(Context context, EntityMessage message, JSONObject jargs) throws JSONException, IOException, AddressException {
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
|
|
|
|
|
|
|
long iid = jargs.getLong("identity");
|
|
|
|
|
|
|
|
long aid = jargs.getLong("answer");
|
|
|
|
|
|
|
|
String to = jargs.optString("to");
|
|
|
|
|
|
|
|
boolean cc = jargs.optBoolean("cc");
|
|
|
|
|
|
|
|
boolean attachments = jargs.optBoolean("attachments");
|
|
|
|
boolean attachments = jargs.optBoolean("attachments");
|
|
|
|
|
|
|
|
|
|
|
|
if (message.auto_submitted != null && message.auto_submitted) {
|
|
|
|
if (message.auto_submitted != null && message.auto_submitted) {
|
|
|
@ -439,13 +437,38 @@ public class EntityRule {
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
executor.submit(new Runnable() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
answer(context, EntityRule.this, message, jargs);
|
|
|
|
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
|
|
|
|
Log.e(ex);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static void answer(Context context, EntityRule rule, EntityMessage message, JSONObject jargs) throws JSONException, AddressException, IOException {
|
|
|
|
|
|
|
|
Log.i("Answering name=" + rule.name);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
long iid = jargs.getLong("identity");
|
|
|
|
|
|
|
|
long aid = jargs.getLong("answer");
|
|
|
|
|
|
|
|
String to = jargs.optString("to");
|
|
|
|
|
|
|
|
boolean cc = jargs.optBoolean("cc");
|
|
|
|
|
|
|
|
boolean attachments = jargs.optBoolean("attachments");
|
|
|
|
|
|
|
|
|
|
|
|
EntityIdentity identity = db.identity().getIdentity(iid);
|
|
|
|
EntityIdentity identity = db.identity().getIdentity(iid);
|
|
|
|
if (identity == null)
|
|
|
|
if (identity == null)
|
|
|
|
throw new IllegalArgumentException("Rule identity not found name=" + name);
|
|
|
|
throw new IllegalArgumentException("Rule identity not found name=" + rule.name);
|
|
|
|
|
|
|
|
|
|
|
|
EntityAnswer answer = db.answer().getAnswer(aid);
|
|
|
|
EntityAnswer answer = db.answer().getAnswer(aid);
|
|
|
|
if (answer == null)
|
|
|
|
if (answer == null)
|
|
|
|
throw new IllegalArgumentException("Rule answer not found name=" + name);
|
|
|
|
throw new IllegalArgumentException("Rule answer not found name=" + rule.name);
|
|
|
|
|
|
|
|
|
|
|
|
EntityFolder outbox = db.folder().getOutbox();
|
|
|
|
EntityFolder outbox = db.folder().getOutbox();
|
|
|
|
if (outbox == null) {
|
|
|
|
if (outbox == null) {
|
|
|
@ -464,7 +487,7 @@ public class EntityRule {
|
|
|
|
EntityLog.log(context, "Answer loop" +
|
|
|
|
EntityLog.log(context, "Answer loop" +
|
|
|
|
" name=" + answer.name +
|
|
|
|
" name=" + answer.name +
|
|
|
|
" from=" + MessageHelper.formatAddresses(from));
|
|
|
|
" from=" + MessageHelper.formatAddresses(from));
|
|
|
|
return false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
EntityMessage reply = new EntityMessage();
|
|
|
|
EntityMessage reply = new EntityMessage();
|
|
|
@ -537,8 +560,6 @@ public class EntityRule {
|
|
|
|
|
|
|
|
|
|
|
|
// Batch send operations, wait until after commit
|
|
|
|
// Batch send operations, wait until after commit
|
|
|
|
ServiceSend.schedule(context, SEND_DELAY);
|
|
|
|
ServiceSend.schedule(context, SEND_DELAY);
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private boolean onActionAutomation(Context context, EntityMessage message, JSONObject jargs) {
|
|
|
|
private boolean onActionAutomation(Context context, EntityMessage message, JSONObject jargs) {
|
|
|
@ -569,6 +590,22 @@ public class EntityRule {
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
executor.submit(new Runnable() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
speak(context, EntityRule.this, message);
|
|
|
|
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
|
|
|
|
Log.e(ex);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static void speak(Context context, EntityRule rule, EntityMessage message) throws IOException {
|
|
|
|
|
|
|
|
Log.i("Speaking name=" + rule.name);
|
|
|
|
Locale locale = (message.language == null ? Locale.getDefault() : new Locale(message.language));
|
|
|
|
Locale locale = (message.language == null ? Locale.getDefault() : new Locale(message.language));
|
|
|
|
|
|
|
|
|
|
|
|
Configuration configuration = new Configuration(context.getResources().getConfiguration());
|
|
|
|
Configuration configuration = new Configuration(context.getResources().getConfiguration());
|
|
|
@ -593,8 +630,6 @@ public class EntityRule {
|
|
|
|
.append(' ').append(preview);
|
|
|
|
.append(' ').append(preview);
|
|
|
|
|
|
|
|
|
|
|
|
TTSHelper.speak(context, "rule:" + message.id, sb.toString(), locale);
|
|
|
|
TTSHelper.speak(context, "rule:" + message.id, sb.toString(), locale);
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private boolean onActionSnooze(Context context, EntityMessage message, JSONObject jargs) throws JSONException {
|
|
|
|
private boolean onActionSnooze(Context context, EntityMessage message, JSONObject jargs) throws JSONException {
|
|
|
|