comparison mod_firewall/mod_firewall.lua @ 1052:80f0a3231c59

mod_firewall: Add support for being called as a prosodyctl command
author Kim Alvefur <zash@zash.se>
date Wed, 05 Jun 2013 21:14:59 +0200
parents c66ea26e4224
children 8a3f3f485675
comparison
equal deleted inserted replaced
1051:c66ea26e4224 1052:80f0a3231c59
339 chunk = chunk()(active_definitions, fire_event, logger(filename)); -- Returns event handler with 'zones' upvalue. 339 chunk = chunk()(active_definitions, fire_event, logger(filename)); -- Returns event handler with 'zones' upvalue.
340 return chunk; 340 return chunk;
341 end 341 end
342 342
343 function module.load() 343 function module.load()
344 if not prosody.arg then return end -- Don't run in prosodyctl
344 active_definitions = {}; 345 active_definitions = {};
345 local firewall_scripts = module:get_option_set("firewall_scripts", {}); 346 local firewall_scripts = module:get_option_set("firewall_scripts", {});
346 for script in firewall_scripts do 347 for script in firewall_scripts do
347 script = resolve_relative_path(prosody.paths.config, script); 348 script = resolve_relative_path(prosody.paths.config, script);
348 local chain_functions, err = compile_firewall_rules(script) 349 local chain_functions, err = compile_firewall_rules(script)
370 end 371 end
371 -- Replace contents of definitions table (shared) with active definitions 372 -- Replace contents of definitions table (shared) with active definitions
372 for k in it.keys(definitions) do definitions[k] = nil; end 373 for k in it.keys(definitions) do definitions[k] = nil; end
373 for k,v in pairs(active_definitions) do definitions[k] = v; end 374 for k,v in pairs(active_definitions) do definitions[k] = v; end
374 end 375 end
376
377 function module.command(arg)
378 if not arg[1] or arg[1] == "--help" then
379 require"util.prosodyctl".show_usage([[mod_firewall <firewall.pfw>]], [[Compile files with firewall rules to Lua code]]);
380 return 1;
381 end
382
383 for _, filename in ipairs(arg) do
384 print("\n-- File "..filename);
385 local chain_functions = assert(compile_firewall_rules(arg[1]));
386 for chain, handler_code in pairs(chain_functions) do
387 print("\n---- Chain "..chain);
388 print(handler_code);
389 print("\n---- End of chain "..chain);
390 end
391 print("\n-- End of file "..filename);
392 end
393 end