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