Newer Older
158 lines | 5.797kb
ajout système d'impression p...
seb authored on 2019-12-22
1
---------------
2
--  Options  --
3
---------------
4

            
5
options.timeout = 120
6
options.subscribe = true
7
options.cache = true
8
options.close = false
9
options.expunge = true
10
options.recover = 'all'
11
options.info = false
12
options.keepalive = 15
fix certificate issue with i...
Sébastien authored on 2022-08-20
13
--options.starttls = true
14
options.certificates = false
ajout système d'impression p...
seb authored on 2019-12-22
15

            
16
dofile(os.getenv('HOME') .. '/' .. os.getenv('IMAPACCOUNT') .. '.lua')
17
dofile(os.getenv('HOME') .. '/' .. os.getenv('IMAPACCOUNT') .. '.credentials.lua')
18

            
19
if account1.connect() == nil then return 2 end
20

            
21
delete_oldest = function (a)
22
	local r = Set {}
23
	for _, k in pairs(a.age_table) do
24
		r = a.account[k.folder]:is_older(k.age) * a.account[k.folder]:is_unflagged()
25
		if #r > 0 then
26
			r:delete_messages()
27
		end
28
	end
29
end
30

            
31
-- SYSTÈME D'IMPRESSION
32
authorized_mails = function ()
33
	authorized = {}
34
	dofile(os.getenv('HOME') .. '/authorized.lua')
35
	local s = ""
36
	for mail, _ in pairs(authorized) do
37
		s = s .. "|" .. mail
38
	end
39
	return "(" .. string.sub(s, 2) .. ")"
40
end
41

            
42
set_info = function (subject, from, pdf)
43
	local info = {
44
		["colormodel"] = "Gray"               , ["colormodel_hr"] = "niveaux de gris"     ,
45
		["sides"]      = "two-sided-long-edge", ["sides_hr"]      = "rect-verso bord long",
46
		["papersize"]  = "a4"                 , ["papersize_hr"]  = "A4"                  ,
improve: recherche une seule...
Sébastien authored on 2022-08-20
47
		["inputslot"]  = "PF500B"             , ["inputslot_hr"]  = "1"                   ,
ajout système d'impression p...
seb authored on 2019-12-22
48
		["qty"]        = string.gsub(subject, "^.*impression%s+(%d+).*$", "%1")           ,
49
		["pdf"]        = pdf                                                              ,
50
		["prenom"]     = authorized[from].prenom                                          ,
51
		["phone"]      = authorized[from].phone                                           ,
52
		["from"]       = from                                                             ,
53
		["mode"]       = ""                                                               ,
54
	}
55
	local options = {
56
		["court"]   = function () info.sides      = "two-sided-short-edge"; info.sides_hr = "recto-verso bord court"  end,
57
		["long"]    = function () info.sides      = "two-sided-long-edge";  info.sides_hr = "recto-verso bord long"   end,
58
		["recto"]   = function () info.sides      = "recto";                info.sides_hr = "recto"                   end,
improve: recherche une seule...
Sébastien authored on 2022-08-20
59
		["couleur"] = function () info.colormodel = "CMYK";                 info.colormodel_hr = "couleur"            end,
ajout système d'impression p...
seb authored on 2019-12-22
60
		["gris"]    = function () info.colormodel = "Gray";                 info.colormodel_hr = "niveaux de gris"    end,
improve: recherche une seule...
Sébastien authored on 2022-08-20
61
		["bac1"]    = function () info.inputslot  = "PF500B";               info.inputslot_hr  = "1"                  end,
62
		["bac2"]    = function () info.inputslot  = "PF500A";               info.inputslot_hr  = "2"                  end,
ajout système d'impression p...
seb authored on 2019-12-22
63
		["a4"]      = function () info.papersize  = "a4";                   info.papersize_hr  = "A4"                 end,
64
		["a3"]      = function () info.papersize  = "a3";                   info.papersize_hr  = "A3"                 end,
65
		["massicot"]= function () info.mode       = "massicot"                                                        end, 
66
		["livret"]  = function () info.mode       = "livret"                                                          end,
67
	}
68
	if (tonumber(info.qty) > 250) then
