Subject: [PATCH] gnu: msmtp: Make it configurable with envrinoment variables

From: Andrew Tropin <[email-masked]>
Date: Fri, 09 Jul 2021 13:26:30 +0300

From 38987bffcd1088315c6414d6c6141140c3969e6b Mon Sep 17 00:00:00 2001
From: Andrew Tropin <[email-masked]>
Date: Fri, 9 Jul 2021 13:20:10 +0300
Subject: [PATCH] gnu: msmtp: Make it configurable with envrinoment variables

Add the support of following environment variables:
MSMTPQ_QUEUE_DIR
MSMTPQ_LOG
MSMTPQ_QUIET
MSMTPQ_CONN_TEST

Add new --enqueue option, which forces to enqueue message instead of
attempting to send it.

Make msmtpq quite by default for compatibility with emacs and use ping instead
of netcat for connection test.

Use binary absolute path in msmtpq and msmtp-queue scripts instead of relying
on the binary being present in the $PATH.
---
 gnu/packages/mail.scm                         | 19 +++++--
 .../patches/msmtpq-add-enqueue-option.patch   | 40 +++++++++++++
 .../patches/msmtpq-add-env-variables.patch    | 56 +++++++++++++++++++
 3 files changed, 111 insertions(+), 4 deletions(-)
 create mode 100644 gnu/packages/patches/msmtpq-add-enqueue-option.patch
 create mode 100644 gnu/packages/patches/msmtpq-add-env-variables.patch

