config/cron/job.guile: Don't sync emails when key is not cached.

This avoid unexpected lost of focus in Emacs.
master
Pierre Neidhardt 2019-08-31 11:54:35 +02:00
parent 00ed121116
commit d4b4193f45
1 changed files with 32 additions and 1 deletions

View File

@ -1,8 +1,36 @@
;; -*- mode: scheme; -*-
(use-modules (ice-9 popen))
(use-modules (rnrs io ports))
;; This cannot be let-bound within `job'.
(define currency-file (string-append (getenv "HOME") "/.cache/currency.units"))
(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?)
"Return #t of a key is cached in the GPG agent, #f otherwise."
(let ((keyinfo (gpg-keyinfo)))
(not (null? (filter (lambda (info) (string= (list-ref info 6) "1"))
keyinfo)))))
(job
(lambda (current-time)
(let* ((seconds-in-a-day (* 60 60 24))
@ -21,5 +49,8 @@
(job '(next-hour (range 0 24 3)) "updatedb-local")
(job '(next-minute (range 0 60 15))
'(system* "notmuch" "new") ; Email is sync'ed from a pre-new hook.
(lambda ()
(when (gpg-key-cached?)
;; Email is sync'ed from a pre-new hook.
(system* "notmuch" "new")))
"mail")