06ba72a100c5d3ebdf73fa80bf14707a7b065703
[prosody.git] / core / hostmanager.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 -- 
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
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();
13 local NULL = {};
14
15 local jid_split = require "util.jid".split;
16 local uuid_gen = require "util.uuid".generate;
17
18 local log = require "util.logger".init("hostmanager");
19
20 local hosts = prosody.hosts;
21 local prosody_events = prosody.events;
22 if not _G.prosody.incoming_s2s then
23         require "core.s2smanager";
24 end
25 local incoming_s2s = _G.prosody.incoming_s2s;
26 local core_route_stanza = _G.prosody.core_route_stanza;
27
28 local pairs, select, rawget = pairs, select, rawget;
29 local tostring, type = tostring, type;
30
31 module "hostmanager"
32
33 local hosts_loaded_once;
34
35 local function load_enabled_hosts(config)
36         local defined_hosts = config or configmanager.getconfig();
37         local activated_any_host;
38         
39         for host, host_config in pairs(defined_hosts) do
40                 if host ~= "*" and host_config.enabled ~= false then
41                         if not host_config.component_module then
42                                 activated_any_host = true;
43                         end
44                         activate(host, host_config);
45                 end
46         end
47         
48         if not activated_any_host then
49                 log("error", "No active VirtualHost entries in the config file. This may cause unexpected behaviour as no modules will be loaded.");
50         end
51         
52         prosody_events.fire_event("hosts-activated", defined_hosts);
53         hosts_loaded_once = true;
54 end
55
56 prosody_events.add_handler("server-starting", load_enabled_hosts);
57
58 local function host_send(stanza)
59         local name, type = stanza.name, stanza.attr.type;
60         if type == "error" or (name == "iq" and type == "result") then
61                 local dest_host_name = select(2, jid_split(stanza.attr.to));
62                 local dest_host = hosts[dest_host_name] or { type = "unknown" };
63                 log("warn", "Unhandled response sent to %s host %s: %s", dest_host.type, dest_host_name, tostring(stanza));
64                 return;
65         end
66         core_route_stanza(nil, stanza);
67 end
68
69 function activate(host, host_config)
70         if rawget(hosts, host) then return nil, "The host "..host.." is already activated"; end
71         host_config = host_config or configmanager.getconfig()[host];
72         if not host_config then return nil, "Couldn't find the host "..tostring(host).." defined in the current config"; end
73         local host_session = {
74                 host = host;
75                 s2sout = {};
76                 events = events_new();
77                 dialback_secret = configmanager.get(host, "dialback_secret") or uuid_gen();
78                 send = host_send;
79                 modules = {};
80         };
81         if not host_config.component_module then -- host
82                 host_session.type = "local";
83                 host_session.sessions = {};
84         else -- component
85                 host_session.type = "component";
86         end
87         hosts[host] = host_session;
88         if not host:match("[@/]") then
89                 disco_items:set(host:match("%.(.*)") or "*", host, host_config.name or true);
90         end
91         for option_name in pairs(host_config) do
92                 if option_name:match("_ports$") or option_name:match("_interface$") then
93                         log("warn", "%s: Option '%s' has no effect for virtual hosts - put it in the server-wide section instead", host, option_name);
94                 end
95         end
96         
97         log((hosts_loaded_once and "info") or "debug", "Activated host: %s", host);
98         prosody_events.fire_event("host-activated", host);
99         return true;
100 end
101
102 function deactivate(host, reason)
103         local host_session = hosts[host];
104         if not host_session then return nil, "The host "..tostring(host).." is not activated"; end
105         log("info", "Deactivating host: %s", host);
106         prosody_events.fire_event("host-deactivating", { host = host, host_session = host_session, reason = reason });
107         
108         if type(reason) ~= "table" then
109                 reason = { condition = "host-gone", text = tostring(reason or "This server has stopped serving "..host) };
110         end
111         
112         -- Disconnect local users, s2s connections
113         -- TODO: These should move to mod_c2s and mod_s2s (how do they know they're being unloaded and not reloaded?)
114         if host_session.sessions then
115                 for username, user in pairs(host_session.sessions) do
116                         for resource, session in pairs(user.sessions) do
117                                 log("debug", "Closing connection for %s@%s/%s", username, host, resource);
118                                 session:close(reason);
119                         end
120                 end
121         end
122         if host_session.s2sout then
123                 for remotehost, session in pairs(host_session.s2sout) do
124                         if session.close then
125                                 log("debug", "Closing outgoing connection to %s", remotehost);
126                                 if session.srv_hosts then session.srv_hosts = nil; end
127                                 session:close(reason);
128                         end
129                 end
130         end
131         for remote_session in pairs(incoming_s2s) do
132                 if remote_session.to_host == host then
133                         log("debug", "Closing incoming connection from %s", remote_session.from_host or "<unknown>");
134                         remote_session:close(reason);
135                 end
136         end
137
138         -- TODO: This should be done in modulemanager
139         if host_session.modules then
140                 for module in pairs(host_session.modules) do
141                         modulemanager.unload(host, module);
142                 end
143         end
144
145         hosts[host] = nil;
146         if not host:match("[@/]") then
147                 disco_items:remove(host:match("%.(.*)") or "*", host);
148         end
149         prosody_events.fire_event("host-deactivated", host);
150         log("info", "Deactivated host: %s", host);
151         return true;
152 end
153
154 function get_children(host)
155         return disco_items:get(host) or NULL;
156 end
157
158 return _M;