9458c90487649560816f7b1b07f90b7355794bd5
[prosody.git] / prosody
1 #!/usr/bin/env lua
2
3 -- Config here --
4
5 CFG_SOURCEDIR=nil;
6 CFG_CONFIGDIR=os.getenv("PROSODY_CFGDIR");
7 CFG_PLUGINDIR=nil;
8 CFG_DATADIR=os.getenv("PROSODY_DATADIR");
9
10 -- -- -- -- -- --
11
12 if CFG_SOURCEDIR then
13         package.path = CFG_SOURCEDIR.."/?.lua;"..package.path
14         package.cpath = CFG_SOURCEDIR.."/?.so;"..package.cpath
15 end
16
17 if CFG_DATADIR then
18         if os.getenv("HOME") then
19                 CFG_DATADIR = CFG_DATADIR:gsub("^~", os.getenv("HOME"));
20         end
21 end
22
23 -- Required to be able to find packages installed with luarocks
24 pcall(require, "luarocks.require")
25
26
27 config = require "core.configmanager"
28 log = require "util.logger".init("general");
29
30 do
31         -- TODO: Check for other formats when we add support for them
32         -- Use lfs? Make a new conf/ dir?
33         local ok, err = config.load((CFG_CONFIGDIR or ".").."/prosody.cfg.lua");
34         if not ok then
35                 log("error", "Couldn't load config file: %s", err);
36                 log("info", "Falling back to old config file format...")
37                 ok, err = pcall(dofile, "lxmppd.cfg");
38                 if not ok then
39                         log("error", "Old config format loading failed too: %s", err);
40                 else
41                         for _, host in ipairs(_G.config.hosts) do
42                                 config.set(host, "core", "defined", true);
43                         end
44                         
45                         config.set("*", "core", "modules_enabled", _G.config.modules);
46                         config.set("*", "core", "ssl", _G.config.ssl_ctx);
47                 end
48         end
49 end
50
51 local data_path = config.get("*", "core", "data_path") or CFG_DATADIR or "data";
52 local path_separator = "/"; if os.getenv("WINDIR") then path_separator = "\\" end
53 local _mkdir = {}
54 function mkdir(path)
55         path = path:gsub("/", path_separator);
56         --print("mkdir",path);
57         local x = io.popen("mkdir \""..path.."\" 2>&1"):read("*a");
58 end
59 function encode(s) return s and (s:gsub("%W", function (c) return string.format("%%%x", c:byte()); end)); end
60 function mkdirs(host)
61         if not _mkdir[host] then
62                 local host_dir = string.format("%s/%s", data_path, encode(host));
63                 mkdir(host_dir);
64                 mkdir(host_dir.."/accounts");
65                 mkdir(host_dir.."/vcard");
66                 mkdir(host_dir.."/roster");
67                 mkdir(host_dir.."/private");
68                 mkdir(host_dir.."/offline");
69                 _mkdir[host] = true;
70         end
71 end
72 mkdir(data_path);
73
74 require "util.datamanager".set_data_path(data_path);
75
76 local server = require "net.server"
77
78 require "util.dependencies"
79
80 -- Maps connections to sessions --
81 sessions = {};
82 hosts = {};
83
84 local defined_hosts = config.getconfig();
85
86 for host, host_config in pairs(defined_hosts) do
87         if host ~= "*" and (host_config.core.enabled == nil or host_config.core.enabled) then
88                 hosts[host] = {type = "local", connected = true, sessions = {}, host = host, s2sout = {} };
89                 mkdirs(host);
90         end
91 end
92
93 -- Load and initialise core modules --
94
95 require "util.import"
96 require "core.xmlhandlers"
97 require "core.rostermanager"
98 require "core.offlinemessage"
99 require "core.modulemanager"
100 require "core.usermanager"
101 require "core.sessionmanager"
102 require "core.stanza_router"
103
104 --[[
105 pcall(require, "remdebug.engine");
106 if remdebug then remdebug.engine.start() end
107 ]]
108
109 local start = require "net.connlisteners".start;
110 require "util.stanza"
111 require "util.jid"
112
113 ------------------------------------------------------------------------
114
115 -- Initialise modules
116
117 for host in pairs(hosts) do
118         if host ~= "*" then
119                 local modules_enabled = config.get(host, "core", "modules_enabled");
120                 if modules_enabled then
121                         for _, module in pairs(modules_enabled) do
122                                 modulemanager.load(host, module);
123                         end
124                 end
125         end
126 end
127
128 -- setup error handling
129 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 }) --]][][[]][];
130
131 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;
132 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;
133
134
135 local global_ssl_ctx = config.get("*", "core", "ssl");
136 if global_ssl_ctx then
137         local default_ssl_ctx = { mode = "server", protocol = "sslv23", capath = "/etc/ssl/certs", verify = "none"; };
138         setmetatable(global_ssl_ctx, { __index = default_ssl_ctx });
139 end
140
141 -- start listening on sockets
142 start("xmppclient", { ssl = global_ssl_ctx })
143 start("xmppserver", { ssl = global_ssl_ctx })
144
145 if config.get("*", "core", "console_enabled") then
146         start("console")
147 end
148
149 modulemanager.fire_event("server-started");
150
151 server.loop();