3 -- Copyright (C) 2008 Matthew Wild
4 -- Copyright (C) 2008 Waqas Hussain
6 -- This program is free software; you can redistribute it and/or
7 -- modify it under the terms of the GNU General Public License
8 -- as published by the Free Software Foundation; either version 2
9 -- of the License, or (at your option) any later version.
11 -- This program is distributed in the hope that it will be useful,
12 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
13 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 -- GNU General Public License for more details.
16 -- You should have received a copy of the GNU General Public License
17 -- along with this program; if not, write to the Free Software
18 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 CFG_CONFIGDIR=os.getenv("PROSODY_CFGDIR");
26 CFG_DATADIR=os.getenv("PROSODY_DATADIR");
31 package.path = CFG_SOURCEDIR.."/?.lua;"..package.path
32 package.cpath = CFG_SOURCEDIR.."/?.so;"..package.cpath
36 if os.getenv("HOME") then
37 CFG_DATADIR = CFG_DATADIR:gsub("^~", os.getenv("HOME"));
41 -- Required to be able to find packages installed with luarocks
42 pcall(require, "luarocks.require")
45 config = require "core.configmanager"
46 log = require "util.logger".init("general");
48 -- Disable log output, needs to read from config
49 -- require "util.logger".setwriter(function () end);
52 -- TODO: Check for other formats when we add support for them
53 -- Use lfs? Make a new conf/ dir?
54 local ok, err = config.load((CFG_CONFIGDIR or ".").."/prosody.cfg.lua");
56 log("error", "Couldn't load config file: %s", err);
57 log("info", "Falling back to old config file format...")
58 ok, err = pcall(dofile, "lxmppd.cfg");
60 log("error", "Old config format loading failed too: %s", err);
62 for _, host in ipairs(_G.config.hosts) do
63 config.set(host, "core", "defined", true);
66 config.set("*", "core", "modules_enabled", _G.config.modules);
67 config.set("*", "core", "ssl", _G.config.ssl_ctx);
72 local server = require "net.server"
74 require "util.dependencies"
76 -- Maps connections to sessions --
80 -- Load and initialise core modules --
83 require "core.xmlhandlers"
84 require "core.rostermanager"
85 require "core.offlinemessage"
86 require "core.eventmanager"
87 require "core.hostmanager"
88 require "core.modulemanager"
89 require "core.usermanager"
90 require "core.sessionmanager"
91 require "core.stanza_router"
94 pcall(require, "remdebug.engine");
95 if remdebug then remdebug.engine.start() end
98 local cl = require "net.connlisteners";
100 require "util.stanza"
103 ------------------------------------------------------------------------
106 ------------- Begin code without a home ---------------------
108 local data_path = config.get("*", "core", "data_path") or CFG_DATADIR or "data";
109 require "util.datamanager".set_data_path(data_path);
111 ----------- End of out-of-place code --------------
113 eventmanager.fire_event("server-starting");
116 -- setup error handling
117 setmetatable(_G, { __index = function (t, k) error("Attempt to read a non-existent global '"..k.."'", 2); end, __newindex = function (t, k, v) error("Attempt to set a global: "..tostring(k).." = "..tostring(v), 2); end });
119 local global_ssl_ctx = config.get("*", "core", "ssl");
120 if global_ssl_ctx then
121 local default_ssl_ctx = { mode = "server", protocol = "sslv23", capath = "/etc/ssl/certs", verify = "none"; };
122 setmetatable(global_ssl_ctx, { __index = default_ssl_ctx });
125 -- start listening on sockets
126 local function do_ports(option, listener, default, conntype)
127 local ports = config.get("*", "core", option) or default;
128 --if type(ports) == "number" then ports = {ports} end;
130 if type(ports) ~= "table" then
131 log("error", "core."..option.." is not a table");
133 for _, port in ipairs(ports) do
134 if type(port) ~= "number" then
135 log("error", "Non-numeric "..option..": "..tostring(port));
137 cl.start(listener, { ssl = global_ssl_ctx, port = port, type = conntype });
143 do_ports("c2s_ports", "xmppclient", {5222}, (global_ssl_ctx and "tls") or "tcp");
144 do_ports("s2s_ports", "xmppserver", {5269}, "tcp");
145 do_ports("legacy_ssl_ports", "xmppclient", {}, "ssl");
147 if config.get("*", "core", "console_enabled") then
148 if cl.get("console") then
149 cl.start("console", { interface = config.get("*", "core", "console_interface") or "127.0.0.1" })
151 log("error", "Console is enabled, but the console module appears not to be loaded");
155 eventmanager.fire_event("server-started");