core/modulemanager.lua
author Matthew Wild <mwild1@gmail.com>
Fri, 16 Jan 2009 05:38:03 +0000
changeset 733 b1aedec00661
parent 713 2afd6d9e21cd
child 745 5a343599cd3e
permissions -rw-r--r--
modulemanager: Comment out logging of modules hooking stanzas, too noisy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
615
4ae3e81513f3 0.1 -> 0.2
Matthew Wild <mwild1@gmail.com>
parents: 608
diff changeset
     1
-- Prosody IM v0.2
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 467
diff changeset
     2
-- Copyright (C) 2008 Matthew Wild
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 467
diff changeset
     3
-- Copyright (C) 2008 Waqas Hussain
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 467
diff changeset
     4
-- 
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 467
diff changeset
     5
-- This program is free software; you can redistribute it and/or
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 467
diff changeset
     6
-- modify it under the terms of the GNU General Public License
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 467
diff changeset
     7
-- as published by the Free Software Foundation; either version 2
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 467
diff changeset
     8
-- of the License, or (at your option) any later version.
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 467
diff changeset
     9
-- 
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 467
diff changeset
    10
-- This program is distributed in the hope that it will be useful,
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 467
diff changeset
    11
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 467
diff changeset
    12
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 467
diff changeset
    13
-- GNU General Public License for more details.
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 467
diff changeset
    14
-- 
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 467
diff changeset
    15
-- You should have received a copy of the GNU General Public License
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 467
diff changeset
    16
-- along with this program; if not, write to the Free Software
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 467
diff changeset
    17
-- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 467
diff changeset
    18
--
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 467
diff changeset
    19
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 467
diff changeset
    20
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    21
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
    22
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
    23
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
    24
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
    25
local log = logger.init("modulemanager");
ec03f6968fa8 Added function add_feature to modules API (for adding disco features)
Waqas Hussain <waqas20@gmail.com>
parents: 519
diff changeset
    26
local addDiscoInfoHandler = require "core.discomanager".addDiscoInfoHandler;
569
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 540
diff changeset
    27
local eventmanager = require "core.eventmanager";
573
f6555ebf84ec Move module loading to modulemanager
Matthew Wild <mwild1@gmail.com>
parents: 569
diff changeset
    28
local config = require "core.configmanager";
578
5879264e28e2 Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents: 573
diff changeset
    29
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
    30
local register_actions = require "core.actions".register;
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    31
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    32
local loadfile, pcall = loadfile, pcall;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    33
local setmetatable, setfenv, getfenv = setmetatable, setfenv, getfenv;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    34
local pairs, ipairs = pairs, ipairs;
39
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
    35
local t_insert = table.insert;
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    36
local type = type;
670
d5cf10b7fc44 Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents: 637
diff changeset
    37
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
    38
local rawget = rawget;
30
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
local tostring, print = tostring, print;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    41
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
    42
-- We need this to let modules access the real global namespace
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    43
local _G = _G;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    44
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    45
module "modulemanager"
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    46
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
    47
local api = {}; -- Module API container
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
    48
584
eb0cea29c8d7 Temporary hack for global modules
Matthew Wild <mwild1@gmail.com>
parents: 579
diff changeset
    49
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
    50
591
980ded4c60ef Removed unused variables
Waqas Hussain <waqas20@gmail.com>
parents: 590
diff changeset
    51
local stanza_handlers = multitable_new();
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    52
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
    53
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    54
local modulehelpers = setmetatable({}, { __index = _G });
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    55
670
d5cf10b7fc44 Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents: 637
diff changeset
    56
local features_table = multitable_new();
d5cf10b7fc44 Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents: 637
diff changeset
    57
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
    58
local hooked = multitable_new();
13ed38531f69 modulemanager: Per-host event hooks for plugins - solves issue 41
Waqas Hussain <waqas20@gmail.com>
parents: 675
diff changeset
    59
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
    60
670
d5cf10b7fc44 Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents: 637
diff changeset
    61
local NULL = {};
d5cf10b7fc44 Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents: 637
diff changeset
    62
573
f6555ebf84ec Move module loading to modulemanager
Matthew Wild <mwild1@gmail.com>
parents: 569
diff changeset
    63
-- Load modules when a host is activated
f6555ebf84ec Move module loading to modulemanager
Matthew Wild <mwild1@gmail.com>
parents: 569
diff changeset
    64
function load_modules_for_host(host)
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
    65
	-- Load modules from global section
