Merge 0.9->0.10
[prosody.git] / core / moduleapi.lua
index 5407782677e4075252a3211294d9b67174682d6f..e32c116aa7fe57d694df7a79b86d9c0251bfb887 100644 (file)
@@ -1,27 +1,33 @@
 -- Prosody IM
 -- Copyright (C) 2008-2012 Matthew Wild
 -- Copyright (C) 2008-2012 Waqas Hussain
--- 
+--
 -- This project is MIT/X11 licensed. Please see the
 -- COPYING file in the source package for more information.
 --
 
 local config = require "core.configmanager";
-local modulemanager = require "modulemanager";
+local modulemanager; -- This gets set from modulemanager
 local array = require "util.array";
 local set = require "util.set";
 local logger = require "util.logger";
 local pluginloader = require "util.pluginloader";
 local timer = require "util.timer";
+local resolve_relative_path = require"util.paths".resolve_relative_path;
+local measure = require "core.statsmanager".measure;
 
 local t_insert, t_remove, t_concat = table.insert, table.remove, table.concat;
 local error, setmetatable, type = error, setmetatable, type;
 local ipairs, pairs, select, unpack = ipairs, pairs, select, unpack;
 local tonumber, tostring = tonumber, tostring;
+local require = require;
 
 local prosody = prosody;
 local hosts = prosody.hosts;
-local core_post_stanza = prosody.core_post_stanza;
+
+-- FIXME: This assert() is to try and catch an obscure bug (2013-04-05)
+local core_post_stanza = assert(prosody.core_post_stanza,
+       "prosody.core_post_stanza is nil, please report this as a bug");
 
 -- Registry of shared module data
 local shared_data = setmetatable({}, { __mode = "v" });
@@ -41,7 +47,7 @@ function api:get_host()
 end
 
 function api:get_host_type()
-       return self.host ~= "*" and hosts[self.host].type or nil;
+       return (self.host == "*" and "global") or hosts[self.host].type or "local";
 end
 
 function api:set_global()
@@ -62,6 +68,20 @@ end
 function api:add_extension(data)
        self:add_item("extension", data);
 end
+function api:has_feature(xmlns)
+       for _, feature in ipairs(self:get_host_items("feature")) do
+               if feature == xmlns then return true; end
+       end
+       return false;
+end
+function api:has_identity(category, type, name)
+       for _, id in ipairs(self:get_host_items("identity")) do
+               if id.category == category and id.type == type and id.name == name then
+                       return true;
+               end
+       end
+       return false;
+end
 
 function api:fire_event(...)
        return (hosts[self.host] or prosody).events.fire_event(...);
@@ -96,6 +116,10 @@ function api:hook_tag(xmlns, name, handler, priority)
 end
 api.hook_stanza = api.hook_tag; -- COMPAT w/pre-0.9
 
+function api:unhook(event, handler)
+       return self:unhook_object_event((hosts[self.host] or prosody).events, event, handler);
+end
+
 function api:require(lib)
        local f, n = pluginloader.load_code(self.name, lib..".lib.lua", self.environment);
        if not f then
@@ -125,7 +149,7 @@ function api:depends(name)
        local mod = modulemanager.get_module(self.host, name) or modulemanager.get_module("*", name);
        if mod and mod.module.host == "*" and self.host ~= "*"
        and modulemanager.module_has_method(mod, "add_host") then
-               mod = nil; -- This is a shared module, so we still want to load it on our host
+               mod = nil; -- Target is a shared module, so we still want to load it on our host
        end
        if not mod then
                local err;
@@ -155,6 +179,9 @@ function api:shared(...)
                local shared = shared_data[path];
                if not shared then
                        shared = {};
+                       if path:match("%-cache$") then
+                               setmetatable(shared, { __mode = "kv" });
+                       end
                        shared_data[path] = shared;
                end
                t_insert(data_array, shared);
@@ -164,12 +191,9 @@ function api:shared(...)
 end
 
 function api:get_option(name, default_value)
-       local value = config.get(self.host, self.name, name);
+       local value = config.get(self.host, name);
        if value == nil then
-               value = config.get(self.host, "core", name);
-               if value == nil then
-                       value = default_value;
-               end
+               value = default_value;
        end
        return value;
 end
@@ -235,24 +259,40 @@ function api:get_option_array(name, ...)
        if value == nil then
                return nil;
        end
-       
+
        if type(value) ~= "table" then
                return array{ value }; -- Assume any non-list is a single-item list
        end
-       
+
        return array():append(value); -- Clone
 end
 
 function api:get_option_set(name, ...)
        local value = self:get_option_array(name, ...);
-       
+
        if value == nil then
                return nil;
        end
-       
+
        return set.new(value);
 end
 
+function api:get_option_inherited_set(name, ...)
+       local value = self:get_option_set(name, ...);
+       local global_value = self:context("*"):get_option_set(name, ...);
+       if not value then
+               return global_value;
+       elseif not global_value then
+               return value;
+       end
+       value:include(global_value);
+       return value;
+end
+
+function api:context(host)
+       return setmetatable({host=host or "*"}, {__index=self,__newindex=self});
+end
+
 function api:add_item(key, value)
        self.items = self.items or {};
        self.items[key] = self.items[key] or {};
@@ -271,23 +311,7 @@ function api:remove_item(key, value)
 end
 
 function api:get_host_items(key)
-       local result = {};
-       for mod_name, module in pairs(modulemanager.get_modules(self.host)) do
-               module = module.module;
-               if module.items then
-                       for _, item in ipairs(module.items[key] or NULL) do
-                               t_insert(result, item);
-                       end
-               end
-       end
-       for mod_name, module in pairs(modulemanager.get_modules("*")) do
-               module = module.module;
-               if module.items then
-                       for _, item in ipairs(module.items[key] or NULL) do
-                               t_insert(result, item);
-                       end
-               end
-       end
+       local result = modulemanager.get_items(key, self.host) or {};
        return result;
 end
 
@@ -302,7 +326,13 @@ function api:handle_items(type, added_cb, removed_cb, existing)
 end
 
 function api:provides(name, item)
-       if not item then item = self.environment; end
+       -- if not item then item = setmetatable({}, { __index = function(t,k) return rawget(self.environment, k); end }); end
+       if not item then
+               item = {}
+               for k,v in pairs(self.environment) do
+                       if k ~= "module" then item[k] = v; end
+               end
+       end
        if not item.name then
                local item_name = self.name;
                -- Strip a provider prefix to find the item name
@@ -312,6 +342,7 @@ function api:provides(name, item)
                end
                item.name = item_name;
        end
+       item._provided_by = self.name;
        self:add_item(name.."-provider", item);
 end
 
@@ -332,8 +363,21 @@ function api:get_directory()
 end
 
 function api:load_resource(path, mode)
-       path = config.resolve_relative_path(self:get_directory(), path);
+       path = resolve_relative_path(self:get_directory(), path);
        return io.open(path, mode);
 end
 
-return api;
\ No newline at end of file
+function api:open_store(name, type)
+       return require"core.storagemanager".open(self.host, name or self.name, type);
+end
+
+function api:measure(name, type)
+       return measure(type, "/"..self.host.."/mod_"..self.name.."/"..name);
+end
+
+function api.init(mm)
+       modulemanager = mm;
+       return api;
+end
+
+return api;