X-Git-Url: https://git.enpas.org/?a=blobdiff_plain;f=util%2Fpluginloader.lua;h=0d7eafa7a154122d3af7242bac792349963794b6;hb=a8958cbe5ebad3abc12efa7a76c8aedac8f21389;hp=3a4e80030022b08043cd2e6441cd7803e55fdcc2;hpb=e272ab057d341287ccb6085cf00cc304ed0c8d40;p=prosody.git diff --git a/util/pluginloader.lua b/util/pluginloader.lua index 3a4e8003..0d7eafa7 100644 --- a/util/pluginloader.lua +++ b/util/pluginloader.lua @@ -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,54 +14,49 @@ 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; - -module "pluginloader" +local io_open = io.open; +local envload = require "util.envload".envload; -local function load_file(name) +local function load_file(names) local file, err, path; for i=1,#plugin_dir do - path = plugin_dir[i]..name; - file, err = io_open(path); - if file then break; end + for j=1,#names do + path = plugin_dir[i]..names[j]; + file, err = io_open(path); + if file then + local content = file:read("*a"); + file:close(); + return content, path; + end + end end - if not file then return file, err; end - local content = file:read("*a"); - file:close(); - return content, path; + return file, err; end -function load_resource(plugin, resource) - local path, name = plugin:match("([^/]*)/?(.*)"); - if name == "" then - if not resource then - resource = "mod_"..plugin..".lua"; - end +local function load_resource(plugin, resource) + resource = resource or "mod_"..plugin..".lua"; - local content, err = load_file(plugin.."/"..resource); - if not content then content, err = load_file(resource); end - - return content, err; - else - if not resource then - resource = "mod_"..name..".lua"; - end + local names = { + "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 + }; - local content, err = load_file(plugin.."/"..resource); - if not content then content, err = load_file(path.."/"..resource); end - - return content, err; - end + return load_file(names); end -function load_code(plugin, resource) +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; - local f, err = loadstring(content, "@"..path); + local f, err = envload(content, "@"..path, env); if not f then return f, err; end return f, path; end -return _M; +return { + load_file = load_file; + load_resource = load_resource; + load_code = load_code; +};