core/modulemanager.lua
author Matthew Wild <mwild1@gmail.com>
Wed, 02 Dec 2009 20:32:44 +0000
changeset 2278 8c10f13c0c20
parent 2270 97f25da177af
child 2349 e0d9f4000d6b
permissions -rw-r--r--
modulemanager, net.dns: Remove trailing whitespace
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
760
90ce865eebd8 Update copyright notices for 2009
Matthew Wild <mwild1@gmail.com>
parents: 759
diff changeset
     2
-- Copyright (C) 2008-2009 Matthew Wild
90ce865eebd8 Update copyright notices for 2009
Matthew Wild <mwild1@gmail.com>
parents: 759
diff changeset
     3
-- Copyright (C) 2008-2009 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
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
     9
local plugin_dir = CFG_PLUGINDIR or "./plugins/";
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
    10
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
    11
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
    12
local log = logger.init("modulemanager");
569
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 540
diff changeset
    13
local eventmanager = require "core.eventmanager";
573
f6555ebf84ec Move module loading to modulemanager
Matthew Wild <mwild1@gmail.com>
parents: 569
diff changeset
    14
local config = require "core.configmanager";
578
5879264e28e2 Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents: 573
diff changeset
    15
local multitable_new = require "util.multitable".new;
698
d8a678e40a0a Add core.actions for managing server 'actions'; and make modulemanager register actions 'load' and 'unload'
Matthew Wild <mwild1@gmail.com>
parents: 695
diff changeset
    16
local register_actions = require "core.actions".register;
1174
f7b6d5839092 modulemanager: require util.stanza
Waqas Hussain <waqas20@gmail.com>
parents: 1173
diff changeset
    17
local st = require "util.stanza";
1360
857034905016 modulemanager: Changed to use util.pluginloader
Waqas Hussain <waqas20@gmail.com>
parents: 1346
diff changeset
    18
local pluginloader = require "util.pluginloader";
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    19
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
    20
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
    21
local prosody = prosody;
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
    22
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    23
local loadfile, pcall = loadfile, pcall;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    24
local setmetatable, setfenv, getfenv = setmetatable, setfenv, getfenv;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    25
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
    26
local t_insert, t_concat = table.insert, table.concat;
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    27
local type = type;
670
d5cf10b7fc44 Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents: 637
diff changeset
    28
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
    29
local rawget = rawget;
1392
9935ddfd8ccf modulemanager: Added simple module:require implementation
Waqas Hussain <waqas20@gmail.com>
parents: 1361
diff changeset
    30
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
    31
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
    32
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
    33
local array, set = require "util.array", require "util.set";
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    34
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
    35
local autoload_modules = {"presence", "message", "iq"};
e19cb945c25b modulemanager: Small code improvement, move autoloaded modules list to the top of the file
Matthew Wild <mwild1@gmail.com>
parents: 1504
diff changeset
    36
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
    37
-- We need this to let modules access the real global namespace
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    38
local _G = _G;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    39
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    40
module "modulemanager"
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    41
1389
846df07536eb modulemanager: Expose api table to allow others to extend the module API
Matthew Wild <mwild1@gmail.com>
parents: 1386
diff changeset
    42
api = {};
846df07536eb modulemanager: Expose api table to allow others to extend the module API
Matthew Wild <mwild1@gmail.com>
parents: 1386
diff changeset
    43
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
    44
584
eb0cea29c8d7 Temporary hack for global modules
Matthew Wild <mwild1@gmail.com>
parents: 579
diff changeset
    45
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
    46
591
980ded4c60ef Removed unused variables
Waqas Hussain <waqas20@gmail.com>
parents: 590
diff changeset
    47
local stanza_handlers = multitable_new();
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    48
local handler_info = {};
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
    49
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    50
local modulehelpers = setmetatable({}, { __index = _G });
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    51
670
d5cf10b7fc44 Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents: 637
diff changeset
    52
local handler_table = multitable_new();
686
13ed38531f69 modulemanager: Per-host event hooks for plugins - solves issue 41
Waqas Hussain <waqas20@gmail.com>
parents: 675
diff changeset
    53
local hooked = multitable_new();
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
    54
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
    55
local event_hooks = multitable_new();
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)
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
    61
	local disabled_set = {};
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
    62
	local modules_disabled = config.get(host, "core", "modules_disabled");
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
    63
	if modules_disabled then
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
    64
		for _, module in ipairs(modules_disabled) do
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
    65
			disabled_set[module] = true;
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
    66
		end
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
    67
	end
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
    68
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
    69
	-- Load auto-loaded modules for this host
1988
a6e7fe0fc3dd modulemanager: Fixed: Stanza modules were being auto-loaded for components (regression in hg:1e674dae31ae).
Waqas Hussain <waqas20@gmail.com>
parents: 1987
diff changeset
    70
	if hosts[host].type == "local" then
a6e7fe0fc3dd modulemanager: Fixed: Stanza modules were being auto-loaded for components (regression in hg:1e674dae31ae).
Waqas Hussain <waqas20@gmail.com>
parents: 1987
diff changeset
    71
		for _, module in ipairs(autoload_modules) do
a6e7fe0fc3dd modulemanager: Fixed: Stanza modules were being auto-loaded for components (regression in hg:1e674dae31ae).
Waqas Hussain <waqas20@gmail.com>
parents: 1987
diff changeset
    72
			if not disabled_set[module] then
