Newer Older
158 lines | 5.808kb
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"                  ,
47
		["inputslot"]  = "PF730A"             , ["inputslot_hr"]  = "1"                   ,
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,
59
		["couleur"] = function () info.colormodel = "CYMB";                 info.colormodel_hr = "couleur"            end,
60
		["gris"]    = function () info.colormodel = "Gray";                 info.colormodel_hr = "niveaux de gris"    end,
61
		["bac1"]    = function () info.inputslot  = "PF730A";               info.inputslot_hr  = "1"                  end,
62
		["bac2"]    = function () info.inputslot  = "PF730B";               info.inputslot_hr  = "2"                  end,
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
87
		info.inputslot    = "PF730D"
88
		info.inputslot_hr = "4"
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

            
124
set_prints = function (a)
125
	local recent = a.inbox:is_unseen()
126
	local results = Set {}
127
	results = recent:match_from(authorized_mails())
128
	        * recent:match_subject("(?i)^impression\\s+\\d")
129
	for _, mesg in ipairs(results) do 
130
		mbox, uid = table.unpack(mesg)
131
		structure = mbox[uid]:fetch_structure()
132
		for partid, partinf in pairs(structure) do 
133
			if (partinf.type == "application/pdf") then
134
				local spool = "/media/nextcloud_db/impression/"
135
				local info = set_info(
136
					string.lower(mbox[uid]:fetch_field("subject")),
137
					string.gsub(mbox[uid]:fetch_field("from"), "^.*<(.+@.+)>.*$", "%1"),
138
					partinf.name)
139
				pipe_to("base64 -id > \"" .. spool .. info.pdf .. "\"", mbox[uid]:fetch_part(partid))
140
				generate_script(info, spool)
141
				done = table.pack(mbox, uid)
142
				inform_user(info)
143
			end
144
		end
145
	end
146
	results:mark_seen()
147
end
148

            
149
date = os.date('%j')
150
while true do
151
	set_prints(account1)
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