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;
17 local log = require "util.logger".init("hostmanager");
19 local hosts = prosody.hosts;
20 local prosody_events = prosody.events;
21 if not _G.prosody.incoming_s2s then
22 require "core.s2smanager";
24 local incoming_s2s = _G.prosody.incoming_s2s;
25 local core_route_stanza = _G.prosody.core_route_stanza;
27 local pairs, select, rawget = pairs, select, rawget;
28 local tostring, type = tostring, type;
29 local setmetatable = setmetatable;
34 function host_mt:__tostring()
35 if self.type == "component" then
36 local typ = configmanager.get(self.host, "component_module");
37 if typ == "component" then
38 return ("Component %q"):format(self.host);
40 return ("Component %q %q"):format(self.host, typ);
41 elseif self.type == "local" then
42 return ("VirtualHost %q"):format(self.host);
46 local hosts_loaded_once;
48 local activate, deactivate;
50 local function load_enabled_hosts(config)
51 local defined_hosts = config or configmanager.getconfig();
52 local activated_any_host;
54 for host, host_config in pairs(defined_hosts) do
55 if host ~= "*" and host_config.enabled ~= false then
56 if not host_config.component_module then
57 activated_any_host = true;
59 activate(host, host_config);
63 if not activated_any_host then
64 log("error", "No active VirtualHost entries in the config file. This may cause unexpected behaviour as no modules will be loaded.");
67 prosody_events.fire_event("hosts-activated", defined_hosts);
68 hosts_loaded_once = true;
71 prosody_events.add_handler("server-starting", load_enabled_hosts);
73 local function host_send(stanza)
74 core_route_stanza(nil, stanza);
77 function activate(host, host_config)
78 if rawget(hosts, host) then return nil, "The host "..host.." is already activated"; end
79 host_config = host_config or configmanager.getconfig()[host];
80 if not host_config then return nil, "Couldn't find the host "..tostring(host).." defined in the current config"; end
81 local host_session = {
84 events = events_new();
88 setmetatable(host_session, host_mt);
89 if not host_config.component_module then -- host
90 host_session.type = "local";
91 host_session.sessions = {};
93 host_session.type = "component";
95 hosts[host] = host_session;
96 if not host:match("[@/]") then
97 disco_items:set(host:match("%.(.*)") or "*", host, host_config.name or true);
99 for option_name in pairs(host_config) do
100 if option_name:match("_ports$") or option_name:match("_interface$") then
101 log("warn", "%s: Option '%s' has no effect for virtual hosts - put it in the server-wide section instead", host, option_name);
105 log((hosts_loaded_once and "info") or "debug", "Activated host: %s", host);
106 prosody_events.fire_event("host-activated", host);
110 function deactivate(host, reason)
111 local host_session = hosts[host];
112 if not host_session then return nil, "The host "..tostring(host).." is not activated"; end
113 log("info", "Deactivating host: %s", host);
114 prosody_events.fire_event("host-deactivating", { host = host, host_session = host_session, reason = reason });
116 if type(reason) ~= "table" then
117 reason = { condition = "host-gone", text = tostring(reason or "This server has stopped serving "..host) };
120 -- Disconnect local users, s2s connections
121 -- TODO: These should move to mod_c2s and mod_s2s (how do they know they're being unloaded and not reloaded?)
122 if host_session.sessions then
123 for username, user in pairs(host_session.sessions) do
124 for resource, session in pairs(user.sessions) do
125 log("debug", "Closing connection for %s@%s/%s", username, host, resource);
126 session:close(reason);
130 if host_session.s2sout then
131 for remotehost, session in pairs(host_session.s2sout) do
132 if session.close then
133 log("debug", "Closing outgoing connection to %s", remotehost);
134 if session.srv_hosts then session.srv_hosts = nil; end
135 session:close(reason);
139 for remote_session in pairs(incoming_s2s) do
140 if remote_session.to_host == host then
141 log("debug", "Closing incoming connection from %s", remote_session.from_host or "<unknown>");
142 remote_session:close(reason);
146 -- TODO: This should be done in modulemanager
147 if host_session.modules then
148 for module in pairs(host_session.modules) do
149 modulemanager.unload(host, module);
154 if not host:match("[@/]") then
155 disco_items:remove(host:match("%.(.*)") or "*", host);
157 prosody_events.fire_event("host-deactivated", host);
158 log("info", "Deactivated host: %s", host);
162 local function get_children(host)
163 return disco_items:get(host) or NULL;
168 deactivate = deactivate;
169 get_children = get_children;