Automated merge.
[prosody.git] / prosody
1 #!/usr/bin/env lua
2
3 -- Config here --
4
5 CFG_SOURCEDIR=nil;
6 CFG_CONFIGDIR=nil;
7 CFG_PLUGINDIR=nil;
8
9 -- -- -- -- -- --
10
11 if CFG_SOURCEDIR then
12         if os.getenv("HOME") then
13                 CFG_SOURCEDIR = CFG_SOURCEDIR:gsub("^~", os.getenv("HOME"));
14         end
15         package.path = CFG_SOURCEDIR.."/?.lua;"..package.path
16         package.cpath = CFG_SOURCEDIR.."/?.so;"..package.cpath
17 end
18
19 if CFG_CONFIGDIR then
20         if os.getenv("HOME") then
21                 CFG_CONFIGDIR = CFG_CONFIGDIR:gsub("^~", os.getenv("HOME"));
22         end
23 end     
24
25 if CFG_PLUGINDIR then
26         if os.getenv("HOME") then
27                 CFG_PLUGINDIR = CFG_PLUGINDIR:gsub("^~", os.getenv("HOME"));
28         end
29 end     
30
31 -- Required to be able to find packages installed with luarocks
32 pcall(require, "luarocks.require")
33
34
35 config = require "core.configmanager"
36 log = require "util.logger".init("general");
37
38 do
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");
42         if not ok then
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");
46                 if not ok then
47                         log("error", "Old config format loading failed too: %s", err);
48                 else
49                         for _, host in ipairs(_G.config.hosts) do
50                                 config.set(host, "core", "defined", true);
51                         end
52                         
53                         config.set("*", "core", "modules_enabled", _G.config.modules);
54                         config.set("*", "core", "ssl", _G.config.ssl_ctx);
55                 end
56         end
57 end
58
59 require "util.datamanager".set_data_path(config.get("*", "core", "data_path") or "data");
60
61 local server = require "net.server"
62
63 require "util.dependencies"
64
65 -- Maps connections to sessions --
66 sessions = {};
67 hosts = {};
68
69 local defined_hosts = config.getconfig();
70
71 for host, host_config in pairs(defined_hosts) do
72         if host ~= "*" and (host_config.core.enabled == nil or host_config.core.enabled) then
73                 hosts[host] = {type = "local", connected = true, sessions = {}, host = host, s2sout = {} };
74         end
75 end
76
77 -- Load and initialise core modules --
78
79 require "util.import"
80 require "core.xmlhandlers"
81 require "core.rostermanager"
82 require "core.offlinemessage"
83 require "core.modulemanager"
84 require "core.usermanager"
85 require "core.sessionmanager"
86 require "core.stanza_router"
87
88 --[[
89 pcall(require, "remdebug.engine");
90 if remdebug then remdebug.engine.start() end
91 ]]
92
93 local start = require "net.connlisteners".start;
94 require "util.stanza"
95 require "util.jid"
96
97 ------------------------------------------------------------------------
98
99 -- Initialise modules
100
101 for host in pairs(hosts) do
102         if host ~= "*" then
103                 local modules_enabled = config.get(host, "core", "modules_enabled");
104                 if modules_enabled then
105                         for _, module in pairs(modules_enabled) do
106                                 modulemanager.load(host, module);
107                         end
108                 end
109         end
110 end
111
112 -- setup error handling
113 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 }) --]][][[]][];
114
115 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;
116 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;
117
118
119 local global_ssl_ctx = config.get("*", "core", "ssl");
120 if global_ssl_ctx then
121         local default_ssl_ctx = { mode = "server", protocol = "sslv23", capath = "/etc/ssl/certs", verify = "none"; };
122         setmetatable(global_ssl_ctx, { __index = default_ssl_ctx });
123 end
124
125 -- start listening on sockets
126 start("xmppclient", { ssl = global_ssl_ctx })
127 start("xmppserver", { ssl = global_ssl_ctx })
128
129 if config.get("*", "core", "console_enabled") then
130         start("console")
131 end
132
133 modulemanager.fire_event("server-started");
134
135 server.loop();