mail: Refactor imapfilter configuration

Make imapfilter configuraiton more flexible to prepare for different
filters being added.

The program will expect its configuration and filter files to be located
in XDG directory structure, within the sub-folder 'filters'.
E.g.: `/home/user/.config/imapfilter/filters`

A global `configDir` variable is available for any filters, which
describes the path to the current configuration directory.
The path to the filter directory can also be accessed through
`getFilterDir()`.

The configuration runs `accounts.lua` and expects it to return a table
with the individual imap boxes as top level entries, so that filters can
then loop through each account as needed.

Lastly, the config will automatically apply all filters found in the
filters sub-folder. A filter is a '.lua' file within the directory.
Other files can exist in the dir and will not be touched.
This commit is contained in:
Marty Oehme 2020-09-18 16:53:10 +02:00
parent 0d00953f30
commit 698e8a63ee
Signed by: Marty
GPG key ID: B7538B8F50A1C800

View file

@ -12,7 +12,20 @@ options.subscribe = true
-- How long to wait for servers response.
options.timeout = 120
accounts = loadfile(os.getenv("HOME") .. "/.config/imapfilter/accounts.lua")()
-- will set filters to be grabbed from XDG-compliant filter directory
-- can be overridden with env var IMAPFILTER_FILTERDIR
function getConfigDir()
-- -- set directory for imapfilter files
local configdir
if os.getenv("IMAPFILTER_CONFIGDIR") then
configdir=os.getenv("IMAPFILTER_CONFIGDIR")
elseif os.getenv("XDG_CONFIG_HOME") then
configdir=os.getenv("XDG_CONFIG_HOME") .. "/imapfilter"
else
configdir=os.getenv("HOME") .. "/.config/imapfilter"
end
return configdir
end
-- will set filters to be grabbed from XDG-compliant filter directory
-- can be overridden with env var IMAPFILTER_FILTERDIR
@ -21,21 +34,26 @@ function getFilterDir()
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"
imapfilterdir=configDir .. "/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.
local p = io.popen('find "'..dir..'" -type f -name "*.lua"') --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
-- create global variable containing the configuration files
configDir=getConfigDir()
assert(configDir, "No configuration directory found. Ensure " .. os.getenv("HOME") .. "/.config/imapfilter exists.")
-- create global variable containing account access
accounts = loadfile(configDir .. "/accounts.lua")()
assert(accounts, "No accounts configured. Ensure accounts.lua exists and returns a table of accounts.")
applyFilters(getFilterDir())