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()
local fastmail = IMAP {
server = 'imap.fastmail.com',
username = 'maitysanchayan@fastmail.com',
password = get_pwd_fastmail(),
ssl = 'tls1',
}
options.timeout = 120
options.create = false
options.subscribe = false
options.expunge = true
fastmail.INBOX:check_status()
mails = fastmail.INBOX:select_all()
move_mailing_lists(fastmail, mails)
mails = fastmail.Sent:select_all()
move_mailing_lists(fastmail, mails)
mails = fastmail['Trash']:select_all()
delete_from_trash(fastmail, mails)
mails = fastmail['Spam']:select_all()
move_mailing_lists(fastmail, mails)
local sanchayan = IMAP {
server = 'imap.gmail.com',
username = 'maitysanchayan@gmail.com',
password = get_pwd_sanchayan(),
ssl = 'tls1',
}
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)
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_mailing_lists(account, mails)
move_if_to_contains(account, mails, "gstreamer-devel@lists.freedesktop.org", "gstreamer")
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)
function move_if_contains(account, mails, contain, mailbox)
local filtered = mails:contain_cc(contain)
filtered:move_messages(account[mailbox]);
end
function move_if_to_contains(account, mails, to, mailbox)
filtered = mails:contain_to(to)
filtered = mails:contain_from(contain)
filtered:move_messages(account[mailbox]);
end
function move_if_from_contains(account, mails, from, mailbox)
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 = mails:contain_to(contain)
filtered:move_messages(account[mailbox]);
end
function move_to_gmail_trash(account, mails, mailbox)
filtered = mails:select_all()
local filtered = mails:select_all()
filtered:move_messages(account[mailbox])
end
function delete_mail_from(account, mails, from)
filtered = mails:contain_from(from)
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()
function delete_trash(mails)
local filtered = mails:select_all()
filtered:delete_messages()
end
-- Utility function to get IMAP password from file
function get_imap_password(file)
local home = os.getenv("HOME")
local file = home .. "/" .. file
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")
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
function get_pwd_sanchayan()
local cmd = "pass show apppass/sanchayan"
local fd = io.popen(cmd, 'r')
pass = fd:read("*a")
fd:close()
return pass;
-- 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
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