main.lua

changeset 376
6d87944df37c
parent 359
8fbfa8f885a6
child 381
e967f162501e
equal deleted inserted replaced
375:a6a4ea3633b0 376:6d87944df37c
2 2
3 local server = require "net.server" 3 local server = require "net.server"
4 require "lxp" 4 require "lxp"
5 require "socket" 5 require "socket"
6 require "ssl" 6 require "ssl"
7 local config = require "core.configmanager"
7 8
8 function log(type, area, message) 9 log = require "util.logger".init("general");
9 print(type, area, message); 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
10 end 30 end
11
12 dofile "lxmppd.cfg"
13 31
14 -- Maps connections to sessions -- 32 -- Maps connections to sessions --
15 sessions = {}; 33 sessions = {};
16 hosts = {}; 34 hosts = {};
17 35
18 if config.hosts and #config.hosts > 0 then 36 local defined_hosts = config.getconfig();
19 for _, host in pairs(config.hosts) do 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
20 hosts[host] = {type = "local", connected = true, sessions = {}, host = host, s2sout = {} }; 40 hosts[host] = {type = "local", connected = true, sessions = {}, host = host, s2sout = {} };
21 end 41 end
22 else error("No hosts defined in the configuration file"); end 42 end
23 43
24 -- Load and initialise core modules -- 44 -- Load and initialise core modules --
25 45
26 require "util.import" 46 require "util.import"
27 require "core.xmlhandlers" 47 require "core.xmlhandlers"
40 require "util.jid" 60 require "util.jid"
41 61
42 ------------------------------------------------------------------------ 62 ------------------------------------------------------------------------
43 63
44 -- Initialise modules 64 -- Initialise modules
45 if config.modules and #config.modules > 0 then 65 local modules_enabled = config.get("*", "core", "modules_enabled");
46 for _, module in pairs(config.modules) do 66 if modules_enabled then
67 for _, module in pairs(modules_enabled) do
47 modulemanager.load(module); 68 modulemanager.load(module);
48 end 69 end
49 else error("No modules enabled in the configuration file"); end 70 end
50 71
51 -- setup error handling 72 -- setup error handling
52 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 }) --]][][[]][]; 73 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 }) --]][][[]][];
53 74
54 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; 75 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;
55 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; 76 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;
56 77
78
79 local global_ssl_ctx = config.get("*", "core", "ssl");
80 if global_ssl_ctx then
81 local default_ssl_ctx = { mode = "server", protocol = "sslv23", capath = "/etc/ssl/certs", verify = "none"; };
82 setmetatable(global_ssl_ctx, { __index = default_ssl_ctx });
83 end
84
57 -- start listening on sockets 85 -- start listening on sockets
58 start("xmppclient", { ssl = config.ssl_ctx }) 86 start("xmppclient", { ssl = global_ssl_ctx })
59 start("xmppserver", { ssl = config.ssl_ctx }) 87 start("xmppserver", { ssl = global_ssl_ctx })
60 88
61 modulemanager.fire_event("server-started"); 89 modulemanager.fire_event("server-started");
62 90
63 server.loop(); 91 server.loop();

mercurial