dotfiles/imapfilter/.imapfilter/config.lua
Sanchayan Maity 11ce66190d
imapfilter: Add email accounts for our own domains
Switch to Migadu for our own domains. Migadu mandates
bringing your own domain for email and as such we can
now have separate mail boxes for both our .com & .net
domains. We will drop fastmail once our plan expires.

While at it, clean up and improve the filtering logic.
2024-11-05 14:12:02 +05:30

123 lines
4.1 KiB
Lua

options.timeout = 120
options.create = false
options.subscribe = false
options.expunge = true
function move_mails(account, mails)
move_if_contains(account, mails, "linux-bluetooth" , "bluez" )
move_if_contains(account, mails, "coffee" , "Coffee" )
move_if_contains(account, mails, "freedesktop.org" , "FreedesktopGitlab")
move_if_contains(account, mails, "gstreamer-devel" , "gstreamer" )
move_if_contains(account, mails, "gstreamer-embedded" , "gstreamer" )
move_if_contains(account, mails, "ietf.org" , "ietf" )
move_if_contains(account, mails, "monster.com" , "Jobs" )
move_if_contains(account, mails, "naukri.com" , "Jobs" )
move_if_contains(account, mails, "lists.infradead.org" , "Linux Kernel" )
move_if_contains(account, mails, "vger.kernel.org" , "Linux Kernel" )
move_if_contains(account, mails, "u-boot@lists.denx.de", "Uboot" )
move_if_contains(account, mails, "bseindia.in" , "Zerodha" )
move_if_contains(account, mails, "cdslindia.co.in" , "Zerodha" )
move_if_contains(account, mails, "zerodha.net" , "Zerodha" )
end
function move_if_contains(account, mails, contain, mailbox)
local filtered = mails:contain_cc(contain)
filtered:move_messages(account[mailbox]);
filtered = mails:contain_from(contain)
filtered:move_messages(account[mailbox]);
filtered = mails:contain_to(contain)
filtered:move_messages(account[mailbox]);
end
function move_to_gmail_trash(account, mails, mailbox)
local filtered = mails:select_all()
filtered:move_messages(account[mailbox])
end
function delete_trash(mails)
local filtered = mails:select_all()
filtered:delete_messages()
end
function get_password(pass_account_name)
local cmd = "pass show " .. pass_account_name
local fd = io.popen(cmd, 'r')
local pass = fd:read("*a")
fd:close()
return pass;
end
-- https://github.com/lefcha/imapfilter/issues/199
function sanitize_pwd(pwd)
pwd = string.gsub(pwd, "\n", "")
pwd = string.gsub(pwd, '%\\', '\\\\')
pwd = string.gsub(pwd, '%"', '\\"')
return pwd
end
accounts = {}
credentials = {
["sanchayanmaity.net"] = {
server = 'imap.migadu.com',
username = 'me@sanchayanmaity.net',
password = sanitize_pwd(get_password("migadu/sanchayanmaity.net")),
},
["sanchayanmaity.com"] = {
server = 'imap.migadu.com',
username = 'me@sanchayanmaity.com',
password = sanitize_pwd(get_password("migadu/sanchayanmaity.com")),
},
["fastmail"] = {
server = 'imap.fastmail.com',
username = 'maitysanchayan@fastmail.com',
password = get_password("apppass/fastmail"),
},
["gmail-sanchayan"] = {
server = 'imap.gmail.com',
username = 'maitysanchayan@gmail.com',
password = get_password("apppass/sanchayan"),
},
}
function map_credentials_to_account(account_name)
accounts[account_name] = IMAP {
server = credentials[account_name].server,
username = credentials[account_name].username,
password = credentials[account_name].password,
ssl = 'auto',
}
end
function filter_mails(account_name)
local account = accounts[account_name]
account.INBOX:check_status()
local mails = account.INBOX:select_all()
move_mails(account, mails)
account.Sent:check_status()
local mails = account.Sent:select_all()
move_mails(account, mails)
account['Trash']:check_status()
local trash_mails = account['Trash']:select_all()
if string.find(account_name, "gmail") then
move_to_gmail_trash(account, trash_mails, '[Gmail]/Trash')
mails = account['[Gmail]/Trash']:select_all()
delete_trash(mails)
else
delete_trash(trash_mails)
end
end
map_credentials_to_account("sanchayanmaity.net")
map_credentials_to_account("sanchayanmaity.com")
map_credentials_to_account("fastmail")
map_credentials_to_account("gmail-sanchayan")
for account_name, _ in pairs(accounts) do
filter_mails(account_name)
end