util/xmlrpc: Added <nil/> extension to RPC functions (allowing nil parameters and...
[prosody.git] / prosody
1 #!/usr/bin/env lua
2 -- Prosody IM v0.3
3 -- Copyright (C) 2008-2009 Matthew Wild
4 -- Copyright (C) 2008-2009 Waqas Hussain
5 -- 
6 -- This project is MIT/X11 licensed. Please see the
7 -- COPYING file in the source package for more information.
8 --
9
10 -- Will be modified by configure script if run --
11
12 CFG_SOURCEDIR=nil;
13 CFG_CONFIGDIR=os.getenv("PROSODY_CFGDIR");
14 CFG_PLUGINDIR=nil;
15 CFG_DATADIR=os.getenv("PROSODY_DATADIR");
16
17 -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- --
18
19 if CFG_SOURCEDIR then
20         package.path = CFG_SOURCEDIR.."/?.lua;"..package.path
21         package.cpath = CFG_SOURCEDIR.."/?.so;"..package.cpath
22 end
23
24 if CFG_DATADIR then
25         if os.getenv("HOME") then
26                 CFG_DATADIR = CFG_DATADIR:gsub("^~", os.getenv("HOME"));
27         end
28 end
29
30 -- Required to be able to find packages installed with luarocks
31 pcall(require, "luarocks.require")
32
33
34 config = require "core.configmanager"
35 log = require "util.logger".init("general");
36
37 -- Disable log output, needs to read from config
38 -- require "util.logger".setwriter(function () end);
39
40 do
41         -- TODO: Check for other formats when we add support for them
42         -- Use lfs? Make a new conf/ dir?
43         local ok, level, err = config.load((CFG_CONFIGDIR or ".").."/prosody.cfg.lua");
44         if not ok then
45                 print("");
46                 print("**************************");
47                 if level == "parser" then
48                         print("A problem occured while reading the config file "..(CFG_CONFIGDIR or ".").."/prosody.cfg.lua");
49                         local err_line, err_message = tostring(err):match("%[string .-%]:(%d*): (.*)");
50                         print("Error"..(err_line and (" on line "..err_line) or "")..": "..(err_message or tostring(err)));
51                         print("");
52                 elseif level == "file" then
53                         print("Prosody was unable to find the configuration file.");
54                         print("We looked for: "..(CFG_CONFIGDIR or ".").."/prosody.cfg.lua");
55                         print("A sample config file is included in the Prosody download called prosody.cfg.lua.dist");
56                         print("Copy or rename it to prosody.cfg.lua and edit as necessary.");
57                 end
58                 print("More help on configuring Prosody can be found at http://prosody.im/doc/configure");
59                 print("Good luck!");
60                 print("**************************");
61                 print("");
62                 os.exit(1);
63         end
64 end
65
66 require "util.dependencies"
67
68 local server = require "net.server"
69
70
71 -- Maps connections to sessions --
72 sessions = {};
73 hosts = {};
74
75 -- Load and initialise core modules --
76
77 require "util.import"
78 require "core.xmlhandlers"
79 require "core.rostermanager"
80 require "core.eventmanager"
81 require "core.hostmanager"
82 require "core.modulemanager"
83 require "core.usermanager"
84 require "core.sessionmanager"
85 require "core.stanza_router"
86
87 -- Commented to protect us from 
88 -- the second kind of people
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
110 eventmanager.fire_event("server-starting");
111
112
113 -- setup error handling
114 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 });
115
116 local global_ssl_ctx = config.get("*", "core", "ssl");
117 if global_ssl_ctx then
118         local default_ssl_ctx = { mode = "server", protocol = "sslv23", capath = "/etc/ssl/certs", verify = "none"; };
119         setmetatable(global_ssl_ctx, { __index = default_ssl_ctx });
120 end
121
122 -- start listening on sockets
123 local function do_ports(option, listener, default, conntype)
124         local ports = config.get("*", "core", option) or default;
125         --if type(ports) == "number" then ports = {ports} end;
126         
127         if type(ports) ~= "table" then
128                 log("error", "core."..option.." is not a table");
129         else
130                 for _, port in ipairs(ports) do
131                         if type(port) ~= "number" then
132                                 log("error", "Non-numeric "..option..": "..tostring(port));
133                         else
134                                 cl.start(listener, { ssl = conntype ~= "tcp" and global_ssl_ctx, port = port, type = conntype });
135                         end
136                 end
137         end
138 end
139
140 do_ports("c2s_ports", "xmppclient", {5222}, (global_ssl_ctx and "tls") or "tcp");
141 do_ports("s2s_ports", "xmppserver", {5269}, "tcp");
142 do_ports("legacy_ssl_ports", "xmppclient", {}, "ssl");
143
144 if config.get("*", "core", "console_enabled") then
145         if cl.get("console") then
146                 cl.start("console", { interface = config.get("*", "core", "console_interface") or "127.0.0.1" })
147         else
148                 log("error", "Console is enabled, but the console module appears not to be loaded");
149         end
150 end
151
152 eventmanager.fire_event("server-started");
153
154 local quitting;
155 while not quitting do
156         xpcall(server.loop, function (err)
157                                         if err:match("%d*: interrupted!$") then
158                                                 quitting = true;
159                                                 return;
160                                         end
161
162                                         log("error", "Top-level error, please report:\n%s", tostring(err));
163
164                                         local traceback = debug.traceback("", 2);
165                                         if traceback then
166                                                 log("error", "%s", traceback);
167                                         end
168                                         
169                                         eventmanager.fire_event("very-bad-error", "*", err, traceback);
170                                 end);
171 end