3bec8a6cb6fd1b8ff090325cb9e9ca71919b7fd9
[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                 print("");
57                 print("**************************");
58                 print("Prosody was unable to find the configuration file.");
59                 print("We looked for: "..(CFG_CONFIGDIR or ".").."/prosody.cfg.lua");
60                 print("A sample config file is included in the Prosody download called prosody.cfg.lua.dist");
61                 print("Copy or rename it to prosody.cfg.lua and edit as necessary.");
62                 print("More help on configuring Prosody can be found at http://prosody.im/doc/configure");
63                 print("Good luck!");
64                 print("**************************");
65                 os.exit(1);
66         end
67 end
68
69 local server = require "net.server"
70
71 require "util.dependencies"
72
73 -- Maps connections to sessions --
74 sessions = {};
75 hosts = {};
76
77 -- Load and initialise core modules --
78
79 require "util.import"
80 require "core.xmlhandlers"
81 require "core.rostermanager"
82 require "core.eventmanager"
83 require "core.hostmanager"
84 require "core.modulemanager"
85 require "core.usermanager"
86 require "core.sessionmanager"
87 require "core.stanza_router"
88
89 --[[
90 pcall(require, "remdebug.engine");
91 if remdebug then remdebug.engine.start() end
92 ]]
93
94 local cl = require "net.connlisteners";
95
96 require "util.stanza"
97 require "util.jid"
98
99 ------------------------------------------------------------------------
100
101
102 ------------- Begin code without a home ---------------------
103
104 local data_path = config.get("*", "core", "data_path") or CFG_DATADIR or "data";
105 require "util.datamanager".set_data_path(data_path);
106
107 ----------- End of out-of-place code --------------
108
109 eventmanager.fire_event("server-starting");
110
111
112 -- setup error handling
113 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 });
114
115 local global_ssl_ctx = config.get("*", "core", "ssl");
116 if global_ssl_ctx then
117         local default_ssl_ctx = { mode = "server", protocol = "sslv23", capath = "/etc/ssl/certs", verify = "none"; };
118         setmetatable(global_ssl_ctx, { __index = default_ssl_ctx });
119 end
120
121 -- start listening on sockets
122 local function do_ports(option, listener, default, conntype)
123         local ports = config.get("*", "core", option) or default;
124         --if type(ports) == "number" then ports = {ports} end;
125         
126         if type(ports) ~= "table" then
127                 log("error", "core."..option.." is not a table");
128         else
129                 for _, port in ipairs(ports) do
130                         if type(port) ~= "number" then
131                                 log("error", "Non-numeric "..option..": "..tostring(port));
132                         else
133                                 cl.start(listener, { ssl = conntype ~= "tcp" and global_ssl_ctx, port = port, type = conntype });
134                         end
135                 end
136         end
137 end
138
139 do_ports("c2s_ports", "xmppclient", {5222}, (global_ssl_ctx and "tls") or "tcp");
140 do_ports("s2s_ports", "xmppserver", {5269}, "tcp");
141 do_ports("legacy_ssl_ports", "xmppclient", {}, "ssl");
142
143 if config.get("*", "core", "console_enabled") then
144         if cl.get("console") then
145                 cl.start("console", { interface = config.get("*", "core", "console_interface") or "127.0.0.1" })
146         else
147                 log("error", "Console is enabled, but the console module appears not to be loaded");
148         end
149 end
150
151 eventmanager.fire_event("server-started");
152
153 server.loop();