2019-01-21 10:14:34 +01:00
|
|
|
;; -*- mode: scheme; -*-
|
2019-08-31 11:54:35 +02:00
|
|
|
(use-modules (ice-9 popen))
|
|
|
|
(use-modules (rnrs io ports))
|
2018-03-31 17:57:24 +02:00
|
|
|
|
|
|
|
;; This cannot be let-bound within `job'.
|
|
|
|
(define currency-file (string-append (getenv "HOME") "/.cache/currency.units"))
|
|
|
|
|
2019-08-31 11:54:35 +02:00
|
|
|
(define* (system-to-string #:rest args)
|
|
|
|
(let* ((port (apply open-pipe* OPEN_READ args))
|
|
|
|
(str (get-string-all port)))
|
|
|
|
(close-pipe port)
|
|
|
|
str))
|
|
|
|
|
|
|
|
(define (gpg-keyinfo)
|
|
|
|
"Return GPG keyinfo as a list of list of strings.
|
|
|
|
Typical output is:
|
|
|
|
|
|
|
|
S KEYINFO ???????????????????????????????????????? D - - - P - - -
|
|
|
|
S KEYINFO ???????????????????????????????????????? D - - 1 P - - -
|
|
|
|
|
|
|
|
The \"1\" means the key is cached."
|
|
|
|
(filter (lambda (info) (string= (car info) "S"))
|
|
|
|
(map (lambda (s) (string-split s #\space))
|
|
|
|
(string-split
|
|
|
|
(system-to-string "gpg-connect-agent" "keyinfo --list" "/bye")
|
|
|
|
#\newline))))
|
|
|
|
|
|
|
|
(define (gpg-key-cached?)
|
2019-09-10 17:14:46 +02:00
|
|
|
"Return #t if a key is cached in the GPG agent, #f otherwise."
|
2019-08-31 11:54:35 +02:00
|
|
|
(let ((keyinfo (gpg-keyinfo)))
|
|
|
|
(not (null? (filter (lambda (info) (string= (list-ref info 6) "1"))
|
|
|
|
keyinfo)))))
|
|
|
|
|
2018-03-31 17:57:24 +02:00
|
|
|
(job
|
|
|
|
(lambda (current-time)
|
|
|
|
(let* ((seconds-in-a-day (* 60 60 24))
|
2019-01-21 10:14:34 +01:00
|
|
|
(currency-time (if (not (file-exists? currency-file))
|
|
|
|
0
|
|
|
|
(stat:mtime (stat currency-file)))))
|
2018-03-31 17:57:24 +02:00
|
|
|
(if (< currency-time (- current-time seconds-in-a-day))
|
2018-04-24 08:04:01 +02:00
|
|
|
;; Use next-minute to avoid overwhelming the system in case of failure.
|
|
|
|
(next-minute)
|
2018-05-23 16:58:43 +02:00
|
|
|
(next-hour-from (next-day) (list (tm:hour (localtime currency-time)))))))
|
2018-03-31 17:57:24 +02:00
|
|
|
;; A string is nicer than Scheme code for `mcron --schedule' output.
|
2018-04-02 08:12:26 +02:00
|
|
|
;; Otherwise we could return '(system* "units_cur" currency-file)
|
|
|
|
;; and use job's 3rd argument as a description.
|
2018-03-31 17:57:24 +02:00
|
|
|
(string-append "units_cur " currency-file))
|
2018-04-02 08:12:26 +02:00
|
|
|
|
2018-06-24 11:18:55 +02:00
|
|
|
(job '(next-hour (range 0 24 3)) "updatedb-local")
|
2019-03-17 09:27:18 +01:00
|
|
|
|
2020-02-11 07:06:29 +01:00
|
|
|
;; (job '(next-minute (range 0 60 15))
|
|
|
|
;; (lambda ()
|
|
|
|
;; (when (gpg-key-cached?)
|
|
|
|
;; ;; Email is sync'ed from a pre-new hook.
|
|
|
|
;; (system* "notmuch" "new")))
|
|
|
|
;; "mail")
|