util/pluginloader.lua

1
-- Prosody IM
2
-- Copyright (C) 2008-2010 Matthew Wild
3
-- Copyright (C) 2008-2010 Waqas Hussain
4
-- 
5
-- This project is MIT/X11 licensed. Please see the
6
-- COPYING file in the source package for more information.
7
--
8
 
9
 
10
local plugin_dir = CFG_PLUGINDIR or "./plugins/";
11
 
12
local io_open, os_time = io.open, os.time;
13
local loadstring, pairs = loadstring, pairs;
14
 
15
module "pluginloader"
16
 
17
local function load_file(name)
18
	local file, err = io_open(plugin_dir..name);
19
	if not file then return file, err; end
20
	local content = file:read("*a");
21
	file:close();
22
	return content, name;
23
end
24
 
25
function load_resource(plugin, resource, loader)
26
	local path, name = plugin:match("([^/]*)/?(.*)");
27
	if name == "" then
28
		if not resource then
29
			resource = "mod_"..plugin..".lua";
30
		end
31
		loader = loader or load_file;
32
 
33
		local content, err = loader(plugin.."/"..resource);
34
		if not content then content, err = loader(resource); end
35
		-- TODO add support for packed plugins
36
		
37
		return content, err;
38
	else
39
		if not resource then
40
			resource = "mod_"..name..".lua";
41
		end
42
		loader = loader or load_file;
43
 
44
		local content, err = loader(plugin.."/"..resource);
45
		if not content then content, err = loader(path.."/"..resource); end
46
		-- TODO add support for packed plugins
47
		
48
		return content, err;
49
	end
50
end
51
 
52
function load_code(plugin, resource)
53
	local content, err = load_resource(plugin, resource);
54
	if not content then return content, err; end
55
	return loadstring(content, "@"..err);
56
end
57
 
58
return _M;