core/moduleapi.lua
author Matthew Wild <mwild1@gmail.com>
Sun, 22 Jan 2012 19:35:50 +0000
changeset 4538 d0a89c1c43fd
parent 4534 7a0a31c4f6c5
child 4539 3cbfa768eb06
permissions -rw-r--r--
moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4531
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     1
-- Prosody IM
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     2
-- Copyright (C) 2008-2012 Matthew Wild
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     3
-- Copyright (C) 2008-2012 Waqas Hussain
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     4
-- 
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     5
-- This project is MIT/X11 licensed. Please see the
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     6
-- COPYING file in the source package for more information.
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     7
--
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     8
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     9
local config = require "core.configmanager";
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    10
local modulemanager = require "modulemanager";
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    11
local array = require "util.array";
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    12
local set = require "util.set";
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    13
local logger = require "util.logger";
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    14
local pluginloader = require "util.pluginloader";
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    15
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    16
local multitable_new = require "util.multitable".new;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    17
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    18
local t_insert, t_remove, t_concat = table.insert, table.remove, table.concat;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    19
local error, setmetatable, setfenv, type = error, setmetatable, setfenv, type;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    20
local ipairs, pairs, select, unpack = ipairs, pairs, select, unpack;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    21
local tonumber, tostring = tonumber, tostring;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    22
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    23
local prosody = prosody;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    24
local hosts = prosody.hosts;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    25
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    26
-- Registry of shared module data
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    27
local shared_data = setmetatable({}, { __mode = "v" });
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    28
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    29
local NULL = {};
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    30
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    31
local api = {};
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    32
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    33
-- Returns the name of the current module
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    34
function api:get_name()
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    35
	return self.name;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    36
end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    37
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    38
-- Returns the host that the current module is serving
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    39
function api:get_host()
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    40
	return self.host;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    41
end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    42
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    43
function api:get_host_type()
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    44
	return hosts[self.host].type;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    45
end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    46
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    47
function api:set_global()
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    48
	self.host = "*";
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    49
	-- Update the logger
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    50
	local _log = logger.init("mod_"..self.name);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    51
	self.log = function (self, ...) return _log(...); end;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    52
	self._log = _log;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    53
end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    54
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    55
function api:add_feature(xmlns)
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    56
	self:add_item("feature", xmlns);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    57
end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    58
function api:add_identity(category, type, name)
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    59
	self:add_item("identity", {category = category, type = type, name = name});
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    60
end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    61
function api:add_extension(data)
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    62
	self:add_item("extension", data);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    63
end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    64
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    65
function api:fire_event(...)
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    66
	return (hosts[self.host] or prosody).events.fire_event(...);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    67
end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    68
4534
7a0a31c4f6c5 modulemanager, moduleapi: Replace hooks multitable with an event_handlers map stored in individual modules. Also adds module:hook_object_event() to hook events on any util.events compatible object.
Matthew Wild <mwild1@gmail.com>
parents: 4531
diff changeset
    69
function api:hook_object_event(object, event, handler, priority)
7a0a31c4f6c5 modulemanager, moduleapi: Replace hooks multitable with an event_handlers map stored in individual modules. Also adds module:hook_object_event() to hook events on any util.events compatible object.
Matthew Wild <mwild1@gmail.com>
parents: 4531
diff changeset
    70
	self.event_handlers[handler] = { name = event, priority = priority, object = object };
7a0a31c4f6c5 modulemanager, moduleapi: Replace hooks multitable with an event_handlers map stored in individual modules. Also adds module:hook_object_event() to hook events on any util.events compatible object.
Matthew Wild <mwild1@gmail.com>
parents: 4531
diff changeset
    71
	return object.add_handler(event, handler, priority);
7a0a31c4f6c5 modulemanager, moduleapi: Replace hooks multitable with an event_handlers map stored in individual modules. Also adds module:hook_object_event() to hook events on any util.events compatible object.
Matthew Wild <mwild1@gmail.com>
parents: 4531
diff changeset
    72
