Mercurial > prosody-hg
annotate core/moduleapi.lua @ 13946:f5e8ab42c708
mod_http_file_share: Check that files are still there with correct size
Failed uploads can leave behind unused slots. Files shouldn't change
size after they have been successfully uploaded, but might as well
double check it.
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Sat, 04 Dec 2021 18:56:51 +0100 |
| parents | a0faff6ba853 |
| children | cf21d285385b |
| rev | line source |
|---|---|
|
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 -- Prosody IM |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 -- Copyright (C) 2008-2012 Matthew Wild |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 -- Copyright (C) 2008-2012 Waqas Hussain |
|
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5771
diff
changeset
|
4 -- |
|
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 -- This project is MIT/X11 licensed. Please see the |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 -- COPYING file in the source package for more information. |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 -- |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 |
|
12972
ead41e25ebc0
core: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12921
diff
changeset
|
9 local array = require "prosody.util.array"; |
|
ead41e25ebc0
core: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12921
diff
changeset
|
10 local set = require "prosody.util.set"; |
|
ead41e25ebc0
core: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12921
diff
changeset
|
11 local it = require "prosody.util.iterators"; |
|
ead41e25ebc0
core: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12921
diff
changeset
|
12 local logger = require "prosody.util.logger"; |
|
ead41e25ebc0
core: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12921
diff
changeset
|
13 local timer = require "prosody.util.timer"; |
|
ead41e25ebc0
core: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12921
diff
changeset
|
14 local resolve_relative_path = require"prosody.util.paths".resolve_relative_path; |
|
ead41e25ebc0
core: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12921
diff
changeset
|
15 local st = require "prosody.util.stanza"; |
|
ead41e25ebc0
core: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12921
diff
changeset
|
16 local cache = require "prosody.util.cache"; |
|
ead41e25ebc0
core: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12921
diff
changeset
|
17 local errors = require "prosody.util.error"; |
|
ead41e25ebc0
core: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12921
diff
changeset
|
18 local promise = require "prosody.util.promise"; |
|
ead41e25ebc0
core: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12921
diff
changeset
|
19 local time_now = require "prosody.util.time".now; |
|
ead41e25ebc0
core: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12921
diff
changeset
|
20 local format = require "prosody.util.format".format; |
|
ead41e25ebc0
core: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12921
diff
changeset
|
21 local jid_node = require "prosody.util.jid".node; |
|
ead41e25ebc0
core: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12921
diff
changeset
|
22 local jid_split = require "prosody.util.jid".split; |
|
ead41e25ebc0
core: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12921
diff
changeset
|
23 local jid_resource = require "prosody.util.jid".resource; |
|
13204
c9ef35fab0b1
core.moduleapi: Add :get_option_period for parsing time intervals
Kim Alvefur <zash@zash.se>
parents:
13203
diff
changeset
|
24 local human_io = require "prosody.util.human.io"; |
|
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 local t_insert, t_remove, t_concat = table.insert, table.remove, table.concat; |
|
5021
85b2689dbcfe
Eliminate direct setfenv usage
Florian Zeitz <florob@babelmonkeys.de>
parents:
4896
diff
changeset
|
27 local error, setmetatable, type = error, setmetatable, type; |
|
7162
d0b64f1e4f5d
loggingmanager,modulemanager,moduleapi: Localize unpack compatible with Lua 5.2+
Kim Alvefur <zash@zash.se>
parents:
7127
diff
changeset
|
28 local ipairs, pairs, select = ipairs, pairs, select; |
|
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 local tonumber, tostring = tonumber, tostring; |
|
6414
31c15004bfb0
core.moduleapi: Use require instead of global to get storagemanager in module:open_store()
Kim Alvefur <zash@zash.se>
parents:
6165
diff
changeset
|
30 local require = require; |
|
12590
5eaf77114fdb
compat: Use table.pack (there since Lua 5.2) over our util.table
Kim Alvefur <zash@zash.se>
parents:
12589
diff
changeset
|
31 local pack = table.pack; |
|
12589
39ae08180c81
compat: Remove handling of Lua 5.1 location of 'unpack' function
Kim Alvefur <zash@zash.se>
parents:
12479
diff
changeset
|
32 local unpack = table.unpack; |
|
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 local prosody = prosody; |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 local hosts = prosody.hosts; |
|
5434
9dd36e20c1e3
moduleapi: assert() that prosody.core_post_stanza is not nil
Matthew Wild <mwild1@gmail.com>
parents:
5412
diff
changeset
|
36 |
|
9dd36e20c1e3
moduleapi: assert() that prosody.core_post_stanza is not nil
Matthew Wild <mwild1@gmail.com>
parents:
5412
diff
changeset
|
37 -- FIXME: This assert() is to try and catch an obscure bug (2013-04-05) |
|
9dd36e20c1e3
moduleapi: assert() that prosody.core_post_stanza is not nil
Matthew Wild <mwild1@gmail.com>
parents:
5412
diff
changeset
|
38 local core_post_stanza = assert(prosody.core_post_stanza, |
|
9dd36e20c1e3
moduleapi: assert() that prosody.core_post_stanza is not nil
Matthew Wild <mwild1@gmail.com>
parents:
5412
diff
changeset
|
39 "prosody.core_post_stanza is nil, please report this as a bug"); |
|
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 -- Registry of shared module data |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 local shared_data = setmetatable({}, { __mode = "v" }); |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 local NULL = {}; |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 local api = {}; |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 -- Returns the name of the current module |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 function api:get_name() |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 return self.name; |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 end |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 -- Returns the host that the current module is serving |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 function api:get_host() |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 return self.host; |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 end |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 function api:get_host_type() |
|
5771
c4ed6680bf8d
moduleapi: module:get_host_type() now returns 'global' for * and 'local' for non-components
Matthew Wild <mwild1@gmail.com>
parents:
5530
diff
changeset
|
59 return (self.host == "*" and "global") or hosts[self.host].type or "local"; |
|
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 end |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 function api:set_global() |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 self.host = "*"; |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 -- Update the logger |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 local _log = logger.init("mod_"..self.name); |
|
6656
58c111a39d27
moduleapi: Add luacheck annotation
Matthew Wild <mwild1@gmail.com>
parents:
6655
diff
changeset
|
66 self.log = function (self, ...) return _log(...); end; --luacheck: ignore self |
|
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 self._log = _log; |
|
4605
69746db53125
moduleapi: Set module.global = true when module:set_global() is called
Matthew Wild <mwild1@gmail.com>
parents:
4539
diff
changeset
|
68 self.global = true; |
|
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 end |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 function api:add_feature(xmlns) |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 self:add_item("feature", xmlns); |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 end |
|
6655
a99ed5f5f709
moduleapi: Minor variable renaming to avoid clashes with the 'type' function
Matthew Wild <mwild1@gmail.com>
parents:
6654
diff
changeset
|
74 function api:add_identity(category, identity_type, name) |
|
a99ed5f5f709
moduleapi: Minor variable renaming to avoid clashes with the 'type' function
Matthew Wild <mwild1@gmail.com>
parents:
6654
diff
changeset
|
75 self:add_item("identity", {category = category, type = identity_type, name = name}); |
|
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 end |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 function api:add_extension(data) |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 self:add_item("extension", data); |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 end |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 function api:fire_event(...) |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 return (hosts[self.host] or prosody).events.fire_event(...); |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 end |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 |
|
4534
7a0a31c4f6c5
modulemanager, moduleapi: Replace hooks multitable with an event_handlers map stored in individual modules. Also adds module:hook_object_event() to hook events on any util.events compatible object.
Matthew Wild <mwild1@gmail.com>
parents:
4531
diff
changeset
|
85 function api:hook_object_event(object, event, handler, priority) |
|
4896
27cda15104f2
modulemanager, moduleapi: Turn module.event_handlers into a multitable and track object->event->handler associations correctly (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents:
4893
diff
changeset
|
86 self.event_handlers:set(object, event, handler, true); |
|
4534
7a0a31c4f6c5
modulemanager, moduleapi: Replace hooks multitable with an event_handlers map stored in individual modules. Also adds module:hook_object_event() to hook events on any util.events compatible object.
Matthew Wild <mwild1@gmail.com>
parents:
4531
diff
changeset
|
87 return object.add_handler(event, handler, priority); |
|
7a0a31c4f6c5
modulemanager, moduleapi: Replace hooks multitable with an event_handlers map stored in individual modules. Also adds module:hook_object_event() to hook events on any util.events compatible object.
Matthew Wild <mwild1@gmail.com>
parents:
4531
diff
changeset
|
88 end |
|
7a0a31c4f6c5
modulemanager, moduleapi: Replace hooks multitable with an event_handlers map stored in individual modules. Also adds module:hook_object_event() to hook events on any util.events compatible object.
Matthew Wild <mwild1@gmail.com>
parents:
4531
diff
changeset
|
89 |
|
4708
0e324923ff95
moduleapi: Fix parameters to unhook_object_event()
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
90 function api:unhook_object_event(object, event, handler) |
|
6654
22a7ee3379bc
moduleapi: Clear self.event_handlers when unhooking an event, to prevent leaks
Matthew Wild <mwild1@gmail.com>
parents:
6653
diff
changeset
|
91 self.event_handlers:set(object, event, handler, nil); |
|
4695
838ad61c6b2c
moduleapi: Add module:unhook_object_event()
Matthew Wild <mwild1@gmail.com>
parents:
4666
diff
changeset
|
92 return object.remove_handler(event, handler); |
|
838ad61c6b2c
moduleapi: Add module:unhook_object_event()
Matthew Wild <mwild1@gmail.com>
parents:
4666
diff
changeset
|
93 end |
|
838ad61c6b2c
moduleapi: Add module:unhook_object_event()
Matthew Wild <mwild1@gmail.com>
parents:
4666
diff
changeset
|
94 |
|
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
95 function api:hook(event, handler, priority) |
|
13910
622790951285
moduleapi: :hook(): don't silently fall back to global events when host unknown
Matthew Wild <mwild1@gmail.com>
parents:
13909
diff
changeset
|
96 local events; |
|
622790951285
moduleapi: :hook(): don't silently fall back to global events when host unknown
Matthew Wild <mwild1@gmail.com>
parents:
13909
diff
changeset
|
97 local host = self.host; |
|
622790951285
moduleapi: :hook(): don't silently fall back to global events when host unknown
Matthew Wild <mwild1@gmail.com>
parents:
13909
diff
changeset
|
98 if host == "*" then |
|
622790951285
moduleapi: :hook(): don't silently fall back to global events when host unknown
Matthew Wild <mwild1@gmail.com>
parents:
13909
diff
changeset
|
99 events = prosody.events; |
|
622790951285
moduleapi: :hook(): don't silently fall back to global events when host unknown
Matthew Wild <mwild1@gmail.com>
parents:
13909
diff
changeset
|
100 else |
|
622790951285
moduleapi: :hook(): don't silently fall back to global events when host unknown
Matthew Wild <mwild1@gmail.com>
parents:
13909
diff
changeset
|
101 events = hosts[host].events; |
|
622790951285
moduleapi: :hook(): don't silently fall back to global events when host unknown
Matthew Wild <mwild1@gmail.com>
parents:
13909
diff
changeset
|
102 end |
|
622790951285
moduleapi: :hook(): don't silently fall back to global events when host unknown
Matthew Wild <mwild1@gmail.com>
parents:
13909
diff
changeset
|
103 return self:hook_object_event(events, event, handler, priority); |
|
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
104 end |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
105 |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
106 function api:hook_global(event, handler, priority) |
|
4534
7a0a31c4f6c5
modulemanager, moduleapi: Replace hooks multitable with an event_handlers map stored in individual modules. Also adds module:hook_object_event() to hook events on any util.events compatible object.
Matthew Wild <mwild1@gmail.com>
parents:
4531
diff
changeset
|
107 return self:hook_object_event(prosody.events, event, handler, priority); |
|
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
108 end |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
109 |
|
4719
2087d42f1e77
moduleapi: Rename module:hook_stanza() -> module:hook_tag() (hook_stanza works for compat)
Matthew Wild <mwild1@gmail.com>
parents:
4718
diff
changeset
|
110 function api:hook_tag(xmlns, name, handler, priority) |
|
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
111 if not handler and type(name) == "function" then |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
112 -- If only 2 options then they specified no xmlns |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
113 xmlns, name, handler, priority = nil, xmlns, name, handler; |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
114 elseif not (handler and name) then |
|
11921
89aef37fca54
core.moduleapi: Fix name of renamed API in log message
Kim Alvefur <zash@zash.se>
parents:
11823
diff
changeset
|
115 self:log("warn", "Error: Insufficient parameters to module:hook_tag()"); |
|
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
116 return; |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
117 end |
|
7947
24170d74b00b
core: Split some very long lines [luacheck]
Kim Alvefur <zash@zash.se>
parents:
7646
diff
changeset
|
118 return self:hook("stanza/"..(xmlns and (xmlns..":") or "")..name, |
|
24170d74b00b
core: Split some very long lines [luacheck]
Kim Alvefur <zash@zash.se>
parents:
7646
diff
changeset
|
119 function (data) return handler(data.origin, data.stanza, data); end, priority); |
|
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
120 end |
|
4719
2087d42f1e77
moduleapi: Rename module:hook_stanza() -> module:hook_tag() (hook_stanza works for compat)
Matthew Wild <mwild1@gmail.com>
parents:
4718
diff
changeset
|
121 api.hook_stanza = api.hook_tag; -- COMPAT w/pre-0.9 |
|
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
122 |
|
5825
ac5e05ffc921
moduleapi: Add module:unhook()
Matthew Wild <mwild1@gmail.com>
parents:
5776
diff
changeset
|
123 function api:unhook(event, handler) |
|
ac5e05ffc921
moduleapi: Add module:unhook()
Matthew Wild <mwild1@gmail.com>
parents:
5776
diff
changeset
|
124 return self:unhook_object_event((hosts[self.host] or prosody).events, event, handler); |
|
ac5e05ffc921
moduleapi: Add module:unhook()
Matthew Wild <mwild1@gmail.com>
parents:
5776
diff
changeset
|
125 end |
|
ac5e05ffc921
moduleapi: Add module:unhook()
Matthew Wild <mwild1@gmail.com>
parents:
5776
diff
changeset
|
126 |
|
6640
859e9af53aa1
moduleapi: New methods for modules to conveniently wrap events
Matthew Wild <mwild1@gmail.com>
parents:
6639
diff
changeset
|
127 function api:wrap_object_event(events_object, event, handler) |
|
859e9af53aa1
moduleapi: New methods for modules to conveniently wrap events
Matthew Wild <mwild1@gmail.com>
parents:
6639
diff
changeset
|
128 return self:hook_object_event(assert(events_object.wrappers, "no wrappers"), event, handler); |
|
859e9af53aa1
moduleapi: New methods for modules to conveniently wrap events
Matthew Wild <mwild1@gmail.com>
parents:
6639
diff
changeset
|
129 end |
|
859e9af53aa1
moduleapi: New methods for modules to conveniently wrap events
Matthew Wild <mwild1@gmail.com>
parents:
6639
diff
changeset
|
130 |
|
859e9af53aa1
moduleapi: New methods for modules to conveniently wrap events
Matthew Wild <mwild1@gmail.com>
parents:
6639
diff
changeset
|
131 function api:wrap_event(event, handler) |
|
859e9af53aa1
moduleapi: New methods for modules to conveniently wrap events
Matthew Wild <mwild1@gmail.com>
parents:
6639
diff
changeset
|
132 return self:wrap_object_event((hosts[self.host] or prosody).events, event, handler); |
|
859e9af53aa1
moduleapi: New methods for modules to conveniently wrap events
Matthew Wild <mwild1@gmail.com>
parents:
6639
diff
changeset
|
133 end |
|
859e9af53aa1
moduleapi: New methods for modules to conveniently wrap events
Matthew Wild <mwild1@gmail.com>
parents:
6639
diff
changeset
|
134 |
|
859e9af53aa1
moduleapi: New methods for modules to conveniently wrap events
Matthew Wild <mwild1@gmail.com>
parents:
6639
diff
changeset
|
135 function api:wrap_global(event, handler) |
|
6652
06116b2789f0
moduleapi: Remove accidental use of undefined and unnecessary 'priority' variable
Matthew Wild <mwild1@gmail.com>
parents:
6651
diff
changeset
|
136 return self:hook_object_event(prosody.events, event, handler); |
|
6640
859e9af53aa1
moduleapi: New methods for modules to conveniently wrap events
Matthew Wild <mwild1@gmail.com>
parents:
6639
diff
changeset
|
137 end |
|
859e9af53aa1
moduleapi: New methods for modules to conveniently wrap events
Matthew Wild <mwild1@gmail.com>
parents:
6639
diff
changeset
|
138 |
|
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
139 function api:require(lib) |
|
12972
ead41e25ebc0
core: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12921
diff
changeset
|
140 local modulemanager = require"prosody.core.modulemanager"; |
|
12253
57d35fcde488
modulemanager, moduleapi: Switch to new pluginloader interface
Matthew Wild <mwild1@gmail.com>
parents:
12095
diff
changeset
|
141 local f, n = modulemanager.loader:load_code_ext(self.name, lib, "lib.lua", self.environment); |
|
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
142 if not f then error("Failed to load plugin library '"..lib.."', error: "..n); end -- FIXME better error message |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
143 return f(); |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
144 end |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
145 |
|
13719
4309c934e813
moduleapi: Allow soft dependencies via module:depends(mod, true)
Matthew Wild <mwild1@gmail.com>
parents:
13604
diff
changeset
|
146 function api:depends(name, soft) |
|
12972
ead41e25ebc0
core: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12921
diff
changeset
|
147 local modulemanager = require"prosody.core.modulemanager"; |
|
9556
e4c09e335bd9
moduleapi: Prevent loading disabled module as dependency of enabled one
Kim Alvefur <zash@zash.se>
parents:
9509
diff
changeset
|
148 if self:get_option_inherited_set("modules_disabled", {}):contains(name) then |
|
13719
4309c934e813
moduleapi: Allow soft dependencies via module:depends(mod, true)
Matthew Wild <mwild1@gmail.com>
parents:
13604
diff
changeset
|
149 if not soft then |
|
4309c934e813
moduleapi: Allow soft dependencies via module:depends(mod, true)
Matthew Wild <mwild1@gmail.com>
parents:
13604
diff
changeset
|
150 error("Dependency on disabled module mod_"..name); |
|
4309c934e813
moduleapi: Allow soft dependencies via module:depends(mod, true)
Matthew Wild <mwild1@gmail.com>
parents:
13604
diff
changeset
|
151 end |
|
4309c934e813
moduleapi: Allow soft dependencies via module:depends(mod, true)
Matthew Wild <mwild1@gmail.com>
parents:
13604
diff
changeset
|
152 self:log("debug", "Not loading disabled soft dependency mod_%s", name); |
|
4309c934e813
moduleapi: Allow soft dependencies via module:depends(mod, true)
Matthew Wild <mwild1@gmail.com>
parents:
13604
diff
changeset
|
153 return nil, "disabled"; |
|
9556
e4c09e335bd9
moduleapi: Prevent loading disabled module as dependency of enabled one
Kim Alvefur <zash@zash.se>
parents:
9509
diff
changeset
|
154 end |
|
4538
d0a89c1c43fd
moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents:
4534
diff
changeset
|
155 if not self.dependencies then |
|
d0a89c1c43fd
moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents:
4534
diff
changeset
|
156 self.dependencies = {}; |
|
d0a89c1c43fd
moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents:
4534
diff
changeset
|
157 self:hook("module-reloaded", function (event) |
|
4854
dab55c6f7710
moduleapi: Don't auto-reload self when already reloading (fixes reload of modules with cyclic dependencies)
Matthew Wild <mwild1@gmail.com>
parents:
4790
diff
changeset
|
158 if self.dependencies[event.module] and not self.reloading then |
|
4538
d0a89c1c43fd
moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents:
4534
diff
changeset
|
159 self:log("info", "Auto-reloading due to reload of %s:%s", event.host, event.module); |
|
d0a89c1c43fd
moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents:
4534
diff
changeset
|
160 modulemanager.reload(self.host, self.name); |
|
d0a89c1c43fd
moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents:
4534
diff
changeset
|
161 return; |
|
d0a89c1c43fd
moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents:
4534
diff
changeset
|
162 end |
|
d0a89c1c43fd
moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents:
4534
diff
changeset
|
163 end); |
|
d0a89c1c43fd
moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents:
4534
diff
changeset
|
164 self:hook("module-unloaded", function (event) |
|
d0a89c1c43fd
moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents:
4534
diff
changeset
|
165 if self.dependencies[event.module] then |
|
d0a89c1c43fd
moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents:
4534
diff
changeset
|
166 self:log("info", "Auto-unloading due to unload of %s:%s", event.host, event.module); |
|
d0a89c1c43fd
moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents:
4534
diff
changeset
|
167 modulemanager.unload(self.host, self.name); |
|
d0a89c1c43fd
moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents:
4534
diff
changeset
|
168 end |
|
d0a89c1c43fd
moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents:
4534
diff
changeset
|
169 end); |
|
d0a89c1c43fd
moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents:
4534
diff
changeset
|
170 end |
|
d0a89c1c43fd
moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents:
4534
diff
changeset
|
171 local mod = modulemanager.get_module(self.host, name) or modulemanager.get_module("*", name); |
|
4707
d8fc9a1aabeb
moduleapi: module:depends(): Don't load shared modules onto the current host if the current host is '*'...
Matthew Wild <mwild1@gmail.com>
parents:
4695
diff
changeset
|
172 if mod and mod.module.host == "*" and self.host ~= "*" |
|
d8fc9a1aabeb
moduleapi: module:depends(): Don't load shared modules onto the current host if the current host is '*'...
Matthew Wild <mwild1@gmail.com>
parents:
4695
diff
changeset
|
173 and modulemanager.module_has_method(mod, "add_host") then |
|
5077
6c2c8bf36d22
moduleapi: Clarify comment
Matthew Wild <mwild1@gmail.com>
parents:
5053
diff
changeset
|
174 mod = nil; -- Target is a shared module, so we still want to load it on our host |
|
4663
24524d70a50a
moduleapi: module:depends(): Load shared modules onto the current host even if they are loaded globally already
Matthew Wild <mwild1@gmail.com>
parents:
4661
diff
changeset
|
175 end |
|
4538
d0a89c1c43fd
moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents:
4534
diff
changeset
|
176 if not mod then |
|
d0a89c1c43fd
moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents:
4534
diff
changeset
|
177 local err; |
|
d0a89c1c43fd
moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents:
4534
diff
changeset
|
178 mod, err = modulemanager.load(self.host, name); |
|
d0a89c1c43fd
moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents:
4534
diff
changeset
|
179 if not mod then |
|
d0a89c1c43fd
moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents:
4534
diff
changeset
|
180 return error(("Unable to load required module, mod_%s: %s"):format(name, ((err or "unknown error"):gsub("%-", " ")) )); |
|
d0a89c1c43fd
moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents:
4534
diff
changeset
|
181 end |
|
d0a89c1c43fd
moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents:
4534
diff
changeset
|
182 end |
|
d0a89c1c43fd
moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents:
4534
diff
changeset
|
183 self.dependencies[name] = true; |
|
12921
d238633a9d67
core.moduleapi: Record reverse dependencies
Kim Alvefur <zash@zash.se>
parents:
12874
diff
changeset
|
184 if not mod.module.reverse_dependencies then |
|
d238633a9d67
core.moduleapi: Record reverse dependencies
Kim Alvefur <zash@zash.se>
parents:
12874
diff
changeset
|
185 mod.module.reverse_dependencies = {}; |
|
d238633a9d67
core.moduleapi: Record reverse dependencies
Kim Alvefur <zash@zash.se>
parents:
12874
diff
changeset
|
186 end |
|
d238633a9d67
core.moduleapi: Record reverse dependencies
Kim Alvefur <zash@zash.se>
parents:
12874
diff
changeset
|
187 mod.module.reverse_dependencies[self.name] = true; |
|
4538
d0a89c1c43fd
moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents:
4534
diff
changeset
|
188 return mod; |
|
d0a89c1c43fd
moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents:
4534
diff
changeset
|
189 end |
|
d0a89c1c43fd
moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents:
4534
diff
changeset
|
190 |
|
9149
d03f21729b2c
moduleapi: Remove multiple-parameters feature from module:shared()
Matthew Wild <mwild1@gmail.com>
parents:
9042
diff
changeset
|
191 local function get_shared_table_from_path(module, tables, path) |
|
d03f21729b2c
moduleapi: Remove multiple-parameters feature from module:shared()
Matthew Wild <mwild1@gmail.com>
parents:
9042
diff
changeset
|
192 if path:sub(1,1) ~= "/" then -- Prepend default components |
|
d03f21729b2c
moduleapi: Remove multiple-parameters feature from module:shared()
Matthew Wild <mwild1@gmail.com>
parents:
9042
diff
changeset
|
193 local default_path_components = { module.host, module.name }; |
|
d03f21729b2c
moduleapi: Remove multiple-parameters feature from module:shared()
Matthew Wild <mwild1@gmail.com>
parents:
9042
diff
changeset
|
194 local n_components = select(2, path:gsub("/", "%1")); |
|
d03f21729b2c
moduleapi: Remove multiple-parameters feature from module:shared()
Matthew Wild <mwild1@gmail.com>
parents:
9042
diff
changeset
|
195 path = (n_components<#default_path_components and "/" or "") |
|
d03f21729b2c
moduleapi: Remove multiple-parameters feature from module:shared()
Matthew Wild <mwild1@gmail.com>
parents:
9042
diff
changeset
|
196 ..t_concat(default_path_components, "/", 1, #default_path_components-n_components).."/"..path; |
|
d03f21729b2c
moduleapi: Remove multiple-parameters feature from module:shared()
Matthew Wild <mwild1@gmail.com>
parents:
9042
diff
changeset
|
197 end |
|
d03f21729b2c
moduleapi: Remove multiple-parameters feature from module:shared()
Matthew Wild <mwild1@gmail.com>
parents:
9042
diff
changeset
|
198 local shared = tables[path]; |
|
d03f21729b2c
moduleapi: Remove multiple-parameters feature from module:shared()
Matthew Wild <mwild1@gmail.com>
parents:
9042
diff
changeset
|
199 if not shared then |
|
d03f21729b2c
moduleapi: Remove multiple-parameters feature from module:shared()
Matthew Wild <mwild1@gmail.com>
parents:
9042
diff
changeset
|
200 shared = {}; |
|
d03f21729b2c
moduleapi: Remove multiple-parameters feature from module:shared()
Matthew Wild <mwild1@gmail.com>
parents:
9042
diff
changeset
|
201 if path:match("%-cache$") then |
|
d03f21729b2c
moduleapi: Remove multiple-parameters feature from module:shared()
Matthew Wild <mwild1@gmail.com>
parents:
9042
diff
changeset
|
202 setmetatable(shared, { __mode = "kv" }); |
|
d03f21729b2c
moduleapi: Remove multiple-parameters feature from module:shared()
Matthew Wild <mwild1@gmail.com>
parents:
9042
diff
changeset
|
203 end |
|
d03f21729b2c
moduleapi: Remove multiple-parameters feature from module:shared()
Matthew Wild <mwild1@gmail.com>
parents:
9042
diff
changeset
|
204 tables[path] = shared; |
|
d03f21729b2c
moduleapi: Remove multiple-parameters feature from module:shared()
Matthew Wild <mwild1@gmail.com>
parents:
9042
diff
changeset
|
205 end |
|
d03f21729b2c
moduleapi: Remove multiple-parameters feature from module:shared()
Matthew Wild <mwild1@gmail.com>
parents:
9042
diff
changeset
|
206 return shared; |
|
d03f21729b2c
moduleapi: Remove multiple-parameters feature from module:shared()
Matthew Wild <mwild1@gmail.com>
parents:
9042
diff
changeset
|
207 end |
|
d03f21729b2c
moduleapi: Remove multiple-parameters feature from module:shared()
Matthew Wild <mwild1@gmail.com>
parents:
9042
diff
changeset
|
208 |
|
d03f21729b2c
moduleapi: Remove multiple-parameters feature from module:shared()
Matthew Wild <mwild1@gmail.com>
parents:
9042
diff
changeset
|
209 -- Returns a shared table at the specified virtual path |
|
d03f21729b2c
moduleapi: Remove multiple-parameters feature from module:shared()
Matthew Wild <mwild1@gmail.com>
parents:
9042
diff
changeset
|
210 -- Intentionally does not allow the table to be _set_, it |
|
4539
3cbfa768eb06
moduleapi: Add module:shared(), a way to easily share data between multiple loaded modules
Matthew Wild <mwild1@gmail.com>
parents:
4538
diff
changeset
|
211 -- is auto-created if it does not exist. |
|
9149
d03f21729b2c
moduleapi: Remove multiple-parameters feature from module:shared()
Matthew Wild <mwild1@gmail.com>
parents:
9042
diff
changeset
|
212 function api:shared(path) |
|
4651
d1739d72100a
moduleapi: Have modules internally store a reference to shared tables they use, to ensure they don't get collected while any module that had access to that table is still loaded (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents:
4614
diff
changeset
|
213 if not self.shared_data then self.shared_data = {}; end |
|
9149
d03f21729b2c
moduleapi: Remove multiple-parameters feature from module:shared()
Matthew Wild <mwild1@gmail.com>
parents:
9042
diff
changeset
|
214 local shared = get_shared_table_from_path(self, shared_data, path); |
|
d03f21729b2c
moduleapi: Remove multiple-parameters feature from module:shared()
Matthew Wild <mwild1@gmail.com>
parents:
9042
diff
changeset
|
215 self.shared_data[path] = shared; |
|
d03f21729b2c
moduleapi: Remove multiple-parameters feature from module:shared()
Matthew Wild <mwild1@gmail.com>
parents:
9042
diff
changeset
|
216 return shared; |
|
4539
3cbfa768eb06
moduleapi: Add module:shared(), a way to easily share data between multiple loaded modules
Matthew Wild <mwild1@gmail.com>
parents:
4538
diff
changeset
|
217 end |
|
3cbfa768eb06
moduleapi: Add module:shared(), a way to easily share data between multiple loaded modules
Matthew Wild <mwild1@gmail.com>
parents:
4538
diff
changeset
|
218 |
|
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
219 function api:get_option(name, default_value) |
|
12972
ead41e25ebc0
core: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12921
diff
changeset
|
220 local config = require "prosody.core.configmanager"; |
|
5377
898454038524
core.*: Complete removal of all traces of the "core" section and section-related code.
Kim Alvefur <zash@zash.se>
parents:
5163
diff
changeset
|
221 local value = config.get(self.host, name); |
|
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
222 if value == nil then |
|
5377
898454038524
core.*: Complete removal of all traces of the "core" section and section-related code.
Kim Alvefur <zash@zash.se>
parents:
5163
diff
changeset
|
223 value = default_value; |
|
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
224 end |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
225 return value; |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
226 end |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
227 |
|
7975
c64ddee9d671
core.moduleapi: Factor out code for getting a scalar config option
Kim Alvefur <zash@zash.se>
parents:
7947
diff
changeset
|
228 function api:get_option_scalar(name, default_value) |
|
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
229 local value = self:get_option(name, default_value); |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
230 if type(value) == "table" then |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
231 if #value > 1 then |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
232 self:log("error", "Config option '%s' does not take a list, using just the first item", name); |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
233 end |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
234 value = value[1]; |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
235 end |
|
7975
c64ddee9d671
core.moduleapi: Factor out code for getting a scalar config option
Kim Alvefur <zash@zash.se>
parents:
7947
diff
changeset
|
236 return value; |
|
c64ddee9d671
core.moduleapi: Factor out code for getting a scalar config option
Kim Alvefur <zash@zash.se>
parents:
7947
diff
changeset
|
237 end |
|
c64ddee9d671
core.moduleapi: Factor out code for getting a scalar config option
Kim Alvefur <zash@zash.se>
parents:
7947
diff
changeset
|
238 |
|
c64ddee9d671
core.moduleapi: Factor out code for getting a scalar config option
Kim Alvefur <zash@zash.se>
parents:
7947
diff
changeset
|
239 function api:get_option_string(name, default_value) |
|
c64ddee9d671
core.moduleapi: Factor out code for getting a scalar config option
Kim Alvefur <zash@zash.se>
parents:
7947
diff
changeset
|
240 local value = self:get_option_scalar(name, default_value); |
|
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
241 if value == nil then |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
242 return nil; |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
243 end |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
244 return tostring(value); |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
245 end |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
246 |
|
13203
aa6c2692a4be
core.moduleapi: Allow specifying an acceptable range for number options
Kim Alvefur <zash@zash.se>
parents:
13201
diff
changeset
|
247 function api:get_option_number(name, default_value, min, max) |
|
aa6c2692a4be
core.moduleapi: Allow specifying an acceptable range for number options
Kim Alvefur <zash@zash.se>
parents:
13201
diff
changeset
|
248 local value = self:get_option_scalar(name, default_value); |
|
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
249 local ret = tonumber(value); |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
250 if value ~= nil and ret == nil then |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
251 self:log("error", "Config option '%s' not understood, expecting a number", name); |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
252 end |
|
13203
aa6c2692a4be
core.moduleapi: Allow specifying an acceptable range for number options
Kim Alvefur <zash@zash.se>
parents:
13201
diff
changeset
|
253 if ret == default_value then |
|
aa6c2692a4be
core.moduleapi: Allow specifying an acceptable range for number options
Kim Alvefur <zash@zash.se>
parents:
13201
diff
changeset
|
254 -- skip interval checks for default or nil |
|
aa6c2692a4be
core.moduleapi: Allow specifying an acceptable range for number options
Kim Alvefur <zash@zash.se>
parents:
13201
diff
changeset
|
255 return ret; |
|
aa6c2692a4be
core.moduleapi: Allow specifying an acceptable range for number options
Kim Alvefur <zash@zash.se>
parents:
13201
diff
changeset
|
256 end |
|
aa6c2692a4be
core.moduleapi: Allow specifying an acceptable range for number options
Kim Alvefur <zash@zash.se>
parents:
13201
diff
changeset
|
257 if min and ret < min then |
|
aa6c2692a4be
core.moduleapi: Allow specifying an acceptable range for number options
Kim Alvefur <zash@zash.se>
parents:
13201
diff
changeset
|
258 self:log("warn", "Config option '%s' out of bounds %g < %g", name, ret, min); |
|
aa6c2692a4be
core.moduleapi: Allow specifying an acceptable range for number options
Kim Alvefur <zash@zash.se>
parents:
13201
diff
changeset
|
259 return min; |
|
aa6c2692a4be
core.moduleapi: Allow specifying an acceptable range for number options
Kim Alvefur <zash@zash.se>
parents:
13201
diff
changeset
|
260 end |
|
aa6c2692a4be
core.moduleapi: Allow specifying an acceptable range for number options
Kim Alvefur <zash@zash.se>
parents:
13201
diff
changeset
|
261 if max and ret > max then |
|
aa6c2692a4be
core.moduleapi: Allow specifying an acceptable range for number options
Kim Alvefur <zash@zash.se>
parents:
13201
diff
changeset
|
262 self:log("warn", "Config option '%s' out of bounds %g > %g", name, ret, max); |
|
aa6c2692a4be
core.moduleapi: Allow specifying an acceptable range for number options
Kim Alvefur <zash@zash.se>
parents:
13201
diff
changeset
|
263 return max; |
|
aa6c2692a4be
core.moduleapi: Allow specifying an acceptable range for number options
Kim Alvefur <zash@zash.se>
parents:
13201
diff
changeset
|
264 end |
|
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
265 return ret; |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
266 end |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
267 |
|
13211
4d4f9e42bcf8
moduleapi: Add :get_option_integer()
Kim Alvefur <zash@zash.se>
parents:
13208
diff
changeset
|
268 function api:get_option_integer(name, default_value, min, max) |
|
13215
b1c2e70de470
core.moduleapi: Fix min/maxinteger fallback for Lua 5.2
Kim Alvefur <zash@zash.se>
parents:
13212
diff
changeset
|
269 local value = self:get_option_number(name, default_value, min or math.mininteger or -2 ^ 52, max or math.maxinteger or 2 ^ 53); |
|
13211
4d4f9e42bcf8
moduleapi: Add :get_option_integer()
Kim Alvefur <zash@zash.se>
parents:
13208
diff
changeset
|
270 if value == default_value then |
|
4d4f9e42bcf8
moduleapi: Add :get_option_integer()
Kim Alvefur <zash@zash.se>
parents:
13208
diff
changeset
|
271 -- pass default trough unaltered, violates ranges sometimes |
|
4d4f9e42bcf8
moduleapi: Add :get_option_integer()
Kim Alvefur <zash@zash.se>
parents:
13208
diff
changeset
|
272 return value; |
|
4d4f9e42bcf8
moduleapi: Add :get_option_integer()
Kim Alvefur <zash@zash.se>
parents:
13208
diff
changeset
|
273 end |
|
4d4f9e42bcf8
moduleapi: Add :get_option_integer()
Kim Alvefur <zash@zash.se>
parents:
13208
diff
changeset
|
274 if math.type(value) == "float" then |
|
4d4f9e42bcf8
moduleapi: Add :get_option_integer()
Kim Alvefur <zash@zash.se>
parents:
13208
diff
changeset
|
275 self:log("warn", "Config option '%s' expected an integer, not a float (%g)", name, value) |
|
4d4f9e42bcf8
moduleapi: Add :get_option_integer()
Kim Alvefur <zash@zash.se>
parents:
13208
diff
changeset
|
276 return math.floor(value); |
|
4d4f9e42bcf8
moduleapi: Add :get_option_integer()
Kim Alvefur <zash@zash.se>
parents:
13208
diff
changeset
|
277 end |
|
4d4f9e42bcf8
moduleapi: Add :get_option_integer()
Kim Alvefur <zash@zash.se>
parents:
13208
diff
changeset
|
278 -- nil or an integer |
|
4d4f9e42bcf8
moduleapi: Add :get_option_integer()
Kim Alvefur <zash@zash.se>
parents:
13208
diff
changeset
|
279 return value; |
|
4d4f9e42bcf8
moduleapi: Add :get_option_integer()
Kim Alvefur <zash@zash.se>
parents:
13208
diff
changeset
|
280 end |
|
4d4f9e42bcf8
moduleapi: Add :get_option_integer()
Kim Alvefur <zash@zash.se>
parents:
13208
diff
changeset
|
281 |
|
13212
3e6e98cc63e9
core.moduleapi: Add min/max range support to :get_option_period
Kim Alvefur <zash@zash.se>
parents:
13211
diff
changeset
|
282 function api:get_option_period(name, default_value, min, max) |
|
13204
c9ef35fab0b1
core.moduleapi: Add :get_option_period for parsing time intervals
Kim Alvefur <zash@zash.se>
parents:
13203
diff
changeset
|
283 local value = self:get_option_scalar(name, default_value); |
|
13212
3e6e98cc63e9
core.moduleapi: Add min/max range support to :get_option_period
Kim Alvefur <zash@zash.se>
parents:
13211
diff
changeset
|
284 |
|
3e6e98cc63e9
core.moduleapi: Add min/max range support to :get_option_period
Kim Alvefur <zash@zash.se>
parents:
13211
diff
changeset
|
285 local ret; |
|
3e6e98cc63e9
core.moduleapi: Add min/max range support to :get_option_period
Kim Alvefur <zash@zash.se>
parents:
13211
diff
changeset
|
286 if value == "never" or value == false then |
|
13206
7435a9341bb3
core.moduleapi: Turn negative periods or "never" into infinity
Kim Alvefur <zash@zash.se>
parents:
13205
diff
changeset
|
287 -- usually for disabling some periodic thing |
|
7435a9341bb3
core.moduleapi: Turn negative periods or "never" into infinity
Kim Alvefur <zash@zash.se>
parents:
13205
diff
changeset
|
288 return math.huge; |
|
13212
3e6e98cc63e9
core.moduleapi: Add min/max range support to :get_option_period
Kim Alvefur <zash@zash.se>
parents:
13211
diff
changeset
|
289 elseif type(value) == "number" then |
|
3e6e98cc63e9
core.moduleapi: Add min/max range support to :get_option_period
Kim Alvefur <zash@zash.se>
parents:
13211
diff
changeset
|
290 -- assume seconds |
|
3e6e98cc63e9
core.moduleapi: Add min/max range support to :get_option_period
Kim Alvefur <zash@zash.se>
parents:
13211
diff
changeset
|
291 ret = value; |
|
13205
0ccd82b965d5
core.moduleapi: Improve handling of different types in :get_option_period
Kim Alvefur <zash@zash.se>
parents:
13204
diff
changeset
|
292 elseif type(value) == "string" then |
|
13212
3e6e98cc63e9
core.moduleapi: Add min/max range support to :get_option_period
Kim Alvefur <zash@zash.se>
parents:
13211
diff
changeset
|
293 ret = human_io.parse_duration(value); |
|
13205
0ccd82b965d5
core.moduleapi: Improve handling of different types in :get_option_period
Kim Alvefur <zash@zash.se>
parents:
13204
diff
changeset
|
294 if value ~= nil and ret == nil then |
|
13368
80a1ce9974e5
moduleapi: Log error message when ambiguous period spec is found in config
Matthew Wild <mwild1@gmail.com>
parents:
13360
diff
changeset
|
295 ret = human_io.parse_duration_lax(value); |
|
80a1ce9974e5
moduleapi: Log error message when ambiguous period spec is found in config
Matthew Wild <mwild1@gmail.com>
parents:
13360
diff
changeset
|
296 if ret then |
|
80a1ce9974e5
moduleapi: Log error message when ambiguous period spec is found in config
Matthew Wild <mwild1@gmail.com>
parents:
13360
diff
changeset
|
297 local num = value:match("%d+"); |
|
80a1ce9974e5
moduleapi: Log error message when ambiguous period spec is found in config
Matthew Wild <mwild1@gmail.com>
parents:
13360
diff
changeset
|
298 self:log("error", "Config option '%s' is set to ambiguous period '%s' - use full syntax e.g. '%s months' or '%s minutes'", name, value, num, num); |
|
80a1ce9974e5
moduleapi: Log error message when ambiguous period spec is found in config
Matthew Wild <mwild1@gmail.com>
parents:
13360
diff
changeset
|
299 -- COMPAT: w/more relaxed behaviour in post-0.12 trunk. Return nil for this case too, eventually. |
|
80a1ce9974e5
moduleapi: Log error message when ambiguous period spec is found in config
Matthew Wild <mwild1@gmail.com>
parents:
13360
diff
changeset
|
300 else |
|
80a1ce9974e5
moduleapi: Log error message when ambiguous period spec is found in config
Matthew Wild <mwild1@gmail.com>
parents:
13360
diff
changeset
|
301 self:log("error", "Config option '%s' not understood, expecting a period (e.g. \"2 days\")", name); |
|
80a1ce9974e5
moduleapi: Log error message when ambiguous period spec is found in config
Matthew Wild <mwild1@gmail.com>
parents:
13360
diff
changeset
|
302 return nil; |
|
80a1ce9974e5
moduleapi: Log error message when ambiguous period spec is found in config
Matthew Wild <mwild1@gmail.com>
parents:
13360
diff
changeset
|
303 end |
|
13205
0ccd82b965d5
core.moduleapi: Improve handling of different types in :get_option_period
Kim Alvefur <zash@zash.se>
parents:
13204
diff
changeset
|
304 end |
|
13207
c563da1694bf
core.moduleapi: Log error for unexpected types (booleans?) set as periods
Kim Alvefur <zash@zash.se>
parents:
13206
diff
changeset
|
305 elseif value ~= nil then |
|
c563da1694bf
core.moduleapi: Log error for unexpected types (booleans?) set as periods
Kim Alvefur <zash@zash.se>
parents:
13206
diff
changeset
|
306 self:log("error", "Config option '%s' expects a number or a period description string (e.g. \"3 hours\"), not %s", name, type(value)); |
|
13212
3e6e98cc63e9
core.moduleapi: Add min/max range support to :get_option_period
Kim Alvefur <zash@zash.se>
parents:
13211
diff
changeset
|
307 return nil; |
|
3e6e98cc63e9
core.moduleapi: Add min/max range support to :get_option_period
Kim Alvefur <zash@zash.se>
parents:
13211
diff
changeset
|
308 else |
|
3e6e98cc63e9
core.moduleapi: Add min/max range support to :get_option_period
Kim Alvefur <zash@zash.se>
parents:
13211
diff
changeset
|
309 return nil; |
|
13204
c9ef35fab0b1
core.moduleapi: Add :get_option_period for parsing time intervals
Kim Alvefur <zash@zash.se>
parents:
13203
diff
changeset
|
310 end |
|
13212
3e6e98cc63e9
core.moduleapi: Add min/max range support to :get_option_period
Kim Alvefur <zash@zash.se>
parents:
13211
diff
changeset
|
311 |
|
3e6e98cc63e9
core.moduleapi: Add min/max range support to :get_option_period
Kim Alvefur <zash@zash.se>
parents:
13211
diff
changeset
|
312 if ret < 0 then |
|
3e6e98cc63e9
core.moduleapi: Add min/max range support to :get_option_period
Kim Alvefur <zash@zash.se>
parents:
13211
diff
changeset
|
313 self:log("debug", "Treating negative period as infinity"); |
|
3e6e98cc63e9
core.moduleapi: Add min/max range support to :get_option_period
Kim Alvefur <zash@zash.se>
parents:
13211
diff
changeset
|
314 return math.huge; |
|
3e6e98cc63e9
core.moduleapi: Add min/max range support to :get_option_period
Kim Alvefur <zash@zash.se>
parents:
13211
diff
changeset
|
315 end |
|
3e6e98cc63e9
core.moduleapi: Add min/max range support to :get_option_period
Kim Alvefur <zash@zash.se>
parents:
13211
diff
changeset
|
316 |
|
13237
59c3d775c7fa
core.moduleapi: Parse period min/max arguments
Kim Alvefur <zash@zash.se>
parents:
13215
diff
changeset
|
317 if type(min) == "string" then |
|
59c3d775c7fa
core.moduleapi: Parse period min/max arguments
Kim Alvefur <zash@zash.se>
parents:
13215
diff
changeset
|
318 min = human_io.parse_duration(min); |
|
59c3d775c7fa
core.moduleapi: Parse period min/max arguments
Kim Alvefur <zash@zash.se>
parents:
13215
diff
changeset
|
319 end |
|
13212
3e6e98cc63e9
core.moduleapi: Add min/max range support to :get_option_period
Kim Alvefur <zash@zash.se>
parents:
13211
diff
changeset
|
320 if min and ret < min then |
|
3e6e98cc63e9
core.moduleapi: Add min/max range support to :get_option_period
Kim Alvefur <zash@zash.se>
parents:
13211
diff
changeset
|
321 self:log("warn", "Config option '%s' out of bounds %g < %g", name, ret, min); |
|
3e6e98cc63e9
core.moduleapi: Add min/max range support to :get_option_period
Kim Alvefur <zash@zash.se>
parents:
13211
diff
changeset
|
322 return min; |
|
3e6e98cc63e9
core.moduleapi: Add min/max range support to :get_option_period
Kim Alvefur <zash@zash.se>
parents:
13211
diff
changeset
|
323 end |
|
13237
59c3d775c7fa
core.moduleapi: Parse period min/max arguments
Kim Alvefur <zash@zash.se>
parents:
13215
diff
changeset
|
324 if type(max) == "string" then |
|
59c3d775c7fa
core.moduleapi: Parse period min/max arguments
Kim Alvefur <zash@zash.se>
parents:
13215
diff
changeset
|
325 max = human_io.parse_duration(max); |
|
59c3d775c7fa
core.moduleapi: Parse period min/max arguments
Kim Alvefur <zash@zash.se>
parents:
13215
diff
changeset
|
326 end |
|
13212
3e6e98cc63e9
core.moduleapi: Add min/max range support to :get_option_period
Kim Alvefur <zash@zash.se>
parents:
13211
diff
changeset
|
327 if max and ret > max then |
|
3e6e98cc63e9
core.moduleapi: Add min/max range support to :get_option_period
Kim Alvefur <zash@zash.se>
parents:
13211
diff
changeset
|
328 self:log("warn", "Config option '%s' out of bounds %g > %g", name, ret, max); |
|
3e6e98cc63e9
core.moduleapi: Add min/max range support to :get_option_period
Kim Alvefur <zash@zash.se>
parents:
13211
diff
changeset
|
329 return max; |
|
3e6e98cc63e9
core.moduleapi: Add min/max range support to :get_option_period
Kim Alvefur <zash@zash.se>
parents:
13211
diff
changeset
|
330 end |
|
3e6e98cc63e9
core.moduleapi: Add min/max range support to :get_option_period
Kim Alvefur <zash@zash.se>
parents:
13211
diff
changeset
|
331 |
|
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
332 return ret; |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
333 end |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
334 |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
335 function api:get_option_boolean(name, ...) |
|
7975
c64ddee9d671
core.moduleapi: Factor out code for getting a scalar config option
Kim Alvefur <zash@zash.se>
parents:
7947
diff
changeset
|
336 local value = self:get_option_scalar(name, ...); |
|
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
337 if value == nil then |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
338 return nil; |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
339 end |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
340 local ret = value == true or value == "true" or value == 1 or nil; |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
341 if ret == nil then |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
342 ret = (value == false or value == "false" or value == 0); |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
343 if ret then |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
344 ret = false; |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
345 else |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
346 ret = nil; |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
347 end |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
348 end |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
349 if ret == nil then |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
350 self:log("error", "Config option '%s' not understood, expecting true/false", name); |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
351 end |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
352 return ret; |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
353 end |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
354 |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
355 function api:get_option_array(name, ...) |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
356 local value = self:get_option(name, ...); |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
357 |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
358 if value == nil then |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
359 return nil; |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
360 end |
|
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5771
diff
changeset
|
361 |
|
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
362 if type(value) ~= "table" then |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
363 return array{ value }; -- Assume any non-list is a single-item list |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
364 end |
|
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5771
diff
changeset
|
365 |
|
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
366 return array():append(value); -- Clone |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
367 end |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
368 |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
369 function api:get_option_set(name, ...) |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
370 local value = self:get_option_array(name, ...); |
|
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5771
diff
changeset
|
371 |
|
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
372 if value == nil then |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
373 return nil; |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
374 end |
|
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5771
diff
changeset
|
375 |
|
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
376 return set.new(value); |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
377 end |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
378 |
|
5527
7e7f45f587a1
moduleapi: Add module:get_option_inherited_set() to return a set that inherits items from a globally set set, if set
Matthew Wild <mwild1@gmail.com>
parents:
5526
diff
changeset
|
379 function api:get_option_inherited_set(name, ...) |
|
7e7f45f587a1
moduleapi: Add module:get_option_inherited_set() to return a set that inherits items from a globally set set, if set
Matthew Wild <mwild1@gmail.com>
parents:
5526
diff
changeset
|
380 local value = self:get_option_set(name, ...); |
|
7e7f45f587a1
moduleapi: Add module:get_option_inherited_set() to return a set that inherits items from a globally set set, if set
Matthew Wild <mwild1@gmail.com>
parents:
5526
diff
changeset
|
381 local global_value = self:context("*"):get_option_set(name, ...); |
|
7e7f45f587a1
moduleapi: Add module:get_option_inherited_set() to return a set that inherits items from a globally set set, if set
Matthew Wild <mwild1@gmail.com>
parents:
5526
diff
changeset
|
382 if not value then |
|
7e7f45f587a1
moduleapi: Add module:get_option_inherited_set() to return a set that inherits items from a globally set set, if set
Matthew Wild <mwild1@gmail.com>
parents:
5526
diff
changeset
|
383 return global_value; |
|
7e7f45f587a1
moduleapi: Add module:get_option_inherited_set() to return a set that inherits items from a globally set set, if set
Matthew Wild <mwild1@gmail.com>
parents:
5526
diff
changeset
|
384 elseif not global_value then |
|
7e7f45f587a1
moduleapi: Add module:get_option_inherited_set() to return a set that inherits items from a globally set set, if set
Matthew Wild <mwild1@gmail.com>
parents:
5526
diff
changeset
|
385 return value; |
|
7e7f45f587a1
moduleapi: Add module:get_option_inherited_set() to return a set that inherits items from a globally set set, if set
Matthew Wild <mwild1@gmail.com>
parents:
5526
diff
changeset
|
386 end |
|
7e7f45f587a1
moduleapi: Add module:get_option_inherited_set() to return a set that inherits items from a globally set set, if set
Matthew Wild <mwild1@gmail.com>
parents:
5526
diff
changeset
|
387 value:include(global_value); |
|
7e7f45f587a1
moduleapi: Add module:get_option_inherited_set() to return a set that inherits items from a globally set set, if set
Matthew Wild <mwild1@gmail.com>
parents:
5526
diff
changeset
|
388 return value; |
|
7e7f45f587a1
moduleapi: Add module:get_option_inherited_set() to return a set that inherits items from a globally set set, if set
Matthew Wild <mwild1@gmail.com>
parents:
5526
diff
changeset
|
389 end |
|
7e7f45f587a1
moduleapi: Add module:get_option_inherited_set() to return a set that inherits items from a globally set set, if set
Matthew Wild <mwild1@gmail.com>
parents:
5526
diff
changeset
|
390 |
|
7127
27557dd7b460
moduleapi: Add API method for getting a file path
Kim Alvefur <zash@zash.se>
parents:
7115
diff
changeset
|
391 function api:get_option_path(name, default, parent) |
|
27557dd7b460
moduleapi: Add API method for getting a file path
Kim Alvefur <zash@zash.se>
parents:
7115
diff
changeset
|
392 if parent == nil then |
|
9509
b57353f76c83
core.moduleapi: Remove redundant condition
Kim Alvefur <zash@zash.se>
parents:
9149
diff
changeset
|
393 parent = self:get_directory(); |
|
7127
27557dd7b460
moduleapi: Add API method for getting a file path
Kim Alvefur <zash@zash.se>
parents:
7115
diff
changeset
|
394 elseif prosody.paths[parent] then |
|
27557dd7b460
moduleapi: Add API method for getting a file path
Kim Alvefur <zash@zash.se>
parents:
7115
diff
changeset
|
395 parent = prosody.paths[parent]; |
|
27557dd7b460
moduleapi: Add API method for getting a file path
Kim Alvefur <zash@zash.se>
parents:
7115
diff
changeset
|
396 end |
|
27557dd7b460
moduleapi: Add API method for getting a file path
Kim Alvefur <zash@zash.se>
parents:
7115
diff
changeset
|
397 local value = self:get_option_string(name, default); |
|
27557dd7b460
moduleapi: Add API method for getting a file path
Kim Alvefur <zash@zash.se>
parents:
7115
diff
changeset
|
398 if value == nil then |
|
27557dd7b460
moduleapi: Add API method for getting a file path
Kim Alvefur <zash@zash.se>
parents:
7115
diff
changeset
|
399 return nil; |
|
27557dd7b460
moduleapi: Add API method for getting a file path
Kim Alvefur <zash@zash.se>
parents:
7115
diff
changeset
|
400 end |
|
27557dd7b460
moduleapi: Add API method for getting a file path
Kim Alvefur <zash@zash.se>
parents:
7115
diff
changeset
|
401 return resolve_relative_path(parent, value); |
|
27557dd7b460
moduleapi: Add API method for getting a file path
Kim Alvefur <zash@zash.se>
parents:
7115
diff
changeset
|
402 end |
|
27557dd7b460
moduleapi: Add API method for getting a file path
Kim Alvefur <zash@zash.se>
parents:
7115
diff
changeset
|
403 |
|
13201
65fb0d7a2312
moduleapi: Add enum config option method
Kim Alvefur <zash@zash.se>
parents:
13016
diff
changeset
|
404 function api:get_option_enum(name, default, ...) |
|
65fb0d7a2312
moduleapi: Add enum config option method
Kim Alvefur <zash@zash.se>
parents:
13016
diff
changeset
|
405 local value = self:get_option_scalar(name, default); |
|
65fb0d7a2312
moduleapi: Add enum config option method
Kim Alvefur <zash@zash.se>
parents:
13016
diff
changeset
|
406 if value == nil then return nil; end |
|
65fb0d7a2312
moduleapi: Add enum config option method
Kim Alvefur <zash@zash.se>
parents:
13016
diff
changeset
|
407 local options = set.new{default, ...}; |
|
65fb0d7a2312
moduleapi: Add enum config option method
Kim Alvefur <zash@zash.se>
parents:
13016
diff
changeset
|
408 if not options:contains(value) then |
|
65fb0d7a2312
moduleapi: Add enum config option method
Kim Alvefur <zash@zash.se>
parents:
13016
diff
changeset
|
409 self:log("error", "Config option '%s' not in set of allowed values (one of: %s)", name, options); |
|
65fb0d7a2312
moduleapi: Add enum config option method
Kim Alvefur <zash@zash.se>
parents:
13016
diff
changeset
|
410 end |
|
65fb0d7a2312
moduleapi: Add enum config option method
Kim Alvefur <zash@zash.se>
parents:
13016
diff
changeset
|
411 return value; |
|
65fb0d7a2312
moduleapi: Add enum config option method
Kim Alvefur <zash@zash.se>
parents:
13016
diff
changeset
|
412 end |
|
7127
27557dd7b460
moduleapi: Add API method for getting a file path
Kim Alvefur <zash@zash.se>
parents:
7115
diff
changeset
|
413 |
|
5526
d54011a23b20
moduleapi: Add module:context(host) to produce a fake API context for a given host (or global). module:context("*"):get_option("foo") to get global options.
Matthew Wild <mwild1@gmail.com>
parents:
5498
diff
changeset
|
414 function api:context(host) |
|
13912
a0faff6ba853
moduleapi: Fix traceback when no host passed to :context() (thanks Menel, riau)
Matthew Wild <mwild1@gmail.com>
parents:
13910
diff
changeset
|
415 local is_global; |
|
a0faff6ba853
moduleapi: Fix traceback when no host passed to :context() (thanks Menel, riau)
Matthew Wild <mwild1@gmail.com>
parents:
13910
diff
changeset
|
416 if not host or host == "*" then |
|
a0faff6ba853
moduleapi: Fix traceback when no host passed to :context() (thanks Menel, riau)
Matthew Wild <mwild1@gmail.com>
parents:
13910
diff
changeset
|
417 host, is_global = "*", true; |
|
a0faff6ba853
moduleapi: Fix traceback when no host passed to :context() (thanks Menel, riau)
Matthew Wild <mwild1@gmail.com>
parents:
13910
diff
changeset
|
418 end |
|
13909
e6c6f58dfda1
moduleapi: :context(): ensure requested host exists (fixes #605)
Matthew Wild <mwild1@gmail.com>
parents:
13719
diff
changeset
|
419 if not is_global and not hosts[host] then |
|
e6c6f58dfda1
moduleapi: :context(): ensure requested host exists (fixes #605)
Matthew Wild <mwild1@gmail.com>
parents:
13719
diff
changeset
|
420 error("Cannot create context for unknown host: "..host); |
|
e6c6f58dfda1
moduleapi: :context(): ensure requested host exists (fixes #605)
Matthew Wild <mwild1@gmail.com>
parents:
13719
diff
changeset
|
421 end |
|
13912
a0faff6ba853
moduleapi: Fix traceback when no host passed to :context() (thanks Menel, riau)
Matthew Wild <mwild1@gmail.com>
parents:
13910
diff
changeset
|
422 return setmetatable({ host = host, global = is_global }, { __index = self, __newindex = self }); |
|
5526
d54011a23b20
moduleapi: Add module:context(host) to produce a fake API context for a given host (or global). module:context("*"):get_option("foo") to get global options.
Matthew Wild <mwild1@gmail.com>
parents:
5498
diff
changeset
|
423 end |
|
d54011a23b20
moduleapi: Add module:context(host) to produce a fake API context for a given host (or global). module:context("*"):get_option("foo") to get global options.
Matthew Wild <mwild1@gmail.com>
parents:
5498
diff
changeset
|
424 |
|
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
425 function api:add_item(key, value) |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
426 self.items = self.items or {}; |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
427 self.items[key] = self.items[key] or {}; |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
428 t_insert(self.items[key], value); |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
429 self:fire_event("item-added/"..key, {source = self, item = value}); |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
430 end |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
431 function api:remove_item(key, value) |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
432 local t = self.items and self.items[key] or NULL; |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
433 for i = #t,1,-1 do |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
434 if t[i] == value then |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
435 t_remove(self.items[key], i); |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
436 self:fire_event("item-removed/"..key, {source = self, item = value}); |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
437 return value; |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
438 end |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
439 end |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
440 end |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
441 |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
442 function api:get_host_items(key) |
|
12972
ead41e25ebc0
core: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12921
diff
changeset
|
443 local modulemanager = require"prosody.core.modulemanager"; |
|
5412
a5fcda77c6b1
moduleapi: have get_host_items wrap on get_items from modulemanager, also add has_{feature/identity} to the API.
Marco Cirillo <maranda@lightwitch.org>
parents:
5377
diff
changeset
|
444 local result = modulemanager.get_items(key, self.host) or {}; |
|
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
445 return result; |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
446 end |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
447 |
|
6655
a99ed5f5f709
moduleapi: Minor variable renaming to avoid clashes with the 'type' function
Matthew Wild <mwild1@gmail.com>
parents:
6654
diff
changeset
|
448 function api:handle_items(item_type, added_cb, removed_cb, existing) |
|
a99ed5f5f709
moduleapi: Minor variable renaming to avoid clashes with the 'type' function
Matthew Wild <mwild1@gmail.com>
parents:
6654
diff
changeset
|
449 self:hook("item-added/"..item_type, added_cb); |
|
a99ed5f5f709
moduleapi: Minor variable renaming to avoid clashes with the 'type' function
Matthew Wild <mwild1@gmail.com>
parents:
6654
diff
changeset
|
450 self:hook("item-removed/"..item_type, removed_cb); |
|
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
451 if existing ~= false then |
|
13604
a4217361c1c6
core.moduleapi: Include source modules when handling items
Kim Alvefur <zash@zash.se>
parents:
13514
diff
changeset
|
452 local modulemanager = require"prosody.core.modulemanager"; |
|
a4217361c1c6
core.moduleapi: Include source modules when handling items
Kim Alvefur <zash@zash.se>
parents:
13514
diff
changeset
|
453 local modules = modulemanager.get_modules(self.host); |
|
a4217361c1c6
core.moduleapi: Include source modules when handling items
Kim Alvefur <zash@zash.se>
parents:
13514
diff
changeset
|
454 |
|
a4217361c1c6
core.moduleapi: Include source modules when handling items
Kim Alvefur <zash@zash.se>
parents:
13514
diff
changeset
|
455 for _, module in pairs(modules) do |
|
a4217361c1c6
core.moduleapi: Include source modules when handling items
Kim Alvefur <zash@zash.se>
parents:
13514
diff
changeset
|
456 local mod = module.module; |
|
a4217361c1c6
core.moduleapi: Include source modules when handling items
Kim Alvefur <zash@zash.se>
parents:
13514
diff
changeset
|
457 if mod.items and mod.items[item_type] then |
|
a4217361c1c6
core.moduleapi: Include source modules when handling items
Kim Alvefur <zash@zash.se>
parents:
13514
diff
changeset
|
458 for _, item in ipairs(mod.items[item_type]) do |
|
a4217361c1c6
core.moduleapi: Include source modules when handling items
Kim Alvefur <zash@zash.se>
parents:
13514
diff
changeset
|
459 added_cb({ source = mod; item = item }); |
|
a4217361c1c6
core.moduleapi: Include source modules when handling items
Kim Alvefur <zash@zash.se>
parents:
13514
diff
changeset
|
460 end |
|
a4217361c1c6
core.moduleapi: Include source modules when handling items
Kim Alvefur <zash@zash.se>
parents:
13514
diff
changeset
|
461 end |
|
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
462 end |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
463 end |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
464 end |
|
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
465 |
|
4613
27fbc9c69eb5
moduleapi: Add module:provides(), a shortcut to add an item with the current module's name
Matthew Wild <mwild1@gmail.com>
parents:
4605
diff
changeset
|
466 function api:provides(name, item) |
|
5529
af58eea131b4
moduleapi: module:provides called without an item makes a copy of the environment instead. Fixes warnings about non-existent globals
Kim Alvefur <zash@zash.se>
parents:
5527
diff
changeset
|
467 -- if not item then item = setmetatable({}, { __index = function(t,k) return rawget(self.environment, k); end }); end |
|
af58eea131b4
moduleapi: module:provides called without an item makes a copy of the environment instead. Fixes warnings about non-existent globals
Kim Alvefur <zash@zash.se>
parents:
5527
diff
changeset
|
468 if not item then |
|
af58eea131b4
moduleapi: module:provides called without an item makes a copy of the environment instead. Fixes warnings about non-existent globals
Kim Alvefur <zash@zash.se>
parents:
5527
diff
changeset
|
469 item = {} |
|
af58eea131b4
moduleapi: module:provides called without an item makes a copy of the environment instead. Fixes warnings about non-existent globals
Kim Alvefur <zash@zash.se>
parents:
5527
diff
changeset
|
470 for k,v in pairs(self.environment) do |
|
af58eea131b4
moduleapi: module:provides called without an item makes a copy of the environment instead. Fixes warnings about non-existent globals
Kim Alvefur <zash@zash.se>
parents:
5527
diff
changeset
|
471 if k ~= "module" then item[k] = v; end |
|
af58eea131b4
moduleapi: module:provides called without an item makes a copy of the environment instead. Fixes warnings about non-existent globals
Kim Alvefur <zash@zash.se>
parents:
5527
diff
changeset
|
472 end |
|
af58eea131b4
moduleapi: module:provides called without an item makes a copy of the environment instead. Fixes warnings about non-existent globals
Kim Alvefur <zash@zash.se>
parents:
5527
diff
changeset
|
473 end |
|
4613
27fbc9c69eb5
moduleapi: Add module:provides(), a shortcut to add an item with the current module's name
Matthew Wild <mwild1@gmail.com>
parents:
4605
diff
changeset
|
474 if not item.name then |
|
4660
96b40b5e8ea8
moduleapi: module:provides(): Fix usage of wrong table
Matthew Wild <mwild1@gmail.com>
parents:
4651
diff
changeset
|
475 local item_name = self.name; |
|
4613
27fbc9c69eb5
moduleapi: Add module:provides(), a shortcut to add an item with the current module's name
Matthew Wild <mwild1@gmail.com>
parents:
4605
diff
changeset
|
476 -- Strip a provider prefix to find the item name |
|
27fbc9c69eb5
moduleapi: Add module:provides(), a shortcut to add an item with the current module's name
Matthew Wild <mwild1@gmail.com>
parents:
4605
diff
changeset
|
477 -- (e.g. "auth_foo" -> "foo" for an auth provider) |
|
12095
c1d2bc6603ae
moduleapi: Support stripping of multi-word from module names
Matthew Wild <mwild1@gmail.com>
parents:
12002
diff
changeset
|
478 if item_name:find((name:gsub("%-", "_")).."_", 1, true) == 1 then |
|
4613
27fbc9c69eb5
moduleapi: Add module:provides(), a shortcut to add an item with the current module's name
Matthew Wild <mwild1@gmail.com>
parents:
4605
diff
changeset
|
479 item_name = item_name:sub(#name+2); |
|
27fbc9c69eb5
moduleapi: Add module:provides(), a shortcut to add an item with the current module's name
Matthew Wild <mwild1@gmail.com>
parents:
4605
diff
changeset
|
480 end |
|
27fbc9c69eb5
moduleapi: Add module:provides(), a shortcut to add an item with the current module's name
Matthew Wild <mwild1@gmail.com>
parents:
4605
diff
changeset
|
481 item.name = item_name; |
|
27fbc9c69eb5
moduleapi: Add module:provides(), a shortcut to add an item with the current module's name
Matthew Wild <mwild1@gmail.com>
parents:
4605
diff
changeset
|
482 end |
|
5530
d83482fc4a81
moduleapi: in module:provides(), add the name of the module in item._provided_by
Kim Alvefur <zash@zash.se>
parents:
5529
diff
changeset
|
483 item._provided_by = self.name; |
|
4661
76db5d0a2104
moduleapi: module:provides(): Add "-provider" onto the key name
Matthew Wild <mwild1@gmail.com>
parents:
4660
diff
changeset
|
484 self:add_item(name.."-provider", item); |
|
4613
27fbc9c69eb5
moduleapi: Add module:provides(), a shortcut to add an item with the current module's name
Matthew Wild <mwild1@gmail.com>
parents:
4605
diff
changeset
|
485 end |
|
27fbc9c69eb5
moduleapi: Add module:provides(), a shortcut to add an item with the current module's name
Matthew Wild <mwild1@gmail.com>
parents:
4605
diff
changeset
|
486 |
|
7342
79a5db780e8b
moduleapi: Allow an origin session to be passed to module:send()
Kim Alvefur <zash@zash.se>
parents:
7163
diff
changeset
|
487 function api:send(stanza, origin) |
|
79a5db780e8b
moduleapi: Allow an origin session to be passed to module:send()
Kim Alvefur <zash@zash.se>
parents:
7163
diff
changeset
|
488 return core_post_stanza(origin or hosts[self.host], stanza); |
|
4614
20940729c1b4
moduleapi: Add module:send() as an alias for core_post_stanza() from the current host's origin
Matthew Wild <mwild1@gmail.com>
parents:
4613
diff
changeset
|
489 end |
|
20940729c1b4
moduleapi: Add module:send() as an alias for core_post_stanza() from the current host's origin
Matthew Wild <mwild1@gmail.com>
parents:
4613
diff
changeset
|
490 |
|
9733
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
491 function api:send_iq(stanza, origin, timeout) |
|
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
492 local iq_cache = self._iq_cache; |
|
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
493 if not iq_cache then |
|
9747
c8240f931a68
core.moduleapi: Move util imports to top
Kim Alvefur <zash@zash.se>
parents:
9733
diff
changeset
|
494 iq_cache = cache.new(256, function (_, iq) |
|
10576
f88f1151bc72
core.moduleapi: Rename local name for util.error for consistency
Kim Alvefur <zash@zash.se>
parents:
10575
diff
changeset
|
495 iq.reject(errors.new({ |
|
9748
99199b53019f
core.moduleapi: Use util.error for :send_iq errors
Kim Alvefur <zash@zash.se>
parents:
9747
diff
changeset
|
496 type = "wait", condition = "resource-constraint", |
|
99199b53019f
core.moduleapi: Use util.error for :send_iq errors
Kim Alvefur <zash@zash.se>
parents:
9747
diff
changeset
|
497 text = "evicted from iq tracking cache" |
|
99199b53019f
core.moduleapi: Use util.error for :send_iq errors
Kim Alvefur <zash@zash.se>
parents:
9747
diff
changeset
|
498 })); |
|
9733
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
499 end); |
|
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
500 self._iq_cache = iq_cache; |
|
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
501 end |
|
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
502 |
|
10214
f864e685e618
core.moduleapi: Restructure send_iq method for more atomic cleanup
Kim Alvefur <zash@zash.se>
parents:
10213
diff
changeset
|
503 local event_type; |
|
10595
17bab303daf5
core.moduleapi: Hook correct event type in some cases
Kim Alvefur <zash@zash.se>
parents:
10576
diff
changeset
|
504 if not jid_node(stanza.attr.from) then |
|
10214
f864e685e618
core.moduleapi: Restructure send_iq method for more atomic cleanup
Kim Alvefur <zash@zash.se>
parents:
10213
diff
changeset
|
505 event_type = "host"; |
|
11822
bdabb0425d77
core.moduleapi: Enable full JID origin queries with module:send_iq()
Kim Alvefur <zash@zash.se>
parents:
11821
diff
changeset
|
506 elseif jid_resource(stanza.attr.from) then |
|
bdabb0425d77
core.moduleapi: Enable full JID origin queries with module:send_iq()
Kim Alvefur <zash@zash.se>
parents:
11821
diff
changeset
|
507 event_type = "full"; |
|
10214
f864e685e618
core.moduleapi: Restructure send_iq method for more atomic cleanup
Kim Alvefur <zash@zash.se>
parents:
10213
diff
changeset
|
508 else -- assume bare since we can't hook full jids |
|
f864e685e618
core.moduleapi: Restructure send_iq method for more atomic cleanup
Kim Alvefur <zash@zash.se>
parents:
10213
diff
changeset
|
509 event_type = "bare"; |
|
f864e685e618
core.moduleapi: Restructure send_iq method for more atomic cleanup
Kim Alvefur <zash@zash.se>
parents:
10213
diff
changeset
|
510 end |
|
f864e685e618
core.moduleapi: Restructure send_iq method for more atomic cleanup
Kim Alvefur <zash@zash.se>
parents:
10213
diff
changeset
|
511 local result_event = "iq-result/"..event_type.."/"..stanza.attr.id; |
|
f864e685e618
core.moduleapi: Restructure send_iq method for more atomic cleanup
Kim Alvefur <zash@zash.se>
parents:
10213
diff
changeset
|
512 local error_event = "iq-error/"..event_type.."/"..stanza.attr.id; |
|
f864e685e618
core.moduleapi: Restructure send_iq method for more atomic cleanup
Kim Alvefur <zash@zash.se>
parents:
10213
diff
changeset
|
513 local cache_key = event_type.."/"..stanza.attr.id; |
|
11822
bdabb0425d77
core.moduleapi: Enable full JID origin queries with module:send_iq()
Kim Alvefur <zash@zash.se>
parents:
11821
diff
changeset
|
514 if event_type == "full" then |
|
bdabb0425d77
core.moduleapi: Enable full JID origin queries with module:send_iq()
Kim Alvefur <zash@zash.se>
parents:
11821
diff
changeset
|
515 result_event = "iq/" .. event_type; |
|
bdabb0425d77
core.moduleapi: Enable full JID origin queries with module:send_iq()
Kim Alvefur <zash@zash.se>
parents:
11821
diff
changeset
|
516 error_event = "iq/" .. event_type; |
|
bdabb0425d77
core.moduleapi: Enable full JID origin queries with module:send_iq()
Kim Alvefur <zash@zash.se>
parents:
11821
diff
changeset
|
517 end |
|
10214
f864e685e618
core.moduleapi: Restructure send_iq method for more atomic cleanup
Kim Alvefur <zash@zash.se>
parents:
10213
diff
changeset
|
518 |
|
f864e685e618
core.moduleapi: Restructure send_iq method for more atomic cleanup
Kim Alvefur <zash@zash.se>
parents:
10213
diff
changeset
|
519 local p = promise.new(function (resolve, reject) |
|
9733
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
520 local function result_handler(event) |
|
11822
bdabb0425d77
core.moduleapi: Enable full JID origin queries with module:send_iq()
Kim Alvefur <zash@zash.se>
parents:
11821
diff
changeset
|
521 local response = event.stanza; |
|
bdabb0425d77
core.moduleapi: Enable full JID origin queries with module:send_iq()
Kim Alvefur <zash@zash.se>
parents:
11821
diff
changeset
|
522 if response.attr.type == "result" and response.attr.from == stanza.attr.to and response.attr.id == stanza.attr.id then |
|
9733
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
523 resolve(event); |
|
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
524 return true; |
|
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
525 end |
|
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
526 end |
|
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
527 |
|
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
528 local function error_handler(event) |
|
11822
bdabb0425d77
core.moduleapi: Enable full JID origin queries with module:send_iq()
Kim Alvefur <zash@zash.se>
parents:
11821
diff
changeset
|
529 local response = event.stanza; |
|
bdabb0425d77
core.moduleapi: Enable full JID origin queries with module:send_iq()
Kim Alvefur <zash@zash.se>
parents:
11821
diff
changeset
|
530 if response.attr.type == "error" and response.attr.from == stanza.attr.to and response.attr.id == stanza.attr.id then |
|
10576
f88f1151bc72
core.moduleapi: Rename local name for util.error for consistency
Kim Alvefur <zash@zash.se>
parents:
10575
diff
changeset
|
531 reject(errors.from_stanza(event.stanza, event)); |
|
9733
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
532 return true; |
|
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
533 end |
|
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
534 end |
|
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
535 |
|
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
536 if iq_cache:get(cache_key) then |
|
10576
f88f1151bc72
core.moduleapi: Rename local name for util.error for consistency
Kim Alvefur <zash@zash.se>
parents:
10575
diff
changeset
|
537 reject(errors.new({ |
|
9748
99199b53019f
core.moduleapi: Use util.error for :send_iq errors
Kim Alvefur <zash@zash.se>
parents:
9747
diff
changeset
|
538 type = "modify", condition = "conflict", |
|
10213
ee62754b0233
core.moduleapi: Uppercase "IQ stanza" for consistency
Kim Alvefur <zash@zash.se>
parents:
9930
diff
changeset
|
539 text = "IQ stanza id attribute already used", |
|
9748
99199b53019f
core.moduleapi: Use util.error for :send_iq errors
Kim Alvefur <zash@zash.se>
parents:
9747
diff
changeset
|
540 })); |
|
99199b53019f
core.moduleapi: Use util.error for :send_iq errors
Kim Alvefur <zash@zash.se>
parents:
9747
diff
changeset
|
541 return; |
|
9733
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
542 end |
|
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
543 |
|
11823
36a7a3137d41
core.moduleapi: Ensure module:send_iq() handler priority over mod_iq
Kim Alvefur <zash@zash.se>
parents:
11822
diff
changeset
|
544 self:hook(result_event, result_handler, 1); |
|
36a7a3137d41
core.moduleapi: Ensure module:send_iq() handler priority over mod_iq
Kim Alvefur <zash@zash.se>
parents:
11822
diff
changeset
|
545 self:hook(error_event, error_handler, 1); |
|
9733
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
546 |
|
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
547 local timeout_handle = self:add_timer(timeout or 120, function () |
|
10576
f88f1151bc72
core.moduleapi: Rename local name for util.error for consistency
Kim Alvefur <zash@zash.se>
parents:
10575
diff
changeset
|
548 reject(errors.new({ |
|
9748
99199b53019f
core.moduleapi: Use util.error for :send_iq errors
Kim Alvefur <zash@zash.se>
parents:
9747
diff
changeset
|
549 type = "wait", condition = "remote-server-timeout", |
|
99199b53019f
core.moduleapi: Use util.error for :send_iq errors
Kim Alvefur <zash@zash.se>
parents:
9747
diff
changeset
|
550 text = "IQ stanza timed out", |
|
99199b53019f
core.moduleapi: Use util.error for :send_iq errors
Kim Alvefur <zash@zash.se>
parents:
9747
diff
changeset
|
551 })); |
|
9733
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
552 end); |
|
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
553 |
|
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
554 local ok = iq_cache:set(cache_key, { |
|
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
555 reject = reject, resolve = resolve, |
|
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
556 timeout_handle = timeout_handle, |
|
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
557 result_handler = result_handler, error_handler = error_handler; |
|
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
558 }); |
|
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
559 |
|
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
560 if not ok then |
|
10576
f88f1151bc72
core.moduleapi: Rename local name for util.error for consistency
Kim Alvefur <zash@zash.se>
parents:
10575
diff
changeset
|
561 reject(errors.new({ |
|
9748
99199b53019f
core.moduleapi: Use util.error for :send_iq errors
Kim Alvefur <zash@zash.se>
parents:
9747
diff
changeset
|
562 type = "wait", condition = "internal-server-error", |
|
99199b53019f
core.moduleapi: Use util.error for :send_iq errors
Kim Alvefur <zash@zash.se>
parents:
9747
diff
changeset
|
563 text = "Could not store IQ tracking data" |
|
99199b53019f
core.moduleapi: Use util.error for :send_iq errors
Kim Alvefur <zash@zash.se>
parents:
9747
diff
changeset
|
564 })); |
|
9733
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
565 return; |
|
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
566 end |
|
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
567 |
|
10705
2cffb5ce9f7a
moduleapi: Fix handling of replies to :send_iq from internal modules
Kim Alvefur <zash@zash.se>
parents:
10595
diff
changeset
|
568 local wrapped_origin = setmetatable({ |
|
2cffb5ce9f7a
moduleapi: Fix handling of replies to :send_iq from internal modules
Kim Alvefur <zash@zash.se>
parents:
10595
diff
changeset
|
569 -- XXX Needed in some cases for replies to work correctly when sending queries internally. |
|
10706
0230ceecb8a9
moduleapi: Rename argument to silence luacheck
Kim Alvefur <zash@zash.se>
parents:
10705
diff
changeset
|
570 send = function (reply) |
|
11821
a9ad287c3388
core.moduleapi: Filter out unrelated direct replies to module:send_iq
Kim Alvefur <zash@zash.se>
parents:
11523
diff
changeset
|
571 if reply.name == stanza.name and reply.attr.id == stanza.attr.id then |
|
a9ad287c3388
core.moduleapi: Filter out unrelated direct replies to module:send_iq
Kim Alvefur <zash@zash.se>
parents:
11523
diff
changeset
|
572 resolve({ stanza = reply }); |
|
a9ad287c3388
core.moduleapi: Filter out unrelated direct replies to module:send_iq
Kim Alvefur <zash@zash.se>
parents:
11523
diff
changeset
|
573 end |
|
a9ad287c3388
core.moduleapi: Filter out unrelated direct replies to module:send_iq
Kim Alvefur <zash@zash.se>
parents:
11523
diff
changeset
|
574 return (origin or hosts[self.host]).send(reply) |
|
10705
2cffb5ce9f7a
moduleapi: Fix handling of replies to :send_iq from internal modules
Kim Alvefur <zash@zash.se>
parents:
10595
diff
changeset
|
575 end; |
|
2cffb5ce9f7a
moduleapi: Fix handling of replies to :send_iq from internal modules
Kim Alvefur <zash@zash.se>
parents:
10595
diff
changeset
|
576 }, { |
|
2cffb5ce9f7a
moduleapi: Fix handling of replies to :send_iq from internal modules
Kim Alvefur <zash@zash.se>
parents:
10595
diff
changeset
|
577 __index = origin or hosts[self.host]; |
|
2cffb5ce9f7a
moduleapi: Fix handling of replies to :send_iq from internal modules
Kim Alvefur <zash@zash.se>
parents:
10595
diff
changeset
|
578 }); |
|
2cffb5ce9f7a
moduleapi: Fix handling of replies to :send_iq from internal modules
Kim Alvefur <zash@zash.se>
parents:
10595
diff
changeset
|
579 |
|
2cffb5ce9f7a
moduleapi: Fix handling of replies to :send_iq from internal modules
Kim Alvefur <zash@zash.se>
parents:
10595
diff
changeset
|
580 self:send(stanza, wrapped_origin); |
|
9733
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
581 end); |
|
10214
f864e685e618
core.moduleapi: Restructure send_iq method for more atomic cleanup
Kim Alvefur <zash@zash.se>
parents:
10213
diff
changeset
|
582 |
|
f864e685e618
core.moduleapi: Restructure send_iq method for more atomic cleanup
Kim Alvefur <zash@zash.se>
parents:
10213
diff
changeset
|
583 p:finally(function () |
|
f864e685e618
core.moduleapi: Restructure send_iq method for more atomic cleanup
Kim Alvefur <zash@zash.se>
parents:
10213
diff
changeset
|
584 local iq = iq_cache:get(cache_key); |
|
f864e685e618
core.moduleapi: Restructure send_iq method for more atomic cleanup
Kim Alvefur <zash@zash.se>
parents:
10213
diff
changeset
|
585 if iq then |
|
f864e685e618
core.moduleapi: Restructure send_iq method for more atomic cleanup
Kim Alvefur <zash@zash.se>
parents:
10213
diff
changeset
|
586 self:unhook(result_event, iq.result_handler); |
|
f864e685e618
core.moduleapi: Restructure send_iq method for more atomic cleanup
Kim Alvefur <zash@zash.se>
parents:
10213
diff
changeset
|
587 self:unhook(error_event, iq.error_handler); |
|
f864e685e618
core.moduleapi: Restructure send_iq method for more atomic cleanup
Kim Alvefur <zash@zash.se>
parents:
10213
diff
changeset
|
588 iq.timeout_handle:stop(); |
|
f864e685e618
core.moduleapi: Restructure send_iq method for more atomic cleanup
Kim Alvefur <zash@zash.se>
parents:
10213
diff
changeset
|
589 iq_cache:set(cache_key, nil); |
|
f864e685e618
core.moduleapi: Restructure send_iq method for more atomic cleanup
Kim Alvefur <zash@zash.se>
parents:
10213
diff
changeset
|
590 end |
|
f864e685e618
core.moduleapi: Restructure send_iq method for more atomic cleanup
Kim Alvefur <zash@zash.se>
parents:
10213
diff
changeset
|
591 end); |
|
f864e685e618
core.moduleapi: Restructure send_iq method for more atomic cleanup
Kim Alvefur <zash@zash.se>
parents:
10213
diff
changeset
|
592 |
|
f864e685e618
core.moduleapi: Restructure send_iq method for more atomic cleanup
Kim Alvefur <zash@zash.se>
parents:
10213
diff
changeset
|
593 return p; |
|
9733
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
594 end |
|
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
595 |
|
6651
deaa3d66dc2c
moduleapi: Add module:broadcast() to send a stanza to a list of JIDs
Matthew Wild <mwild1@gmail.com>
parents:
6640
diff
changeset
|
596 function api:broadcast(jids, stanza, iter) |
|
deaa3d66dc2c
moduleapi: Add module:broadcast() to send a stanza to a list of JIDs
Matthew Wild <mwild1@gmail.com>
parents:
6640
diff
changeset
|
597 for jid in (iter or it.values)(jids) do |
|
deaa3d66dc2c
moduleapi: Add module:broadcast() to send a stanza to a list of JIDs
Matthew Wild <mwild1@gmail.com>
parents:
6640
diff
changeset
|
598 local new_stanza = st.clone(stanza); |
|
deaa3d66dc2c
moduleapi: Add module:broadcast() to send a stanza to a list of JIDs
Matthew Wild <mwild1@gmail.com>
parents:
6640
diff
changeset
|
599 new_stanza.attr.to = jid; |
|
9042
734ba7080b35
moduleapi: Use :send API from :broadcast for compactness
Kim Alvefur <zash@zash.se>
parents:
8994
diff
changeset
|
600 self:send(new_stanza); |
|
6651
deaa3d66dc2c
moduleapi: Add module:broadcast() to send a stanza to a list of JIDs
Matthew Wild <mwild1@gmail.com>
parents:
6640
diff
changeset
|
601 end |
|
deaa3d66dc2c
moduleapi: Add module:broadcast() to send a stanza to a list of JIDs
Matthew Wild <mwild1@gmail.com>
parents:
6640
diff
changeset
|
602 end |
|
deaa3d66dc2c
moduleapi: Add module:broadcast() to send a stanza to a list of JIDs
Matthew Wild <mwild1@gmail.com>
parents:
6640
diff
changeset
|
603 |
|
5899
26f54b462601
core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents:
5825
diff
changeset
|
604 local timer_methods = { } |
|
26f54b462601
core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents:
5825
diff
changeset
|
605 local timer_mt = { |
|
26f54b462601
core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents:
5825
diff
changeset
|
606 __index = timer_methods; |
|
26f54b462601
core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents:
5825
diff
changeset
|
607 } |
|
26f54b462601
core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents:
5825
diff
changeset
|
608 function timer_methods:stop( ) |
|
26f54b462601
core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents:
5825
diff
changeset
|
609 timer.stop(self.id); |
|
26f54b462601
core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents:
5825
diff
changeset
|
610 end |
|
26f54b462601
core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents:
5825
diff
changeset
|
611 timer_methods.disarm = timer_methods.stop |
|
26f54b462601
core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents:
5825
diff
changeset
|
612 function timer_methods:reschedule(delay) |
|
26f54b462601
core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents:
5825
diff
changeset
|
613 timer.reschedule(self.id, delay) |
|
26f54b462601
core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents:
5825
diff
changeset
|
614 end |
|
26f54b462601
core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents:
5825
diff
changeset
|
615 |
|
7142
67226eaef97c
moduleapi: Silence luacheck warning about unused 'id' parameter
Matthew Wild <mwild1@gmail.com>
parents:
7141
diff
changeset
|
616 local function timer_callback(now, id, t) --luacheck: ignore 212/id |
|
5899
26f54b462601
core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents:
5825
diff
changeset
|
617 if t.module_env.loaded == false then return; end |
|
26f54b462601
core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents:
5825
diff
changeset
|
618 return t.callback(now, unpack(t, 1, t.n)); |
|
26f54b462601
core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents:
5825
diff
changeset
|
619 end |
|
26f54b462601
core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents:
5825
diff
changeset
|
620 |
|
26f54b462601
core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents:
5825
diff
changeset
|
621 function api:add_timer(delay, callback, ...) |
|
26f54b462601
core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents:
5825
diff
changeset
|
622 local t = pack(...) |
|
26f54b462601
core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents:
5825
diff
changeset
|
623 t.module_env = self; |
|
26f54b462601
core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents:
5825
diff
changeset
|
624 t.callback = callback; |
|
26f54b462601
core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents:
5825
diff
changeset
|
625 t.id = timer.add_task(delay, timer_callback, t); |
|
26f54b462601
core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents:
5825
diff
changeset
|
626 return setmetatable(t, timer_mt); |
|
4666
fb522fbd495e
moduleapi: Add module:add_timer(delay, callback) - automatically halts the timer on module unload
Matthew Wild <mwild1@gmail.com>
parents:
4663
diff
changeset
|
627 end |
|
fb522fbd495e
moduleapi: Add module:add_timer(delay, callback) - automatically halts the timer on module unload
Matthew Wild <mwild1@gmail.com>
parents:
4663
diff
changeset
|
628 |
|
11987
4b519c575ad0
core.moduleapi: Add API for adding daily or hourly tasks via mod_cron
Kim Alvefur <zash@zash.se>
parents:
11931
diff
changeset
|
629 function api:cron(task_spec) |
|
4b519c575ad0
core.moduleapi: Add API for adding daily or hourly tasks via mod_cron
Kim Alvefur <zash@zash.se>
parents:
11931
diff
changeset
|
630 self:depends("cron"); |
|
4b519c575ad0
core.moduleapi: Add API for adding daily or hourly tasks via mod_cron
Kim Alvefur <zash@zash.se>
parents:
11931
diff
changeset
|
631 self:add_item("task", task_spec); |
|
4b519c575ad0
core.moduleapi: Add API for adding daily or hourly tasks via mod_cron
Kim Alvefur <zash@zash.se>
parents:
11931
diff
changeset
|
632 end |
|
4b519c575ad0
core.moduleapi: Add API for adding daily or hourly tasks via mod_cron
Kim Alvefur <zash@zash.se>
parents:
11931
diff
changeset
|
633 |
|
4b519c575ad0
core.moduleapi: Add API for adding daily or hourly tasks via mod_cron
Kim Alvefur <zash@zash.se>
parents:
11931
diff
changeset
|
634 function api:hourly(name, fun) |
|
4b519c575ad0
core.moduleapi: Add API for adding daily or hourly tasks via mod_cron
Kim Alvefur <zash@zash.se>
parents:
11931
diff
changeset
|
635 if type(name) == "function" then fun, name = name, nil; end |
|
4b519c575ad0
core.moduleapi: Add API for adding daily or hourly tasks via mod_cron
Kim Alvefur <zash@zash.se>
parents:
11931
diff
changeset
|
636 self:cron({ name = name; when = "hourly"; run = fun }); |
|
4b519c575ad0
core.moduleapi: Add API for adding daily or hourly tasks via mod_cron
Kim Alvefur <zash@zash.se>
parents:
11931
diff
changeset
|
637 end |
|
4b519c575ad0
core.moduleapi: Add API for adding daily or hourly tasks via mod_cron
Kim Alvefur <zash@zash.se>
parents:
11931
diff
changeset
|
638 |
|
4b519c575ad0
core.moduleapi: Add API for adding daily or hourly tasks via mod_cron
Kim Alvefur <zash@zash.se>
parents:
11931
diff
changeset
|
639 function api:daily(name, fun) |
|
4b519c575ad0
core.moduleapi: Add API for adding daily or hourly tasks via mod_cron
Kim Alvefur <zash@zash.se>
parents:
11931
diff
changeset
|
640 if type(name) == "function" then fun, name = name, nil; end |
|
4b519c575ad0
core.moduleapi: Add API for adding daily or hourly tasks via mod_cron
Kim Alvefur <zash@zash.se>
parents:
11931
diff
changeset
|
641 self:cron({ name = name; when = "daily"; run = fun }); |
|
4b519c575ad0
core.moduleapi: Add API for adding daily or hourly tasks via mod_cron
Kim Alvefur <zash@zash.se>
parents:
11931
diff
changeset
|
642 end |
|
4b519c575ad0
core.moduleapi: Add API for adding daily or hourly tasks via mod_cron
Kim Alvefur <zash@zash.se>
parents:
11931
diff
changeset
|
643 |
|
12002
cbed7d8d8f35
mod_cron: Add a 'weekly' job frequency
Kim Alvefur <zash@zash.se>
parents:
11987
diff
changeset
|
644 function api:weekly(name, fun) |
|
cbed7d8d8f35
mod_cron: Add a 'weekly' job frequency
Kim Alvefur <zash@zash.se>
parents:
11987
diff
changeset
|
645 if type(name) == "function" then fun, name = name, nil; end |
|
cbed7d8d8f35
mod_cron: Add a 'weekly' job frequency
Kim Alvefur <zash@zash.se>
parents:
11987
diff
changeset
|
646 self:cron({ name = name; when = "weekly"; run = fun }); |
|
cbed7d8d8f35
mod_cron: Add a 'weekly' job frequency
Kim Alvefur <zash@zash.se>
parents:
11987
diff
changeset
|
647 end |
|
cbed7d8d8f35
mod_cron: Add a 'weekly' job frequency
Kim Alvefur <zash@zash.se>
parents:
11987
diff
changeset
|
648 |
|
4790
8bf710b19f65
moduleapi: Add module:get_directory() to get module file's directory, and module:load_resource() to load a file relative to that directory
Matthew Wild <mwild1@gmail.com>
parents:
4750
diff
changeset
|
649 local path_sep = package.config:sub(1,1); |
|
8bf710b19f65
moduleapi: Add module:get_directory() to get module file's directory, and module:load_resource() to load a file relative to that directory
Matthew Wild <mwild1@gmail.com>
parents:
4750
diff
changeset
|
650 function api:get_directory() |
|
11148
1dc49accb58e
core.moduleapi: Return resource path from module:get_directory() (API BC)
Kim Alvefur <zash@zash.se>
parents:
11146
diff
changeset
|
651 return self.resource_path or self.path and (self.path:gsub("%"..path_sep.."[^"..path_sep.."]*$", "")) or nil; |
|
4790
8bf710b19f65
moduleapi: Add module:get_directory() to get module file's directory, and module:load_resource() to load a file relative to that directory
Matthew Wild <mwild1@gmail.com>
parents:
4750
diff
changeset
|
652 end |
|
8bf710b19f65
moduleapi: Add module:get_directory() to get module file's directory, and module:load_resource() to load a file relative to that directory
Matthew Wild <mwild1@gmail.com>
parents:
4750
diff
changeset
|
653 |
|
8bf710b19f65
moduleapi: Add module:get_directory() to get module file's directory, and module:load_resource() to load a file relative to that directory
Matthew Wild <mwild1@gmail.com>
parents:
4750
diff
changeset
|
654 function api:load_resource(path, mode) |
|
11148
1dc49accb58e
core.moduleapi: Return resource path from module:get_directory() (API BC)
Kim Alvefur <zash@zash.se>
parents:
11146
diff
changeset
|
655 path = resolve_relative_path(self:get_directory(), path); |
|
4790
8bf710b19f65
moduleapi: Add module:get_directory() to get module file's directory, and module:load_resource() to load a file relative to that directory
Matthew Wild <mwild1@gmail.com>
parents:
4750
diff
changeset
|
656 return io.open(path, mode); |
|
8bf710b19f65
moduleapi: Add module:get_directory() to get module file's directory, and module:load_resource() to load a file relative to that directory
Matthew Wild <mwild1@gmail.com>
parents:
4750
diff
changeset
|
657 end |
|
8bf710b19f65
moduleapi: Add module:get_directory() to get module file's directory, and module:load_resource() to load a file relative to that directory
Matthew Wild <mwild1@gmail.com>
parents:
4750
diff
changeset
|
658 |
|
6655
a99ed5f5f709
moduleapi: Minor variable renaming to avoid clashes with the 'type' function
Matthew Wild <mwild1@gmail.com>
parents:
6654
diff
changeset
|
659 function api:open_store(name, store_type) |
|
12662
07424992d7fc
mod_authz_internal, and more: New iteration of role API
Matthew Wild <mwild1@gmail.com>
parents:
12652
diff
changeset
|
660 if self.host == "*" then return nil, "global-storage-not-supported"; end |
|
12972
ead41e25ebc0
core: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12921
diff
changeset
|
661 return require"prosody.core.storagemanager".open(self.host, name or self.name, store_type); |
|
5496
7a0b81b5ca71
moduleapi: Add module:open_store() as a front-end to storagemanager.open()
Matthew Wild <mwild1@gmail.com>
parents:
5434
diff
changeset
|
662 end |
|
7a0b81b5ca71
moduleapi: Add module:open_store() as a front-end to storagemanager.open()
Matthew Wild <mwild1@gmail.com>
parents:
5434
diff
changeset
|
663 |
|
10885
2f751880767c
core.moduleapi: Allow passing a config table trough :measure
Kim Alvefur <zash@zash.se>
parents:
10706
diff
changeset
|
664 function api:measure(name, stat_type, conf) |
|
12972
ead41e25ebc0
core: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12921
diff
changeset
|
665 local measure = require "prosody.core.statsmanager".measure; |
|
11523
5f15ab7c6ae5
Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents:
11148
diff
changeset
|
666 local fixed_label_key, fixed_label_value |
|
5f15ab7c6ae5
Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents:
11148
diff
changeset
|
667 if self.host ~= "*" then |
|
5f15ab7c6ae5
Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents:
11148
diff
changeset
|
668 fixed_label_key = "host" |
|
5f15ab7c6ae5
Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents:
11148
diff
changeset
|
669 fixed_label_value = self.host |
|
5f15ab7c6ae5
Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents:
11148
diff
changeset
|
670 end |
|
5f15ab7c6ae5
Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents:
11148
diff
changeset
|
671 -- new_legacy_metric takes care of scoping for us, as it does not accept |
|
5f15ab7c6ae5
Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents:
11148
diff
changeset
|
672 -- an array of labels |
|
5f15ab7c6ae5
Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents:
11148
diff
changeset
|
673 -- the prosody_ prefix is automatically added by statsmanager for legacy |
|
5f15ab7c6ae5
Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents:
11148
diff
changeset
|
674 -- metrics. |
|
11931
c65d5da8e99a
mod_admin_shell: List collected metrics in module:info
Kim Alvefur <zash@zash.se>
parents:
11921
diff
changeset
|
675 self:add_item("measure", { name = name, type = stat_type, conf = conf }); |
|
11523
5f15ab7c6ae5
Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents:
11148
diff
changeset
|
676 return measure(stat_type, "mod_"..self.name.."/"..name, conf, fixed_label_key, fixed_label_value) |
|
6556
74253c7beb9c
moduleapi: Module API for statsmanager
Matthew Wild <mwild1@gmail.com>
parents:
6422
diff
changeset
|
677 end |
|
74253c7beb9c
moduleapi: Module API for statsmanager
Matthew Wild <mwild1@gmail.com>
parents:
6422
diff
changeset
|
678 |
|
11523
5f15ab7c6ae5
Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents:
11148
diff
changeset
|
679 function api:metric(type_, name, unit, description, label_keys, conf) |
|
12972
ead41e25ebc0
core: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12921
diff
changeset
|
680 local metric = require "prosody.core.statsmanager".metric; |
|
11523
5f15ab7c6ae5
Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents:
11148
diff
changeset
|
681 local is_scoped = self.host ~= "*" |
|
13513
2fb79fe54390
core.moduleapi: Default labels to empty list to fix error if omitted
Kim Alvefur <zash@zash.se>
parents:
12479
diff
changeset
|
682 label_keys = label_keys or {}; |
|
11523
5f15ab7c6ae5
Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents:
11148
diff
changeset
|
683 if is_scoped then |
|
5f15ab7c6ae5
Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents:
11148
diff
changeset
|
684 -- prepend `host` label to label keys if this is not a global module |
|
5f15ab7c6ae5
Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents:
11148
diff
changeset
|
685 local orig_labels = label_keys |
|
5f15ab7c6ae5
Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents:
11148
diff
changeset
|
686 label_keys = array { "host" } |
|
5f15ab7c6ae5
Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents:
11148
diff
changeset
|
687 label_keys:append(orig_labels) |
|
6639
3003d041c159
moduleapi: Experimental API for modules to measure the rate+duration of events
Matthew Wild <mwild1@gmail.com>
parents:
6556
diff
changeset
|
688 end |
|
11523
5f15ab7c6ae5
Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents:
11148
diff
changeset
|
689 local mf = metric(type_, "prosody_mod_"..self.name.."/"..name, unit, description, label_keys, conf) |
|
11931
c65d5da8e99a
mod_admin_shell: List collected metrics in module:info
Kim Alvefur <zash@zash.se>
parents:
11921
diff
changeset
|
690 self:add_item("metric", { name = name, mf = mf }); |
|
11523
5f15ab7c6ae5
Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents:
11148
diff
changeset
|
691 if is_scoped then |
|
5f15ab7c6ae5
Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents:
11148
diff
changeset
|
692 -- make sure to scope the returned metric family to the current host |
|
5f15ab7c6ae5
Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents:
11148
diff
changeset
|
693 return mf:with_partial_label(self.host) |
|
5f15ab7c6ae5
Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents:
11148
diff
changeset
|
694 end |
|
5f15ab7c6ae5
Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents:
11148
diff
changeset
|
695 return mf |
|
6639
3003d041c159
moduleapi: Experimental API for modules to measure the rate+duration of events
Matthew Wild <mwild1@gmail.com>
parents:
6556
diff
changeset
|
696 end |
|
3003d041c159
moduleapi: Experimental API for modules to measure the rate+duration of events
Matthew Wild <mwild1@gmail.com>
parents:
6556
diff
changeset
|
697 |
|
9866
09cc8c856e5e
moduleapi: New API for modules to set a status
Matthew Wild <mwild1@gmail.com>
parents:
9750
diff
changeset
|
698 local status_priorities = { error = 3, warn = 2, info = 1, core = 0 }; |
|
09cc8c856e5e
moduleapi: New API for modules to set a status
Matthew Wild <mwild1@gmail.com>
parents:
9750
diff
changeset
|
699 |
|
09cc8c856e5e
moduleapi: New API for modules to set a status
Matthew Wild <mwild1@gmail.com>
parents:
9750
diff
changeset
|
700 function api:set_status(status_type, status_message, override) |
|
09cc8c856e5e
moduleapi: New API for modules to set a status
Matthew Wild <mwild1@gmail.com>
parents:
9750
diff
changeset
|
701 local priority = status_priorities[status_type]; |
|
09cc8c856e5e
moduleapi: New API for modules to set a status
Matthew Wild <mwild1@gmail.com>
parents:
9750
diff
changeset
|
702 if not priority then |
|
12874
b9468c8ac1d3
core.moduleapi: Fix passing variable to logging
Kim Alvefur <zash@zash.se>
parents:
12690
diff
changeset
|
703 self:log("error", "set_status: Invalid status type '%s', assuming 'info'", status_type); |
|
9866
09cc8c856e5e
moduleapi: New API for modules to set a status
Matthew Wild <mwild1@gmail.com>
parents:
9750
diff
changeset
|
704 status_type, priority = "info", status_priorities.info; |
|
09cc8c856e5e
moduleapi: New API for modules to set a status
Matthew Wild <mwild1@gmail.com>
parents:
9750
diff
changeset
|
705 end |
|
09cc8c856e5e
moduleapi: New API for modules to set a status
Matthew Wild <mwild1@gmail.com>
parents:
9750
diff
changeset
|
706 local current_priority = status_priorities[self.status_type] or 0; |
|
09cc8c856e5e
moduleapi: New API for modules to set a status
Matthew Wild <mwild1@gmail.com>
parents:
9750
diff
changeset
|
707 -- By default an 'error' status can only be overwritten by another 'error' status |
|
09cc8c856e5e
moduleapi: New API for modules to set a status
Matthew Wild <mwild1@gmail.com>
parents:
9750
diff
changeset
|
708 if (current_priority >= status_priorities.error and priority < current_priority and override ~= true) |
|
09cc8c856e5e
moduleapi: New API for modules to set a status
Matthew Wild <mwild1@gmail.com>
parents:
9750
diff
changeset
|
709 or (override == false and current_priority > priority) then |
|
9930
12a31296d63d
moduleapi: Log suppressed status priority and message when not overriding
Matthew Wild <mwild1@gmail.com>
parents:
9929
diff
changeset
|
710 self:log("debug", "moduleapi: ignoring status [prio %d override %s]: %s", priority, override, status_message); |
|
9866
09cc8c856e5e
moduleapi: New API for modules to set a status
Matthew Wild <mwild1@gmail.com>
parents:
9750
diff
changeset
|
711 return; |
|
09cc8c856e5e
moduleapi: New API for modules to set a status
Matthew Wild <mwild1@gmail.com>
parents:
9750
diff
changeset
|
712 end |
|
09cc8c856e5e
moduleapi: New API for modules to set a status
Matthew Wild <mwild1@gmail.com>
parents:
9750
diff
changeset
|
713 self.status_type, self.status_message, self.status_time = status_type, status_message, time_now(); |
|
09cc8c856e5e
moduleapi: New API for modules to set a status
Matthew Wild <mwild1@gmail.com>
parents:
9750
diff
changeset
|
714 self:fire_event("module-status/updated", { name = self.name }); |
|
09cc8c856e5e
moduleapi: New API for modules to set a status
Matthew Wild <mwild1@gmail.com>
parents:
9750
diff
changeset
|
715 end |
|
09cc8c856e5e
moduleapi: New API for modules to set a status
Matthew Wild <mwild1@gmail.com>
parents:
9750
diff
changeset
|
716 |
|
09cc8c856e5e
moduleapi: New API for modules to set a status
Matthew Wild <mwild1@gmail.com>
parents:
9750
diff
changeset
|
717 function api:log_status(level, msg, ...) |
|
09cc8c856e5e
moduleapi: New API for modules to set a status
Matthew Wild <mwild1@gmail.com>
parents:
9750
diff
changeset
|
718 self:set_status(level, format(msg, ...)); |
|
09cc8c856e5e
moduleapi: New API for modules to set a status
Matthew Wild <mwild1@gmail.com>
parents:
9750
diff
changeset
|
719 return self:log(level, msg, ...); |
|
09cc8c856e5e
moduleapi: New API for modules to set a status
Matthew Wild <mwild1@gmail.com>
parents:
9750
diff
changeset
|
720 end |
|
09cc8c856e5e
moduleapi: New API for modules to set a status
Matthew Wild <mwild1@gmail.com>
parents:
9750
diff
changeset
|
721 |
|
09cc8c856e5e
moduleapi: New API for modules to set a status
Matthew Wild <mwild1@gmail.com>
parents:
9750
diff
changeset
|
722 function api:get_status() |
|
09cc8c856e5e
moduleapi: New API for modules to set a status
Matthew Wild <mwild1@gmail.com>
parents:
9750
diff
changeset
|
723 return self.status_type, self.status_message, self.status_time; |
|
09cc8c856e5e
moduleapi: New API for modules to set a status
Matthew Wild <mwild1@gmail.com>
parents:
9750
diff
changeset
|
724 end |
|
09cc8c856e5e
moduleapi: New API for modules to set a status
Matthew Wild <mwild1@gmail.com>
parents:
9750
diff
changeset
|
725 |
|
12642
9061f9621330
Switch to a new role-based authorization framework, removing is_admin()
Matthew Wild <mwild1@gmail.com>
parents:
12590
diff
changeset
|
726 function api:default_permission(role_name, permission) |
|
9061f9621330
Switch to a new role-based authorization framework, removing is_admin()
Matthew Wild <mwild1@gmail.com>
parents:
12590
diff
changeset
|
727 permission = permission:gsub("^:", self.name..":"); |
|
12650
e08bf2ad67da
moduleapi: Distribute permissions set from global modules to all hosts
Kim Alvefur <zash@zash.se>
parents:
12645
diff
changeset
|
728 if self.host == "*" then |
|
e08bf2ad67da
moduleapi: Distribute permissions set from global modules to all hosts
Kim Alvefur <zash@zash.se>
parents:
12645
diff
changeset
|
729 for _, host in pairs(hosts) do |
|
e08bf2ad67da
moduleapi: Distribute permissions set from global modules to all hosts
Kim Alvefur <zash@zash.se>
parents:
12645
diff
changeset
|
730 if host.authz then |
|
e08bf2ad67da
moduleapi: Distribute permissions set from global modules to all hosts
Kim Alvefur <zash@zash.se>
parents:
12645
diff
changeset
|
731 host.authz.add_default_permission(role_name, permission); |
|
e08bf2ad67da
moduleapi: Distribute permissions set from global modules to all hosts
Kim Alvefur <zash@zash.se>
parents:
12645
diff
changeset
|
732 end |
|
e08bf2ad67da
moduleapi: Distribute permissions set from global modules to all hosts
Kim Alvefur <zash@zash.se>
parents:
12645
diff
changeset
|
733 end |
|
e08bf2ad67da
moduleapi: Distribute permissions set from global modules to all hosts
Kim Alvefur <zash@zash.se>
parents:
12645
diff
changeset
|
734 return |
|
e08bf2ad67da
moduleapi: Distribute permissions set from global modules to all hosts
Kim Alvefur <zash@zash.se>
parents:
12645
diff
changeset
|
735 end |
|
12642
9061f9621330
Switch to a new role-based authorization framework, removing is_admin()
Matthew Wild <mwild1@gmail.com>
parents:
12590
diff
changeset
|
736 hosts[self.host].authz.add_default_permission(role_name, permission); |
|
9061f9621330
Switch to a new role-based authorization framework, removing is_admin()
Matthew Wild <mwild1@gmail.com>
parents:
12590
diff
changeset
|
737 end |
|
9061f9621330
Switch to a new role-based authorization framework, removing is_admin()
Matthew Wild <mwild1@gmail.com>
parents:
12590
diff
changeset
|
738 |
|
9061f9621330
Switch to a new role-based authorization framework, removing is_admin()
Matthew Wild <mwild1@gmail.com>
parents:
12590
diff
changeset
|
739 function api:default_permissions(role_name, permissions) |
|
9061f9621330
Switch to a new role-based authorization framework, removing is_admin()
Matthew Wild <mwild1@gmail.com>
parents:
12590
diff
changeset
|
740 for _, permission in ipairs(permissions) do |
|
9061f9621330
Switch to a new role-based authorization framework, removing is_admin()
Matthew Wild <mwild1@gmail.com>
parents:
12590
diff
changeset
|
741 self:default_permission(role_name, permission); |
|
9061f9621330
Switch to a new role-based authorization framework, removing is_admin()
Matthew Wild <mwild1@gmail.com>
parents:
12590
diff
changeset
|
742 end |
|
9061f9621330
Switch to a new role-based authorization framework, removing is_admin()
Matthew Wild <mwild1@gmail.com>
parents:
12590
diff
changeset
|
743 end |
|
9061f9621330
Switch to a new role-based authorization framework, removing is_admin()
Matthew Wild <mwild1@gmail.com>
parents:
12590
diff
changeset
|
744 |
|
12995
e385f3a06673
moduleapi: Add 'peek' to :may() and new :could() helper to suppress logging
Matthew Wild <mwild1@gmail.com>
parents:
12994
diff
changeset
|
745 function api:could(action, context) |
|
e385f3a06673
moduleapi: Add 'peek' to :may() and new :could() helper to suppress logging
Matthew Wild <mwild1@gmail.com>
parents:
12994
diff
changeset
|
746 return self:may(action, context, true); |
|
e385f3a06673
moduleapi: Add 'peek' to :may() and new :could() helper to suppress logging
Matthew Wild <mwild1@gmail.com>
parents:
12994
diff
changeset
|
747 end |
|
e385f3a06673
moduleapi: Add 'peek' to :may() and new :could() helper to suppress logging
Matthew Wild <mwild1@gmail.com>
parents:
12994
diff
changeset
|
748 |
|
e385f3a06673
moduleapi: Add 'peek' to :may() and new :could() helper to suppress logging
Matthew Wild <mwild1@gmail.com>
parents:
12994
diff
changeset
|
749 function api:may(action, context, peek) |
|
12645
a741183eec97
core.moduleapi: Expand permission name ':' prefix earlier
Kim Alvefur <zash@zash.se>
parents:
12644
diff
changeset
|
750 if action:byte(1) == 58 then -- action begins with ':' |
|
a741183eec97
core.moduleapi: Expand permission name ':' prefix earlier
Kim Alvefur <zash@zash.se>
parents:
12644
diff
changeset
|
751 action = self.name..action; -- prepend module name |
|
a741183eec97
core.moduleapi: Expand permission name ':' prefix earlier
Kim Alvefur <zash@zash.se>
parents:
12644
diff
changeset
|
752 end |
|
13309
113ce2ac73a2
moduleapi: may(): Support explicit actor_jid in context object
Matthew Wild <mwild1@gmail.com>
parents:
13237
diff
changeset
|
753 |
|
113ce2ac73a2
moduleapi: may(): Support explicit actor_jid in context object
Matthew Wild <mwild1@gmail.com>
parents:
13237
diff
changeset
|
754 do |
|
113ce2ac73a2
moduleapi: may(): Support explicit actor_jid in context object
Matthew Wild <mwild1@gmail.com>
parents:
13237
diff
changeset
|
755 -- JID-based actor |
|
113ce2ac73a2
moduleapi: may(): Support explicit actor_jid in context object
Matthew Wild <mwild1@gmail.com>
parents:
13237
diff
changeset
|
756 local actor_jid = type(context) == "string" and context or context.actor_jid; |
|
113ce2ac73a2
moduleapi: may(): Support explicit actor_jid in context object
Matthew Wild <mwild1@gmail.com>
parents:
13237
diff
changeset
|
757 if actor_jid then -- check JID permissions |
|
113ce2ac73a2
moduleapi: may(): Support explicit actor_jid in context object
Matthew Wild <mwild1@gmail.com>
parents:
13237
diff
changeset
|
758 local role; |
|
113ce2ac73a2
moduleapi: may(): Support explicit actor_jid in context object
Matthew Wild <mwild1@gmail.com>
parents:
13237
diff
changeset
|
759 local node, host = jid_split(actor_jid); |
|
113ce2ac73a2
moduleapi: may(): Support explicit actor_jid in context object
Matthew Wild <mwild1@gmail.com>
parents:
13237
diff
changeset
|
760 if host == self.host then |
|
113ce2ac73a2
moduleapi: may(): Support explicit actor_jid in context object
Matthew Wild <mwild1@gmail.com>
parents:
13237
diff
changeset
|
761 role = hosts[host].authz.get_user_role(node); |
|
113ce2ac73a2
moduleapi: may(): Support explicit actor_jid in context object
Matthew Wild <mwild1@gmail.com>
parents:
13237
diff
changeset
|
762 else |
|
113ce2ac73a2
moduleapi: may(): Support explicit actor_jid in context object
Matthew Wild <mwild1@gmail.com>
parents:
13237
diff
changeset
|
763 role = hosts[self.host].authz.get_jid_role(actor_jid); |
|
113ce2ac73a2
moduleapi: may(): Support explicit actor_jid in context object
Matthew Wild <mwild1@gmail.com>
parents:
13237
diff
changeset
|
764 end |
|
113ce2ac73a2
moduleapi: may(): Support explicit actor_jid in context object
Matthew Wild <mwild1@gmail.com>
parents:
13237
diff
changeset
|
765 if not role then |
|
113ce2ac73a2
moduleapi: may(): Support explicit actor_jid in context object
Matthew Wild <mwild1@gmail.com>
parents:
13237
diff
changeset
|
766 if not peek then |
|
113ce2ac73a2
moduleapi: may(): Support explicit actor_jid in context object
Matthew Wild <mwild1@gmail.com>
parents:
13237
diff
changeset
|
767 self:log("debug", "Access denied: JID <%s> may not %s (no role found)", actor_jid, action); |
|
113ce2ac73a2
moduleapi: may(): Support explicit actor_jid in context object
Matthew Wild <mwild1@gmail.com>
parents:
13237
diff
changeset
|
768 end |
|
113ce2ac73a2
moduleapi: may(): Support explicit actor_jid in context object
Matthew Wild <mwild1@gmail.com>
parents:
13237
diff
changeset
|
769 return false; |
|
113ce2ac73a2
moduleapi: may(): Support explicit actor_jid in context object
Matthew Wild <mwild1@gmail.com>
parents:
13237
diff
changeset
|
770 end |
|
113ce2ac73a2
moduleapi: may(): Support explicit actor_jid in context object
Matthew Wild <mwild1@gmail.com>
parents:
13237
diff
changeset
|
771 local permit = role:may(action); |
|
113ce2ac73a2
moduleapi: may(): Support explicit actor_jid in context object
Matthew Wild <mwild1@gmail.com>
parents:
13237
diff
changeset
|
772 if not permit then |
|
113ce2ac73a2
moduleapi: may(): Support explicit actor_jid in context object
Matthew Wild <mwild1@gmail.com>
parents:
13237
diff
changeset
|
773 if not peek then |
|
113ce2ac73a2
moduleapi: may(): Support explicit actor_jid in context object
Matthew Wild <mwild1@gmail.com>
parents:
13237
diff
changeset
|
774 self:log("debug", "Access denied: JID <%s> may not %s (not permitted by role %s)", actor_jid, action, role.name); |
|
113ce2ac73a2
moduleapi: may(): Support explicit actor_jid in context object
Matthew Wild <mwild1@gmail.com>
parents:
13237
diff
changeset
|
775 end |
|
113ce2ac73a2
moduleapi: may(): Support explicit actor_jid in context object
Matthew Wild <mwild1@gmail.com>
parents:
13237
diff
changeset
|
776 end |
|
113ce2ac73a2
moduleapi: may(): Support explicit actor_jid in context object
Matthew Wild <mwild1@gmail.com>
parents:
13237
diff
changeset
|
777 return permit; |
|
12642
9061f9621330
Switch to a new role-based authorization framework, removing is_admin()
Matthew Wild <mwild1@gmail.com>
parents:
12590
diff
changeset
|
778 end |
|
9061f9621330
Switch to a new role-based authorization framework, removing is_admin()
Matthew Wild <mwild1@gmail.com>
parents:
12590
diff
changeset
|
779 end |
|
9061f9621330
Switch to a new role-based authorization framework, removing is_admin()
Matthew Wild <mwild1@gmail.com>
parents:
12590
diff
changeset
|
780 |
|
13309
113ce2ac73a2
moduleapi: may(): Support explicit actor_jid in context object
Matthew Wild <mwild1@gmail.com>
parents:
13237
diff
changeset
|
781 -- Session-based actor |
|
12642
9061f9621330
Switch to a new role-based authorization framework, removing is_admin()
Matthew Wild <mwild1@gmail.com>
parents:
12590
diff
changeset
|
782 local session = context.origin or context.session; |
|
12652
30e2a0107217
moduleapi: Stricter type check for actor in permission check
Kim Alvefur <zash@zash.se>
parents:
12651
diff
changeset
|
783 if type(session) ~= "table" then |
|
12642
9061f9621330
Switch to a new role-based authorization framework, removing is_admin()
Matthew Wild <mwild1@gmail.com>
parents:
12590
diff
changeset
|
784 error("Unable to identify actor session from context"); |
|
9061f9621330
Switch to a new role-based authorization framework, removing is_admin()
Matthew Wild <mwild1@gmail.com>
parents:
12590
diff
changeset
|
785 end |
|
12994
5625da6ae6b6
moduleapi: may: Fail early if a local session has no role assigned
Matthew Wild <mwild1@gmail.com>
parents:
12972
diff
changeset
|
786 if session.type == "c2s" and session.host == self.host then |
|
5625da6ae6b6
moduleapi: may: Fail early if a local session has no role assigned
Matthew Wild <mwild1@gmail.com>
parents:
12972
diff
changeset
|
787 local role = session.role; |
|
5625da6ae6b6
moduleapi: may: Fail early if a local session has no role assigned
Matthew Wild <mwild1@gmail.com>
parents:
12972
diff
changeset
|
788 if not role then |
|
12995
e385f3a06673
moduleapi: Add 'peek' to :may() and new :could() helper to suppress logging
Matthew Wild <mwild1@gmail.com>
parents:
12994
diff
changeset
|
789 if not peek then |
|
e385f3a06673
moduleapi: Add 'peek' to :may() and new :could() helper to suppress logging
Matthew Wild <mwild1@gmail.com>
parents:
12994
diff
changeset
|
790 self:log("warn", "Access denied: session %s has no role assigned"); |
|
e385f3a06673
moduleapi: Add 'peek' to :may() and new :could() helper to suppress logging
Matthew Wild <mwild1@gmail.com>
parents:
12994
diff
changeset
|
791 end |
|
12994
5625da6ae6b6
moduleapi: may: Fail early if a local session has no role assigned
Matthew Wild <mwild1@gmail.com>
parents:
12972
diff
changeset
|
792 return false; |
|
5625da6ae6b6
moduleapi: may: Fail early if a local session has no role assigned
Matthew Wild <mwild1@gmail.com>
parents:
12972
diff
changeset
|
793 end |
|
5625da6ae6b6
moduleapi: may: Fail early if a local session has no role assigned
Matthew Wild <mwild1@gmail.com>
parents:
12972
diff
changeset
|
794 local permit = role:may(action, context); |
|
12995
e385f3a06673
moduleapi: Add 'peek' to :may() and new :could() helper to suppress logging
Matthew Wild <mwild1@gmail.com>
parents:
12994
diff
changeset
|
795 if not permit and not peek then |
|
12690
546c7e0f3f31
core.moduleapi: Check for local role-aware sessions before e.g. s2s
Kim Alvefur <zash@zash.se>
parents:
12662
diff
changeset
|
796 self:log("debug", "Access denied: session %s (%s) may not %s (not permitted by role %s)", |
|
12994
5625da6ae6b6
moduleapi: may: Fail early if a local session has no role assigned
Matthew Wild <mwild1@gmail.com>
parents:
12972
diff
changeset
|
797 session.id, session.full_jid, action, role.name |
|
12690
546c7e0f3f31
core.moduleapi: Check for local role-aware sessions before e.g. s2s
Kim Alvefur <zash@zash.se>
parents:
12662
diff
changeset
|
798 ); |
|
546c7e0f3f31
core.moduleapi: Check for local role-aware sessions before e.g. s2s
Kim Alvefur <zash@zash.se>
parents:
12662
diff
changeset
|
799 end |
|
546c7e0f3f31
core.moduleapi: Check for local role-aware sessions before e.g. s2s
Kim Alvefur <zash@zash.se>
parents:
12662
diff
changeset
|
800 return permit; |
|
546c7e0f3f31
core.moduleapi: Check for local role-aware sessions before e.g. s2s
Kim Alvefur <zash@zash.se>
parents:
12662
diff
changeset
|
801 else |
|
12642
9061f9621330
Switch to a new role-based authorization framework, removing is_admin()
Matthew Wild <mwild1@gmail.com>
parents:
12590
diff
changeset
|
802 local actor_jid = context.stanza.attr.from; |
|
9061f9621330
Switch to a new role-based authorization framework, removing is_admin()
Matthew Wild <mwild1@gmail.com>
parents:
12590
diff
changeset
|
803 local role = hosts[self.host].authz.get_jid_role(actor_jid); |
|
9061f9621330
Switch to a new role-based authorization framework, removing is_admin()
Matthew Wild <mwild1@gmail.com>
parents:
12590
diff
changeset
|
804 if not role then |
|
12995
e385f3a06673
moduleapi: Add 'peek' to :may() and new :could() helper to suppress logging
Matthew Wild <mwild1@gmail.com>
parents:
12994
diff
changeset
|
805 if not peek then |
|
e385f3a06673
moduleapi: Add 'peek' to :may() and new :could() helper to suppress logging
Matthew Wild <mwild1@gmail.com>
parents:
12994
diff
changeset
|
806 self:log("debug", "Access denied: JID <%s> may not %s (no role found)", actor_jid, action); |
|
e385f3a06673
moduleapi: Add 'peek' to :may() and new :could() helper to suppress logging
Matthew Wild <mwild1@gmail.com>
parents:
12994
diff
changeset
|
807 end |
|
12642
9061f9621330
Switch to a new role-based authorization framework, removing is_admin()
Matthew Wild <mwild1@gmail.com>
parents:
12590
diff
changeset
|
808 return false; |
|
9061f9621330
Switch to a new role-based authorization framework, removing is_admin()
Matthew Wild <mwild1@gmail.com>
parents:
12590
diff
changeset
|
809 end |
|
9061f9621330
Switch to a new role-based authorization framework, removing is_admin()
Matthew Wild <mwild1@gmail.com>
parents:
12590
diff
changeset
|
810 local permit = role:may(action, context); |
|
12995
e385f3a06673
moduleapi: Add 'peek' to :may() and new :could() helper to suppress logging
Matthew Wild <mwild1@gmail.com>
parents:
12994
diff
changeset
|
811 if not permit and not peek then |
|
12642
9061f9621330
Switch to a new role-based authorization framework, removing is_admin()
Matthew Wild <mwild1@gmail.com>
parents:
12590
diff
changeset
|
812 self:log("debug", "Access denied: JID <%s> may not %s (not permitted by role %s)", actor_jid, action, role.name); |
|
9061f9621330
Switch to a new role-based authorization framework, removing is_admin()
Matthew Wild <mwild1@gmail.com>
parents:
12590
diff
changeset
|
813 end |
|
9061f9621330
Switch to a new role-based authorization framework, removing is_admin()
Matthew Wild <mwild1@gmail.com>
parents:
12590
diff
changeset
|
814 return permit; |
|
9061f9621330
Switch to a new role-based authorization framework, removing is_admin()
Matthew Wild <mwild1@gmail.com>
parents:
12590
diff
changeset
|
815 end |
|
9061f9621330
Switch to a new role-based authorization framework, removing is_admin()
Matthew Wild <mwild1@gmail.com>
parents:
12590
diff
changeset
|
816 end |
|
9061f9621330
Switch to a new role-based authorization framework, removing is_admin()
Matthew Wild <mwild1@gmail.com>
parents:
12590
diff
changeset
|
817 |
|
13015
46c05c2e34f7
moduleapi: Add module:once() to execute a function after module load/startup
Matthew Wild <mwild1@gmail.com>
parents:
12995
diff
changeset
|
818 -- Execute a function, once, but only after startup is complete |
|
13360
6037b7a2131c
moduleapi: Rename :once() to :on_ready() for clarity
Matthew Wild <mwild1@gmail.com>
parents:
13309
diff
changeset
|
819 function api:on_ready(f) --luacheck: ignore 212/self |
|
13015
46c05c2e34f7
moduleapi: Add module:once() to execute a function after module load/startup
Matthew Wild <mwild1@gmail.com>
parents:
12995
diff
changeset
|
820 return prosody.started:next(f); |
|
46c05c2e34f7
moduleapi: Add module:once() to execute a function after module load/startup
Matthew Wild <mwild1@gmail.com>
parents:
12995
diff
changeset
|
821 end |
|
46c05c2e34f7
moduleapi: Add module:once() to execute a function after module load/startup
Matthew Wild <mwild1@gmail.com>
parents:
12995
diff
changeset
|
822 |
|
13360
6037b7a2131c
moduleapi: Rename :once() to :on_ready() for clarity
Matthew Wild <mwild1@gmail.com>
parents:
13309
diff
changeset
|
823 -- COMPAT w/post 0.12 trunk |
|
6037b7a2131c
moduleapi: Rename :once() to :on_ready() for clarity
Matthew Wild <mwild1@gmail.com>
parents:
13309
diff
changeset
|
824 function api:once(f) |
|
6037b7a2131c
moduleapi: Rename :once() to :on_ready() for clarity
Matthew Wild <mwild1@gmail.com>
parents:
13309
diff
changeset
|
825 self:log("warn", "This module uses deprecated module:once() - switch to module:on_ready() or (better) expose function module.ready()"); |
|
6037b7a2131c
moduleapi: Rename :once() to :on_ready() for clarity
Matthew Wild <mwild1@gmail.com>
parents:
13309
diff
changeset
|
826 return self:on_ready(f); |
|
6037b7a2131c
moduleapi: Rename :once() to :on_ready() for clarity
Matthew Wild <mwild1@gmail.com>
parents:
13309
diff
changeset
|
827 end |
|
6037b7a2131c
moduleapi: Rename :once() to :on_ready() for clarity
Matthew Wild <mwild1@gmail.com>
parents:
13309
diff
changeset
|
828 |
|
6422
6d4d87a89026
core.module{manager,api}: Fix for 010b141e91ed (Thanks v1ct0r)
Kim Alvefur <zash@zash.se>
parents:
6415
diff
changeset
|
829 return api; |
