util.pluginloader: Initial commit - a plugin resource loader
[prosody.git] / util / pluginloader.lua
1
2 local plugin_dir = CFG_PLUGINDIR or "./plugins/";
3
4 local io_open = io.open;
5 local loadstring = loadstring;
6
7 module "pluginloader"
8
9 local function load_file(name)
10         local file, err = io_open(plugin_dir..name);
11         if not file then return file, err; end
12         local content = file:read("*a");
13         file:close();
14         return content, name;
15 end
16
17 function load_resource(plugin, resource)
18         if not resource then
19                 resource = "mod_"..plugin..".lua";
20         end
21         local content, err = load_file(plugin.."/"..resource);
22         if not content then content, err = load_file(resource); end
23         -- TODO add support for packed plugins
24         return content, err;
25 end
26
27 function load_code(plugin, resource)
28         local content, err = load_resource(plugin, resource);
29         if not content then return content, err; end
30         return loadstring(content, err), err;
31 end
32
33 return _M;