mail.scm 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2023 Tanguy Le Carrour <tanguy@bioneland.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (gnu home services mail)
  19. #:use-module (guix gexp)
  20. #:use-module (gnu services)
  21. #:use-module (gnu services configuration)
  22. #:use-module (gnu home services)
  23. #:use-module (gnu home services shepherd)
  24. #:use-module (gnu packages mail)
  25. #:use-module (ice-9 string-fun)
  26. #:use-module (srfi srfi-1)
  27. #:use-module (srfi srfi-26)
  28. #:export (home-msmtp-configuration
  29. home-msmtp-configuration?
  30. home-msmtp-configuration-defaults
  31. home-msmtp-configuration-accounts
  32. home-msmtp-configuration-default-account
  33. home-msmtp-configuration-extra-content
  34. home-msmtp-service-type
  35. msmtp-configuration
  36. msmtp-configuration-auth?
  37. msmtp-configuration-tls?
  38. msmtp-configuration-tls-starttls?
  39. msmtp-configuration-tls-trust-file
  40. msmtp-configuration-log-file
  41. msmtp-configuration-host
  42. msmtp-configuration-port
  43. msmtp-configuration-user
  44. msmtp-configuration-from
  45. msmtp-configuration-password-eval
  46. msmtp-configuration-extra-content
  47. msmtp-account
  48. msmtp-account-name
  49. msmtp-account-configuration))
  50. (define-maybe string (prefix msmtp-configuration-))
  51. (define-maybe boolean (prefix msmtp-configuration-))
  52. (define-maybe integer (prefix msmtp-configuration-))
  53. ;; Serialization of 'msmtp'.
  54. (define (uglify-symbol field-name)
  55. (let* ((name (symbol->string field-name))
  56. (ugly-name (string-replace-substring name "-" "_")))
  57. (if (string-suffix? "?" ugly-name)
  58. (string-drop-right ugly-name 1)
  59. ugly-name)))
  60. (define (msmtp-configuration-serialize-boolean field-name value)
  61. #~(string-append #$(uglify-symbol field-name) " "
  62. (if #$value "on" "off") "\n"))
  63. (define (msmtp-configuration-serialize-string field-name value)
  64. #~(string-append #$(uglify-symbol field-name) " " #$value "\n"))
  65. (define (msmtp-configuration-serialize-maybe-string-no-underscore field-name value)
  66. #~(if #$(maybe-value-set? value)
  67. (string-append
  68. #$(string-replace-substring (uglify-symbol field-name) "_" "") " " #$value "\n")
  69. ""))
  70. (define (msmtp-configuration-serialize-integer field-name value)
  71. #~(string-append #$(uglify-symbol field-name) " "
  72. (number->string #$value) "\n"))
  73. (define (msmtp-configuration-serialize-extra-content field-name value)
  74. #~(if (string=? #$value "") "" (string-append #$value "\n")))
  75. (define (msmtp-account-serialize-name field-name value)
  76. #~(string-append "\naccount " #$value "\n"))
  77. (define (msmtp-account-serialize-msmtp-configuration field-name value)
  78. #~(string-append #$(serialize-configuration value msmtp-configuration-fields)))
  79. (define (home-msmtp-configuration-serialize-list-of-msmtp-accounts field-name value)
  80. #~(string-append #$@(map (cut serialize-configuration <> msmtp-account-fields)
  81. value)))
  82. (define (home-msmtp-configuration-serialize-msmtp-configuration field-name value)
  83. #~(string-append "defaults\n"
  84. #$(serialize-configuration value msmtp-configuration-fields)))
  85. (define (home-msmtp-configuration-serialize-default-account field-name value)
  86. #~(if #$(maybe-value-set? value)
  87. (string-append "\naccount default : " #$value "\n")
  88. ""))
  89. (define (home-msmtp-configuration-serialize-extra-content field-name value)
  90. #~(if (string=? #$value "") "" (string-append #$value "\n")))
  91. ;; Configuration of 'msmtp'.
  92. ;; Source <https://marlam.de/msmtp/msmtp.html#Configuration-files>.
  93. (define-configuration msmtp-configuration
  94. (auth?
  95. maybe-boolean
  96. "Enable or disable authentication.")
  97. (tls?
  98. maybe-boolean
  99. "Enable or disable TLS (also known as SSL) for secured connections.")
  100. (tls-starttls?
  101. maybe-boolean
  102. "Choose the TLS variant: start TLS from within the session (‘on’, default),
  103. or tunnel the session through TLS (‘off’).")
  104. (tls-trust-file
  105. maybe-string
  106. "Activate server certificate verification using a list of
  107. trusted Certification Authorities (CAs).")
  108. (log-file
  109. maybe-string
  110. "Enable logging to the specified file. An empty argument disables logging.
  111. The file name ‘-’ directs the log information to standard output."
  112. (serializer msmtp-configuration-serialize-maybe-string-no-underscore))
  113. (host
  114. maybe-string
  115. "The SMTP server to send the mail to.")
  116. (port
  117. maybe-integer
  118. "The port that the SMTP server listens on. The default is 25 (\"smtp\"),
  119. unless TLS without STARTTLS is used, in which case it is 465 (\"smtps\").")
  120. (user
  121. maybe-string
  122. "Set the user name for authentication.")
  123. (from
  124. maybe-string
  125. "Set the envelope-from address.")
  126. (password-eval
  127. maybe-string
  128. "Set the password for authentication to the output (stdout) of the command cmd."
  129. (serializer msmtp-configuration-serialize-maybe-string-no-underscore))
  130. (extra-content
  131. (string "")
  132. "Extra content appended as-is to the configuration block. Run
  133. @command{man msmtp} for more information about the configuration file
  134. format."
  135. (serializer msmtp-configuration-serialize-extra-content))
  136. (prefix msmtp-configuration-))
  137. (define-configuration msmtp-account
  138. (name
  139. (string)
  140. "The unique name of the account."
  141. (serializer msmtp-account-serialize-name))
  142. (configuration
  143. (msmtp-configuration)
  144. "The configuration for this given account.")
  145. (prefix msmtp-account-))
  146. (define (list-of-msmtp-accounts? lst)
  147. (every msmtp-account? lst))
  148. (define-configuration home-msmtp-configuration
  149. (defaults
  150. (msmtp-configuration (msmtp-configuration))
  151. "The configuration that will be set as default for all accounts.")
  152. (accounts
  153. (list-of-msmtp-accounts '())
  154. "A list of @code{msmtp-account} records which contain
  155. information about all your accounts.")
  156. (default-account
  157. maybe-string
  158. "Set the default account."
  159. (serializer home-msmtp-configuration-serialize-default-account))
  160. (extra-content
  161. (string "")
  162. "Extra content appended as-is to the configuration file. Run
  163. @command{man msmtp} for more information about the configuration file
  164. format."
  165. (serializer home-msmtp-configuration-serialize-extra-content))
  166. (prefix home-msmtp-configuration-))
  167. (define (home-msmtp-files config)
  168. (list
  169. `(".config/msmtp/config"
  170. ,(mixed-text-file "msmtp-config"
  171. (serialize-configuration config home-msmtp-configuration-fields)))))
  172. (define (home-msmtp-profile-entries config)
  173. (list msmtp))
  174. (define home-msmtp-service-type
  175. (service-type (name 'home-msmtp)
  176. (extensions
  177. (list (service-extension home-profile-service-type
  178. home-msmtp-profile-entries)
  179. (service-extension home-files-service-type
  180. home-msmtp-files)))
  181. (default-value (home-msmtp-configuration))
  182. (description "Configure msmtp, a simple
  183. @acronym{SMTP, Simple Mail Transfer Protocol} client that can relay email
  184. to SMTP servers.")))