57e705ceb8b3f1b5a6e6064af8621043d103f285
[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 -- 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 require "util.datamanager".set_callback(function(username, host, datastore)
108         return config.get(host, "core", "anonymous_login");
109 end);
110
111 ----------- End of out-of-place code --------------
112
113 eventmanager.fire_event("server-starting");
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 function net_activate_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 net_activate_ports("c2s_ports", "xmppclient", {5222}, (global_ssl_ctx and "tls") or "tcp");
140 net_activate_ports("s2s_ports", "xmppserver", {5269}, "tcp");
141 net_activate_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 -- setup error handling
152 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 });
153
154 eventmanager.fire_event("server-started");
155
156 local quitting;
157 while not quitting do
158         xpcall(server.loop, function (err)
159                                         if err:match("%d*: interrupted!$") then
160                                                 quitting = true;
161                                                 return;
162                                         end
163
164                                         log("error", "Top-level error, please report:\n%s", tostring(err));
165
166                                         local traceback = debug.traceback("", 2);
167                                         if traceback then
168                                                 log("error", "%s", traceback);
169                                         end
170                                         
171                                         eventmanager.fire_event("very-bad-error", "*", err, traceback);
172                                 end);
173 end