mail: Add basic filtering system

imapfilter now looks for account information in an accounts.lua file in
its configuration directory -- the file should return account
information as a table with each account being a top-level entry in the
table.

A sample spam-moving script has been supplied:
For now, it will take a list of sender addresses and put everything in
the list from the inbox into the dump directory.

An example list of senders has been supplied for testing purposes, but
this should eventually be read from a file containing an extensible list
of senders (and perhaps even different properties such as subject, etc.)

The imapfilter xdg alias has been fixed.

Coherence between the imap folder structure and notmuch tagging system
still has to be achieved -- either by aerc using the folder structure as
a query map, or notmuch tags automatically being applied for specific
imap filtering options.
This commit is contained in:
Marty Oehme 2020-09-17 19:49:53 +02:00
parent 1564aa8176
commit 5a256186c2
Signed by: Marty
GPG key ID: B7538B8F50A1C800
8 changed files with 102 additions and 42 deletions

View file

@ -29,6 +29,7 @@ K = :prev-folder<Enter>
<C-p> = :prev-folder<Enter> <C-p> = :prev-folder<Enter>
gi = :cf Inbox<Enter> gi = :cf Inbox<Enter>
g' = :cf Important<Enter> g' = :cf Important<Enter>
' = :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>
@ -41,6 +42,7 @@ V = :mark -v<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> i = :modify-labels +inbox -archived -deleted<Enter>
m = :modify-labels +flagged<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

@ -4,6 +4,6 @@ Archive=tag:archived and not tag:inbox and not tag:dump
Dump=tag:dump and not tag:inbox Dump=tag:dump and not tag:inbox
Trash=tag:deleted Trash=tag:deleted
Important=tag:flagged Important=tag:flagged
Sent=from:marty.oehme@gmail.com Sent=from:marty.oehme@gmail.com or from:marty.oehme@googlemail.com or from:moehme@ruc.dk or from:mo82rimu@studserv.uni-leipzig.de
Replied=tag:replied Replied=tag:replied
Patches=subject:/^\[PATCH/ Patches=subject:/^\[PATCH/

View file

@ -0,0 +1,13 @@
local accounts = {}
local status, gmailuser = pipe_from('pass show misc/aerc-gmail-app-password | grep username | cut -d: -f2')
local status, gmailpass = pipe_from('pass show misc/aerc-gmail-app-password | head -n1')
-- Setup an imap account called gmail
accounts.gmail = IMAP {
server = "imap.gmail.com",
username = gmailuser,
password = gmailpass,
ssl = "auto"
}
return accounts

View file

@ -1,22 +0,0 @@
-- -- 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

@ -12,7 +12,30 @@ options.subscribe = true
-- How long to wait for servers response. -- How long to wait for servers response.
options.timeout = 120 options.timeout = 120
-- set directory for imapfilter files accounts = loadfile(os.getenv("HOME") .. "/.config/imapfilter/accounts.lua")()
imapfilterdir= os.getenv("HOME") .. '/.config/imapfilter/'
loadfile(imapfilterdir .. "adverts-to-dump.lua") -- will set filters to be grabbed from XDG-compliant filter directory
-- can be overridden with env var IMAPFILTER_FILTERDIR
function getFilterDir()
-- -- set directory for imapfilter files
local imapfilterdir
if os.getenv("IMAPFILTER_FILTERDIR") then
imapfilterdir=os.getenv("IMAPFILTER_FILTERDIR")
elseif os.getenv("XDG_CONFIG_HOME") then
imapfilterdir=os.getenv("XDG_CONFIG_HOME") .. "/imapfilter/filters"
else
imapfilterdir=os.getenv("HOME") .. "/.config/imapfilter/filters"
end
return imapfilterdir
end
local filters = {}
-- dirlist, from https://stackoverflow.com/a/25266573
function applyFilters(dir)
local p = io.popen('find "'..dir..'" -type f') --Open directory look for files, save data in p. By giving '-type f' as parameter, it returns all files.
for file in p:lines() do --Loop through all files
loadfile(file)()
end
end
applyFilters(getFilterDir())

View file

@ -0,0 +1,39 @@
function sendtoRollup(acc, senders)
for _, sender in pairs(senders) do
messages = acc["Inbox"]:contain_from(sender)
messages:mark_seen()
messages:move_messages(acc["Dump"])
-- for _, msg in ipairs(messages) do
-- mailbox, uid = table.unpack(msg)
-- local from = mailbox[uid]:fetch_field("From")
-- local subject = mailbox[uid]:fetch_field("Subject")
-- print(from .. "; " .. subject)
-- end
end
end
for _, acc in pairs(accounts) do
sendtoRollup ( acc, {
"news@todoist.com",
"server@email.woommart.com",
"noreply@medium.com",
"rollup@unroll.me",
"team@readdlenews.com",
"info@netdata.cloud",
"taco@trello.com",
"quincy@freecodecamp.org",
"support@instapaper.com",
"update@author.email.elsevier.com",
"newsletter@cloudflare.com",
"noreply@notify.docker.com",
"hello@skillshare.com",
"noreply@hostelworld.com",
"waitlist@isthereanydeal.com",
"info@mynameisgriz.com",
"news@postman.com",
})
end

View file

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

View file

@ -12,12 +12,9 @@
# https://sourceforge.net/p/isync/feature-requests/8/ # https://sourceforge.net/p/isync/feature-requests/8/
MBSYNC_MAX_TRIES=3 MBSYNC_MAX_TRIES=3
MBSYNC_PRE="imapfilter" MBSYNC_PRE="imapfilter -c /home/marty/.config/imapfilter/config.lua"
MBSYNC_POST="notmuch new" MBSYNC_POST="notmuch new"
success="false"
tries=0
# fail the routine and optionally send a message why # fail the routine and optionally send a message why
fail() { fail() {
[ -n "$1" ] && echo "$1" [ -n "$1" ] && echo "$1"
@ -43,15 +40,23 @@ if [ "$1" = "raw" ]; then
exit exit
fi fi
main() {
$MBSYNC_PRE $MBSYNC_PRE
tries=0
while true; do while true; do
if checkmail; then if checkmail; then
success="true" break
fi fi
if [ $success = "true" ] || [ $tries -gt $MBSYNC_MAX_TRIES ]; then
fail "Maximum retries reached unsuccessfully."
fi
tries=$((tries + 1)) tries=$((tries + 1))
if [ $tries -gt $MBSYNC_MAX_TRIES ]; then
fail "maximum retries reached without success."
fi
done done
unset tries
$MBSYNC_POST $MBSYNC_POST
}
main