mail: Add IDLE mode watching
Added option to imapfilter to continuously run in the background and watch for new incoming mail through the Imap IDLE mode. By default it will run as one-shot and just sort mail according to currently active filters. But if enabling the `CONTINUOUS` mode in config file, it will instead keep running and continuously check for arriving mail to filter. This could, in the future, be useful to run on a separate server which watches remote imap folders and pulls them down to its own storage. For local usage, a systemd service and timer unit are probably more suitable than the daemonization of imapfilter.
This commit is contained in:
parent
85c68fc676
commit
2b289a953b
1 changed files with 24 additions and 0 deletions
|
@ -12,6 +12,13 @@ options.subscribe = true
|
||||||
-- How long to wait for servers response.
|
-- How long to wait for servers response.
|
||||||
options.timeout = 120
|
options.timeout = 120
|
||||||
|
|
||||||
|
-- whether to enter IDLE mode and conintuously check
|
||||||
|
-- for new incoming mail to filter
|
||||||
|
CONTINUOUS=false
|
||||||
|
-- time to wait for next synchronization
|
||||||
|
-- only used in case server does not support IDLE mode
|
||||||
|
UPDATE_TIME=120
|
||||||
|
|
||||||
-- will set filters to be grabbed from XDG-compliant filter directory
|
-- will set filters to be grabbed from XDG-compliant filter directory
|
||||||
-- can be overridden with env var IMAPFILTER_FILTERDIR
|
-- can be overridden with env var IMAPFILTER_FILTERDIR
|
||||||
function getConfigDir()
|
function getConfigDir()
|
||||||
|
@ -56,4 +63,21 @@ assert(configDir, "No configuration directory found. Ensure " .. os.getenv("HOME
|
||||||
accounts = loadfile(configDir .. "/accounts.lua")()
|
accounts = loadfile(configDir .. "/accounts.lua")()
|
||||||
assert(accounts, "No accounts configured. Ensure accounts.lua exists and returns a table of accounts.")
|
assert(accounts, "No accounts configured. Ensure accounts.lua exists and returns a table of accounts.")
|
||||||
|
|
||||||
|
-- implement simple wait function in case server does not support IDLE mode
|
||||||
|
function sleep(n)
|
||||||
|
os.execute("sleep " .. tonumber(n))
|
||||||
|
end
|
||||||
|
|
||||||
|
-- immediately act on the filters once
|
||||||
applyFilters(getFilterDir())
|
applyFilters(getFilterDir())
|
||||||
|
|
||||||
|
-- continuously watch for mail if needed
|
||||||
|
while CONTINUOUS==true do
|
||||||
|
local has_idle=accounts.gmail['Inbox']:enter_idle()
|
||||||
|
applyFilters(getFilterDir())
|
||||||
|
|
||||||
|
if has_idle == false then
|
||||||
|
print("Server does not support idle, application will be polling again in " .. UPDATE_TIME .. "minutes.")
|
||||||
|
sleep(UPDATE_TIME)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
Loading…
Reference in a new issue