From 5a256186c240710e5c94637ed967a6379697862c Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Thu, 17 Sep 2020 19:49:53 +0200 Subject: [PATCH] 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. --- mail/.config/aerc/binds.conf | 2 + mail/.config/aerc/notmuch-querymap | 2 +- mail/.config/imapfilter/accounts.lua | 13 +++++++ mail/.config/imapfilter/adverts-to-dump.lua | 22 ----------- mail/.config/imapfilter/config.lua | 29 ++++++++++++-- .../imapfilter/filters/rollup-dump.lua | 39 +++++++++++++++++++ mail/.config/sh/alias.d/imapfilter-xdg.sh | 2 +- mail/.local/bin/checkmail | 35 ++++++++++------- 8 files changed, 102 insertions(+), 42 deletions(-) create mode 100644 mail/.config/imapfilter/accounts.lua delete mode 100644 mail/.config/imapfilter/adverts-to-dump.lua create mode 100644 mail/.config/imapfilter/filters/rollup-dump.lua diff --git a/mail/.config/aerc/binds.conf b/mail/.config/aerc/binds.conf index efe2b7a..122aba4 100644 --- a/mail/.config/aerc/binds.conf +++ b/mail/.config/aerc/binds.conf @@ -29,6 +29,7 @@ K = :prev-folder = :prev-folder gi = :cf Inbox g' = :cf Important +' = :cf Important ga = :cf Archive gA = :cf All gs = :cf Sent @@ -41,6 +42,7 @@ V = :mark -v l = :view D = :prompt 'Really delete this message?' 'delete-message' i = :modify-labels +inbox -archived -deleted +m = :modify-labels +flagged d = :modify-labels +deleted -inbox -archived a = :modify-labels +archived -deleted -inbox A = :modify-labels +archived -deleted -inbox diff --git a/mail/.config/aerc/notmuch-querymap b/mail/.config/aerc/notmuch-querymap index 79db81e..08f54e9 100644 --- a/mail/.config/aerc/notmuch-querymap +++ b/mail/.config/aerc/notmuch-querymap @@ -4,6 +4,6 @@ Archive=tag:archived and not tag:inbox and not tag:dump Dump=tag:dump and not tag:inbox Trash=tag:deleted 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 Patches=subject:/^\[PATCH/ diff --git a/mail/.config/imapfilter/accounts.lua b/mail/.config/imapfilter/accounts.lua new file mode 100644 index 0000000..8b92663 --- /dev/null +++ b/mail/.config/imapfilter/accounts.lua @@ -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 diff --git a/mail/.config/imapfilter/adverts-to-dump.lua b/mail/.config/imapfilter/adverts-to-dump.lua deleted file mode 100644 index ac5cc0b..0000000 --- a/mail/.config/imapfilter/adverts-to-dump.lua +++ /dev/null @@ -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", -} diff --git a/mail/.config/imapfilter/config.lua b/mail/.config/imapfilter/config.lua index e0a1d76..aa6df60 100644 --- a/mail/.config/imapfilter/config.lua +++ b/mail/.config/imapfilter/config.lua @@ -12,7 +12,30 @@ options.subscribe = true -- How long to wait for servers response. options.timeout = 120 --- set directory for imapfilter files -imapfilterdir= os.getenv("HOME") .. '/.config/imapfilter/' +accounts = loadfile(os.getenv("HOME") .. "/.config/imapfilter/accounts.lua")() -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()) diff --git a/mail/.config/imapfilter/filters/rollup-dump.lua b/mail/.config/imapfilter/filters/rollup-dump.lua new file mode 100644 index 0000000..2fd8290 --- /dev/null +++ b/mail/.config/imapfilter/filters/rollup-dump.lua @@ -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 diff --git a/mail/.config/sh/alias.d/imapfilter-xdg.sh b/mail/.config/sh/alias.d/imapfilter-xdg.sh index fdf5bb2..3499b3d 100644 --- a/mail/.config/sh/alias.d/imapfilter-xdg.sh +++ b/mail/.config/sh/alias.d/imapfilter-xdg.sh @@ -1,3 +1,3 @@ #!/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"' diff --git a/mail/.local/bin/checkmail b/mail/.local/bin/checkmail index fa28d88..27ae526 100755 --- a/mail/.local/bin/checkmail +++ b/mail/.local/bin/checkmail @@ -12,12 +12,9 @@ # https://sourceforge.net/p/isync/feature-requests/8/ MBSYNC_MAX_TRIES=3 -MBSYNC_PRE="imapfilter" +MBSYNC_PRE="imapfilter -c /home/marty/.config/imapfilter/config.lua" MBSYNC_POST="notmuch new" -success="false" -tries=0 - # fail the routine and optionally send a message why fail() { [ -n "$1" ] && echo "$1" @@ -43,15 +40,23 @@ if [ "$1" = "raw" ]; then exit fi -$MBSYNC_PRE -while true; do - if checkmail; then - success="true" - fi +main() { + $MBSYNC_PRE - if [ $success = "true" ] || [ $tries -gt $MBSYNC_MAX_TRIES ]; then - fail "Maximum retries reached unsuccessfully." - fi - tries=$((tries + 1)) -done -$MBSYNC_POST + tries=0 + while true; do + if checkmail; then + break + fi + + tries=$((tries + 1)) + if [ $tries -gt $MBSYNC_MAX_TRIES ]; then + fail "maximum retries reached without success." + fi + done + unset tries + + $MBSYNC_POST +} + +main