end
7a0a31c4f6c5 modulemanager, moduleapi: Replace hooks multitable with an event_handlers map stored in individual modules. Also adds module:hook_object_event() to hook events on any util.events compatible object.
Matthew Wild <mwild1@gmail.com>
parents: 4531
diff changeset
    73
4531
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    74
function api:hook(event, handler, priority)
4534
7a0a31c4f6c5 modulemanager, moduleapi: Replace hooks multitable with an event_handlers map stored in individual modules. Also adds module:hook_object_event() to hook events on any util.events compatible object.
Matthew Wild <mwild1@gmail.com>
parents: 4531
diff changeset
    75
	return self:hook_object_event((hosts[self.host] or prosody).events, event, handler, priority);
4531
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    76
end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    77
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    78
function api:hook_global(event, handler, priority)
4534
7a0a31c4f6c5 modulemanager, moduleapi: Replace hooks multitable with an event_handlers map stored in individual modules. Also adds module:hook_object_event() to hook events on any util.events compatible object.
Matthew Wild <mwild1@gmail.com>
parents: 4531
diff changeset
    79
	return self:hook_object_event(prosody.events, event, handler, priority);
4531
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    80
end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    81
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    82
function api:hook_stanza(xmlns, name, handler, priority)
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    83
	if not handler and type(name) == "function" then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    84
		-- If only 2 options then they specified no xmlns
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    85
		xmlns, name, handler, priority = nil, xmlns, name, handler;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    86
	elseif not (handler and name) then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    87
		self:log("warn", "Error: Insufficient parameters to module:hook_stanza()");
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    88
		return;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    89
	end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    90
	return self:hook("stanza/"..(xmlns and (xmlns..":") or "")..name, function (data) return handler(data.origin, data.stanza, data); end, priority);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    91
end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    92
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    93
function api:require(lib)
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    94
	local f, n = pluginloader.load_code(self.name, lib..".lib.lua");
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    95
	if not f then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    96
		f, n = pluginloader.load_code(lib, lib..".lib.lua");
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    97
	end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    98
	if not f then error("Failed to load plugin library '"..lib.."', error: "..n); end -- FIXME better error message
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    99
	setfenv(f, self.environment);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   100
	return f();
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   101
end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   102
4538
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
   103
function api:depends(name)
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
   104
	if not self.dependencies then
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
   105
		self.dependencies = {};
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
   106
		self:hook("module-reloaded", function (event)
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
   107
			if self.dependencies[event.module] then
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
   108
				self:log("info", "Auto-reloading due to reload of %s:%s", event.host, event.module);
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
   109
				modulemanager.reload(self.host, self.name);
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
   110
				return;
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
   111
			end
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
   112
		end);
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
   113
		self:hook("module-unloaded", function (event)
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
   114
			if self.dependencies[event.module] then
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
   115
				self:log("info", "Auto-unloading due to unload of %s:%s", event.host, event.module);
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
   116
				modulemanager.unload(self.host, self.name);
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
   117
			end
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
   118
		end);
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
   119
	end
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
   120
	local mod = modulemanager.get_module(self.host, name) or modulemanager.get_module("*", name);
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
   121
	if not mod then
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
   122
		local err;
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
   123
		mod, err = modulemanager.load(self.host, name);
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
   124
		if not mod then
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
   125
			return error(("Unable to load required module, mod_%s: %s"):format(name, ((err or "unknown error"):gsub("%-", " ")) ));
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
   126
		end
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
   127
	end
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
   128
	self.dependencies[name] = true;
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
   129
	return mod;
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
   130
end
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
   131
4531
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   132
function api:get_option(name, default_value)
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   133
	local value = config.get(self.host, self.name, name);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   134
	if value == nil then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   135
		value = config.get(self.host, "core", name);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   136
		if value == nil then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   137
			value = default_value;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   138
		end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   139
	end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   140
	return value;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   141
