Merge
[prosody.git] / prosody
1 #!/usr/bin/env lua
2 -- Prosody IM v0.4
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
36 do
37         -- TODO: Check for other formats when we add support for them
38         -- Use lfs? Make a new conf/ dir?
39         local ok, level, err = config.load((CFG_CONFIGDIR or ".").."/prosody.cfg.lua");
40         if not ok then
41                 print("");
42                 print("**************************");
43                 if level == "parser" then
44                         print("A problem occured while reading the config file "..(CFG_CONFIGDIR or ".").."/prosody.cfg.lua");
45                         local err_line, err_message = tostring(err):match("%[string .-%]:(%d*): (.*)");
46                         print("Error"..(err_line and (" on line "..err_line) or "")..": "..(err_message or tostring(err)));
47                         print("");
48                 elseif level == "file" then
49                         print("Prosody was unable to find the configuration file.");
50                         print("We looked for: "..(CFG_CONFIGDIR or ".").."/prosody.cfg.lua");
51                         print("A sample config file is included in the Prosody download called prosody.cfg.lua.dist");
52                         print("Copy or rename it to prosody.cfg.lua and edit as necessary.");
53                 end
54                 print("More help on configuring Prosody can be found at http://prosody.im/doc/configure");
55                 print("Good luck!");
56                 print("**************************");
57                 print("");
58                 os.exit(1);
59         end
60 end
61
62 log = require "util.logger".init("general");
63
64 -- Disable log output, needs to read from config
65 -- require "util.logger".setwriter(function () end);
66
67 require "util.dependencies"
68
69 local server = require "net.server"
70
71
72 -- Maps connections to sessions --
73 sessions = {};
74 hosts = {};
75
76 -- Load and initialise core modules --
77
78 require "util.import"
79 require "core.xmlhandlers"
80 require "core.rostermanager"
81 require "core.eventmanager"
82 require "core.hostmanager"
83 require "core.modulemanager"
84 require "core.usermanager"
85 require "core.sessionmanager"
86 require "core.stanza_router"
87
88 require "util.array"
89 require "util.iterators"
90
91 -- Commented to protect us from 
92 -- the second kind of people
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 require "util.datamanager".set_callback(function(username, host, datastore)
111         return config.get(host, "core", "anonymous_login");
112 end);
113
114 ----------- End of out-of-place code --------------
115
116 eventmanager.fire_event("server-starting");
117
118 local global_ssl_ctx = ssl and config.get("*", "core", "ssl");
119 if global_ssl_ctx then
120         local default_ssl_ctx = { mode = "server", protocol = "sslv23", capath = "/etc/ssl/certs", verify = "none"; };
121         setmetatable(global_ssl_ctx, { __index = default_ssl_ctx });
122 end
123
124 -- start listening on sockets
125 function net_activate_ports(option, listener, default, conntype)
126         local ports = config.get("*", "core", option) or default;
127         if type(ports) == "number" then ports = {ports} end;
128         
129         if type(ports) ~= "table" then
130                 log("error", "core."..option.." is not a table");
131         else
132                 for _, port in ipairs(ports) do
133                         if type(port) ~= "number" then
134                                 log("error", "Non-numeric "..option..": "..tostring(port));
135                         else
136                                 cl.start(listener, { ssl = conntype ~= "tcp" and global_ssl_ctx, port = port, type = conntype });
137                         end
138                 end
139         end
140 end
141
142 net_activate_ports("c2s_ports", "xmppclient", {5222}, (global_ssl_ctx and "tls") or "tcp");
143 net_activate_ports("s2s_ports", "xmppserver", {5269}, "tcp");
144 net_activate_ports("legacy_ssl_ports", "xmppclient", {}, "ssl");
145
146 if config.get("*", "core", "console_enabled") then
147         if cl.get("console") then
148                 cl.start("console", { interface = config.get("*", "core", "console_interface") or "127.0.0.1" })
149         else
150                 log("error", "Console is enabled, but the console module appears not to be loaded");
151         end
152 end
153
154 -- setup error handling
155 local locked_globals_mt = { __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 }
156
157 function unlock_globals()
158         setmetatable(_G, nil);
159 end
160
161 function lock_globals()
162         setmetatable(_G, locked_globals_mt);
163 end
164
165 lock_globals();
166
167 eventmanager.fire_event("server-started");
168
169 local quitting;
170 while not quitting do
171         xpcall(server.loop, function (err)
172                                         if err:match("%d*: interrupted!$") then
173                                                 quitting = true;
174                                                 return;
175                                         end
176
177                                         log("error", "Top-level error, please report:\n%s", tostring(err));
178
179                                         local traceback = debug.traceback("", 2);
180                                         if traceback then
181                                                 log("error", "%s", traceback);
182                                         end
183                                         
184                                         eventmanager.fire_event("very-bad-error", "*", err, traceback);
185                                 end);
186 end