Add hostmanager, and eventmanager
[prosody.git] / prosody
1 #!/usr/bin/env lua
2 -- Prosody IM v0.1
3 -- Copyright (C) 2008 Matthew Wild
4 -- Copyright (C) 2008 Waqas Hussain
5 -- 
6 -- This program is free software; you can redistribute it and/or
7 -- modify it under the terms of the GNU General Public License
8 -- as published by the Free Software Foundation; either version 2
9 -- of the License, or (at your option) any later version.
10 -- 
11 -- This program is distributed in the hope that it will be useful,
12 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
13 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 -- GNU General Public License for more details.
15 -- 
16 -- You should have received a copy of the GNU General Public License
17 -- along with this program; if not, write to the Free Software
18 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 --
20
21
22
23 -- Config here --
24
25 CFG_SOURCEDIR=nil;
26 CFG_CONFIGDIR=os.getenv("PROSODY_CFGDIR");
27 CFG_PLUGINDIR=nil;
28 CFG_DATADIR=os.getenv("PROSODY_DATADIR");
29
30 -- -- -- -- -- --
31
32 if CFG_SOURCEDIR then
33         package.path = CFG_SOURCEDIR.."/?.lua;"..package.path
34         package.cpath = CFG_SOURCEDIR.."/?.so;"..package.cpath
35 end
36
37 if CFG_DATADIR then
38         if os.getenv("HOME") then
39                 CFG_DATADIR = CFG_DATADIR:gsub("^~", os.getenv("HOME"));
40         end
41 end
42
43 -- Required to be able to find packages installed with luarocks
44 pcall(require, "luarocks.require")
45
46
47 config = require "core.configmanager"
48 log = require "util.logger".init("general");
49
50 do
51         -- TODO: Check for other formats when we add support for them
52         -- Use lfs? Make a new conf/ dir?
53         local ok, err = config.load((CFG_CONFIGDIR or ".").."/prosody.cfg.lua");
54         if not ok then
55                 log("error", "Couldn't load config file: %s", err);
56                 log("info", "Falling back to old config file format...")
57                 ok, err = pcall(dofile, "lxmppd.cfg");
58                 if not ok then
59                         log("error", "Old config format loading failed too: %s", err);
60                 else
61                         for _, host in ipairs(_G.config.hosts) do
62                                 config.set(host, "core", "defined", true);
63                         end
64                         
65                         config.set("*", "core", "modules_enabled", _G.config.modules);
66                         config.set("*", "core", "ssl", _G.config.ssl_ctx);
67                 end
68         end
69 end
70
71 require "util.datamanager".set_data_path(data_path);
72
73 local server = require "net.server"
74
75 require "util.dependencies"
76
77 -- Maps connections to sessions --
78 sessions = {};
79 hosts = {};
80
81
82 -- Load and initialise core modules --
83
84 require "util.import"
85 require "core.xmlhandlers"
86 require "core.rostermanager"
87 require "core.offlinemessage"
88 require "core.eventmanager"
89 require "core.hostmanager"
90 require "core.modulemanager"
91 require "core.usermanager"
92 require "core.sessionmanager"
93 require "core.stanza_router"
94
95 --[[
96 pcall(require, "remdebug.engine");
97 if remdebug then remdebug.engine.start() end
98 ]]
99
100 local cl = require "net.connlisteners";
101
102 require "util.stanza"
103 require "util.jid"
104
105 ------------------------------------------------------------------------
106
107 ------------- Begin code without a home ---------------------
108
109 local data_path = config.get("*", "core", "data_path") or CFG_DATADIR or "data";
110 local path_separator = "/"; if os.getenv("WINDIR") then path_separator = "\\" end
111 local _mkdir = {}
112 function mkdir(path)
113         path = path:gsub("/", path_separator);
114         --print("mkdir",path);
115         local x = io.popen("mkdir \""..path.."\" 2>&1"):read("*a");
116 end
117 function encode(s) return s and (s:gsub("%W", function (c) return string.format("%%%x", c:byte()); end)); end
118 function mkdirs(host)
119         if not _mkdir[host] then
120                 local host_dir = string.format("%s/%s", data_path, encode(host));
121                 mkdir(host_dir);
122                 mkdir(host_dir.."/accounts");
123                 mkdir(host_dir.."/vcard");
124                 mkdir(host_dir.."/roster");
125                 mkdir(host_dir.."/private");
126                 mkdir(host_dir.."/offline");
127                 _mkdir[host] = true;
128         end
129 end
130 mkdir(data_path);
131
132 eventmanager.add_event_hook("host-activated", mkdirs);
133
134 ----------- End of out-of-place code --------------
135
136 eventmanager.fire_event("server-starting");
137
138 -- Initialise modules
139
140 for host in pairs(hosts) do
141         if host ~= "*" then
142                 local modules_enabled = config.get(host, "core", "modules_enabled");
143                 if modules_enabled then
144                         for _, module in pairs(modules_enabled) do
145                                 modulemanager.load(host, module);
146                         end
147                 end
148         end
149 end
150
151 -- setup error handling
152 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 }) --]][][[]][];
153
154 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;
155 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;
156
157
158 local global_ssl_ctx = config.get("*", "core", "ssl");
159 if global_ssl_ctx then
160         local default_ssl_ctx = { mode = "server", protocol = "sslv23", capath = "/etc/ssl/certs", verify = "none"; };
161         setmetatable(global_ssl_ctx, { __index = default_ssl_ctx });
162 end
163
164 -- start listening on sockets
165 cl.start("xmppclient", { ssl = global_ssl_ctx })
166 cl.start("xmppserver", { ssl = global_ssl_ctx })
167
168 if config.get("*", "core", "console_enabled") then
169         if cl.get("console") then
170                 cl.start("console")
171         else
172                 log("error", "Console is enabled, but the console module appears not to be loaded");
173         end
174 end
175
176 eventmanager.fire_event("server-started");
177
178 server.loop();