Comparison

mod_firewall/mod_firewall.lua @ 1303:8a3f3f485675

mod_firewall: Produce code with nicer indentation
author Florian Zeitz <florob@babelmonkeys.de>
date Sun, 16 Feb 2014 17:17:23 +0100
parent 1052:80f0a3231c59
child 1304:9f24ccaa66a6
comparison
equal deleted inserted replaced
1302:e556219cb43d 1303:8a3f3f485675
42 -- <actions> 42 -- <actions>
43 -- end 43 -- end
44 -- end 44 -- end
45 45
46 local available_deps = { 46 local available_deps = {
47 st = { global_code = [[local st = require "util.stanza"]]}; 47 st = { global_code = [[local st = require "util.stanza";]]};
48 jid_split = { 48 jid_split = {
49 global_code = [[local jid_split = require "util.jid".split;]]; 49 global_code = [[local jid_split = require "util.jid".split;]];
50 }; 50 };
51 jid_bare = { 51 jid_bare = {
52 global_code = [[local jid_bare = require "util.jid".bare;]]; 52 global_code = [[local jid_bare = require "util.jid".bare;]];
115 table.insert(code.global_header, dep_info.global_code); 115 table.insert(code.global_header, dep_info.global_code);
116 end 116 end
117 end 117 end
118 if dep_info.local_code then 118 if dep_info.local_code then
119 if dep_param ~= "" then 119 if dep_param ~= "" then
120 table.insert(code, "\n\t-- "..dep.."\n\t"..dep_info.local_code(dep_param).."\n\n\t"); 120 table.insert(code, "\n\t\t-- "..dep.."\n\t\t"..dep_info.local_code(dep_param).."\n");
121 else 121 else
122 table.insert(code, "\n\t-- "..dep.."\n\t"..dep_info.local_code.."\n\n\t"); 122 table.insert(code, "\n\t\t-- "..dep.."\n\t\t"..dep_info.local_code.."\n");
123 end 123 end
124 end 124 end
125 code.included_deps[dep] = true; 125 code.included_deps[dep] = true;
126 end 126 end
127 127
281 -- chain (filter-based will be added later) 281 -- chain (filter-based will be added later)
282 for _, rule in ipairs(rules) do 282 for _, rule in ipairs(rules) do
283 for _, dep in ipairs(rule.deps) do 283 for _, dep in ipairs(rule.deps) do
284 include_dep(dep, code); 284 include_dep(dep, code);
285 end 285 end
286 local rule_code = table.concat(rule.actions, "\n\t"); 286 table.insert(code, "\n\t\t");
287 local rule_code;
287 if #rule.conditions > 0 then 288 if #rule.conditions > 0 then
288 for i, condition in ipairs(rule.conditions) do 289 for i, condition in ipairs(rule.conditions) do
289 local negated = condition:match("^not%(.+%)$"); 290 local negated = condition:match("^not%(.+%)$");
290 if negated then 291 if negated then
291 condition = condition:match("^not%((.+)%)$"); 292 condition = condition:match("^not%((.+)%)$");
294 rule.conditions[i] = (negated and "not(" or "")..condition_cache[condition]..(negated and "_" or ""); 295 rule.conditions[i] = (negated and "not(" or "")..condition_cache[condition]..(negated and "_" or "");
295 else 296 else
296 n_conditions = n_conditions + 1; 297 n_conditions = n_conditions + 1;
297 local name = "condition"..n_conditions; 298 local name = "condition"..n_conditions;
298 condition_cache[condition] = name; 299 condition_cache[condition] = name;
299 table.insert(code, "local "..name.." = "..condition..";\n\t"); 300 table.insert(code, "local "..name.." = "..condition..";\n\t\t");
300 rule.conditions[i] = (negated and "not(" or "")..name..(negated and ")" or ""); 301 rule.conditions[i] = (negated and "not(" or "")..name..(negated and ")" or "");
301 end 302 end
302 end 303 end
303 rule_code = "if "..table.concat(rule.conditions, " and ").." then\n\t" 304 rule_code = "if "..table.concat(rule.conditions, " and ").." then\n\t\t\t"
304 ..rule_code 305 ..table.concat(rule.actions, "\n\t\t\t")
305 .."\n end\n"; 306 .."\n\t\tend\n";
307 else
308 rule_code = table.concat(rule.actions, "\n\t\t");
306 end 309 end
307 table.insert(code, rule_code); 310 table.insert(code, rule_code);
308 end 311 end
309 312
310 for name in pairs(definition_handlers) do 313 for name in pairs(definition_handlers) do
311 table.insert(code.global_header, 1, "local "..name:lower().."s = definitions."..name..";"); 314 table.insert(code.global_header, 1, "local "..name:lower().."s = definitions."..name..";");
312 end 315 end
313 316
314 local code_string = [[return function (definitions, fire_event, log) 317 local code_string = "return function (definitions, fire_event, log)\n\t"
315 ]]..table.concat(code.global_header, "\n")..[[ 318 ..table.concat(code.global_header, "\n\t")
316 local db = require 'util.debug' 319 .."\n\tlocal db = require 'util.debug';\n\n\t"
317 return function (event) 320 .."return function (event)\n\t\t"
318 local stanza, session = event.stanza, event.origin; 321 .."local stanza, session = event.stanza, event.origin;\n"
319 322 ..table.concat(code, "")
320 ]]..table.concat(code, " ")..[[ 323 .."\n\tend;\nend";
321 end;
322 end]];
323 324
324 chain_handlers[chain_name] = code_string; 325 chain_handlers[chain_name] = code_string;
325 end 326 end
326 327
327 return chain_handlers; 328 return chain_handlers;