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