Software /
code /
prosody
Annotate
core/moduleapi.lua @ 4893:98ff89ab5d1d
moduleapi, modulemanager: Re-structure module.event_handlers so that the same handler can harmlessly handle multiple events (thanks Zash)
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 18 May 2012 23:53:02 +0100 |
parent | 4854:dab55c6f7710 |
child | 4896:27cda15104f2 |
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"; |
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 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
|
18 |
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 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
|
20 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
|
21 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
|
22 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
|
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; |
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
|
26 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
|
27 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 -- 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
|
29 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
|
30 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 local NULL = {}; |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 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
|
34 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 -- 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
|
36 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
|
37 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
|
38 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 -- 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
|
41 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
|
42 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
|
43 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 function api:get_host_type() |
4718
a9f6088a83e3
moduleapi: get_host_type(): Return nil for global modules (no host)
Matthew Wild <mwild1@gmail.com>
parents:
4708
diff
changeset
|
46 return self.host ~= "*" and hosts[self.host].type or nil; |
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 function api: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
|
50 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
|
51 -- 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
|
52 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
|
53 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
|
54 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
|
55 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
|
56 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 function api: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
|
59 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
|
60 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 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
|
62 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
|
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_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 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
|
66 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 function api: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 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
|
70 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 |
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
|
72 function api:hook_object_event(object, event, handler, priority) |
4893
98ff89ab5d1d
moduleapi, modulemanager: Re-structure module.event_handlers so that the same handler can harmlessly handle multiple events (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents:
4854
diff
changeset
|
73 local handlers = self.event_handlers[event]; |
98ff89ab5d1d
moduleapi, modulemanager: Re-structure module.event_handlers so that the same handler can harmlessly handle multiple events (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents:
4854
diff
changeset
|
74 if not handlers then |
98ff89ab5d1d
moduleapi, modulemanager: Re-structure module.event_handlers so that the same handler can harmlessly handle multiple events (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents:
4854
diff
changeset
|
75 handlers = {}; |
98ff89ab5d1d
moduleapi, modulemanager: Re-structure module.event_handlers so that the same handler can harmlessly handle multiple events (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents:
4854
diff
changeset
|
76 self.event_handlers[event] = handlers; |
98ff89ab5d1d
moduleapi, modulemanager: Re-structure module.event_handlers so that the same handler can harmlessly handle multiple events (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents:
4854
diff
changeset
|
77 end |
98ff89ab5d1d
moduleapi, modulemanager: Re-structure module.event_handlers so that the same handler can harmlessly handle multiple events (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents:
4854
diff
changeset
|
78 handlers[event] = { handler = handler, priority = priority, object = object }; |
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
|
79 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
|
80 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
|
81 |
4708
0e324923ff95
moduleapi: Fix parameters to unhook_object_event()
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
82 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
|
83 return object.remove_handler(event, handler); |
838ad61c6b2c
moduleapi: Add module:unhook_object_event()
Matthew Wild <mwild1@gmail.com>
parents:
4666
diff
changeset
|
84 end |
838ad61c6b2c
moduleapi: Add module:unhook_object_event()
Matthew Wild <mwild1@gmail.com>
parents:
4666
diff
changeset
|
85 |
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 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
|
87 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
|
88 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 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
|
91 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
|
92 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 |
4719
2087d42f1e77
moduleapi: Rename module:hook_stanza() -> module:hook_tag() (hook_stanza works for compat)
Matthew Wild <mwild1@gmail.com>
parents:
4718
diff
changeset
|
94 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
|
95 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
|
96 -- 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
|
97 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
|
98 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
|
99 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
|
100 return; |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
101 end |
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 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
|
103 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
|
104 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
|
105 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
106 function api: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
|
107 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
|
108 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
|
109 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
|
110 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
111 if not 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
|
112 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
|
113 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
|
114 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
115 |
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
|
116 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
|
117 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
|
118 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
|
119 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
|
120 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
|
121 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
|
122 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
|
123 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
|
124 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
|
125 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
|
126 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
|
127 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
|
128 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
|
129 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
|
130 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
|
131 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
|
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 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
|
134 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
|
135 and modulemanager.module_has_method(mod, "add_host") then |
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
|
136 mod = nil; -- This is a shared module, so we still want to load it on our host |
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
|
137 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
|
138 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
|
139 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
|
140 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
|
141 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
|
142 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
|
143 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
|
144 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
|
145 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
|
146 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
|
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 |
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
|
149 -- 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
|
150 -- 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
|
151 -- 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
|
152 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
|
153 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
|
154 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
|
155 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
|
156 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
|
157 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
|
158 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
|
159 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
|
160 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
|
161 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
|
162 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
|
163 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
|
164 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
|
165 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
|
166 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
|
167 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
|
168 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
|
169 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
|
170 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
|
171 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
|
172 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
|
173 |
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
174 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
|
175 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
|
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 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
|
178 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
|
179 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
|
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 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
182 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
|
183 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
184 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
185 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
|
186 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
|
187 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
|
188 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
|
189 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
|
190 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
191 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
|
192 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
193 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
|
194 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
|
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 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
|
197 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
198 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
199 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
|
200 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
|
201 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
|
202 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
|
203 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
|
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 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
|
206 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
207 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
|
208 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
|
209 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
|
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 ret; |
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_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
|
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 if value == nil then |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
223 return nil; |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
224 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
225 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
|
226 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
|
227 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
|
228 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
|
229 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
|
230 else |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
231 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
|
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 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
234 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
|
235 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
|
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 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
|
238 end |
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 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
|
241 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
|
242 |
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 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
|
244 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
|
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 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
|
248 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
|
249 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
250 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
251 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
|
252 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
253 |
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: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
|
255 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
|
256 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
257 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
|
258 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
|
259 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
260 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
261 return 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
|
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 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
264 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
|
265 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
|
266 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
|
267 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
|
268 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
|
269 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
270 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
|
271 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
|
272 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
|
273 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
|
274 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
|
275 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
|
276 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
|
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 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
279 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
280 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
281 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
|
282 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
|
283 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
|
284 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
|
285 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
|
286 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
|
287 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
|
288 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
289 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
290 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
291 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
|
292 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
|
293 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
|
294 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
|
295 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
|
296 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
297 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
298 end |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
299 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
|
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 |
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
302 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
|
303 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
|
304 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
|
305 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
|
306 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
|
307 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
|
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 |
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
|
312 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
|
313 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
|
314 if not item.name then |
4660
96b40b5e8ea8
moduleapi: module:provides(): Fix usage of wrong table
Matthew Wild <mwild1@gmail.com>
parents:
4651
diff
changeset
|
315 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
|
316 -- 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
|
317 -- (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
|
318 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
|
319 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
|
320 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
|
321 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
|
322 end |
4661
76db5d0a2104
moduleapi: module:provides(): Add "-provider" onto the key name
Matthew Wild <mwild1@gmail.com>
parents:
4660
diff
changeset
|
323 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
|
324 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
|
325 |
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
|
326 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
|
327 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
|
328 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
|
329 |
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
|
330 function api:add_timer(delay, callback) |
fb522fbd495e
moduleapi: Add module:add_timer(delay, callback) - automatically halts the timer on module unload
Matthew Wild <mwild1@gmail.com>
parents:
4663
diff
changeset
|
331 return timer.add_task(delay, function (t) |
fb522fbd495e
moduleapi: Add module:add_timer(delay, callback) - automatically halts the timer on module unload
Matthew Wild <mwild1@gmail.com>
parents:
4663
diff
changeset
|
332 if self.loaded == false then return; 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
|
333 return callback(t); |
fb522fbd495e
moduleapi: Add module:add_timer(delay, callback) - automatically halts the timer on module unload
Matthew Wild <mwild1@gmail.com>
parents:
4663
diff
changeset
|
334 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
|
335 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
|
336 |
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
|
337 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
|
338 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
|
339 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
|
340 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
|
341 |
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
|
342 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
|
343 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
|
344 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
|
345 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
|
346 |
4531
c778ce7e3c78
modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
347 return api; |