77f72e7ff1c12f51e61d5757ea0d2071f50638de
[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 local data_path = config.get("*", "core", "data_path") or "data";
60 local path_separator = "/"; if os.getenv("WINDIR") then path_separator = "\\" end
61 local _mkdir = {}
62 function mkdir(path)
63         path = path:gsub("/", path_separator);
64         --print("mkdir",path);
65         local x = io.popen("mkdir "..path.." 2>&1"):read("*a");
66 end
67 function encode(s) return s and (s:gsub("%W", function (c) return string.format("%%%x", c:byte()); end)); end
68 function mkdirs(host)
69         if not _mkdir[host] then
70                 local host_dir = string.format("%s/%s", data_path, encode(host));
71                 mkdir(host_dir);
72                 mkdir(host_dir.."/accounts");
73                 mkdir(host_dir.."/vcard");
74                 mkdir(host_dir.."/roster");
75                 mkdir(host_dir.."/private");
76                 mkdir(host_dir.."/offline");
77                 _mkdir[host] = true;
78         end
79 end
80 mkdir(data_path);
81
82 require "util.datamanager".set_data_path(data_path);
83
84 local server = require "net.server"
85
86 require "util.dependencies"
87
88 -- Maps connections to sessions --
89 sessions = {};
90 hosts = {};
91
92 local defined_hosts = config.getconfig();
93
94 for host, host_config in pairs(defined_hosts) do
95         if host ~= "*" and (host_config.core.enabled == nil or host_config.core.enabled) then
96                 hosts[host] = {type = "local", connected = true, sessions = {}, host = host, s2sout = {} };
97                 mkdirs(host);
98         end
99 end
100
101 -- Load and initialise core modules --
102
103 require "util.import"
104 require "core.xmlhandlers"
105 require "core.rostermanager"
106 require "core.offlinemessage"
107 require "core.modulemanager"
108 require "core.usermanager"
109 require "core.sessionmanager"
110 require "core.stanza_router"
111
112 --[[
113 pcall(require, "remdebug.engine");
114 if remdebug then remdebug.engine.start() end
115 ]]
116
117 local start = require "net.connlisteners".start;
118 require "util.stanza"
119 require "util.jid"
120
121 ------------------------------------------------------------------------
122
123 -- Initialise modules
124
125 for host in pairs(hosts) do
126         if host ~= "*" then
127                 local modules_enabled = config.get(host, "core", "modules_enabled");
128                 if modules_enabled then
129                         for _, module in pairs(modules_enabled) do
130                                 modulemanager.load(host, module);
131                         end
132                 end
133         end
134 end
135
136 -- setup error handling
137 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 }) --]][][[]][];
138
139 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;
140 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;
141
142
143 local global_ssl_ctx = config.get("*", "core", "ssl");
144 if global_ssl_ctx then
145         local default_ssl_ctx = { mode = "server", protocol = "sslv23", capath = "/etc/ssl/certs", verify = "none"; };
146         setmetatable(global_ssl_ctx, { __index = default_ssl_ctx });
147 end
148
149 -- start listening on sockets
150 start("xmppclient", { ssl = global_ssl_ctx })
151 start("xmppserver", { ssl = global_ssl_ctx })
152
153 if config.get("*", "core", "console_enabled") then
154         start("console")
155 end
156
157 modulemanager.fire_event("server-started");
158
159 server.loop();