2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
9 local configmanager = require "core.configmanager";
10 local modulemanager = require "core.modulemanager";
11 local events_new = require "util.events".new;
12 local disco_items = require "util.multitable".new();
15 local jid_split = require "util.jid".split;
16 local uuid_gen = require "util.uuid".generate;
18 local log = require "util.logger".init("hostmanager");
21 local prosody_events = prosody.events;
22 if not _G.prosody.incoming_s2s then
23 require "core.s2smanager";
25 local incoming_s2s = _G.prosody.incoming_s2s;
27 local pairs, select = pairs, select;
28 local tostring, type = tostring, type;
32 local hosts_loaded_once;
34 local function load_enabled_hosts(config)
35 local defined_hosts = config or configmanager.getconfig();
36 local activated_any_host;
38 for host, host_config in pairs(defined_hosts) do
39 if host ~= "*" and host_config.core.enabled ~= false then
40 if not host_config.core.component_module then
41 activated_any_host = true;
43 activate(host, host_config);
47 if not activated_any_host then
48 log("error", "No active VirtualHost entries in the config file. This may cause unexpected behaviour as no modules will be loaded.");
51 prosody_events.fire_event("hosts-activated", defined_hosts);
52 hosts_loaded_once = true;
55 prosody_events.add_handler("server-starting", load_enabled_hosts);
57 local function host_send(stanza)
58 local name, type = stanza.name, stanza.attr.type;
59 if type == "error" or (name == "iq" and type == "result") then
60 local dest_host_name = select(2, jid_split(stanza.attr.to));
61 local dest_host = hosts[dest_host_name] or { type = "unknown" };
62 log("warn", "Unhandled response sent to %s host %s: %s", dest_host.type, dest_host_name, tostring(stanza));
65 core_route_stanza(nil, stanza);
68 function activate(host, host_config)
69 if hosts[host] then return nil, "The host "..host.." is already activated"; end
70 host_config = host_config or configmanager.getconfig()[host];
71 if not host_config then return nil, "Couldn't find the host "..tostring(host).." defined in the current config"; end
72 local host_session = {
75 events = events_new();
76 dialback_secret = configmanager.get(host, "core", "dialback_secret") or uuid_gen();
79 if not host_config.core.component_module then -- host
80 host_session.type = "local";
81 host_session.sessions = {};
83 host_session.type = "component";
85 hosts[host] = host_session;
86 if not host:match("[@/]") then
87 disco_items:set(host:match("%.(.*)") or "*", host, true);
89 for option_name in pairs(host_config.core) do
90 if option_name:match("_ports$") or option_name:match("_interface$") then
91 log("warn", "%s: Option '%s' has no effect for virtual hosts - put it in the server-wide section instead", host, option_name);
95 log((hosts_loaded_once and "info") or "debug", "Activated host: %s", host);
96 prosody_events.fire_event("host-activated", host);
100 function deactivate(host, reason)
101 local host_session = hosts[host];
102 if not host_session then return nil, "The host "..tostring(host).." is not activated"; end
103 log("info", "Deactivating host: %s", host);
104 prosody_events.fire_event("host-deactivating", { host = host, host_session = host_session, reason = reason });
106 if type(reason) ~= "table" then
107 reason = { condition = "host-gone", text = tostring(reason or "This server has stopped serving "..host) };
110 -- Disconnect local users, s2s connections
111 -- TODO: These should move to mod_c2s and mod_s2s (how do they know they're being unloaded and not reloaded?)
112 if host_session.sessions then
113 for username, user in pairs(host_session.sessions) do
114 for resource, session in pairs(user.sessions) do
115 log("debug", "Closing connection for %s@%s/%s", username, host, resource);
116 session:close(reason);
120 if host_session.s2sout then
121 for remotehost, session in pairs(host_session.s2sout) do
122 if session.close then
123 log("debug", "Closing outgoing connection to %s", remotehost);
124 if session.srv_hosts then session.srv_hosts = nil; end
125 session:close(reason);
129 for remote_session in pairs(incoming_s2s) do
130 if remote_session.to_host == host then
131 log("debug", "Closing incoming connection from %s", remote_session.from_host or "<unknown>");
132 remote_session:close(reason);
136 -- TODO: This should be done in modulemanager
137 if host_session.modules then
138 for module in pairs(host_session.modules) do
139 modulemanager.unload(host, module);
144 if not host:match("[@/]") then
145 disco_items:remove(host:match("%.(.*)") or "*", host);
147 prosody_events.fire_event("host-deactivated", host);
148 log("info", "Deactivated host: %s", host);
152 function get_children(host)
153 return disco_items:get(host) or NULL;