Comparison

mod_host_guard/mod_host_guard.lua @ 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
parent 727:99f5846bcd85
child 729:ce8e7b784be0
comparison
equal deleted inserted replaced
727:99f5846bcd85 728:8ad2e24f5efd
38 end 38 end
39 39
40 return nil 40 return nil
41 end 41 end
42 42
43 local function handle_activation (host) 43 local function handle_activation (host, u)
44 if guard_blockall:contains(host) or guard_protect:contains(host) then 44 if guard_blockall:contains(host) or guard_protect:contains(host) then
45 if hosts[host] and hosts[host].events then 45 if hosts[host] and hosts[host].events then
46 hosts[host].events.add_handler("s2sin-established", s2s_hook, 500) 46 hosts[host].events.add_handler("s2sin-established", s2s_hook, 500)
47 hosts[host].events.add_handler("route/remote", rr_hook, 500) 47 hosts[host].events.add_handler("route/remote", rr_hook, 500)
48 hosts[host].events.add_handler("stanza/jabber:server:dialback:result", s2s_hook, 500) 48 hosts[host].events.add_handler("stanza/jabber:server:dialback:result", s2s_hook, 500)
49 module:log ("debug", "adding host protection for: "..host) 49 if not u then
50 module:log ("debug", "adding host protection for: "..host)
51 else
52 module:log ("debug", "updating host protection for: "..host)
53 end
50 end 54 end
51 end 55 end
52 end 56 end
53 57
54 local function handle_deactivation (host) 58 local function handle_deactivation (host, u, i)
55 if guard_blockall:contains(host) or guard_protect:contains(host) then 59 if guard_blockall:contains(host) or guard_protect:contains(host) then
56 if hosts[host] and hosts[host].events then 60 if hosts[host] and hosts[host].events then
57 hosts[host].events.remove_handler("s2sin-established", s2s_hook) 61 hosts[host].events.remove_handler("s2sin-established", s2s_hook)
58 hosts[host].events.remove_handler("route/remote", rr_hook) 62 hosts[host].events.remove_handler("route/remote", rr_hook)
59 hosts[host].events.remove_handler("stanza/jabber:server:dialback:result", s2s_hook) 63 hosts[host].events.remove_handler("stanza/jabber:server:dialback:result", s2s_hook)
60 module:log ("debug", "removing host protection for: "..host) 64 if not u and not i then module:log ("debug", "removing host protection for: "..host) end
61 end 65 end
62 end 66 end
63 end 67 end
64 68
65 local function init_hosts() 69 local function init_hosts(u, i)
66 for n in pairs(hosts) do 70 for n in pairs(hosts) do
67 if guard_blockall:contains(n) or guard_protect:contains(n) then 71 if guard_blockall:contains(n) or guard_protect:contains(n) then
68 -- required as during config:reload() handlers should be reinitialized as well. 72 handle_deactivation(n, u, i)
69 hosts[n].events.remove_handler("s2sin-established", s2s_hook) 73 handle_activation(n, u)
70 hosts[n].events.remove_handler("route/remote", rr_hook)
71 hosts[n].events.remove_handler("stanza/jabber:server:dialback:result", s2s_hook)
72
73 handle_activation(n)
74 end 74 end
75 end 75 end
76 end 76 end
77 77
78 local function reload() 78 local function reload()
80 guard_blockall = module:get_option_set("host_guard_blockall", {}) 80 guard_blockall = module:get_option_set("host_guard_blockall", {})
81 guard_ball_wl = module:get_option_set("host_guard_blockall_exceptions", {}) 81 guard_ball_wl = module:get_option_set("host_guard_blockall_exceptions", {})
82 guard_protect = module:get_option_set("host_guard_selective", {}) 82 guard_protect = module:get_option_set("host_guard_selective", {})
83 guard_block_bl = module:get_option_set("host_guard_blacklist", {}) 83 guard_block_bl = module:get_option_set("host_guard_blacklist", {})
84 84
85 init_hosts() 85 init_hosts(true)
86 end 86 end
87 87
88 local function setup() 88 local function setup()
89 module:log ("debug", "initializing host guard module...") 89 module:log ("debug", "initializing host guard module...")
90 module:hook ("host-activated", handle_activation) 90 module:hook ("host-activated", handle_activation)
91 module:hook ("host-deactivated", handle_deactivation) 91 module:hook ("host-deactivated", handle_deactivation)
92 module:hook ("config-reloaded", reload) 92 module:hook ("config-reloaded", reload)
93 93
94 init_hosts() 94 init_hosts(false, true)
95 end 95 end
96 96
97 function module.unload() 97 function module.unload()
98 module:log ("debug", "removing host handlers as module is being unloaded...") 98 module:log ("debug", "removing host handlers as module is being unloaded...")
99 for n in pairs(hosts) do 99 for n in pairs(hosts) do