# HG changeset patch # User Matthew Wild # Date 1368091164 -3600 # Node ID 9c88960b0f811cbbe727bb82ccc83293e5e839df # Parent 591590de34ef6469f5f09d2665e1a1330f7cde71 mod_auto_activate_hosts: Automatically activate and deactivate hosts when they are added/removed from the config diff -r 591590de34ef -r 9c88960b0f81 mod_auto_activate_hosts/mod_auto_activate_hosts.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mod_auto_activate_hosts/mod_auto_activate_hosts.lua Thu May 09 10:19:24 2013 +0100 @@ -0,0 +1,37 @@ +module:set_global(); + +local array = require "util.array"; +local set = require "util.set"; +local it = require "util.iterators"; +local config = require "core.configmanager"; + +local function host_not_global(host) + return host ~= "*"; +end + +local function host_is_enabled(host) + return config.get(host, "enabled") ~= false; +end + +function handle_reload() + local new_config = config.getconfig(); + local active_hosts = set.new(array.collect(it.keys(prosody.hosts)):filter(host_not_global)); + local enabled_hosts = set.new(array.collect(it.keys(new_config)):filter(host_is_enabled)); + local need_to_activate = enabled_hosts - active_hosts; + local need_to_deactivate = active_hosts - enabled_hosts; + + module:log("debug", "Config reloaded... %d hosts need activating, and %d hosts need deactivating", it.count(need_to_activate), it.count(need_to_deactivate)); + module:log("debug", "There are %d enabled and %d active hosts", it.count(enabled_hosts), it.count(active_hosts)); + for host in need_to_deactivate do + hostmanager.deactivate(host); + end + + -- If the lazy loader is loaded, hosts will get activated when they are needed + if not(getmetatable(prosody.hosts) and getmetatable(prosody.hosts).lazy_loader) then + for host in need_to_activate do + hostmanager.activate(host); + end + end +end + +module:hook_global("config-reloaded", handle_reload);