Added external send template

pull/212/head
M66B 2 years ago
parent 40320ed887
commit d52411afdb

@ -562,6 +562,7 @@
<action android:name="${applicationId}.DISABLE" />
<action android:name="${applicationId}.INTERVAL" />
<action android:name="${applicationId}.RULE" />
<action android:name="${applicationId}.TEMPLATE" />
<action android:name="${applicationId}.DISCONNECT.ME" />
</intent-filter>
</service>

@ -568,6 +568,7 @@
<action android:name="${applicationId}.DISABLE" />
<action android:name="${applicationId}.INTERVAL" />
<action android:name="${applicationId}.RULE" />
<action android:name="${applicationId}.TEMPLATE" />
<action android:name="${applicationId}.DISCONNECT.ME" />
</intent-filter>
</service>

@ -568,6 +568,7 @@
<action android:name="${applicationId}.DISABLE" />
<action android:name="${applicationId}.INTERVAL" />
<action android:name="${applicationId}.RULE" />
<action android:name="${applicationId}.TEMPLATE" />
<action android:name="${applicationId}.DISCONNECT.ME" />
</intent-filter>
</service>

@ -560,6 +560,7 @@
<action android:name="${applicationId}.DISABLE" />
<action android:name="${applicationId}.INTERVAL" />
<action android:name="${applicationId}.RULE" />
<action android:name="${applicationId}.TEMPLATE" />
<action android:name="${applicationId}.DISCONNECT.ME" />
</intent-filter>
</service>

