util.logger: Small code tidying :)
[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
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 -- Commented to protect us from 
89 -- the second kind of people
90 --[[ 
91 pcall(require, "remdebug.engine");
92 if remdebug then remdebug.engine.start() end
93 ]]
94
95 local cl = require "net.connlisteners";
96
97 require "util.stanza"
98 require "util.jid"
99
100 ------------------------------------------------------------------------
101
102
103 ------------- Begin code without a home ---------------------
104
105 local data_path = config.get("*", "core", "data_path") or CFG_DATADIR or "data";
106 require "util.datamanager".set_data_path(data_path);
107
108 ----------- End of out-of-place code --------------
109
110
111 eventmanager.fire_event("server-starting");
112
113
114 -- setup error handling
115 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 });
116
117 local global_ssl_ctx = config.get("*", "core", "ssl");
118 if global_ssl_ctx then
119         local default_ssl_ctx = { mode = "server", protocol = "sslv23", capath = "/etc/ssl/certs", verify = "none"; };
120         setmetatable(global_ssl_ctx, { __index = default_ssl_ctx });
121 end
122
123 -- start listening on sockets
124 local function do_ports(option, listener, default, conntype)
125         local ports = config.get("*", "core", option) or default;
126         --if type(ports) == "number" then ports = {ports} end;
127         
128         if type(ports) ~= "table" then
129                 log("error", "core."..option.." is not a table");
130         else
131                 for _, port in ipairs(ports) do
132                         if type(port) ~= "number" then
133                                 log("error", "Non-numeric "..option..": "..tostring(port));
134                         else
135                                 cl.start(listener, { ssl = conntype ~= "tcp" and global_ssl_ctx, port = port, type = conntype });
136                         end
137                 end
138         end
139 end
140
141 do_ports("c2s_ports", "xmppclient", {5222}, (global_ssl_ctx and "tls") or "tcp");
142 do_ports("s2s_ports", "xmppserver", {5269}, "tcp");
143 do_ports("legacy_ssl_ports", "xmppclient", {}, "ssl");
144
145 if config.get("*", "core", "console_enabled") then
146         if cl.get("console") then
147                 cl.start("console", { interface = config.get("*", "core", "console_interface") or "127.0.0.1" })
148         else
149                 log("error", "Console is enabled, but the console module appears not to be loaded");
150         end
151 end
152
153 eventmanager.fire_event("server-started");
154
155 local quitting;
156 while not quitting do
157         xpcall(server.loop, function (err)
158                                         if err:match("%d*: interrupted!$") then
159                                                 quitting = true;
160                                                 return;
161                                         end
162
163                                         log("error", "Top-level error, please report:\n%s", tostring(err));
164
165                                         local traceback = debug.traceback("", 2);
166                                         if traceback then
167                                                 log("error", "%s", traceback);
168                                         end
169                                         
170                                         eventmanager.fire_event("very-bad-error", "*", err, traceback);
171                                 end);
172 end