diff mbox series

[bug#62969,v4] home: Add msmtp service.

Message ID b2cd3fe9db8d8c4ddb0678dbd36937e83a09511d.1684313128.git.tanguy@bioneland.org
State New
Headers show
Series [bug#62969,v4] home: Add msmtp service. | expand

Commit Message

Tanguy LE CARROUR May 17, 2023, 8:51 a.m. UTC
* gnu/home/services/mail.scm: New file.
* gnu/local.mk (GNU_SYSTEM_MODULES): Add it.
* doc/guix.texi (Mailing): New node.
---
 doc/guix.texi              | 137 ++++++++++++++++++++++
 gnu/home/services/mail.scm | 229 +++++++++++++++++++++++++++++++++++++
 gnu/local.mk               |   3 +-
 3 files changed, 368 insertions(+), 1 deletion(-)
 create mode 100644 gnu/home/services/mail.scm


base-commit: c8e599b9391f789a8a3e2183fc8f0c2a5061ceb0
diff mbox series

Patch

diff --git a/doc/guix.texi b/doc/guix.texi
index 60972f408d..e61d7f81ed 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -115,6 +115,7 @@ 
 Copyright @copyright{} 2022 Antero Mejr@*
 Copyright @copyright{} 2023 Karl Hallsby
 Copyright @copyright{} 2023 Nathaniel Nicandro
+Copyright @copyright{} 2023 Tanguy Le Carrour
 
 Permission is granted to copy, distribute and/or modify this document
 under the terms of the GNU Free Documentation License, Version 1.3 or
@@ -42223,6 +42224,7 @@  Home Services
 * Guix: Guix Home Services.     Services for Guix.
 * Fonts: Fonts Home Services.   Services for managing User's fonts.
 * Sound: Sound Home Services.   Dealing with audio.
+* Mail: Mail Home Services.  Services for managing mail.
 * Messaging: Messaging Home Services.  Services for managing messaging.
 * Media: Media Home Services.   Services for managing media.
 @end menu
@@ -43452,6 +43454,141 @@  Sound Home Services
 This is the multicast address used by default by the two services above.
 @end defvar
 
+@node Mail Home Services
+@subsection Mail Home Services
+ 
+The @code{(gnu home services mail)} module provides services that help
+you set up the tools to work with emails in your home environment.
+ 
+@cindex msmtp
+@uref{https://marlam.de/msmtp, MSMTP} is a @acronym{SMTP, Simple Mail
+Transfer Protocol} client.  It sends mail to a predefined SMTP server
+that takes care of proper delivery.
+ 
+The service reference is given below.
+ 
+@defvar home-msmtp-service-type
+This is the service type for @command{msmtp}.  Its value must be a
+@code{home-msmtp-configuration}, as shown below.  It provides the
+@file{~/.config/msmtp/config} file.
+ 
+As an example, here is how you would configure @code{msmtp} for a single
+account:
+ 
+@lisp
+(service home-msmtp-service-type
+         (home-msmtp-configuration
+          (accounts
+           (list
+            (msmtp-account
+             (name "alice")
+             (configuration
+              (msmtp-configuration
+               (host "mail.example.org")
+               (port 587)
+               (user "alice")
+               (password-eval "pass Mail/alice"))))))))
+@end lisp
+@end defvar
+
+@c %start of fragment
+ 
+@deftp {Data Type} home-msmtp-configuration
+Available @code{home-msmtp-configuration} fields are:
+ 
+@table @asis
+@item @code{defaults} (type: msmtp-configuration)
+The configuration that will be set as default for all accounts.
+ 
+@item @code{accounts} (default: @code{()}) (type: list-of-msmtp-accounts)
+A list of @code{msmtp-account} records which contain information about
+all your accounts.
+ 
+@item @code{default-account} (type: maybe-string)
+Set the default account.
+ 
+@item @code{extra-content} (default: @code{""}) (type: string)
+Extra content appended as-is to the configuration file.  Run
+@command{man msmtp} for more information about the configuration file
+format.
+ 
+@end table
+ 
+@end deftp
+ 
+@c %end of fragment
+ 
+@c %start of fragment
+ 
+@deftp {Data Type} msmtp-account
+Available @code{msmtp-account} fields are:
+ 
+@table @asis
+@item @code{name} (type: string)
+The unique name of the account.
+ 
+@item @code{configuration} (type: msmtp-configuration)
+The configuration for this given account.
+ 
+@end table
+ 
+@end deftp
+ 
+@c %end of fragment
+
+@c %start of fragment
+ 
+@deftp {Data Type} msmtp-configuration
+Available @code{msmtp-configuration} fields are:
+ 
+@table @asis
+@item @code{auth?} (type: maybe-boolean)
+Enable or disable authentication.
+ 
+@item @code{tls?} (type: maybe-boolean)
+Enable or disable TLS (also known as SSL) for secured connections.
+ 
+@item @code{tls-starttls?} (type: maybe-boolean)
+Choose the TLS variant: start TLS from within the session (‘on’,
+default), or tunnel the session through TLS (‘off’).
+ 
+@item @code{tls-trust-file} (type: maybe-string)
+Activate server certificate verification using a list of trusted
+Certification Authorities (CAs).
+ 
+@item @code{log-file} (type: maybe-string)
+Enable logging to the specified file.  An empty argument disables
+logging.  The file name ‘-’ directs the log information to standard
+output.
+ 
+@item @code{host} (type: maybe-string)
+The SMTP server to send the mail to.
+ 
+@item @code{port} (type: maybe-integer)
+The port that the SMTP server listens on.  The default is 25 ("smtp"),
+unless TLS without STARTTLS is used, in which case it is 465 ("smtps").
+ 
+@item @code{user} (type: maybe-string)
+Set the user name for authentication.
+ 
+@item @code{from} (type: maybe-string)
+Set the envelope-from address.
+ 
+@item @code{password-eval} (type: maybe-string)
+Set the password for authentication to the output (stdout) of the
+command cmd.
+ 
+@item @code{extra-content} (default: @code{""}) (type: string)
+Extra content appended as-is to the configuration block.  Run
+@command{man msmtp} for more information about the configuration file
+format.
+ 
+@end table
+ 
+@end deftp
+ 
+@c %end of fragment
+
 @node Messaging Home Services
 @subsection Messaging Home Services
 
diff --git a/gnu/home/services/mail.scm b/gnu/home/services/mail.scm
new file mode 100644
index 0000000000..c3d34240f1
--- /dev/null
+++ b/gnu/home/services/mail.scm
@@ -0,0 +1,229 @@ 
+;;; GNU Guix --- Functional package management for GNU
+;;; Copyright © 2023 Tanguy Le Carrour <tanguy@bioneland.org>
+;;;
+;;; This file is part of GNU Guix.
+;;;
+;;; GNU Guix is free software; you can redistribute it and/or modify it
+;;; under the terms of the GNU General Public License as published by
+;;; the Free Software Foundation; either version 3 of the License, or (at
+;;; your option) any later version.
+;;;
+;;; GNU Guix is distributed in the hope that it will be useful, but
+;;; WITHOUT ANY WARRANTY; without even the implied warranty of
+;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;;; GNU General Public License for more details.
+;;;
+;;; You should have received a copy of the GNU General Public License
+;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.
+
+(define-module (gnu home services mail)
+  #:use-module (guix gexp)
+  #:use-module (gnu packages)
+  #:use-module (gnu services)
+  #:use-module (gnu services configuration)
+  #:use-module (gnu home services)
+  #:use-module (gnu home services shepherd)
+  #:use-module (ice-9 string-fun)
+  #:use-module (srfi srfi-1)
+  #:use-module (srfi srfi-26)
+  #:export (home-msmtp-configuration
+            home-msmtp-configuration?
+            home-msmtp-configuration-defaults
+            home-msmtp-configuration-accounts
+            home-msmtp-configuration-default-account
+            home-msmtp-configuration-extra-content
+            home-msmtp-service-type
+            msmtp-configuration
+            msmtp-configuration-auth?
+            msmtp-configuration-tls?
+            msmtp-configuration-tls-starttls?
+            msmtp-configuration-tls-trust-file
+            msmtp-configuration-log-file
+            msmtp-configuration-host
+            msmtp-configuration-port
+            msmtp-configuration-user
+            msmtp-configuration-from
+            msmtp-configuration-password-eval
+            msmtp-configuration-extra-content
+            msmtp-account
+            msmtp-account-name
+            msmtp-account-configuration))
+
+(define-maybe string)
+(define-maybe boolean)
+(define-maybe integer)
+
+;; Serialization of 'msmtp'.
+(define (uglify-symbol field-name)
+  (let* ((name (symbol->string field-name))
+         (ugly-name (string-replace-substring name "-" "_")))
+    (if (string-suffix? "?" ugly-name)
+      (string-drop-right ugly-name 1)
+      ugly-name)))
+
+(define (msmtp-configuration-serialize-maybe-boolean field-name value)
+  #~(if #$(maybe-value-set? value)
+      (string-append #$(uglify-symbol field-name) " " (if #$value "on" "off") "\n")
+      ""))
+
+(define (msmtp-configuration-serialize-maybe-string field-name value)
+  #~(if #$(maybe-value-set? value)
+      (string-append #$(uglify-symbol field-name) " " #$value "\n")
+      ""))
+
+(define (msmtp-configuration-serialize-maybe-string-no-underscore field-name value)
+  #~(if #$(maybe-value-set? value)
+      (string-append
+        #$(string-replace-substring (uglify-symbol field-name) "_" "") " " #$value "\n")
+      ""))
+
+(define (msmtp-configuration-serialize-maybe-integer field-name value)
+  #~(if #$(maybe-value-set? value)
+      (string-append #$(uglify-symbol field-name) " " (number->string #$value) "\n")
+      ""))
+
+(define (msmtp-configuration-serialize-extra-content field-name value)
+  #~(if (string=? #$value "") "" (string-append #$value "\n")))
+
+(define (msmtp-account-serialize-name field-name value)
+  #~(string-append "\naccount " #$value "\n"))
+
+(define (msmtp-account-serialize-msmtp-configuration field-name value)
+  #~(string-append #$(serialize-configuration value msmtp-configuration-fields)))
+
+(define (home-msmtp-configuration-serialize-list-of-msmtp-accounts field-name value)
+  #~(string-append #$@(map (cut serialize-configuration <> msmtp-account-fields)
+                           value)))
+
+(define (home-msmtp-configuration-serialize-msmtp-configuration field-name value)
+  #~(string-append "defaults\n"
+                   #$(serialize-configuration value msmtp-configuration-fields)))
+
+(define (home-msmtp-configuration-serialize-default-account field-name value)
+  #~(if #$(maybe-value-set? value)
+      (string-append "\naccount default : " #$value "\n")
+      ""))
+
+(define (home-msmtp-configuration-serialize-extra-content field-name value)
+  #~(if (string=? #$value "") "" (string-append #$value "\n")))
+
+;; Configuration of 'msmtp'.
+;; Source <https://marlam.de/msmtp/msmtp.html#Configuration-files>.
+(define-configuration msmtp-configuration
+  (auth?
+   maybe-boolean
+   "Enable or disable authentication.")
+
+  (tls?
+   maybe-boolean
+   "Enable or disable TLS (also known as SSL) for secured connections.")
+
+  (tls-starttls?
+   maybe-boolean
+   "Choose the TLS variant: start TLS from within the session (‘on’, default),
+or tunnel the session through TLS (‘off’).")
+
+  (tls-trust-file
+   maybe-string
+   "Activate server certificate verification using a list of
+trusted Certification Authorities (CAs).")
+
+  (log-file
+   maybe-string
+   "Enable logging to the specified file. An empty argument disables logging.
+The file name ‘-’ directs the log information to standard output."
+   (serializer msmtp-configuration-serialize-maybe-string-no-underscore))
+
+  (host
+    maybe-string
+    "The SMTP server to send the mail to.")
+
+  (port
+    maybe-integer
+    "The port that the SMTP server listens on. The default is 25 (\"smtp\"),
+unless TLS without STARTTLS is used, in which case it is 465 (\"smtps\").")
+
+  (user
+    maybe-string
+    "Set the user name for authentication.")
+
+  (from
+    maybe-string
+    "Set the envelope-from address.")
+
+  (password-eval
+    maybe-string
+    "Set the password for authentication to the output (stdout) of the command cmd."
+    (serializer msmtp-configuration-serialize-maybe-string-no-underscore))
+
+  (extra-content
+   (string "")
+   "Extra content appended as-is to the configuration block.  Run
+@command{man msmtp} for more information about the configuration file
+format."
+   (serializer msmtp-configuration-serialize-extra-content))
+
+  (prefix msmtp-configuration-))
+
+(define-configuration msmtp-account
+  (name
+   (string)
+   "The unique name of the account."
+   (serializer msmtp-account-serialize-name))
+
+  (configuration
+   (msmtp-configuration)
+   "The configuration for this given account.")
+
+  (prefix msmtp-account-))
+
+(define (list-of-msmtp-accounts? lst)
+  (every msmtp-account? lst))
+
+(define-configuration home-msmtp-configuration
+  (defaults
+   (msmtp-configuration (msmtp-configuration))
+   "The configuration that will be set as default for all accounts.")
+
+  (accounts
+   (list-of-msmtp-accounts '())
+   "A list of @code{msmtp-account} records which contain
+information about all your accounts.")
+
+  (default-account
+   maybe-string
+   "Set the default account."
+   (serializer home-msmtp-configuration-serialize-default-account))
+
+  (extra-content
+   (string "")
+   "Extra content appended as-is to the configuration file.  Run
+@command{man msmtp} for more information about the configuration file
+format."
+   (serializer home-msmtp-configuration-serialize-extra-content))
+
+  (prefix home-msmtp-configuration-))
+
+(define (home-msmtp-files-service config)
+  (list
+   `(".config/msmtp/config"
+     ,(mixed-text-file "config"
+                       (serialize-configuration config home-msmtp-configuration-fields)))))
+
+(define (home-msmtp-profile-service config)
+  (specifications->packages (list "msmtp")))
+
+(define home-msmtp-service-type
+  (service-type (name 'home-msmtp)
+                (extensions
+                  (list
+                    (service-extension
+                      home-profile-service-type
+                      home-msmtp-profile-service)
+                    (service-extension
+                      home-files-service-type
+                      home-msmtp-files-service)))
+                (default-value (home-msmtp-configuration))
+                (description "Configure msmtp, a simple
+@acronym{SMTP, Simple Mail Transfer Protocol} client that can relay email
+to SMTP servers.")))
diff --git a/gnu/local.mk b/gnu/local.mk
index 42514ded8e..930e69e289 100644
--- a/gnu/local.mk
+++ b/gnu/local.mk
@@ -35,7 +35,7 @@ 
 # Copyright © 2020 Ryan Prior <rprior@protonmail.com>
 # Copyright © 2020 Jan Wielkiewicz <tona_kosmicznego_smiecia@interia.pl>
 # Copyright © 2020, 2021 Brice Waegeneire <brice@waegenei.re>
-# Copyright © 2020 Tanguy Le Carrour <tanguy@bioneland.org>
+# Copyright © 2020, 2023 Tanguy Le Carrour <tanguy@bioneland.org>
 # Copyright © 2020 Martin Becze <mjbecze@riseup.net>
 # Copyright © 2020 Malte Frank Gerdes <mate.f.gerdes@gmail.com>
 # Copyright © 2020, 2023 Vinicius Monego <monego@posteo.net>
@@ -94,6 +94,7 @@  GNU_SYSTEM_MODULES =				\
   %D%/home/services/fontutils.scm		\
   %D%/home/services/gnupg.scm			\
   %D%/home/services/guix.scm			\
+  %D%/home/services/mail.scm			\
   %D%/home/services/media.scm			\
   %D%/home/services/messaging.scm		\
   %D%/home/services/pm.scm			\