60 print(""); |
60 print(""); |
61 os.exit(1); |
61 os.exit(1); |
62 end |
62 end |
63 end |
63 end |
64 |
64 |
65 read_config(); |
|
66 |
|
67 function load_libraries() |
65 function load_libraries() |
68 --- Initialize logging |
66 --- Initialize logging |
69 require "core.loggingmanager" |
67 require "core.loggingmanager" |
70 |
68 |
71 --- Check runtime dependencies |
69 --- Check runtime dependencies |
72 require "util.dependencies" |
70 require "util.dependencies" |
73 |
71 |
74 --- Load socket framework |
72 --- Load socket framework |
75 server = require "net.server" |
73 server = require "net.server" |
76 end |
74 end |
77 load_libraries(); |
|
78 |
75 |
79 function init_global_state() |
76 function init_global_state() |
80 bare_sessions = {}; |
77 bare_sessions = {}; |
81 full_sessions = {}; |
78 full_sessions = {}; |
82 hosts = {}; |
79 hosts = {}; |
93 plugins = CFG_PLUGINDIR, data = CFG_DATADIR }; |
90 plugins = CFG_PLUGINDIR, data = CFG_DATADIR }; |
94 |
91 |
95 prosody.arg = _G.arg; |
92 prosody.arg = _G.arg; |
96 |
93 |
97 prosody.events = require "util.events".new(); |
94 prosody.events = require "util.events".new(); |
98 end |
95 |
99 init_global_state(); |
96 |
100 |
97 -- Function to reload the config file |
|
98 function prosody.reload_config() |
|
99 log("info", "Reloading configuration file"); |
|
100 prosody.events.fire_event("reloading-config"); |
|
101 local ok, level, err = config.load((rawget(_G, "CFG_CONFIGDIR") or ".").."/prosody.cfg.lua"); |
|
102 if not ok then |
|
103 if level == "parser" then |
|
104 log("error", "There was an error parsing the configuration file: %s", tostring(err)); |
|
105 elseif level == "file" then |
|
106 log("error", "Couldn't read the config file when trying to reload: %s", tostring(err)); |
|
107 end |
|
108 end |
|
109 end |
|
110 |
|
111 -- Function to reopen logfiles |
|
112 function prosody.reopen_logfiles() |
|
113 log("info", "Re-opening log files"); |
|
114 eventmanager.fire_event("reopen-log-files"); -- Handled by appropriate log sinks |
|
115 prosody.events.fire_event("reopen-log-files"); |
|
116 end |
|
117 |
|
118 -- Function to initiate prosody shutdown |
|
119 function prosody.shutdown(reason) |
|
120 log("info", "Shutting down: %s", reason or "unknown reason"); |
|
121 prosody.events.fire_event("server-stopping", {reason = reason}); |
|
122 server.setquitting(true); |
|
123 end |
|
124 end |
101 |
125 |
102 function read_version() |
126 function read_version() |
103 -- Try to determine version |
127 -- Try to determine version |
104 local version_file = io.open((CFG_SOURCEDIR or ".").."/prosody.version"); |
128 local version_file = io.open((CFG_SOURCEDIR or ".").."/prosody.version"); |
105 if version_file then |
129 if version_file then |
110 end |
134 end |
111 else |
135 else |
112 prosody.version = "unknown"; |
136 prosody.version = "unknown"; |
113 end |
137 end |
114 end |
138 end |
115 read_version(); |
|
116 log("info", "Hello and welcome to Prosody version %s", prosody.version); |
|
117 |
139 |
118 function load_secondary_libraries() |
140 function load_secondary_libraries() |
119 --- Load and initialise core modules |
141 --- Load and initialise core modules |
120 require "util.import" |
142 require "util.import" |
121 require "core.xmlhandlers" |
143 require "core.xmlhandlers" |
141 require "net.connlisteners"; |
163 require "net.connlisteners"; |
142 |
164 |
143 require "util.stanza" |
165 require "util.stanza" |
144 require "util.jid" |
166 require "util.jid" |
145 end |
167 end |
146 load_secondary_libraries(); |
|
147 |
|
148 |
168 |
149 function init_data_store() |
169 function init_data_store() |
150 local data_path = config.get("*", "core", "data_path") or CFG_DATADIR or "data"; |
170 local data_path = config.get("*", "core", "data_path") or CFG_DATADIR or "data"; |
151 require "util.datamanager".set_data_path(data_path); |
171 require "util.datamanager".set_data_path(data_path); |
152 require "util.datamanager".add_callback(function(username, host, datastore, data) |
172 require "util.datamanager".add_callback(function(username, host, datastore, data) |
154 return false; |
174 return false; |
155 end |
175 end |
156 return username, host, datastore, data; |
176 return username, host, datastore, data; |
157 end); |
177 end); |
158 end |
178 end |
159 init_data_store(); |
179 |
160 |
180 function prepare_to_start() |
161 -- Function to reload the config file |
|
162 function prosody.reload_config() |
|
163 log("info", "Reloading configuration file"); |
|
164 prosody.events.fire_event("reloading-config"); |
|
165 local ok, level, err = config.load((rawget(_G, "CFG_CONFIGDIR") or ".").."/prosody.cfg.lua"); |
|
166 if not ok then |
|
167 if level == "parser" then |
|
168 log("error", "There was an error parsing the configuration file: %s", tostring(err)); |
|
169 elseif level == "file" then |
|
170 log("error", "Couldn't read the config file when trying to reload: %s", tostring(err)); |
|
171 end |
|
172 end |
|
173 end |
|
174 |
|
175 -- Function to reopen logfiles |
|
176 function prosody.reopen_logfiles() |
|
177 log("info", "Re-opening log files"); |
|
178 eventmanager.fire_event("reopen-log-files"); -- Handled by appropriate log sinks |
|
179 prosody.events.fire_event("reopen-log-files"); |
|
180 end |
|
181 |
|
182 -- Function to initiate prosody shutdown |
|
183 function prosody.shutdown(reason) |
|
184 log("info", "Shutting down: %s", reason or "unknown reason"); |
|
185 prosody.events.fire_event("server-stopping", {reason = reason}); |
|
186 server.setquitting(true); |
|
187 end |
|
188 |
|
189 function prosody.prepare_to_start() |
|
190 -- Signal to modules that we are ready to start |
181 -- Signal to modules that we are ready to start |
191 eventmanager.fire_event("server-starting"); |
182 eventmanager.fire_event("server-starting"); |
192 prosody.events.fire_event("server-starting"); |
183 prosody.events.fire_event("server-starting"); |
193 |
184 |
194 -- Load SSL settings from config, and create a ctx table |
185 -- Load SSL settings from config, and create a ctx table |
233 cl.start("console", { interface = config.get("*", "core", "console_interface") or "127.0.0.1" }) |
224 cl.start("console", { interface = config.get("*", "core", "console_interface") or "127.0.0.1" }) |
234 end |
225 end |
235 |
226 |
236 prosody.start_time = os.time(); |
227 prosody.start_time = os.time(); |
237 end |
228 end |
238 prosody.prepare_to_start(); |
229 |
239 |
230 function init_global_protection() |
240 function prosody.init_global_protection() |
|
241 -- Catch global accesses -- |
231 -- Catch global accesses -- |
242 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 } |
232 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 } |
243 |
233 |
244 function prosody.unlock_globals() |
234 function prosody.unlock_globals() |
245 setmetatable(_G, nil); |
235 setmetatable(_G, nil); |
250 end |
240 end |
251 |
241 |
252 -- And lock now... |
242 -- And lock now... |
253 prosody.lock_globals(); |
243 prosody.lock_globals(); |
254 end |
244 end |
255 prosody.init_global_protection(); |
245 |
256 |
246 function loop() |
257 |
|
258 eventmanager.fire_event("server-started"); |
|
259 prosody.events.fire_event("server-started"); |
|
260 |
|
261 function prosody.loop() |
|
262 -- Error handler for errors that make it this far |
247 -- Error handler for errors that make it this far |
263 local function catch_uncaught_error(err) |
248 local function catch_uncaught_error(err) |
264 if err:match("%d*: interrupted!$") then |
249 if err:match("%d*: interrupted!$") then |
265 return "quitting"; |
250 return "quitting"; |
266 end |
251 end |
276 |
261 |
277 while select(2, xpcall(server.loop, catch_uncaught_error)) ~= "quitting" do |
262 while select(2, xpcall(server.loop, catch_uncaught_error)) ~= "quitting" do |
278 socket.sleep(0.2); |
263 socket.sleep(0.2); |
279 end |
264 end |
280 end |
265 end |
281 prosody.loop(); |
266 |
282 |
267 function cleanup() |
283 function prosody.cleanup() |
|
284 log("info", "Shutdown status: Cleaning up"); |
268 log("info", "Shutdown status: Cleaning up"); |
285 prosody.events.fire_event("server-cleanup"); |
269 prosody.events.fire_event("server-cleanup"); |
286 |
270 |
287 -- Ok, we're quitting I know, but we |
271 -- Ok, we're quitting I know, but we |
288 -- need to do some tidying before we go :) |
272 -- need to do some tidying before we go :) |
315 log("info", "Shutdown status: Closing all server connections"); |
299 log("info", "Shutdown status: Closing all server connections"); |
316 server.closeall(); |
300 server.closeall(); |
317 |
301 |
318 server.setquitting(true); |
302 server.setquitting(true); |
319 end |
303 end |
320 prosody.cleanup(); |
304 |
321 |
305 read_config(); |
|
306 load_libraries(); |
|
307 init_global_state(); |
|
308 read_version(); |
|
309 log("info", "Hello and welcome to Prosody version %s", prosody.version); |
|
310 load_secondary_libraries(); |
|
311 init_data_store(); |
|
312 prepare_to_start(); |
|
313 init_global_protection(); |
|
314 |
|
315 eventmanager.fire_event("server-started"); |
|
316 prosody.events.fire_event("server-started"); |
|
317 |
|
318 loop(); |
|
319 |
|
320 log("info", "Shutting down..."); |
|
321 cleanup(); |
322 eventmanager.fire_event("server-stopped"); |
322 eventmanager.fire_event("server-stopped"); |
323 prosody.events.fire_event("server-stopped"); |
323 prosody.events.fire_event("server-stopped"); |
324 log("info", "Shutdown status: Complete!"); |
324 log("info", "Shutdown complete"); |
|
325 |