a6e7fe0fc3dd modulemanager: Fixed: Stanza modules were being auto-loaded for components (regression in hg:1e674dae31ae).
Waqas Hussain <waqas20@gmail.com>
parents: 1987
diff changeset
    73
				load(host, module);
a6e7fe0fc3dd modulemanager: Fixed: Stanza modules were being auto-loaded for components (regression in hg:1e674dae31ae).
Waqas Hussain <waqas20@gmail.com>
parents: 1987
diff changeset
    74
			end
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
    75
		end
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
    76
	end
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
    77
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
    78
	-- Load modules from global section
1504
9d8c35e98ca2 configmanager, modulemanager: Allow components to have modules specified in the config (but don't load the global set of modules for them)
Matthew Wild <mwild1@gmail.com>
parents: 1447
diff changeset
    79
	if config.get(host, "core", "load_global_modules") ~= false then
9d8c35e98ca2 configmanager, modulemanager: Allow components to have modules specified in the config (but don't load the global set of modules for them)
Matthew Wild <mwild1@gmail.com>
parents: 1447
diff changeset
    80
		local modules_enabled = config.get("*", "core", "modules_enabled");
9d8c35e98ca2 configmanager, modulemanager: Allow components to have modules specified in the config (but don't load the global set of modules for them)
Matthew Wild <mwild1@gmail.com>
parents: 1447
diff changeset
    81
		if modules_enabled then
9d8c35e98ca2 configmanager, modulemanager: Allow components to have modules specified in the config (but don't load the global set of modules for them)
Matthew Wild <mwild1@gmail.com>
parents: 1447
diff changeset
    82
			for _, module in ipairs(modules_enabled) do
9d8c35e98ca2 configmanager, modulemanager: Allow components to have modules specified in the config (but don't load the global set of modules for them)
Matthew Wild <mwild1@gmail.com>
parents: 1447
diff changeset
    83
				if not disabled_set[module] and not is_loaded(host, module) then
9d8c35e98ca2 configmanager, modulemanager: Allow components to have modules specified in the config (but don't load the global set of modules for them)
Matthew Wild <mwild1@gmail.com>
parents: 1447
diff changeset
    84
					load(host, module);
9d8c35e98ca2 configmanager, modulemanager: Allow components to have modules specified in the config (but don't load the global set of modules for them)
Matthew Wild <mwild1@gmail.com>
parents: 1447
diff changeset
    85
				end
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
30b8ad9f7b70 Fix for not loading global modules when host-specific modules are specified in config
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
    87
		end
30b8ad9f7b70 Fix for not loading global modules when host-specific modules are specified in config
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
    88
	end
1504
9d8c35e98ca2 configmanager, modulemanager: Allow components to have modules specified in the config (but don't load the global set of modules for them)
Matthew Wild <mwild1@gmail.com>
parents: 1447
diff changeset
    89
	
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
    90
	-- Load modules from just this host
573
f6555ebf84ec Move module loading to modulemanager
Matthew Wild <mwild1@gmail.com>
parents: 569
diff changeset
    91
	local modules_enabled = config.get(host, "core", "modules_enabled");
1504
9d8c35e98ca2 configmanager, modulemanager: Allow components to have modules specified in the config (but don't load the global set of modules for them)
Matthew Wild <mwild1@gmail.com>
parents: 1447
diff changeset
    92
	if modules_enabled and modules_enabled ~= config.get("*", "core", "modules_enabled") then
573
f6555ebf84ec Move module loading to modulemanager
Matthew Wild <mwild1@gmail.com>
parents: 569
diff changeset
    93
		for _, module in pairs(modules_enabled) do
672
7db1044d2fab Remove warning of already-loaded modules at startup
Matthew Wild <mwild1@gmail.com>
parents: 670
diff changeset
    94
			if not is_loaded(host, module) then
7db1044d2fab Remove warning of already-loaded modules at startup
Matthew Wild <mwild1@gmail.com>
parents: 670
diff changeset
    95
				load(host, module);
7db1044d2fab Remove warning of already-loaded modules at startup
Matthew Wild <mwild1@gmail.com>
parents: 670
diff changeset
    96
			end
573
f6555ebf84ec Move module loading to modulemanager
Matthew Wild <mwild1@gmail.com>
parents: 569
diff changeset
    97
		end
f6555ebf84ec Move module loading to modulemanager
Matthew Wild <mwild1@gmail.com>
parents: 569
diff changeset
    98
	end
f6555ebf84ec Move module loading to modulemanager
Matthew Wild <mwild1@gmail.com>
parents: 569
diff changeset
    99
end
f6555ebf84ec Move module loading to modulemanager
Matthew Wild <mwild1@gmail.com>
parents: 569
diff changeset
   100
eventmanager.add_event_hook("host-activated", load_modules_for_host);
1852
f9b58f37bc14 modulemanager: Load modules for components, too
Matthew Wild <mwild1@gmail.com>
parents: 1730
diff changeset
   101
eventmanager.add_event_hook("component-activated", load_modules_for_host);
573
f6555ebf84ec Move module loading to modulemanager
Matthew Wild <mwild1@gmail.com>
parents: 569
diff changeset
   102
--
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   103
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
   104
function load(host, module_name, config)
439
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
   105
	if not (host and module_name) then
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
   106
		return nil, "insufficient-parameters";
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
   107
	end
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   108
	
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
   109
	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
   110
		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
   111
	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
   112
	
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
   113
	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
   114
		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
   115
		return nil, "module-already-loaded";
584
eb0cea29c8d7 Temporary hack for global modules
Matthew Wild <mwild1@gmail.com>
parents: 579
diff changeset
   116
	elseif modulemap["*"][module_name] then
eb0cea29c8d7 Temporary hack for global modules
Matthew Wild <mwild1@gmail.com>
parents: 579
diff changeset
   117
		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
   118
	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
   119
	
584
eb0cea29c8d7 Temporary hack for global modules
Matthew Wild <mwild1@gmail.com>
parents: 579
diff changeset
   120
1360
857034905016 modulemanager: Changed to use util.pluginloader
Waqas Hussain <waqas20@gmail.com>
parents: 1346
diff changeset
   121
	local mod, err = pluginloader.load_code(module_name);
584
eb0cea29c8d7 Temporary hack for global modules
Matthew Wild <mwild1@gmail.com>
parents: 579
diff changeset
   122
	if not mod then
eb0cea29c8d7 Temporary hack for global modules
Matthew Wild <mwild1@gmail.com>
parents: 579
diff changeset
   123
		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
   124
		return nil, err;
eb0cea29c8d7 Temporary hack for global modules
Matthew Wild <mwild1@gmail.com>
parents: 579
diff changeset
   125
	end
eb0cea29c8d7 Temporary hack for global modules
Matthew Wild <mwild1@gmail.com>
parents: 579
diff changeset
   126
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
   127
	local _log = logger.init(host..":"..module_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
   128
	local api_instance = setmetatable({ name = module_name, host = host, config = config,  _log = _log, log = function (self, ...) return _log(...); end }, { __index = api });
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
   129
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
   130
	local pluginenv = setmetatable({ module = api_instance }, { __index = _G });
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   131
	
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   132
	setfenv(mod, pluginenv);
1993
09621f8d0366 modulemanager: Use componentmanager to create new components.
Waqas Hussain <waqas20@gmail.com>
parents: 1988
diff changeset
   133
	if not hosts[host] then
09621f8d0366 modulemanager: Use componentmanager to create new components.
Waqas Hussain <waqas20@gmail.com>
parents: 1988
diff changeset
   134
		local create_component = _G.require "core.componentmanager".create_component;
09621f8d0366 modulemanager: Use componentmanager to create new components.
Waqas Hussain <waqas20@gmail.com>
parents: 1988
diff changeset
   135
		hosts[host] = create_component(host);
09621f8d0366 modulemanager: Use componentmanager to create new components.
Waqas Hussain <waqas20@gmail.com>
parents: 1988
diff changeset
   136
		hosts[host].connected = false;
09621f8d0366 modulemanager: Use componentmanager to create new components.
Waqas Hussain <waqas20@gmail.com>
parents: 1988
diff changeset
   137
		log("debug", "Created new component: %s", host);
09621f8d0366 modulemanager: Use componentmanager to create new components.
Waqas Hussain <waqas20@gmail.com>
parents: 1988
diff changeset
   138
	end
1987
94ecd3e7be87 modulemanager: Fixed traceback on loading modules on unknown hosts.
Waqas Hussain <waqas20@gmail.com>
parents: 1986
diff changeset
   139
	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
   140
	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
   141
	
1994
9cc9b096c8f5 modulemanager: Module loading rewritten to gracefully deal with errors in module initialization.
Waqas Hussain <waqas20@gmail.com>
parents: 1993
diff changeset
   142
	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
   143
	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
   144
		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
   145
			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
   146
			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
   147
				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
   148
			end
1378
d09ecc8ee1ef modulemanager: Call 'load' method when loading a module
Matthew Wild <mwild1@gmail.com>
parents: 1361
diff changeset
   149
		end
d09ecc8ee1ef modulemanager: Call 'load' method when loading a module
Matthew Wild <mwild1@gmail.com>
parents: 1361
diff changeset
   150
1994
9cc9b096c8f5 modulemanager: Module loading rewritten to gracefully deal with errors in module initialization.
Waqas Hussain <waqas20@gmail.com>
parents: 1993
diff changeset
   151
		-- 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
   152
		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
   153
			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
   154
			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
   155
			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
   156
		end
9cc9b096c8f5 modulemanager: Module loading rewritten to gracefully deal with errors in module initialization.
Waqas Hussain <waqas20@gmail.com>
parents: 1993
diff changeset
   157
	else
9cc9b096c8f5 modulemanager: Module loading rewritten to gracefully deal with errors in module initialization.
Waqas Hussain <waqas20@gmail.com>
parents: 1993
diff changeset
   158
		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
   159
	end
1994
9cc9b096c8f5 modulemanager: Module loading rewritten to gracefully deal with errors in module initialization.
Waqas Hussain <waqas20@gmail.com>
parents: 1993
diff changeset
   160
	if success then
2270
97f25da177af modulemanager: Fire events module-loaded and module-unloaded, both receive a table with 'module' and 'host' fields, thanks bjc
Matthew Wild <mwild1@gmail.com>
parents: 2151
diff changeset
   161
		hosts[host].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
   162
		return true;
9cc9b096c8f5 modulemanager: Module loading rewritten to gracefully deal with errors in module initialization.
Waqas Hussain <waqas20@gmail.com>
parents: 1993
diff changeset
   163
	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
   164
		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
   165
		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
   166
	end
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   167
end
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   168
745
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   169
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
   170
	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
   171
end
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   172
439
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
   173
function is_loaded(host, name)
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
   174
	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
   175
end
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
   176
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
   177
function unload(host, name, ...)
2278
8c10f13c0c20 modulemanager, net.dns: Remove trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2270
diff changeset
   178
	local mod = get_module(host, name);
439
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
   179
	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
   180
	
745
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   181
	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
   182
		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
   183
		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
   184
			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
   185
		end
674
4f506c627b49 modulemanager: module.unload now gets called when modules are being unloaded
Waqas Hussain <waqas20@gmail.com>
parents: 670
diff changeset
   186
	end
670
d5cf10b7fc44 Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents: 637
diff changeset
   187
	local params = handler_table:get(host, name); -- , {module.host, origin_type, tag, xmlns}
708
b72d408f5f15 modulemanager: Fixed error on unloading modules with no handlers
Waqas Hussain <waqas20@gmail.com>
parents: 686
diff changeset
   188
	for _, param in pairs(params or NULL) do
670
d5cf10b7fc44 Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents: 637
diff changeset
   189
		local handlers = stanza_handlers:get(param[1], param[2], param[3], param[4]);
708
b72d408f5f15 modulemanager: Fixed error on unloading modules with no handlers
Waqas Hussain <waqas20@gmail.com>
parents: 686
diff changeset
   190
		if handlers then
b72d408f5f15 modulemanager: Fixed error on unloading modules with no handlers
Waqas Hussain <waqas20@gmail.com>
parents: 686
diff changeset
   191
			handler_info[handlers[1]] = nil;
b72d408f5f15 modulemanager: Fixed error on unloading modules with no handlers
Waqas Hussain <waqas20@gmail.com>
parents: 686
diff changeset
   192
			stanza_handlers:remove(param[1], param[2], param[3], param[4]);
b72d408f5f15 modulemanager: Fixed error on unloading modules with no handlers
Waqas Hussain <waqas20@gmail.com>
parents: 686
diff changeset
   193
		end
439
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
   194
	end
686
13ed38531f69 modulemanager: Per-host event hooks for plugins - solves issue 41
Waqas Hussain <waqas20@gmail.com>
parents: 675
diff changeset
   195
	event_hooks:remove(host, name);
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
   196
	-- 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
   197
	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
   198
		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
   199
			(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
   200
		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
   201
	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
   202
	hooks:remove(host, name);
1986
d4ba9d94eb74 modulemanager: Slightly rearranged code for more robust unloading of modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1960
diff changeset
   203
	modulemap[host][name] = nil;
2270
97f25da177af modulemanager: Fire events module-loaded and module-unloaded, both receive a table with 'module' and 'host' fields, thanks bjc
Matthew Wild <mwild1@gmail.com>
parents: 2151
diff changeset
   204
	hosts[host].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
   205
	return true;
439
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
   206
end
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
   207
710
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   208
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
   209
	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
   210
	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
   211
1360
857034905016 modulemanager: Changed to use util.pluginloader
Waqas Hussain <waqas20@gmail.com>
parents: 1346
diff changeset
   212
	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
   213
	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
   214
		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
   215
		return nil, err;
2afd6d9e21cd modulemanager: Check for syntax errors before reloading a module
Waqas Hussain <waqas20@gmail.com>
parents: 710
diff changeset
   216
	end
2afd6d9e21cd modulemanager: Check for syntax errors before reloading a module
Waqas Hussain <waqas20@gmail.com>
parents: 710
diff changeset
   217
710
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   218
	local saved;
745
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   219
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   220
	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
   221
		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
   222
		if ok then
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   223
			saved = ret;
710
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   224
		else
745
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   225
			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
   226
			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
   227
				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
   228
				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
   229
			else
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   230
				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
   231
			end
710
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   232
		end
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   233
	end
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   234
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   235
	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
   236
	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
   237
	if ok then
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   238
		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
   239
		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
   240
			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
   241
			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
   242
				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
   243
			end
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   244
		end
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   245
		return true;
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   246
	end
745
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   247
	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
   248
end
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   249
578
5879264e28e2 Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents: 573
diff changeset
   250
function handle_stanza(host, origin, stanza)
1730
f4170bc82969 modulemanager: Fixed: Internally generated stanzas could be missing namespaces, which causes tracebacks in logging (e.g., auto-generated offline presence)
Waqas Hussain <waqas20@gmail.com>
parents: 1523
diff changeset
   251
	local name, xmlns, origin_type = stanza.name, stanza.attr.xmlns or "jabber:client", origin.type;
578
5879264e28e2 Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents: 573
diff changeset
   252
	if name == "iq" and xmlns == "jabber:client" then
608
3758af511ce8 Don't try processing stanzas not of type get or set in module manager
Waqas Hussain <waqas20@gmail.com>
parents: 592
diff changeset
   253
		if stanza.attr.type == "get" or stanza.attr.type == "set" then
929
b4f2ca7f6f00 Fixed: modulemanager: IQs with extended elements in the default namespace could cause backtraces (related to issue #74)
Waqas Hussain <waqas20@gmail.com>
parents: 896
diff changeset
   254
			xmlns = stanza.tags[1].attr.xmlns or "jabber:client";
608
3758af511ce8 Don't try processing stanzas not of type get or set in module manager
Waqas Hussain <waqas20@gmail.com>
parents: 592
diff changeset
   255
			log("debug", "Stanza of type %s from %s has xmlns: %s", name, origin_type, xmlns);
3758af511ce8 Don't try processing stanzas not of type get or set in module manager
Waqas Hussain <waqas20@gmail.com>
parents: 592
diff changeset
   256
		else
3758af511ce8 Don't try processing stanzas not of type get or set in module manager
Waqas Hussain <waqas20@gmail.com>
parents: 592
diff changeset
   257
			log("debug", "Discarding %s from %s of type: %s", name, origin_type, stanza.attr.type);
3758af511ce8 Don't try processing stanzas not of type get or set in module manager
Waqas Hussain <waqas20@gmail.com>
parents: 592
diff changeset
   258
			return true;
3758af511ce8 Don't try processing stanzas not of type get or set in module manager
Waqas Hussain <waqas20@gmail.com>
parents: 592
diff changeset
   259
		end
578
5879264e28e2 Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents: 573
diff changeset
   260
	end
591
980ded4c60ef Removed unused variables
Waqas Hussain <waqas20@gmail.com>
parents: 590
diff changeset
   261
	local handlers = stanza_handlers:get(host, origin_type, name, xmlns);
876
fad020cead52 Allow global modules to hook stanza handlers
Waqas Hussain <waqas20@gmail.com>
parents: 778
diff changeset
   262
	if not handlers then handlers = stanza_handlers:get("*", origin_type, name, xmlns); end
578
5879264e28e2 Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents: 573
diff changeset
   263
	if handlers then
5879264e28e2 Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents: 573
diff changeset
   264
		log("debug", "Passing stanza to mod_%s", handler_info[handlers[1]].name);
5879264e28e2 Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents: 573
diff changeset
   265
		(handlers[1])(origin, stanza);
5879264e28e2 Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents: 573
diff changeset
   266
		return true;
5879264e28e2 Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents: 573
diff changeset
   267
	else
1167
5620ea24be94 stanza_router: Removed global function core_handle_stanza
Waqas Hussain <waqas20@gmail.com>
parents: 1105
diff changeset
   268
		if stanza.attr.xmlns == "jabber:client" then
1933
3884a0aac4d7 modulemanager: Bump log level of unhandled non-default-namespace elements, and log their full XML
Matthew Wild <mwild1@gmail.com>
parents: 1908
diff changeset
   269
			log("debug", "Unhandled %s stanza: %s; xmlns=%s", origin.type, stanza.name, xmlns); -- we didn't handle it
1167
5620ea24be94 stanza_router: Removed global function core_handle_stanza
Waqas Hussain <waqas20@gmail.com>
parents: 1105
diff changeset
   270
			if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
5620ea24be94 stanza_router: Removed global function core_handle_stanza
Waqas Hussain <waqas20@gmail.com>
parents: 1105
diff changeset
   271
				origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
5620ea24be94 stanza_router: Removed global function core_handle_stanza
Waqas Hussain <waqas20@gmail.com>
parents: 1105
diff changeset
   272
			end
1447
cc20d6dfa32d modulemanager: Don't close stream on stream:error with unsupported-stanza-type (fixes #102)
Waqas Hussain <waqas20@gmail.com>
parents: 1394
diff changeset
   273
		elseif not((name == "features" or name == "error") and xmlns == "http://etherx.jabber.org/streams") then -- FIXME remove check once we handle S2S features
1933
3884a0aac4d7 modulemanager: Bump log level of unhandled non-default-namespace elements, and log their full XML
Matthew Wild <mwild1@gmail.com>
parents: 1908
diff changeset
   274
			log("warn", "Unhandled %s stream element: %s; xmlns=%s: %s", origin.type, stanza.name, xmlns, tostring(stanza)); -- we didn't handle it
1167
5620ea24be94 stanza_router: Removed global function core_handle_stanza
Waqas Hussain <waqas20@gmail.com>
parents: 1105
diff changeset
   275
			origin:close("unsupported-stanza-type");
5620ea24be94 stanza_router: Removed global function core_handle_stanza
Waqas Hussain <waqas20@gmail.com>
parents: 1105
diff changeset
   276
		end
578
5879264e28e2 Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents: 573
diff changeset
   277
	end
5879264e28e2 Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents: 573
diff changeset
   278
end
39
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
   279
745
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   280
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
   281
	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
   282
end
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   283
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
   284
function call_module_method(module, method, ...)
2278
8c10f13c0c20 modulemanager, net.dns: Remove trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2270
diff changeset
   285
	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
   286
		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
   287
		return pcall(f, ...);
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   288
	else
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   289
		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
   290
	end
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   291
end
5a343599cd3e core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents: 733
diff changeset
   292
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
   293
----- API functions exposed to modules -----------
2278
8c10f13c0c20 modulemanager, net.dns: Remove trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2270
diff changeset
   294
-- 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
   295
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
   296
-- 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
   297
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
   298
	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
   299
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
   300
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
   301
-- 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
   302
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
   303
	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
   304
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
   305
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
   306
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
   307
	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
   308
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
   309
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
   310
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
   311
	self.host = "*";
1069
034e345c0f8d modulemanager: Update logger when a module becomes global
Matthew Wild <mwild1@gmail.com>
parents: 929
diff changeset
   312
	-- Update the logger
034e345c0f8d modulemanager: Update logger when a module becomes global
Matthew Wild <mwild1@gmail.com>
parents: 929
diff changeset
   313
	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
   314
	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
   315
	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
   316
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
   317
590
54afe37cccbf Combined and merged similar code
Waqas Hussain <waqas20@gmail.com>
parents: 589
diff changeset
   318
local function _add_handler(module, origin_type, tag, xmlns, handler)
591
980ded4c60ef Removed unused variables
Waqas Hussain <waqas20@gmail.com>
parents: 590
diff changeset
   319
	local handlers = stanza_handlers:get(module.host, origin_type, tag, xmlns);
590
54afe37cccbf Combined and merged similar code
Waqas Hussain <waqas20@gmail.com>
parents: 589
diff changeset
   320
	local msg = (tag == "iq") and "namespace" or "payload namespace";
578
5879264e28e2 Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents: 573
diff changeset
   321
	if not handlers then
591
980ded4c60ef Removed unused variables
Waqas Hussain <waqas20@gmail.com>
parents: 590
diff changeset
   322
		stanza_handlers:add(module.host, origin_type, tag, xmlns, handler);
578
5879264e28e2 Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents: 573
diff changeset
   323
		handler_info[handler] = module;
670
d5cf10b7fc44 Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents: 637
diff changeset
   324
		handler_table:add(module.host, module.name, {module.host, origin_type, tag, xmlns});
733
b1aedec00661 modulemanager: Comment out logging of modules hooking stanzas, too noisy
Matthew Wild <mwild1@gmail.com>
parents: 713
diff changeset
   325
		--module:log("debug", "I now handle tag '%s' [%s] with %s '%s'", tag, origin_type, msg, xmlns);
578
5879264e28e2 Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents: 573
diff changeset
   326
	else
590
54afe37cccbf Combined and merged similar code
Waqas Hussain <waqas20@gmail.com>
parents: 589
diff changeset
   327
		module:log("warn", "I wanted to handle tag '%s' [%s] with %s '%s' but mod_%s already handles that", tag, origin_type, msg, xmlns, handler_info[handlers[1]].module.name);
578
5879264e28e2 Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents: 573
diff changeset
   328
	end
5879264e28e2 Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents: 573
diff changeset
   329
end
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
   330
590
54afe37cccbf Combined and merged similar code
Waqas Hussain <waqas20@gmail.com>
parents: 589
diff changeset
   331
function api:add_handler(origin_type, tag, xmlns, handler)
54afe37cccbf Combined and merged similar code
Waqas Hussain <waqas20@gmail.com>
parents: 589
diff changeset
   332
	if not (origin_type and tag and xmlns and handler) then return false; end
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
   333
	if type(origin_type) == "table" 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
   334
		for _, origin_type in ipairs(origin_type) do
590
54afe37cccbf Combined and merged similar code
Waqas Hussain <waqas20@gmail.com>
parents: 589
diff changeset
   335
			_add_handler(self, origin_type, tag, xmlns, handler);
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
   336
		end
590
54afe37cccbf Combined and merged similar code
Waqas Hussain <waqas20@gmail.com>
parents: 589
diff changeset
   337
	else
54afe37cccbf Combined and merged similar code
Waqas Hussain <waqas20@gmail.com>
parents: 589
diff changeset
   338
		_add_handler(self, origin_type, tag, xmlns, handler);
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
   339
	end
590
54afe37cccbf Combined and merged similar code
Waqas Hussain <waqas20@gmail.com>
parents: 589
diff changeset
   340
end
54afe37cccbf Combined and merged similar code
Waqas Hussain <waqas20@gmail.com>
parents: 589
diff changeset
   341
function api:add_iq_handler(origin_type, xmlns, handler)
54afe37cccbf Combined and merged similar code
Waqas Hussain <waqas20@gmail.com>
parents: 589
diff changeset
   342
	self:add_handler(origin_type, "iq", xmlns, handler);
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
   343
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
   344
540
ec03f6968fa8 Added function add_feature to modules API (for adding disco features)
Waqas Hussain <waqas20@gmail.com>
parents: 519
diff changeset
   345
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
   346
	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
   347
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
   348
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
   349
	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
   350
end
540
ec03f6968fa8 Added function add_feature to modules API (for adding disco features)
Waqas Hussain <waqas20@gmail.com>
parents: 519
diff changeset
   351
686
13ed38531f69 modulemanager: Per-host event hooks for plugins - solves issue 41
Waqas Hussain <waqas20@gmail.com>
parents: 675
diff changeset
   352
local event_hook = function(host, mod_name, event_name, ...)
13ed38531f69 modulemanager: Per-host event hooks for plugins - solves issue 41
Waqas Hussain <waqas20@gmail.com>
parents: 675
diff changeset
   353
	if type((...)) == "table" and (...).host and (...).host ~= host then return; end
13ed38531f69 modulemanager: Per-host event hooks for plugins - solves issue 41
Waqas Hussain <waqas20@gmail.com>
parents: 675
diff changeset
   354
	for handler in pairs(event_hooks:get(host, mod_name, event_name) or NULL) do
13ed38531f69 modulemanager: Per-host event hooks for plugins - solves issue 41
Waqas Hussain <waqas20@gmail.com>
parents: 675
diff changeset
   355
		handler(...);
13ed38531f69 modulemanager: Per-host event hooks for plugins - solves issue 41
Waqas Hussain <waqas20@gmail.com>
parents: 675
diff changeset
   356
	end
13ed38531f69 modulemanager: Per-host event hooks for plugins - solves issue 41
Waqas Hussain <waqas20@gmail.com>
parents: 675
diff changeset
   357
end;
13ed38531f69 modulemanager: Per-host event hooks for plugins - solves issue 41
Waqas Hussain <waqas20@gmail.com>
parents: 675
diff changeset
   358
function api:add_event_hook(name, handler)
13ed38531f69 modulemanager: Per-host event hooks for plugins - solves issue 41
Waqas Hussain <waqas20@gmail.com>
parents: 675
diff changeset
   359
	if not hooked:get(self.host, self.name, name) then
13ed38531f69 modulemanager: Per-host event hooks for plugins - solves issue 41
Waqas Hussain <waqas20@gmail.com>
parents: 675
diff changeset
   360
		eventmanager.add_event_hook(name, function(...) event_hook(self.host, self.name, name, ...); end);
13ed38531f69 modulemanager: Per-host event hooks for plugins - solves issue 41
Waqas Hussain <waqas20@gmail.com>
parents: 675
diff changeset
   361
		hooked:set(self.host, self.name, name, true);
13ed38531f69 modulemanager: Per-host event hooks for plugins - solves issue 41
Waqas Hussain <waqas20@gmail.com>
parents: 675
diff changeset
   362
	end
13ed38531f69 modulemanager: Per-host event hooks for plugins - solves issue 41
Waqas Hussain <waqas20@gmail.com>
parents: 675
diff changeset
   363
	event_hooks:set(self.host, self.name, name, handler, true);
13ed38531f69 modulemanager: Per-host event hooks for plugins - solves issue 41
Waqas Hussain <waqas20@gmail.com>
parents: 675
diff changeset
   364
end
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
   365
1183
565e16ee0c74 modulemanager: Add module:fire_event() to module API
Matthew Wild <mwild1@gmail.com>
parents: 1174
diff changeset
   366
function api:fire_event(...)
1253
60156584c442 modulemanager: Stopped using core.eventmanager in module:fire_event
Waqas Hussain <waqas20@gmail.com>
parents: 1250
diff changeset
   367
	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
   368
end
565e16ee0c74 modulemanager: Add module:fire_event() to module API
Matthew Wild <mwild1@gmail.com>
parents: 1174
diff changeset
   369
1319
812c028a877d modulemanager: Allow setting priority of stanza handlers
Matthew Wild <mwild1@gmail.com>
parents: 1318
diff changeset
   370
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
   371
	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
   372
	(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
   373
end
6f251813f1e5 modulemanager: Added hook(event, handler) to the plugin API
Waqas Hussain <waqas20@gmail.com>
parents: 1183
diff changeset
   374
1319
812c028a877d modulemanager: Allow setting priority of stanza handlers
Matthew Wild <mwild1@gmail.com>
parents: 1318
diff changeset
   375
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
   376
	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
   377
		-- 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
   378
		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
   379
	elseif not (handler and name) then
1320
9c736b993e07 modulemanager: module:log() -> self:log() fix
Matthew Wild <mwild1@gmail.com>
parents: 1319
diff changeset
   380
		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
   381
		return;
66f76c332a75 modulemanager: Add module:hook([xmlns, ] name, handler) which uses new stanza events
Matthew Wild <mwild1@gmail.com>
parents: 1309
diff changeset
   382
	end
1319
812c028a877d modulemanager: Allow setting priority of stanza handlers
Matthew Wild <mwild1@gmail.com>
parents: 1318
diff changeset
   383
	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
   384
end
66f76c332a75 modulemanager: Add module:hook([xmlns, ] name, handler) which uses new stanza events
Matthew Wild <mwild1@gmail.com>
parents: 1309
diff changeset
   385
1392
9935ddfd8ccf modulemanager: Added simple module:require implementation
Waqas Hussain <waqas20@gmail.com>
parents: 1361
diff changeset
   386
function api:require(lib)
9935ddfd8ccf modulemanager: Added simple module:require implementation
Waqas Hussain <waqas20@gmail.com>
parents: 1361
diff changeset
   387
	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
   388
	if not f then
9935ddfd8ccf modulemanager: Added simple module:require implementation
Waqas Hussain <waqas20@gmail.com>
parents: 1361
diff changeset
   389
		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
   390
	end
9935ddfd8ccf modulemanager: Added simple module:require implementation
Waqas Hussain <waqas20@gmail.com>
parents: 1361
diff changeset
   391
	if not f then error("Failed to load plugin library '"..lib.."', error: "..n); end -- FIXME better error message
1393
576143941a76 modulemanager: Enabled access to the module API from plugin libraries
Waqas Hussain <waqas20@gmail.com>
parents: 1392
diff changeset
   392
	setfenv(f, setmetatable({ module = self }, { __index = _G }));
1392
9935ddfd8ccf modulemanager: Added simple module:require implementation
Waqas Hussain <waqas20@gmail.com>
parents: 1361
diff changeset
   393
	return f();
9935ddfd8ccf modulemanager: Added simple module:require implementation
Waqas Hussain <waqas20@gmail.com>
parents: 1361
diff changeset
   394
end
9935ddfd8ccf modulemanager: Added simple module:require implementation
Waqas Hussain <waqas20@gmail.com>
parents: 1361
diff changeset
   395
1586
5c627d5d5e37 modulemanager: Added get_option(name, default_value) to plugin API
Waqas Hussain <waqas20@gmail.com>
parents: 1523
diff changeset
   396
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
   397
	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
   398
	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
   399
		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
   400
		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
   401
			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
   402
		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
   403
	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
   404
	return value;
1586
5c627d5d5e37 modulemanager: Added get_option(name, default_value) to plugin API
Waqas Hussain <waqas20@gmail.com>
parents: 1523
diff changeset
   405
end
5c627d5d5e37 modulemanager: Added get_option(name, default_value) to plugin API
Waqas Hussain <waqas20@gmail.com>
parents: 1523
diff changeset
   406
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
   407
function api:get_option_string(...)
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
	local value = self:get_option(...);
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 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
   410
		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
   411
			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
   412
		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
   413
		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
   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
	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
   416
		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
   417
	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
   418
	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
   419
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
   420
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
   421
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
   422
	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
   423
	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
   424
		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
   425
			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
   426
		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
   427
		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
   428
	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
   429
	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
   430
	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
   431
		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
   432
	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
   433
	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
   434
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
   435
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
   436
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
   437
	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
   438
	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
   439
		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
   440
			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
   441
		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
   442
		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
   443
	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
   444
	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
   445
		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
   446
	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
   447
	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
   448
	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
   449
		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
   450
		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
   451
			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
   452
		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
   453
			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
   454
		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
   455
	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
   456
	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
   457
		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
   458
	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
   459
	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
   460
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
   461
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
   462
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
   463
	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
   464
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
   465
	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
   466
		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
   467
	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
   468
	
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
   469
	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
   470
		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
   471
	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
   472
	
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
   473
	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
   474
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
   475
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
   476
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
   477
	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
   478
	
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
   479
	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
   480
		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
   481
	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
   482
	
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
   483
	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
   484
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
   485
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
   486
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
   487
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
   488
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
   489
	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
   490
	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
   491
	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
   492
	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
   493
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
   494
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
   495
	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
   496
	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
   497
		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
   498
			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
   499
			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
   500
			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
   501
		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
   502
	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
   503
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
   504
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
   505
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
   506
	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
   507
	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
   508
		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
   509
		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
   510
			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
   511
				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
   512
			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
   513
		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
   514
	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
   515
	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
   516
		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
   517
		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
   518
			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
   519
				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
   520
			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
   521
		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
   522
	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
   523
	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
   524
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
   525
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
   526
--------------------------------------------------------------------
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
   527
698
d8a678e40a0a Add core.actions for managing server 'actions'; and make modulemanager register actions 'load' and 'unload'
Matthew Wild <mwild1@gmail.com>
parents: 695
diff changeset
   528
local actions = {};
d8a678e40a0a Add core.actions for managing server 'actions'; and make modulemanager register actions 'load' and 'unload'
Matthew Wild <mwild1@gmail.com>
parents: 695
diff changeset
   529
d8a678e40a0a Add core.actions for managing server 'actions'; and make modulemanager register actions 'load' and 'unload'
Matthew Wild <mwild1@gmail.com>
parents: 695
diff changeset
   530
function actions.load(params)
d8a678e40a0a Add core.actions for managing server 'actions'; and make modulemanager register actions 'load' and 'unload'
Matthew Wild <mwild1@gmail.com>
parents: 695
diff changeset
   531
	--return true, "Module loaded ("..params.module.." on "..params.host..")";
d8a678e40a0a Add core.actions for managing server 'actions'; and make modulemanager register actions 'load' and 'unload'
Matthew Wild <mwild1@gmail.com>
parents: 695
diff changeset
   532
	return load(params.host, params.module);
d8a678e40a0a Add core.actions for managing server 'actions'; and make modulemanager register actions 'load' and 'unload'
Matthew Wild <mwild1@gmail.com>
parents: 695
diff changeset
   533
end
d8a678e40a0a Add core.actions for managing server 'actions'; and make modulemanager register actions 'load' and 'unload'
Matthew Wild <mwild1@gmail.com>
parents: 695
diff changeset
   534
d8a678e40a0a Add core.actions for managing server 'actions'; and make modulemanager register actions 'load' and 'unload'
Matthew Wild <mwild1@gmail.com>
parents: 695
diff changeset
   535
function actions.unload(params)
d8a678e40a0a Add core.actions for managing server 'actions'; and make modulemanager register actions 'load' and 'unload'
Matthew Wild <mwild1@gmail.com>
parents: 695
diff changeset
   536
	return unload(params.host, params.module);
d8a678e40a0a Add core.actions for managing server 'actions'; and make modulemanager register actions 'load' and 'unload'
Matthew Wild <mwild1@gmail.com>
parents: 695
diff changeset
   537
end
d8a678e40a0a Add core.actions for managing server 'actions'; and make modulemanager register actions 'load' and 'unload'
Matthew Wild <mwild1@gmail.com>
parents: 695
diff changeset
   538
d8a678e40a0a Add core.actions for managing server 'actions'; and make modulemanager register actions 'load' and 'unload'
Matthew Wild <mwild1@gmail.com>
parents: 695
diff changeset
   539
register_actions("/modules", actions);
d8a678e40a0a Add core.actions for managing server 'actions'; and make modulemanager register actions 'load' and 'unload'
Matthew Wild <mwild1@gmail.com>
parents: 695
diff changeset
   540
39
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
   541
return _M;