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