Comparison

core/modulemanager.lua @ 4896:27cda15104f2

modulemanager, moduleapi: Turn module.event_handlers into a multitable and track object->event->handler associations correctly (thanks Zash)
author Matthew Wild <mwild1@gmail.com>
date Sat, 19 May 2012 15:39:16 +0100
parent 4893:98ff89ab5d1d
child 5021:85b2689dbcfe
comparison
equal deleted inserted replaced
4895:36df30395c44 4896:27cda15104f2
8 8
9 local logger = require "util.logger"; 9 local logger = require "util.logger";
10 local log = logger.init("modulemanager"); 10 local log = logger.init("modulemanager");
11 local config = require "core.configmanager"; 11 local config = require "core.configmanager";
12 local pluginloader = require "util.pluginloader"; 12 local pluginloader = require "util.pluginloader";
13 local set = require "util.set";
14
15 local new_multitable = require "util.multitable".new;
13 16
14 local hosts = hosts; 17 local hosts = hosts;
15 local prosody = prosody; 18 local prosody = prosody;
16 19
17 local pcall, xpcall = pcall, xpcall; 20 local pcall, xpcall = pcall, xpcall;
24 local n = select("#", ...); 27 local n = select("#", ...);
25 local params = {...}; 28 local params = {...};
26 return xpcall(function() return f(unpack(params, 1, n)) end, function(e) return tostring(e).."\n"..debug_traceback(); end); 29 return xpcall(function() return f(unpack(params, 1, n)) end, function(e) return tostring(e).."\n"..debug_traceback(); end);
27 end 30 end
28 31
29 local set = require "util.set";
30
31 local autoload_modules = {"presence", "message", "iq", "offline", "c2s", "s2s"}; 32 local autoload_modules = {"presence", "message", "iq", "offline", "c2s", "s2s"};
32 local component_inheritable_modules = {"tls", "dialback", "iq", "s2s"}; 33 local component_inheritable_modules = {"tls", "dialback", "iq", "s2s"};
33 34
34 -- We need this to let modules access the real global namespace 35 -- We need this to let modules access the real global namespace
35 local _G = _G; 36 local _G = _G;
89 if (not ok) and err then 90 if (not ok) and err then
90 log("warn", "Non-fatal error unloading module '%s' on '%s': %s", name, host, err); 91 log("warn", "Non-fatal error unloading module '%s' on '%s': %s", name, host, err);
91 end 92 end
92 end 93 end
93 94
94 for event, data in pairs(mod.module.event_handlers) do 95 for object, event, handler in mod.module.event_handlers:iter(nil, nil, nil) do
95 data.object.remove_handler(event, data.handler); 96 object.remove_handler(event, handler);
96 end 97 end
97 98
98 if mod.module.items then -- remove items 99 if mod.module.items then -- remove items
99 local events = (host == "*" and prosody.events) or hosts[host].events; 100 local events = (host == "*" and prosody.events) or hosts[host].events;
100 for key,t in pairs(mod.module.items) do 101 for key,t in pairs(mod.module.items) do
130 elseif modulemap["*"][module_name] then 131 elseif modulemap["*"][module_name] then
131 local mod = modulemap["*"][module_name]; 132 local mod = modulemap["*"][module_name];
132 if module_has_method(mod, "add_host") then 133 if module_has_method(mod, "add_host") then
133 local _log = logger.init(host..":"..module_name); 134 local _log = logger.init(host..":"..module_name);
134 local host_module_api = setmetatable({ 135 local host_module_api = setmetatable({
135 host = host, event_handlers = {}, items = {}; 136 host = host, event_handlers = new_multitable(), items = {};
136 _log = _log, log = function (self, ...) return _log(...); end; 137 _log = _log, log = function (self, ...) return _log(...); end;
137 },{ 138 },{
138 __index = modulemap["*"][module_name].module; 139 __index = modulemap["*"][module_name].module;
139 }); 140 });
140 local host_module = setmetatable({ module = host_module_api }, { __index = mod }); 141 local host_module = setmetatable({ module = host_module_api }, { __index = mod });
157 return nil, err; 158 return nil, err;
158 end 159 end
159 160
160 local _log = logger.init(host..":"..module_name); 161 local _log = logger.init(host..":"..module_name);
161 local api_instance = setmetatable({ name = module_name, host = host, path = err, 162 local api_instance = setmetatable({ name = module_name, host = host, path = err,
162 _log = _log, log = function (self, ...) return _log(...); end, event_handlers = {} } 163 _log = _log, log = function (self, ...) return _log(...); end, event_handlers = new_multitable() }
163 , { __index = api }); 164 , { __index = api });
164 165
165 local pluginenv = setmetatable({ module = api_instance }, { __index = _G }); 166 local pluginenv = setmetatable({ module = api_instance }, { __index = _G });
166 api_instance.environment = pluginenv; 167 api_instance.environment = pluginenv;
167 168