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.
26 CFG_CONFIGDIR=os.getenv("PROSODY_CFGDIR");
28 CFG_DATADIR=os.getenv("PROSODY_DATADIR");
33 package.path = CFG_SOURCEDIR.."/?.lua;"..package.path
34 package.cpath = CFG_SOURCEDIR.."/?.so;"..package.cpath
38 if os.getenv("HOME") then
39 CFG_DATADIR = CFG_DATADIR:gsub("^~", os.getenv("HOME"));
43 -- Required to be able to find packages installed with luarocks
44 pcall(require, "luarocks.require")
47 config = require "core.configmanager"
48 log = require "util.logger".init("general");
51 -- TODO: Check for other formats when we add support for them
52 -- Use lfs? Make a new conf/ dir?
53 local ok, err = config.load((CFG_CONFIGDIR or ".").."/prosody.cfg.lua");
55 log("error", "Couldn't load config file: %s", err);
56 log("info", "Falling back to old config file format...")
57 ok, err = pcall(dofile, "lxmppd.cfg");
59 log("error", "Old config format loading failed too: %s", err);
61 for _, host in ipairs(_G.config.hosts) do
62 config.set(host, "core", "defined", true);
65 config.set("*", "core", "modules_enabled", _G.config.modules);
66 config.set("*", "core", "ssl", _G.config.ssl_ctx);
71 local data_path = config.get("*", "core", "data_path") or CFG_DATADIR or "data";
72 local path_separator = "/"; if os.getenv("WINDIR") then path_separator = "\\" end
75 path = path:gsub("/", path_separator);
76 --print("mkdir",path);
77 local x = io.popen("mkdir \""..path.."\" 2>&1"):read("*a");
79 function encode(s) return s and (s:gsub("%W", function (c) return string.format("%%%x", c:byte()); end)); end
81 if not _mkdir[host] then
82 local host_dir = string.format("%s/%s", data_path, encode(host));
84 mkdir(host_dir.."/accounts");
85 mkdir(host_dir.."/vcard");
86 mkdir(host_dir.."/roster");
87 mkdir(host_dir.."/private");
88 mkdir(host_dir.."/offline");
94 require "util.datamanager".set_data_path(data_path);
96 local server = require "net.server"
98 require "util.dependencies"
100 -- Maps connections to sessions --
104 local defined_hosts = config.getconfig();
106 for host, host_config in pairs(defined_hosts) do
107 if host ~= "*" and (host_config.core.enabled == nil or host_config.core.enabled) then
108 hosts[host] = {type = "local", connected = true, sessions = {}, host = host, s2sout = {} };
113 -- Load and initialise core modules --
115 require "util.import"
116 require "core.xmlhandlers"
117 require "core.rostermanager"
118 require "core.offlinemessage"
119 require "core.modulemanager"
120 require "core.usermanager"
121 require "core.sessionmanager"
122 require "core.stanza_router"
125 pcall(require, "remdebug.engine");
126 if remdebug then remdebug.engine.start() end
129 local start = require "net.connlisteners".start;
130 require "util.stanza"
133 ------------------------------------------------------------------------
135 -- Initialise modules
137 for host in pairs(hosts) do
139 local modules_enabled = config.get(host, "core", "modules_enabled");
140 if modules_enabled then
141 for _, module in pairs(modules_enabled) do
142 modulemanager.load(host, module);
148 -- setup error handling
149 setmetatable(_G, { __index = function (t, k) print("WARNING: ATTEMPT TO READ A NIL GLOBAL!!!", k); error("Attempt to read a non-existent global. Naughty boy.", 2); end, __newindex = function (t, k, v) print("ATTEMPT TO SET A GLOBAL!!!!", tostring(k).." = "..tostring(v)); error("Attempt to set a global. Naughty boy.", 2); end }) --]][][[]][];
151 local protected_handler = function (conn, data, err) local success, ret = pcall(handler, conn, data, err); if not success then print("ERROR on "..tostring(conn)..": "..ret); conn:close(); end end;
152 local protected_disconnect = function (conn, err) local success, ret = pcall(disconnect, conn, err); if not success then print("ERROR on "..tostring(conn).." disconnect: "..ret); conn:close(); end end;
155 local global_ssl_ctx = config.get("*", "core", "ssl");
156 if global_ssl_ctx then
157 local default_ssl_ctx = { mode = "server", protocol = "sslv23", capath = "/etc/ssl/certs", verify = "none"; };
158 setmetatable(global_ssl_ctx, { __index = default_ssl_ctx });
161 -- start listening on sockets
162 start("xmppclient", { ssl = global_ssl_ctx })
163 start("xmppserver", { ssl = global_ssl_ctx })
165 if config.get("*", "core", "console_enabled") then
169 modulemanager.fire_event("server-started");