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