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"; -- This is necessary to avoid require loops
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);
65 function api:has_feature(xmlns)
66 for _, feature in ipairs(self:get_host_items("feature")) do
67 if feature == xmlns then return true; end
71 function api:has_identity(category, type, name)
72 for _, id in ipairs(self:get_host_items("identity")) do
73 if id.category == category and id.type == type and id.name == name then
80 function api:fire_event(...)
81 return (hosts[self.host] or prosody).events.fire_event(...);
84 function api:hook_object_event(object, event, handler, priority)
85 self.event_handlers:set(object, event, handler, true);
86 return object.add_handler(event, handler, priority);
89 function api:unhook_object_event(object, event, handler)
90 return object.remove_handler(event, handler);
93 function api:hook(event, handler, priority)
94 return self:hook_object_event((hosts[self.host] or prosody).events, event, handler, priority);
97 function api:hook_global(event, handler, priority)
98 return self:hook_object_event(prosody.events, event, handler, priority);
101 function api:hook_tag(xmlns, name, handler, priority)
102 if not handler and type(name) == "function" then
103 -- If only 2 options then they specified no xmlns
104 xmlns, name, handler, priority = nil, xmlns, name, handler;
105 elseif not (handler and name) then
106 self:log("warn", "Error: Insufficient parameters to module:hook_stanza()");
109 return self:hook("stanza/"..(xmlns and (xmlns..":") or "")..name, function (data) return handler(data.origin, data.stanza, data); end, priority);
111 api.hook_stanza = api.hook_tag; -- COMPAT w/pre-0.9
113 function api:require(lib)
114 local f, n = pluginloader.load_code(self.name, lib..".lib.lua", self.environment);
116 f, n = pluginloader.load_code(lib, lib..".lib.lua", self.environment);
118 if not f then error("Failed to load plugin library '"..lib.."', error: "..n); end -- FIXME better error message
122 function api:depends(name)
123 if not self.dependencies then
124 self.dependencies = {};
125 self:hook("module-reloaded", function (event)
126 if self.dependencies[event.module] and not self.reloading then
127 self:log("info", "Auto-reloading due to reload of %s:%s", event.host, event.module);
128 modulemanager.reload(self.host, self.name);
132 self:hook("module-unloaded", function (event)
133 if self.dependencies[event.module] then
134 self:log("info", "Auto-unloading due to unload of %s:%s", event.host, event.module);
135 modulemanager.unload(self.host, self.name);
139 local mod = modulemanager.get_module(self.host, name) or modulemanager.get_module("*", name);
140 if mod and mod.module.host == "*" and self.host ~= "*"
141 and modulemanager.module_has_method(mod, "add_host") then
142 mod = nil; -- Target is a shared module, so we still want to load it on our host
146 mod, err = modulemanager.load(self.host, name);
148 return error(("Unable to load required module, mod_%s: %s"):format(name, ((err or "unknown error"):gsub("%-", " ")) ));
151 self.dependencies[name] = true;
155 -- Returns one or more shared tables at the specified virtual paths
156 -- Intentionally does not allow the table at a path to be _set_, it
157 -- is auto-created if it does not exist.
158 function api:shared(...)
159 if not self.shared_data then self.shared_data = {}; end
160 local paths = { n = select("#", ...), ... };
161 local data_array = {};
162 local default_path_components = { self.host, self.name };
163 for i = 1, paths.n do
164 local path = paths[i];
165 if path:sub(1,1) ~= "/" then -- Prepend default components
166 local n_components = select(2, path:gsub("/", "%1"));
167 path = (n_components<#default_path_components and "/" or "")..t_concat(default_path_components, "/", 1, #default_path_components-n_components).."/"..path;
169 local shared = shared_data[path];
172 if path:match("%-cache$") then
173 setmetatable(shared, { __mode = "kv" });
175 shared_data[path] = shared;
177 t_insert(data_array, shared);
178 self.shared_data[path] = shared;
180 return unpack(data_array);
183 function api:get_option(name, default_value)
184 local value = config.get(self.host, name);
186 value = default_value;
191 function api:get_option_string(name, default_value)
192 local value = self:get_option(name, default_value);
193 if type(value) == "table" then
195 self:log("error", "Config option '%s' does not take a list, using just the first item", name);
202 return tostring(value);
205 function api:get_option_number(name, ...)
206 local value = self:get_option(name, ...);
207 if type(value) == "table" then
209 self:log("error", "Config option '%s' does not take a list, using just the first item", name);
213 local ret = tonumber(value);
214 if value ~= nil and ret == nil then
215 self:log("error", "Config option '%s' not understood, expecting a number", name);
220 function api:get_option_boolean(name, ...)
221 local value = self:get_option(name, ...);
222 if type(value) == "table" then
224 self:log("error", "Config option '%s' does not take a list, using just the first item", name);
231 local ret = value == true or value == "true" or value == 1 or nil;
233 ret = (value == false or value == "false" or value == 0);
241 self:log("error", "Config option '%s' not understood, expecting true/false", name);
246 function api:get_option_array(name, ...)
247 local value = self:get_option(name, ...);
253 if type(value) ~= "table" then
254 return array{ value }; -- Assume any non-list is a single-item list
257 return array():append(value); -- Clone
260 function api:get_option_set(name, ...)
261 local value = self:get_option_array(name, ...);
267 return set.new(value);
270 function api:add_item(key, value)
271 self.items = self.items or {};
272 self.items[key] = self.items[key] or {};
273 t_insert(self.items[key], value);
274 self:fire_event("item-added/"..key, {source = self, item = value});
276 function api:remove_item(key, value)
277 local t = self.items and self.items[key] or NULL;
279 if t[i] == value then
280 t_remove(self.items[key], i);
281 self:fire_event("item-removed/"..key, {source = self, item = value});
287 function api:get_host_items(key)
288 local result = modulemanager.get_items(key, self.host) or {};
292 function api:handle_items(type, added_cb, removed_cb, existing)
293 self:hook("item-added/"..type, added_cb);
294 self:hook("item-removed/"..type, removed_cb);
295 if existing ~= false then
296 for _, item in ipairs(self:get_host_items(type)) do
297 added_cb({ item = item });
302 function api:provides(name, item)
303 if not item then item = self.environment; end
304 if not item.name then
305 local item_name = self.name;
306 -- Strip a provider prefix to find the item name
307 -- (e.g. "auth_foo" -> "foo" for an auth provider)
308 if item_name:find(name.."_", 1, true) == 1 then
309 item_name = item_name:sub(#name+2);
311 item.name = item_name;
313 self:add_item(name.."-provider", item);
316 function api:send(stanza)
317 return core_post_stanza(hosts[self.host], stanza);
320 function api:add_timer(delay, callback)
321 return timer.add_task(delay, function (t)
322 if self.loaded == false then return; end
327 local path_sep = package.config:sub(1,1);
328 function api:get_directory()
329 return self.path and (self.path:gsub("%"..path_sep.."[^"..path_sep.."]*$", "")) or nil;
332 function api:load_resource(path, mode)
333 path = config.resolve_relative_path(self:get_directory(), path);
334 return io.open(path, mode);