------ API functions exposed to modules -----------
--- Must all be in api.*
-
--- Returns the name of the current module
-function api:get_name()
- return self.name;
-end
-
--- Returns the host that the current module is serving
-function api:get_host()
- return self.host;
-end
-
-function api:get_host_type()
- return hosts[self.host].type;
-end
-
-function api:set_global()
- self.host = "*";
- -- Update the logger
- local _log = logger.init("mod_"..self.name);
- self.log = function (self, ...) return _log(...); end;
- self._log = _log;
-end
-
-local function _add_handler(module, origin_type, tag, xmlns, handler)
- local handlers = stanza_handlers:get(module.host, origin_type, tag, xmlns);
- local msg = (tag == "iq") and "namespace" or "payload namespace";
- if not handlers then
- stanza_handlers:add(module.host, origin_type, tag, xmlns, handler);
- handler_info[handler] = module;
- handler_table:add(module.host, module.name, {module.host, origin_type, tag, xmlns});
- --module:log("debug", "I now handle tag '%s' [%s] with %s '%s'", tag, origin_type, msg, xmlns);
- else
- 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);
- end
-end
-
-function api:add_handler(origin_type, tag, xmlns, handler)
- if not (origin_type and tag and xmlns and handler) then return false; end
- if type(origin_type) == "table" then
- for _, origin_type in ipairs(origin_type) do
- _add_handler(self, origin_type, tag, xmlns, handler);
- end
- else
- _add_handler(self, origin_type, tag, xmlns, handler);
- end
-end
-function api:add_iq_handler(origin_type, xmlns, handler)
- self:add_handler(origin_type, "iq", xmlns, handler);
-end
-
-function api:add_feature(xmlns)
- self:add_item("feature", xmlns);
-end
-function api:add_identity(category, type, name)
- self:add_item("identity", {category = category, type = type, name = name});
-end
-
-local event_hook = function(host, mod_name, event_name, ...)
- if type((...)) == "table" and (...).host and (...).host ~= host then return; end
- for handler in pairs(event_hooks:get(host, mod_name, event_name) or NULL) do
- handler(...);
- end
-end;
-function api:add_event_hook(name, handler)
- if not hooked:get(self.host, self.name, name) then
- eventmanager.add_event_hook(name, function(...) event_hook(self.host, self.name, name, ...); end);
- hooked:set(self.host, self.name, name, true);
- end
- event_hooks:set(self.host, self.name, name, handler, true);
-end
-
-function api:fire_event(...)
- return (hosts[self.host] or prosody).events.fire_event(...);
-end
-
-function api:hook(event, handler, priority)
- hooks:set(self.host, self.name, event, handler, true);
- (hosts[self.host] or prosody).events.add_handler(event, handler, priority);
-end
-
-function api:hook_stanza(xmlns, name, handler, priority)
- if not handler and type(name) == "function" then
- -- If only 2 options then they specified no xmlns
- xmlns, name, handler, priority = nil, xmlns, name, handler;
- elseif not (handler and name) then
- self:log("warn", "Error: Insufficient parameters to module:hook_stanza()");
- return;
- end
- return api.hook(self, "stanza/"..(xmlns and (xmlns..":") or "")..name, function (data) return handler(data.origin, data.stanza, data); end, priority);
-end
-
-function api:require(lib)
- local f, n = pluginloader.load_code(self.name, lib..".lib.lua");
- if not f then
- f, n = pluginloader.load_code(lib, lib..".lib.lua");