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