Comparison

core/modulemanager.lua @ 1712:45a81d6d8777

Merge waqas with Tobias. Eww.
author Matthew Wild <mwild1@gmail.com>
date Tue, 18 Aug 2009 13:03:35 +0100
parent 1709:51cf905414a2
child 1733:d55ee6c66910
comparison
equal deleted inserted replaced
1686:232c2bf155c7 1712:45a81d6d8777
4 -- 4 --
5 -- This project is MIT/X11 licensed. Please see the 5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information. 6 -- COPYING file in the source package for more information.
7 -- 7 --
8 8
9
10
11 local plugin_dir = CFG_PLUGINDIR or "./plugins/"; 9 local plugin_dir = CFG_PLUGINDIR or "./plugins/";
12 10
13 local logger = require "util.logger"; 11 local logger = require "util.logger";
14 local log = logger.init("modulemanager"); 12 local log = logger.init("modulemanager");
15 local addDiscoInfoHandler = require "core.discomanager".addDiscoInfoHandler;
16 local eventmanager = require "core.eventmanager"; 13 local eventmanager = require "core.eventmanager";
17 local config = require "core.configmanager"; 14 local config = require "core.configmanager";
18 local multitable_new = require "util.multitable".new; 15 local multitable_new = require "util.multitable".new;
19 local register_actions = require "core.actions".register; 16 local register_actions = require "core.actions".register;
20 local st = require "util.stanza"; 17 local st = require "util.stanza";
48 local stanza_handlers = multitable_new(); 45 local stanza_handlers = multitable_new();
49 local handler_info = {}; 46 local handler_info = {};
50 47
51 local modulehelpers = setmetatable({}, { __index = _G }); 48 local modulehelpers = setmetatable({}, { __index = _G });
52 49
53 local features_table = multitable_new();
54 local identities_table = multitable_new();
55 local handler_table = multitable_new(); 50 local handler_table = multitable_new();
56 local hooked = multitable_new(); 51 local hooked = multitable_new();
57 local hooks = multitable_new(); 52 local hooks = multitable_new();
58 local event_hooks = multitable_new(); 53 local event_hooks = multitable_new();
59 54
169 if (not ok) and err then 164 if (not ok) and err then
170 log("warn", "Non-fatal error unloading module '%s' on '%s': %s", name, host, err); 165 log("warn", "Non-fatal error unloading module '%s' on '%s': %s", name, host, err);
171 end 166 end
172 end 167 end
173 modulemap[host][name] = nil; 168 modulemap[host][name] = nil;
174 features_table:remove(host, name);
175 identities_table:remove(host, name);
176 local params = handler_table:get(host, name); -- , {module.host, origin_type, tag, xmlns} 169 local params = handler_table:get(host, name); -- , {module.host, origin_type, tag, xmlns}
177 for _, param in pairs(params or NULL) do 170 for _, param in pairs(params or NULL) do
178 local handlers = stanza_handlers:get(param[1], param[2], param[3], param[4]); 171 local handlers = stanza_handlers:get(param[1], param[2], param[3], param[4]);
179 if handlers then 172 if handlers then
180 handler_info[handlers[1]] = nil; 173 handler_info[handlers[1]] = nil;
326 end 319 end
327 function api:add_iq_handler(origin_type, xmlns, handler) 320 function api:add_iq_handler(origin_type, xmlns, handler)
328 self:add_handler(origin_type, "iq", xmlns, handler); 321 self:add_handler(origin_type, "iq", xmlns, handler);
329 end 322 end
330 323
331 addDiscoInfoHandler("*host", function(reply, to, from, node)
332 if #node == 0 then
333 local done = {};
334 for module, identities in pairs(identities_table:get(to) or NULL) do -- for each module
335 for identity, attr in pairs(identities) do
336 if not done[identity] then
337 reply:tag("identity", attr):up(); -- TODO cache
338 done[identity] = true;
339 end
340 end
341 end
342 for module, identities in pairs(identities_table:get("*") or NULL) do -- for each module
343 for identity, attr in pairs(identities) do
344 if not done[identity] then
345 reply:tag("identity", attr):up(); -- TODO cache
346 done[identity] = true;
347 end
348 end
349 end
350 for module, features in pairs(features_table:get(to) or NULL) do -- for each module
351 for feature in pairs(features) do
352 if not done[feature] then
353 reply:tag("feature", {var = feature}):up(); -- TODO cache
354 done[feature] = true;
355 end
356 end
357 end
358 for module, features in pairs(features_table:get("*") or NULL) do -- for each module
359 for feature in pairs(features) do
360 if not done[feature] then
361 reply:tag("feature", {var = feature}):up(); -- TODO cache
362 done[feature] = true;
363 end
364 end
365 end
366 return next(done) ~= nil;
367 end
368 end);
369
370 function api:add_feature(xmlns) 324 function api:add_feature(xmlns)
371 features_table:set(self.host, self.name, xmlns, true); 325 self:add_item("feature", xmlns);
372 end 326 end
373 function api:add_identity(category, type) 327 function api:add_identity(category, type, name)
374 identities_table:set(self.host, self.name, category.."\0"..type, {category = category, type = type}); 328 self:add_item("identity", {category = category, type = type, name = name});
375 end 329 end
376 330
377 local event_hook = function(host, mod_name, event_name, ...) 331 local event_hook = function(host, mod_name, event_name, ...)
378 if type((...)) == "table" and (...).host and (...).host ~= host then return; end 332 if type((...)) == "table" and (...).host and (...).host ~= host then return; end
379 for handler in pairs(event_hooks:get(host, mod_name, event_name) or NULL) do 333 for handler in pairs(event_hooks:get(host, mod_name, event_name) or NULL) do
420 374
421 function api:get_option(name, default_value) 375 function api:get_option(name, default_value)
422 return config.get(self.host, self.name, name) or config.get(self.host, "core", name) or default_value; 376 return config.get(self.host, self.name, name) or config.get(self.host, "core", name) or default_value;
423 end 377 end
424 378
379 local t_remove = _G.table.remove;
380 local module_items = multitable_new();
381 function api:add_item(key, value)
382 self.items = self.items or {};
383 self.items[key] = self.items[key] or {};
384 t_insert(self.items[key], value);
385 self:fire_event("item-added/"..key, {source = self, item = value});
386 end
387 function api:remove_item(key, value)
388 local t = self.items and self.items[key] or NULL;
389 for i = #t,1,-1 do
390 if t[i] == value then
391 t_remove(self.items[key], i);
392 self:fire_event("item-removed/"..key, {source = self, item = value});
393 return value;
394 end
395 end
396 end
397
398 function api:get_host_items(key)
399 local result = {};
400 for mod_name, module in pairs(modulemap[self.host]) do
401 module = module.module;
402 if module.items then
403 for _, item in ipairs(module.items[key] or NULL) do
404 t_insert(result, item);
405 end
406 end
407 end
408 return result;
409 end
410
425 -------------------------------------------------------------------- 411 --------------------------------------------------------------------
426 412
427 local actions = {}; 413 local actions = {};
428 414
429 function actions.load(params) 415 function actions.load(params)