end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   142
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   143
function api:get_option_string(name, default_value)
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   144
	local value = self:get_option(name, default_value);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   145
	if type(value) == "table" then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   146
		if #value > 1 then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   147
			self:log("error", "Config option '%s' does not take a list, using just the first item", name);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   148
		end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   149
		value = value[1];
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   150
	end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   151
	if value == nil then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   152
		return nil;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   153
	end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   154
	return tostring(value);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   155
end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   156
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   157
function api:get_option_number(name, ...)
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   158
	local value = self:get_option(name, ...);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   159
	if type(value) == "table" then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   160
		if #value > 1 then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   161
			self:log("error", "Config option '%s' does not take a list, using just the first item", name);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   162
		end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   163
		value = value[1];
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   164
	end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   165
	local ret = tonumber(value);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   166
	if value ~= nil and ret == nil then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   167
		self:log("error", "Config option '%s' not understood, expecting a number", name);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   168
	end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   169
	return ret;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   170
end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   171
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   172
function api:get_option_boolean(name, ...)
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   173
	local value = self:get_option(name, ...);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   174
	if type(value) == "table" then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   175
		if #value > 1 then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   176
			self:log("error", "Config option '%s' does not take a list, using just the first item", name);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   177
		end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   178
		value = value[1];
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   179
	end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   180
	if value == nil then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   181
		return nil;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   182
	end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   183
	local ret = value == true or value == "true" or value == 1 or nil;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   184
	if ret == nil then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   185
		ret = (value == false or value == "false" or value == 0);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   186
		if ret then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   187
			ret = false;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   188
		else
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   189
			ret = nil;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   190
		end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   191
	end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   192
	if ret == nil then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   193
		self:log("error", "Config option '%s' not understood, expecting true/false", name);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   194
	end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   195
	return ret;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   196
end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   197
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   198
function api:get_option_array(name, ...)
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   199
	local value = self:get_option(name, ...);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   200
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   201
	if value == nil then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   202
		return nil;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   203
	end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   204
	
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   205
	if type(value) ~= "table" then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   206
		return array{ value }; -- Assume any non-list is a single-item list
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   207
	end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   208
	
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   209
	return array():append(value); -- Clone
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   210
end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   211
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   212
function api:get_option_set(name, ...)
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   213
	local value = self:get_option_array(name, ...);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   214
	
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   215
	if value == nil then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   216
		return nil;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   217
	end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   218
	
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   219
	return set.new(value);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   220
end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   221
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   222
local module_items = multitable_new();
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   223
function api:add_item(key, value)
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   224
	self.items = self.items or {};
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   225
	self.items[key] = self.items[key] or {};
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   226
	t_insert(self.items[key], value);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   227
	self:fire_event("item-added/"..key, {source = self, item = value});
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   228
end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   229
function api:remove_item(key, value)
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   230
	local t = self.items and self.items[key] or NULL;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   231
	for i = #t,1,-1 do
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   232
		if t[i] == value then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   233
			t_remove(self.items[key], i);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   234
			self:fire_event("item-removed/"..key, {source = self, item = value});
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   235
			return value;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   236
		end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   237
	end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   238
end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   239
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   240
function api:get_host_items(key)
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   241
	local result = {};
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   242
	for mod_name, module in pairs(modulemanager.get_modules(self.host)) do
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   243
		module = module.module;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   244
		if module.items then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   245
			for _, item in ipairs(module.items[key] or NULL) do
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   246
				t_insert(result, item);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   247
			end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   248
		end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   249
	end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   250
	for mod_name, module in pairs(modulemanager.get_modules("*")) do
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   251
		module = module.module;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   252
		if module.items then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   253
			for _, item in ipairs(module.items[key] or NULL) do
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   254
				t_insert(result, item);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   255
			end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   256
		end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   257
	end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   258
	return result;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   259
end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   260
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   261
function api:handle_items(type, added_cb, removed_cb, existing)
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   262
	self:hook("item-added/"..type, added_cb);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   263
	self:hook("item-removed/"..type, removed_cb);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   264
	if existing ~= false then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   265
		for _, item in ipairs(self:get_host_items(type)) do
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   266
			added_cb({ item = item });
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   267
		end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   268
	end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   269
end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   270
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   271
return api;