Newer Older
239 lines | 8.819kb
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

            
ajoute requête pour les scru...
Sébastien authored on 2022-08-20
148
--SYSTÈME DE GÉNÉRATION DU FICHIER D'ANALYSE DES SCRUTINS DE L'ASSEMBLÉE NATIONALE
149
set_votes = function (r)
150
	local filters = {
151
		'ascii2uni -qaI',
152
		'iconv -f iso8859-1 -t utf-8',
153
	}
154
	local script = '/usr/local/bin/analyse-votes-AN'
155
	local results = Set {}
156
	results = r:match_subject("^scrutins\\s+AN.*")
157
	for _, mesg in ipairs(results) do
158
		local command = ""
159
		mbox, uid = table.unpack(mesg)
160
		local filename = '/dev/shm/set_votes.' .. uid
161
		sender = string.gsub(mbox[uid]:fetch_field("from"), "^.*<(.+@.+)>.*$", "%1")
162
		local command = {
163
			'nohup', script,
164
			'--mail', sender,
fix retour de string.gsub re...
Sébastien authored on 2022-12-06
165
			'--mailconf', (string.gsub(script, "^.*/([^/]+)$", "/usr/local/etc/%1.mail.conf")),
166
			'--database', (string.gsub(script, "^.*/([^/]+)$", "/var/local/%1.db")),
167
			'--conf',     (string.gsub(script, "^.*/([^/]+)$", "/usr/local/etc/%1.conf")),
ajoute requête pour les scru...
Sébastien authored on 2022-08-20
168
		}
169
		structure = mbox[uid]:fetch_structure()
170
		for partid, partinf in pairs(structure) do
171
			if (partinf.type == "text/plain") then
172
				local commands = {
173
					["utf-8"]      = filters[1] .. ' ' .. filename,
174
					["iso-8859-1"] = filters[1] .. ' ' .. filename .. ' | ' .. filters[2],
175
				}
176
				local data = ""
177
				local file = assert(io.open(filename, 'w'))
178
				file:write(mbox[uid]:fetch_body())
179
				file:close()
180
				_, charset = pipe_from("sed -rn 's/\r//; s|^Content-Type: text/plain; charset=\"([^\"]+).+|\\1|p' " .. filename)
181
				charset = string.sub(charset, 1, -2)
182
				if charset == "" then charset = "iso-8859-1" end
183
				--print(charset .. ' : ' .. commands[charset])
184
				file = assert(io.open(filename, 'w'))
185
				file:write(mbox[uid]:fetch_part(partid))
186
				file:close()
187
				_, data = pipe_from(commands[charset])
188
				file = assert(io.open(filename, 'w'))
189
				file:write(data)
190
				file:close()
191
				search_options = {
192
					{["--couleurs"]      = "(%x%x%x%x%x%x:%x%x%x%x%x%x)"},
193
					{["--période"]       = "([0-9]+/[0-9/]+:[0-9]+/[0-9/]+)"},
élargi les possibilités de l...
Sébastien authored on 2022-12-06
194
					{["--liste-députés"] = "liste%s+(des%s+)?députée?s"},
ajoute requête pour les scru...
Sébastien authored on 2022-08-20
195
					{["--cible"]         = ""},
196
				}
197
				local script_options = {}
198
				for line in io.lines(filename) do
199
					for i, _ in ipairs(search_options) do
200
						for opt, regex in pairs(search_options[i]) do
201
							local value = string.match(line, "^%s*" .. regex .. "%s*$")
202
							if i < #search_options and value ~= nil then
203
								table.insert(script_options, opt .. ' ' .. value)
204
								opt_found = 1
205
							elseif i == #search_options and value == nil then
206
								table.insert(script_options, opt .. ' "' .. string.sub(line, 1, -2) .. '"')
207
							elseif value ~= nil then
208
								opt_end = 1
209
							end
210
						end
211
						if opt_found then break end
212
					end
213
					if opt_end then break end
214
				end
215
				os.remove(filename)
216
				_command = #command
217
				for i = _command + 1, _command + #script_options do table.insert(command, script_options[i - _command]) end
218
				table.insert(command, '> /dev/null 2>&1 &')
219
			end
220
		end
221
		done = table.pack(mbox, uid)
222
		print(table.concat(command, " "))
223
		os.execute(table.concat(command, " "))
224
	end
225
	results:mark_seen()
226
end
227

            
ajout système d'impression p...
seb authored on 2019-12-22
228
date = os.date('%j')
229
while true do
improve: recherche une seule...
Sébastien authored on 2022-08-20
230
	recent = account1.inbox:is_unseen()
231
	set_prints(recent)
ajoute requête pour les scru...
Sébastien authored on 2022-08-20
232
	set_votes(recent)
ajout système d'impression p...
seb authored on 2019-12-22
233
	local date_ = os.date('%j')
234
	if date ~= date_ then
235
		delete_oldest(account1)
236
		date = date_
237
	end
238
	account1.inbox:enter_idle()
239
end