diff --git a/gnu/packages/mail.scm b/gnu/packages/mail.scm
index 74e3bda866..ed898d91d0 100644
--- a/gnu/packages/mail.scm
+++ b/gnu/packages/mail.scm
@@ -1677,7 +1677,10 @@ addons which can add many functionalities to the base client.")
        (uri (string-append "https://marlam.de/msmtp/releases/"
                            "/msmtp-" version ".tar.xz"))
        (sha256
-        (base32 "1klrj2a77671xb6xa0a0iyszhjb7swxhmzpzd4qdybmzkrixqr92"))))
+        (base32 "1klrj2a77671xb6xa0a0iyszhjb7swxhmzpzd4qdybmzkrixqr92"))
+       (patches
+        (search-patches "msmtpq-add-enqueue-option.patch"
+                        "msmtpq-add-env-variables.patch"))))
     (build-system gnu-build-system)
     (inputs
      `(("libsecret" ,libsecret)
@@ -1704,10 +1707,18 @@ addons which can add many functionalities to the base client.")
                (install-file (string-append msmtpq "/msmtp-queue") bin)
                (install-file (string-append msmtpq "/README.msmtpq") doc)
                (install-file "scripts/vim/msmtp.vim" vimfiles)
-               ;; Don't rely on netcat being in the PATH to test for a
-               ;; connection, instead look up and ping debian.org.
+               (substitute* (string-append bin "/msmtp-queue")
+                 (("^exec msmtpq") (format #f "exec ~a/msmtpq" bin)))
                (substitute* (string-append bin "/msmtpq")
-                 (("EMAIL_CONN_TEST=n") "EMAIL_CONN_TEST=p"))
+                 (("^MSMTP=msmtp") (format #f "MSMTP=~a/msmtp" bin))
+                 ;; Make msmtpq quite by default, because Emacs treat output
+                 ;; as an indicator of error.  Logging still works as it was.
+                 (("^EMAIL_QUEUE_QUIET=\\$\\{MSMTPQ_QUIET:-\\}")
+                  "EMAIL_QUEUE_QUIET=${MSMTPQ_QUIET:-t}")
+                 ;; Use ping test instead of netcat by default, because netcat
+                 ;; is optional and can be missing.
+                 (("^EMAIL_CONN_TEST=\\$\\{MSMTPQ_CONN_TEST:-n\\}")
+                  "EMAIL_CONN_TEST=${MSMTPQ_CONN_TEST:-p}"))
                #t))))))
     (synopsis
      "Simple and easy to use SMTP client with decent sendmail compatibility")
diff --git a/gnu/packages/patches/msmtpq-add-enqueue-option.patch b/gnu/packages/patches/msmtpq-add-enqueue-option.patch
new file mode 100644
index 0000000000..275ea6d04c
--- /dev/null
+++ b/gnu/packages/patches/msmtpq-add-enqueue-option.patch
@@ -0,0 +1,40 @@
+From a1be5a9b5ebaa3941f4cb08f99482bccf52a3082 Mon Sep 17 00:00:00 2001
+From: Andrew Tropin <[email-masked]>
+Date: Fri, 9 Jul 2021 08:30:00 +0300
+Subject: [PATCH] msmtpq: Add --enqueue option
+
+---
+ scripts/msmtpq/msmtpq | 13 ++++++++++++-
+ 1 file changed, 12 insertions(+), 1 deletion(-)
+
+diff --git a/scripts/msmtpq/msmtpq b/scripts/msmtpq/msmtpq
+index fbf38f9..0f127f0 100755
+--- a/scripts/msmtpq/msmtpq
++++ b/scripts/msmtpq/msmtpq
+@@ -520,11 +520,22 @@ if [ ! "$1" = '--q-mgmt' ] ; then    # msmtpq - sendmail mode
+   # write mail body text to queue .mail file
+   cat > "${FQP}.mail" || \
+     log -e "$?" "creating mail body file [ ${FQP}.mail ] : failed" # test for error
++  # remove first argument, when --engueue provided
++  if [ "$1" = '--enqueue' ] ; then
++    enqueue=t
++    shift
++  fi
+   # write msmtp command line to queue .msmtp file
+   echo "$@" > "${FQP}.msmtp" || \
+     log -e "$?" "creating msmtp cmd line file { $* }"\
+                 "           to [ ${ID}.msmtp ] : failed" # test for error
+-  send_mail "$@"                     # send the mail if possible, queue it if not
++
++  if [ -n "$enqueue" ] ; then
++    enqueue_mail "$@"                # always enqueue the mail
++  else
++    send_mail "$@"                   # send the mail if possible, queue it if not
++  fi
++
+   lock_queue -u                      # unlock here
+ else                                 # msmtp-queue - queue management mode
+   shift                              # trim off first (--q-mgmt) arg
+-- 
+2.32.0
+
diff --git a/gnu/packages/patches/msmtpq-add-env-variables.patch b/gnu/packages/patches/msmtpq-add-env-variables.patch
new file mode 100644
index 0000000000..d0a2813812
--- /dev/null
+++ b/gnu/packages/patches/msmtpq-add-env-variables.patch
@@ -0,0 +1,56 @@
+From 511be91b0aad2dc9bcb9ae16b4576cd01787ac34 Mon Sep 17 00:00:00 2001
+From: Andrew Tropin <[email-masked]>
+Date: Fri, 9 Jul 2021 08:29:19 +0300
+Subject: [PATCH] msmtpq: Allow to control queue dir, log file, etc via env
+ variables
+
+Provided variables:
+MSMTPQ_QUEUE_DIR
+MSMTPQ_LOG
+MSMTPQ_QUIET
+MSMTPQ_CONN_TEST
+---
+ scripts/msmtpq/msmtpq | 13 +++++++------
+ 1 file changed, 7 insertions(+), 6 deletions(-)
+
+diff --git a/scripts/msmtpq/msmtpq b/scripts/msmtpq/msmtpq
+index 4b074de..fbf38f9 100755
+--- a/scripts/msmtpq/msmtpq
++++ b/scripts/msmtpq/msmtpq
+@@ -70,9 +70,9 @@ MSMTP=msmtp
+ ##            ( chmod 0700 msmtp.queue )
+ ##
+ ## the queue dir - modify this to reflect where you'd like it to be  (no quotes !!)
+-Q=~/.msmtp.queue
+-[ -d "$Q" ] || \
+-  err '' "msmtpq : can't find msmtp queue directory [ $Q ]" ''     # if not present - complain ; quit
++Q=${MSMTPQ_QUEUE_DIR:-~/.msmtp.queue}
++[ -d "$Q" ] || mkdir -p "$Q" || \
++  err '' "msmtpq : can't create msmtp queue directory [ $Q ]" ''
+ ##
+ ## set the queue log file var to the location of the msmtp queue log file
+ ##   where it is or where you'd like it to be
+@@ -84,7 +84,8 @@ Q=~/.msmtp.queue
+ ##     (doing so would be inadvisable under most conditions, however)
+ ##
+ ## the queue log file - modify (or comment out) to taste  (but no quotes !!)
+-LOG=~/log/msmtp.queue.log
++LOG=${MSMTPQ_LOG:-~/log/msmtp.queue.log}
++[ -d "$(dirname $LOG)" ] || mkdir -p "$(dirname $LOG)"
+ ## ======================================================================================
+ 
+ ## msmtpq can use the following environment variables :
+@@ -108,8 +109,8 @@ LOG=~/log/msmtp.queue.log
+ ##
+ #EMAIL_CONN_NOTEST=y                 # deprecated ; use below var
+ #EMAIL_CONN_TEST={x| |p|P|n|s}       # see settings above for EMAIL_CONN_TEST
+-EMAIL_CONN_TEST=n
+-#EMAIL_QUEUE_QUIET=t
++EMAIL_QUEUE_QUIET=${MSMTPQ_QUIET:-}
++EMAIL_CONN_TEST=${MSMTPQ_CONN_TEST:-n}
+ ## ======================================================================================
+ 
+ umask 077                            # set secure permissions on created directories and files
+-- 
+2.32.0
+
-- 
2.32.0