30b8ad9f7b70 Fix for not loading global modules when host-specific modules are specified in config
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
    66
	local modules_enabled = config.get("*", "core", "modules_enabled");
30b8ad9f7b70 Fix for not loading global modules when host-specific modules are specified in config
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
    67
	local modules_disabled = config.get(host, "core", "modules_disabled");
30b8ad9f7b70 Fix for not loading global modules when host-specific modules are specified in config
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
    68
	local disabled_set = {};
30b8ad9f7b70 Fix for not loading global modules when host-specific modules are specified in config
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
    69
	if modules_enabled then
30b8ad9f7b70 Fix for not loading global modules when host-specific modules are specified in config
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
    70
		if modules_disabled then
695
3384f2784795 modulemanager: Change pairs() to ipairs() to allow ordered module loading
Matthew Wild <mwild1@gmail.com>
parents: 686
diff changeset
    71
			for _, module in ipairs(modules_disabled) do
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
    72
				disabled_set[module] = true;
30b8ad9f7b70 Fix for not loading global modules when host-specific modules are specified in config
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
    73
			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
    74
		end
695
3384f2784795 modulemanager: Change pairs() to ipairs() to allow ordered module loading
Matthew Wild <mwild1@gmail.com>
parents: 686
diff changeset
    75
		for _, module in ipairs(modules_enabled) do
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
    76
			if not disabled_set[module] then
30b8ad9f7b70 Fix for not loading global modules when host-specific modules are specified in config
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
    77
				load(host, module);
30b8ad9f7b70 Fix for not loading global modules when host-specific modules are specified in config
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
    78
			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
    79
		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
    80
	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
    81
30b8ad9f7b70 Fix for not loading global modules when host-specific modules are specified in config
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
    82
	-- Load modules from just this host
573
f6555ebf84ec Move module loading to modulemanager
Matthew Wild <mwild1@gmail.com>
parents: 569
diff changeset
    83
	local modules_enabled = config.get(host, "core", "modules_enabled");
f6555ebf84ec Move module loading to modulemanager
Matthew Wild <mwild1@gmail.com>
parents: 569
diff changeset
    84
	if modules_enabled then
f6555ebf84ec Move module loading to modulemanager
Matthew Wild <mwild1@gmail.com>
parents: 569
diff changeset
    85
		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
    86
			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
    87
				load(host, module);
7db1044d2fab Remove warning of already-loaded modules at startup
Matthew Wild <mwild1@gmail.com>
parents: 670
diff changeset
    88
			end
573
f6555ebf84ec Move module loading to modulemanager
Matthew Wild <mwild1@gmail.com>
parents: 569
diff changeset
    89
		end
f6555ebf84ec Move module loading to modulemanager
Matthew Wild <mwild1@gmail.com>
parents: 569
diff changeset
    90
	end
f6555ebf84ec Move module loading to modulemanager
Matthew Wild <mwild1@gmail.com>
parents: 569
diff changeset
    91
end
f6555ebf84ec Move module loading to modulemanager
Matthew Wild <mwild1@gmail.com>
parents: 569
diff changeset
    92
eventmanager.add_event_hook("host-activated", load_modules_for_host);
f6555ebf84ec Move module loading to modulemanager
Matthew Wild <mwild1@gmail.com>
parents: 569
diff changeset
    93
--
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    94
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
    95
