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.
This commit is contained in:
Marty Oehme 2020-09-16 18:49:08 +02:00
parent 674b1cfeac
commit 3bbb7d0360
Signed by: Marty
GPG key ID: B7538B8F50A1C800
7 changed files with 112 additions and 12 deletions

View file

@ -4,7 +4,7 @@
gT = :prev-tab<Enter> gT = :prev-tab<Enter>
gt = :next-tab<Enter> gt = :next-tab<Enter>
<C-t> = :term<Enter> <C-t> = :term<Enter>
gR = :exec updatemail gR = :exec checkmail<Enter>
[messages] [messages]
q = :quit<Enter> q = :quit<Enter>
@ -28,7 +28,7 @@ J = :next-folder<Enter>
K = :prev-folder<Enter> K = :prev-folder<Enter>
<C-p> = :prev-folder<Enter> <C-p> = :prev-folder<Enter>
gi = :cf Inbox<Enter> gi = :cf Inbox<Enter>
gm = :cf Important<Enter> g' = :cf Important<Enter>
ga = :cf Archive<Enter> ga = :cf Archive<Enter>
gA = :cf All<Enter> gA = :cf All<Enter>
gs = :cf Sent<Enter> gs = :cf Sent<Enter>
@ -40,6 +40,7 @@ V = :mark -v<Enter>
<Enter> = :view<Enter> <Enter> = :view<Enter>
l = :view<Enter> l = :view<Enter>
D = :prompt 'Really delete this message?' 'delete-message'<Enter> D = :prompt 'Really delete this message?' 'delete-message'<Enter>
i = :modify-labels +inbox -archived -deleted<Enter>
d = :modify-labels +deleted -inbox -archived<Enter> d = :modify-labels +deleted -inbox -archived<Enter>
a = :modify-labels +archived -deleted -inbox<Enter> a = :modify-labels +archived -deleted -inbox<Enter>
A = :modify-labels +archived -deleted -inbox<Enter> A = :modify-labels +archived -deleted -inbox<Enter>

View file

@ -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",
}

View file

@ -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")

View file

@ -10,27 +10,26 @@ PassCmd "pass show misc/aerc-gmail-app-password | head -n1"
# #
# Use SSL # Use SSL
SSLType IMAPS 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 /etc/ssl/certs/ca-certificates.crt
#CertificateFile ~/.cert/imap.gmail.com.pem # Throttle simultaneous access to make google happy
#CertificateFile ~/.cert/Equifax_Secure_CA.pem PipelineDepth 60
PipelineDepth 50
IMAPStore gmail-remote IMAPStore gmail-remote
Account gmail Account gmail
MaildirStore gmail-local MaildirStore mail-local
Subfolders Verbatim Subfolders Verbatim
# The trailing "/" is important # The trailing "/" is important
Path ~/documents/mail/ Path ~/documents/mail/
Inbox ~/documents/mail/Inbox Inbox ~/documents/mail/Inbox
# define generous maximum size to store locally
MaxSize 50M
Channel gmail Channel gmail-general
Far :gmail-remote: Far :gmail-remote:
Near :gmail-local: Near :mail-local:
# Exclude everything under the internal [Gmail] folder, except the interesting folders # Exclude everything under the internal [Gmail] folder, except the interesting folders
Patterns * ![Gmail]* "[Gmail]/Sent Mail" "[Gmail]/Starred" "[Gmail]/All Mail" Patterns * ![Gmail]* "[Gmail]/Sent Mail" "[Gmail]/Starred" "[Gmail]/All Mail" "[Gmail]/Trash"
# Or include everything
#Patterns * #Patterns *
# Automatically create missing mailboxes, both locally and on the server # Automatically create missing mailboxes, both locally and on the server
Create Both Create Both

View file

@ -0,0 +1,3 @@
#!/bin/sh
alias imapfilter='imapfilter -c \"${XDG_CONFIG_HOME:-$HOME/.config}/imapfilter/config.lua\"'

View file

@ -1,3 +1,3 @@
#!/bin/sh #!/bin/sh
alias mbsync='mbsync -c $XDG_CONFIG_HOME/isync/mbsyncrc' alias mbsync='mbsync -c ${XDG_CONFIG_HOME:-$HOME/.config}/isync/mbsyncrc'

57
mail/.local/bin/checkmail Executable file
View file

@ -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