Annotate

core/moduleapi.lua @ 5900:cb1103423aa7

core.moduleapi: Fix some global accesses.
author Waqas Hussain <waqas20@gmail.com>
date Wed, 06 Nov 2013 14:38:51 -0500
parent 5899:26f54b462601
child 6168:3942630b4e35
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 config = require "core.configmanager";
5163
854f6d9aee2d moduleapi: Revert previous commit and add comment, it's meant to be this way
Matthew Wild <mwild1@gmail.com>
parents: 5161
diff changeset
10 local modulemanager = require "modulemanager"; -- This is necessary to avoid require loops
4531
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 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
12 local set = require "util.set";
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 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
14 local pluginloader = require "util.pluginloader";
4750
b3525f3c2fee moduleapi: Import util.timer
Matthew Wild <mwild1@gmail.com>
parents: 4747
diff changeset
15 local timer = require "util.timer";
4531
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 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
18 local error, setmetatable, type = error, setmetatable, type;
5900
cb1103423aa7 core.moduleapi: Fix some global accesses.
Waqas Hussain <waqas20@gmail.com>
parents: 5899
diff changeset
19 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
20 local tonumber, tostring = tonumber, tostring;
5900
cb1103423aa7 core.moduleapi: Fix some global accesses.
Waqas Hussain <waqas20@gmail.com>
parents: 5899
diff changeset
21 local pack = table.pack or function(...) return {n=select("#",...), ...}; end -- table.pack is only in 5.2
cb1103423aa7 core.moduleapi: Fix some global accesses.
Waqas Hussain <waqas20@gmail.com>
parents: 5899
diff changeset
22 local unpack = table.unpack or unpack; -- 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
23
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 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
25 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
26
9dd36e20c1e3 moduleapi: assert() that prosody.core_post_stanza is not nil
Matthew Wild <mwild1@gmail.com>
parents: 5412
diff changeset
27 -- 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
28 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
29 "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
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 -- 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
32 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
33
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 local NULL = {};
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 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
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 -- 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
39 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
40 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
41 end
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 -- 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
44 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
45 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
46 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 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
49 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
50 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 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
53 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
54 -- 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
55 local _log = logger.init("mod_"..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
56 self.log = function (self, ...) return _log(...); end;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 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
58 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
59 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 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
62 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
63 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 function api:add_identity(category, type, name)
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 self:add_item("identity", {category = category, type = type, name = name});
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 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
68 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
69 end
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
70 function api:has_feature(xmlns)
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
71 for _, feature in ipairs(self:get_host_items("feature")) do
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
72 if feature == xmlns then return true; end
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
73 end
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
74 return false;
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
75 end
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
76 function api:has_identity(category, type, name)
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
77 for _, id in ipairs(self:get_host_items("identity")) do
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
78 if id.category == category and id.type == type and id.name == name then
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5771
diff changeset
79 return true;
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
80 end
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
81 end
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
82 return false;
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
83 end
4531
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 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
86 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
87 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88
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
89 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
90 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
91 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
92 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
93
4708
0e324923ff95 moduleapi: Fix parameters to unhook_object_event()
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
94 function api:unhook_object_event(object, event, handler)
4695
838ad61c6b2c moduleapi: Add module:unhook_object_event()
Matthew Wild <mwild1@gmail.com>
parents: 4666
diff changeset
95 return object.remove_handler(event, handler);
838ad61c6b2c moduleapi: Add module:unhook_object_event()
Matthew Wild <mwild1@gmail.com>
parents: 4666
diff changeset
96 end
838ad61c6b2c moduleapi: Add module:unhook_object_event()
Matthew Wild <mwild1@gmail.com>
parents: 4666
diff changeset
97
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 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
99 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
100 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 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
103 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
104 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105
4719
2087d42f1e77 moduleapi: Rename module:hook_stanza() -> module:hook_tag() (hook_stanza works for compat)
Matthew Wild <mwild1@gmail.com>
parents: 4718
diff changeset
106 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
107 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
108 -- 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
109 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
110 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
111 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
112 return;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 return self:hook("stanza/"..(xmlns and (xmlns..":") or "")..name, function (data) return handler(data.origin, data.stanza, data); end, priority);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 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
116 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
117
5825
ac5e05ffc921 moduleapi: Add module:unhook()
Matthew Wild <mwild1@gmail.com>
parents: 5776
diff changeset
118 function api:unhook(event, handler)
ac5e05ffc921 moduleapi: Add module:unhook()
Matthew Wild <mwild1@gmail.com>
parents: 5776
diff changeset
119 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
120 end
ac5e05ffc921 moduleapi: Add module:unhook()
Matthew Wild <mwild1@gmail.com>
parents: 5776
diff changeset
121
4531
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 function api:require(lib)
5021
85b2689dbcfe Eliminate direct setfenv usage
Florian Zeitz <florob@babelmonkeys.de>
parents: 4896
diff changeset
123 local f, n = pluginloader.load_code(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
124 if not f then
5021
85b2689dbcfe Eliminate direct setfenv usage
Florian Zeitz <florob@babelmonkeys.de>
parents: 4896
diff changeset
125 f, n = pluginloader.load_code(lib, 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
126 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 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
128 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
129 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130
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
131 function api:depends(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
132 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
133 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
134 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
135 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
136 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
137 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
138 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
139 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
140 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
141 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
142 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
143 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
144 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
145 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
146 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
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 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
149 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
150 and modulemanager.module_has_method(mod, "add_host") then
5077
6c2c8bf36d22 moduleapi: Clarify comment
Matthew Wild <mwild1@gmail.com>
parents: 5053
diff changeset
151 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
152 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
153 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
154 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
155 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
156 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
157 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
158 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
159 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
160 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
161 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
162 end
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
163
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
164 -- Returns one or more shared tables at the specified virtual paths
3cbfa768eb06 moduleapi: Add module:shared(), a way to easily share data between multiple loaded modules
Matthew Wild <mwild1@gmail.com>
parents: 4538
diff changeset
165 -- Intentionally does not allow the table at a path to be _set_, it
3cbfa768eb06 moduleapi: Add module:shared(), a way to easily share data between multiple loaded modules
Matthew Wild <mwild1@gmail.com>
parents: 4538
diff changeset
166 -- is auto-created if it does not exist.
3cbfa768eb06 moduleapi: Add module:shared(), a way to easily share data between multiple loaded modules
Matthew Wild <mwild1@gmail.com>
parents: 4538
diff changeset
167 function api:shared(...)
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
168 if not self.shared_data then self.shared_data = {}; end
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
169 local paths = { n = select("#", ...), ... };
3cbfa768eb06 moduleapi: Add module:shared(), a way to easily share data between multiple loaded modules
Matthew Wild <mwild1@gmail.com>
parents: 4538
diff changeset
170 local data_array = {};
3cbfa768eb06 moduleapi: Add module:shared(), a way to easily share data between multiple loaded modules
Matthew Wild <mwild1@gmail.com>
parents: 4538
diff changeset
171 local default_path_components = { self.host, self.name };
3cbfa768eb06 moduleapi: Add module:shared(), a way to easily share data between multiple loaded modules
Matthew Wild <mwild1@gmail.com>
parents: 4538
diff changeset
172 for i = 1, paths.n do
3cbfa768eb06 moduleapi: Add module:shared(), a way to easily share data between multiple loaded modules
Matthew Wild <mwild1@gmail.com>
parents: 4538
diff changeset
173 local path = paths[i];
3cbfa768eb06 moduleapi: Add module:shared(), a way to easily share data between multiple loaded modules
Matthew Wild <mwild1@gmail.com>
parents: 4538
diff changeset
174 if path:sub(1,1) ~= "/" then -- Prepend default components
3cbfa768eb06 moduleapi: Add module:shared(), a way to easily share data between multiple loaded modules
Matthew Wild <mwild1@gmail.com>
parents: 4538
diff changeset
175 local n_components = select(2, path:gsub("/", "%1"));
3cbfa768eb06 moduleapi: Add module:shared(), a way to easily share data between multiple loaded modules
Matthew Wild <mwild1@gmail.com>
parents: 4538
diff changeset
176 path = (n_components<#default_path_components and "/" or "")..t_concat(default_path_components, "/", 1, #default_path_components-n_components).."/"..path;
3cbfa768eb06 moduleapi: Add module:shared(), a way to easily share data between multiple loaded modules
Matthew Wild <mwild1@gmail.com>
parents: 4538
diff changeset
177 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
178 local shared = shared_data[path];
3cbfa768eb06 moduleapi: Add module:shared(), a way to easily share data between multiple loaded modules
Matthew Wild <mwild1@gmail.com>
parents: 4538
diff changeset
179 if not shared then
3cbfa768eb06 moduleapi: Add module:shared(), a way to easily share data between multiple loaded modules
Matthew Wild <mwild1@gmail.com>
parents: 4538
diff changeset
180 shared = {};
5053
375ab71e4b37 moduleapi: If path name ends with '-cache' create table as weak (keys and values)
Matthew Wild <mwild1@gmail.com>
parents: 5024
diff changeset
181 if path:match("%-cache$") then
375ab71e4b37 moduleapi: If path name ends with '-cache' create table as weak (keys and values)
Matthew Wild <mwild1@gmail.com>
parents: 5024
diff changeset
182 setmetatable(shared, { __mode = "kv" });
375ab71e4b37 moduleapi: If path name ends with '-cache' create table as weak (keys and values)
Matthew Wild <mwild1@gmail.com>
parents: 5024
diff changeset
183 end
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
184 shared_data[path] = shared;
3cbfa768eb06 moduleapi: Add module:shared(), a way to easily share data between multiple loaded modules
Matthew Wild <mwild1@gmail.com>
parents: 4538
diff changeset
185 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
186 t_insert(data_array, shared);
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
187 self.shared_data[path] = 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
188 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
189 return unpack(data_array);
3cbfa768eb06 moduleapi: Add module:shared(), a way to easily share data between multiple loaded modules
Matthew Wild <mwild1@gmail.com>
parents: 4538
diff changeset
190 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
191
4531
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
192 function api:get_option(name, default_value)
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
193 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
194 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
195 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
196 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
197 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
198 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
199
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_string(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
201 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
202 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
203 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
204 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
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 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
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 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
209 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
210 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
211 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
212 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
213
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
214 function api:get_option_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
215 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
216 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
217 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
218 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
219 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
220 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
221 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
222 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
223 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
224 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
225 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
226 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
227 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
228
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
229 function api:get_option_boolean(name, ...)
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 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
231 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
232 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
233 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
234 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
235 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
236 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
237 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
238 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
239 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
240 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
241 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
242 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
243 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
244 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
245 else
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 = nil;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
247 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
248 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
249 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
250 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
251 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
252 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
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
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
255 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
256 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
257
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
258 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
259 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
260 end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5771
diff changeset
261
4531
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
262 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
263 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
264 end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5771
diff changeset
265
4531
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
266 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
267 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
268
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
269 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
270 local value = self:get_option_array(name, ...);
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5771
diff changeset
271
4531
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
272 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
273 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
274 end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5771
diff changeset
275
4531
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
276 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
277 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
278
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
279 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
280 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
281 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
282 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
283 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
284 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
285 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
286 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
287 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
288 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
289 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
290
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
291 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
292 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
293 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
294
4531
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
295 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
296 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
297 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
298 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
299 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
300 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
301 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
302 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
303 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
304 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
305 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
306 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
307 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
308 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
309 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
310 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
311
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
312 function api:get_host_items(key)
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
313 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
314 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
315 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
316
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:handle_items(type, added_cb, removed_cb, existing)
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
318 self:hook("item-added/"..type, added_cb);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
319 self:hook("item-removed/"..type, removed_cb);
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 existing ~= false 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 for _, item in ipairs(self:get_host_items(type)) do
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
322 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
323 end
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
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
327 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
328 -- 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
329 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
330 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
331 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
332 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
333 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
334 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
335 if not item.name then
4660
96b40b5e8ea8 moduleapi: module:provides(): Fix usage of wrong table
Matthew Wild <mwild1@gmail.com>
parents: 4651
diff changeset
336 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
337 -- 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
338 -- (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
339 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
340 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
341 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
342 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
343 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
344 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
345 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
346 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
347
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
348 function api:send(stanza)
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
349 return core_post_stanza(hosts[self.host], stanza);
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
350 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
351
5899
26f54b462601 core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents: 5825
diff changeset
352 local timer_methods = { }
26f54b462601 core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents: 5825
diff changeset
353 local timer_mt = {
26f54b462601 core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents: 5825
diff changeset
354 __index = timer_methods;
26f54b462601 core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents: 5825
diff changeset
355 }
26f54b462601 core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents: 5825
diff changeset
356 function timer_methods:stop( )
26f54b462601 core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents: 5825
diff changeset
357 timer.stop(self.id);
26f54b462601 core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents: 5825
diff changeset
358 end
26f54b462601 core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents: 5825
diff changeset
359 timer_methods.disarm = timer_methods.stop
26f54b462601 core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents: 5825
diff changeset
360 function timer_methods:reschedule(delay)
26f54b462601 core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents: 5825
diff changeset
361 timer.reschedule(self.id, delay)
26f54b462601 core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents: 5825
diff changeset
362 end
26f54b462601 core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents: 5825
diff changeset
363
26f54b462601 core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents: 5825
diff changeset
364 local function timer_callback(now, id, t)
26f54b462601 core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents: 5825
diff changeset
365 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
366 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
367 end
26f54b462601 core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents: 5825
diff changeset
368
26f54b462601 core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents: 5825
diff changeset
369 function api:add_timer(delay, callback, ...)
26f54b462601 core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents: 5825
diff changeset
370 local t = pack(...)
26f54b462601 core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents: 5825
diff changeset
371 t.module_env = self;
26f54b462601 core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents: 5825
diff changeset
372 t.callback = callback;
26f54b462601 core/moduleapi: Return timer object from module:add_timer
daurnimator <quae@daurnimator.com>
parents: 5825
diff changeset
373 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
374 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
375 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
376
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
377 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
378 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
379 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
380 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
381
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
382 function api:load_resource(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
383 path = config.resolve_relative_path(self:get_directory(), path);
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
384 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
385 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
386
5496
7a0b81b5ca71 moduleapi: Add module:open_store() as a front-end to storagemanager.open()
Matthew Wild <mwild1@gmail.com>
parents: 5434
diff changeset
387 function api:open_store(name, type)
5498
2a67235e1d4d moduleapi: Make module:open_store() open a store named after the calling module by default
Kim Alvefur <zash@zash.se>
parents: 5496
diff changeset
388 return storagemanager.open(self.host, name or self.name, type);
5496
7a0b81b5ca71 moduleapi: Add module:open_store() as a front-end to storagemanager.open()
Matthew Wild <mwild1@gmail.com>
parents: 5434
diff changeset
389 end
7a0b81b5ca71 moduleapi: Add module:open_store() as a front-end to storagemanager.open()
Matthew Wild <mwild1@gmail.com>
parents: 5434
diff changeset
390
5053
375ab71e4b37 moduleapi: If path name ends with '-cache' create table as weak (keys and values)
Matthew Wild <mwild1@gmail.com>
parents: 5024
diff changeset
391 return api;