Software /
code /
prosody
Annotate
core/moduleapi.lua @ 4614:20940729c1b4
moduleapi: Add module:send() as an alias for core_post_stanza() from the current host's origin
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 15 Mar 2012 02:53:05 +0000 |
parent | 4613:27fbc9c69eb5 |
child | 4651:d1739d72100a |
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 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 -- |
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"; |
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 modulemanager = require "modulemanager"; |
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"; |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 local multitable_new = require "util.multitable".new; |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 local t_insert, t_remove, t_concat = table.insert, table.remove, table.concat; |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 local error, setmetatable, setfenv, type = error, setmetatable, setfenv, type; |
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 ipairs, pairs, select, unpack = ipairs, pairs, select, unpack; |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 local tonumber, tostring = tonumber, tostring; |
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 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
|
24 local hosts = prosody.hosts; |
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
|
25 local core_post_stanza = prosody.core_post_stanza; |
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 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 -- 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
|
28 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
|
29 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 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
|
31 |
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 api = {}; |
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 -- 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
|
35 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
|
36 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
|
37 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 -- 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
|
40 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
|
41 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
|
42 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 function api:get_host_type() |
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 hosts[self.host].type; |
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: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
|
49 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
|
50 -- 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
|
51 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
|
52 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
|
53 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
|
54 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
|
55 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 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
|
58 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
|
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 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
|
61 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
|
62 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 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
|
64 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
|
65 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 |
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: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
|
68 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
|
69 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 |
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
|
71 function api:hook_object_event(object, 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
|
72 self.event_handlers[handler] = { name = event, priority = priority, object = object }; |
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
|
73 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
|
74 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
|
75 |
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 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
|
77 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
|
78 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 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
|
81 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
|
82 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 function api:hook_stanza(xmlns, name, handler, priority) |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 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
|
86 -- 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
|
87 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
|
88 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
|
89 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
|
90 return; |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 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
|
93 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
94 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
95 function api:require(lib) |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
96 local f, n = pluginloader.load_code(self.name, lib..".lib.lua"); |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
97 if not f then |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
98 f, n = pluginloader.load_code(lib, lib..".lib.lua"); |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
99 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
100 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
|
101 setfenv(f, self.environment); |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
102 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
|
103 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
104 |
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
|
105 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
|
106 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
|
107 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
|
108 self:hook("module-reloaded", 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
|
109 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
|
110 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
|
111 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
|
112 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
|
113 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
|
114 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
|
115 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
|
116 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
|
117 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
|
118 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
|
119 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
|
120 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
|
121 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
|
122 local mod = modulemanager.get_module(self.host, name) or modulemanager.get_module("*", 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
|
123 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
|
124 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
|
125 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
|
126 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
|
127 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
|
128 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
|
129 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
|
130 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
|
131 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
|
132 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
|
133 |
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
|
134 -- 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
|
135 -- 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
|
136 -- 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
|
137 function api: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
|
138 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
|
139 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
|
140 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
|
141 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
|
142 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
|
143 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
|
144 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
|
145 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
|
146 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
|
147 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
|
148 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
|
149 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
|
150 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
|
151 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
|
152 t_insert(data_array, 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
|
153 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
|
154 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
|
155 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
|
156 |
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
157 function api: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
|
158 local value = config.get(self.host, self.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
|
159 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
|
160 value = config.get(self.host, "core", name); |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
161 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
|
162 value = 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
|
163 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
164 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
165 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
|
166 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
167 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
168 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
|
169 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
|
170 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
|
171 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
|
172 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
|
173 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
174 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
|
175 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
176 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
|
177 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
|
178 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
179 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
|
180 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
181 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
182 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
|
183 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
|
184 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
|
185 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
|
186 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
|
187 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
188 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
|
189 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
190 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
|
191 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
|
192 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
|
193 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
194 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
|
195 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
196 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
197 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
|
198 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
|
199 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
|
200 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
|
201 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
|
202 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
203 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
|
204 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
205 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
|
206 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
|
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 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
|
209 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
|
210 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
|
211 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
|
212 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
|
213 else |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
214 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
|
215 end |
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 |
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 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
|
218 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
|
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 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
|
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 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
223 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
|
224 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
|
225 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
226 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
|
227 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
|
228 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
229 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
230 if type(value) ~= "table" then |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
231 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
|
232 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
233 |
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 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
|
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_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
|
238 local value = self: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
|
239 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
240 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
|
241 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
|
242 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
243 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
244 return 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
|
245 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
246 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
247 local module_items = multitable_new(); |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
248 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
|
249 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
|
250 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
|
251 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
|
252 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
|
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 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
|
255 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
|
256 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
|
257 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
|
258 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
|
259 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
|
260 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
|
261 end |
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 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
263 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
264 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
265 function api:get_host_items(key) |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
266 local result = {}; |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
267 for mod_name, module in pairs(modulemanager.get_modules(self.host)) do |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
268 module = module.module; |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
269 if module.items then |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
270 for _, item in ipairs(module.items[key] or NULL) do |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
271 t_insert(result, item); |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
272 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
273 end |
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 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
275 for mod_name, module in pairs(modulemanager.get_modules("*")) do |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
276 module = module.module; |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
277 if module.items then |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
278 for _, item in ipairs(module.items[key] or NULL) do |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
279 t_insert(result, item); |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
280 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
281 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
282 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
283 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
|
284 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
285 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
286 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
|
287 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
|
288 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
|
289 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
|
290 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
|
291 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
|
292 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
293 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
294 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
295 |
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
|
296 function api:provides(name, item) |
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
|
297 if not item then item = self.environment; 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
|
298 if not item.name 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
|
299 local item_name = module.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
|
300 -- 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
|
301 -- (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
|
302 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
|
303 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
|
304 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
|
305 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
|
306 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
|
307 self:add_item(name, item); |
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
|
308 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
|
309 |
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
|
310 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
|
311 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
|
312 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
|
313 |
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 api; |