Software / code / prosody
Comparison
core/moduleapi.lua @ 12972:ead41e25ebc0
core: Prefix module imports with prosody namespace
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Fri, 17 Mar 2023 16:23:07 +0100 |
| parent | 12921:d238633a9d67 |
| child | 12994:5625da6ae6b6 |
comparison
equal
deleted
inserted
replaced
| 12971:7214baed9e9d | 12972:ead41e25ebc0 |
|---|---|
| 4 -- | 4 -- |
| 5 -- This project is MIT/X11 licensed. Please see the | 5 -- This project is MIT/X11 licensed. Please see the |
| 6 -- COPYING file in the source package for more information. | 6 -- COPYING file in the source package for more information. |
| 7 -- | 7 -- |
| 8 | 8 |
| 9 local array = require "util.array"; | 9 local array = require "prosody.util.array"; |
| 10 local set = require "util.set"; | 10 local set = require "prosody.util.set"; |
| 11 local it = require "util.iterators"; | 11 local it = require "prosody.util.iterators"; |
| 12 local logger = require "util.logger"; | 12 local logger = require "prosody.util.logger"; |
| 13 local timer = require "util.timer"; | 13 local timer = require "prosody.util.timer"; |
| 14 local resolve_relative_path = require"util.paths".resolve_relative_path; | 14 local resolve_relative_path = require"prosody.util.paths".resolve_relative_path; |
| 15 local st = require "util.stanza"; | 15 local st = require "prosody.util.stanza"; |
| 16 local cache = require "util.cache"; | 16 local cache = require "prosody.util.cache"; |
| 17 local errors = require "util.error"; | 17 local errors = require "prosody.util.error"; |
| 18 local promise = require "util.promise"; | 18 local promise = require "prosody.util.promise"; |
| 19 local time_now = require "util.time".now; | 19 local time_now = require "prosody.util.time".now; |
| 20 local format = require "util.format".format; | 20 local format = require "prosody.util.format".format; |
| 21 local jid_node = require "util.jid".node; | 21 local jid_node = require "prosody.util.jid".node; |
| 22 local jid_split = require "util.jid".split; | 22 local jid_split = require "prosody.util.jid".split; |
| 23 local jid_resource = require "util.jid".resource; | 23 local jid_resource = require "prosody.util.jid".resource; |
| 24 | 24 |
| 25 local t_insert, t_remove, t_concat = table.insert, table.remove, table.concat; | 25 local t_insert, t_remove, t_concat = table.insert, table.remove, table.concat; |
| 26 local error, setmetatable, type = error, setmetatable, type; | 26 local error, setmetatable, type = error, setmetatable, type; |
| 27 local ipairs, pairs, select = ipairs, pairs, select; | 27 local ipairs, pairs, select = ipairs, pairs, select; |
| 28 local tonumber, tostring = tonumber, tostring; | 28 local tonumber, tostring = tonumber, tostring; |
| 127 function api:wrap_global(event, handler) | 127 function api:wrap_global(event, handler) |
| 128 return self:hook_object_event(prosody.events, event, handler); | 128 return self:hook_object_event(prosody.events, event, handler); |
| 129 end | 129 end |
| 130 | 130 |
| 131 function api:require(lib) | 131 function api:require(lib) |
| 132 local modulemanager = require"core.modulemanager"; | 132 local modulemanager = require"prosody.core.modulemanager"; |
| 133 local f, n = modulemanager.loader:load_code_ext(self.name, lib, "lib.lua", self.environment); | 133 local f, n = modulemanager.loader:load_code_ext(self.name, lib, "lib.lua", self.environment); |
| 134 if not f then error("Failed to load plugin library '"..lib.."', error: "..n); end -- FIXME better error message | 134 if not f then error("Failed to load plugin library '"..lib.."', error: "..n); end -- FIXME better error message |
| 135 return f(); | 135 return f(); |
| 136 end | 136 end |
| 137 | 137 |
| 138 function api:depends(name) | 138 function api:depends(name) |
| 139 local modulemanager = require"core.modulemanager"; | 139 local modulemanager = require"prosody.core.modulemanager"; |
| 140 if self:get_option_inherited_set("modules_disabled", {}):contains(name) then | 140 if self:get_option_inherited_set("modules_disabled", {}):contains(name) then |
| 141 error("Dependency on disabled module mod_"..name); | 141 error("Dependency on disabled module mod_"..name); |
| 142 end | 142 end |
| 143 if not self.dependencies then | 143 if not self.dependencies then |
| 144 self.dependencies = {}; | 144 self.dependencies = {}; |
| 203 self.shared_data[path] = shared; | 203 self.shared_data[path] = shared; |
| 204 return shared; | 204 return shared; |
| 205 end | 205 end |
| 206 | 206 |
| 207 function api:get_option(name, default_value) | 207 function api:get_option(name, default_value) |
| 208 local config = require "core.configmanager"; | 208 local config = require "prosody.core.configmanager"; |
| 209 local value = config.get(self.host, name); | 209 local value = config.get(self.host, name); |
| 210 if value == nil then | 210 if value == nil then |
| 211 value = default_value; | 211 value = default_value; |
| 212 end | 212 end |
| 213 return value; | 213 return value; |
| 331 end | 331 end |
| 332 end | 332 end |
| 333 end | 333 end |
| 334 | 334 |
| 335 function api:get_host_items(key) | 335 function api:get_host_items(key) |
| 336 local modulemanager = require"core.modulemanager"; | 336 local modulemanager = require"prosody.core.modulemanager"; |
| 337 local result = modulemanager.get_items(key, self.host) or {}; | 337 local result = modulemanager.get_items(key, self.host) or {}; |
| 338 return result; | 338 return result; |
| 339 end | 339 end |
| 340 | 340 |
| 341 function api:handle_items(item_type, added_cb, removed_cb, existing) | 341 function api:handle_items(item_type, added_cb, removed_cb, existing) |
| 541 return io.open(path, mode); | 541 return io.open(path, mode); |
| 542 end | 542 end |
| 543 | 543 |
| 544 function api:open_store(name, store_type) | 544 function api:open_store(name, store_type) |
| 545 if self.host == "*" then return nil, "global-storage-not-supported"; end | 545 if self.host == "*" then return nil, "global-storage-not-supported"; end |
| 546 return require"core.storagemanager".open(self.host, name or self.name, store_type); | 546 return require"prosody.core.storagemanager".open(self.host, name or self.name, store_type); |
| 547 end | 547 end |
| 548 | 548 |
| 549 function api:measure(name, stat_type, conf) | 549 function api:measure(name, stat_type, conf) |
| 550 local measure = require "core.statsmanager".measure; | 550 local measure = require "prosody.core.statsmanager".measure; |
| 551 local fixed_label_key, fixed_label_value | 551 local fixed_label_key, fixed_label_value |
| 552 if self.host ~= "*" then | 552 if self.host ~= "*" then |
| 553 fixed_label_key = "host" | 553 fixed_label_key = "host" |
| 554 fixed_label_value = self.host | 554 fixed_label_value = self.host |
| 555 end | 555 end |
| 560 self:add_item("measure", { name = name, type = stat_type, conf = conf }); | 560 self:add_item("measure", { name = name, type = stat_type, conf = conf }); |
| 561 return measure(stat_type, "mod_"..self.name.."/"..name, conf, fixed_label_key, fixed_label_value) | 561 return measure(stat_type, "mod_"..self.name.."/"..name, conf, fixed_label_key, fixed_label_value) |
| 562 end | 562 end |
| 563 | 563 |
| 564 function api:metric(type_, name, unit, description, label_keys, conf) | 564 function api:metric(type_, name, unit, description, label_keys, conf) |
| 565 local metric = require "core.statsmanager".metric; | 565 local metric = require "prosody.core.statsmanager".metric; |
| 566 local is_scoped = self.host ~= "*" | 566 local is_scoped = self.host ~= "*" |
| 567 if is_scoped then | 567 if is_scoped then |
| 568 -- prepend `host` label to label keys if this is not a global module | 568 -- prepend `host` label to label keys if this is not a global module |
| 569 local orig_labels = label_keys | 569 local orig_labels = label_keys |
| 570 label_keys = array { "host" } | 570 label_keys = array { "host" } |