Diff

mod_host_guard/mod_host_guard.lua @ 753:9d5731af2c27

Merge with Oliver Gerlich
author Matthew Wild <mwild1@gmail.com>
date Fri, 27 Jul 2012 14:29:59 +0100
parent 729:ce8e7b784be0
child 815:b16b291d68c3
line wrap: on
line diff
--- a/mod_host_guard/mod_host_guard.lua	Mon Jun 11 22:32:45 2012 +0200
+++ b/mod_host_guard/mod_host_guard.lua	Fri Jul 27 14:29:59 2012 +0100
@@ -8,7 +8,7 @@
 local guard_protect = module:get_option_set("host_guard_selective", {})
 local guard_block_bl = module:get_option_set("host_guard_blacklist", {})
 
-local config = require "core.configmanager"
+local config = configmanager
 local error_reply = require "util.stanza".error_reply
 
 local function s2s_hook (event)
@@ -40,34 +40,38 @@
 	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 or adding 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()
-	for n,table in pairs(hosts) do
-		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)
-		if guard_blockall:contains(n) or guard_protect:contains(n) then	handle_activation(n) end
+local function init_hosts(u, i)
+	for n in pairs(hosts) do
+		if guard_blockall:contains(n) or guard_protect:contains(n) then
+			handle_deactivation(n, u, i)
+			handle_activation(n, u) 
+		end
 	end
 end
 
@@ -78,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()
@@ -87,7 +91,16 @@
         module:hook ("host-deactivated", handle_deactivation)
         module:hook ("config-reloaded", reload)
 
-        init_hosts()
+        init_hosts(false, true)
+end
+
+function module.unload()
+	module:log ("debug", "removing host handlers as module is being unloaded...")
+	for n in pairs(hosts) do
+		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)
+	end
 end
 
 if prosody.start_time then