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