Sorting my emails

I started subscribing to a few mailing lists, and since that meant I might actually be using my email for something, I thought it'd be good to figure out a way to filter my emails into different folders so they don't get too gross and cluttered. After looking around I found imapfilter, which turned out to be very easy to use. You can write a lua script that'll scan through your emails and do things to them like deleting them or moving them to different folders. I wrote a few utility functions and made a nice-loooking configuration script with it:

function convert(map)
	local res = {}
	for mbox, contents in pairs(map) do
		if type(mbox) == "string" then
			for index, pattern in ipairs(contents) do
				res[pattern] = mbox
			end
			for k, v in pairs(convert(contents)) do
				res[k] = mbox .. "/" .. v
			end
		end
	end
	return res
end

function filter_by_originator(account, map)
	map = convert(map)
	for origin, mailbox in pairs(map) do
		results = account.INBOX:match_field("from", origin) + 
			account.INBOX:match_field("sender", origin) + 
			account.INBOX:match_field("reply-to", origin)
		results:move_messages(account[mailbox])
	end
end

function do_luna(account)
	filter_by_originator(account, {
		junk = {
			"News@InsideApple.Apple.com",
			"no-reply@email.patreon.com",
			"no-reply@mail.goodreads.com",
			"no-reply@substack.com",
			"no-reply@twitch.tv",
			"noreply@updates.itch.io",
			"team@info.digitalocean.com",
		},
		pr = {
			"NoReply.ODD@dhl.com",
			"account@twitch.tv",
			"appleid@id.apple.com",
			"bingo@patreon.com",
			"donotreply_odd@dhl.com",
			"expiry@letsencrypt.org",
			"no-reply@patreon.com",
			"noreply@bandcamp.com",
			"noreply@github.com",
			"renewals@namecheap.com",
			"support@buyee.jp",
			"support@digitalocean.com",
			"support@itch.io",
			"support@namecheap.com",
			"support@porkbun.com",
		},
		info = {
			"gitlab@gitlab.freedesktop.org",
			"no-reply@disroot.org",
		},
		news = {
			"erininthemorn@substack.com",
		},
		lists = {
			lua           = { ".*@lists.lua.org"            },
			hare_users    = { ".*hare-users@lists.sr.ht"    },
			hare_announce = { ".*hare-announce@lists.sr.ht" },
		}
	})
end

do_luna(IMAP {
	server = "disroot.org",
	username = "lunacb",
	password = ":3",
	port = 993,
	ssl = "auto",
})