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;
13 local uuid_gen = require "util.uuid".generate;
15 local log = require "util.logger".init("hostmanager");
18 local prosody_events = prosody.events;
19 if not _G.prosody.incoming_s2s then
20 require "core.s2smanager";
22 local incoming_s2s = _G.prosody.incoming_s2s;
24 local pairs, setmetatable = pairs, setmetatable;
28 local hosts_loaded_once;
30 local function load_enabled_hosts(config)
31 local defined_hosts = config or configmanager.getconfig();
32 local activated_any_host;
34 for host, host_config in pairs(defined_hosts) do
35 if host ~= "*" and host_config.core.enabled ~= false then
36 if not host_config.core.component_module then
37 activated_any_host = true;
39 activate(host, host_config);
43 if not activated_any_host then
44 log("error", "No active VirtualHost entries in the config file. This may cause unexpected behaviour as no modules will be loaded.");
47 prosody_events.fire_event("hosts-activated", defined_hosts);
48 hosts_loaded_once = true;
51 prosody_events.add_handler("server-starting", load_enabled_hosts);
53 function activate(host, host_config)
54 if hosts[host] then return nil, "host-already-exists"; end
55 local host_session = {
58 events = events_new();
59 dialback_secret = configmanager.get(host, "core", "dialback_secret") or uuid_gen();
60 disallow_s2s = configmanager.get(host, "core", "disallow_s2s");
62 if not host_config.core.component_module then -- host
63 host_session.type = "local";
64 host_session.sessions = {};
65 if configmanager.get(host, "core", "anonymous_login") then
66 host_session.disallow_s2s = (configmanager.get(host, "core", "disallow_s2s") ~= false);
69 host_session.type = "component";
71 hosts[host] = host_session;
72 for option_name in pairs(host_config.core) do
73 if option_name:match("_ports$") or option_name:match("_interface$") then
74 log("warn", "%s: Option '%s' has no effect for virtual hosts - put it in the server-wide section instead", host, option_name);
78 log((hosts_loaded_once and "info") or "debug", "Activated host: %s", host);
79 prosody_events.fire_event("host-activated", host, host_config);
83 function deactivate(host, reason)
84 local host_session = hosts[host];
85 log("info", "Deactivating host: %s", host);
86 prosody_events.fire_event("host-deactivating", host, host_session);
88 reason = reason or { condition = "host-gone", text = "This server has stopped serving "..host };
90 -- Disconnect local users, s2s connections
91 if host_session.sessions then
92 for username, user in pairs(host_session.sessions) do
93 for resource, session in pairs(user.sessions) do
94 log("debug", "Closing connection for %s@%s/%s", username, host, resource);
95 session:close(reason);
99 if host_session.s2sout then
100 for remotehost, session in pairs(host_session.s2sout) do
101 if session.close then
102 log("debug", "Closing outgoing connection to %s", remotehost);
103 if session.srv_hosts then session.srv_hosts = nil; end
104 session:close(reason);
108 for remote_session in pairs(incoming_s2s) do
109 if remote_session.to_host == host then
110 log("debug", "Closing incoming connection from %s", remote_session.from_host or "<unknown>");
111 remote_session:close(reason);
115 if host_session.modules then
116 for module in pairs(host_session.modules) do
117 modulemanager.unload(host, module);
122 prosody_events.fire_event("host-deactivated", host);
123 log("info", "Deactivated host: %s", host);
126 function getconfig(name)