function load(host, module_name, config)
439
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
    96
	if not (host and module_name) then
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
    97
		return nil, "insufficient-parameters";
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
    98
	end
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    99
	
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
   100
	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
   101
		modulemap[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
   102
	elseif modulemap[host][module_name] 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
   103
		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
   104
		return nil, "module-already-loaded";
584
eb0cea29c8d7 Temporary hack for global modules
Matthew Wild <mwild1@gmail.com>
parents: 579
diff changeset
   105
	elseif modulemap["*"][module_name] then
eb0cea29c8d7 Temporary hack for global modules
Matthew Wild <mwild1@gmail.com>
parents: 579
diff changeset
   106
		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
   107
	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
   108
	
584
eb0cea29c8d7 Temporary hack for global modules
Matthew Wild <mwild1@gmail.com>
parents: 579
diff changeset
   109
eb0cea29c8d7 Temporary hack for global modules
Matthew Wild <mwild1@gmail.com>
parents: 579
diff changeset
   110
	local mod, err = loadfile(plugin_dir.."mod_"..module_name..".lua");
eb0cea29c8d7 Temporary hack for global modules
Matthew Wild <mwild1@gmail.com>
parents: 579
diff changeset
   111
	if not mod then
eb0cea29c8d7 Temporary hack for global modules
Matthew Wild <mwild1@gmail.com>
parents: 579
diff changeset
   112
		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
   113
		return nil, err;
eb0cea29c8d7 Temporary hack for global modules
Matthew Wild <mwild1@gmail.com>
parents: 579
diff changeset
   114
	end
eb0cea29c8d7 Temporary hack for global modules
Matthew Wild <mwild1@gmail.com>
parents: 579
diff changeset
   115
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
   116
	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
   117
	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
   118
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
   119
	local pluginenv = setmetatable({ module = api_instance }, { __index = _G });
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   120
	
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   121
	setfenv(mod, 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
   122
	
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   123
	local success, ret = pcall(mod);
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   124
	if not success then
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   125
		log("error", "Error initialising module '%s': %s", name or "nil", ret or "nil");
391
79bd7a3e906c Typo prevented modulemanager.load() from returning the error if load failed.
Matthew Wild <mwild1@gmail.com>
parents: 385
diff changeset
   126
		return nil, ret;
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   127
	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
   128
	
584
eb0cea29c8d7 Temporary hack for global modules
Matthew Wild <mwild1@gmail.com>
parents: 579
diff changeset
   129
	-- Use modified host, if the module set one
670
d5cf10b7fc44 Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents: 637
diff changeset
   130
	modulemap[api_instance.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
   131
	
229
01bd24ea488d We now fail if modules fail to load at startup.
Waqas Hussain <waqas20@gmail.com>
parents: 218
diff changeset
   132
	return true;
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   133
end
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   134
439
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
   135
function is_loaded(host, name)
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
   136
	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
   137
end
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
   138
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
   139
function unload(host, name, ...)
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
   140
	local mod = modulemap[host] and modulemap[host][name];
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
   141
	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
   142
	
710
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   143
	if type(mod.module.unload) == "function" then
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   144
		local ok, err = pcall(mod.module.unload, ...)
439
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
   145
		if (not ok) and err then
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
   146
			log("warn", "Non-fatal error unloading module '%s' from '%s': %s", name, host, err);
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
   147
		end
674
4f506c627b49 modulemanager: module.unload now gets called when modules are being unloaded
Waqas Hussain <waqas20@gmail.com>
parents: 670
diff changeset
   148
	end
670
d5cf10b7fc44 Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents: 637
diff changeset
   149
	modulemap[host][name] = nil;
d5cf10b7fc44 Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents: 637
diff changeset
   150
	features_table:remove(host, name);
d5cf10b7fc44 Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents: 637
diff changeset
   151
	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
   152
	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
   153
		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
   154
		if handlers then
b72d408f5f15 modulemanager: Fixed error on unloading modules with no handlers
Waqas Hussain <waqas20@gmail.com>
parents: 686
diff changeset
   155
			handler_info[handlers[1]] = nil;
b72d408f5f15 modulemanager: Fixed error on unloading modules with no handlers
Waqas Hussain <waqas20@gmail.com>
parents: 686
diff changeset
   156
			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
   157
		end
439
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
   158
	end
686
13ed38531f69 modulemanager: Per-host event hooks for plugins - solves issue 41
Waqas Hussain <waqas20@gmail.com>
parents: 675
diff changeset
   159
	event_hooks:remove(host, name);
670
d5cf10b7fc44 Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents: 637
diff changeset
   160
	return true;
439
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
   161
end
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
   162
710
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   163
function reload(host, name, ...)
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   164
	local mod = modulemap[host] and modulemap[host][name];
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   165
	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
   166
713
2afd6d9e21cd modulemanager: Check for syntax errors before reloading a module
Waqas Hussain <waqas20@gmail.com>
parents: 710
diff changeset
   167
	local _mod, err = loadfile(plugin_dir.."mod_"..name..".lua"); -- checking for syntax errors
2afd6d9e21cd modulemanager: Check for syntax errors before reloading a module
Waqas Hussain <waqas20@gmail.com>
parents: 710
diff changeset
   168
	if not _mod then
2afd6d9e21cd modulemanager: Check for syntax errors before reloading a module
Waqas Hussain <waqas20@gmail.com>
parents: 710
diff changeset
   169
		log("error", "Unable to load module '%s': %s", module_name or "nil", err or "nil");
2afd6d9e21cd modulemanager: Check for syntax errors before reloading a module
Waqas Hussain <waqas20@gmail.com>
parents: 710
diff changeset
   170
		return nil, err;
2afd6d9e21cd modulemanager: Check for syntax errors before reloading a module
Waqas Hussain <waqas20@gmail.com>
parents: 710
diff changeset
   171
	end
2afd6d9e21cd modulemanager: Check for syntax errors before reloading a module
Waqas Hussain <waqas20@gmail.com>
parents: 710
diff changeset
   172
710
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   173
	local saved;
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   174
	if type(mod.module.save) == "function" then
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   175
		local ok, err = pcall(mod.module.save)
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   176
		if (not ok) and err then
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   177
			log("warn", "Non-fatal error unloading module '%s' from '%s': %s", name, host, err);
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   178
		else
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   179
			saved = err;
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   180
		end
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   181
	end
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   182
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   183
	unload(host, name, ...);
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   184
	if load(host, name, ...) then
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   185
		mod = modulemap[host] and modulemap[host][name];
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   186
		if type(mod.module.restore) == "function" then
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   187
			local ok, err = pcall(mod.module.restore, saved or {})
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   188
			if (not ok) and err then
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   189
				log("warn", "Non-fatal error unloading module '%s' from '%s': %s", name, host, err);
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   190
			end
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   191
		end
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   192
		return true;
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   193
	end
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   194
end
56f6c115bc69 modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents: 709
diff changeset
   195
578
5879264e28e2 Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents: 573
diff changeset
   196
function handle_stanza(host, origin, stanza)
5879264e28e2 Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents: 573
diff changeset
   197
	local name, xmlns, origin_type = stanza.name, stanza.attr.xmlns, origin.type;
5879264e28e2 Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents: 573
diff changeset
   198
	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
   199
		if stanza.attr.type == "get" or stanza.attr.type == "set" then
3758af511ce8 Don't try processing stanzas not of type get or set in module manager
Waqas Hussain <waqas20@gmail.com>
parents: 592
diff changeset
   200
			xmlns = stanza.tags[1].attr.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
   201
			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
   202
		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
   203
			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
   204
			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
   205
		end
578
5879264e28e2 Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents: 573
diff changeset
   206
	end
591
980ded4c60ef Removed unused variables
Waqas Hussain <waqas20@gmail.com>
parents: 590
diff changeset
   207
	local handlers = stanza_handlers:get(host, origin_type, name, xmlns);
578
5879264e28e2 Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents: 573
diff changeset
   208
	if handlers then
5879264e28e2 Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents: 573
diff changeset
   209
		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
   210
		(handlers[1])(origin, stanza);
5879264e28e2 Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents: 573
diff changeset
   211
		return true;
5879264e28e2 Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents: 573
diff changeset
   212
	else
5879264e28e2 Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents: 573
diff changeset
   213
		log("debug", "Stanza unhandled by any modules, xmlns: %s", stanza.attr.xmlns); -- we didn't handle it
5879264e28e2 Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents: 573
diff changeset
   214
	end
5879264e28e2 Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents: 573
diff changeset
   215
end
39
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
   216
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
   217
----- API functions exposed to modules -----------
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
   218
-- Must all be in 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
   219
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
   220
-- 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
   221
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
   222
	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
   223
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
   224
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
   225
-- 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
   226
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
   227
	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
   228
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
   229
590
54afe37cccbf Combined and merged similar code
Waqas Hussain <waqas20@gmail.com>
parents: 589
diff changeset
   230
local function _add_handler(module, origin_type, tag, xmlns, handler)
591
980ded4c60ef Removed unused variables
Waqas Hussain <waqas20@gmail.com>
parents: 590
diff changeset
   231
	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
   232
	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
   233
	if not handlers then
591
980ded4c60ef Removed unused variables
Waqas Hussain <waqas20@gmail.com>
parents: 590
diff changeset
   234
		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
   235
		handler_info[handler] = module;
670
d5cf10b7fc44 Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents: 637
diff changeset
   236
		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
   237
		--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
   238
	else
590
54afe37cccbf Combined and merged similar code
Waqas Hussain <waqas20@gmail.com>
parents: 589
diff changeset
   239
		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
   240
	end
5879264e28e2 Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents: 573
diff changeset
   241
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
   242
590
54afe37cccbf Combined and merged similar code
Waqas Hussain <waqas20@gmail.com>
parents: 589
diff changeset
   243
function api:add_handler(origin_type, tag, xmlns, handler)
54afe37cccbf Combined and merged similar code
Waqas Hussain <waqas20@gmail.com>
parents: 589
diff changeset
   244
	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
   245
	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
   246
		for _, origin_type in ipairs(origin_type) do
590
54afe37cccbf Combined and merged similar code
Waqas Hussain <waqas20@gmail.com>
parents: 589
diff changeset
   247
			_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
   248
		end
590
54afe37cccbf Combined and merged similar code
Waqas Hussain <waqas20@gmail.com>
parents: 589
diff changeset
   249
	else
54afe37cccbf Combined and merged similar code
Waqas Hussain <waqas20@gmail.com>
parents: 589
diff changeset
   250
		_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
   251
	end
590
54afe37cccbf Combined and merged similar code
Waqas Hussain <waqas20@gmail.com>
parents: 589
diff changeset
   252
end
54afe37cccbf Combined and merged similar code
Waqas Hussain <waqas20@gmail.com>
parents: 589
diff changeset
   253
function api:add_iq_handler(origin_type, xmlns, handler)
54afe37cccbf Combined and merged similar code
Waqas Hussain <waqas20@gmail.com>
parents: 589
diff changeset
   254
	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
   255
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
   256
670
d5cf10b7fc44 Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents: 637
diff changeset
   257
addDiscoInfoHandler("*host", function(reply, to, from, node)
d5cf10b7fc44 Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents: 637
diff changeset
   258
	if #node == 0 then
d5cf10b7fc44 Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents: 637
diff changeset
   259
		local done = {};
d5cf10b7fc44 Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents: 637
diff changeset
   260
		for module, features in pairs(features_table:get(to) or NULL) do -- for each module
d5cf10b7fc44 Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents: 637
diff changeset
   261
			for feature in pairs(features) do
d5cf10b7fc44 Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents: 637
diff changeset
   262
				if not done[feature] then
d5cf10b7fc44 Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents: 637
diff changeset
   263
					reply:tag("feature", {var = feature}):up(); -- TODO cache
d5cf10b7fc44 Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents: 637
diff changeset
   264
					done[feature] = true;
d5cf10b7fc44 Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents: 637
diff changeset
   265
				end
d5cf10b7fc44 Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents: 637
diff changeset
   266
			end
d5cf10b7fc44 Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents: 637
diff changeset
   267
		end
d5cf10b7fc44 Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents: 637
diff changeset
   268
		return next(done) ~= nil;
d5cf10b7fc44 Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents: 637
diff changeset
   269
	end
d5cf10b7fc44 Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents: 637
diff changeset
   270
end);
d5cf10b7fc44 Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents: 637
diff changeset
   271
540
ec03f6968fa8 Added function add_feature to modules API (for adding disco features)
Waqas Hussain <waqas20@gmail.com>
parents: 519
diff changeset
   272
function api:add_feature(xmlns)
670
d5cf10b7fc44 Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents: 637
diff changeset
   273
	features_table:set(self.host, self.name, xmlns, true);
540
ec03f6968fa8 Added function add_feature to modules API (for adding disco features)
Waqas Hussain <waqas20@gmail.com>
parents: 519
diff changeset
   274
end
ec03f6968fa8 Added function add_feature to modules API (for adding disco features)
Waqas Hussain <waqas20@gmail.com>
parents: 519
diff changeset
   275
686
13ed38531f69 modulemanager: Per-host event hooks for plugins - solves issue 41
Waqas Hussain <waqas20@gmail.com>
parents: 675
diff changeset
   276
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
   277
	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
   278
	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
   279
		handler(...);
13ed38531f69 modulemanager: Per-host event hooks for plugins - solves issue 41
Waqas Hussain <waqas20@gmail.com>
parents: 675
diff changeset
   280
	end
13ed38531f69 modulemanager: Per-host event hooks for plugins - solves issue 41
Waqas Hussain <waqas20@gmail.com>
parents: 675
diff changeset
   281
end;
13ed38531f69 modulemanager: Per-host event hooks for plugins - solves issue 41
Waqas Hussain <waqas20@gmail.com>
parents: 675
diff changeset
   282
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
   283
	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
   284
		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
   285
		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
   286
	end
13ed38531f69 modulemanager: Per-host event hooks for plugins - solves issue 41
Waqas Hussain <waqas20@gmail.com>
parents: 675
diff changeset
   287
	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
   288
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
   289
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
   290
--------------------------------------------------------------------
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
   291
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
   292
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
   293
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
   294
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
   295
	--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
   296
	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
   297
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
   298
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
   299
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
   300
	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
   301
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
   302
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
   303
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
   304
39
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
   305
return _M;