69
		info.qty = 250
70
	end
71
	for option in string.gmatch(subject, "[^%s]+") do
72
		if options[option] then
73
			options[option]()
74
		end
75
	end
76
	if (info.mode == "massicot") then
77
		info.papersize    = "a4"
78
		info.papersize_hr = "A4"
79
		info.sides        = "two-sided-short-edge"
80
		info.sides_hr     = "recto-verso bord court"
81
	end
82
	if (info.mode == "livret") then
83
		info.sides        = "two-sided-short-edge"
84
		info.sides_hr     = "recto-verso bord court"
85
	end
86
	if (info.papersize == "a3") then
improve: recherche une seule...
Sébastien authored on 2022-08-20
87
		info.inputslot    = "PF500A"
88
		info.inputslot_hr = "2"
ajout système d'impression p...
seb authored on 2019-12-22
89
	end
90
	return info
91
end
92

            
93
generate_script = function (info, spool)
94
	local script = '"' .. spool .. info.pdf .. '.print"'
95
	local sedcmd = ""
96
	for key, value in pairs(info) do
97
		sedcmd = sedcmd .. "s/__" .. key .. "__/" .. string.gsub(value, "/", "\\/") .. "/g;"
98
	end
99
	os.execute("sed '" .. sedcmd:sub(1, -2) .. "' " 
100
		   .. os.getenv("HOME") .. "/printscript.model > " .. script)
101
end
102

            
103
inform_user = function (info)
104
	local print_time, wakeup_time = 9, 9
105
	for _, wakeup_time in ipairs({9, 11, 16, 18, 21}) do
106
		if (tonumber(os.date('%H')) < tonumber(wakeup_time)) then
107
			print_time = wakeup_time
108
			break
109
		end
110
	end
111
	mode = info.mode ~= "" and ", en mode " .. info.mode or ""
112
	os.execute("/usr/local/bin/smsapi 'sms pour " .. info.phone
113
		   .. ":impression de " .. string.gsub(info.pdf, "'", "\\'")
114
		   .. " prévue pour " .. print_time .. "h ("
115
		   .. info.qty .. ","
116
		   .. info.papersize_hr .. ","
117
		   .. info.sides_hr .. ","
118
		   .. info.inputslot_hr .. ","
119
		   .. info.colormodel_hr
120
		   .. mode
121
		   .. ")'")
122
end
123

            
improve: recherche une seule...
Sébastien authored on 2022-08-20
124
set_prints = function (r)
ajout système d'impression p...
seb authored on 2019-12-22
125
	local results = Set {}
improve: recherche une seule...
Sébastien authored on 2022-08-20
126
	results = r:match_from(authorized_mails())
127
	        * r:match_subject("(?i)^impression\\s+\\d")
ajout système d'impression p...
seb authored on 2019-12-22
128
	for _, mesg in ipairs(results) do 
129
		mbox, uid = table.unpack(mesg)
130
		structure = mbox[uid]:fetch_structure()
131
		for partid, partinf in pairs(structure) do 
132
			if (partinf.type == "application/pdf") then
133
				local spool = "/media/nextcloud_db/impression/"
134
				local info = set_info(
135
					string.lower(mbox[uid]:fetch_field("subject")),
136
					string.gsub(mbox[uid]:fetch_field("from"), "^.*<(.+@.+)>.*$", "%1"),
137
					partinf.name)
138
				pipe_to("base64 -id > \"" .. spool .. info.pdf .. "\"", mbox[uid]:fetch_part(partid))
139
				generate_script(info, spool)
140
				done = table.pack(mbox, uid)
141
				inform_user(info)
142
			end
143
		end
144
	end
145
	results:mark_seen()
146
end
147

            
148
date = os.date('%j')
149
while true do
improve: recherche une seule...
Sébastien authored on 2022-08-20
150
	recent = account1.inbox:is_unseen()
151
	set_prints(recent)
ajout système d'impression p...
seb authored on 2019-12-22
152
	local date_ = os.date('%j')
153
	if date ~= date_ then
154
		delete_oldest(account1)
155
		date = date_
156
	end
157
	account1.inbox:enter_idle()
158
end