Newer Older
156 lines | 5.753kb
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
13

            
14
dofile(os.getenv('HOME') .. '/' .. os.getenv('IMAPACCOUNT') .. '.lua')
15
dofile(os.getenv('HOME') .. '/' .. os.getenv('IMAPACCOUNT') .. '.credentials.lua')
16

            
17
if account1.connect() == nil then return 2 end
18

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

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

            
40
set_info = function (subject, from, pdf)
41
	local info = {
42
		["colormodel"] = "Gray"               , ["colormodel_hr"] = "niveaux de gris"     ,
43
		["sides"]      = "two-sided-long-edge", ["sides_hr"]      = "rect-verso bord long",
44
		["papersize"]  = "a4"                 , ["papersize_hr"]  = "A4"                  ,
45
		["inputslot"]  = "PF730A"             , ["inputslot_hr"]  = "1"                   ,
46
		["qty"]        = string.gsub(subject, "^.*impression%s+(%d+).*$", "%1")           ,
47
		["pdf"]        = pdf                                                              ,
48
		["prenom"]     = authorized[from].prenom                                          ,
49
		["phone"]      = authorized[from].phone                                           ,
50
		["from"]       = from                                                             ,
51
		["mode"]       = ""                                                               ,
52
	}
53
	local options = {
54
		["court"]   = function () info.sides      = "two-sided-short-edge"; info.sides_hr = "recto-verso bord court"  end,
55
		["long"]    = function () info.sides      = "two-sided-long-edge";  info.sides_hr = "recto-verso bord long"   end,
56
		["recto"]   = function () info.sides      = "recto";                info.sides_hr = "recto"                   end,
57
		["couleur"] = function () info.colormodel = "CYMB";                 info.colormodel_hr = "couleur"            end,
58
		["gris"]    = function () info.colormodel = "Gray";                 info.colormodel_hr = "niveaux de gris"    end,
59
		["bac1"]    = function () info.inputslot  = "PF730A";               info.inputslot_hr  = "1"                  end,
60
		["bac2"]    = function () info.inputslot  = "PF730B";               info.inputslot_hr  = "2"                  end,
61
		["a4"]      = function () info.papersize  = "a4";                   info.papersize_hr  = "A4"                 end,
62
		["a3"]      = function () info.papersize  = "a3";                   info.papersize_hr  = "A3"                 end,
63
		["massicot"]= function () info.mode       = "massicot"                                                        end, 
64
		["livret"]  = function () info.mode       = "livret"                                                          end,
65
	}
66
	if (tonumber(info.qty) > 250) then
67
		info.qty = 250
68
	end
69
	for option in string.gmatch(subject, "[^%s]+") do
70
		if options[option] then
71
			options[option]()
72
		end
73
	end
74
	if (info.mode == "massicot") then
75
		info.papersize    = "a4"
76
		info.papersize_hr = "A4"
77
		info.sides        = "two-sided-short-edge"
78
		info.sides_hr     = "recto-verso bord court"
79
	end
80
	if (info.mode == "livret") then
81
		info.sides        = "two-sided-short-edge"
82
		info.sides_hr     = "recto-verso bord court"
83
	end
84
	if (info.papersize == "a3") then
85
		info.inputslot    = "PF730D"
86
		info.inputslot_hr = "4"
87
	end
88
	return info
89
end
90

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

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

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

            
147
date = os.date('%j')
148
while true do
149
	set_prints(account1)
150
	local date_ = os.date('%j')
151
	if date ~= date_ then
152
		delete_oldest(account1)
153
		date = date_
154
	end
155
	account1.inbox:enter_idle()
156
end