5cfa740db33895d328ede13770cb6d2f6efa14d8
[prosody.git] / prosody
1 #!/usr/bin/env lua
2 -- Prosody IM v0.2
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 -- Config here --
22
23 CFG_SOURCEDIR=nil;
24 CFG_CONFIGDIR=os.getenv("PROSODY_CFGDIR");
25 CFG_PLUGINDIR=nil;
26 CFG_DATADIR=os.getenv("PROSODY_DATADIR");
27
28 -- -- -- -- -- --
29
30 if CFG_SOURCEDIR then
31         package.path = CFG_SOURCEDIR.."/?.lua;"..package.path
32         package.cpath = CFG_SOURCEDIR.."/?.so;"..package.cpath
33 end
34
35 if CFG_DATADIR then
36         if os.getenv("HOME") then
37                 CFG_DATADIR = CFG_DATADIR:gsub("^~", os.getenv("HOME"));
38         end
39 end
40
41 -- Required to be able to find packages installed with luarocks
42 pcall(require, "luarocks.require")
43
44
45 config = require "core.configmanager"
46 log = require "util.logger".init("general");
47
48 -- Disable log output, needs to read from config
49 -- require "util.logger".setwriter(function () end);
50
51 do
52         -- TODO: Check for other formats when we add support for them
53         -- Use lfs? Make a new conf/ dir?
54         local ok, err = config.load((CFG_CONFIGDIR or ".").."/prosody.cfg.lua");
55         if not ok then
56                 log("error", "Couldn't load config file: %s", err);
57                 log("info", "Falling back to old config file format...")
58                 ok, err = pcall(dofile, "lxmppd.cfg");
59                 if not ok then
60                         log("error", "Old config format loading failed too: %s", err);
61                 else
62                         for _, host in ipairs(_G.config.hosts) do
63                                 config.set(host, "core", "defined", true);
64                         end
65                         
66                         config.set("*", "core", "modules_enabled", _G.config.modules);
67                         config.set("*", "core", "ssl", _G.config.ssl_ctx);
68                 end
69         end
70 end
71
72 local server = require "net.server"
73
74 require "util.dependencies"
75
76 -- Maps connections to sessions --
77 sessions = {};
78 hosts = {};
79
80 -- Load and initialise core modules --
81
82 require "util.import"
83 require "core.xmlhandlers"
84 require "core.rostermanager"
85 require "core.offlinemessage"
86 require "core.eventmanager"
87 require "core.hostmanager"
88 require "core.modulemanager"
89 require "core.usermanager"
90 require "core.sessionmanager"
91 require "core.stanza_router"
92
93 --[[
94 pcall(require, "remdebug.engine");
95 if remdebug then remdebug.engine.start() end
96 ]]
97
98 local cl = require "net.connlisteners";
99
100 require "util.stanza"
101 require "util.jid"
102
103 ------------------------------------------------------------------------
104
105
106 ------------- Begin code without a home ---------------------
107
108 local data_path = config.get("*", "core", "data_path") or CFG_DATADIR or "data";
109 require "util.datamanager".set_data_path(data_path);
110
111 ----------- End of out-of-place code --------------
112
113 eventmanager.fire_event("server-starting");
114
115
116 -- setup error handling
117 setmetatable(_G, { __index = function (t, k) error("Attempt to read a non-existent global '"..k.."'", 2); end, __newindex = function (t, k, v) error("Attempt to set a global: "..tostring(k).." = "..tostring(v), 2); end });
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 local function do_ports(option, listener, default, conntype)
127         local ports = config.get("*", "core", option) or default;
128         --if type(ports) == "number" then ports = {ports} end;
129         
130         if type(ports) ~= "table" then
131                 log("error", "core."..option.." is not a table");
132         else
133                 for _, port in ipairs(ports) do
134                         if type(port) ~= "number" then
135                                 log("error", "Non-numeric "..option..": "..tostring(port));
136                         else
137                                 cl.start(listener, { ssl = global_ssl_ctx, port = port, type = conntype });
138                         end
139                 end
140         end
141 end
142
143 do_ports("c2s_ports", "xmppclient", {5222}, (global_ssl_ctx and "tls") or "tcp");
144 do_ports("s2s_ports", "xmppserver", {5269}, "tcp");
145 do_ports("legacy_ssl_ports", "xmppclient", {}, "ssl");
146
147 if config.get("*", "core", "console_enabled") then
148         if cl.get("console") then
149                 cl.start("console", { interface = config.get("*", "core", "console_interface") or "127.0.0.1" })
150         else
151                 log("error", "Console is enabled, but the console module appears not to be loaded");
152         end
153 end
154
155 eventmanager.fire_event("server-started");
156
157 server.loop();