Merge 0.10->trunk
[prosody.git] / 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 local dir_sep, path_sep = package.config:match("^(%S+)%s(%S+)");
10 local plugin_dir = {};
11 for path in (CFG_PLUGINDIR or "./plugins/"):gsub("[/\\]", dir_sep):gmatch("[^"..path_sep.."]+") do
12         path = path..dir_sep; -- add path separator to path end
13         path = path:gsub(dir_sep..dir_sep.."+", dir_sep); -- coalesce multiple separaters
14         plugin_dir[#plugin_dir + 1] = path;
15 end
16
17 local io_open = io.open;
18 local envload = require "util.envload".envload;
19
20 local function load_file(names)
21         local file, err, path;
22         for i=1,#plugin_dir do
23                 for j=1,#names do
24                         path = plugin_dir[i]..names[j];
25                         file, err = io_open(path);
26                         if file then
27                                 local content = file:read("*a");
28                                 file:close();
29                                 return content, path;
30                         end
31                 end
32         end
33         return file, err;
34 end
35
36 local function load_resource(plugin, resource)
37         resource = resource or "mod_"..plugin..".lua";
38
39         local names = {
40                 "mod_"..plugin..dir_sep..plugin..dir_sep..resource; -- mod_hello/hello/mod_hello.lua
41                 "mod_"..plugin..dir_sep..resource;                  -- mod_hello/mod_hello.lua
42                 plugin..dir_sep..resource;                          -- hello/mod_hello.lua
43                 resource;                                           -- mod_hello.lua
44         };
45
46         return load_file(names);
47 end
48
49 local function load_code(plugin, resource, env)
50         local content, err = load_resource(plugin, resource);
51         if not content then return content, err; end
52         local path = err;
53         local f, err = envload(content, "@"..path, env);
54         if not f then return f, err; end
55         return f, path;
56 end
57
58 local function load_code_ext(plugin, resource, extension, env)
59         local content, err = load_resource(plugin, resource.."."..extension);
60         if not content then
61                 content, err = load_resource(resource, resource.."."..extension);
62                 if not content then
63                         return content, err;
64                 end
65         end
66         local path = err;
67         local f, err = envload(content, "@"..path, env);
68         if not f then return f, err; end
69         return f, path;
70 end
71
72 return {
73         load_file = load_file;
74         load_resource = load_resource;
75         load_code = load_code;
76         load_code_ext = load_code_ext;
77 };