all messages for Guix-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Saku Laesvuori <saku@laesvuori.fi>
To: Felix Lechner <felix.lechner@lease-up.com>
Cc: help-guix@gnu.org
Subject: Re: Home service for mbsync?
Date: Thu, 22 Feb 2024 10:03:16 +0200	[thread overview]
Message-ID: <e7qcu6bphgjxmylvvxsegemy32cp5jvt5fjjuljesql42ldzk2@yudfuayrczzx> (raw)
In-Reply-To: <87bk89ee80.fsf@lease-up.com>


[-- Attachment #1.1: Type: text/plain, Size: 273 bytes --]

> Does anyone have an 'mbsync' home service, please? Thanks!

I use a service that combines mbsync and imapnotify. It's not quite
ready yet (the code could be cleaner and it should provide a way to sync
all mailboxes manually) but it's attached if you want to take a look.

[-- Attachment #1.2: mailsync.scm --]
[-- Type: text/plain, Size: 5330 bytes --]

(define-module (mailsync)
  #:use-module (gnu home services shepherd)
  #:use-module (gnu home services)
  #:use-module (gnu packages mail)
  #:use-module (gnu packages admin)
  #:use-module (gnu services configuration)
  #:use-module (guix gexp)
  #:use-module (guix records)
  #:use-module (srfi srfi-1)
  #:export (mailsync-mailbox
            mailsync-mailbox?
            home-mailsync-configuration
            home-mailsync-configuration?
            home-mailsync-service-type))

(define (string-list? x)
  (and (list? x)
       (every string? x)))

(define (string-or-file-like? x)
  (or (string? x)
      (file-like? x)))

(define-maybe/no-serialization string-or-file-like)

(define-configuration/no-serialization mailsync-mailbox
  (host
   (string)
   "The hostname of the IMAP server to synchronise with.")
  (port
   (integer 993)
   "The port to use to connect to the IMAP server.")
  (tls?
   (boolean #t)
   "Whether to connect with IMAPS or not.")
  (password-command
   (string)
   "The command to run to get the IMAP password")
  (user
   (string)
   "The IMAP username.")
  (listen-boxes
   (string-list (list "INBOX"))
   "The mailboxes to listen for changes to.")
  (post-sync
   (maybe-string-or-file-like)
   "A command to run after syncing the mailbox.")
  (id
   (symbol)
   "An unique identifier for this mailbox."))

(define (make-imapnotify-config mailbox syncer-id)
  (match-record mailbox <mailsync-mailbox>
    (host port tls? password-command user listen-boxes post-sync)
    #~(format #f "{\"host\": ~s,
\"port\": ~a,
\"tls\": ~a,
\"username\": ~s,
\"passwordCmd\": ~s,
\"onNewMail\": ~s,
\"onNewMailPost\": ~s,
\"wait\": 3,
\"boxes\": ~a}"
       #$host #$port #$(if tls? "true" "false") #$user #$password-command
       #$(file-append shepherd (string-append "/bin/herd start " (symbol->string syncer-id)))
       #$(if (maybe-value-set? post-sync) post-sync "")
       #$(format #f "[~a]"
               (string-join
                (map (lambda (x)
                       (format #f "~s" x))
                     listen-boxes)
                ", ")))))

(define (make-isync-config mailbox)
  (match-record mailbox <mailsync-mailbox>
    (host port tls? password-command user id)
    (format #f "IMAPAccount account
Host ~a
User ~a
PassCmd ~s
SSLType ~a

IMAPStore remote
Account account

MaildirStore local
SubFolders Verbatim
Path ~~/.mail/~a/
Inbox ~~/.mail/~a/Inbox

Channel channel
Far :remote:
Near :local:
Create Both
Expunge Both
SyncState *
Patterns *\n"
            host user password-command (if tls? "IMAPS" "STARTTLS") id id)))

(define (mailsync-mailbox-list? x)
  (and (list? x)
       (every mailsync-mailbox? x)))

(define-configuration/no-serialization home-mailsync-configuration
  (isync
   (file-like isync)
   "The @code{isync} package to use.")
  (goimapnotify
   (file-like go-gitlab.com-shackra-goimapnotify)
   "The @code{goimapnotify} package to use.")
  (mailboxes
   (mailsync-mailbox-list '())
   "The mailboxes to synchronise."))

(define (home-mailsync-shepherd-service config)
  (match-record config <home-mailsync-configuration>
    (isync goimapnotify mailboxes)
    (apply append
           (map (lambda (mailbox)
                  (let* ((id (mailsync-mailbox-id mailbox))
                         (syncer-id (symbol-append 'mailsync-syncer- id))
                         (listener-id (symbol-append 'mailsync-listener- id))
                         (isync-config-file
                          (mixed-text-file
                           "mbsyncrc"
                           (make-isync-config mailbox)))
                         (imapnotify-config-file
                          (mixed-text-file
                           "imapnotify-config.json"
                           (make-imapnotify-config mailbox syncer-id))))
                    (list
                      (shepherd-service
                       (provision (list syncer-id))
                       (requirement '())
                       (one-shot? #t)
                       (start #~(make-forkexec-constructor
                                 (list
                                  #$(file-append isync "/bin/mbsync")
                                  "--all"
                                  "--quiet"
                                  "--config" #$isync-config-file)))
                       (actions (list (shepherd-configuration-action isync-config-file))))
                      (shepherd-service
                       (provision (list listener-id))
                       (requirement (list syncer-id))
                       (one-shot? #f)
                       (start #~(make-forkexec-constructor
                                 (list
                                  #$(file-append goimapnotify "/bin/goimapnotify")
                                  "-conf" #$imapnotify-config-file)))
                       (stop #~(make-kill-destructor))
                       (actions (list (shepherd-configuration-action imapnotify-config-file)))))))
          mailboxes))))

(define home-mailsync-service-type
  (service-type
   (name 'home-mailsync)
   (extensions
    (list
     (service-extension home-shepherd-service-type home-mailsync-shepherd-service)))
   (default-value (home-mailsync-configuration))
   (description "Sync email over imap with isync and imapnotify.")))

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  parent reply	other threads:[~2024-02-22  8:03 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-21 20:47 Home service for mbsync? Felix Lechner via
2024-02-21 20:59 ` Aleksandr Vityazev
2024-02-22  8:03 ` Saku Laesvuori [this message]
2024-02-22  8:05 ` Efraim Flashner
2024-02-22  9:27 ` Carlo Zancanaro
2024-02-22  9:59   ` Tanguy LE CARROUR
2024-02-22 12:37   ` mcron service and PATH (was: Home service for mbsync?) Emmanuel Beffara
2024-02-22 13:51     ` Tanguy LE CARROUR
2024-02-22 14:05       ` Emmanuel Beffara
2024-02-22 14:17     ` Felix Lechner via
2024-02-22 10:15 ` Home service for mbsync? Miguel Ángel Moreno

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e7qcu6bphgjxmylvvxsegemy32cp5jvt5fjjuljesql42ldzk2@yudfuayrczzx \
    --to=saku@laesvuori.fi \
    --cc=felix.lechner@lease-up.com \
    --cc=help-guix@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/guix.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.