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.
This commit is contained in:
Sanchayan Maity 2024-10-28 12:11:53 +05:30
parent 969e039838
commit 11ce66190d
Signed by: sanchayanmaity
GPG key ID: 6F6A0609C12038F3

View file

@ -1,147 +1,123 @@
function main() options.timeout = 120
local fastmail = IMAP { options.create = false
server = 'imap.fastmail.com', options.subscribe = false
username = 'maitysanchayan@fastmail.com', options.expunge = true
password = get_pwd_fastmail(),
ssl = 'tls1',
}
fastmail.INBOX:check_status() function move_mails(account, mails)
mails = fastmail.INBOX:select_all() move_if_contains(account, mails, "linux-bluetooth" , "bluez" )
move_mailing_lists(fastmail, mails) move_if_contains(account, mails, "coffee" , "Coffee" )
mails = fastmail.Sent:select_all() move_if_contains(account, mails, "freedesktop.org" , "FreedesktopGitlab")
move_mailing_lists(fastmail, mails) move_if_contains(account, mails, "gstreamer-devel" , "gstreamer" )
mails = fastmail['Trash']:select_all() move_if_contains(account, mails, "gstreamer-embedded" , "gstreamer" )
delete_from_trash(fastmail, mails) move_if_contains(account, mails, "ietf.org" , "ietf" )
mails = fastmail['Spam']:select_all() move_if_contains(account, mails, "monster.com" , "Jobs" )
move_mailing_lists(fastmail, mails) move_if_contains(account, mails, "naukri.com" , "Jobs" )
move_if_contains(account, mails, "lists.infradead.org" , "Linux Kernel" )
local sanchayan = IMAP { move_if_contains(account, mails, "vger.kernel.org" , "Linux Kernel" )
server = 'imap.gmail.com', move_if_contains(account, mails, "u-boot@lists.denx.de", "Uboot" )
username = 'maitysanchayan@gmail.com', move_if_contains(account, mails, "bseindia.in" , "Zerodha" )
password = get_pwd_sanchayan(), move_if_contains(account, mails, "cdslindia.co.in" , "Zerodha" )
ssl = 'tls1', move_if_contains(account, mails, "zerodha.net" , "Zerodha" )
}
sanchayan.INBOX:check_status()
-- sanchayan['[Gmail]/Trash']:check_status()
-- sanchayan['[Gmail]/Spam']:check_status()
mails = sanchayan.INBOX:select_all()
move_mailing_lists(sanchayan, mails)
mails = sanchayan.Sent:select_all()
move_mailing_lists(sanchayan, mails)
mails = sanchayan['[Gmail]/Trash']:select_all()
imap_trash = sanchayan['Trash']:select_all()
move_to_gmail_trash(sanchayan, imap_trash, '[Gmail]/Trash')
delete_from_trash(sanchayan, mails)
mails = sanchayan['[Gmail]/Spam']:select_all()
move_mailing_lists(sanchayan, mails)
end end
function move_mailing_lists(account, mails) function move_if_contains(account, mails, contain, mailbox)
move_if_to_contains(account, mails, "gstreamer-devel@lists.freedesktop.org", "gstreamer") local filtered = mails:contain_cc(contain)
move_if_from_contains(account, mails, "gstreamer-devel@lists.freedesktop.org", "gstreamer")
move_if_cc_contains(account, mails, "gstreamer-devel@lists.freedesktop.org", "gstreamer")
move_if_to_contains(account, mails, "gstreamer-embedded@lists.freedesktop.org", "gstreamer")
move_if_from_contains(account, mails, "gstreamer-embedded@lists.freedesktop.org", "gstreamer")
move_if_cc_contains(account, mails, "gstreamer-embedded@lists.freedesktop.org", "gstreamer")
move_if_to_contains(account, mails, "pulseaudio-discuss@lists.freedesktop.org", "pulseaudio")
move_if_from_contains(account, mails, "pulseaudio-discuss@lists.freedesktop.org", "pulseaudio")
move_if_cc_contains(account, mails, "pulseaudio-discuss@lists.freedesktop.org", "pulseaudio")
-- Bluetooth
move_if_to_contains(account, mails, "linux-bluetooth@vger.kernel.org", "bluez")
move_if_from_contains(account, mails, "linux-bluetooth@vger.kernel.org", "bluez")
move_if_cc_contains(account, mails, "linux-bluetooth@vger.kernel.org", "bluez")
-- Linux Kernel
move_if_to_contains(account, mails, "lists.infradead.org", "Linux Kernel")
move_if_from_contains(account, mails, "lists.infradead.org", "Linux Kernel")
move_if_cc_contains(account, mails, "lists.infradead.org", "Linux Kernel")
move_if_to_contains(account, mails, "vger.kernel.org", "Linux Kernel")
move_if_from_contains(account, mails, "vger.kernel.org", "Linux Kernel")
move_if_cc_contains(account, mails, "vger.kernel.org", "Linux Kernel")
-- U boot
move_if_to_contains(account, mails, "u-boot@lists.denx.de", "Uboot")
move_if_from_contains(account, mails, "u-boot@lists.denx.de", "Uboot")
move_if_cc_contains(account, mails, "u-boot@lists.denx.de", "Uboot")
-- Jobs
move_if_from_contains(account, mails, "monster.com","Jobs")
move_if_from_contains(account, mails, "naukri.com","Jobs")
move_if_to_contains(account, mails, "gitlab@gitlab.freedesktop.org", "FreedesktopGitlab")
move_if_from_contains(account, mails, "gitlab@gitlab.freedesktop.org", "FreedesktopGitlab")
-- IETF
move_if_to_contains(account, mails, "ietf.org", "ietf")
move_if_from_contains(account, mails, "ietf.org", "ietf")
move_if_cc_contains(account, mails, "ietf.org", "ietf")
end
function move_if_subject_contains(account, mails, subject, mailbox)
filtered = mails:contain_subject(subject)
filtered:move_messages(account[mailbox]); filtered:move_messages(account[mailbox]);
end
function move_if_to_contains(account, mails, to, mailbox) filtered = mails:contain_from(contain)
filtered = mails:contain_to(to)
filtered:move_messages(account[mailbox]); filtered:move_messages(account[mailbox]);
end
function move_if_from_contains(account, mails, from, mailbox) filtered = mails:contain_to(contain)
filtered = mails:contain_from(from)
filtered:move_messages(account[mailbox]);
end
function move_if_cc_contains(account, mails, cc, mailbox)
filtered = mails:contain_cc(cc)
filtered:move_messages(account[mailbox]); filtered:move_messages(account[mailbox]);
end end
function move_to_gmail_trash(account, mails, mailbox) function move_to_gmail_trash(account, mails, mailbox)
filtered = mails:select_all() local filtered = mails:select_all()
filtered:move_messages(account[mailbox]) filtered:move_messages(account[mailbox])
end end
function delete_mail_from(account, mails, from) function delete_trash(mails)
filtered = mails:contain_from(from) local filtered = mails:select_all()
filtered:delete_messages()
end
function delete_mail_if_subject_contains(account, mails, subject)
filtered = mails:contain_subject(subject)
filtered:delete_messages()
end
function delete_from_trash(account, mails)
filtered = mails:select_all()
filtered:delete_messages() filtered:delete_messages()
end end
-- Utility function to get IMAP password from file function get_password(pass_account_name)
function get_imap_password(file) local cmd = "pass show " .. pass_account_name
local home = os.getenv("HOME") local fd = io.popen(cmd, 'r')
local file = home .. "/" .. file local pass = fd:read("*a")
local str = io.open(file):read()
return str;
end
function get_pwd_fastmail()
local cmd = "pass show apppass/fastmail"
local fd = io.popen(cmd, 'r')
pass = fd:read("*a")
fd:close() fd:close()
return pass; return pass;
end end
function get_pwd_sanchayan() -- https://github.com/lefcha/imapfilter/issues/199
local cmd = "pass show apppass/sanchayan" function sanitize_pwd(pwd)
local fd = io.popen(cmd, 'r') pwd = string.gsub(pwd, "\n", "")
pass = fd:read("*a") pwd = string.gsub(pwd, '%\\', '\\\\')
fd:close() pwd = string.gsub(pwd, '%"', '\\"')
return pass; return pwd
end end
main() -- Call the main function 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