From 69fbc51b8e46c96caae02b211ede820a13f293a1 Mon Sep 17 00:00:00 2001 From: M66B Date: Thu, 22 Aug 2019 18:09:52 +0200 Subject: [PATCH] Added option for Usenet signatures --- .../eu/faircode/email/FragmentCompose.java | 32 ++++++++++++++++++- .../faircode/email/FragmentOptionsSend.java | 12 ++++++- .../java/eu/faircode/email/MessageHelper.java | 11 ++++++- .../main/res/layout/fragment_options_send.xml | 13 +++++++- app/src/main/res/values/strings.xml | 1 + 5 files changed, 65 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/FragmentCompose.java b/app/src/main/java/eu/faircode/email/FragmentCompose.java index 2376a9e573..bf7dd1ea20 100644 --- a/app/src/main/java/eu/faircode/email/FragmentCompose.java +++ b/app/src/main/java/eu/faircode/email/FragmentCompose.java @@ -110,6 +110,7 @@ import com.google.android.material.bottomnavigation.LabelVisibilityMode; import com.google.android.material.snackbar.Snackbar; import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.openintents.openpgp.OpenPgpError; import org.openintents.openpgp.util.OpenPgpApi; @@ -2226,10 +2227,39 @@ public class FragmentCompose extends FragmentBase { !"editasnew".equals(action) && !"list".equals(action) && !"receipt".equals(action)) { + String refText = Helper.readText(ref.getFile(context)); + + boolean usenet = prefs.getBoolean("usenet_signature", false); + if (usenet) { + Document rdoc = Jsoup.parse(refText); + + Element signature = null; + for (Element e : rdoc.select("*")) + if ("-- ".equals(e.wholeText())) + signature = e; + + if (signature != null) { + List tbd = new ArrayList<>(); + tbd.add(signature); + + Element next = signature.nextElementSibling(); + while (next != null) { + tbd.add(0, next); + next = next.nextElementSibling(); + } + + for (Element e : tbd) + e.remove(); + + if (rdoc.body() != null) + refText = rdoc.body().html(); + } + } + String refBody = String.format("

%s %s:

\n
%s
", Html.escapeHtml(new Date(ref.received).toString()), Html.escapeHtml(MessageHelper.formatAddresses(ref.from)), - Helper.readText(ref.getFile(context))); + refText); Helper.writeText(draft.getRefFile(context), refBody); } diff --git a/app/src/main/java/eu/faircode/email/FragmentOptionsSend.java b/app/src/main/java/eu/faircode/email/FragmentOptionsSend.java index a9746b79f8..f689152a18 100644 --- a/app/src/main/java/eu/faircode/email/FragmentOptionsSend.java +++ b/app/src/main/java/eu/faircode/email/FragmentOptionsSend.java @@ -42,6 +42,7 @@ public class FragmentOptionsSend extends FragmentBase implements SharedPreferenc private SwitchCompat swKeyboard; private SwitchCompat swPrefixOnce; private SwitchCompat swPlainOnly; + private SwitchCompat swUsenetSignature; private SwitchCompat swAutoResize; private Spinner spAutoResize; private TextView tvAutoResize; @@ -50,7 +51,7 @@ public class FragmentOptionsSend extends FragmentBase implements SharedPreferenc private Spinner spSendDelayed; private final static String[] RESET_OPTIONS = new String[]{ - "keyboard", "prefix_once", "plain_only", "autoresize", "resize", "lookup_mx", "autosend", "send_delayed" + "keyboard", "prefix_once", "plain_only", "usenet_signature", "autoresize", "resize", "lookup_mx", "autosend", "send_delayed" }; @Override @@ -66,6 +67,7 @@ public class FragmentOptionsSend extends FragmentBase implements SharedPreferenc swKeyboard = view.findViewById(R.id.swKeyboard); swPrefixOnce = view.findViewById(R.id.swPrefixOnce); swPlainOnly = view.findViewById(R.id.swPlainOnly); + swUsenetSignature = view.findViewById(R.id.swUsenetSignature); swAutoResize = view.findViewById(R.id.swAutoResize); spAutoResize = view.findViewById(R.id.spAutoResize); tvAutoResize = view.findViewById(R.id.tvAutoResize); @@ -100,6 +102,13 @@ public class FragmentOptionsSend extends FragmentBase implements SharedPreferenc } }); + swUsenetSignature.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { + prefs.edit().putBoolean("usenet_signature", checked).apply(); + } + }); + swAutoResize.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { @@ -197,6 +206,7 @@ public class FragmentOptionsSend extends FragmentBase implements SharedPreferenc swKeyboard.setChecked(prefs.getBoolean("keyboard", true)); swPrefixOnce.setChecked(prefs.getBoolean("prefix_once", true)); swPlainOnly.setChecked(prefs.getBoolean("plain_only", false)); + swUsenetSignature.setChecked(prefs.getBoolean("usenet_signature", false)); swAutoResize.setChecked(prefs.getBoolean("autoresize", true)); diff --git a/app/src/main/java/eu/faircode/email/MessageHelper.java b/app/src/main/java/eu/faircode/email/MessageHelper.java index 0a1955bd64..4dabd92e98 100644 --- a/app/src/main/java/eu/faircode/email/MessageHelper.java +++ b/app/src/main/java/eu/faircode/email/MessageHelper.java @@ -20,10 +20,13 @@ package eu.faircode.email; */ import android.content.Context; +import android.content.SharedPreferences; import android.net.MailTo; import android.text.TextUtils; import android.webkit.MimeTypeMap; +import androidx.preference.PreferenceManager; + import com.sun.mail.util.FolderClosedIOException; import com.sun.mail.util.MessageRemovedIOException; @@ -213,6 +216,9 @@ public class MessageHelper { } static void build(Context context, EntityMessage message, List attachments, EntityIdentity identity, MimeMessage imessage) throws IOException, MessagingException { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + boolean usenet = prefs.getBoolean("usenet_signature", false); + if (message.receipt_request != null && message.receipt_request) { // https://www.ietf.org/rfc/rfc3798.txt Multipart report = new MimeMultipart("report; report-type=disposition-notification"); @@ -249,8 +255,11 @@ public class MessageHelper { if (identity != null) { if (!TextUtils.isEmpty(identity.signature)) { Document sdoc = Jsoup.parse(identity.signature); - if (sdoc.body() != null) + if (sdoc.body() != null) { + if (usenet) // https://www.ietf.org/rfc/rfc3676.txt + body.append("--
"); body.append(sdoc.body().html()); + } } File refFile = message.getRefFile(context); diff --git a/app/src/main/res/layout/fragment_options_send.xml b/app/src/main/res/layout/fragment_options_send.xml index 1871efcdfb..b941931560 100644 --- a/app/src/main/res/layout/fragment_options_send.xml +++ b/app/src/main/res/layout/fragment_options_send.xml @@ -54,6 +54,17 @@ app:layout_constraintTop_toBottomOf="@id/swPrefixOnce" app:switchPadding="12dp" /> + + Show keyboard by default Prefix subject only once on replying or forwarding Send plain text only by default + Usenet signature convention Automatically resize attached and embedded images < %1$d pixels Check recipient email addresses before sending