util.dataforms: Add list-multi support
[prosody.git] / prosody
diff --git a/prosody b/prosody
index 46f3331f004bc9030f5866c5c50a6bcb3e104dc2..6ec9131e5a27aa49b196326ecb6e7db986d2d245 100755 (executable)
--- a/prosody
+++ b/prosody
@@ -22,9 +22,6 @@ if CFG_SOURCEDIR then
        package.cpath = CFG_SOURCEDIR.."/?.so;"..package.cpath;
 end
 
-package.path = package.path..";"..(CFG_SOURCEDIR or ".").."/fallbacks/?.lua";
-package.cpath = package.cpath..";"..(CFG_SOURCEDIR or ".").."/fallbacks/?.so";
-
 -- Substitute ~ with path to home directory in data path
 if CFG_DATADIR then
        if os.getenv("HOME") then
@@ -32,6 +29,10 @@ if CFG_DATADIR then
        end
 end
 
+-- Global 'prosody' object
+prosody = { events = require "util.events".new(); };
+local prosody = prosody;
+
 -- Load the config-parsing module
 config = require "core.configmanager"
 
@@ -123,15 +124,38 @@ function sandbox_require()
        end
 end
 
+function set_function_metatable()
+       local mt = {};
+       function mt.__index(f, upvalue)
+               local i, name, value = 0;
+               repeat
+                       i = i + 1;
+                       name, value = debug.getupvalue(f, i);
+               until name == upvalue or name == nil;
+               return value;
+       end
+       function mt.__newindex(f, upvalue, value)
+               local i, name = 0;
+               repeat
+                       i = i + 1;
+                       name = debug.getupvalue(f, i);
+               until name == upvalue or name == nil;
+               if name then
+                       debug.setupvalue(f, i, value);
+               end
+       end
+       function mt.__tostring(f)
+               local info = debug.getinfo(f);
+               return ("function(%s:%d)"):format(info.short_src:match("[^\\/]*$"), info.linedefined);
+       end
+       debug.setmetatable(function() end, mt);
+end
+
 function init_global_state()
        bare_sessions = {};
        full_sessions = {};
        hosts = {};
 
-       -- Global 'prosody' object
-       prosody = {};
-       local prosody = prosody;
-       
        prosody.bare_sessions = bare_sessions;
        prosody.full_sessions = full_sessions;
        prosody.hosts = hosts;
@@ -139,10 +163,25 @@ function init_global_state()
        prosody.paths = { source = CFG_SOURCEDIR, config = CFG_CONFIGDIR, 
                          plugins = CFG_PLUGINDIR, data = CFG_DATADIR };
        
+       local path_sep = package.config:sub(1,1);
+       local rel_path_start = ".."..path_sep;
+       function prosody.resolve_relative_path(path)
+               if path then
+                       local is_relative;
+                       if path_sep == "/" and path:sub(1,1) ~= "/" then
+                               is_relative = true;
+                       elseif path_sep == "\\" and (path:sub(1,1) ~= "/" and path:sub(2,3) ~= ":\\") then
+                               is_relative = true;
+                       end
+                       if is_relative then
+                               return CFG_CONFIGDIR..path_sep..path;
+                       end
+               end
+               return path;
+       end
+       
        prosody.arg = _G.arg;
 
-       prosody.events = require "util.events".new();
-       
        prosody.platform = "unknown";
        if os.getenv("WINDIR") then
                prosody.platform = "windows";
@@ -173,7 +212,6 @@ function init_global_state()
        -- Function to reopen logfiles
        function prosody.reopen_logfiles()
                log("info", "Re-opening log files");
-               eventmanager.fire_event("reopen-log-files"); -- Handled by appropriate log sinks
                prosody.events.fire_event("reopen-log-files");
        end
 
@@ -264,9 +302,9 @@ end
 function load_secondary_libraries()
        --- Load and initialise core modules
        require "util.import"
+       require "util.xmppstream"
        require "core.xmlhandlers"
        require "core.rostermanager"
-       require "core.eventmanager"
        require "core.hostmanager"
        require "core.modulemanager"
        require "core.usermanager"
@@ -310,7 +348,6 @@ end
 function prepare_to_start()
        log("info", "Prosody is using the %s backend for connection handling", server.get_backend());
        -- Signal to modules that we are ready to start
-       eventmanager.fire_event("server-starting");
        prosody.events.fire_event("server-starting");
 
        -- start listening on sockets
@@ -418,6 +455,7 @@ read_config();
 init_logging();
 check_dependencies();
 sandbox_require();
+set_function_metatable();
 load_libraries();
 init_global_state();
 read_version();
@@ -427,14 +465,12 @@ init_data_store();
 init_global_protection();
 prepare_to_start();
 
-eventmanager.fire_event("server-started");
 prosody.events.fire_event("server-started");
 
 loop();
 
 log("info", "Shutting down...");
 cleanup();
-eventmanager.fire_event("server-stopped");
 prosody.events.fire_event("server-stopped");
 log("info", "Shutdown complete");