From 698e8a63ee3cba2aa2a14e00eb4063c1757d11ba Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Fri, 18 Sep 2020 16:53:10 +0200 Subject: [PATCH] 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. --- mail/.config/imapfilter/config.lua | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/mail/.config/imapfilter/config.lua b/mail/.config/imapfilter/config.lua index aa6df60..a09e6ac 100644 --- a/mail/.config/imapfilter/config.lua +++ b/mail/.config/imapfilter/config.lua @@ -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())