From 3bbb7d0360537903fd276112a4a6020e6ef9ac47 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Wed, 16 Sep 2020 18:49:08 +0200 Subject: [PATCH] mail: Add checkmail routine Added script to call for checking mail. Will run individual pre- and post-hooks. (By default, imapfilter and notmuch update.) Simple options can be changed through environment variables: Hooks can be set to different scripts with MBSYNC_PRE and MBSYNC_POST, respectively. Will also run mbsync repeatedly, until it exits without error. By default it will try running it 3 times before giving up, this can be set with MBSYNC_MAX_TRIES. No functional imapfilters have been added yet. mbsync and imapfilter both adhere to the XDG base directory layout (somewhat), checking for their option files in .config/{isync,imapfilter} respectively. Everything still pending changes, the mail eco-system is hard to wrap one's head around. --- mail/.config/aerc/binds.conf | 5 +- mail/.config/imapfilter/adverts-to-dump.lua | 22 ++++++++ mail/.config/imapfilter/config.lua | 18 +++++++ mail/.config/isync/mbsyncrc | 17 +++--- mail/.config/sh/alias.d/imapfilter-xdg.sh | 3 ++ mail/.config/sh/alias.d/mbsync-xdg.sh | 2 +- mail/.local/bin/checkmail | 57 +++++++++++++++++++++ 7 files changed, 112 insertions(+), 12 deletions(-) create mode 100644 mail/.config/imapfilter/adverts-to-dump.lua create mode 100644 mail/.config/imapfilter/config.lua create mode 100644 mail/.config/sh/alias.d/imapfilter-xdg.sh create mode 100755 mail/.local/bin/checkmail diff --git a/mail/.config/aerc/binds.conf b/mail/.config/aerc/binds.conf index e333b89..efe2b7a 100644 --- a/mail/.config/aerc/binds.conf +++ b/mail/.config/aerc/binds.conf @@ -4,7 +4,7 @@ gT = :prev-tab gt = :next-tab = :term -gR = :exec updatemail +gR = :exec checkmail [messages] q = :quit @@ -28,7 +28,7 @@ J = :next-folder K = :prev-folder = :prev-folder gi = :cf Inbox -gm = :cf Important +g' = :cf Important ga = :cf Archive gA = :cf All gs = :cf Sent @@ -40,6 +40,7 @@ V = :mark -v = :view l = :view D = :prompt 'Really delete this message?' 'delete-message' +i = :modify-labels +inbox -archived -deleted d = :modify-labels +deleted -inbox -archived a = :modify-labels +archived -deleted -inbox A = :modify-labels +archived -deleted -inbox diff --git a/mail/.config/imapfilter/adverts-to-dump.lua b/mail/.config/imapfilter/adverts-to-dump.lua new file mode 100644 index 0000000..ac5cc0b --- /dev/null +++ b/mail/.config/imapfilter/adverts-to-dump.lua @@ -0,0 +1,22 @@ +-- -- Gets password from pass +status, password = pipe_from('pass show work/email') +-- Setup an imap account called work +work = IMAP { + server = "localhost", + port = 1143, + username = "USERNAME", + password = password + -- ssl = auto +} + +-- This function takes a table of email addresses and flags messages from them in the inbox. +function flagSenders(senders) + for _, v in pairs(senders) do + messages = work["Inbox"]:contain_from(v) + messages:mark_flagged() + end +end + +flagSenders { + "specials@isthereanydeal.com", +} diff --git a/mail/.config/imapfilter/config.lua b/mail/.config/imapfilter/config.lua new file mode 100644 index 0000000..e0a1d76 --- /dev/null +++ b/mail/.config/imapfilter/config.lua @@ -0,0 +1,18 @@ +-- According to the IMAP specification, when trying to write a message +-- to a non-existent mailbox, the server must send a hint to the client, +-- whether it should create the mailbox and try again or not. However +-- some IMAP servers don't follow the specification and don't send the +-- correct response code to the client. By enabling this option the +-- client tries to create the mailbox, despite of the server's response. +-- This variable takes a boolean as a value. Default is “false”. +options.create = true +-- By enabling this option new mailboxes that were automatically created, +-- get auto subscribed +options.subscribe = true +-- How long to wait for servers response. +options.timeout = 120 + +-- set directory for imapfilter files +imapfilterdir= os.getenv("HOME") .. '/.config/imapfilter/' + +loadfile(imapfilterdir .. "adverts-to-dump.lua") diff --git a/mail/.config/isync/mbsyncrc b/mail/.config/isync/mbsyncrc index 6320c0d..75ed2a6 100644 --- a/mail/.config/isync/mbsyncrc +++ b/mail/.config/isync/mbsyncrc @@ -10,27 +10,26 @@ PassCmd "pass show misc/aerc-gmail-app-password | head -n1" # # Use SSL SSLType IMAPS -# The following line should work. If get certificate errors, uncomment the two following lines and read the "Troubleshooting" section. CertificateFile /etc/ssl/certs/ca-certificates.crt -#CertificateFile ~/.cert/imap.gmail.com.pem -#CertificateFile ~/.cert/Equifax_Secure_CA.pem -PipelineDepth 50 +# Throttle simultaneous access to make google happy +PipelineDepth 60 IMAPStore gmail-remote Account gmail -MaildirStore gmail-local +MaildirStore mail-local Subfolders Verbatim # The trailing "/" is important Path ~/documents/mail/ Inbox ~/documents/mail/Inbox +# define generous maximum size to store locally +MaxSize 50M -Channel gmail +Channel gmail-general Far :gmail-remote: -Near :gmail-local: +Near :mail-local: # Exclude everything under the internal [Gmail] folder, except the interesting folders -Patterns * ![Gmail]* "[Gmail]/Sent Mail" "[Gmail]/Starred" "[Gmail]/All Mail" -# Or include everything +Patterns * ![Gmail]* "[Gmail]/Sent Mail" "[Gmail]/Starred" "[Gmail]/All Mail" "[Gmail]/Trash" #Patterns * # Automatically create missing mailboxes, both locally and on the server Create Both diff --git a/mail/.config/sh/alias.d/imapfilter-xdg.sh b/mail/.config/sh/alias.d/imapfilter-xdg.sh new file mode 100644 index 0000000..fdf5bb2 --- /dev/null +++ b/mail/.config/sh/alias.d/imapfilter-xdg.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +alias imapfilter='imapfilter -c \"${XDG_CONFIG_HOME:-$HOME/.config}/imapfilter/config.lua\"' diff --git a/mail/.config/sh/alias.d/mbsync-xdg.sh b/mail/.config/sh/alias.d/mbsync-xdg.sh index 80a590d..e621d51 100644 --- a/mail/.config/sh/alias.d/mbsync-xdg.sh +++ b/mail/.config/sh/alias.d/mbsync-xdg.sh @@ -1,3 +1,3 @@ #!/bin/sh -alias mbsync='mbsync -c $XDG_CONFIG_HOME/isync/mbsyncrc' +alias mbsync='mbsync -c ${XDG_CONFIG_HOME:-$HOME/.config}/isync/mbsyncrc' diff --git a/mail/.local/bin/checkmail b/mail/.local/bin/checkmail new file mode 100755 index 0000000..fa28d88 --- /dev/null +++ b/mail/.local/bin/checkmail @@ -0,0 +1,57 @@ +#!/usr/bin/env sh +# runs mbsync, with pre-hooks and post-hooks +# by default, first runs imapfilter +# and after indexes with notmuch +# +# To run without invoking any hooks or retry attempts, +# run `checkmail raw` +# this will simply invoke a single isync run on all +# boxes with the correct config file set. +# +# for more advanced, per-account/channel hooks, see +# https://sourceforge.net/p/isync/feature-requests/8/ + +MBSYNC_MAX_TRIES=3 +MBSYNC_PRE="imapfilter" +MBSYNC_POST="notmuch new" + +success="false" +tries=0 + +# fail the routine and optionally send a message why +fail() { + [ -n "$1" ] && echo "$1" + exit 1 +} + +checkmail() { + mbsync -c "$XDG_CONFIG_HOME/isync/mbsyncrc" -a +} + +checkonline() { + # Ping 1.1.1.1 to confirm that we are on the internet + ping -c 1 "1.1.1.1" >/dev/null 2>/dev/null || fail "checkmail can not access the internet." +} + +# Routine start + +# check + +# skip any retries and pre/post hooks, just run mbsync +if [ "$1" = "raw" ]; then + checkmail + exit +fi + +$MBSYNC_PRE +while true; do + if checkmail; then + success="true" + fi + + if [ $success = "true" ] || [ $tries -gt $MBSYNC_MAX_TRIES ]; then + fail "Maximum retries reached unsuccessfully." + fi + tries=$((tries + 1)) +done +$MBSYNC_POST