From 2b289a953b984b136a642f32324f771ba4b5cf4c Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Mon, 21 Sep 2020 09:41:45 +0200 Subject: [PATCH] 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. --- mail/.config/imapfilter/config.lua | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/mail/.config/imapfilter/config.lua b/mail/.config/imapfilter/config.lua index a09e6ac..80a58e6 100644 --- a/mail/.config/imapfilter/config.lua +++ b/mail/.config/imapfilter/config.lua @@ -12,6 +12,13 @@ options.subscribe = true -- How long to wait for servers response. 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 -- can be overridden with env var IMAPFILTER_FILTERDIR function getConfigDir() @@ -56,4 +63,21 @@ assert(configDir, "No configuration directory found. Ensure " .. os.getenv("HOME accounts = loadfile(configDir .. "/accounts.lua")() 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()) + +-- 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