2 -- Copyright (C) 2008-2012 Matthew Wild
3 -- Copyright (C) 2008-2012 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
9 local config = require "core.configmanager";
10 local modulemanager = require "modulemanager";
11 local array = require "util.array";
12 local set = require "util.set";
13 local logger = require "util.logger";
14 local pluginloader = require "util.pluginloader";
15 local timer = require "util.timer";
17 local t_insert, t_remove, t_concat = table.insert, table.remove, table.concat;
18 local error, setmetatable, type = error, setmetatable, type;
19 local ipairs, pairs, select, unpack = ipairs, pairs, select, unpack;
20 local tonumber, tostring = tonumber, tostring;
22 local prosody = prosody;
23 local hosts = prosody.hosts;
24 local core_post_stanza = prosody.core_post_stanza;
26 -- Registry of shared module data
27 local shared_data = setmetatable({}, { __mode = "v" });
33 -- Returns the name of the current module
34 function api:get_name()
38 -- Returns the host that the current module is serving
39 function api:get_host()
43 function api:get_host_type()
44 return self.host ~= "*" and hosts[self.host].type or nil;
47 function api:set_global()
50 local _log = logger.init("mod_"..self.name);
51 self.log = function (self, ...) return _log(...); end;
56 function api:add_feature(xmlns)
57 self:add_item("feature", xmlns);
59 function api:add_identity(category, type, name)
60 self:add_item("identity", {category = category, type = type, name = name});
62 function api:add_extension(data)
63 self:add_item("extension", data);
66 function api:fire_event(...)
67 return (hosts[self.host] or prosody).events.fire_event(...);
70 function api:hook_object_event(object, event, handler, priority)
71 self.event_handlers:set(object, event, handler, true);
72 return object.add_handler(event, handler, priority);
75 function api:unhook_object_event(object, event, handler)
76 return object.remove_handler(event, handler);
79 function api:hook(event, handler, priority)
80 return self:hook_object_event((hosts[self.host] or prosody).events, event, handler, priority);
83 function api:hook_global(event, handler, priority)
84 return self:hook_object_event(prosody.events, event, handler, priority);
87 function api:hook_tag(xmlns, name, handler, priority)
88 if not handler and type(name) == "function" then
89 -- If only 2 options then they specified no xmlns
90 xmlns, name, handler, priority = nil, xmlns, name, handler;
91 elseif not (handler and name) then
92 self:log("warn", "Error: Insufficient parameters to module:hook_stanza()");
95 return self:hook("stanza/"..(xmlns and (xmlns..":") or "")..name, function (data) return handler(data.origin, data.stanza, data); end, priority);
97 api.hook_stanza = api.hook_tag; -- COMPAT w/pre-0.9
99 function api:require(lib)
100 local f, n = pluginloader.load_code(self.name, lib..".lib.lua", self.environment);
102 f, n = pluginloader.load_code(lib, lib..".lib.lua", self.environment);
104 if not f then error("Failed to load plugin library '"..lib.."', error: "..n); end -- FIXME better error message
108 function api:depends(name)
109 if not self.dependencies then
110 self.dependencies = {};
111 self:hook("module-reloaded", function (event)
112 if self.dependencies[event.module] and not self.reloading then
113 self:log("info", "Auto-reloading due to reload of %s:%s", event.host, event.module);
114 modulemanager.reload(self.host, self.name);
118 self:hook("module-unloaded", function (event)
119 if self.dependencies[event.module] then
120 self:log("info", "Auto-unloading due to unload of %s:%s", event.host, event.module);
121 modulemanager.unload(self.host, self.name);
125 local mod = modulemanager.get_module(self.host, name) or modulemanager.get_module("*", name);
126 if mod and mod.module.host == "*" and self.host ~= "*"
127 and modulemanager.module_has_method(mod, "add_host") then
128 mod = nil; -- This is a shared module, so we still want to load it on our host
132 mod, err = modulemanager.load(self.host, name);
134 return error(("Unable to load required module, mod_%s: %s"):format(name, ((err or "unknown error"):gsub("%-", " ")) ));
137 self.dependencies[name] = true;
141 -- Returns one or more shared tables at the specified virtual paths
142 -- Intentionally does not allow the table at a path to be _set_, it
143 -- is auto-created if it does not exist.
144 function api:shared(...)
145 if not self.shared_data then self.shared_data = {}; end
146 local paths = { n = select("#", ...), ... };
147 local data_array = {};
148 local default_path_components = { self.host, self.name };
149 for i = 1, paths.n do
150 local path = paths[i];
151 if path:sub(1,1) ~= "/" then -- Prepend default components
152 local n_components = select(2, path:gsub("/", "%1"));
153 path = (n_components<#default_path_components and "/" or "")..t_concat(default_path_components, "/", 1, #default_path_components-n_components).."/"..path;
155 local shared = shared_data[path];
158 shared_data[path] = shared;
160 t_insert(data_array, shared);
161 self.shared_data[path] = shared;
163 return unpack(data_array);
166 function api:get_option(name, default_value)
167 local value = config.get(self.host, self.name, name);
169 value = config.get(self.host, "core", name);
171 value = default_value;
177 function api:get_option_string(name, default_value)
178 local value = self:get_option(name, default_value);
179 if type(value) == "table" then
181 self:log("error", "Config option '%s' does not take a list, using just the first item", name);
188 return tostring(value);
191 function api:get_option_number(name, ...)
192 local value = self:get_option(name, ...);
193 if type(value) == "table" then
195 self:log("error", "Config option '%s' does not take a list, using just the first item", name);
199 local ret = tonumber(value);
200 if value ~= nil and ret == nil then
201 self:log("error", "Config option '%s' not understood, expecting a number", name);
206 function api:get_option_boolean(name, ...)
207 local value = self:get_option(name, ...);
208 if type(value) == "table" then
210 self:log("error", "Config option '%s' does not take a list, using just the first item", name);
217 local ret = value == true or value == "true" or value == 1 or nil;
219 ret = (value == false or value == "false" or value == 0);
227 self:log("error", "Config option '%s' not understood, expecting true/false", name);
232 function api:get_option_array(name, ...)
233 local value = self:get_option(name, ...);
239 if type(value) ~= "table" then
240 return array{ value }; -- Assume any non-list is a single-item list
243 return array():append(value); -- Clone
246 function api:get_option_set(name, ...)
247 local value = self:get_option_array(name, ...);
253 return set.new(value);
256 function api:add_item(key, value)
257 self.items = self.items or {};
258 self.items[key] = self.items[key] or {};
259 t_insert(self.items[key], value);
260 self:fire_event("item-added/"..key, {source = self, item = value});
262 function api:remove_item(key, value)
263 local t = self.items and self.items[key] or NULL;
265 if t[i] == value then
266 t_remove(self.items[key], i);
267 self:fire_event("item-removed/"..key, {source = self, item = value});
273 function api:get_host_items(key)
275 for mod_name, module in pairs(modulemanager.get_modules(self.host)) do
276 module = module.module;
278 for _, item in ipairs(module.items[key] or NULL) do
279 t_insert(result, item);
283 for mod_name, module in pairs(modulemanager.get_modules("*")) do
284 module = module.module;
286 for _, item in ipairs(module.items[key] or NULL) do
287 t_insert(result, item);
294 function api:handle_items(type, added_cb, removed_cb, existing)
295 self:hook("item-added/"..type, added_cb);
296 self:hook("item-removed/"..type, removed_cb);
297 if existing ~= false then
298 for _, item in ipairs(self:get_host_items(type)) do
299 added_cb({ item = item });
304 function api:provides(name, item)
305 if not item then item = self.environment; end
306 if not item.name then
307 local item_name = self.name;
308 -- Strip a provider prefix to find the item name
309 -- (e.g. "auth_foo" -> "foo" for an auth provider)
310 if item_name:find(name.."_", 1, true) == 1 then
311 item_name = item_name:sub(#name+2);
313 item.name = item_name;
315 self:add_item(name.."-provider", item);
318 function api:send(stanza)
319 return core_post_stanza(hosts[self.host], stanza);
322 function api:add_timer(delay, callback)
323 return timer.add_task(delay, function (t)
324 if self.loaded == false then return; end
329 local path_sep = package.config:sub(1,1);
330 function api:get_directory()
331 return self.path and (self.path:gsub("%"..path_sep.."[^"..path_sep.."]*$", "")) or nil;
334 function api:load_resource(path, mode)
335 path = config.resolve_relative_path(self:get_directory(), path);
336 return io.open(path, mode);