3 -- Copyright (C) 2008-2010 Matthew Wild
4 -- Copyright (C) 2008-2010 Waqas Hussain
6 -- This project is MIT/X11 licensed. Please see the
7 -- COPYING file in the source package for more information.
10 -- Will be modified by configure script if run --
12 CFG_SOURCEDIR=os.getenv("PROSODY_SRCDIR");
13 CFG_CONFIGDIR=os.getenv("PROSODY_CFGDIR");
14 CFG_PLUGINDIR=os.getenv("PROSODY_PLUGINDIR");
15 CFG_DATADIR=os.getenv("PROSODY_DATADIR");
17 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
19 -- Tell Lua where to find our libraries
21 package.path = CFG_SOURCEDIR.."/?.lua;"..package.path;
22 package.cpath = CFG_SOURCEDIR.."/?.so;"..package.cpath;
25 package.path = package.path..";"..(CFG_SOURCEDIR or ".").."/fallbacks/?.lua";
26 package.cpath = package.cpath..";"..(CFG_SOURCEDIR or ".").."/fallbacks/?.so";
28 -- Substitute ~ with path to home directory in data path
30 if os.getenv("HOME") then
31 CFG_DATADIR = CFG_DATADIR:gsub("^~", os.getenv("HOME"));
35 -- Load the config-parsing module
36 config = require "core.configmanager"
39 -- Define the functions we call during startup, the
40 -- actual startup happens right at the end, where these
41 -- functions get called
43 function read_config()
47 if arg[1] == "--config" and arg[2] then
48 table.insert(filenames, arg[2]);
50 table.insert(filenames, CFG_CONFIGDIR.."/"..arg[2]);
53 for _, format in ipairs(config.parsers()) do
54 table.insert(filenames, (CFG_CONFIGDIR or ".").."/prosody.cfg."..format);
57 for _,_filename in ipairs(filenames) do
59 local file = io.open(filename);
62 CFG_CONFIGDIR = filename:match("^(.*)[\\/][^\\/]*$");
66 local ok, level, err = config.load(filename);
69 print("**************************");
70 if level == "parser" then
71 print("A problem occured while reading the config file "..(CFG_CONFIGDIR or ".").."/prosody.cfg.lua");
72 local err_line, err_message = tostring(err):match("%[string .-%]:(%d*): (.*)");
73 print("Error"..(err_line and (" on line "..err_line) or "")..": "..(err_message or tostring(err)));
75 elseif level == "file" then
76 print("Prosody was unable to find the configuration file.");
77 print("We looked for: "..(CFG_CONFIGDIR or ".").."/prosody.cfg.lua");
78 print("A sample config file is included in the Prosody download called prosody.cfg.lua.dist");
79 print("Copy or rename it to prosody.cfg.lua and edit as necessary.");
81 print("More help on configuring Prosody can be found at http://prosody.im/doc/configure");
83 print("**************************");
89 function load_libraries()
90 -- Load socket framework
91 server = require "net.server"
94 function init_logging()
96 require "core.loggingmanager"
99 function check_dependencies()
100 -- Check runtime dependencies
101 if not require "util.dependencies".check_dependencies() then
106 function sandbox_require()
107 -- Replace require() with one that doesn't pollute _G, required
108 -- for neat sandboxing of modules
110 local _real_require = require;
111 function require(...)
112 local curr_env = getfenv(2);
113 local curr_env_mt = getmetatable(getfenv(2));
114 local _realG_mt = getmetatable(_realG);
115 if curr_env_mt and curr_env_mt.__index and not curr_env_mt.__newindex and _realG_mt then
117 old_newindex, _realG_mt.__newindex = _realG_mt.__newindex, curr_env;
118 local ret = _real_require(...);
119 _realG_mt.__newindex = old_newindex;
122 return _real_require(...);
126 function init_global_state()
131 -- Global 'prosody' object
133 local prosody = prosody;
135 prosody.bare_sessions = bare_sessions;
136 prosody.full_sessions = full_sessions;
137 prosody.hosts = hosts;
139 prosody.paths = { source = CFG_SOURCEDIR, config = CFG_CONFIGDIR,
140 plugins = CFG_PLUGINDIR, data = CFG_DATADIR };
142 prosody.arg = _G.arg;
144 prosody.events = require "util.events".new();
146 prosody.platform = "unknown";
147 if os.getenv("WINDIR") then
148 prosody.platform = "windows";
149 elseif package.config:sub(1,1) == "/" then
150 prosody.platform = "posix";
153 prosody.installed = nil;
154 if CFG_SOURCEDIR and (prosody.platform == "windows" or CFG_SOURCEDIR:match("^/")) then
155 prosody.installed = true;
158 -- Function to reload the config file
159 function prosody.reload_config()
160 log("info", "Reloading configuration file");
161 prosody.events.fire_event("reloading-config");
162 local ok, level, err = config.load((rawget(_G, "CFG_CONFIGDIR") or ".").."/prosody.cfg.lua");
164 if level == "parser" then
165 log("error", "There was an error parsing the configuration file: %s", tostring(err));
166 elseif level == "file" then
167 log("error", "Couldn't read the config file when trying to reload: %s", tostring(err));
170 return ok, (err and tostring(level)..": "..tostring(err)) or nil;
173 -- Function to reopen logfiles
174 function prosody.reopen_logfiles()
175 log("info", "Re-opening log files");
176 eventmanager.fire_event("reopen-log-files"); -- Handled by appropriate log sinks
177 prosody.events.fire_event("reopen-log-files");
180 -- Function to initiate prosody shutdown
181 function prosody.shutdown(reason)
182 log("info", "Shutting down: %s", reason or "unknown reason");
183 prosody.shutdown_reason = reason;
184 prosody.events.fire_event("server-stopping", {reason = reason});
185 server.setquitting(true);
188 -- Load SSL settings from config, and create a ctx table
189 local certmanager = require "core.certmanager";
190 local global_ssl_ctx = certmanager.create_context("*", "server");
191 prosody.global_ssl_ctx = global_ssl_ctx;
193 local cl = require "net.connlisteners";
194 function prosody.net_activate_ports(option, listener, default, conntype)
195 conntype = conntype or (global_ssl_ctx and "tls") or "tcp";
196 local ports_option = option and option.."_ports" or "ports";
197 if not cl.get(listener) then return; end
198 local ports = config.get("*", "core", ports_option) or default;
199 if type(ports) == "number" then ports = {ports} end;
201 if type(ports) ~= "table" then
202 log("error", "core."..ports_option.." is not a table");
204 for _, port in ipairs(ports) do
205 port = tonumber(port);
206 if type(port) ~= "number" then
207 log("error", "Non-numeric "..ports_option..": "..tostring(port));
209 local ok, err = cl.start(listener, {
210 ssl = conntype == "ssl" and global_ssl_ctx,
212 interface = (option and config.get("*", "core", option.."_interface"))
213 or cl.get(listener).default_interface
214 or config.get("*", "core", "interface"),
218 local friendly_message = err;
219 if err:match(" in use") then
220 if port == 5222 or port == 5223 or port == 5269 then
221 friendly_message = "check that Prosody or another XMPP server is "
222 .."not already running and using this port";
223 elseif port == 80 or port == 81 then
224 friendly_message = "check that a HTTP server is not already using "
226 elseif port == 5280 then
227 friendly_message = "check that Prosody or a BOSH connection manager "
228 .."is not already running";
230 friendly_message = "this port is in use by another application";
232 elseif err:match("permission") then
233 friendly_message = "Prosody does not have sufficient privileges to use this port";
234 elseif err == "no ssl context" then
235 if not config.get("*", "core", "ssl") then
236 friendly_message = "there is no 'ssl' config under Host \"*\" which is "
237 .."require for legacy SSL ports";
239 friendly_message = "initializing SSL support failed, see previous log entries";
242 log("error", "Failed to open server port %d, %s", port, friendly_message);
250 function read_version()
251 -- Try to determine version
252 local version_file = io.open((CFG_SOURCEDIR or ".").."/prosody.version");
254 prosody.version = version_file:read("*a"):gsub("%s*$", "");
255 version_file:close();
256 if #prosody.version == 12 and prosody.version:match("^[a-f0-9]+$") then
257 prosody.version = "hg:"..prosody.version;
260 prosody.version = "unknown";
264 function load_secondary_libraries()
265 --- Load and initialise core modules
266 require "util.import"
267 require "core.xmlhandlers"
268 require "core.rostermanager"
269 require "core.eventmanager"
270 require "core.hostmanager"
271 require "core.modulemanager"
272 require "core.usermanager"
273 require "core.sessionmanager"
274 require "core.stanza_router"
279 require "util.datetime"
280 require "util.iterators"
282 require "util.helpers"
284 pcall(require, "util.signal") -- Not on Windows
286 -- Commented to protect us from
287 -- the second kind of people
289 pcall(require, "remdebug.engine");
290 if remdebug then remdebug.engine.start() end
293 require "net.connlisteners";
295 require "util.stanza"
299 function init_data_store()
300 local data_path = config.get("*", "core", "data_path") or CFG_DATADIR or "data";
301 require "util.datamanager".set_data_path(data_path);
302 require "util.datamanager".add_callback(function(username, host, datastore, data)
303 if config.get(host, "core", "anonymous_login") then
306 return username, host, datastore, data;
310 function prepare_to_start()
311 log("info", "Prosody is using the %s backend for connection handling", server.get_backend());
312 -- Signal to modules that we are ready to start
313 eventmanager.fire_event("server-starting");
314 prosody.events.fire_event("server-starting");
316 -- start listening on sockets
317 if config.get("*", "core", "ports") then
318 prosody.net_activate_ports(nil, "multiplex", {5222, 5269});
319 if config.get("*", "core", "ssl_ports") then
320 prosody.net_activate_ports("ssl", "multiplex", {5223}, "ssl");
323 prosody.net_activate_ports("c2s", "xmppclient", {5222});
324 prosody.net_activate_ports("s2s", "xmppserver", {5269});
325 prosody.net_activate_ports("component", "xmppcomponent", {5347}, "tcp");
326 prosody.net_activate_ports("legacy_ssl", "xmppclient", {}, "ssl");
329 prosody.start_time = os.time();
332 function init_global_protection()
333 -- Catch global accesses
334 local locked_globals_mt = {
335 __index = function (t, k) log("warn", "%s", debug.traceback("Attempt to read a non-existent global '"..tostring(k).."'", 2)); end;
336 __newindex = function (t, k, v) error("Attempt to set a global: "..tostring(k).." = "..tostring(v), 2); end;
339 function prosody.unlock_globals()
340 setmetatable(_G, nil);
343 function prosody.lock_globals()
344 setmetatable(_G, locked_globals_mt);
348 prosody.lock_globals();
352 -- Error handler for errors that make it this far
353 local function catch_uncaught_error(err)
354 if type(err) == "string" and err:match("interrupted!$") then
358 log("error", "Top-level error, please report:\n%s", tostring(err));
359 local traceback = debug.traceback("", 2);
361 log("error", "%s", traceback);
364 prosody.events.fire_event("very-bad-error", {error = err, traceback = traceback});
367 while select(2, xpcall(server.loop, catch_uncaught_error)) ~= "quitting" do
373 log("info", "Shutdown status: Cleaning up");
374 prosody.events.fire_event("server-cleanup");
376 -- Ok, we're quitting I know, but we
377 -- need to do some tidying before we go :)
378 server.setquitting(false);
380 log("info", "Shutdown status: Closing all active sessions");
381 for hostname, host in pairs(hosts) do
382 log("debug", "Shutdown status: Closing client connections for %s", hostname)
383 if host.sessions then
384 local reason = { condition = "system-shutdown", text = "Server is shutting down" };
385 if prosody.shutdown_reason then
386 reason.text = reason.text..": "..prosody.shutdown_reason;
388 for username, user in pairs(host.sessions) do
389 for resource, session in pairs(user.sessions) do
390 log("debug", "Closing connection for %s@%s/%s", username, hostname, resource);
391 session:close(reason);
396 log("debug", "Shutdown status: Closing outgoing s2s connections from %s", hostname);
398 for remotehost, session in pairs(host.s2sout) do
399 if session.close then
400 session:close("system-shutdown");
402 log("warn", "Unable to close outgoing s2s session to %s, no session:close()?!", remotehost);
408 log("info", "Shutdown status: Closing all server connections");
411 server.setquitting(true);
415 -- These actions are in a strict order, as many depend on
416 -- previous steps to have already been performed
419 check_dependencies();
424 log("info", "Hello and welcome to Prosody version %s", prosody.version);
425 load_secondary_libraries();
427 init_global_protection();
430 eventmanager.fire_event("server-started");
431 prosody.events.fire_event("server-started");
435 log("info", "Shutting down...");
437 eventmanager.fire_event("server-stopped");
438 prosody.events.fire_event("server-stopped");
439 log("info", "Shutdown complete");