Showing 1 changed files with 81 additions and 0 deletions
+81
imapfilter/.imapfilter/config.lua
... ...
@@ -145,10 +145,91 @@ set_prints = function (r)
145 145
 	results:mark_seen()
146 146
 end
147 147
 
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,
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"),
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/]+)"},
194
+					{["--liste-députés"] = "liste%s+(des%s+)?députés"},
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
+
148 228
 date = os.date('%j')
149 229
 while true do
150 230
 	recent = account1.inbox:is_unseen()
151 231
 	set_prints(recent)
232
+	set_votes(recent)
152 233
 	local date_ = os.date('%j')
153 234
 	if date ~= date_ then
154 235
 		delete_oldest(account1)