lua: Format with stylua
This commit is contained in:
parent
e434c191c9
commit
5f93ecba7c
33 changed files with 4062 additions and 3413 deletions
|
|
@ -1,15 +1,13 @@
|
|||
local accounts = {}
|
||||
|
||||
local _, gmailuser = pipe_from(
|
||||
'pass show misc/gmail-app-password | grep username | cut -d: -f2')
|
||||
local _, gmailpass = pipe_from(
|
||||
'pass show misc/gmail-app-password | head -n1')
|
||||
local _, gmailuser = pipe_from("pass show misc/gmail-app-password | grep username | cut -d: -f2")
|
||||
local _, gmailpass = pipe_from("pass show misc/gmail-app-password | head -n1")
|
||||
-- Setup an imap account called gmail
|
||||
accounts.gmail = IMAP {
|
||||
server = "imap.gmail.com",
|
||||
username = gmailuser,
|
||||
password = gmailpass,
|
||||
ssl = "auto"
|
||||
}
|
||||
accounts.gmail = IMAP({
|
||||
server = "imap.gmail.com",
|
||||
username = gmailuser,
|
||||
password = gmailpass,
|
||||
ssl = "auto",
|
||||
})
|
||||
|
||||
return accounts
|
||||
|
|
|
|||
|
|
@ -20,67 +20,64 @@ CONTINUOUS = false
|
|||
UPDATE_TIME = 120
|
||||
|
||||
-- implement simple wait function in case server does not support IDLE mode
|
||||
function sleep(n) os.execute("sleep " .. tonumber(n)) end
|
||||
function sleep(n)
|
||||
os.execute("sleep " .. tonumber(n))
|
||||
end
|
||||
|
||||
-- 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
|
||||
-- -- 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
|
||||
function getFilterDir()
|
||||
-- -- set directory for imapfilter files
|
||||
local imapfilterdir
|
||||
if os.getenv("IMAPFILTER_FILTERDIR") then
|
||||
imapfilterdir = os.getenv("IMAPFILTER_FILTERDIR")
|
||||
else
|
||||
imapfilterdir = configDir .. "/filters"
|
||||
end
|
||||
return imapfilterdir
|
||||
-- -- set directory for imapfilter files
|
||||
local imapfilterdir
|
||||
if os.getenv("IMAPFILTER_FILTERDIR") then
|
||||
imapfilterdir = os.getenv("IMAPFILTER_FILTERDIR")
|
||||
else
|
||||
imapfilterdir = configDir .. "/filters"
|
||||
end
|
||||
return imapfilterdir
|
||||
end
|
||||
|
||||
-- dirlist, from https://stackoverflow.com/a/25266573
|
||||
function applyFilters(dir)
|
||||
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
|
||||
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.")
|
||||
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.")
|
||||
assert(accounts, "No accounts configured. Ensure accounts.lua exists and returns a table of accounts.")
|
||||
|
||||
-- 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())
|
||||
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
|
||||
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
|
||||
|
|
|
|||
|
|
@ -1,40 +1,39 @@
|
|||
function sendToFolder(folderFrom, folderTo, senders)
|
||||
local messages = folderFrom:select_all()
|
||||
for _, sender in pairs(senders) do
|
||||
local filtered = messages:contain_from(sender)
|
||||
filtered:mark_seen()
|
||||
filtered:move_messages(folderTo)
|
||||
end
|
||||
local messages = folderFrom:select_all()
|
||||
for _, sender in pairs(senders) do
|
||||
local filtered = messages:contain_from(sender)
|
||||
filtered:mark_seen()
|
||||
filtered:move_messages(folderTo)
|
||||
end
|
||||
end
|
||||
|
||||
-- will set filters to be grabbed from XDG-compliant filter directory
|
||||
-- can be overridden with env var IMAPFILTER_ROLLUPFILE
|
||||
function getRollupFile(fname)
|
||||
local f
|
||||
local fname = fname or "rollup.txt"
|
||||
if os.getenv("IMAPFILTER_ROLLUPFILE") then
|
||||
f = os.getenv("IMAPFILTER_ROLLUPFILE")
|
||||
elseif os.getenv("XDG_DATA_HOME") then
|
||||
f = os.getenv("XDG_DATA_HOME") .. "/imapfilter/" .. fname
|
||||
else
|
||||
f = os.getenv("HOME") .. "/.local/share/imapfilter/" .. fname
|
||||
end
|
||||
return f
|
||||
local f
|
||||
local fname = fname or "rollup.txt"
|
||||
if os.getenv("IMAPFILTER_ROLLUPFILE") then
|
||||
f = os.getenv("IMAPFILTER_ROLLUPFILE")
|
||||
elseif os.getenv("XDG_DATA_HOME") then
|
||||
f = os.getenv("XDG_DATA_HOME") .. "/imapfilter/" .. fname
|
||||
else
|
||||
f = os.getenv("HOME") .. "/.local/share/imapfilter/" .. fname
|
||||
end
|
||||
return f
|
||||
end
|
||||
|
||||
function getSenderList(rollupfile)
|
||||
local rollupSenders = {}
|
||||
local rollupSenders = {}
|
||||
|
||||
local file = io.open(rollupfile)
|
||||
if file then
|
||||
for line in file:lines() do table.insert(rollupSenders, line) end
|
||||
else
|
||||
print(
|
||||
"rollup did not find rollup.txt file containing mail addresses at " ..
|
||||
rollupfile or ". Skipping.")
|
||||
end
|
||||
return rollupSenders
|
||||
local file = io.open(rollupfile)
|
||||
if file then
|
||||
for line in file:lines() do
|
||||
table.insert(rollupSenders, line)
|
||||
end
|
||||
else
|
||||
print("rollup did not find rollup.txt file containing mail addresses at " .. rollupfile or ". Skipping.")
|
||||
end
|
||||
return rollupSenders
|
||||
end
|
||||
|
||||
sendToFolder(accounts.gmail["Inbox"], accounts.gmail["Dump"],
|
||||
getSenderList(getRollupFile()))
|
||||
sendToFolder(accounts.gmail["Inbox"], accounts.gmail["Dump"], getSenderList(getRollupFile()))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue