core/modulemanager.lua
author Waqas Hussain <waqas20@gmail.com>
Sat, 17 Sep 2011 20:28:46 +0500
changeset 4381 bf81b4784853
parent 4363 35ebd8e5747a
child 4383 718445c040c4
permissions -rw-r--r--
modulemanager: Fixed undefined global access in broadcast of item-remove events on module unload.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1523
841d61be198f Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents: 1505
diff changeset
     1
-- Prosody IM
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2828
diff changeset
     2
-- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2828
diff changeset
     3
-- Copyright (C) 2008-2010 Waqas Hussain
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 467
diff changeset
     4
-- 
758
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 748
diff changeset
     5
-- This project is MIT/X11 licensed. Please see the
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 748
diff changeset
     6
-- COPYING file in the source package for more information.
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 467
diff changeset
     7
--
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 467
diff changeset
     8
438
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
     9
local logger = require "util.logger";
540
ec03f6968fa8 Added function add_feature to modules API (for adding disco features)
Waqas Hussain <waqas20@gmail.com>
parents: 519
diff changeset
    10
local log = logger.init("modulemanager");
573
f6555ebf84ec Move module loading to modulemanager
Matthew Wild <mwild1@gmail.com>
parents: 569
diff changeset
    11
local config = require "core.configmanager";
578
5879264e28e2 Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents: 573
diff changeset
    12
local multitable_new = require "util.multitable".new;
1174
f7b6d5839092 modulemanager: require util.stanza
Waqas Hussain <waqas20@gmail.com>
parents: 1173
diff changeset
    13
local st = require "util.stanza";
1360
857034905016 modulemanager: Changed to use util.pluginloader
Waqas Hussain <waqas20@gmail.com>
parents: 1346
diff changeset
    14
local pluginloader = require "util.pluginloader";
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    15
747
40837f3422ab modulemanager: Add get_host_type() API method, and fix up call_module_method to work properly
Matthew Wild <mwild1@gmail.com>
parents: 746
diff changeset
    16
local hosts = hosts;
1247
4721e4124692 modulemanager: module:hook now allows global modules to hook events on the prosody.events object
Waqas Hussain <waqas20@gmail.com>
parents: 1231
diff changeset
    17
local prosody = prosody;
3433
b567ac8e2040 modulemanager: Remove dependency on eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 3180
diff changeset
    18
local prosody_events = prosody.events;
747
40837f3422ab modulemanager: Add get_host_type() API method, and fix up call_module_method to work properly
Matthew Wild <mwild1@gmail.com>
parents: 746
diff changeset
    19
2977
686f9a5a7f5e modulemanager: Log proper tracebacks on errors during module load/unload.
Waqas Hussain <waqas20@gmail.com>
parents: 2951
diff changeset
    20
local loadfile, pcall, xpcall = loadfile, pcall, xpcall;
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    21
local setmetatable, setfenv, getfenv = setmetatable, setfenv, getfenv;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    22
local pairs, ipairs = pairs, ipairs;
745
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
    23
local t_insert, t_concat = table.insert, table.concat;
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    24
local type = type;
670
d5cf10b7fc44 Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents: 637
diff changeset
    25
local next = next;
674
4f506c627b49 modulemanager: module.unload now gets called when modules are being unloaded
Waqas Hussain <waqas20@gmail.com>
parents: 670
diff changeset
    26
local rawget = rawget;
1392
9935ddfd8ccf modulemanager: Added simple module:require implementation
Waqas Hussain <waqas20@gmail.com>
parents: 1361
diff changeset
    27
local error = error;
2151
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
    28
local tostring, tonumber = tostring, tonumber;
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
    29
2977
686f9a5a7f5e modulemanager: Log proper tracebacks on errors during module load/unload.
Waqas Hussain <waqas20@gmail.com>
parents: 2951
diff changeset
    30
local debug_traceback = debug.traceback;
686f9a5a7f5e modulemanager: Log proper tracebacks on errors during module load/unload.
Waqas Hussain <waqas20@gmail.com>
parents: 2951
diff changeset
    31
local unpack, select = unpack, select;
686f9a5a7f5e modulemanager: Log proper tracebacks on errors during module load/unload.
Waqas Hussain <waqas20@gmail.com>
parents: 2951
diff changeset
    32
pcall = function(f, ...)
686f9a5a7f5e modulemanager: Log proper tracebacks on errors during module load/unload.
Waqas Hussain <waqas20@gmail.com>
parents: 2951
diff changeset
    33
	local n = select("#", ...);
686f9a5a7f5e modulemanager: Log proper tracebacks on errors during module load/unload.
Waqas Hussain <waqas20@gmail.com>
parents: 2951
diff changeset
    34
	local params = {...};
3588
1e570ed17147 modulemanager: Fixed: Locally defined pcall wasn't returning return values of the called function.
Waqas Hussain <waqas20@gmail.com>
parents: 3587
diff changeset
    35
	return xpcall(function() return f(unpack(params, 1, n)) end, function(e) return tostring(e).."\n"..debug_traceback(); end);
2977
686f9a5a7f5e modulemanager: Log proper tracebacks on errors during module load/unload.
Waqas Hussain <waqas20@gmail.com>
parents: 2951
diff changeset
    36
end
686f9a5a7f5e modulemanager: Log proper tracebacks on errors during module load/unload.
Waqas Hussain <waqas20@gmail.com>
parents: 2951
diff changeset
    37
2151
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
    38
local array, set = require "util.array", require "util.set";
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    39
3967
1a9bcbaeb55a modulemanager: Auto-load mod_offline.
Waqas Hussain <waqas20@gmail.com>
parents: 3758
diff changeset
    40
local autoload_modules = {"presence", "message", "iq", "offline"};
3677
dad563ed54aa modulemanager: Allow components to inherit mod_iq. This allows modules loaded on components to hook IQ stanza sub-events ("iq-set/bare/xmlns:tag", etc).
Waqas Hussain <waqas20@gmail.com>
parents: 3623
diff changeset
    41
local component_inheritable_modules = {"tls", "dialback", "iq"};
1505
e19cb945c25b modulemanager: Small code improvement, move autoloaded modules list to the top of the file
Matthew Wild <mwild1@gmail.com>
parents: 1504
diff changeset
    42
467
66f145f5c932 Update Makefile to now pass config paths to prosody. Update prosody, modulemanager and connectionlisteners to obey these paths.
Matthew Wild <mwild1@gmail.com>
parents: 439
diff changeset
    43
-- We need this to let modules access the real global namespace
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    44
local _G = _G;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    45
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    46
module "modulemanager"
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    47
1389
846df07536eb modulemanager: Expose api table to allow others to extend the module API
Matthew Wild <mwild1@gmail.com>
parents: 1386
diff changeset
    48
api = {};
846df07536eb modulemanager: Expose api table to allow others to extend the module API
Matthew Wild <mwild1@gmail.com>
parents: 1386
diff changeset
    49
local api = api; -- Module API container
438
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
    50
584
eb0cea29c8d7 Temporary hack for global modules
Matthew Wild <mwild1@gmail.com>
parents: 579
diff changeset
    51
local modulemap = { ["*"] = {} };
438
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
    52
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    53
local modulehelpers = setmetatable({}, { __index = _G });
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    54
1259
6bd11bca9725 modulemanager: Keep track of event handlers added by module:hook, and remove them on module unload
Waqas Hussain <waqas20@gmail.com>
parents: 1253
diff changeset
    55
local hooks = multitable_new();
686
13ed38531f69 modulemanager: Per-host event hooks for plugins - solves issue 41
Waqas Hussain <waqas20@gmail.com>
parents: 675
diff changeset
    56
670
d5cf10b7fc44 Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents: 637
diff changeset
    57
local NULL = {};
d5cf10b7fc44 Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents: 637
diff changeset
    58
573
f6555ebf84ec Move module loading to modulemanager
Matthew Wild <mwild1@gmail.com>
parents: 569
diff changeset
    59
-- Load modules when a host is activated
f6555ebf84ec Move module loading to modulemanager
Matthew Wild <mwild1@gmail.com>
parents: 569
diff changeset
    60
function load_modules_for_host(host)
3595
ec1151d0c4a4 modulemanager: load_modules_for_host(): Inherit 'tls' and 'dialback' from global modules list for components, and load the component module. Also refactored to use util.set.
Waqas Hussain <waqas20@gmail.com>
parents: 3588
diff changeset
    61
	local component = config.get(host, "core", "component_module");
ec1151d0c4a4 modulemanager: load_modules_for_host(): Inherit 'tls' and 'dialback' from global modules list for components, and load the component module. Also refactored to use util.set.
Waqas Hussain <waqas20@gmail.com>
parents: 3588
diff changeset
    62
	
ec1151d0c4a4 modulemanager: load_modules_for_host(): Inherit 'tls' and 'dialback' from global modules list for components, and load the component module. Also refactored to use util.set.
Waqas Hussain <waqas20@gmail.com>
parents: 3588
diff changeset
    63
	local global_modules_enabled = config.get("*", "core", "modules_enabled");
ec1151d0c4a4 modulemanager: load_modules_for_host(): Inherit 'tls' and 'dialback' from global modules list for components, and load the component module. Also refactored to use util.set.
Waqas Hussain <waqas20@gmail.com>
parents: 3588
diff changeset
    64
	local global_modules_disabled = config.get("*", "core", "modules_disabled");
ec1151d0c4a4 modulemanager: load_modules_for_host(): Inherit 'tls' and 'dialback' from global modules list for components, and load the component module. Also refactored to use util.set.
Waqas Hussain <waqas20@gmail.com>
parents: 3588
diff changeset
    65
	local host_modules_enabled = config.get(host, "core", "modules_enabled");
ec1151d0c4a4 modulemanager: load_modules_for_host(): Inherit 'tls' and 'dialback' from global modules list for components, and load the component module. Also refactored to use util.set.
Waqas Hussain <waqas20@gmail.com>
parents: 3588
diff changeset
    66
	local host_modules_disabled = config.get(host, "core", "modules_disabled");
ec1151d0c4a4 modulemanager: load_modules_for_host(): Inherit 'tls' and 'dialback' from global modules list for components, and load the component module. Also refactored to use util.set.
Waqas Hussain <waqas20@gmail.com>
parents: 3588
diff changeset
    67
	
ec1151d0c4a4 modulemanager: load_modules_for_host(): Inherit 'tls' and 'dialback' from global modules list for components, and load the component module. Also refactored to use util.set.
Waqas Hussain <waqas20@gmail.com>
parents: 3588
diff changeset
    68
	if host_modules_enabled == global_modules_enabled then host_modules_enabled = nil; end
ec1151d0c4a4 modulemanager: load_modules_for_host(): Inherit 'tls' and 'dialback' from global modules list for components, and load the component module. Also refactored to use util.set.
Waqas Hussain <waqas20@gmail.com>
parents: 3588
diff changeset
    69
	if host_modules_disabled == global_modules_disabled then host_modules_disabled = nil; end
ec1151d0c4a4 modulemanager: load_modules_for_host(): Inherit 'tls' and 'dialback' from global modules list for components, and load the component module. Also refactored to use util.set.
Waqas Hussain <waqas20@gmail.com>
parents: 3588
diff changeset
    70
	
ec1151d0c4a4 modulemanager: load_modules_for_host(): Inherit 'tls' and 'dialback' from global modules list for components, and load the component module. Also refactored to use util.set.
Waqas Hussain <waqas20@gmail.com>
parents: 3588
diff changeset
    71
	local global_modules = set.new(autoload_modules) + set.new(global_modules_enabled) - set.new(global_modules_disabled);
ec1151d0c4a4 modulemanager: load_modules_for_host(): Inherit 'tls' and 'dialback' from global modules list for components, and load the component module. Also refactored to use util.set.
Waqas Hussain <waqas20@gmail.com>
parents: 3588
diff changeset
    72
	if component then
3596
bbeba9f2acf8 modulemanager: load_modules_for_host(): For components, the inherited modules are the intersection of the inheritable and global modules lists, not the difference.
Waqas Hussain <waqas20@gmail.com>
parents: 3595
diff changeset
    73
		global_modules = set.intersection(set.new(component_inheritable_modules), global_modules);
1960
1e674dae31ae modulemanager: Re-organise module loading to still work when no global modules_enabled is defined in the config (thanks hoelzro for accidentally discovering this one)
Matthew Wild <mwild1@gmail.com>
parents: 1946
diff changeset
    74
	end
4135
9dfb3c0101b5 modulemanager: Fix disabling a module on a single host
Paul Aurich <paul@darkrain42.org>
parents: 4002
diff changeset
    75
	local modules = (global_modules + set.new(host_modules_enabled)) - set.new(host_modules_disabled);
3595
ec1151d0c4a4 modulemanager: load_modules_for_host(): Inherit 'tls' and 'dialback' from global modules list for components, and load the component module. Also refactored to use util.set.
Waqas Hussain <waqas20@gmail.com>
parents: 3588
diff changeset
    76
	
3758
41f174b61b6a modulemanager, mod_console: Rename mod_console -> mod_admin_telnet - add compatibility code to modulemanager for existing configs
Matthew Wild <mwild1@gmail.com>
parents: 3677
diff changeset
    77
	-- COMPAT w/ pre 0.8
41f174b61b6a modulemanager, mod_console: Rename mod_console -> mod_admin_telnet - add compatibility code to modulemanager for existing configs
Matthew Wild <mwild1@gmail.com>
parents: 3677
diff changeset
    78
	if modules:contains("console") then
41f174b61b6a modulemanager, mod_console: Rename mod_console -> mod_admin_telnet - add compatibility code to modulemanager for existing configs
Matthew Wild <mwild1@gmail.com>
parents: 3677
diff changeset
    79
		log("error", "The mod_console plugin has been renamed to mod_admin_telnet. Please update your config.");
41f174b61b6a modulemanager, mod_console: Rename mod_console -> mod_admin_telnet - add compatibility code to modulemanager for existing configs
Matthew Wild <mwild1@gmail.com>
parents: 3677
diff changeset
    80
		modules:remove("console");
41f174b61b6a modulemanager, mod_console: Rename mod_console -> mod_admin_telnet - add compatibility code to modulemanager for existing configs
Matthew Wild <mwild1@gmail.com>
parents: 3677
diff changeset
    81
		modules:add("admin_telnet");
41f174b61b6a modulemanager, mod_console: Rename mod_console -> mod_admin_telnet - add compatibility code to modulemanager for existing configs
Matthew Wild <mwild1@gmail.com>
parents: 3677
diff changeset
    82
	end
41f174b61b6a modulemanager, mod_console: Rename mod_console -> mod_admin_telnet - add compatibility code to modulemanager for existing configs
Matthew Wild <mwild1@gmail.com>
parents: 3677
diff changeset
    83
	
3595
ec1151d0c4a4 modulemanager: load_modules_for_host(): Inherit 'tls' and 'dialback' from global modules list for components, and load the component module. Also refactored to use util.set.
Waqas Hussain <waqas20@gmail.com>
parents: 3588
diff changeset
    84
	if component then
ec1151d0c4a4 modulemanager: load_modules_for_host(): Inherit 'tls' and 'dialback' from global modules list for components, and load the component module. Also refactored to use util.set.
Waqas Hussain <waqas20@gmail.com>
parents: 3588
diff changeset
    85
		load(host, component);
637
30b8ad9f7b70 Fix for not loading global modules when host-specific modules are specified in config
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
    86
	end
3595
ec1151d0c4a4 modulemanager: load_modules_for_host(): Inherit 'tls' and 'dialback' from global modules list for components, and load the component module. Also refactored to use util.set.
Waqas Hussain <waqas20@gmail.com>
parents: 3588
diff changeset
    87
	for module in modules do
ec1151d0c4a4 modulemanager: load_modules_for_host(): Inherit 'tls' and 'dialback' from global modules list for components, and load the component module. Also refactored to use util.set.
Waqas Hussain <waqas20@gmail.com>
parents: 3588
diff changeset
    88
		load(host, module);
573
f6555ebf84ec Move module loading to modulemanager
Matthew Wild <mwild1@gmail.com>
parents: 569
diff changeset
    89
	end
f6555ebf84ec Move module loading to modulemanager
Matthew Wild <mwild1@gmail.com>
parents: 569
diff changeset
    90
end
3433
b567ac8e2040 modulemanager: Remove dependency on eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 3180
diff changeset
    91
prosody_events.add_handler("host-activated", load_modules_for_host);
573
f6555ebf84ec Move module loading to modulemanager
Matthew Wild <mwild1@gmail.com>
parents: 569
diff changeset
    92
--
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    93
438
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
    94
function load(host, module_name, config)
439
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
    95
	if not (host and module_name) then
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
    96
		return nil, "insufficient-parameters";
3598
48ebea937260 modulemanager: Return an error when loading a module on a non-existent host, don't create the host. Removes dependecy on componentmanager.
Waqas Hussain <waqas20@gmail.com>
parents: 3596
diff changeset
    97
	elseif not hosts[host] then
48ebea937260 modulemanager: Return an error when loading a module on a non-existent host, don't create the host. Removes dependecy on componentmanager.
Waqas Hussain <waqas20@gmail.com>
parents: 3596
diff changeset
    98
		return nil, "unknown-host";
439
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
    99
	end
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   100
	
438
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
   101
	if not modulemap[host] then
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
   102
		modulemap[host] = {};
769
9e76018c62fa Fix for never checking if the first module for a host is already loaded (affects global modules)
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
   103
	end
9e76018c62fa Fix for never checking if the first module for a host is already loaded (affects global modules)
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
   104
	
9e76018c62fa Fix for never checking if the first module for a host is already loaded (affects global modules)
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
   105
	if modulemap[host][module_name] then
438
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
   106
		log("warn", "%s is already loaded for %s, so not loading again", module_name, host);
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
   107
		return nil, "module-already-loaded";
584
eb0cea29c8d7 Temporary hack for global modules
Matthew Wild <mwild1@gmail.com>
parents: 579
diff changeset
   108
	elseif modulemap["*"][module_name] then
eb0cea29c8d7 Temporary hack for global modules
Matthew Wild <mwild1@gmail.com>
parents: 579
diff changeset
   109
		return nil, "global-module-already-loaded";
438
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
   110
	end
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
   111
	
584
eb0cea29c8d7 Temporary hack for global modules
Matthew Wild <mwild1@gmail.com>
parents: 579
diff changeset
   112
1360
857034905016 modulemanager: Changed to use util.pluginloader
Waqas Hussain <waqas20@gmail.com>
parents: 1346
diff changeset
   113
	local mod, err = pluginloader.load_code(module_name);
584
eb0cea29c8d7 Temporary hack for global modules
Matthew Wild <mwild1@gmail.com>
parents: 579
diff changeset
   114
	if not mod then
eb0cea29c8d7 Temporary hack for global modules
Matthew Wild <mwild1@gmail.com>
parents: 579
diff changeset
   115
		log("error", "Unable to load module '%s': %s", module_name or "nil", err or "nil");
eb0cea29c8d7 Temporary hack for global modules
Matthew Wild <mwild1@gmail.com>
parents: 579
diff changeset
   116
		return nil, err;
eb0cea29c8d7 Temporary hack for global modules
Matthew Wild <mwild1@gmail.com>
parents: 579
diff changeset
   117
	end
eb0cea29c8d7 Temporary hack for global modules
Matthew Wild <mwild1@gmail.com>
parents: 579
diff changeset
   118
438
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
   119
	local _log = logger.init(host..":"..module_name);
4151
a1ac8f064dcb modulemanager: Added module.path to the plugin API to let plugins determine their load path.
Waqas Hussain <waqas20@gmail.com>
parents: 4135
diff changeset
   120
	local api_instance = setmetatable({ name = module_name, host = host, path = err, config = config,  _log = _log, log = function (self, ...) return _log(...); end }, { __index = api });
438
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
   121
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
   122
	local pluginenv = setmetatable({ module = api_instance }, { __index = _G });
2584
5091548a3805 modulemanager: Make the plugin environment available directly (module.environment within plugins).
Waqas Hussain <waqas20@gmail.com>
parents: 2479
diff changeset
   123
	api_instance.environment = pluginenv;
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   124
	
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   125
	setfenv(mod, pluginenv);
1987
94ecd3e7be87 modulemanager: Fixed traceback on loading modules on unknown hosts.
Waqas Hussain <waqas20@gmail.com>
parents: 1986
diff changeset
   126
	hosts[host].modules = modulemap[host];
1994
9cc9b096c8f5 modulemanager: Module loading rewritten to gracefully deal with errors in module initialization.
Waqas Hussain <waqas20@gmail.com>
parents: 1993
diff changeset
   127
	modulemap[host][module_name] = pluginenv;
438
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
   128
	
1994
9cc9b096c8f5 modulemanager: Module loading rewritten to gracefully deal with errors in module initialization.
Waqas Hussain <waqas20@gmail.com>
parents: 1993
diff changeset
   129
	local success, err = pcall(mod);
9cc9b096c8f5 modulemanager: Module loading rewritten to gracefully deal with errors in module initialization.
Waqas Hussain <waqas20@gmail.com>
parents: 1993
diff changeset
   130
	if success then
9cc9b096c8f5 modulemanager: Module loading rewritten to gracefully deal with errors in module initialization.
Waqas Hussain <waqas20@gmail.com>
parents: 1993
diff changeset
   131
		if module_has_method(pluginenv, "load") then
9cc9b096c8f5 modulemanager: Module loading rewritten to gracefully deal with errors in module initialization.
Waqas Hussain <waqas20@gmail.com>
parents: 1993
diff changeset
   132
			success, err = call_module_method(pluginenv, "load");
9cc9b096c8f5 modulemanager: Module loading rewritten to gracefully deal with errors in module initialization.
Waqas Hussain <waqas20@gmail.com>
parents: 1993
diff changeset
   133
			if not success then
9cc9b096c8f5 modulemanager: Module loading rewritten to gracefully deal with errors in module initialization.
Waqas Hussain <waqas20@gmail.com>
parents: 1993
diff changeset
   134
				log("warn", "Error loading module '%s' on '%s': %s", module_name, host, err or "nil");
9cc9b096c8f5 modulemanager: Module loading rewritten to gracefully deal with errors in module initialization.
Waqas Hussain <waqas20@gmail.com>
parents: 1993
diff changeset
   135
			end
1378
d09ecc8ee1ef modulemanager: Call 'load' method when loading a module
Matthew Wild <mwild1@gmail.com>
parents: 1361
diff changeset
   136
		end
d09ecc8ee1ef modulemanager: Call 'load' method when loading a module
Matthew Wild <mwild1@gmail.com>
parents: 1361
diff changeset
   137
1994
9cc9b096c8f5 modulemanager: Module loading rewritten to gracefully deal with errors in module initialization.
Waqas Hussain <waqas20@gmail.com>
parents: 1993
diff changeset
   138
		-- Use modified host, if the module set one
9cc9b096c8f5 modulemanager: Module loading rewritten to gracefully deal with errors in module initialization.
Waqas Hussain <waqas20@gmail.com>
parents: 1993
diff changeset
   139
		if api_instance.host == "*" and host ~= "*" then
9cc9b096c8f5 modulemanager: Module loading rewritten to gracefully deal with errors in module initialization.
Waqas Hussain <waqas20@gmail.com>
parents: 1993
diff changeset
   140
			modulemap[host][module_name] = nil;
9cc9b096c8f5 modulemanager: Module loading rewritten to gracefully deal with errors in module initialization.
Waqas Hussain <waqas20@gmail.com>
parents: 1993
diff changeset
   141
			modulemap["*"][module_name] = pluginenv;
9cc9b096c8f5 modulemanager: Module loading rewritten to gracefully deal with errors in module initialization.
Waqas Hussain <waqas20@gmail.com>
parents: 1993
diff changeset
   142
			api_instance:set_global();
9cc9b096c8f5 modulemanager: Module loading rewritten to gracefully deal with errors in module initialization.
Waqas Hussain <waqas20@gmail.com>
parents: 1993
diff changeset
   143
		end
9cc9b096c8f5 modulemanager: Module loading rewritten to gracefully deal with errors in module initialization.
Waqas Hussain <waqas20@gmail.com>
parents: 1993
diff changeset
   144
	else
9cc9b096c8f5 modulemanager: Module loading rewritten to gracefully deal with errors in module initialization.
Waqas Hussain <waqas20@gmail.com>
parents: 1993
diff changeset
   145
		log("error", "Error initializing module '%s' on '%s': %s", module_name, host, err or "nil");
1069
034e345c0f8d modulemanager: Update logger when a module becomes global
Matthew Wild <mwild1@gmail.com>
parents: 929
diff changeset
   146
	end
1994
9cc9b096c8f5 modulemanager: Module loading rewritten to gracefully deal with errors in module initialization.
Waqas Hussain <waqas20@gmail.com>
parents: 1993
diff changeset
   147
	if success then
2659
ba6dd11f7259 modulemanager: Fix traceback on unloading global modules (thanks KSid)
Matthew Wild <mwild1@gmail.com>
parents: 2585
diff changeset
   148
		(hosts[api_instance.host] or prosody).events.fire_event("module-loaded", { module = module_name, host = host });
1994
9cc9b096c8f5 modulemanager: Module loading rewritten to gracefully deal with errors in module initialization.
Waqas Hussain <waqas20@gmail.com>
parents: 1993
diff changeset
   149
		return true;
9cc9b096c8f5 modulemanager: Module loading rewritten to gracefully deal with errors in module initialization.
Waqas Hussain <waqas20@gmail.com>
parents: 1993
diff changeset
   150
	else -- load failed, unloading
9cc9b096c8f5 modulemanager: Module loading rewritten to gracefully deal with errors in module initialization.
Waqas Hussain <waqas20@gmail.com>
parents: 1993
diff changeset
   151
		unload(api_instance.host, module_name);
9cc9b096c8f5 modulemanager: Module loading rewritten to gracefully deal with errors in module initialization.
Waqas Hussain <waqas20@gmail.com>
parents: 1993
diff changeset
   152
		return nil, err;
9cc9b096c8f5 modulemanager: Module loading rewritten to gracefully deal with errors in module initialization.
Waqas Hussain <waqas20@gmail.com>
parents: 1993
diff changeset
   153
	end
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   154
end
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   155
745
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   156
function get_module(host, name)
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   157
	return modulemap[host] and modulemap[host][name];
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   158
end
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   159
439
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
   160
function is_loaded(host, name)
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
   161
	return modulemap[host] and modulemap[host][name] and true;
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
   162
end
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
   163
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
   164
function unload(host, name, ...)
2278
8c10f13c0c20 modulemanager, net.dns: Remove trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2270
diff changeset
   165
	local mod = get_module(host, name);
439
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
   166
	if not mod then return nil, "module-not-loaded"; end
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
   167
	
745
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   168
	if module_has_method(mod, "unload") then
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   169
		local ok, err = call_module_method(mod, "unload");
439
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
   170
		if (not ok) and err then
745
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   171
			log("warn", "Non-fatal error unloading module '%s' on '%s': %s", name, host, err);
439
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
   172
		end
674
4f506c627b49 modulemanager: module.unload now gets called when modules are being unloaded
Waqas Hussain <waqas20@gmail.com>
parents: 670
diff changeset
   173
	end
1259
6bd11bca9725 modulemanager: Keep track of event handlers added by module:hook, and remove them on module unload
Waqas Hussain <waqas20@gmail.com>
parents: 1253
diff changeset
   174
	-- unhook event handlers hooked by module:hook
6bd11bca9725 modulemanager: Keep track of event handlers added by module:hook, and remove them on module unload
Waqas Hussain <waqas20@gmail.com>
parents: 1253
diff changeset
   175
	for event, handlers in pairs(hooks:get(host, name) or NULL) do
6bd11bca9725 modulemanager: Keep track of event handlers added by module:hook, and remove them on module unload
Waqas Hussain <waqas20@gmail.com>
parents: 1253
diff changeset
   176
		for handler in pairs(handlers or NULL) do
6bd11bca9725 modulemanager: Keep track of event handlers added by module:hook, and remove them on module unload
Waqas Hussain <waqas20@gmail.com>
parents: 1253
diff changeset
   177
			(hosts[host] or prosody).events.remove_handler(event, handler);
6bd11bca9725 modulemanager: Keep track of event handlers added by module:hook, and remove them on module unload
Waqas Hussain <waqas20@gmail.com>
parents: 1253
diff changeset
   178
		end
6bd11bca9725 modulemanager: Keep track of event handlers added by module:hook, and remove them on module unload
Waqas Hussain <waqas20@gmail.com>
parents: 1253
diff changeset
   179
	end
4344
a24ee48ec30d modulemanager: Add module:hook_global(name, handler, priority) to hook global (server-wide) events
Matthew Wild <mwild1@gmail.com>
parents: 4337
diff changeset
   180
	-- unhook event handlers hooked by module:hook_global
a24ee48ec30d modulemanager: Add module:hook_global(name, handler, priority) to hook global (server-wide) events
Matthew Wild <mwild1@gmail.com>
parents: 4337
diff changeset
   181
	for event, handlers in pairs(hooks:get("*", name) or NULL) do
a24ee48ec30d modulemanager: Add module:hook_global(name, handler, priority) to hook global (server-wide) events
Matthew Wild <mwild1@gmail.com>
parents: 4337
diff changeset
   182
		for handler in pairs(handlers or NULL) do
a24ee48ec30d modulemanager: Add module:hook_global(name, handler, priority) to hook global (server-wide) events
Matthew Wild <mwild1@gmail.com>
parents: 4337
diff changeset
   183
			prosody.events.remove_handler(event, handler);
a24ee48ec30d modulemanager: Add module:hook_global(name, handler, priority) to hook global (server-wide) events
Matthew Wild <mwild1@gmail.com>
parents: 4337
diff changeset
   184
		end
a24ee48ec30d modulemanager: Add module:hook_global(name, handler, priority) to hook global (server-wide) events
Matthew Wild <mwild1@gmail.com>
parents: 4337
diff changeset
   185
	end
1259
6bd11bca9725 modulemanager: Keep track of event handlers added by module:hook, and remove them on module unload
Waqas Hussain <waqas20@gmail.com>
parents: 1253
diff changeset
   186
	hooks:remove(host, name);
2828
fbddc3ed0d09 modulemanager: Fire item-removed events on module unload.
Waqas Hussain <waqas20@gmail.com>
parents: 2072
diff changeset
   187
	if mod.module.items then -- remove items
fbddc3ed0d09 modulemanager: Fire item-removed events on module unload.
Waqas Hussain <waqas20@gmail.com>
parents: 2072
diff changeset
   188
		for key,t in pairs(mod.module.items) do
fbddc3ed0d09 modulemanager: Fire item-removed events on module unload.
Waqas Hussain <waqas20@gmail.com>
parents: 2072
diff changeset
   189
			for i = #t,1,-1 do
fbddc3ed0d09 modulemanager: Fire item-removed events on module unload.
Waqas Hussain <waqas20@gmail.com>
parents: 2072
diff changeset
   190
				local value = t[i];
fbddc3ed0d09 modulemanager: Fire item-removed events on module unload.
Waqas Hussain <waqas20@gmail.com>
parents: 2072
diff changeset
   191
				t[i] = nil;
4381
bf81b4784853 modulemanager: Fixed undefined global access in broadcast of item-remove events on module unload.
Waqas Hussain <waqas20@gmail.com>
parents: 4363
diff changeset
   192
				hosts[host].events.fire_event("item-removed/"..key, {source = mod.module, item = value});
2828
fbddc3ed0d09 modulemanager: Fire item-removed events on module unload.
Waqas Hussain <waqas20@gmail.com>
parents: 2072
diff changeset
   193
			end
fbddc3ed0d09 modulemanager: Fire item-removed events on module unload.
Waqas Hussain <waqas20@gmail.com>
parents: 2072
diff changeset
   194
		end
fbddc3ed0d09 modulemanager: Fire item-removed events on module unload.
Waqas Hussain <waqas20@gmail.com>
parents: 2072
diff changeset
   195
	end
1986
d4ba9d94eb74 modulemanager: Slightly rearranged code for more robust unloading of modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1960
diff changeset
   196
	modulemap[host][name] = nil;
2659
ba6dd11f7259 modulemanager: Fix traceback on unloading global modules (thanks KSid)
Matthew Wild <mwild1@gmail.com>
parents: 2585
diff changeset
   197
	(hosts[host] or prosody).events.fire_event("module-unloaded", { module = name, host = host });
670
d5cf10b7fc44 Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents: 637
diff changeset
   198
	return true;
439
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
   199
end
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
   200
710
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   201
function reload(host, name, ...)
745
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   202
	local mod = get_module(host, name);
710
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   203
	if not mod then return nil, "module-not-loaded"; end
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   204
1360
857034905016 modulemanager: Changed to use util.pluginloader
Waqas Hussain <waqas20@gmail.com>
parents: 1346
diff changeset
   205
	local _mod, err = pluginloader.load_code(name); -- checking for syntax errors
713
2afd6d9e21cd modulemanager: Check for syntax errors before reloading a module
Waqas Hussain <waqas20@gmail.com>
parents: 710
diff changeset
   206
	if not _mod then
1386
9132f16666e4 modulemanager: Fix copy/paste error, should be name instead of module_name
Matthew Wild <mwild1@gmail.com>
parents: 1378
diff changeset
   207
		log("error", "Unable to load module '%s': %s", name or "nil", err or "nil");
713
2afd6d9e21cd modulemanager: Check for syntax errors before reloading a module
Waqas Hussain <waqas20@gmail.com>
parents: 710
diff changeset
   208
		return nil, err;
2afd6d9e21cd modulemanager: Check for syntax errors before reloading a module
Waqas Hussain <waqas20@gmail.com>
parents: 710
diff changeset
   209
	end
2afd6d9e21cd modulemanager: Check for syntax errors before reloading a module
Waqas Hussain <waqas20@gmail.com>
parents: 710
diff changeset
   210
710
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   211
	local saved;
745
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   212
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   213
	if module_has_method(mod, "save") then
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   214
		local ok, ret, err = call_module_method(mod, "save");
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   215
		if ok then
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   216
			saved = ret;
710
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   217
		else
745
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   218
			log("warn", "Error saving module '%s:%s' state: %s", host, module, ret);
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   219
			if not config.get(host, "core", "force_module_reload") then
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   220
				log("warn", "Aborting reload due to error, set force_module_reload to ignore this");
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   221
				return nil, "save-state-failed";
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   222
			else
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   223
				log("warn", "Continuing with reload (using the force)");
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   224
			end
710
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   225
		end
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   226
	end
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   227
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   228
	unload(host, name, ...);
745
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   229
	local ok, err = load(host, name, ...);
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   230
	if ok then
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   231
		mod = get_module(host, name);
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   232
		if module_has_method(mod, "restore") then
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   233
			local ok, err = call_module_method(mod, "restore", saved or {})
710
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   234
			if (not ok) and err then
745
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   235
				log("warn", "Error restoring module '%s' from '%s': %s", name, host, err);
710
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   236
			end
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   237
		end
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   238
		return true;
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   239
	end
745
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   240
	return ok, err;
710
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   241
end
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   242
745
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   243
function module_has_method(module, method)
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   244
	return type(module.module[method]) == "function";
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   245
end
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   246
747
40837f3422ab modulemanager: Add get_host_type() API method, and fix up call_module_method to work properly
Matthew Wild <mwild1@gmail.com>
parents: 746
diff changeset
   247
function call_module_method(module, method, ...)
2278
8c10f13c0c20 modulemanager, net.dns: Remove trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2270
diff changeset
   248
	if module_has_method(module, method) then
748
172c43d735e9 modulemanager: Really fix call_module_method to work properly
Matthew Wild <mwild1@gmail.com>
parents: 747
diff changeset
   249
		local f = module.module[method];
745
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   250
		return pcall(f, ...);
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   251
	else
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   252
		return false, "no-such-method";
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   253
	end
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   254
end
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   255
438
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
   256
----- API functions exposed to modules -----------
2278
8c10f13c0c20 modulemanager, net.dns: Remove trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2270
diff changeset
   257
-- Must all be in api.*
438
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
   258
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
   259
-- Returns the name of the current module
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
   260
function api:get_name()
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
   261
	return self.name;
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
   262
end
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
   263
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
   264
-- Returns the host that the current module is serving
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
   265
function api:get_host()
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
   266
	return self.host;
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
   267
end
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
   268
747
40837f3422ab modulemanager: Add get_host_type() API method, and fix up call_module_method to work properly
Matthew Wild <mwild1@gmail.com>
parents: 746
diff changeset
   269
function api:get_host_type()
40837f3422ab modulemanager: Add get_host_type() API method, and fix up call_module_method to work properly
Matthew Wild <mwild1@gmail.com>
parents: 746
diff changeset
   270
	return hosts[self.host].type;
40837f3422ab modulemanager: Add get_host_type() API method, and fix up call_module_method to work properly
Matthew Wild <mwild1@gmail.com>
parents: 746
diff changeset
   271
end
40837f3422ab modulemanager: Add get_host_type() API method, and fix up call_module_method to work properly
Matthew Wild <mwild1@gmail.com>
parents: 746
diff changeset
   272
746
7027de4c039d modulemanager: Add module:set_global() as a cleaner way for a module to declare itself 'global'
Matthew Wild <mwild1@gmail.com>
parents: 745
diff changeset
   273
function api:set_global()
7027de4c039d modulemanager: Add module:set_global() as a cleaner way for a module to declare itself 'global'
Matthew Wild <mwild1@gmail.com>
parents: 745
diff changeset
   274
	self.host = "*";
1069
034e345c0f8d modulemanager: Update logger when a module becomes global
Matthew Wild <mwild1@gmail.com>
parents: 929
diff changeset
   275
	-- Update the logger
034e345c0f8d modulemanager: Update logger when a module becomes global
Matthew Wild <mwild1@gmail.com>
parents: 929
diff changeset
   276
	local _log = logger.init("mod_"..self.name);
034e345c0f8d modulemanager: Update logger when a module becomes global
Matthew Wild <mwild1@gmail.com>
parents: 929
diff changeset
   277
	self.log = function (self, ...) return _log(...); end;
034e345c0f8d modulemanager: Update logger when a module becomes global
Matthew Wild <mwild1@gmail.com>
parents: 929
diff changeset
   278
	self._log = _log;
746
7027de4c039d modulemanager: Add module:set_global() as a cleaner way for a module to declare itself 'global'
Matthew Wild <mwild1@gmail.com>
parents: 745
diff changeset
   279
end
7027de4c039d modulemanager: Add module:set_global() as a cleaner way for a module to declare itself 'global'
Matthew Wild <mwild1@gmail.com>
parents: 745
diff changeset
   280
540
ec03f6968fa8 Added function add_feature to modules API (for adding disco features)
Waqas Hussain <waqas20@gmail.com>
parents: 519
diff changeset
   281
function api:add_feature(xmlns)
1697
f1783e621a36 modulemanager: Module API functions add_item and add_feature updated to use the add_item API
Waqas Hussain <waqas20@gmail.com>
parents: 1695
diff changeset
   282
	self:add_item("feature", xmlns);
540
ec03f6968fa8 Added function add_feature to modules API (for adding disco features)
Waqas Hussain <waqas20@gmail.com>
parents: 519
diff changeset
   283
end
1695
9786e650393b modulemanager: Changed the add_identity module API to accept an optional name parameter (the 'name' attribute for the <identity/> element)
Waqas Hussain <waqas20@gmail.com>
parents: 1694
diff changeset
   284
function api:add_identity(category, type, name)
1697
f1783e621a36 modulemanager: Module API functions add_item and add_feature updated to use the add_item API
Waqas Hussain <waqas20@gmail.com>
parents: 1695
diff changeset
   285
	self:add_item("identity", {category = category, type = type, name = name});
1334
6846c5cc9ce2 modulemanager: Added function add_identity(category, type) to the modules API
Waqas Hussain <waqas20@gmail.com>
parents: 1330
diff changeset
   286
end
4337
a2ee8ab82dd9 core.modulemanager, mod_disco: Add support for XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents: 4151
diff changeset
   287
function api:add_extension(data)
a2ee8ab82dd9 core.modulemanager, mod_disco: Add support for XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents: 4151
diff changeset
   288
	self:add_item("extension", data);
a2ee8ab82dd9 core.modulemanager, mod_disco: Add support for XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents: 4151
diff changeset
   289
end
540
ec03f6968fa8 Added function add_feature to modules API (for adding disco features)
Waqas Hussain <waqas20@gmail.com>
parents: 519
diff changeset
   290
1183
565e16ee0c74 modulemanager: Add module:fire_event() to module API
Matthew Wild <mwild1@gmail.com>
parents: 1174
diff changeset
   291
function api:fire_event(...)
1253
60156584c442 modulemanager: Stopped using core.eventmanager in module:fire_event
Waqas Hussain <waqas20@gmail.com>
parents: 1250
diff changeset
   292
	return (hosts[self.host] or prosody).events.fire_event(...);
1183
565e16ee0c74 modulemanager: Add module:fire_event() to module API
Matthew Wild <mwild1@gmail.com>
parents: 1174
diff changeset
   293
end
565e16ee0c74 modulemanager: Add module:fire_event() to module API
Matthew Wild <mwild1@gmail.com>
parents: 1174
diff changeset
   294
1319
812c028a877d modulemanager: Allow setting priority of stanza handlers
Matthew Wild <mwild1@gmail.com>
parents: 1318
diff changeset
   295
function api:hook(event, handler, priority)
1259
6bd11bca9725 modulemanager: Keep track of event handlers added by module:hook, and remove them on module unload
Waqas Hussain <waqas20@gmail.com>
parents: 1253
diff changeset
   296
	hooks:set(self.host, self.name, event, handler, true);
1319
812c028a877d modulemanager: Allow setting priority of stanza handlers
Matthew Wild <mwild1@gmail.com>
parents: 1318
diff changeset
   297
	(hosts[self.host] or prosody).events.add_handler(event, handler, priority);
1231
6f251813f1e5 modulemanager: Added hook(event, handler) to the plugin API
Waqas Hussain <waqas20@gmail.com>
parents: 1183
diff changeset
   298
end
6f251813f1e5 modulemanager: Added hook(event, handler) to the plugin API
Waqas Hussain <waqas20@gmail.com>
parents: 1183
diff changeset
   299
4344
a24ee48ec30d modulemanager: Add module:hook_global(name, handler, priority) to hook global (server-wide) events
Matthew Wild <mwild1@gmail.com>
parents: 4337
diff changeset
   300
function api:hook_global(event, handler, priority)
a24ee48ec30d modulemanager: Add module:hook_global(name, handler, priority) to hook global (server-wide) events
Matthew Wild <mwild1@gmail.com>
parents: 4337
diff changeset
   301
	hooks:set("*", self.name, event, handler, true);
a24ee48ec30d modulemanager: Add module:hook_global(name, handler, priority) to hook global (server-wide) events
Matthew Wild <mwild1@gmail.com>
parents: 4337
diff changeset
   302
	prosody.events.add_handler(event, handler, priority);
a24ee48ec30d modulemanager: Add module:hook_global(name, handler, priority) to hook global (server-wide) events
Matthew Wild <mwild1@gmail.com>
parents: 4337
diff changeset
   303
end
a24ee48ec30d modulemanager: Add module:hook_global(name, handler, priority) to hook global (server-wide) events
Matthew Wild <mwild1@gmail.com>
parents: 4337
diff changeset
   304
1319
812c028a877d modulemanager: Allow setting priority of stanza handlers
Matthew Wild <mwild1@gmail.com>
parents: 1318
diff changeset
   305
function api:hook_stanza(xmlns, name, handler, priority)
1318
66f76c332a75 modulemanager: Add module:hook([xmlns, ] name, handler) which uses new stanza events
Matthew Wild <mwild1@gmail.com>
parents: 1309
diff changeset
   306
	if not handler and type(name) == "function" then
66f76c332a75 modulemanager: Add module:hook([xmlns, ] name, handler) which uses new stanza events
Matthew Wild <mwild1@gmail.com>
parents: 1309
diff changeset
   307
		-- If only 2 options then they specified no xmlns
1319
812c028a877d modulemanager: Allow setting priority of stanza handlers
Matthew Wild <mwild1@gmail.com>
parents: 1318
diff changeset
   308
		xmlns, name, handler, priority = nil, xmlns, name, handler;
1321
0698d0d39b35 modulemanager: Don't require xmlns to be non-nil
Matthew Wild <mwild1@gmail.com>
parents: 1320
diff changeset
   309
	elseif not (handler and name) then
1320
9c736b993e07 modulemanager: module:log() -> self:log() fix
Matthew Wild <mwild1@gmail.com>
parents: 1319
diff changeset
   310
		self:log("warn", "Error: Insufficient parameters to module:hook_stanza()");
1318
66f76c332a75 modulemanager: Add module:hook([xmlns, ] name, handler) which uses new stanza events
Matthew Wild <mwild1@gmail.com>
parents: 1309
diff changeset
   311
		return;
66f76c332a75 modulemanager: Add module:hook([xmlns, ] name, handler) which uses new stanza events
Matthew Wild <mwild1@gmail.com>
parents: 1309
diff changeset
   312
	end
1319
812c028a877d modulemanager: Allow setting priority of stanza handlers
Matthew Wild <mwild1@gmail.com>
parents: 1318
diff changeset
   313
	return api.hook(self, "stanza/"..(xmlns and (xmlns..":") or "")..name, function (data) return handler(data.origin, data.stanza, data); end, priority);
1318
66f76c332a75 modulemanager: Add module:hook([xmlns, ] name, handler) which uses new stanza events
Matthew Wild <mwild1@gmail.com>
parents: 1309
diff changeset
   314
end
66f76c332a75 modulemanager: Add module:hook([xmlns, ] name, handler) which uses new stanza events
Matthew Wild <mwild1@gmail.com>
parents: 1309
diff changeset
   315
1392
9935ddfd8ccf modulemanager: Added simple module:require implementation
Waqas Hussain <waqas20@gmail.com>
parents: 1361
diff changeset
   316
function api:require(lib)
9935ddfd8ccf modulemanager: Added simple module:require implementation
Waqas Hussain <waqas20@gmail.com>
parents: 1361
diff changeset
   317
	local f, n = pluginloader.load_code(self.name, lib..".lib.lua");
9935ddfd8ccf modulemanager: Added simple module:require implementation
Waqas Hussain <waqas20@gmail.com>
parents: 1361
diff changeset
   318
	if not f then
9935ddfd8ccf modulemanager: Added simple module:require implementation
Waqas Hussain <waqas20@gmail.com>
parents: 1361
diff changeset
   319
		f, n = pluginloader.load_code(lib, lib..".lib.lua");
9935ddfd8ccf modulemanager: Added simple module:require implementation
Waqas Hussain <waqas20@gmail.com>
parents: 1361
diff changeset
   320
	end
9935ddfd8ccf modulemanager: Added simple module:require implementation
Waqas Hussain <waqas20@gmail.com>
parents: 1361
diff changeset
   321
	if not f then error("Failed to load plugin library '"..lib.."', error: "..n); end -- FIXME better error message
2585
a9e99897b2e8 modulemanager: Load plugin libraries in the same environment as the plugins.
Waqas Hussain <waqas20@gmail.com>
parents: 2584
diff changeset
   322
	setfenv(f, self.environment);
1392
9935ddfd8ccf modulemanager: Added simple module:require implementation
Waqas Hussain <waqas20@gmail.com>
parents: 1361
diff changeset
   323
	return f();
9935ddfd8ccf modulemanager: Added simple module:require implementation
Waqas Hussain <waqas20@gmail.com>
parents: 1361
diff changeset
   324
end
9935ddfd8ccf modulemanager: Added simple module:require implementation
Waqas Hussain <waqas20@gmail.com>
parents: 1361
diff changeset
   325
1586
5c627d5d5e37 modulemanager: Added get_option(name, default_value) to plugin API
Waqas Hussain <waqas20@gmail.com>
parents: 1523
diff changeset
   326
function api:get_option(name, default_value)
2072
464a5392bc80 modulemanager: api:get_option(): Handle correctly option values of boolean false, don't assume value unset
Matthew Wild <mwild1@gmail.com>
parents: 1994
diff changeset
   327
	local value = config.get(self.host, self.name, name);
464a5392bc80 modulemanager: api:get_option(): Handle correctly option values of boolean false, don't assume value unset
Matthew Wild <mwild1@gmail.com>
parents: 1994
diff changeset
   328
	if value == nil then
464a5392bc80 modulemanager: api:get_option(): Handle correctly option values of boolean false, don't assume value unset
Matthew Wild <mwild1@gmail.com>
parents: 1994
diff changeset
   329
		value = config.get(self.host, "core", name);
464a5392bc80 modulemanager: api:get_option(): Handle correctly option values of boolean false, don't assume value unset
Matthew Wild <mwild1@gmail.com>
parents: 1994
diff changeset
   330
		if value == nil then
464a5392bc80 modulemanager: api:get_option(): Handle correctly option values of boolean false, don't assume value unset
Matthew Wild <mwild1@gmail.com>
parents: 1994
diff changeset
   331
			value = default_value;
464a5392bc80 modulemanager: api:get_option(): Handle correctly option values of boolean false, don't assume value unset
Matthew Wild <mwild1@gmail.com>
parents: 1994
diff changeset
   332
		end
464a5392bc80 modulemanager: api:get_option(): Handle correctly option values of boolean false, don't assume value unset
Matthew Wild <mwild1@gmail.com>
parents: 1994
diff changeset
   333
	end
464a5392bc80 modulemanager: api:get_option(): Handle correctly option values of boolean false, don't assume value unset
Matthew Wild <mwild1@gmail.com>
parents: 1994
diff changeset
   334
	return value;
1586
5c627d5d5e37 modulemanager: Added get_option(name, default_value) to plugin API
Waqas Hussain <waqas20@gmail.com>
parents: 1523
diff changeset
   335
end
5c627d5d5e37 modulemanager: Added get_option(name, default_value) to plugin API
Waqas Hussain <waqas20@gmail.com>
parents: 1523
diff changeset
   336
2349
e0d9f4000d6b modulemanager: Fixed a global access.
Waqas Hussain <waqas20@gmail.com>
parents: 2278
diff changeset
   337
function api:get_option_string(name, default_value)
e0d9f4000d6b modulemanager: Fixed a global access.
Waqas Hussain <waqas20@gmail.com>
parents: 2278
diff changeset
   338
	local value = self:get_option(name, default_value);
2151
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   339
	if type(value) == "table" then
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   340
		if #value > 1 then
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   341
			self:log("error", "Config option '%s' does not take a list, using just the first item", name);
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   342
		end
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   343
		value = value[1];
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   344
	end
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   345
	if value == nil then
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   346
		return nil;
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   347
	end
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   348
	return tostring(value);
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   349
end
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   350
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   351
function api:get_option_number(name, ...)
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   352
	local value = self:get_option(name, ...);
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   353
	if type(value) == "table" then
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   354
		if #value > 1 then
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   355
			self:log("error", "Config option '%s' does not take a list, using just the first item", name);
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   356
		end
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   357
		value = value[1];
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   358
	end
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   359
	local ret = tonumber(value);
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   360
	if value ~= nil and ret == nil then
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   361
		self:log("error", "Config option '%s' not understood, expecting a number", name);
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   362
	end
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   363
	return ret;
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   364
end
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   365
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   366
function api:get_option_boolean(name, ...)
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   367
	local value = self:get_option(name, ...);
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   368
	if type(value) == "table" then
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   369
		if #value > 1 then
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   370
			self:log("error", "Config option '%s' does not take a list, using just the first item", name);
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   371
		end
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   372
		value = value[1];
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   373
	end
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   374
	if value == nil then
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   375
		return nil;
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   376
	end
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   377
	local ret = value == true or value == "true" or value == 1 or nil;
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   378
	if ret == nil then
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   379
		ret = (value == false or value == "false" or value == 0);
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   380
		if ret then
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   381
			ret = false;
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   382
		else
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   383
			ret = nil;
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   384
		end
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   385
	end
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   386
	if ret == nil then
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   387
		self:log("error", "Config option '%s' not understood, expecting true/false", name);
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   388
	end
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   389
	return ret;
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   390
end
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   391
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   392
function api:get_option_array(name, ...)
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   393
	local value = self:get_option(name, ...);
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   394
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   395
	if value == nil then
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   396
		return nil;
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   397
	end
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   398
	
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   399
	if type(value) ~= "table" then
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   400
		return array{ value }; -- Assume any non-list is a single-item list
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   401
	end
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   402
	
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   403
	return array():append(value); -- Clone
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   404
end
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   405
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   406
function api:get_option_set(name, ...)
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   407
	local value = self:get_option_array(name, ...);
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   408
	
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   409
	if value == nil then
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   410
		return nil;
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   411
	end
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   412
	
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   413
	return set.new(value);
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   414
end
3bb7c1daa93f modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Matthew Wild <mwild1@gmail.com>
parents: 2072
diff changeset
   415
1694
0b7888c0995d Added: functions add_item and remove_item to add and remove items to the module API
Waqas Hussain <waqas20@gmail.com>
parents: 1586
diff changeset
   416
local t_remove = _G.table.remove;
0b7888c0995d Added: functions add_item and remove_item to add and remove items to the module API
Waqas Hussain <waqas20@gmail.com>
parents: 1586
diff changeset
   417
local module_items = multitable_new();
0b7888c0995d Added: functions add_item and remove_item to add and remove items to the module API
Waqas Hussain <waqas20@gmail.com>
parents: 1586
diff changeset
   418
function api:add_item(key, value)
0b7888c0995d Added: functions add_item and remove_item to add and remove items to the module API
Waqas Hussain <waqas20@gmail.com>
parents: 1586
diff changeset
   419
	self.items = self.items or {};
0b7888c0995d Added: functions add_item and remove_item to add and remove items to the module API
Waqas Hussain <waqas20@gmail.com>
parents: 1586
diff changeset
   420
	self.items[key] = self.items[key] or {};
0b7888c0995d Added: functions add_item and remove_item to add and remove items to the module API
Waqas Hussain <waqas20@gmail.com>
parents: 1586
diff changeset
   421
	t_insert(self.items[key], value);
0b7888c0995d Added: functions add_item and remove_item to add and remove items to the module API
Waqas Hussain <waqas20@gmail.com>
parents: 1586
diff changeset
   422
	self:fire_event("item-added/"..key, {source = self, item = value});
0b7888c0995d Added: functions add_item and remove_item to add and remove items to the module API
Waqas Hussain <waqas20@gmail.com>
parents: 1586
diff changeset
   423
end
0b7888c0995d Added: functions add_item and remove_item to add and remove items to the module API
Waqas Hussain <waqas20@gmail.com>
parents: 1586
diff changeset
   424
function api:remove_item(key, value)
0b7888c0995d Added: functions add_item and remove_item to add and remove items to the module API
Waqas Hussain <waqas20@gmail.com>
parents: 1586
diff changeset
   425
	local t = self.items and self.items[key] or NULL;
0b7888c0995d Added: functions add_item and remove_item to add and remove items to the module API
Waqas Hussain <waqas20@gmail.com>
parents: 1586
diff changeset
   426
	for i = #t,1,-1 do
0b7888c0995d Added: functions add_item and remove_item to add and remove items to the module API
Waqas Hussain <waqas20@gmail.com>
parents: 1586
diff changeset
   427
		if t[i] == value then
0b7888c0995d Added: functions add_item and remove_item to add and remove items to the module API
Waqas Hussain <waqas20@gmail.com>
parents: 1586
diff changeset
   428
			t_remove(self.items[key], i);
0b7888c0995d Added: functions add_item and remove_item to add and remove items to the module API
Waqas Hussain <waqas20@gmail.com>
parents: 1586
diff changeset
   429
			self:fire_event("item-removed/"..key, {source = self, item = value});
0b7888c0995d Added: functions add_item and remove_item to add and remove items to the module API
Waqas Hussain <waqas20@gmail.com>
parents: 1586
diff changeset
   430
			return value;
0b7888c0995d Added: functions add_item and remove_item to add and remove items to the module API
Waqas Hussain <waqas20@gmail.com>
parents: 1586
diff changeset
   431
		end
0b7888c0995d Added: functions add_item and remove_item to add and remove items to the module API
Waqas Hussain <waqas20@gmail.com>
parents: 1586
diff changeset
   432
	end
0b7888c0995d Added: functions add_item and remove_item to add and remove items to the module API
Waqas Hussain <waqas20@gmail.com>
parents: 1586
diff changeset
   433
end
0b7888c0995d Added: functions add_item and remove_item to add and remove items to the module API
Waqas Hussain <waqas20@gmail.com>
parents: 1586
diff changeset
   434
1698
af89f646200f modulemanager: Added module API function to get all items for a given host based on a key
Waqas Hussain <waqas20@gmail.com>
parents: 1697
diff changeset
   435
function api:get_host_items(key)
af89f646200f modulemanager: Added module API function to get all items for a given host based on a key
Waqas Hussain <waqas20@gmail.com>
parents: 1697
diff changeset
   436
	local result = {};
af89f646200f modulemanager: Added module API function to get all items for a given host based on a key
Waqas Hussain <waqas20@gmail.com>
parents: 1697
diff changeset
   437
	for mod_name, module in pairs(modulemap[self.host]) do
af89f646200f modulemanager: Added module API function to get all items for a given host based on a key
Waqas Hussain <waqas20@gmail.com>
parents: 1697
diff changeset
   438
		module = module.module;
af89f646200f modulemanager: Added module API function to get all items for a given host based on a key
Waqas Hussain <waqas20@gmail.com>
parents: 1697
diff changeset
   439
		if module.items then
af89f646200f modulemanager: Added module API function to get all items for a given host based on a key
Waqas Hussain <waqas20@gmail.com>
parents: 1697
diff changeset
   440
			for _, item in ipairs(module.items[key] or NULL) do
af89f646200f modulemanager: Added module API function to get all items for a given host based on a key
Waqas Hussain <waqas20@gmail.com>
parents: 1697
diff changeset
   441
				t_insert(result, item);
af89f646200f modulemanager: Added module API function to get all items for a given host based on a key
Waqas Hussain <waqas20@gmail.com>
parents: 1697
diff changeset
   442
			end
af89f646200f modulemanager: Added module API function to get all items for a given host based on a key
Waqas Hussain <waqas20@gmail.com>
parents: 1697
diff changeset
   443
		end
af89f646200f modulemanager: Added module API function to get all items for a given host based on a key
Waqas Hussain <waqas20@gmail.com>
parents: 1697
diff changeset
   444
	end
1946
0eb3835ef9bf modulemanager: Changed api:get_host_items to include items from the global host in its result.
Waqas Hussain <waqas20@gmail.com>
parents: 1933
diff changeset
   445
	for mod_name, module in pairs(modulemap["*"]) do
0eb3835ef9bf modulemanager: Changed api:get_host_items to include items from the global host in its result.
Waqas Hussain <waqas20@gmail.com>
parents: 1933
diff changeset
   446
		module = module.module;
0eb3835ef9bf modulemanager: Changed api:get_host_items to include items from the global host in its result.
Waqas Hussain <waqas20@gmail.com>
parents: 1933
diff changeset
   447
		if module.items then
0eb3835ef9bf modulemanager: Changed api:get_host_items to include items from the global host in its result.
Waqas Hussain <waqas20@gmail.com>
parents: 1933
diff changeset
   448
			for _, item in ipairs(module.items[key] or NULL) do
0eb3835ef9bf modulemanager: Changed api:get_host_items to include items from the global host in its result.
Waqas Hussain <waqas20@gmail.com>
parents: 1933
diff changeset
   449
				t_insert(result, item);
0eb3835ef9bf modulemanager: Changed api:get_host_items to include items from the global host in its result.
Waqas Hussain <waqas20@gmail.com>
parents: 1933
diff changeset
   450
			end
0eb3835ef9bf modulemanager: Changed api:get_host_items to include items from the global host in its result.
Waqas Hussain <waqas20@gmail.com>
parents: 1933
diff changeset
   451
		end
0eb3835ef9bf modulemanager: Changed api:get_host_items to include items from the global host in its result.
Waqas Hussain <waqas20@gmail.com>
parents: 1933
diff changeset
   452
	end
1698
af89f646200f modulemanager: Added module API function to get all items for a given host based on a key
Waqas Hussain <waqas20@gmail.com>
parents: 1697
diff changeset
   453
	return result;
af89f646200f modulemanager: Added module API function to get all items for a given host based on a key
Waqas Hussain <waqas20@gmail.com>
parents: 1697
diff changeset
   454
end
af89f646200f modulemanager: Added module API function to get all items for a given host based on a key
Waqas Hussain <waqas20@gmail.com>
parents: 1697
diff changeset
   455
4363
35ebd8e5747a modulemanager: Add module:handle_items() to allow a module to more easily handle a list of items on a host
Matthew Wild <mwild1@gmail.com>
parents: 4344
diff changeset
   456
function api:handle_items(type, added_cb, removed_cb, existing)
35ebd8e5747a modulemanager: Add module:handle_items() to allow a module to more easily handle a list of items on a host
Matthew Wild <mwild1@gmail.com>
parents: 4344
diff changeset
   457
	self:hook("item-added/"..type, added_cb);
35ebd8e5747a modulemanager: Add module:handle_items() to allow a module to more easily handle a list of items on a host
Matthew Wild <mwild1@gmail.com>
parents: 4344
diff changeset
   458
	self:hook("item-removed/"..type, removed_cb);
35ebd8e5747a modulemanager: Add module:handle_items() to allow a module to more easily handle a list of items on a host
Matthew Wild <mwild1@gmail.com>
parents: 4344
diff changeset
   459
	if existing ~= false then
35ebd8e5747a modulemanager: Add module:handle_items() to allow a module to more easily handle a list of items on a host
Matthew Wild <mwild1@gmail.com>
parents: 4344
diff changeset
   460
		for _, item in ipairs(self:get_host_items(type)) do
35ebd8e5747a modulemanager: Add module:handle_items() to allow a module to more easily handle a list of items on a host
Matthew Wild <mwild1@gmail.com>
parents: 4344
diff changeset
   461
			added_cb({ item = item });
35ebd8e5747a modulemanager: Add module:handle_items() to allow a module to more easily handle a list of items on a host
Matthew Wild <mwild1@gmail.com>
parents: 4344
diff changeset
   462
		end
35ebd8e5747a modulemanager: Add module:handle_items() to allow a module to more easily handle a list of items on a host
Matthew Wild <mwild1@gmail.com>
parents: 4344
diff changeset
   463
	end
35ebd8e5747a modulemanager: Add module:handle_items() to allow a module to more easily handle a list of items on a host
Matthew Wild <mwild1@gmail.com>
parents: 4344
diff changeset
   464
end
35ebd8e5747a modulemanager: Add module:handle_items() to allow a module to more easily handle a list of items on a host
Matthew Wild <mwild1@gmail.com>
parents: 4344
diff changeset
   465
39
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
   466
return _M;