Prepare required data folder for integration tests
[prosody.git] / prosody
blobe82318d1e035275306a67723753458091a3b4e4a
1 #!/usr/bin/env lua
2 -- Prosody IM
3 -- Copyright (C) 2008-2010 Matthew Wild
4 -- Copyright (C) 2008-2010 Waqas Hussain
5 --
6 -- This project is MIT/X11 licensed. Please see the
7 -- COPYING file in the source package for more information.
8 --
10 -- prosody - main executable for Prosody XMPP server
12 -- Will be modified by configure script if run --
14 CFG_SOURCEDIR=CFG_SOURCEDIR or os.getenv("PROSODY_SRCDIR");
15 CFG_CONFIGDIR=CFG_CONFIGDIR or os.getenv("PROSODY_CFGDIR");
16 CFG_PLUGINDIR=CFG_PLUGINDIR or os.getenv("PROSODY_PLUGINDIR");
17 CFG_DATADIR=CFG_DATADIR or os.getenv("PROSODY_DATADIR");
19 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
21 local function is_relative(path)
22         local path_sep = package.config:sub(1,1);
23         return ((path_sep == "/" and path:sub(1,1) ~= "/")
24                 or (path_sep == "\\" and (path:sub(1,1) ~= "/" and path:sub(2,3) ~= ":\\")))
25 end
27 -- Tell Lua where to find our libraries
28 if CFG_SOURCEDIR then
29         local function filter_relative_paths(path)
30                 if is_relative(path) then return ""; end
31         end
32         local function sanitise_paths(paths)
33                 return (paths:gsub("[^;]+;?", filter_relative_paths):gsub(";;+", ";"));
34         end
35         package.path = sanitise_paths(CFG_SOURCEDIR.."/?.lua;"..package.path);
36         package.cpath = sanitise_paths(CFG_SOURCEDIR.."/?.so;"..package.cpath);
37 end
39 -- Substitute ~ with path to home directory in data path
40 if CFG_DATADIR then
41         if os.getenv("HOME") then
42                 CFG_DATADIR = CFG_DATADIR:gsub("^~", os.getenv("HOME"));
43         end
44 end
46 if #arg > 0 and arg[1] ~= "--config" then
47         print("Unknown command-line option: "..tostring(arg[1]));
48         print("Perhaps you meant to use prosodyctl instead?");
49         return 1;
50 end
52 local startup = require "util.startup";
53 local async = require "util.async";
55 -- Note: it's important that this thread is not GC'd, as some C libraries
56 -- that are initialized here store a pointer to it ( :/ ).
57 local thread = async.runner();
59 thread:run(startup.prosody);
61 local function loop()
62         -- Error handler for errors that make it this far
63         local function catch_uncaught_error(err)
64                 if type(err) == "string" and err:match("interrupted!$") then
65                         return "quitting";
66                 end
68                 prosody.log("error", "Top-level error, please report:\n%s", tostring(err));
69                 local traceback = debug.traceback("", 2);
70                 if traceback then
71                         prosody.log("error", "%s", traceback);
72                 end
74                 prosody.events.fire_event("very-bad-error", {error = err, traceback = traceback});
75         end
77         local sleep = require"socket".sleep;
78         local server = require "net.server";
80         while select(2, xpcall(server.loop, catch_uncaught_error)) ~= "quitting" do
81                 sleep(0.2);
82         end
83 end
85 local function cleanup()
86         prosody.log("info", "Shutdown status: Cleaning up");
87         prosody.events.fire_event("server-cleanup");
88 end
90 loop();
92 prosody.log("info", "Shutting down...");
93 cleanup();
94 prosody.events.fire_event("server-stopped");
95 prosody.log("info", "Shutdown complete");
97 prosody.log("debug", "Shutdown reason was: %s", prosody.shutdown_reason or "not specified");
98 prosody.log("debug", "Exiting with status code: %d", prosody.shutdown_code or 0);
99 os.exit(prosody.shutdown_code);