1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2019 Christopher Baines <mail@cbaines.net>
4 ;;; This file is part of GNU Guix.
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.
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.
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/>.
19 (define-module (gnu services getmail)
20 #:use-module (gnu services)
21 #:use-module (gnu services base)
22 #:use-module (gnu services configuration)
23 #:use-module (gnu services shepherd)
24 #:use-module (gnu system pam)
25 #:use-module (gnu system shadow)
26 #:use-module (gnu packages mail)
27 #:use-module (gnu packages admin)
28 #:use-module (gnu packages tls)
29 #:use-module (guix records)
30 #:use-module (guix store)
31 #:use-module (guix packages)
32 #:use-module (guix gexp)
33 #:use-module (ice-9 match)
34 #:use-module (ice-9 format)
35 #:use-module (srfi srfi-1)
36 #:export (getmail-retriever-configuration
37 getmail-retriever-configuration-extra-parameters
38 getmail-destination-configuration
39 getmail-options-configuration
40 getmail-configuration-file
42 getmail-service-type))
46 ;;; Service for the getmail mail retriever.
50 (define (uglify-field-name field-name)
51 (let ((str (symbol->string field-name)))
52 (string-join (string-split (if (string-suffix? "?" str)
53 (substring str 0 (1- (string-length str)))
58 (define (serialize-field field-name val)
60 (format #f "~a = ~a\n"
61 #$(uglify-field-name field-name)
67 (map (lambda (list-val)
68 (format #f "\"~a\", " list-val))
74 (define (serialize-string field-name val)
77 (serialize-field field-name val)))
79 (define (string-or-filelike? val)
82 (define (serialize-string-or-filelike field-name val)
85 (serialize-field field-name val)))
87 (define (serialize-boolean field-name val)
88 (serialize-field field-name (if val "true" "false")))
90 (define (non-negative-integer? val)
91 (and (exact-integer? val) (not (negative? val))))
92 (define (serialize-non-negative-integer field-name val)
93 (serialize-field field-name val))
95 (define serialize-list serialize-field)
97 (define parameter-alist? list?)
98 (define (serialize-parameter-alist field-name val)
100 #$@(map (match-lambda
102 (serialize-field key value)))
105 (define (serialize-getmail-retriever-configuration field-name val)
106 (serialize-configuration val getmail-retriever-configuration-fields))
108 (define-configuration getmail-retriever-configuration
110 (string "SimpleIMAPSSLRetriever")
111 "The type of mail retriever to use. Valid values include
112 @samp{passwd} and @samp{static}.")
115 "Space separated list of arguments to the userdb driver.")
118 "Space separated list of arguments to the userdb driver.")
120 (non-negative-integer #f)
121 "Space separated list of arguments to the userdb driver.")
124 "Override fields from passwd.")
127 "Override fields from passwd.")
130 "PEM-formatted key file to use for the TLS negotiation")
133 "PEM-formatted certificate file to use for the TLS negotiation")
136 "CA certificates to use")
138 (parameter-alist '())
139 "Extra retriever parameters"))
141 (define (serialize-getmail-destination-configuration field-name val)
142 (serialize-configuration val getmail-destination-configuration-fields))
144 (define-configuration getmail-destination-configuration
147 "The type of mail destination. Valid values include @samp{Maildir},
148 @samp{Mboxrd} and @samp{MDA_external}.")
150 (string-or-filelike "")
151 "The path option for the mail destination. The behaviour depends on the
154 (parameter-alist '())
155 "Extra destination parameters"))
157 (define (serialize-getmail-options-configuration field-name val)
158 (serialize-configuration val getmail-options-configuration-fields))
160 (define-configuration getmail-options-configuration
162 (non-negative-integer 1)
163 "If set to @samp{0}, getmail will only print warnings and errors. A value
164 of @samp{1} means that messages will be printed about retrieving and deleting
165 messages. If set to @samp{2}, getmail will print messages about each of it's
169 "If true, getmail will retrieve all available messages. Otherwise it will
170 only retrieve messages it hasn't seen previously.")
173 "If set to true, messages will be deleted from the server after retrieving
174 and successfully delivering them. Otherwise, messages will be left on the
177 (non-negative-integer 0)
178 "Getmail will delete messages this number of days after seeing them, if
179 they have not been delivered. This means messages will be left on the server
180 this number of days after delivering them. A value of @samp{0} disabled this
183 (non-negative-integer 0)
184 "Delete messages larger than this of bytes after retrieving them, even if
185 the delete and delete-after options are disabled. A value of @samp{0}
186 disables this feature.")
187 (max-bytes-per-session
188 (non-negative-integer 0)
189 "Retrieve messages totalling up to this number of bytes before closing the
190 session with the server. A value of @samp{0} disables this feature.")
192 (non-negative-integer 0)
193 "Don't retrieve messages larger than this number of bytes. A value of
194 @samp{0} disables this feature.")
197 "If true, getmail will add a Delivered-To header to messages.")
200 "If set, getmail adds a Received header to the messages.")
203 "Getmail will record a log of its actions to the named file. A value of
204 @samp{\"\"} disables this feature.")
207 "If true, getmail will record a log of its actions using the system
211 "If true, getmail will log information about messages not retrieved and the
212 reason for not retrieving them, as well as starting and ending information
215 (parameter-alist '())
216 "Extra options to include."))
218 (define (serialize-getmail-configuration-file field-name val)
220 (($ <getmail-configuration-file> location
221 retriever destination options)
224 #$(serialize-getmail-retriever-configuration #f retriever)
226 #$(serialize-getmail-destination-configuration #f destination)
228 #$(serialize-getmail-options-configuration #f options)))))
230 (define-configuration getmail-configuration-file
232 (getmail-retriever-configuration (getmail-retriever-configuration))
233 "What mail account to retrieve mail from, and how to access that account.")
235 (getmail-destination-configuration (getmail-destination-configuration))
236 "What to do with retrieved messages.")
238 (getmail-options-configuration (getmail-options-configuration))
239 "Configure getmail."))
241 (define (serialize-symbol field-name val) "")
242 (define (serialize-getmail-configuration field-name val) "")
244 (define-configuration getmail-configuration
247 "A symbol to identify the getmail service.")
250 "The getmail package to use.")
253 "The user to run getmail as.")
256 "The group to run getmail as.")
258 (string "/var/lib/getmail/default")
259 "The getmail directory to use.")
261 (getmail-configuration-file (getmail-configuration-file))
262 "The getmail configuration file to use.")
265 "A list of mailboxes that getmail should wait on the server for new mail
266 notifications. This depends on the server supporting the IDLE extension.")
267 (environment-variables
269 "Environment variables to set for getmail."))
271 (define (generate-getmail-documentation)
272 (generate-documentation
273 `((getmail-configuration
274 ,getmail-configuration-fields
275 (rcfile getmail-configuration-file))
276 (getmail-configuration-file
277 ,getmail-configuration-file-fields
278 (retriever getmail-retriever-configuration)
279 (destination getmail-destination-configuration)
280 (options getmail-options-configuration))
281 (getmail-retriever-configuration ,getmail-retriever-configuration-fields)
282 (getmail-destination-configuration ,getmail-destination-configuration-fields)
283 (getmail-options-configuration ,getmail-options-configuration-fields))
284 'getmail-configuration))
286 (define-gexp-compiler (getmail-configuration-file-compiler
287 (rcfile <getmail-configuration-file>) system target)
290 #~(call-with-output-file #$output
292 (display #$(serialize-getmail-configuration-file #f rcfile)
297 (define (getmail-accounts configs)
298 (let ((users (delete-duplicates
299 (map getmail-configuration-user
301 (groups (delete-duplicates
302 (map getmail-configuration-group
313 (group (getmail-configuration-group
314 (find (lambda (config)
316 (string=? user (getmail-configuration-user config))
317 (getmail-configuration-group config)))
320 (comment "Getmail user")
321 (home-directory "/var/empty")
322 (shell (file-append shadow "/sbin/nologin"))))
325 (define (getmail-activation configs)
326 "Return the activation GEXP for CONFIGS."
327 (with-imported-modules '((guix build utils))
329 (use-modules (guix build utils))
332 #~(let* ((pw (getpw #$(getmail-configuration-user config)))
333 (uid (passwd:uid pw))
334 (gid (passwd:gid pw))
335 (getmaildir #$(getmail-configuration-directory config)))
337 (chown getmaildir uid gid)))
340 (define (getmail-shepherd-services configs)
341 "Return a list of <shepherd-service> for CONFIGS."
343 (($ <getmail-configuration> location name package
344 user group directory rcfile idle
345 environment-variables)
347 (documentation "Run getmail.")
348 (provision (list (symbol-append 'getmail- name)))
349 (requirement '(networking))
350 (start #~(make-forkexec-constructor
351 `(#$(file-append package "/bin/getmail")
352 ,(string-append "--getmaildir=" #$directory)
353 #$@(map (lambda (idle)
354 (string-append "--idle=" idle))
356 ,(string-append "--rcfile=" #$rcfile))
359 #:environment-variables
360 (list #$@environment-variables)
362 #$(string-append "/var/log/getmail-"
363 (symbol->string name)))))))
366 (define getmail-service-type
370 (list (service-extension shepherd-root-service-type
371 getmail-shepherd-services)
372 (service-extension activation-service-type
374 (service-extension account-service-type
377 "Run @command{getmail}, a mail retriever program.")
379 (compose concatenate)