Annotate

util/pluginloader.lua @ 2743:fd1d4d96a708

Merge with 0.7
author Matthew Wild <mwild1@gmail.com>
date Fri, 05 Mar 2010 18:35:02 +0000
parent 2325:265441f2ad48
child 2925:692b3c6c5bd2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1522
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1441
diff changeset
1 -- Prosody IM
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1441
diff changeset
2 -- Copyright (C) 2008-2009 Matthew Wild
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1441
diff changeset
3 -- Copyright (C) 2008-2009 Waqas Hussain
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1441
diff changeset
4 --
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1441
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1441
diff changeset
6 -- COPYING file in the source package for more information.
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1441
diff changeset
7 --
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1441
diff changeset
8
1359
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
9
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
10 local plugin_dir = CFG_PLUGINDIR or "./plugins/";
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
11
2276
d9302be05f86 util.pluginloader: Support for fetching plugins from the data store
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
12 local io_open, os_time = io.open, os.time;
d9302be05f86 util.pluginloader: Support for fetching plugins from the data store
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
13 local loadstring, pairs = loadstring, pairs;
d9302be05f86 util.pluginloader: Support for fetching plugins from the data store
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
14
d9302be05f86 util.pluginloader: Support for fetching plugins from the data store
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
15 local datamanager = require "util.datamanager";
1359
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
16
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
17 module "pluginloader"
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
18
2276
d9302be05f86 util.pluginloader: Support for fetching plugins from the data store
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
19 local function load_from_datastore(name)
2325
265441f2ad48 util.pluginloader: Don't specify a host for the plugin data store (clever waqas...)
Matthew Wild <mwild1@gmail.com>
parents: 2276
diff changeset
20 local content = datamanager.load(name, nil, "plugins");
2276
d9302be05f86 util.pluginloader: Support for fetching plugins from the data store
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
21 if not content or not content[1] then return nil, "Resource not found"; end
d9302be05f86 util.pluginloader: Support for fetching plugins from the data store
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
22 return content[1], name;
d9302be05f86 util.pluginloader: Support for fetching plugins from the data store
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
23 end
d9302be05f86 util.pluginloader: Support for fetching plugins from the data store
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
24
1359
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
25 local function load_file(name)
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
26 local file, err = io_open(plugin_dir..name);
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
27 if not file then return file, err; end
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
28 local content = file:read("*a");
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
29 file:close();
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
30 return content, name;
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
31 end
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
32
2276
d9302be05f86 util.pluginloader: Support for fetching plugins from the data store
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
33 function load_resource(plugin, resource, loader)
1359
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
34 if not resource then
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
35 resource = "mod_"..plugin..".lua";
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
36 end
2276
d9302be05f86 util.pluginloader: Support for fetching plugins from the data store
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
37 loader = loader or load_file;
d9302be05f86 util.pluginloader: Support for fetching plugins from the data store
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
38
d9302be05f86 util.pluginloader: Support for fetching plugins from the data store
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
39 local content, err = loader(plugin.."/"..resource);
d9302be05f86 util.pluginloader: Support for fetching plugins from the data store
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
40 if not content then content, err = loader(resource); end
1359
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
41 -- TODO add support for packed plugins
2276
d9302be05f86 util.pluginloader: Support for fetching plugins from the data store
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
42
d9302be05f86 util.pluginloader: Support for fetching plugins from the data store
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
43 if not content and loader == load_file then
d9302be05f86 util.pluginloader: Support for fetching plugins from the data store
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
44 return load_resource(plugin, resource, load_from_datastore);
d9302be05f86 util.pluginloader: Support for fetching plugins from the data store
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
45 end
d9302be05f86 util.pluginloader: Support for fetching plugins from the data store
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
46
1359
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
47 return content, err;
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
48 end
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
49
2276
d9302be05f86 util.pluginloader: Support for fetching plugins from the data store
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
50 function store_resource(plugin, resource, content, metadata)
d9302be05f86 util.pluginloader: Support for fetching plugins from the data store
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
51 if not resource then
d9302be05f86 util.pluginloader: Support for fetching plugins from the data store
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
52 resource = "mod_"..plugin..".lua";
d9302be05f86 util.pluginloader: Support for fetching plugins from the data store
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
53 end
d9302be05f86 util.pluginloader: Support for fetching plugins from the data store
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
54 local store = { content };
d9302be05f86 util.pluginloader: Support for fetching plugins from the data store
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
55 if metadata then
d9302be05f86 util.pluginloader: Support for fetching plugins from the data store
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
56 for k,v in pairs(metadata) do
d9302be05f86 util.pluginloader: Support for fetching plugins from the data store
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
57 store[k] = v;
d9302be05f86 util.pluginloader: Support for fetching plugins from the data store
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
58 end
d9302be05f86 util.pluginloader: Support for fetching plugins from the data store
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
59 end
2325
265441f2ad48 util.pluginloader: Don't specify a host for the plugin data store (clever waqas...)
Matthew Wild <mwild1@gmail.com>
parents: 2276
diff changeset
60 datamanager.store(plugin.."/"..resource, nil, "plugins", store);
2276
d9302be05f86 util.pluginloader: Support for fetching plugins from the data store
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
61 end
d9302be05f86 util.pluginloader: Support for fetching plugins from the data store
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
62
1359
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
63 function load_code(plugin, resource)
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
64 local content, err = load_resource(plugin, resource);
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
65 if not content then return content, err; end
1441
9c6c7aa5dc60 util.pluginloader: Append "@" to chunk names (fixes weird formatting in plugin tracebacks)
Waqas Hussain <waqas20@gmail.com>
parents: 1387
diff changeset
66 return loadstring(content, "@"..err);
1359
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
67 end
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
68
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
69 return _M;