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"] = "PF730A" , ["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 = "CYMB"; info.colormodel_hr = "couleur" end,
["gris"] = function () info.colormodel = "Gray"; info.colormodel_hr = "niveaux de gris" end,
["bac1"] = function () info.inputslot = "PF730A"; info.inputslot_hr = "1" end,
["bac2"] = function () info.inputslot = "PF730B"; 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 = "PF730D"
info.inputslot_hr = "4"
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 (a)
local recent = a.inbox:is_unseen()
local results = Set {}
results = recent:match_from(authorized_mails())
* recent: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
date = os.date('%j')
while true do
set_prints(account1)
local date_ = os.date('%j')
if date ~= date_ then
delete_oldest(account1)
date = date_
end
account1.inbox:enter_idle()
end