12 if os.getenv("HOME") then
13 CFG_SOURCEDIR = CFG_SOURCEDIR:gsub("^~", os.getenv("HOME"));
15 package.path = CFG_SOURCEDIR.."/?.lua;"..package.path
16 package.cpath = CFG_SOURCEDIR.."/?.so;"..package.cpath
20 if os.getenv("HOME") then
21 CFG_CONFIGDIR = CFG_CONFIGDIR:gsub("^~", os.getenv("HOME"));
26 if os.getenv("HOME") then
27 CFG_PLUGINDIR = CFG_PLUGINDIR:gsub("^~", os.getenv("HOME"));
31 -- Required to be able to find packages installed with luarocks
32 pcall(require, "luarocks.require")
35 config = require "core.configmanager"
36 log = require "util.logger".init("general");
39 -- TODO: Check for other formats when we add support for them
40 -- Use lfs? Make a new conf/ dir?
41 local ok, err = config.load((CFG_CONFIGDIR or ".").."/prosody.cfg.lua");
43 log("error", "Couldn't load config file: %s", err);
44 log("info", "Falling back to old config file format...")
45 ok, err = pcall(dofile, "lxmppd.cfg");
47 log("error", "Old config format loading failed too: %s", err);
49 for _, host in ipairs(_G.config.hosts) do
50 config.set(host, "core", "defined", true);
53 config.set("*", "core", "modules_enabled", _G.config.modules);
54 config.set("*", "core", "ssl", _G.config.ssl_ctx);
59 local data_path = config.get("*", "core", "data_path") or "data";
60 local path_separator = "/"; if os.getenv("WINDIR") then path_separator = "\\" end
63 path = path:gsub("/", path_separator);
64 --print("mkdir",path);
65 local x = io.popen("mkdir "..path.." 2>&1"):read("*a");
67 function encode(s) return s and (s:gsub("%W", function (c) return string.format("%%%x", c:byte()); end)); end
69 if not _mkdir[host] then
70 local host_dir = string.format("%s/%s", data_path, encode(host));
72 mkdir(host_dir.."/accounts");
73 mkdir(host_dir.."/vcard");
74 mkdir(host_dir.."/roster");
75 mkdir(host_dir.."/private");
76 mkdir(host_dir.."/offline");
82 require "util.datamanager".set_data_path(data_path);
84 local server = require "net.server"
86 require "util.dependencies"
88 -- Maps connections to sessions --
92 local defined_hosts = config.getconfig();
94 for host, host_config in pairs(defined_hosts) do
95 if host ~= "*" and (host_config.core.enabled == nil or host_config.core.enabled) then
96 hosts[host] = {type = "local", connected = true, sessions = {}, host = host, s2sout = {} };
101 -- Load and initialise core modules --
103 require "util.import"
104 require "core.xmlhandlers"
105 require "core.rostermanager"
106 require "core.offlinemessage"
107 require "core.modulemanager"
108 require "core.usermanager"
109 require "core.sessionmanager"
110 require "core.stanza_router"
113 pcall(require, "remdebug.engine");
114 if remdebug then remdebug.engine.start() end
117 local start = require "net.connlisteners".start;
118 require "util.stanza"
121 ------------------------------------------------------------------------
123 -- Initialise modules
125 for host in pairs(hosts) do
127 local modules_enabled = config.get(host, "core", "modules_enabled");
128 if modules_enabled then
129 for _, module in pairs(modules_enabled) do
130 modulemanager.load(host, module);
136 -- setup error handling
137 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 }) --]][][[]][];
139 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;
140 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;
143 local global_ssl_ctx = config.get("*", "core", "ssl");
144 if global_ssl_ctx then
145 local default_ssl_ctx = { mode = "server", protocol = "sslv23", capath = "/etc/ssl/certs", verify = "none"; };
146 setmetatable(global_ssl_ctx, { __index = default_ssl_ctx });
149 -- start listening on sockets
150 start("xmppclient", { ssl = global_ssl_ctx })
151 start("xmppserver", { ssl = global_ssl_ctx })
153 if config.get("*", "core", "console_enabled") then
157 modulemanager.fire_event("server-started");