@ -58,6 +58,9 @@ public interface DaoAnswer {
@Query("SELECT * FROM answer WHERE uuid = :uuid")
EntityAnswer getAnswerByUUID(String uuid);
@Query("SELECT * FROM answer WHERE name = :name")
List<EntityAnswer> getAnswerByName(String name);
@Query("SELECT * FROM answer" +
" WHERE standard AND NOT hide")
EntityAnswer getStandardAnswer();

@ -89,6 +89,9 @@ public interface DaoIdentity {
@Query("SELECT * FROM identity WHERE uuid = :uuid")
EntityIdentity getIdentityByUUID(String uuid);
@Query("SELECT * FROM identity WHERE display = :display")
List<EntityIdentity> getIdentityByDisplayName(String display);
@Insert
long insertIdentity(EntityIdentity identity);

@ -24,7 +24,9 @@ import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.IBinder;
import android.text.TextUtils;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
@ -32,12 +34,18 @@ import androidx.preference.PreferenceManager;
import org.json.JSONException;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import javax.mail.Address;
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
public class ServiceExternal extends Service {
private static final String ACTION_POLL = BuildConfig.APPLICATION_ID + ".POLL";
@ -45,13 +53,15 @@ public class ServiceExternal extends Service {
private static final String ACTION_DISABLE = BuildConfig.APPLICATION_ID + ".DISABLE";
private static final String ACTION_INTERVAL = BuildConfig.APPLICATION_ID + ".INTERVAL";
private static final String ACTION_RULE = BuildConfig.APPLICATION_ID + ".RULE";
private static final String ACTION_TEMPLATE = BuildConfig.APPLICATION_ID + ".TEMPLATE";
private static final String ACTION_DISCONNECT_ME = BuildConfig.APPLICATION_ID + ".DISCONNECT.ME";
// adb shell am start-foreground-service -a eu.faircode.email.POLL --es account Gmail
// adb shell am start-foreground-service -a eu.faircode.email.ENABLE --es account Gmail
// adb shell am start-foreground-service -a eu.faircode.email.DISABLE --es account Gmail
// adb shell am start-foreground-service -a eu.faircode.email.INTERVAL --ei minutes {0, 15, 30, 60, 120, 240, 480, 1440}
// adb shell am start-foreground-service -a eu.faircode.email.RULE --es account Gmail -e rule Test
// adb shell am start-foreground-service -a eu.faircode.email.RULE --es account Gmail -es rule Test
// adb shell am start-foreground-service -a eu.faircode.email.TEMPLATE --es template ... --es identity ... --es to ... --es cc ... --es subject ...
// adb shell am start-foreground-service -a eu.faircode.email.DISCONNECT
@Override
@ -103,6 +113,9 @@ public class ServiceExternal extends Service {
case ACTION_RULE:
rule(context, intent);
break;
case ACTION_TEMPLATE:
template(context, intent);
break;
case ACTION_DISCONNECT_ME:
disconnect(context, intent);
break;
@ -250,6 +263,74 @@ public class ServiceExternal extends Service {
EntityLog.log(context, "Executing rule=" + rule.name + " applied=" + applied);
}
private static void template(Context context, Intent intent) throws AddressException, IOException {
String templateName = intent.getStringExtra("template");
String identityName = intent.getStringExtra("identity");
String toName = intent.getStringExtra("to");
String ccName = intent.getStringExtra("cc");
String subject = intent.getStringExtra("subject");
DB db = DB.getInstance(context);
List<EntityAnswer> answers = db.answer().getAnswerByName(templateName);
List<EntityIdentity> identity = db.identity().getIdentityByDisplayName(identityName);
Address[] to = InternetAddress.parse(toName);
Address[] cc = TextUtils.isEmpty(ccName) ? null : InternetAddress.parse(ccName);
if (answers == null || answers.size() == 0)
throw new IllegalArgumentException("Unknown template: " + templateName);
if (answers.size() != 1)
throw new IllegalArgumentException("Ambiguous template: " + templateName);
if (identity == null || identity.size() == 0)
throw new IllegalArgumentException("Unknown identity: " + identityName);
if (identity.size() != 1)
throw new IllegalArgumentException("Ambiguous identity: " + identityName);
if (to == null || to.length == 0)
throw new IllegalArgumentException("No to recipients: " + toName);
EntityFolder outbox = db.folder().getOutbox();
if (outbox == null) {
outbox = EntityFolder.getOutbox();
outbox.id = db.folder().insertFolder(outbox);
}
Address[] from = new Address[]{
new InternetAddress(identity.get(0).email, identity.get(0).name, StandardCharsets.UTF_8.name())};
if (subject == null) // Allow empty string
subject = answers.get(0).name;
String body = answers.get(0).getHtml(context, to);
EntityMessage msg = new EntityMessage();
msg.account = identity.get(0).account;
msg.folder = outbox.id;
msg.identity = identity.get(0).id;
msg.msgid = EntityMessage.generateMessageId();
msg.thread = msg.msgid;
msg.from = from;
msg.to = to;
msg.cc = cc;
msg.subject = subject;
msg.received = new Date().getTime();
msg.sender = MessageHelper.getSortKey(msg.from);
Uri lookupUri = ContactInfo.getLookupUri(msg.from);
msg.avatar = (lookupUri == null ? null : lookupUri.toString());
msg.id = db.message().insertMessage(msg);
File file = msg.getFile(context);
Helper.writeText(file, body);
String text = HtmlHelper.getFullText(body);
msg.preview = HtmlHelper.getPreview(text);
msg.language = HtmlHelper.getLanguage(context, msg.subject, text);
db.message().setMessageContent(msg.id,
true,
msg.language,
0,
msg.preview,
null);
EntityOperation.queue(context, msg, EntityOperation.SEND);
ServiceSend.start(context);
}
private static void disconnect(Context context, Intent intent) throws IOException, JSONException {
DisconnectBlacklist.download(context);
}

@ -562,6 +562,7 @@
<action android:name="${applicationId}.DISABLE" />
<action android:name="${applicationId}.INTERVAL" />
<action android:name="${applicationId}.RULE" />
<action android:name="${applicationId}.TEMPLATE" />
<action android:name="${applicationId}.DISCONNECT.ME" />
</intent-filter>
</service>

Loading…
Cancel
Save