2 contributor
---------------
-- Options --
---------------
options.timeout = 120
options.subscribe = true
options.cache = true
options.close = false
options.expunge = true
options.recover = 'all'
options.info = false
options.keepalive = 15
--options.starttls = true
options.certificates = false
dofile(os.getenv('HOME') .. '/' .. os.getenv('IMAPACCOUNT') .. '.lua')
dofile(os.getenv('HOME') .. '/' .. os.getenv('IMAPACCOUNT') .. '.credentials.lua')
if account1.connect() == nil then return 2 end
delete_oldest = function (a)
local r = Set {}
for _, k in pairs(a.age_table) do
r = a.account[k.folder]:is_older(k.age) * a.account[k.folder]:is_unflagged()
if #r > 0 then
r:delete_messages()
end
end
end
-- SYSTÈME D'IMPRESSION
authorized_mails = function ()
authorized = {}
dofile(os.getenv('HOME') .. '/authorized.lua')
local s = ""
for mail, _ in pairs(authorized) do
s = s .. "|" .. mail
end
return "(" .. string.sub(s, 2) .. ")"
end
set_info = function (subject, from, pdf)
local info = {
["colormodel"] = "Gray" , ["colormodel_hr"] = "niveaux de gris" ,
["sides"] = "two-sided-long-edge", ["sides_hr"] = "rect-verso bord long",
["papersize"] = "a4" , ["papersize_hr"] = "A4" ,
["inputslot"] = "PF500B" , ["inputslot_hr"] = "1" ,
["qty"] = string.gsub(subject, "^.*impression%s+(%d+).*$", "%1") ,
["pdf"] = pdf ,
["prenom"] = authorized[from].prenom ,
["phone"] = authorized[from].phone ,
["from"] = from ,
["mode"] = "" ,
}
local options = {
["court"] = function () info.sides = "two-sided-short-edge"; info.sides_hr = "recto-verso bord court" end,
["long"] = function () info.sides = "two-sided-long-edge"; info.sides_hr = "recto-verso bord long" end,
["recto"] = function () info.sides = "recto"; info.sides_hr = "recto" end,
["couleur"] = function () info.colormodel = "CMYK"; info.colormodel_hr = "couleur" end,
["gris"] = function () info.colormodel = "Gray"; info.colormodel_hr = "niveaux de gris" end,
["bac1"] = function () info.inputslot = "PF500B"; info.inputslot_hr = "1" end,
["bac2"] = function () info.inputslot = "PF500A"; info.inputslot_hr = "2" end,
["a4"] = function () info.papersize = "a4"; info.papersize_hr = "A4" end,
["a3"] = function () info.papersize = "a3"; info.papersize_hr = "A3" end,
["massicot"]= function () info.mode = "massicot" end,
["livret"] = function () info.mode = "livret" end,
}
if (tonumber(info.qty) > 250) then
info.qty = 250
end
for option in string.gmatch(subject, "[^%s]+") do
if options[option] then
options[option]()
end
end
if (info.mode == "massicot") then
info.papersize = "a4"
info.papersize_hr = "A4"
info.sides = "two-sided-short-edge"
info.sides_hr = "recto-verso bord court"
end
if (info.mode == "livret") then
info.sides = "two-sided-short-edge"
info.sides_hr = "recto-verso bord court"
end
if (info.papersize == "a3") then
info.inputslot = "PF500A"
info.inputslot_hr = "2"
end
return info
end
generate_script = function (info, spool)
local script = '"' .. spool .. info.pdf .. '.print"'
local sedcmd = ""
for key, value in pairs(info) do
sedcmd = sedcmd .. "s/__" .. key .. "__/" .. string.gsub(value, "/", "\\/") .. "/g;"
end
os.execute("sed '" .. sedcmd:sub(1, -2) .. "' "
.. os.getenv("HOME") .. "/printscript.model > " .. script)
end
inform_user = function (info)
local print_time, wakeup_time = 9, 9
for _, wakeup_time in ipairs({9, 11, 16, 18, 21}) do
if (tonumber(os.date('%H')) < tonumber(wakeup_time)) then
print_time = wakeup_time
break
end
end
mode = info.mode ~= "" and ", en mode " .. info.mode or ""
os.execute("/usr/local/bin/smsapi 'sms pour " .. info.phone
.. ":impression de " .. string.gsub(info.pdf, "'", "\\'")
.. " prévue pour " .. print_time .. "h ("
.. info.qty .. ","
.. info.papersize_hr .. ","
.. info.sides_hr .. ","
.. info.inputslot_hr .. ","
.. info.colormodel_hr
.. mode
.. ")'")
end
set_prints = function (r)
local results = Set {}
results = r:match_from(authorized_mails())
* r:match_subject("(?i)^impression\\s+\\d")
for _, mesg in ipairs(results) do
mbox, uid = table.unpack(mesg)
structure = mbox[uid]:fetch_structure()
for partid, partinf in pairs(structure) do
if (partinf.type == "application/pdf") then
local spool = "/media/nextcloud_db/impression/"
local info = set_info(
string.lower(mbox[uid]:fetch_field("subject")),
string.gsub(mbox[uid]:fetch_field("from"), "^.*<(.+@.+)>.*$", "%1"),
partinf.name)
pipe_to("base64 -id > \"" .. spool .. info.pdf .. "\"", mbox[uid]:fetch_part(partid))
generate_script(info, spool)
done = table.pack(mbox, uid)
inform_user(info)
end
end
end
results:mark_seen()
end
--SYSTÈME DE GÉNÉRATION DU FICHIER D'ANALYSE DES SCRUTINS DE L'ASSEMBLÉE NATIONALE
set_votes = function (r)
local filters = {
'ascii2uni -qaI',
'iconv -f iso8859-1 -t utf-8',
}
local script = '/usr/local/bin/analyse-votes-AN'
local results = Set {}
results = r:match_subject("^scrutins\\s+AN.*")
for _, mesg in ipairs(results) do
local command = ""
mbox, uid = table.unpack(mesg)
local filename = '/dev/shm/set_votes.' .. uid
sender = string.gsub(mbox[uid]:fetch_field("from"), "^.*<(.+@.+)>.*$", "%1")
local command = {
'nohup', script,
'--mail', sender,
'--mailconf', string.gsub(script, "^.*/([^/]+)$", "/usr/local/etc/%1.mail.conf"),
'--database', string.gsub(script, "^.*/([^/]+)$", "/var/local/%1.db"),
'--conf', string.gsub(script, "^.*/([^/]+)$", "/usr/local/etc/%1.conf"),
}
structure = mbox[uid]:fetch_structure()
for partid, partinf in pairs(structure) do
if (partinf.type == "text/plain") then
local commands = {
["utf-8"] = filters[1] .. ' ' .. filename,
["iso-8859-1"] = filters[1] .. ' ' .. filename .. ' | ' .. filters[2],
}
local data = ""
local file = assert(io.open(filename, 'w'))
file:write(mbox[uid]:fetch_body())
file:close()
_, charset = pipe_from("sed -rn 's/\r//; s|^Content-Type: text/plain; charset=\"([^\"]+).+|\\1|p' " .. filename)
charset = string.sub(charset, 1, -2)
if charset == "" then charset = "iso-8859-1" end
--print(charset .. ' : ' .. commands[charset])
file = assert(io.open(filename, 'w'))
file:write(mbox[uid]:fetch_part(partid))
file:close()
_, data = pipe_from(commands[charset])
file = assert(io.open(filename, 'w'))
file:write(data)
file:close()
search_options = {
{["--couleurs"] = "(%x%x%x%x%x%x:%x%x%x%x%x%x)"},
{["--période"] = "([0-9]+/[0-9/]+:[0-9]+/[0-9/]+)"},
{["--liste-députés"] = "liste%s+(des%s+)?députés"},
{["--cible"] = ""},
}
local script_options = {}
for line in io.lines(filename) do
for i, _ in ipairs(search_options) do
for opt, regex in pairs(search_options[i]) do
local value = string.match(line, "^%s*" .. regex .. "%s*$")
if i < #search_options and value ~= nil then
table.insert(script_options, opt .. ' ' .. value)
opt_found = 1
elseif i == #search_options and value == nil then
table.insert(script_options, opt .. ' "' .. string.sub(line, 1, -2) .. '"')
elseif value ~= nil then
opt_end = 1
end
end
if opt_found then break end
end
if opt_end then break end
end
os.remove(filename)
_command = #command
for i = _command + 1, _command + #script_options do table.insert(command, script_options[i - _command]) end
table.insert(command, '> /dev/null 2>&1 &')
end
end
done = table.pack(mbox, uid)
print(table.concat(command, " "))
os.execute(table.concat(command, " "))
end
results:mark_seen()
end
date = os.date('%j')
while true do
recent = account1.inbox:is_unseen()
set_prints(recent)
set_votes(recent)
local date_ = os.date('%j')
if date ~= date_ then
delete_oldest(account1)
date = date_
end
account1.inbox:enter_idle()
end