Changeset

728:8ad2e24f5efd

mod_host_guard: reduced code duplication and added better logging.
author Marco Cirillo <maranda@lightwitch.org>
date Tue, 26 Jun 2012 12:05:24 +0000
parents 727:99f5846bcd85
children 729:ce8e7b784be0
files mod_host_guard/mod_host_guard.lua
diffstat 1 files changed, 13 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/mod_host_guard/mod_host_guard.lua	Tue Jun 26 10:57:24 2012 +0000
+++ b/mod_host_guard/mod_host_guard.lua	Tue Jun 26 12:05:24 2012 +0000
@@ -40,37 +40,37 @@
 	return nil
 end
 
-local function handle_activation (host)
+local function handle_activation (host, u)
 	if guard_blockall:contains(host) or guard_protect:contains(host) then
 		if hosts[host] and hosts[host].events then
 			hosts[host].events.add_handler("s2sin-established", s2s_hook, 500)
 			hosts[host].events.add_handler("route/remote", rr_hook, 500)
 			hosts[host].events.add_handler("stanza/jabber:server:dialback:result", s2s_hook, 500)
-                	module:log ("debug", "adding host protection for: "..host)
+                	if not u then 
+				module:log ("debug", "adding host protection for: "..host)
+			else
+				module:log ("debug", "updating host protection for: "..host)
+			end
 		end
 	end
 end
 
-local function handle_deactivation (host)
+local function handle_deactivation (host, u, i)
 	if guard_blockall:contains(host) or guard_protect:contains(host) then
 		if hosts[host] and hosts[host].events then
 			hosts[host].events.remove_handler("s2sin-established", s2s_hook)
 			hosts[host].events.remove_handler("route/remote", rr_hook)
 			hosts[host].events.remove_handler("stanza/jabber:server:dialback:result", s2s_hook)
-                	module:log ("debug", "removing host protection for: "..host)
+                	if not u and not i then module:log ("debug", "removing host protection for: "..host) end
 		end
 	end
 end
 
-local function init_hosts()
+local function init_hosts(u, i)
 	for n in pairs(hosts) do
 		if guard_blockall:contains(n) or guard_protect:contains(n) then
-			-- required as during config:reload() handlers should be reinitialized as well.
-			hosts[n].events.remove_handler("s2sin-established", s2s_hook)
-			hosts[n].events.remove_handler("route/remote", rr_hook)
-			hosts[n].events.remove_handler("stanza/jabber:server:dialback:result", s2s_hook)
-			
-			handle_activation(n) 
+			handle_deactivation(n, u, i)
+			handle_activation(n, u) 
 		end
 	end
 end
@@ -82,7 +82,7 @@
 	guard_protect = module:get_option_set("host_guard_selective", {})
 	guard_block_bl = module:get_option_set("host_guard_blacklist", {})
 
-	init_hosts()
+	init_hosts(true)
 end
 
 local function setup()
@@ -91,7 +91,7 @@
         module:hook ("host-deactivated", handle_deactivation)
         module:hook ("config-reloaded", reload)
 
-        init_hosts()
+        init_hosts(false, true)
 end
 
 function module.unload()