97c97c5cdf1d2c33586018c4f16b4e94a136f6d2
[prosody.git] / main.lua
1 pcall(require, "luarocks.require")
2
3 local server = require "net.server"
4 require "lxp"
5 require "socket"
6 require "ssl"
7 local config = require "core.configmanager"
8
9 log = require "util.logger".init("general");
10
11 do
12         -- TODO: Check for other formats when we add support for them
13         -- Use lfs? Make a new conf/ dir?
14         local ok, err = config.load("lxmppd.cfg.lua");
15         if not ok then
16                 log("error", "Couldn't load config file: %s", err);
17                 log("info", "Falling back to old config file format...")
18                 ok, err = pcall(dofile, "lxmppd.cfg");
19                 if not ok then
20                         log("error", "Old config format loading failed too: %s", err);
21                 else
22                         for _, host in ipairs(_G.config.hosts) do
23                                 config.set(host, "core", "defined", true);
24                         end
25                         
26                         config.set("*", "core", "modules_enabled", _G.config.modules);
27                         config.set("*", "core", "ssl", _G.config.ssl_ctx);
28                 end
29         end
30 end
31
32 -- Maps connections to sessions --
33 sessions = {};
34 hosts = {};
35
36 local defined_hosts = config.getconfig();
37
38 for host, host_config in pairs(defined_hosts) do
39         if host ~= "*" and (host_config.core.enabled == nil or host_config.core.enabled) then
40                 hosts[host] = {type = "local", connected = true, sessions = {}, host = host, s2sout = {} };
41         end
42 end
43
44 -- Load and initialise core modules --
45
46 require "util.import"
47 require "core.xmlhandlers"
48 require "core.rostermanager"
49 require "core.offlinemessage"
50 require "core.modulemanager"
51 require "core.usermanager"
52 require "core.sessionmanager"
53 require "core.stanza_router"
54
55 --[[
56 pcall(require, "remdebug.engine");
57 if remdebug then remdebug.engine.start() end
58 ]]
59
60 local start = require "net.connlisteners".start;
61 require "util.stanza"
62 require "util.jid"
63
64 ------------------------------------------------------------------------
65
66 -- Initialise modules
67 local modules_enabled = config.get("*", "core", "modules_enabled");
68 if modules_enabled then
69         for _, module in pairs(modules_enabled) do
70                 modulemanager.load(module);
71         end
72 end
73
74 -- setup error handling
75 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 }) --]][][[]][];
76
77 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;
78 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;
79
80
81 local global_ssl_ctx = config.get("*", "core", "ssl");
82 if global_ssl_ctx then
83         local default_ssl_ctx = { mode = "server", protocol = "sslv23", capath = "/etc/ssl/certs", verify = "none"; };
84         setmetatable(global_ssl_ctx, { __index = default_ssl_ctx });
85 end
86
87 -- start listening on sockets
88 start("xmppclient", { ssl = global_ssl_ctx })
89 start("xmppserver", { ssl = global_ssl_ctx })
90
91 modulemanager.fire_event("server-started");
92
93 server.loop();