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 multitable_new = require "util.multitable".new;
19 local t_insert, t_remove, t_concat = table.insert, table.remove, table.concat;
20 local error, setmetatable, setfenv, type = error, setmetatable, setfenv, type;
21 local ipairs, pairs, select, unpack = ipairs, pairs, select, unpack;
22 local tonumber, tostring = tonumber, tostring;
24 local prosody = prosody;
25 local hosts = prosody.hosts;
26 local core_post_stanza = prosody.core_post_stanza;
28 -- Registry of shared module data
29 local shared_data = setmetatable({}, { __mode = "v" });
35 -- Returns the name of the current module
36 function api:get_name()
40 -- Returns the host that the current module is serving
41 function api:get_host()
45 function api:get_host_type()
46 return self.host ~= "*" and hosts[self.host].type or nil;
49 function api:set_global()
52 local _log = logger.init("mod_"..self.name);
53 self.log = function (self, ...) return _log(...); end;
58 function api:add_feature(xmlns)
59 self:add_item("feature", xmlns);
61 function api:add_identity(category, type, name)
62 self:add_item("identity", {category = category, type = type, name = name});
64 function api:add_extension(data)
65 self:add_item("extension", data);
68 function api:fire_event(...)
69 return (hosts[self.host] or prosody).events.fire_event(...);
72 function api:hook_object_event(object, event, handler, priority)
73 local handlers = self.event_handlers[event];
76 self.event_handlers[event] = handlers;
78 handlers[event] = { handler = handler, priority = priority, object = object };
79 return object.add_handler(event, handler, priority);
82 function api:unhook_object_event(object, event, handler)
83 return object.remove_handler(event, handler);
86 function api:hook(event, handler, priority)
87 return self:hook_object_event((hosts[self.host] or prosody).events, event, handler, priority);
90 function api:hook_global(event, handler, priority)
91 return self:hook_object_event(prosody.events, event, handler, priority);
94 function api:hook_tag(xmlns, name, handler, priority)
95 if not handler and type(name) == "function" then
96 -- If only 2 options then they specified no xmlns
97 xmlns, name, handler, priority = nil, xmlns, name, handler;
98 elseif not (handler and name) then
99 self:log("warn", "Error: Insufficient parameters to module:hook_stanza()");
102 return self:hook("stanza/"..(xmlns and (xmlns..":") or "")..name, function (data) return handler(data.origin, data.stanza, data); end, priority);
104 api.hook_stanza = api.hook_tag; -- COMPAT w/pre-0.9
106 function api:require(lib)
107 local f, n = pluginloader.load_code(self.name, lib..".lib.lua");
109 f, n = pluginloader.load_code(lib, lib..".lib.lua");
111 if not f then error("Failed to load plugin library '"..lib.."', error: "..n); end -- FIXME better error message
112 setfenv(f, self.environment);
116 function api:depends(name)
117 if not self.dependencies then
118 self.dependencies = {};
119 self:hook("module-reloaded", function (event)
120 if self.dependencies[event.module] and not self.reloading then
121 self:log("info", "Auto-reloading due to reload of %s:%s", event.host, event.module);
122 modulemanager.reload(self.host, self.name);
126 self:hook("module-unloaded", function (event)
127 if self.dependencies[event.module] then
128 self:log("info", "Auto-unloading due to unload of %s:%s", event.host, event.module);
129 modulemanager.unload(self.host, self.name);
133 local mod = modulemanager.get_module(self.host, name) or modulemanager.get_module("*", name);
134 if mod and mod.module.host == "*" and self.host ~= "*"
135 and modulemanager.module_has_method(mod, "add_host") then
136 mod = nil; -- This is a shared module, so we still want to load it on our host
140 mod, err = modulemanager.load(self.host, name);
142 return error(("Unable to load required module, mod_%s: %s"):format(name, ((err or "unknown error"):gsub("%-", " ")) ));
145 self.dependencies[name] = true;
149 -- Returns one or more shared tables at the specified virtual paths
150 -- Intentionally does not allow the table at a path to be _set_, it
151 -- is auto-created if it does not exist.
152 function api:shared(...)
153 if not self.shared_data then self.shared_data = {}; end
154 local paths = { n = select("#", ...), ... };
155 local data_array = {};
156 local default_path_components = { self.host, self.name };
157 for i = 1, paths.n do
158 local path = paths[i];
159 if path:sub(1,1) ~= "/" then -- Prepend default components
160 local n_components = select(2, path:gsub("/", "%1"));
161 path = (n_components<#default_path_components and "/" or "")..t_concat(default_path_components, "/", 1, #default_path_components-n_components).."/"..path;
163 local shared = shared_data[path];
166 shared_data[path] = shared;
168 t_insert(data_array, shared);
169 self.shared_data[path] = shared;
171 return unpack(data_array);
174 function api:get_option(name, default_value)
175 local value = config.get(self.host, self.name, name);
177 value = config.get(self.host, "core", name);
179 value = default_value;
185 function api:get_option_string(name, default_value)
186 local value = self:get_option(name, default_value);
187 if type(value) == "table" then
189 self:log("error", "Config option '%s' does not take a list, using just the first item", name);
196 return tostring(value);
199 function api:get_option_number(name, ...)
200 local value = self:get_option(name, ...);
201 if type(value) == "table" then
203 self:log("error", "Config option '%s' does not take a list, using just the first item", name);
207 local ret = tonumber(value);
208 if value ~= nil and ret == nil then
209 self:log("error", "Config option '%s' not understood, expecting a number", name);
214 function api:get_option_boolean(name, ...)
215 local value = self:get_option(name, ...);
216 if type(value) == "table" then
218 self:log("error", "Config option '%s' does not take a list, using just the first item", name);
225 local ret = value == true or value == "true" or value == 1 or nil;
227 ret = (value == false or value == "false" or value == 0);
235 self:log("error", "Config option '%s' not understood, expecting true/false", name);
240 function api:get_option_array(name, ...)
241 local value = self:get_option(name, ...);
247 if type(value) ~= "table" then
248 return array{ value }; -- Assume any non-list is a single-item list
251 return array():append(value); -- Clone
254 function api:get_option_set(name, ...)
255 local value = self:get_option_array(name, ...);
261 return set.new(value);
264 function api:add_item(key, value)
265 self.items = self.items or {};
266 self.items[key] = self.items[key] or {};
267 t_insert(self.items[key], value);
268 self:fire_event("item-added/"..key, {source = self, item = value});
270 function api:remove_item(key, value)
271 local t = self.items and self.items[key] or NULL;
273 if t[i] == value then
274 t_remove(self.items[key], i);
275 self:fire_event("item-removed/"..key, {source = self, item = value});
281 function api:get_host_items(key)
283 for mod_name, module in pairs(modulemanager.get_modules(self.host)) do
284 module = module.module;
286 for _, item in ipairs(module.items[key] or NULL) do
287 t_insert(result, item);
291 for mod_name, module in pairs(modulemanager.get_modules("*")) do
292 module = module.module;
294 for _, item in ipairs(module.items[key] or NULL) do
295 t_insert(result, item);
302 function api:handle_items(type, added_cb, removed_cb, existing)
303 self:hook("item-added/"..type, added_cb);
304 self:hook("item-removed/"..type, removed_cb);
305 if existing ~= false then
306 for _, item in ipairs(self:get_host_items(type)) do
307 added_cb({ item = item });
312 function api:provides(name, item)
313 if not item then item = self.environment; end
314 if not item.name then
315 local item_name = self.name;
316 -- Strip a provider prefix to find the item name
317 -- (e.g. "auth_foo" -> "foo" for an auth provider)
318 if item_name:find(name.."_", 1, true) == 1 then
319 item_name = item_name:sub(#name+2);
321 item.name = item_name;
323 self:add_item(name.."-provider", item);
326 function api:send(stanza)
327 return core_post_stanza(hosts[self.host], stanza);
330 function api:add_timer(delay, callback)
331 return timer.add_task(delay, function (t)
332 if self.loaded == false then return; end
337 local path_sep = package.config:sub(1,1);
338 function api:get_directory()
339 return self.path and (self.path:gsub("%"..path_sep.."[^"..path_sep.."]*$", "")) or nil;
342 function api:load_resource(path, mode)
343 path = config.resolve_relative_path(self:get_directory(), path);
344 return io.open(path, mode);