comparison mod_firewall/mod_firewall.lua @ 2118:643b254e75de

mod_firewall: Disable more realistic output by default, activated by adding a -v flag
author Kim Alvefur <zash@zash.se>
date Thu, 17 Mar 2016 14:33:14 +0100
parents 5aa3b93cd37a
children 89363766202c
comparison
equal deleted inserted replaced
2117:5aa3b93cd37a 2118:643b254e75de
411 function module.command(arg) 411 function module.command(arg)
412 if not arg[1] or arg[1] == "--help" then 412 if not arg[1] or arg[1] == "--help" then
413 require"util.prosodyctl".show_usage([[mod_firewall <firewall.pfw>]], [[Compile files with firewall rules to Lua code]]); 413 require"util.prosodyctl".show_usage([[mod_firewall <firewall.pfw>]], [[Compile files with firewall rules to Lua code]]);
414 return 1; 414 return 1;
415 end 415 end
416 local verbose = arg[1] == "-v";
417 if verbose then table.remove(arg, 1); end
416 418
417 local serialize = require "util.serialization".serialize; 419 local serialize = require "util.serialization".serialize;
418 print("local logger = require \"util.logger\".init;"); 420 if verbose then
419 print(); 421 print("local logger = require \"util.logger\".init;");
420 print("local function fire_event(name, data)\n\tmodule:fire_event(name, data)\nend"); 422 print();
421 print(); 423 print("local function fire_event(name, data)\n\tmodule:fire_event(name, data)\nend");
424 print();
425 end
422 426
423 for _, filename in ipairs(arg) do 427 for _, filename in ipairs(arg) do
424 print("do -- File "..filename); 428 print("do -- File "..filename);
425 local chain_functions = assert(compile_firewall_rules(arg[1])); 429 local chain_functions = assert(compile_firewall_rules(arg[1]));
426 print(); 430 if verbose then
427 print("local active_definitions = "..serialize(active_definitions)..";"); 431 print();
428 print(); 432 print("local active_definitions = "..serialize(active_definitions)..";");
433 print();
434 end
429 for chain, handler_code in pairs(chain_functions) do 435 for chain, handler_code in pairs(chain_functions) do
430 print("---- Chain "..chain:gsub("_", " ")); 436 print("---- Chain "..chain:gsub("_", " "));
431 437 if not verbose then
432 print(("local %s = (%s)(active_definitions, fire_event, logger(%q));"):format(chain, handler_code:sub(8), filename)); 438 print(("%s = %s;"):format(chain, handler_code:sub(8)));
433 print(); 439 else
434 440
435 local chain_definition = chains[chain]; 441 print(("local %s = (%s)(active_definitions, fire_event, logger(%q));"):format(chain, handler_code:sub(8), filename));
436 if chain_definition and chain_definition.type == "event" then 442 print();
437 for _, event_name in ipairs(chain_definition) do 443
438 print(("module:hook(%q, %s, %d);"):format(event_name, chain, chain_definition.priority or 0)); 444 local chain_definition = chains[chain];
445 if chain_definition and chain_definition.type == "event" then
446 for _, event_name in ipairs(chain_definition) do
447 print(("module:hook(%q, %s, %d);"):format(event_name, chain, chain_definition.priority or 0));
448 end
439 end 449 end
440 end 450 print(("module:hook(%q, %s, %d);"):format("firewall/chains/"..chain, chain, chain_definition.priority or 0));
441 print(("module:hook(%q, %s, %d);"):format("firewall/chains/"..chain, chain, chain_definition.priority or 0)); 451 end
442 452
443 print("---- End of chain "..chain); 453 print("---- End of chain "..chain);
444 print(); 454 print();
445 end 455 end
446 print("end -- End of file "..filename); 456 print("end -- End of file "..filename);