util.prosodyctl: Handle os.execute in Lua 5.2 returning true when command terminates...
[prosody.git] / util / pluginloader.lua
index 8b4dbd1673394e8c01e01a43f4f33cd99dc16286..004855f092ee7cabb4655b9533b14642ecb685cf 100644 (file)
@@ -1,7 +1,7 @@
 -- Prosody IM
 -- Copyright (C) 2008-2010 Matthew Wild
 -- Copyright (C) 2008-2010 Waqas Hussain
--- 
+--
 -- This project is MIT/X11 licensed. Please see the
 -- COPYING file in the source package for more information.
 --
@@ -14,12 +14,9 @@ for path in (CFG_PLUGINDIR or "./plugins/"):gsub("[/\\]", dir_sep):gmatch("[^"..
        plugin_dir[#plugin_dir + 1] = path;
 end
 
-local io_open, os_time = io.open, os.time;
-local loadstring, pairs = loadstring, pairs;
+local io_open = io.open;
 local envload = require "util.envload".envload;
 
-module "pluginloader"
-
 local function load_file(names)
        local file, err, path;
        for i=1,#plugin_dir do
@@ -36,20 +33,20 @@ local function load_file(names)
        return file, err;
 end
 
-function load_resource(plugin, resource)
+local function load_resource(plugin, resource)
        resource = resource or "mod_"..plugin..".lua";
 
        local names = {
-               "mod_"..plugin.."/"..plugin.."/"..resource; -- mod_hello/hello/mod_hello.lua
-               "mod_"..plugin.."/"..resource;              -- mod_hello/mod_hello.lua
-               plugin.."/"..resource;                      -- hello/mod_hello.lua
-               resource;                                   -- mod_hello.lua
+               "mod_"..plugin..dir_sep..plugin..dir_sep..resource; -- mod_hello/hello/mod_hello.lua
+               "mod_"..plugin..dir_sep..resource;                  -- mod_hello/mod_hello.lua
+               plugin..dir_sep..resource;                          -- hello/mod_hello.lua
+               resource;                                           -- mod_hello.lua
        };
 
        return load_file(names);
 end
 
-function load_code(plugin, resource, env)
+local function load_code(plugin, resource, env)
        local content, err = load_resource(plugin, resource);
        if not content then return content, err; end
        local path = err;
@@ -58,4 +55,23 @@ function load_code(plugin, resource, env)
        return f, path;
 end
 
-return _M;
+local function load_code_ext(plugin, resource, extension, env)
+       local content, err = load_resource(plugin, resource.."."..extension);
+       if not content then
+               content, err = load_resource(resource, resource.."."..extension);
+               if not content then
+                       return content, err;
+               end
+       end
+       local path = err;
+       local f, err = envload(content, "@"..path, env);
+       if not f then return f, err; end
+       return f, path;
+end
+
+return {
+       load_file = load_file;
+       load_resource = load_resource;
+       load_code = load_code;
+       load_code_ext = load_code_ext;
+};