Software /
code /
prosody
Annotate
core/moduleapi.lua @ 9929:3b776c32b9be
moduleapi: Remove overly-verbose debug logging on module status change
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Tue, 26 Mar 2019 17:22:25 +0000 |
parent | 9866:09cc8c856e5e |
child | 9930:12a31296d63d |
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 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 local array = require "util.array"; |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 local set = require "util.set"; |
6651
deaa3d66dc2c
moduleapi: Add module:broadcast() to send a stanza to a list of JIDs
Matthew Wild <mwild1@gmail.com>
parents:
6640
diff
changeset
|
11 local it = require "util.iterators"; |
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 local logger = require "util.logger"; |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 local pluginloader = require "util.pluginloader"; |
4750
b3525f3c2fee
moduleapi: Import util.timer
Matthew Wild <mwild1@gmail.com>
parents:
4747
diff
changeset
|
14 local timer = require "util.timer"; |
6165
6a184b16b717
core.certmanager, core.moduleapi, mod_storage_sql, mod_storage_sql2: Import from util.paths
Kim Alvefur <zash@zash.se>
parents:
5825
diff
changeset
|
15 local resolve_relative_path = require"util.paths".resolve_relative_path; |
6659
0a494394cd3e
moduleapi: Import util.stanza required for module:broadcast()
Matthew Wild <mwild1@gmail.com>
parents:
6656
diff
changeset
|
16 local st = require "util.stanza"; |
9747
c8240f931a68
core.moduleapi: Move util imports to top
Kim Alvefur <zash@zash.se>
parents:
9733
diff
changeset
|
17 local cache = require "util.cache"; |
9748
99199b53019f
core.moduleapi: Use util.error for :send_iq errors
Kim Alvefur <zash@zash.se>
parents:
9747
diff
changeset
|
18 local errutil = require "util.error"; |
9747
c8240f931a68
core.moduleapi: Move util imports to top
Kim Alvefur <zash@zash.se>
parents:
9733
diff
changeset
|
19 local promise = require "util.promise"; |
9866
09cc8c856e5e
moduleapi: New API for modules to set a status
Matthew Wild <mwild1@gmail.com>
parents:
9750
diff
changeset
|
20 local time_now = require "util.time".now; |
09cc8c856e5e
moduleapi: New API for modules to set a status
Matthew Wild <mwild1@gmail.com>
parents:
9750
diff
changeset
|
21 local format = require "util.format".format; |
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 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
|
24 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
|
25 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
|
26 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
|
27 local require = require; |
9686
e52e4e6e7ffb
moduleapi: Use pack from util.table
Kim Alvefur <zash@zash.se>
parents:
9556
diff
changeset
|
28 local pack = table.pack or require "util.table".pack; -- table.pack is only in 5.2 |
7164 | 29 local unpack = table.unpack or unpack; --luacheck: ignore 113 -- renamed in 5.2 |
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 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
|
32 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
|
33 |
9dd36e20c1e3
moduleapi: assert() that prosody.core_post_stanza is not nil
Matthew Wild <mwild1@gmail.com>
parents:
5412
diff
changeset
|
34 -- 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
|
35 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
|
36 "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
|
37 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 -- 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
|
39 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
|
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 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
|
42 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 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
|
44 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 -- 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
|
46 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
|
47 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
|
48 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 -- 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
|
51 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
|
52 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
|
53 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 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
|
56 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
|
57 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 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
|
60 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
|
61 -- 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
|
62 local _log = logger.init("mod_"..self.name); |
6656
58c111a39d27
moduleapi: Add luacheck annotation
Matthew Wild <mwild1@gmail.com>
parents:
6655
diff
changeset
|
63 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
|
64 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
|
65 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
|
66 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 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
|
69 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
|
70 end |
6655
a99ed5f5f709
moduleapi: Minor variable renaming to avoid clashes with the 'type' function
Matthew Wild <mwild1@gmail.com>
parents:
6654
diff
changeset
|
71 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
|
72 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
|
73 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 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
|
75 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
|
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 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 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
|
79 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
|
80 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 |
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
|
82 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
|
83 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
|
84 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
|
85 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
|
86 |
4708
0e324923ff95
moduleapi: Fix parameters to unhook_object_event()
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
87 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
|
88 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
|
89 return object.remove_handler(event, handler); |
838ad61c6b2c
moduleapi: Add module:unhook_object_event()
Matthew Wild <mwild1@gmail.com>
parents:
4666
diff
changeset
|
90 end |
838ad61c6b2c
moduleapi: Add module:unhook_object_event()
Matthew Wild <mwild1@gmail.com>
parents:
4666
diff
changeset
|
91 |
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 function api:hook(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
|
93 return self:hook_object_event((hosts[self.host] or 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
|
94 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
95 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
96 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
|
97 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
|
98 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
99 |
4719
2087d42f1e77
moduleapi: Rename module:hook_stanza() -> module:hook_tag() (hook_stanza works for compat)
Matthew Wild <mwild1@gmail.com>
parents:
4718
diff
changeset
|
100 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
|
101 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
|
102 -- 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
|
103 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
|
104 elseif not (handler and name) then |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
105 self:log("warn", "Error: Insufficient parameters to module:hook_stanza()"); |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
106 return; |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
107 end |
7947
24170d74b00b
core: Split some very long lines [luacheck]
Kim Alvefur <zash@zash.se>
parents:
7646
diff
changeset
|
108 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
|
109 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
|
110 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
|
111 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
|
112 |
5825
ac5e05ffc921
moduleapi: Add module:unhook()
Matthew Wild <mwild1@gmail.com>
parents:
5776
diff
changeset
|
113 function api:unhook(event, handler) |
ac5e05ffc921
moduleapi: Add module:unhook()
Matthew Wild <mwild1@gmail.com>
parents:
5776
diff
changeset
|
114 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
|
115 end |
ac5e05ffc921
moduleapi: Add module:unhook()
Matthew Wild <mwild1@gmail.com>
parents:
5776
diff
changeset
|
116 |
6640
859e9af53aa1
moduleapi: New methods for modules to conveniently wrap events
Matthew Wild <mwild1@gmail.com>
parents:
6639
diff
changeset
|
117 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
|
118 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
|
119 end |
859e9af53aa1
moduleapi: New methods for modules to conveniently wrap events
Matthew Wild <mwild1@gmail.com>
parents:
6639
diff
changeset
|
120 |
859e9af53aa1
moduleapi: New methods for modules to conveniently wrap events
Matthew Wild <mwild1@gmail.com>
parents:
6639
diff
changeset
|
121 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
|
122 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
|
123 end |
859e9af53aa1
moduleapi: New methods for modules to conveniently wrap events
Matthew Wild <mwild1@gmail.com>
parents:
6639
diff
changeset
|
124 |
859e9af53aa1
moduleapi: New methods for modules to conveniently wrap events
Matthew Wild <mwild1@gmail.com>
parents:
6639
diff
changeset
|
125 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
|
126 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
|
127 end |
859e9af53aa1
moduleapi: New methods for modules to conveniently wrap events
Matthew Wild <mwild1@gmail.com>
parents:
6639
diff
changeset
|
128 |
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
129 function api:require(lib) |
7115
805d068d2fd5
modulemanager, util.pluginloader: Move logic for locating some module libraries to pluginloader, to fix problems with non-filesystem errors being masked by the second load_code call
Matthew Wild <mwild1@gmail.com>
parents:
6779
diff
changeset
|
130 local f, n = pluginloader.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
|
131 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
|
132 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
|
133 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
134 |
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
|
135 function api:depends(name) |
6779
6236668da30a
core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents:
6659
diff
changeset
|
136 local modulemanager = require"core.modulemanager"; |
9556
e4c09e335bd9
moduleapi: Prevent loading disabled module as dependency of enabled one
Kim Alvefur <zash@zash.se>
parents:
9509
diff
changeset
|
137 if self:get_option_inherited_set("modules_disabled", {}):contains(name) then |
e4c09e335bd9
moduleapi: Prevent loading disabled module as dependency of enabled one
Kim Alvefur <zash@zash.se>
parents:
9509
diff
changeset
|
138 error("Dependency on disabled module mod_"..name); |
e4c09e335bd9
moduleapi: Prevent loading disabled module as dependency of enabled one
Kim Alvefur <zash@zash.se>
parents:
9509
diff
changeset
|
139 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
|
140 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
|
141 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
|
142 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
|
143 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
|
144 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
|
145 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
|
146 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
|
147 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
|
148 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
|
149 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
|
150 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
|
151 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
|
152 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
|
153 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
|
154 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
|
155 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
|
156 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
|
157 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
|
158 and modulemanager.module_has_method(mod, "add_host") then |
5077
6c2c8bf36d22
moduleapi: Clarify comment
Matthew Wild <mwild1@gmail.com>
parents:
5053
diff
changeset
|
159 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
|
160 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
|
161 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
|
162 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
|
163 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
|
164 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
|
165 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
|
166 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
|
167 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
|
168 self.dependencies[name] = true; |
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 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
|
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 |
9149
d03f21729b2c
moduleapi: Remove multiple-parameters feature from module:shared()
Matthew Wild <mwild1@gmail.com>
parents:
9042
diff
changeset
|
172 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
|
173 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
|
174 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
|
175 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
|
176 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
|
177 ..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
|
178 end |
d03f21729b2c
moduleapi: Remove multiple-parameters feature from module:shared()
Matthew Wild <mwild1@gmail.com>
parents:
9042
diff
changeset
|
179 local shared = tables[path]; |
d03f21729b2c
moduleapi: Remove multiple-parameters feature from module:shared()
Matthew Wild <mwild1@gmail.com>
parents:
9042
diff
changeset
|
180 if not shared then |
d03f21729b2c
moduleapi: Remove multiple-parameters feature from module:shared()
Matthew Wild <mwild1@gmail.com>
parents:
9042
diff
changeset
|
181 shared = {}; |
d03f21729b2c
moduleapi: Remove multiple-parameters feature from module:shared()
Matthew Wild <mwild1@gmail.com>
parents:
9042
diff
changeset
|
182 if path:match("%-cache$") then |
d03f21729b2c
moduleapi: Remove multiple-parameters feature from module:shared()
Matthew Wild <mwild1@gmail.com>
parents:
9042
diff
changeset
|
183 setmetatable(shared, { __mode = "kv" }); |
d03f21729b2c
moduleapi: Remove multiple-parameters feature from module:shared()
Matthew Wild <mwild1@gmail.com>
parents:
9042
diff
changeset
|
184 end |
d03f21729b2c
moduleapi: Remove multiple-parameters feature from module:shared()
Matthew Wild <mwild1@gmail.com>
parents:
9042
diff
changeset
|
185 tables[path] = shared; |
d03f21729b2c
moduleapi: Remove multiple-parameters feature from module:shared()
Matthew Wild <mwild1@gmail.com>
parents:
9042
diff
changeset
|
186 end |
d03f21729b2c
moduleapi: Remove multiple-parameters feature from module:shared()
Matthew Wild <mwild1@gmail.com>
parents:
9042
diff
changeset
|
187 return shared; |
d03f21729b2c
moduleapi: Remove multiple-parameters feature from module:shared()
Matthew Wild <mwild1@gmail.com>
parents:
9042
diff
changeset
|
188 end |
d03f21729b2c
moduleapi: Remove multiple-parameters feature from module:shared()
Matthew Wild <mwild1@gmail.com>
parents:
9042
diff
changeset
|
189 |
d03f21729b2c
moduleapi: Remove multiple-parameters feature from module:shared()
Matthew Wild <mwild1@gmail.com>
parents:
9042
diff
changeset
|
190 -- 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
|
191 -- 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
|
192 -- 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
|
193 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
|
194 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
|
195 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
|
196 self.shared_data[path] = shared; |
d03f21729b2c
moduleapi: Remove multiple-parameters feature from module:shared()
Matthew Wild <mwild1@gmail.com>
parents:
9042
diff
changeset
|
197 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
|
198 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
|
199 |
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
200 function api:get_option(name, default_value) |
8993
bd9b67f742c9
moduleapi: Lazy-load configmanager on demand simplify dependency graph
Kim Alvefur <zash@zash.se>
parents:
8533
diff
changeset
|
201 local config = require "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
|
202 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
|
203 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
|
204 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
|
205 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
206 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
|
207 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
208 |
7975
c64ddee9d671
core.moduleapi: Factor out code for getting a scalar config option
Kim Alvefur <zash@zash.se>
parents:
7947
diff
changeset
|
209 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
|
210 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
|
211 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
|
212 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
|
213 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
|
214 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
215 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
|
216 end |
7975
c64ddee9d671
core.moduleapi: Factor out code for getting a scalar config option
Kim Alvefur <zash@zash.se>
parents:
7947
diff
changeset
|
217 return value; |
c64ddee9d671
core.moduleapi: Factor out code for getting a scalar config option
Kim Alvefur <zash@zash.se>
parents:
7947
diff
changeset
|
218 end |
c64ddee9d671
core.moduleapi: Factor out code for getting a scalar config option
Kim Alvefur <zash@zash.se>
parents:
7947
diff
changeset
|
219 |
c64ddee9d671
core.moduleapi: Factor out code for getting a scalar config option
Kim Alvefur <zash@zash.se>
parents:
7947
diff
changeset
|
220 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
|
221 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
|
222 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
|
223 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
|
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 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
|
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 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
228 function api:get_option_number(name, ...) |
7975
c64ddee9d671
core.moduleapi: Factor out code for getting a scalar config option
Kim Alvefur <zash@zash.se>
parents:
7947
diff
changeset
|
229 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
|
230 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
|
231 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
|
232 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
|
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 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
|
235 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
236 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
237 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
|
238 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
|
239 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
|
240 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
|
241 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
242 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
|
243 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
|
244 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
|
245 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
|
246 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
|
247 else |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
248 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
|
249 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
250 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
251 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
|
252 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
|
253 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
254 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
|
255 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
256 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
257 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
|
258 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
|
259 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
260 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
|
261 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
|
262 end |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5771
diff
changeset
|
263 |
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
264 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
|
265 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
|
266 end |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5771
diff
changeset
|
267 |
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
268 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
|
269 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
270 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
271 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
|
272 local value = self:get_option_array(name, ...); |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5771
diff
changeset
|
273 |
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
274 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
|
275 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
|
276 end |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5771
diff
changeset
|
277 |
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
278 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
|
279 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
280 |
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
|
281 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
|
282 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
|
283 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
|
284 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
|
285 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
|
286 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
|
287 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
|
288 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
|
289 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
|
290 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
|
291 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
|
292 |
7127
27557dd7b460
moduleapi: Add API method for getting a file path
Kim Alvefur <zash@zash.se>
parents:
7115
diff
changeset
|
293 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
|
294 if parent == nil then |
9509
b57353f76c83
core.moduleapi: Remove redundant condition
Kim Alvefur <zash@zash.se>
parents:
9149
diff
changeset
|
295 parent = self:get_directory(); |
7127
27557dd7b460
moduleapi: Add API method for getting a file path
Kim Alvefur <zash@zash.se>
parents:
7115
diff
changeset
|
296 elseif prosody.paths[parent] then |
27557dd7b460
moduleapi: Add API method for getting a file path
Kim Alvefur <zash@zash.se>
parents:
7115
diff
changeset
|
297 parent = prosody.paths[parent]; |
27557dd7b460
moduleapi: Add API method for getting a file path
Kim Alvefur <zash@zash.se>
parents:
7115
diff
changeset
|
298 end |
27557dd7b460
moduleapi: Add API method for getting a file path
Kim Alvefur <zash@zash.se>
parents:
7115
diff
changeset
|
299 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
|
300 if value == nil then |
27557dd7b460
moduleapi: Add API method for getting a file path
Kim Alvefur <zash@zash.se>
parents:
7115
diff
changeset
|
301 return nil; |
27557dd7b460
moduleapi: Add API method for getting a file path
Kim Alvefur <zash@zash.se>
parents:
7115
diff
changeset
|
302 end |
27557dd7b460
moduleapi: Add API method for getting a file path
Kim Alvefur <zash@zash.se>
parents:
7115
diff
changeset
|
303 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
|
304 end |
27557dd7b460
moduleapi: Add API method for getting a file path
Kim Alvefur <zash@zash.se>
parents:
7115
diff
changeset
|
305 |
27557dd7b460
moduleapi: Add API method for getting a file path
Kim Alvefur <zash@zash.se>
parents:
7115
diff
changeset
|
306 |
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
|
307 function api:context(host) |
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
|
308 return setmetatable({host=host or "*"}, {__index=self,__newindex=self}); |
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
|
309 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
|
310 |
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
311 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
|
312 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
|
313 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
|
314 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
|
315 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
|
316 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
317 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
|
318 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
|
319 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
|
320 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
|
321 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
|
322 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
|
323 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
|
324 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
325 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
326 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
327 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
328 function api:get_host_items(key) |
6779
6236668da30a
core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents:
6659
diff
changeset
|
329 local modulemanager = require"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
|
330 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
|
331 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
|
332 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
333 |
6655
a99ed5f5f709
moduleapi: Minor variable renaming to avoid clashes with the 'type' function
Matthew Wild <mwild1@gmail.com>
parents:
6654
diff
changeset
|
334 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
|
335 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
|
336 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
|
337 if existing ~= false then |
6655
a99ed5f5f709
moduleapi: Minor variable renaming to avoid clashes with the 'type' function
Matthew Wild <mwild1@gmail.com>
parents:
6654
diff
changeset
|
338 for _, item in ipairs(self:get_host_items(item_type)) do |
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
339 added_cb({ item = item }); |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
340 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
341 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
342 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
343 |
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
|
344 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
|
345 -- 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
|
346 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
|
347 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
|
348 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
|
349 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
|
350 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
|
351 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
|
352 if not item.name then |
4660
96b40b5e8ea8
moduleapi: module:provides(): Fix usage of wrong table
Matthew Wild <mwild1@gmail.com>
parents:
4651
diff
changeset
|
353 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
|
354 -- 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
|
355 -- (e.g. "auth_foo" -> "foo" for an auth provider) |
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
|
356 if item_name:find(name.."_", 1, true) == 1 then |
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
|
357 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
|
358 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
|
359 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
|
360 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
|
361 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
|
362 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
|
363 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
|
364 |
7342
79a5db780e8b
moduleapi: Allow an origin session to be passed to module:send()
Kim Alvefur <zash@zash.se>
parents:
7163
diff
changeset
|
365 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
|
366 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
|
367 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
|
368 |
9733
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
369 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
|
370 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
|
371 if not iq_cache then |
9747
c8240f931a68
core.moduleapi: Move util imports to top
Kim Alvefur <zash@zash.se>
parents:
9733
diff
changeset
|
372 iq_cache = cache.new(256, function (_, iq) |
9748
99199b53019f
core.moduleapi: Use util.error for :send_iq errors
Kim Alvefur <zash@zash.se>
parents:
9747
diff
changeset
|
373 iq.reject(errutil.new({ |
99199b53019f
core.moduleapi: Use util.error for :send_iq errors
Kim Alvefur <zash@zash.se>
parents:
9747
diff
changeset
|
374 type = "wait", condition = "resource-constraint", |
99199b53019f
core.moduleapi: Use util.error for :send_iq errors
Kim Alvefur <zash@zash.se>
parents:
9747
diff
changeset
|
375 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
|
376 })); |
9733
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
377 self:unhook(iq.result_event, iq.result_handler); |
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
378 self:unhook(iq.error_event, iq.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
|
379 end); |
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
380 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
|
381 end |
9747
c8240f931a68
core.moduleapi: Move util imports to top
Kim Alvefur <zash@zash.se>
parents:
9733
diff
changeset
|
382 return 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
|
383 local event_type; |
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
384 if stanza.attr.from == self.host then |
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
385 event_type = "host"; |
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
386 else -- assume bare since we can't hook full jids |
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
387 event_type = "bare"; |
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
388 end |
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
389 local result_event = "iq-result/"..event_type.."/"..stanza.attr.id; |
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
390 local error_event = "iq-error/"..event_type.."/"..stanza.attr.id; |
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
391 local cache_key = event_type.."/"..stanza.attr.id; |
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
392 |
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
393 local function result_handler(event) |
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
394 if event.stanza.attr.from == stanza.attr.to then |
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
395 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
|
396 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
|
397 end |
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
398 end |
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
399 |
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
400 local function error_handler(event) |
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
401 if event.stanza.attr.from == stanza.attr.to then |
9750
65432dc80d90
core.moduleapi: Use convenience function for creating error object from stanza
Kim Alvefur <zash@zash.se>
parents:
9748
diff
changeset
|
402 reject(errutil.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
|
403 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
|
404 end |
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
405 end |
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
406 |
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
407 if iq_cache:get(cache_key) then |
9748
99199b53019f
core.moduleapi: Use util.error for :send_iq errors
Kim Alvefur <zash@zash.se>
parents:
9747
diff
changeset
|
408 reject(errutil.new({ |
99199b53019f
core.moduleapi: Use util.error for :send_iq errors
Kim Alvefur <zash@zash.se>
parents:
9747
diff
changeset
|
409 type = "modify", condition = "conflict", |
99199b53019f
core.moduleapi: Use util.error for :send_iq errors
Kim Alvefur <zash@zash.se>
parents:
9747
diff
changeset
|
410 text = "iq stanza id attribute already used", |
99199b53019f
core.moduleapi: Use util.error for :send_iq errors
Kim Alvefur <zash@zash.se>
parents:
9747
diff
changeset
|
411 })); |
99199b53019f
core.moduleapi: Use util.error for :send_iq errors
Kim Alvefur <zash@zash.se>
parents:
9747
diff
changeset
|
412 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
|
413 end |
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
414 |
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
415 self:hook(result_event, result_handler); |
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
416 self:hook(error_event, 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
|
417 |
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
418 local timeout_handle = self:add_timer(timeout or 120, function () |
9748
99199b53019f
core.moduleapi: Use util.error for :send_iq errors
Kim Alvefur <zash@zash.se>
parents:
9747
diff
changeset
|
419 reject(errutil.new({ |
99199b53019f
core.moduleapi: Use util.error for :send_iq errors
Kim Alvefur <zash@zash.se>
parents:
9747
diff
changeset
|
420 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
|
421 text = "IQ stanza timed out", |
99199b53019f
core.moduleapi: Use util.error for :send_iq errors
Kim Alvefur <zash@zash.se>
parents:
9747
diff
changeset
|
422 })); |
9733
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
423 self:unhook(result_event, result_handler); |
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
424 self:unhook(error_event, 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
|
425 iq_cache:set(cache_key, nil); |
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
426 end); |
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
427 |
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
428 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
|
429 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
|
430 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
|
431 result_event = result_event, error_event = error_event, |
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
432 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
|
433 }); |
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
434 |
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
435 if not ok then |
9748
99199b53019f
core.moduleapi: Use util.error for :send_iq errors
Kim Alvefur <zash@zash.se>
parents:
9747
diff
changeset
|
436 reject(errutil.new({ |
99199b53019f
core.moduleapi: Use util.error for :send_iq errors
Kim Alvefur <zash@zash.se>
parents:
9747
diff
changeset
|
437 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
|
438 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
|
439 })); |
9733
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
440 return; |
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
441 end |
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
442 |
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
443 self:send(stanza, origin); |
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
444 end); |
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
445 end |
9ab9aabafa80
core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714)
Kim Alvefur <zash@zash.se>
parents:
9686
diff
changeset
|
446 |
6651
deaa3d66dc2c
moduleapi: Add module:broadcast() to send a stanza to a list of JIDs
Matthew Wild <mwild1@gmail.com>
parents:
6640
diff
changeset
|
447 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
|
448 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
|
449 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
|
450 new_stanza.attr.to = jid; |
9042
734ba7080b35
moduleapi: Use :send API from :broadcast for compactness
Kim Alvefur <zash@zash.se>
parents:
8994
diff
changeset
|
451 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
|
452 end |
deaa3d66dc2c
moduleapi: Add module:broadcast() to send a stanza to a list of JIDs
Matthew Wild <mwild1@gmail.com>
parents:
6640
diff
changeset
|
453 end |
deaa3d66dc2c
moduleapi: Add module:broadcast() to send a stanza to a list of JIDs
Matthew Wild <mwild1@gmail.com>
parents:
6640
diff
changeset
|
454 |
5899
26f54b462601
core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents:
5825
diff
changeset
|
455 local timer_methods = { } |
26f54b462601
core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents:
5825
diff
changeset
|
456 local timer_mt = { |
26f54b462601
core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents:
5825
diff
changeset
|
457 __index = timer_methods; |
26f54b462601
core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents:
5825
diff
changeset
|
458 } |
26f54b462601
core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents:
5825
diff
changeset
|
459 function timer_methods:stop( ) |
26f54b462601
core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents:
5825
diff
changeset
|
460 timer.stop(self.id); |
26f54b462601
core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents:
5825
diff
changeset
|
461 end |
26f54b462601
core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents:
5825
diff
changeset
|
462 timer_methods.disarm = timer_methods.stop |
26f54b462601
core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents:
5825
diff
changeset
|
463 function timer_methods:reschedule(delay) |
26f54b462601
core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents:
5825
diff
changeset
|
464 timer.reschedule(self.id, delay) |
26f54b462601
core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents:
5825
diff
changeset
|
465 end |
26f54b462601
core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents:
5825
diff
changeset
|
466 |
7142
67226eaef97c
moduleapi: Silence luacheck warning about unused 'id' parameter
Matthew Wild <mwild1@gmail.com>
parents:
7141
diff
changeset
|
467 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
|
468 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
|
469 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
|
470 end |
26f54b462601
core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents:
5825
diff
changeset
|
471 |
26f54b462601
core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents:
5825
diff
changeset
|
472 function api:add_timer(delay, callback, ...) |
26f54b462601
core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents:
5825
diff
changeset
|
473 local t = pack(...) |
26f54b462601
core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents:
5825
diff
changeset
|
474 t.module_env = self; |
26f54b462601
core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents:
5825
diff
changeset
|
475 t.callback = callback; |
26f54b462601
core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents:
5825
diff
changeset
|
476 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
|
477 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
|
478 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
|
479 |
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
|
480 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
|
481 function api:get_directory() |
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
|
482 return self.path and (self.path:gsub("%"..path_sep.."[^"..path_sep.."]*$", "")) or nil; |
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
|
483 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
|
484 |
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
|
485 function api:load_resource(path, mode) |
6165
6a184b16b717
core.certmanager, core.moduleapi, mod_storage_sql, mod_storage_sql2: Import from util.paths
Kim Alvefur <zash@zash.se>
parents:
5825
diff
changeset
|
486 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
|
487 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
|
488 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
|
489 |
6655
a99ed5f5f709
moduleapi: Minor variable renaming to avoid clashes with the 'type' function
Matthew Wild <mwild1@gmail.com>
parents:
6654
diff
changeset
|
490 function api:open_store(name, store_type) |
a99ed5f5f709
moduleapi: Minor variable renaming to avoid clashes with the 'type' function
Matthew Wild <mwild1@gmail.com>
parents:
6654
diff
changeset
|
491 return require"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
|
492 end |
7a0b81b5ca71
moduleapi: Add module:open_store() as a front-end to storagemanager.open()
Matthew Wild <mwild1@gmail.com>
parents:
5434
diff
changeset
|
493 |
6655
a99ed5f5f709
moduleapi: Minor variable renaming to avoid clashes with the 'type' function
Matthew Wild <mwild1@gmail.com>
parents:
6654
diff
changeset
|
494 function api:measure(name, stat_type) |
8994
f17e768c1a99
moduleapi: Lazy-load statsmanager on demand
Kim Alvefur <zash@zash.se>
parents:
8993
diff
changeset
|
495 local measure = require "core.statsmanager".measure; |
6655
a99ed5f5f709
moduleapi: Minor variable renaming to avoid clashes with the 'type' function
Matthew Wild <mwild1@gmail.com>
parents:
6654
diff
changeset
|
496 return measure(stat_type, "/"..self.host.."/mod_"..self.name.."/"..name); |
6556
74253c7beb9c
moduleapi: Module API for statsmanager
Matthew Wild <mwild1@gmail.com>
parents:
6422
diff
changeset
|
497 end |
74253c7beb9c
moduleapi: Module API for statsmanager
Matthew Wild <mwild1@gmail.com>
parents:
6422
diff
changeset
|
498 |
6639
3003d041c159
moduleapi: Experimental API for modules to measure the rate+duration of events
Matthew Wild <mwild1@gmail.com>
parents:
6556
diff
changeset
|
499 function api:measure_object_event(events_object, event_name, stat_name) |
7646
6210dfaec84f
moduleapi: 'duration' became 'times'
Matthew Wild <mwild1@gmail.com>
parents:
7342
diff
changeset
|
500 local m = self:measure(stat_name or event_name, "times"); |
6655
a99ed5f5f709
moduleapi: Minor variable renaming to avoid clashes with the 'type' function
Matthew Wild <mwild1@gmail.com>
parents:
6654
diff
changeset
|
501 local function handler(handlers, _event_name, _event_data) |
6639
3003d041c159
moduleapi: Experimental API for modules to measure the rate+duration of events
Matthew Wild <mwild1@gmail.com>
parents:
6556
diff
changeset
|
502 local finished = m(); |
6655
a99ed5f5f709
moduleapi: Minor variable renaming to avoid clashes with the 'type' function
Matthew Wild <mwild1@gmail.com>
parents:
6654
diff
changeset
|
503 local ret = handlers(_event_name, _event_data); |
6639
3003d041c159
moduleapi: Experimental API for modules to measure the rate+duration of events
Matthew Wild <mwild1@gmail.com>
parents:
6556
diff
changeset
|
504 finished(); |
3003d041c159
moduleapi: Experimental API for modules to measure the rate+duration of events
Matthew Wild <mwild1@gmail.com>
parents:
6556
diff
changeset
|
505 return ret; |
3003d041c159
moduleapi: Experimental API for modules to measure the rate+duration of events
Matthew Wild <mwild1@gmail.com>
parents:
6556
diff
changeset
|
506 end |
3003d041c159
moduleapi: Experimental API for modules to measure the rate+duration of events
Matthew Wild <mwild1@gmail.com>
parents:
6556
diff
changeset
|
507 return self:hook_object_event(events_object, event_name, handler); |
3003d041c159
moduleapi: Experimental API for modules to measure the rate+duration of events
Matthew Wild <mwild1@gmail.com>
parents:
6556
diff
changeset
|
508 end |
3003d041c159
moduleapi: Experimental API for modules to measure the rate+duration of events
Matthew Wild <mwild1@gmail.com>
parents:
6556
diff
changeset
|
509 |
3003d041c159
moduleapi: Experimental API for modules to measure the rate+duration of events
Matthew Wild <mwild1@gmail.com>
parents:
6556
diff
changeset
|
510 function api:measure_event(event_name, stat_name) |
6653
8241eac507c3
moduleapi: module:measure_event(), module:measure_global_event(): Fix copy/paste error
Matthew Wild <mwild1@gmail.com>
parents:
6652
diff
changeset
|
511 return self:measure_object_event((hosts[self.host] or prosody).events.wrappers, event_name, stat_name); |
6639
3003d041c159
moduleapi: Experimental API for modules to measure the rate+duration of events
Matthew Wild <mwild1@gmail.com>
parents:
6556
diff
changeset
|
512 end |
3003d041c159
moduleapi: Experimental API for modules to measure the rate+duration of events
Matthew Wild <mwild1@gmail.com>
parents:
6556
diff
changeset
|
513 |
3003d041c159
moduleapi: Experimental API for modules to measure the rate+duration of events
Matthew Wild <mwild1@gmail.com>
parents:
6556
diff
changeset
|
514 function api:measure_global_event(event_name, stat_name) |
6653
8241eac507c3
moduleapi: module:measure_event(), module:measure_global_event(): Fix copy/paste error
Matthew Wild <mwild1@gmail.com>
parents:
6652
diff
changeset
|
515 return self:measure_object_event(prosody.events.wrappers, event_name, stat_name); |
6639
3003d041c159
moduleapi: Experimental API for modules to measure the rate+duration of events
Matthew Wild <mwild1@gmail.com>
parents:
6556
diff
changeset
|
516 end |
3003d041c159
moduleapi: Experimental API for modules to measure the rate+duration of events
Matthew Wild <mwild1@gmail.com>
parents:
6556
diff
changeset
|
517 |
9866
09cc8c856e5e
moduleapi: New API for modules to set a status
Matthew Wild <mwild1@gmail.com>
parents:
9750
diff
changeset
|
518 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
|
519 |
09cc8c856e5e
moduleapi: New API for modules to set a status
Matthew Wild <mwild1@gmail.com>
parents:
9750
diff
changeset
|
520 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
|
521 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
|
522 if not priority then |
09cc8c856e5e
moduleapi: New API for modules to set a status
Matthew Wild <mwild1@gmail.com>
parents:
9750
diff
changeset
|
523 self:log("error", "set_status: Invalid status type '%s', assuming 'info'"); |
09cc8c856e5e
moduleapi: New API for modules to set a status
Matthew Wild <mwild1@gmail.com>
parents:
9750
diff
changeset
|
524 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
|
525 end |
09cc8c856e5e
moduleapi: New API for modules to set a status
Matthew Wild <mwild1@gmail.com>
parents:
9750
diff
changeset
|
526 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
|
527 -- 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
|
528 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
|
529 or (override == false and current_priority > priority) then |
09cc8c856e5e
moduleapi: New API for modules to set a status
Matthew Wild <mwild1@gmail.com>
parents:
9750
diff
changeset
|
530 self:log("debug", "Ignoring status"); |
09cc8c856e5e
moduleapi: New API for modules to set a status
Matthew Wild <mwild1@gmail.com>
parents:
9750
diff
changeset
|
531 return; |
09cc8c856e5e
moduleapi: New API for modules to set a status
Matthew Wild <mwild1@gmail.com>
parents:
9750
diff
changeset
|
532 end |
09cc8c856e5e
moduleapi: New API for modules to set a status
Matthew Wild <mwild1@gmail.com>
parents:
9750
diff
changeset
|
533 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
|
534 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
|
535 end |
09cc8c856e5e
moduleapi: New API for modules to set a status
Matthew Wild <mwild1@gmail.com>
parents:
9750
diff
changeset
|
536 |
09cc8c856e5e
moduleapi: New API for modules to set a status
Matthew Wild <mwild1@gmail.com>
parents:
9750
diff
changeset
|
537 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
|
538 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
|
539 return self:log(level, msg, ...); |
09cc8c856e5e
moduleapi: New API for modules to set a status
Matthew Wild <mwild1@gmail.com>
parents:
9750
diff
changeset
|
540 end |
09cc8c856e5e
moduleapi: New API for modules to set a status
Matthew Wild <mwild1@gmail.com>
parents:
9750
diff
changeset
|
541 |
09cc8c856e5e
moduleapi: New API for modules to set a status
Matthew Wild <mwild1@gmail.com>
parents:
9750
diff
changeset
|
542 function api:get_status() |
09cc8c856e5e
moduleapi: New API for modules to set a status
Matthew Wild <mwild1@gmail.com>
parents:
9750
diff
changeset
|
543 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
|
544 end |
09cc8c856e5e
moduleapi: New API for modules to set a status
Matthew Wild <mwild1@gmail.com>
parents:
9750
diff
changeset
|
545 |
6422
6d4d87a89026
core.module{manager,api}: Fix for 010b141e91ed (Thanks v1ct0r)
Kim Alvefur <zash@zash.se>
parents:
6415
diff
changeset
|
546 return api; |