3 -- Copyright (C) 2008-2009 Matthew Wild
4 -- Copyright (C) 2008-2009 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 --
13 CFG_CONFIGDIR=os.getenv("PROSODY_CFGDIR");
15 CFG_DATADIR=os.getenv("PROSODY_DATADIR");
17 -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- --
20 package.path = CFG_SOURCEDIR.."/?.lua;"..package.path
21 package.cpath = CFG_SOURCEDIR.."/?.so;"..package.cpath
25 if os.getenv("HOME") then
26 CFG_DATADIR = CFG_DATADIR:gsub("^~", os.getenv("HOME"));
30 -- Required to be able to find packages installed with luarocks
31 pcall(require, "luarocks.require")
34 config = require "core.configmanager"
37 -- TODO: Check for other formats when we add support for them
38 -- Use lfs? Make a new conf/ dir?
39 local ok, level, err = config.load((CFG_CONFIGDIR or ".").."/prosody.cfg.lua");
42 print("**************************");
43 if level == "parser" then
44 print("A problem occured while reading the config file "..(CFG_CONFIGDIR or ".").."/prosody.cfg.lua");
45 local err_line, err_message = tostring(err):match("%[string .-%]:(%d*): (.*)");
46 print("Error"..(err_line and (" on line "..err_line) or "")..": "..(err_message or tostring(err)));
48 elseif level == "file" then
49 print("Prosody was unable to find the configuration file.");
50 print("We looked for: "..(CFG_CONFIGDIR or ".").."/prosody.cfg.lua");
51 print("A sample config file is included in the Prosody download called prosody.cfg.lua.dist");
52 print("Copy or rename it to prosody.cfg.lua and edit as necessary.");
54 print("More help on configuring Prosody can be found at http://prosody.im/doc/configure");
56 print("**************************");
62 log = require "util.logger".init("general");
64 -- Disable log output, needs to read from config
65 -- require "util.logger".setwriter(function () end);
67 require "util.dependencies"
69 local server = require "net.server"
72 -- Maps connections to sessions --
76 -- Load and initialise core modules --
79 require "core.xmlhandlers"
80 require "core.rostermanager"
81 require "core.eventmanager"
82 require "core.hostmanager"
83 require "core.modulemanager"
84 require "core.usermanager"
85 require "core.sessionmanager"
86 require "core.stanza_router"
88 -- Commented to protect us from
89 -- the second kind of people
91 pcall(require, "remdebug.engine");
92 if remdebug then remdebug.engine.start() end
95 local cl = require "net.connlisteners";
100 ------------------------------------------------------------------------
103 ------------- Begin code without a home ---------------------
105 local data_path = config.get("*", "core", "data_path") or CFG_DATADIR or "data";
106 require "util.datamanager".set_data_path(data_path);
107 require "util.datamanager".set_callback(function(username, host, datastore)
108 return config.get(host, "core", "anonymous_login");
111 ----------- End of out-of-place code --------------
113 eventmanager.fire_event("server-starting");
115 local global_ssl_ctx = config.get("*", "core", "ssl");
116 if global_ssl_ctx then
117 local default_ssl_ctx = { mode = "server", protocol = "sslv23", capath = "/etc/ssl/certs", verify = "none"; };
118 setmetatable(global_ssl_ctx, { __index = default_ssl_ctx });
121 -- start listening on sockets
122 function net_activate_ports(option, listener, default, conntype)
123 local ports = config.get("*", "core", option) or default;
124 if type(ports) == "number" then ports = {ports} end;
126 if type(ports) ~= "table" then
127 log("error", "core."..option.." is not a table");
129 for _, port in ipairs(ports) do
130 if type(port) ~= "number" then
131 log("error", "Non-numeric "..option..": "..tostring(port));
133 cl.start(listener, { ssl = conntype ~= "tcp" and global_ssl_ctx, port = port, type = conntype });
139 net_activate_ports("c2s_ports", "xmppclient", {5222}, (global_ssl_ctx and "tls") or "tcp");
140 net_activate_ports("s2s_ports", "xmppserver", {5269}, "tcp");
141 net_activate_ports("legacy_ssl_ports", "xmppclient", {}, "ssl");
143 if config.get("*", "core", "console_enabled") then
144 if cl.get("console") then
145 cl.start("console", { interface = config.get("*", "core", "console_interface") or "127.0.0.1" })
147 log("error", "Console is enabled, but the console module appears not to be loaded");
151 -- setup error handling
152 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 });
154 eventmanager.fire_event("server-started");
157 while not quitting do
158 xpcall(server.loop, function (err)
159 if err:match("%d*: interrupted!$") then
164 log("error", "Top-level error, please report:\n%s", tostring(err));
166 local traceback = debug.traceback("", 2);
168 log("error", "%s", traceback);
171 eventmanager.fire_event("very-bad-error", "*", err, traceback);