Software /
code /
prosody
Annotate
core/modulemanager.lua @ 1386:9132f16666e4
modulemanager: Fix copy/paste error, should be name instead of module_name
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Mon, 22 Jun 2009 21:54:34 +0100 |
parent | 1378:d09ecc8ee1ef |
child | 1389:846df07536eb |
rev | line source |
---|---|
896 | 1 -- Prosody IM v0.4 |
760
90ce865eebd8
Update copyright notices for 2009
Matthew Wild <mwild1@gmail.com>
parents:
759
diff
changeset
|
2 -- Copyright (C) 2008-2009 Matthew Wild |
90ce865eebd8
Update copyright notices for 2009
Matthew Wild <mwild1@gmail.com>
parents:
759
diff
changeset
|
3 -- Copyright (C) 2008-2009 Waqas Hussain |
519
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
467
diff
changeset
|
4 -- |
758 | 5 -- This project is MIT/X11 licensed. Please see the |
6 -- COPYING file in the source package for more information. | |
519
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
467
diff
changeset
|
7 -- |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
467
diff
changeset
|
8 |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
467
diff
changeset
|
9 |
30 | 10 |
467
66f145f5c932
Update Makefile to now pass config paths to prosody. Update prosody, modulemanager and connectionlisteners to obey these paths.
Matthew Wild <mwild1@gmail.com>
parents:
439
diff
changeset
|
11 local plugin_dir = CFG_PLUGINDIR or "./plugins/"; |
438
193f9dd64f17
Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents:
400
diff
changeset
|
12 |
193f9dd64f17
Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents:
400
diff
changeset
|
13 local logger = require "util.logger"; |
540
ec03f6968fa8
Added function add_feature to modules API (for adding disco features)
Waqas Hussain <waqas20@gmail.com>
parents:
519
diff
changeset
|
14 local log = logger.init("modulemanager"); |
ec03f6968fa8
Added function add_feature to modules API (for adding disco features)
Waqas Hussain <waqas20@gmail.com>
parents:
519
diff
changeset
|
15 local addDiscoInfoHandler = require "core.discomanager".addDiscoInfoHandler; |
569
5216efe6088b
Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
540
diff
changeset
|
16 local eventmanager = require "core.eventmanager"; |
573
f6555ebf84ec
Move module loading to modulemanager
Matthew Wild <mwild1@gmail.com>
parents:
569
diff
changeset
|
17 local config = require "core.configmanager"; |
578
5879264e28e2
Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents:
573
diff
changeset
|
18 local multitable_new = require "util.multitable".new; |
698
d8a678e40a0a
Add core.actions for managing server 'actions'; and make modulemanager register actions 'load' and 'unload'
Matthew Wild <mwild1@gmail.com>
parents:
695
diff
changeset
|
19 local register_actions = require "core.actions".register; |
1174
f7b6d5839092
modulemanager: require util.stanza
Waqas Hussain <waqas20@gmail.com>
parents:
1173
diff
changeset
|
20 local st = require "util.stanza"; |
1360
857034905016
modulemanager: Changed to use util.pluginloader
Waqas Hussain <waqas20@gmail.com>
parents:
1346
diff
changeset
|
21 local pluginloader = require "util.pluginloader"; |
30 | 22 |
747
40837f3422ab
modulemanager: Add get_host_type() API method, and fix up call_module_method to work properly
Matthew Wild <mwild1@gmail.com>
parents:
746
diff
changeset
|
23 local hosts = hosts; |
1247
4721e4124692
modulemanager: module:hook now allows global modules to hook events on the prosody.events object
Waqas Hussain <waqas20@gmail.com>
parents:
1231
diff
changeset
|
24 local prosody = prosody; |
747
40837f3422ab
modulemanager: Add get_host_type() API method, and fix up call_module_method to work properly
Matthew Wild <mwild1@gmail.com>
parents:
746
diff
changeset
|
25 |
30 | 26 local loadfile, pcall = loadfile, pcall; |
27 local setmetatable, setfenv, getfenv = setmetatable, setfenv, getfenv; | |
28 local pairs, ipairs = pairs, ipairs; | |
745
5a343599cd3e
core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents:
733
diff
changeset
|
29 local t_insert, t_concat = table.insert, table.concat; |
30 | 30 local type = type; |
670
d5cf10b7fc44
Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents:
637
diff
changeset
|
31 local next = next; |
674
4f506c627b49
modulemanager: module.unload now gets called when modules are being unloaded
Waqas Hussain <waqas20@gmail.com>
parents:
670
diff
changeset
|
32 local rawget = rawget; |
30 | 33 |
1105
965a55db3732
modulemanager: No need for print()
Matthew Wild <mwild1@gmail.com>
parents:
1094
diff
changeset
|
34 local tostring = tostring; |
30 | 35 |
467
66f145f5c932
Update Makefile to now pass config paths to prosody. Update prosody, modulemanager and connectionlisteners to obey these paths.
Matthew Wild <mwild1@gmail.com>
parents:
439
diff
changeset
|
36 -- We need this to let modules access the real global namespace |
30 | 37 local _G = _G; |
38 | |
39 module "modulemanager" | |
40 | |
438
193f9dd64f17
Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents:
400
diff
changeset
|
41 local api = {}; -- Module API container |
193f9dd64f17
Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents:
400
diff
changeset
|
42 |
584
eb0cea29c8d7
Temporary hack for global modules
Matthew Wild <mwild1@gmail.com>
parents:
579
diff
changeset
|
43 local modulemap = { ["*"] = {} }; |
438
193f9dd64f17
Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents:
400
diff
changeset
|
44 |
591 | 45 local stanza_handlers = multitable_new(); |
30 | 46 local handler_info = {}; |
438
193f9dd64f17
Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents:
400
diff
changeset
|
47 |
30 | 48 local modulehelpers = setmetatable({}, { __index = _G }); |
49 | |
670
d5cf10b7fc44
Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents:
637
diff
changeset
|
50 local features_table = multitable_new(); |
1334
6846c5cc9ce2
modulemanager: Added function add_identity(category, type) to the modules API
Waqas Hussain <waqas20@gmail.com>
parents:
1330
diff
changeset
|
51 local identities_table = multitable_new(); |
670
d5cf10b7fc44
Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents:
637
diff
changeset
|
52 local handler_table = multitable_new(); |
686
13ed38531f69
modulemanager: Per-host event hooks for plugins - solves issue 41
Waqas Hussain <waqas20@gmail.com>
parents:
675
diff
changeset
|
53 local hooked = multitable_new(); |
1259
6bd11bca9725
modulemanager: Keep track of event handlers added by module:hook, and remove them on module unload
Waqas Hussain <waqas20@gmail.com>
parents:
1253
diff
changeset
|
54 local hooks = multitable_new(); |
686
13ed38531f69
modulemanager: Per-host event hooks for plugins - solves issue 41
Waqas Hussain <waqas20@gmail.com>
parents:
675
diff
changeset
|
55 local event_hooks = multitable_new(); |
13ed38531f69
modulemanager: Per-host event hooks for plugins - solves issue 41
Waqas Hussain <waqas20@gmail.com>
parents:
675
diff
changeset
|
56 |
670
d5cf10b7fc44
Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents:
637
diff
changeset
|
57 local NULL = {}; |
d5cf10b7fc44
Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents:
637
diff
changeset
|
58 |
573
f6555ebf84ec
Move module loading to modulemanager
Matthew Wild <mwild1@gmail.com>
parents:
569
diff
changeset
|
59 -- Load modules when a host is activated |
f6555ebf84ec
Move module loading to modulemanager
Matthew Wild <mwild1@gmail.com>
parents:
569
diff
changeset
|
60 function load_modules_for_host(host) |
1094
a619525fed5d
modulemanager: Don't load modules when modules_enable is false
Matthew Wild <mwild1@gmail.com>
parents:
1069
diff
changeset
|
61 if config.get(host, "core", "modules_enable") == false then |
a619525fed5d
modulemanager: Don't load modules when modules_enable is false
Matthew Wild <mwild1@gmail.com>
parents:
1069
diff
changeset
|
62 return; -- Only load for hosts, not components, etc. |
a619525fed5d
modulemanager: Don't load modules when modules_enable is false
Matthew Wild <mwild1@gmail.com>
parents:
1069
diff
changeset
|
63 end |
a619525fed5d
modulemanager: Don't load modules when modules_enable is false
Matthew Wild <mwild1@gmail.com>
parents:
1069
diff
changeset
|
64 |
637
30b8ad9f7b70
Fix for not loading global modules when host-specific modules are specified in config
Matthew Wild <mwild1@gmail.com>
parents:
615
diff
changeset
|
65 -- Load modules from global section |
30b8ad9f7b70
Fix for not loading global modules when host-specific modules are specified in config
Matthew Wild <mwild1@gmail.com>
parents:
615
diff
changeset
|
66 local modules_enabled = config.get("*", "core", "modules_enabled"); |
30b8ad9f7b70
Fix for not loading global modules when host-specific modules are specified in config
Matthew Wild <mwild1@gmail.com>
parents:
615
diff
changeset
|
67 local modules_disabled = config.get(host, "core", "modules_disabled"); |
30b8ad9f7b70
Fix for not loading global modules when host-specific modules are specified in config
Matthew Wild <mwild1@gmail.com>
parents:
615
diff
changeset
|
68 local disabled_set = {}; |
30b8ad9f7b70
Fix for not loading global modules when host-specific modules are specified in config
Matthew Wild <mwild1@gmail.com>
parents:
615
diff
changeset
|
69 if modules_enabled then |
30b8ad9f7b70
Fix for not loading global modules when host-specific modules are specified in config
Matthew Wild <mwild1@gmail.com>
parents:
615
diff
changeset
|
70 if modules_disabled then |
695
3384f2784795
modulemanager: Change pairs() to ipairs() to allow ordered module loading
Matthew Wild <mwild1@gmail.com>
parents:
686
diff
changeset
|
71 for _, module in ipairs(modules_disabled) do |
637
30b8ad9f7b70
Fix for not loading global modules when host-specific modules are specified in config
Matthew Wild <mwild1@gmail.com>
parents:
615
diff
changeset
|
72 disabled_set[module] = true; |
30b8ad9f7b70
Fix for not loading global modules when host-specific modules are specified in config
Matthew Wild <mwild1@gmail.com>
parents:
615
diff
changeset
|
73 end |
30b8ad9f7b70
Fix for not loading global modules when host-specific modules are specified in config
Matthew Wild <mwild1@gmail.com>
parents:
615
diff
changeset
|
74 end |
1309
a544e68a0989
modulemanager: Load mod_message, mod_iq and mod_presence implicitly
Waqas Hussain <waqas20@gmail.com>
parents:
1259
diff
changeset
|
75 for _, module in ipairs({"presence", "message", "iq"}) do |
a544e68a0989
modulemanager: Load mod_message, mod_iq and mod_presence implicitly
Waqas Hussain <waqas20@gmail.com>
parents:
1259
diff
changeset
|
76 if not disabled_set[module] then |
a544e68a0989
modulemanager: Load mod_message, mod_iq and mod_presence implicitly
Waqas Hussain <waqas20@gmail.com>
parents:
1259
diff
changeset
|
77 load(host, module); |
a544e68a0989
modulemanager: Load mod_message, mod_iq and mod_presence implicitly
Waqas Hussain <waqas20@gmail.com>
parents:
1259
diff
changeset
|
78 end |
a544e68a0989
modulemanager: Load mod_message, mod_iq and mod_presence implicitly
Waqas Hussain <waqas20@gmail.com>
parents:
1259
diff
changeset
|
79 end |
695
3384f2784795
modulemanager: Change pairs() to ipairs() to allow ordered module loading
Matthew Wild <mwild1@gmail.com>
parents:
686
diff
changeset
|
80 for _, module in ipairs(modules_enabled) do |
1309
a544e68a0989
modulemanager: Load mod_message, mod_iq and mod_presence implicitly
Waqas Hussain <waqas20@gmail.com>
parents:
1259
diff
changeset
|
81 if not disabled_set[module] and not is_loaded(host, module) then |
637
30b8ad9f7b70
Fix for not loading global modules when host-specific modules are specified in config
Matthew Wild <mwild1@gmail.com>
parents:
615
diff
changeset
|
82 load(host, module); |
30b8ad9f7b70
Fix for not loading global modules when host-specific modules are specified in config
Matthew Wild <mwild1@gmail.com>
parents:
615
diff
changeset
|
83 end |
30b8ad9f7b70
Fix for not loading global modules when host-specific modules are specified in config
Matthew Wild <mwild1@gmail.com>
parents:
615
diff
changeset
|
84 end |
30b8ad9f7b70
Fix for not loading global modules when host-specific modules are specified in config
Matthew Wild <mwild1@gmail.com>
parents:
615
diff
changeset
|
85 end |
30b8ad9f7b70
Fix for not loading global modules when host-specific modules are specified in config
Matthew Wild <mwild1@gmail.com>
parents:
615
diff
changeset
|
86 |
30b8ad9f7b70
Fix for not loading global modules when host-specific modules are specified in config
Matthew Wild <mwild1@gmail.com>
parents:
615
diff
changeset
|
87 -- Load modules from just this host |
573
f6555ebf84ec
Move module loading to modulemanager
Matthew Wild <mwild1@gmail.com>
parents:
569
diff
changeset
|
88 local modules_enabled = config.get(host, "core", "modules_enabled"); |
f6555ebf84ec
Move module loading to modulemanager
Matthew Wild <mwild1@gmail.com>
parents:
569
diff
changeset
|
89 if modules_enabled then |
f6555ebf84ec
Move module loading to modulemanager
Matthew Wild <mwild1@gmail.com>
parents:
569
diff
changeset
|
90 for _, module in pairs(modules_enabled) do |
672
7db1044d2fab
Remove warning of already-loaded modules at startup
Matthew Wild <mwild1@gmail.com>
parents:
670
diff
changeset
|
91 if not is_loaded(host, module) then |
7db1044d2fab
Remove warning of already-loaded modules at startup
Matthew Wild <mwild1@gmail.com>
parents:
670
diff
changeset
|
92 load(host, module); |
7db1044d2fab
Remove warning of already-loaded modules at startup
Matthew Wild <mwild1@gmail.com>
parents:
670
diff
changeset
|
93 end |
573
f6555ebf84ec
Move module loading to modulemanager
Matthew Wild <mwild1@gmail.com>
parents:
569
diff
changeset
|
94 end |
f6555ebf84ec
Move module loading to modulemanager
Matthew Wild <mwild1@gmail.com>
parents:
569
diff
changeset
|
95 end |
f6555ebf84ec
Move module loading to modulemanager
Matthew Wild <mwild1@gmail.com>
parents:
569
diff
changeset
|
96 end |
f6555ebf84ec
Move module loading to modulemanager
Matthew Wild <mwild1@gmail.com>
parents:
569
diff
changeset
|
97 eventmanager.add_event_hook("host-activated", load_modules_for_host); |
f6555ebf84ec
Move module loading to modulemanager
Matthew Wild <mwild1@gmail.com>
parents:
569
diff
changeset
|
98 -- |
30 | 99 |
438
193f9dd64f17
Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents:
400
diff
changeset
|
100 function load(host, module_name, config) |
439
6608ad3a72f3
is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents:
438
diff
changeset
|
101 if not (host and module_name) then |
6608ad3a72f3
is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents:
438
diff
changeset
|
102 return nil, "insufficient-parameters"; |
6608ad3a72f3
is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents:
438
diff
changeset
|
103 end |
30 | 104 |
438
193f9dd64f17
Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents:
400
diff
changeset
|
105 if not modulemap[host] then |
193f9dd64f17
Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents:
400
diff
changeset
|
106 modulemap[host] = {}; |
769
9e76018c62fa
Fix for never checking if the first module for a host is already loaded (affects global modules)
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
107 end |
9e76018c62fa
Fix for never checking if the first module for a host is already loaded (affects global modules)
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
108 |
9e76018c62fa
Fix for never checking if the first module for a host is already loaded (affects global modules)
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
109 if modulemap[host][module_name] then |
438
193f9dd64f17
Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents:
400
diff
changeset
|
110 log("warn", "%s is already loaded for %s, so not loading again", module_name, host); |
193f9dd64f17
Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents:
400
diff
changeset
|
111 return nil, "module-already-loaded"; |
584
eb0cea29c8d7
Temporary hack for global modules
Matthew Wild <mwild1@gmail.com>
parents:
579
diff
changeset
|
112 elseif modulemap["*"][module_name] then |
eb0cea29c8d7
Temporary hack for global modules
Matthew Wild <mwild1@gmail.com>
parents:
579
diff
changeset
|
113 return nil, "global-module-already-loaded"; |
438
193f9dd64f17
Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents:
400
diff
changeset
|
114 end |
193f9dd64f17
Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents:
400
diff
changeset
|
115 |
584
eb0cea29c8d7
Temporary hack for global modules
Matthew Wild <mwild1@gmail.com>
parents:
579
diff
changeset
|
116 |
1360
857034905016
modulemanager: Changed to use util.pluginloader
Waqas Hussain <waqas20@gmail.com>
parents:
1346
diff
changeset
|
117 local mod, err = pluginloader.load_code(module_name); |
584
eb0cea29c8d7
Temporary hack for global modules
Matthew Wild <mwild1@gmail.com>
parents:
579
diff
changeset
|
118 if not mod then |
eb0cea29c8d7
Temporary hack for global modules
Matthew Wild <mwild1@gmail.com>
parents:
579
diff
changeset
|
119 log("error", "Unable to load module '%s': %s", module_name or "nil", err or "nil"); |
eb0cea29c8d7
Temporary hack for global modules
Matthew Wild <mwild1@gmail.com>
parents:
579
diff
changeset
|
120 return nil, err; |
eb0cea29c8d7
Temporary hack for global modules
Matthew Wild <mwild1@gmail.com>
parents:
579
diff
changeset
|
121 end |
eb0cea29c8d7
Temporary hack for global modules
Matthew Wild <mwild1@gmail.com>
parents:
579
diff
changeset
|
122 |
438
193f9dd64f17
Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents:
400
diff
changeset
|
123 local _log = logger.init(host..":"..module_name); |
193f9dd64f17
Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents:
400
diff
changeset
|
124 local api_instance = setmetatable({ name = module_name, host = host, config = config, _log = _log, log = function (self, ...) return _log(...); end }, { __index = api }); |
193f9dd64f17
Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents:
400
diff
changeset
|
125 |
193f9dd64f17
Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents:
400
diff
changeset
|
126 local pluginenv = setmetatable({ module = api_instance }, { __index = _G }); |
30 | 127 |
128 setfenv(mod, pluginenv); | |
778
0d94b4903cc7
modulemanager initializes hosts[host] if it isn't already initialized when loading a module.
Waqas Hussain <waqas20@gmail.com>
parents:
769
diff
changeset
|
129 if not hosts[host] then hosts[host] = { type = "component", host = host, connected = false, s2sout = {} }; end |
438
193f9dd64f17
Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents:
400
diff
changeset
|
130 |
30 | 131 local success, ret = pcall(mod); |
132 if not success then | |
1330
baad431dabc5
modulemanager: Use module_name in log entry to fix 'error initialising module "nil"' error
Matthew Wild <mwild1@gmail.com>
parents:
1328
diff
changeset
|
133 log("error", "Error initialising module '%s': %s", module_name or "nil", ret or "nil"); |
391
79bd7a3e906c
Typo prevented modulemanager.load() from returning the error if load failed.
Matthew Wild <mwild1@gmail.com>
parents:
385
diff
changeset
|
134 return nil, ret; |
30 | 135 end |
438
193f9dd64f17
Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents:
400
diff
changeset
|
136 |
1378
d09ecc8ee1ef
modulemanager: Call 'load' method when loading a module
Matthew Wild <mwild1@gmail.com>
parents:
1361
diff
changeset
|
137 if module_has_method(pluginenv, "load") then |
d09ecc8ee1ef
modulemanager: Call 'load' method when loading a module
Matthew Wild <mwild1@gmail.com>
parents:
1361
diff
changeset
|
138 local ok, err = call_module_method(pluginenv, "load"); |
d09ecc8ee1ef
modulemanager: Call 'load' method when loading a module
Matthew Wild <mwild1@gmail.com>
parents:
1361
diff
changeset
|
139 if (not ok) and err then |
d09ecc8ee1ef
modulemanager: Call 'load' method when loading a module
Matthew Wild <mwild1@gmail.com>
parents:
1361
diff
changeset
|
140 log("warn", "Error loading module '%s' on '%s': %s", module_name, host, err); |
d09ecc8ee1ef
modulemanager: Call 'load' method when loading a module
Matthew Wild <mwild1@gmail.com>
parents:
1361
diff
changeset
|
141 end |
d09ecc8ee1ef
modulemanager: Call 'load' method when loading a module
Matthew Wild <mwild1@gmail.com>
parents:
1361
diff
changeset
|
142 end |
d09ecc8ee1ef
modulemanager: Call 'load' method when loading a module
Matthew Wild <mwild1@gmail.com>
parents:
1361
diff
changeset
|
143 |
584
eb0cea29c8d7
Temporary hack for global modules
Matthew Wild <mwild1@gmail.com>
parents:
579
diff
changeset
|
144 -- Use modified host, if the module set one |
670
d5cf10b7fc44
Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents:
637
diff
changeset
|
145 modulemap[api_instance.host][module_name] = pluginenv; |
438
193f9dd64f17
Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents:
400
diff
changeset
|
146 |
1069
034e345c0f8d
modulemanager: Update logger when a module becomes global
Matthew Wild <mwild1@gmail.com>
parents:
929
diff
changeset
|
147 if api_instance.host == "*" and host ~= "*" then |
034e345c0f8d
modulemanager: Update logger when a module becomes global
Matthew Wild <mwild1@gmail.com>
parents:
929
diff
changeset
|
148 api_instance:set_global(); |
034e345c0f8d
modulemanager: Update logger when a module becomes global
Matthew Wild <mwild1@gmail.com>
parents:
929
diff
changeset
|
149 end |
034e345c0f8d
modulemanager: Update logger when a module becomes global
Matthew Wild <mwild1@gmail.com>
parents:
929
diff
changeset
|
150 |
229
01bd24ea488d
We now fail if modules fail to load at startup.
Waqas Hussain <waqas20@gmail.com>
parents:
218
diff
changeset
|
151 return true; |
30 | 152 end |
153 | |
745
5a343599cd3e
core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents:
733
diff
changeset
|
154 function get_module(host, name) |
5a343599cd3e
core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents:
733
diff
changeset
|
155 return modulemap[host] and modulemap[host][name]; |
5a343599cd3e
core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents:
733
diff
changeset
|
156 end |
5a343599cd3e
core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents:
733
diff
changeset
|
157 |
439
6608ad3a72f3
is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents:
438
diff
changeset
|
158 function is_loaded(host, name) |
6608ad3a72f3
is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents:
438
diff
changeset
|
159 return modulemap[host] and modulemap[host][name] and true; |
6608ad3a72f3
is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents:
438
diff
changeset
|
160 end |
6608ad3a72f3
is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents:
438
diff
changeset
|
161 |
6608ad3a72f3
is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents:
438
diff
changeset
|
162 function unload(host, name, ...) |
745
5a343599cd3e
core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents:
733
diff
changeset
|
163 local mod = get_module(host, name); |
439
6608ad3a72f3
is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents:
438
diff
changeset
|
164 if not mod then return nil, "module-not-loaded"; end |
6608ad3a72f3
is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents:
438
diff
changeset
|
165 |
745
5a343599cd3e
core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents:
733
diff
changeset
|
166 if module_has_method(mod, "unload") then |
5a343599cd3e
core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents:
733
diff
changeset
|
167 local ok, err = call_module_method(mod, "unload"); |
439
6608ad3a72f3
is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents:
438
diff
changeset
|
168 if (not ok) and err then |
745
5a343599cd3e
core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents:
733
diff
changeset
|
169 log("warn", "Non-fatal error unloading module '%s' on '%s': %s", name, host, err); |
439
6608ad3a72f3
is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents:
438
diff
changeset
|
170 end |
674
4f506c627b49
modulemanager: module.unload now gets called when modules are being unloaded
Waqas Hussain <waqas20@gmail.com>
parents:
670
diff
changeset
|
171 end |
670
d5cf10b7fc44
Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents:
637
diff
changeset
|
172 modulemap[host][name] = nil; |
d5cf10b7fc44
Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents:
637
diff
changeset
|
173 features_table:remove(host, name); |
1334
6846c5cc9ce2
modulemanager: Added function add_identity(category, type) to the modules API
Waqas Hussain <waqas20@gmail.com>
parents:
1330
diff
changeset
|
174 identities_table:remove(host, name); |
670
d5cf10b7fc44
Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents:
637
diff
changeset
|
175 local params = handler_table:get(host, name); -- , {module.host, origin_type, tag, xmlns} |
708
b72d408f5f15
modulemanager: Fixed error on unloading modules with no handlers
Waqas Hussain <waqas20@gmail.com>
parents:
686
diff
changeset
|
176 for _, param in pairs(params or NULL) do |
670
d5cf10b7fc44
Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents:
637
diff
changeset
|
177 local handlers = stanza_handlers:get(param[1], param[2], param[3], param[4]); |
708
b72d408f5f15
modulemanager: Fixed error on unloading modules with no handlers
Waqas Hussain <waqas20@gmail.com>
parents:
686
diff
changeset
|
178 if handlers then |
b72d408f5f15
modulemanager: Fixed error on unloading modules with no handlers
Waqas Hussain <waqas20@gmail.com>
parents:
686
diff
changeset
|
179 handler_info[handlers[1]] = nil; |
b72d408f5f15
modulemanager: Fixed error on unloading modules with no handlers
Waqas Hussain <waqas20@gmail.com>
parents:
686
diff
changeset
|
180 stanza_handlers:remove(param[1], param[2], param[3], param[4]); |
b72d408f5f15
modulemanager: Fixed error on unloading modules with no handlers
Waqas Hussain <waqas20@gmail.com>
parents:
686
diff
changeset
|
181 end |
439
6608ad3a72f3
is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents:
438
diff
changeset
|
182 end |
686
13ed38531f69
modulemanager: Per-host event hooks for plugins - solves issue 41
Waqas Hussain <waqas20@gmail.com>
parents:
675
diff
changeset
|
183 event_hooks:remove(host, name); |
1259
6bd11bca9725
modulemanager: Keep track of event handlers added by module:hook, and remove them on module unload
Waqas Hussain <waqas20@gmail.com>
parents:
1253
diff
changeset
|
184 -- unhook event handlers hooked by module:hook |
6bd11bca9725
modulemanager: Keep track of event handlers added by module:hook, and remove them on module unload
Waqas Hussain <waqas20@gmail.com>
parents:
1253
diff
changeset
|
185 for event, handlers in pairs(hooks:get(host, name) or NULL) do |
6bd11bca9725
modulemanager: Keep track of event handlers added by module:hook, and remove them on module unload
Waqas Hussain <waqas20@gmail.com>
parents:
1253
diff
changeset
|
186 for handler in pairs(handlers or NULL) do |
6bd11bca9725
modulemanager: Keep track of event handlers added by module:hook, and remove them on module unload
Waqas Hussain <waqas20@gmail.com>
parents:
1253
diff
changeset
|
187 (hosts[host] or prosody).events.remove_handler(event, handler); |
6bd11bca9725
modulemanager: Keep track of event handlers added by module:hook, and remove them on module unload
Waqas Hussain <waqas20@gmail.com>
parents:
1253
diff
changeset
|
188 end |
6bd11bca9725
modulemanager: Keep track of event handlers added by module:hook, and remove them on module unload
Waqas Hussain <waqas20@gmail.com>
parents:
1253
diff
changeset
|
189 end |
6bd11bca9725
modulemanager: Keep track of event handlers added by module:hook, and remove them on module unload
Waqas Hussain <waqas20@gmail.com>
parents:
1253
diff
changeset
|
190 hooks:remove(host, name); |
670
d5cf10b7fc44
Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents:
637
diff
changeset
|
191 return true; |
439
6608ad3a72f3
is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents:
438
diff
changeset
|
192 end |
6608ad3a72f3
is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents:
438
diff
changeset
|
193 |
710
56f6c115bc69
modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents:
709
diff
changeset
|
194 function reload(host, name, ...) |
745
5a343599cd3e
core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents:
733
diff
changeset
|
195 local mod = get_module(host, name); |
710
56f6c115bc69
modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents:
709
diff
changeset
|
196 if not mod then return nil, "module-not-loaded"; end |
56f6c115bc69
modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents:
709
diff
changeset
|
197 |
1360
857034905016
modulemanager: Changed to use util.pluginloader
Waqas Hussain <waqas20@gmail.com>
parents:
1346
diff
changeset
|
198 local _mod, err = pluginloader.load_code(name); -- checking for syntax errors |
713
2afd6d9e21cd
modulemanager: Check for syntax errors before reloading a module
Waqas Hussain <waqas20@gmail.com>
parents:
710
diff
changeset
|
199 if not _mod then |
1386
9132f16666e4
modulemanager: Fix copy/paste error, should be name instead of module_name
Matthew Wild <mwild1@gmail.com>
parents:
1378
diff
changeset
|
200 log("error", "Unable to load module '%s': %s", name or "nil", err or "nil"); |
713
2afd6d9e21cd
modulemanager: Check for syntax errors before reloading a module
Waqas Hussain <waqas20@gmail.com>
parents:
710
diff
changeset
|
201 return nil, err; |
2afd6d9e21cd
modulemanager: Check for syntax errors before reloading a module
Waqas Hussain <waqas20@gmail.com>
parents:
710
diff
changeset
|
202 end |
2afd6d9e21cd
modulemanager: Check for syntax errors before reloading a module
Waqas Hussain <waqas20@gmail.com>
parents:
710
diff
changeset
|
203 |
710
56f6c115bc69
modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents:
709
diff
changeset
|
204 local saved; |
745
5a343599cd3e
core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents:
733
diff
changeset
|
205 |
5a343599cd3e
core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents:
733
diff
changeset
|
206 if module_has_method(mod, "save") then |
5a343599cd3e
core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents:
733
diff
changeset
|
207 local ok, ret, err = call_module_method(mod, "save"); |
5a343599cd3e
core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents:
733
diff
changeset
|
208 if ok then |
5a343599cd3e
core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents:
733
diff
changeset
|
209 saved = ret; |
710
56f6c115bc69
modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents:
709
diff
changeset
|
210 else |
745
5a343599cd3e
core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents:
733
diff
changeset
|
211 log("warn", "Error saving module '%s:%s' state: %s", host, module, ret); |
5a343599cd3e
core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents:
733
diff
changeset
|
212 if not config.get(host, "core", "force_module_reload") then |
5a343599cd3e
core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents:
733
diff
changeset
|
213 log("warn", "Aborting reload due to error, set force_module_reload to ignore this"); |
5a343599cd3e
core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents:
733
diff
changeset
|
214 return nil, "save-state-failed"; |
5a343599cd3e
core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents:
733
diff
changeset
|
215 else |
5a343599cd3e
core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents:
733
diff
changeset
|
216 log("warn", "Continuing with reload (using the force)"); |
5a343599cd3e
core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents:
733
diff
changeset
|
217 end |
710
56f6c115bc69
modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents:
709
diff
changeset
|
218 end |
56f6c115bc69
modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents:
709
diff
changeset
|
219 end |
56f6c115bc69
modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents:
709
diff
changeset
|
220 |
56f6c115bc69
modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents:
709
diff
changeset
|
221 unload(host, name, ...); |
745
5a343599cd3e
core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents:
733
diff
changeset
|
222 local ok, err = load(host, name, ...); |
5a343599cd3e
core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents:
733
diff
changeset
|
223 if ok then |
5a343599cd3e
core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents:
733
diff
changeset
|
224 mod = get_module(host, name); |
5a343599cd3e
core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents:
733
diff
changeset
|
225 if module_has_method(mod, "restore") then |
5a343599cd3e
core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents:
733
diff
changeset
|
226 local ok, err = call_module_method(mod, "restore", saved or {}) |
710
56f6c115bc69
modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents:
709
diff
changeset
|
227 if (not ok) and err then |
745
5a343599cd3e
core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents:
733
diff
changeset
|
228 log("warn", "Error restoring module '%s' from '%s': %s", name, host, err); |
710
56f6c115bc69
modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents:
709
diff
changeset
|
229 end |
56f6c115bc69
modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents:
709
diff
changeset
|
230 end |
56f6c115bc69
modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents:
709
diff
changeset
|
231 return true; |
56f6c115bc69
modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents:
709
diff
changeset
|
232 end |
745
5a343599cd3e
core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents:
733
diff
changeset
|
233 return ok, err; |
710
56f6c115bc69
modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents:
709
diff
changeset
|
234 end |
56f6c115bc69
modulemanager: Added reload support, with callbacks for saving and restoring state
Waqas Hussain <waqas20@gmail.com>
parents:
709
diff
changeset
|
235 |
578
5879264e28e2
Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents:
573
diff
changeset
|
236 function handle_stanza(host, origin, stanza) |
5879264e28e2
Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents:
573
diff
changeset
|
237 local name, xmlns, origin_type = stanza.name, stanza.attr.xmlns, origin.type; |
5879264e28e2
Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents:
573
diff
changeset
|
238 if name == "iq" and xmlns == "jabber:client" then |
608
3758af511ce8
Don't try processing stanzas not of type get or set in module manager
Waqas Hussain <waqas20@gmail.com>
parents:
592
diff
changeset
|
239 if stanza.attr.type == "get" or stanza.attr.type == "set" then |
929
b4f2ca7f6f00
Fixed: modulemanager: IQs with extended elements in the default namespace could cause backtraces (related to issue #74)
Waqas Hussain <waqas20@gmail.com>
parents:
896
diff
changeset
|
240 xmlns = stanza.tags[1].attr.xmlns or "jabber:client"; |
608
3758af511ce8
Don't try processing stanzas not of type get or set in module manager
Waqas Hussain <waqas20@gmail.com>
parents:
592
diff
changeset
|
241 log("debug", "Stanza of type %s from %s has xmlns: %s", name, origin_type, xmlns); |
3758af511ce8
Don't try processing stanzas not of type get or set in module manager
Waqas Hussain <waqas20@gmail.com>
parents:
592
diff
changeset
|
242 else |
3758af511ce8
Don't try processing stanzas not of type get or set in module manager
Waqas Hussain <waqas20@gmail.com>
parents:
592
diff
changeset
|
243 log("debug", "Discarding %s from %s of type: %s", name, origin_type, stanza.attr.type); |
3758af511ce8
Don't try processing stanzas not of type get or set in module manager
Waqas Hussain <waqas20@gmail.com>
parents:
592
diff
changeset
|
244 return true; |
3758af511ce8
Don't try processing stanzas not of type get or set in module manager
Waqas Hussain <waqas20@gmail.com>
parents:
592
diff
changeset
|
245 end |
578
5879264e28e2
Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents:
573
diff
changeset
|
246 end |
591 | 247 local handlers = stanza_handlers:get(host, origin_type, name, xmlns); |
876
fad020cead52
Allow global modules to hook stanza handlers
Waqas Hussain <waqas20@gmail.com>
parents:
778
diff
changeset
|
248 if not handlers then handlers = stanza_handlers:get("*", origin_type, name, xmlns); end |
578
5879264e28e2
Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents:
573
diff
changeset
|
249 if handlers then |
5879264e28e2
Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents:
573
diff
changeset
|
250 log("debug", "Passing stanza to mod_%s", handler_info[handlers[1]].name); |
5879264e28e2
Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents:
573
diff
changeset
|
251 (handlers[1])(origin, stanza); |
5879264e28e2
Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents:
573
diff
changeset
|
252 return true; |
5879264e28e2
Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents:
573
diff
changeset
|
253 else |
1167
5620ea24be94
stanza_router: Removed global function core_handle_stanza
Waqas Hussain <waqas20@gmail.com>
parents:
1105
diff
changeset
|
254 log("debug", "Unhandled %s stanza: %s; xmlns=%s", origin.type, stanza.name, xmlns); -- we didn't handle it |
5620ea24be94
stanza_router: Removed global function core_handle_stanza
Waqas Hussain <waqas20@gmail.com>
parents:
1105
diff
changeset
|
255 if stanza.attr.xmlns == "jabber:client" then |
5620ea24be94
stanza_router: Removed global function core_handle_stanza
Waqas Hussain <waqas20@gmail.com>
parents:
1105
diff
changeset
|
256 if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then |
5620ea24be94
stanza_router: Removed global function core_handle_stanza
Waqas Hussain <waqas20@gmail.com>
parents:
1105
diff
changeset
|
257 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); |
5620ea24be94
stanza_router: Removed global function core_handle_stanza
Waqas Hussain <waqas20@gmail.com>
parents:
1105
diff
changeset
|
258 end |
1173
09a4cd461673
modulemanager: Don't close the stream on unhandled stream:features
Waqas Hussain <waqas20@gmail.com>
parents:
1167
diff
changeset
|
259 elseif not(name == "features" and xmlns == "http://etherx.jabber.org/streams") then -- FIXME remove check once we handle S2S features |
1167
5620ea24be94
stanza_router: Removed global function core_handle_stanza
Waqas Hussain <waqas20@gmail.com>
parents:
1105
diff
changeset
|
260 origin:close("unsupported-stanza-type"); |
5620ea24be94
stanza_router: Removed global function core_handle_stanza
Waqas Hussain <waqas20@gmail.com>
parents:
1105
diff
changeset
|
261 end |
578
5879264e28e2
Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents:
573
diff
changeset
|
262 end |
5879264e28e2
Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents:
573
diff
changeset
|
263 end |
39
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
264 |
745
5a343599cd3e
core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents:
733
diff
changeset
|
265 function module_has_method(module, method) |
5a343599cd3e
core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents:
733
diff
changeset
|
266 return type(module.module[method]) == "function"; |
5a343599cd3e
core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents:
733
diff
changeset
|
267 end |
5a343599cd3e
core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents:
733
diff
changeset
|
268 |
747
40837f3422ab
modulemanager: Add get_host_type() API method, and fix up call_module_method to work properly
Matthew Wild <mwild1@gmail.com>
parents:
746
diff
changeset
|
269 function call_module_method(module, method, ...) |
745
5a343599cd3e
core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents:
733
diff
changeset
|
270 if module_has_method(module, method) then |
748
172c43d735e9
modulemanager: Really fix call_module_method to work properly
Matthew Wild <mwild1@gmail.com>
parents:
747
diff
changeset
|
271 local f = module.module[method]; |
745
5a343599cd3e
core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents:
733
diff
changeset
|
272 return pcall(f, ...); |
5a343599cd3e
core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents:
733
diff
changeset
|
273 else |
5a343599cd3e
core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents:
733
diff
changeset
|
274 return false, "no-such-method"; |
5a343599cd3e
core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents:
733
diff
changeset
|
275 end |
5a343599cd3e
core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents:
733
diff
changeset
|
276 end |
5a343599cd3e
core.modulemanager: Some refactoring to make upcoming changes a little easier
Matthew Wild <mwild1@gmail.com>
parents:
733
diff
changeset
|
277 |
438
193f9dd64f17
Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents:
400
diff
changeset
|
278 ----- API functions exposed to modules ----------- |
193f9dd64f17
Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents:
400
diff
changeset
|
279 -- Must all be in api.* |
193f9dd64f17
Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents:
400
diff
changeset
|
280 |
193f9dd64f17
Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents:
400
diff
changeset
|
281 -- Returns the name of the current module |
193f9dd64f17
Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents:
400
diff
changeset
|
282 function api:get_name() |
193f9dd64f17
Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents:
400
diff
changeset
|
283 return self.name; |
193f9dd64f17
Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents:
400
diff
changeset
|
284 end |
193f9dd64f17
Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents:
400
diff
changeset
|
285 |
193f9dd64f17
Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents:
400
diff
changeset
|
286 -- Returns the host that the current module is serving |
193f9dd64f17
Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents:
400
diff
changeset
|
287 function api:get_host() |
193f9dd64f17
Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents:
400
diff
changeset
|
288 return self.host; |
193f9dd64f17
Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents:
400
diff
changeset
|
289 end |
193f9dd64f17
Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents:
400
diff
changeset
|
290 |
747
40837f3422ab
modulemanager: Add get_host_type() API method, and fix up call_module_method to work properly
Matthew Wild <mwild1@gmail.com>
parents:
746
diff
changeset
|
291 function api:get_host_type() |
40837f3422ab
modulemanager: Add get_host_type() API method, and fix up call_module_method to work properly
Matthew Wild <mwild1@gmail.com>
parents:
746
diff
changeset
|
292 return hosts[self.host].type; |
40837f3422ab
modulemanager: Add get_host_type() API method, and fix up call_module_method to work properly
Matthew Wild <mwild1@gmail.com>
parents:
746
diff
changeset
|
293 end |
40837f3422ab
modulemanager: Add get_host_type() API method, and fix up call_module_method to work properly
Matthew Wild <mwild1@gmail.com>
parents:
746
diff
changeset
|
294 |
746
7027de4c039d
modulemanager: Add module:set_global() as a cleaner way for a module to declare itself 'global'
Matthew Wild <mwild1@gmail.com>
parents:
745
diff
changeset
|
295 function api:set_global() |
7027de4c039d
modulemanager: Add module:set_global() as a cleaner way for a module to declare itself 'global'
Matthew Wild <mwild1@gmail.com>
parents:
745
diff
changeset
|
296 self.host = "*"; |
1069
034e345c0f8d
modulemanager: Update logger when a module becomes global
Matthew Wild <mwild1@gmail.com>
parents:
929
diff
changeset
|
297 -- Update the logger |
034e345c0f8d
modulemanager: Update logger when a module becomes global
Matthew Wild <mwild1@gmail.com>
parents:
929
diff
changeset
|
298 local _log = logger.init("mod_"..self.name); |
034e345c0f8d
modulemanager: Update logger when a module becomes global
Matthew Wild <mwild1@gmail.com>
parents:
929
diff
changeset
|
299 self.log = function (self, ...) return _log(...); end; |
034e345c0f8d
modulemanager: Update logger when a module becomes global
Matthew Wild <mwild1@gmail.com>
parents:
929
diff
changeset
|
300 self._log = _log; |
746
7027de4c039d
modulemanager: Add module:set_global() as a cleaner way for a module to declare itself 'global'
Matthew Wild <mwild1@gmail.com>
parents:
745
diff
changeset
|
301 end |
7027de4c039d
modulemanager: Add module:set_global() as a cleaner way for a module to declare itself 'global'
Matthew Wild <mwild1@gmail.com>
parents:
745
diff
changeset
|
302 |
590
54afe37cccbf
Combined and merged similar code
Waqas Hussain <waqas20@gmail.com>
parents:
589
diff
changeset
|
303 local function _add_handler(module, origin_type, tag, xmlns, handler) |
591 | 304 local handlers = stanza_handlers:get(module.host, origin_type, tag, xmlns); |
590
54afe37cccbf
Combined and merged similar code
Waqas Hussain <waqas20@gmail.com>
parents:
589
diff
changeset
|
305 local msg = (tag == "iq") and "namespace" or "payload namespace"; |
578
5879264e28e2
Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents:
573
diff
changeset
|
306 if not handlers then |
591 | 307 stanza_handlers:add(module.host, origin_type, tag, xmlns, handler); |
578
5879264e28e2
Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents:
573
diff
changeset
|
308 handler_info[handler] = module; |
670
d5cf10b7fc44
Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents:
637
diff
changeset
|
309 handler_table:add(module.host, module.name, {module.host, origin_type, tag, xmlns}); |
733
b1aedec00661
modulemanager: Comment out logging of modules hooking stanzas, too noisy
Matthew Wild <mwild1@gmail.com>
parents:
713
diff
changeset
|
310 --module:log("debug", "I now handle tag '%s' [%s] with %s '%s'", tag, origin_type, msg, xmlns); |
578
5879264e28e2
Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents:
573
diff
changeset
|
311 else |
590
54afe37cccbf
Combined and merged similar code
Waqas Hussain <waqas20@gmail.com>
parents:
589
diff
changeset
|
312 module:log("warn", "I wanted to handle tag '%s' [%s] with %s '%s' but mod_%s already handles that", tag, origin_type, msg, xmlns, handler_info[handlers[1]].module.name); |
578
5879264e28e2
Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents:
573
diff
changeset
|
313 end |
5879264e28e2
Changed module manager to use multitable (initial commit)
Waqas Hussain <waqas20@gmail.com>
parents:
573
diff
changeset
|
314 end |
438
193f9dd64f17
Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents:
400
diff
changeset
|
315 |
590
54afe37cccbf
Combined and merged similar code
Waqas Hussain <waqas20@gmail.com>
parents:
589
diff
changeset
|
316 function api:add_handler(origin_type, tag, xmlns, handler) |
54afe37cccbf
Combined and merged similar code
Waqas Hussain <waqas20@gmail.com>
parents:
589
diff
changeset
|
317 if not (origin_type and tag and xmlns and handler) then return false; end |
438
193f9dd64f17
Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents:
400
diff
changeset
|
318 if type(origin_type) == "table" then |
193f9dd64f17
Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents:
400
diff
changeset
|
319 for _, origin_type in ipairs(origin_type) do |
590
54afe37cccbf
Combined and merged similar code
Waqas Hussain <waqas20@gmail.com>
parents:
589
diff
changeset
|
320 _add_handler(self, origin_type, tag, xmlns, handler); |
438
193f9dd64f17
Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents:
400
diff
changeset
|
321 end |
590
54afe37cccbf
Combined and merged similar code
Waqas Hussain <waqas20@gmail.com>
parents:
589
diff
changeset
|
322 else |
54afe37cccbf
Combined and merged similar code
Waqas Hussain <waqas20@gmail.com>
parents:
589
diff
changeset
|
323 _add_handler(self, origin_type, tag, xmlns, handler); |
438
193f9dd64f17
Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents:
400
diff
changeset
|
324 end |
590
54afe37cccbf
Combined and merged similar code
Waqas Hussain <waqas20@gmail.com>
parents:
589
diff
changeset
|
325 end |
54afe37cccbf
Combined and merged similar code
Waqas Hussain <waqas20@gmail.com>
parents:
589
diff
changeset
|
326 function api:add_iq_handler(origin_type, xmlns, handler) |
54afe37cccbf
Combined and merged similar code
Waqas Hussain <waqas20@gmail.com>
parents:
589
diff
changeset
|
327 self:add_handler(origin_type, "iq", xmlns, handler); |
438
193f9dd64f17
Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents:
400
diff
changeset
|
328 end |
193f9dd64f17
Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents:
400
diff
changeset
|
329 |
670
d5cf10b7fc44
Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents:
637
diff
changeset
|
330 addDiscoInfoHandler("*host", function(reply, to, from, node) |
d5cf10b7fc44
Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents:
637
diff
changeset
|
331 if #node == 0 then |
d5cf10b7fc44
Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents:
637
diff
changeset
|
332 local done = {}; |
1334
6846c5cc9ce2
modulemanager: Added function add_identity(category, type) to the modules API
Waqas Hussain <waqas20@gmail.com>
parents:
1330
diff
changeset
|
333 for module, identities in pairs(identities_table:get(to) or NULL) do -- for each module |
6846c5cc9ce2
modulemanager: Added function add_identity(category, type) to the modules API
Waqas Hussain <waqas20@gmail.com>
parents:
1330
diff
changeset
|
334 for identity, attr in pairs(identities) do |
6846c5cc9ce2
modulemanager: Added function add_identity(category, type) to the modules API
Waqas Hussain <waqas20@gmail.com>
parents:
1330
diff
changeset
|
335 if not done[identity] then |
6846c5cc9ce2
modulemanager: Added function add_identity(category, type) to the modules API
Waqas Hussain <waqas20@gmail.com>
parents:
1330
diff
changeset
|
336 reply:tag("identity", attr):up(); -- TODO cache |
6846c5cc9ce2
modulemanager: Added function add_identity(category, type) to the modules API
Waqas Hussain <waqas20@gmail.com>
parents:
1330
diff
changeset
|
337 done[identity] = true; |
6846c5cc9ce2
modulemanager: Added function add_identity(category, type) to the modules API
Waqas Hussain <waqas20@gmail.com>
parents:
1330
diff
changeset
|
338 end |
6846c5cc9ce2
modulemanager: Added function add_identity(category, type) to the modules API
Waqas Hussain <waqas20@gmail.com>
parents:
1330
diff
changeset
|
339 end |
6846c5cc9ce2
modulemanager: Added function add_identity(category, type) to the modules API
Waqas Hussain <waqas20@gmail.com>
parents:
1330
diff
changeset
|
340 end |
6846c5cc9ce2
modulemanager: Added function add_identity(category, type) to the modules API
Waqas Hussain <waqas20@gmail.com>
parents:
1330
diff
changeset
|
341 for module, identities in pairs(identities_table:get("*") or NULL) do -- for each module |
6846c5cc9ce2
modulemanager: Added function add_identity(category, type) to the modules API
Waqas Hussain <waqas20@gmail.com>
parents:
1330
diff
changeset
|
342 for identity, attr in pairs(identities) do |
6846c5cc9ce2
modulemanager: Added function add_identity(category, type) to the modules API
Waqas Hussain <waqas20@gmail.com>
parents:
1330
diff
changeset
|
343 if not done[identity] then |
6846c5cc9ce2
modulemanager: Added function add_identity(category, type) to the modules API
Waqas Hussain <waqas20@gmail.com>
parents:
1330
diff
changeset
|
344 reply:tag("identity", attr):up(); -- TODO cache |
6846c5cc9ce2
modulemanager: Added function add_identity(category, type) to the modules API
Waqas Hussain <waqas20@gmail.com>
parents:
1330
diff
changeset
|
345 done[identity] = true; |
6846c5cc9ce2
modulemanager: Added function add_identity(category, type) to the modules API
Waqas Hussain <waqas20@gmail.com>
parents:
1330
diff
changeset
|
346 end |
6846c5cc9ce2
modulemanager: Added function add_identity(category, type) to the modules API
Waqas Hussain <waqas20@gmail.com>
parents:
1330
diff
changeset
|
347 end |
6846c5cc9ce2
modulemanager: Added function add_identity(category, type) to the modules API
Waqas Hussain <waqas20@gmail.com>
parents:
1330
diff
changeset
|
348 end |
670
d5cf10b7fc44
Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents:
637
diff
changeset
|
349 for module, features in pairs(features_table:get(to) or NULL) do -- for each module |
d5cf10b7fc44
Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents:
637
diff
changeset
|
350 for feature in pairs(features) do |
d5cf10b7fc44
Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents:
637
diff
changeset
|
351 if not done[feature] then |
d5cf10b7fc44
Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents:
637
diff
changeset
|
352 reply:tag("feature", {var = feature}):up(); -- TODO cache |
d5cf10b7fc44
Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents:
637
diff
changeset
|
353 done[feature] = true; |
d5cf10b7fc44
Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents:
637
diff
changeset
|
354 end |
d5cf10b7fc44
Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents:
637
diff
changeset
|
355 end |
d5cf10b7fc44
Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents:
637
diff
changeset
|
356 end |
878
72a7eeaa9e58
Let global modules add disco features for all hosts
Waqas Hussain <waqas20@gmail.com>
parents:
876
diff
changeset
|
357 for module, features in pairs(features_table:get("*") or NULL) do -- for each module |
72a7eeaa9e58
Let global modules add disco features for all hosts
Waqas Hussain <waqas20@gmail.com>
parents:
876
diff
changeset
|
358 for feature in pairs(features) do |
72a7eeaa9e58
Let global modules add disco features for all hosts
Waqas Hussain <waqas20@gmail.com>
parents:
876
diff
changeset
|
359 if not done[feature] then |
72a7eeaa9e58
Let global modules add disco features for all hosts
Waqas Hussain <waqas20@gmail.com>
parents:
876
diff
changeset
|
360 reply:tag("feature", {var = feature}):up(); -- TODO cache |
72a7eeaa9e58
Let global modules add disco features for all hosts
Waqas Hussain <waqas20@gmail.com>
parents:
876
diff
changeset
|
361 done[feature] = true; |
72a7eeaa9e58
Let global modules add disco features for all hosts
Waqas Hussain <waqas20@gmail.com>
parents:
876
diff
changeset
|
362 end |
72a7eeaa9e58
Let global modules add disco features for all hosts
Waqas Hussain <waqas20@gmail.com>
parents:
876
diff
changeset
|
363 end |
72a7eeaa9e58
Let global modules add disco features for all hosts
Waqas Hussain <waqas20@gmail.com>
parents:
876
diff
changeset
|
364 end |
670
d5cf10b7fc44
Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents:
637
diff
changeset
|
365 return next(done) ~= nil; |
d5cf10b7fc44
Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents:
637
diff
changeset
|
366 end |
d5cf10b7fc44
Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents:
637
diff
changeset
|
367 end); |
d5cf10b7fc44
Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents:
637
diff
changeset
|
368 |
540
ec03f6968fa8
Added function add_feature to modules API (for adding disco features)
Waqas Hussain <waqas20@gmail.com>
parents:
519
diff
changeset
|
369 function api:add_feature(xmlns) |
670
d5cf10b7fc44
Modulemanager: Basic modules can now be unloaded correctly
Waqas Hussain <waqas20@gmail.com>
parents:
637
diff
changeset
|
370 features_table:set(self.host, self.name, xmlns, true); |
540
ec03f6968fa8
Added function add_feature to modules API (for adding disco features)
Waqas Hussain <waqas20@gmail.com>
parents:
519
diff
changeset
|
371 end |
1346
e34f9455b779
modulemanager: Change the ideitity attribute typ to type
Waqas Hussain <waqas20@gmail.com>
parents:
1334
diff
changeset
|
372 function api:add_identity(category, type) |
e34f9455b779
modulemanager: Change the ideitity attribute typ to type
Waqas Hussain <waqas20@gmail.com>
parents:
1334
diff
changeset
|
373 identities_table:set(self.host, self.name, category.."\0"..type, {category = category, type = type}); |
1334
6846c5cc9ce2
modulemanager: Added function add_identity(category, type) to the modules API
Waqas Hussain <waqas20@gmail.com>
parents:
1330
diff
changeset
|
374 end |
540
ec03f6968fa8
Added function add_feature to modules API (for adding disco features)
Waqas Hussain <waqas20@gmail.com>
parents:
519
diff
changeset
|
375 |
686
13ed38531f69
modulemanager: Per-host event hooks for plugins - solves issue 41
Waqas Hussain <waqas20@gmail.com>
parents:
675
diff
changeset
|
376 local event_hook = function(host, mod_name, event_name, ...) |
13ed38531f69
modulemanager: Per-host event hooks for plugins - solves issue 41
Waqas Hussain <waqas20@gmail.com>
parents:
675
diff
changeset
|
377 if type((...)) == "table" and (...).host and (...).host ~= host then return; end |
13ed38531f69
modulemanager: Per-host event hooks for plugins - solves issue 41
Waqas Hussain <waqas20@gmail.com>
parents:
675
diff
changeset
|
378 for handler in pairs(event_hooks:get(host, mod_name, event_name) or NULL) do |
13ed38531f69
modulemanager: Per-host event hooks for plugins - solves issue 41
Waqas Hussain <waqas20@gmail.com>
parents:
675
diff
changeset
|
379 handler(...); |
13ed38531f69
modulemanager: Per-host event hooks for plugins - solves issue 41
Waqas Hussain <waqas20@gmail.com>
parents:
675
diff
changeset
|
380 end |
13ed38531f69
modulemanager: Per-host event hooks for plugins - solves issue 41
Waqas Hussain <waqas20@gmail.com>
parents:
675
diff
changeset
|
381 end; |
13ed38531f69
modulemanager: Per-host event hooks for plugins - solves issue 41
Waqas Hussain <waqas20@gmail.com>
parents:
675
diff
changeset
|
382 function api:add_event_hook(name, handler) |
13ed38531f69
modulemanager: Per-host event hooks for plugins - solves issue 41
Waqas Hussain <waqas20@gmail.com>
parents:
675
diff
changeset
|
383 if not hooked:get(self.host, self.name, name) then |
13ed38531f69
modulemanager: Per-host event hooks for plugins - solves issue 41
Waqas Hussain <waqas20@gmail.com>
parents:
675
diff
changeset
|
384 eventmanager.add_event_hook(name, function(...) event_hook(self.host, self.name, name, ...); end); |
13ed38531f69
modulemanager: Per-host event hooks for plugins - solves issue 41
Waqas Hussain <waqas20@gmail.com>
parents:
675
diff
changeset
|
385 hooked:set(self.host, self.name, name, true); |
13ed38531f69
modulemanager: Per-host event hooks for plugins - solves issue 41
Waqas Hussain <waqas20@gmail.com>
parents:
675
diff
changeset
|
386 end |
13ed38531f69
modulemanager: Per-host event hooks for plugins - solves issue 41
Waqas Hussain <waqas20@gmail.com>
parents:
675
diff
changeset
|
387 event_hooks:set(self.host, self.name, name, handler, true); |
13ed38531f69
modulemanager: Per-host event hooks for plugins - solves issue 41
Waqas Hussain <waqas20@gmail.com>
parents:
675
diff
changeset
|
388 end |
438
193f9dd64f17
Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents:
400
diff
changeset
|
389 |
1183
565e16ee0c74
modulemanager: Add module:fire_event() to module API
Matthew Wild <mwild1@gmail.com>
parents:
1174
diff
changeset
|
390 function api:fire_event(...) |
1253
60156584c442
modulemanager: Stopped using core.eventmanager in module:fire_event
Waqas Hussain <waqas20@gmail.com>
parents:
1250
diff
changeset
|
391 return (hosts[self.host] or prosody).events.fire_event(...); |
1183
565e16ee0c74
modulemanager: Add module:fire_event() to module API
Matthew Wild <mwild1@gmail.com>
parents:
1174
diff
changeset
|
392 end |
565e16ee0c74
modulemanager: Add module:fire_event() to module API
Matthew Wild <mwild1@gmail.com>
parents:
1174
diff
changeset
|
393 |
1319
812c028a877d
modulemanager: Allow setting priority of stanza handlers
Matthew Wild <mwild1@gmail.com>
parents:
1318
diff
changeset
|
394 function api:hook(event, handler, priority) |
1259
6bd11bca9725
modulemanager: Keep track of event handlers added by module:hook, and remove them on module unload
Waqas Hussain <waqas20@gmail.com>
parents:
1253
diff
changeset
|
395 hooks:set(self.host, self.name, event, handler, true); |
1319
812c028a877d
modulemanager: Allow setting priority of stanza handlers
Matthew Wild <mwild1@gmail.com>
parents:
1318
diff
changeset
|
396 (hosts[self.host] or prosody).events.add_handler(event, handler, priority); |
1231
6f251813f1e5
modulemanager: Added hook(event, handler) to the plugin API
Waqas Hussain <waqas20@gmail.com>
parents:
1183
diff
changeset
|
397 end |
6f251813f1e5
modulemanager: Added hook(event, handler) to the plugin API
Waqas Hussain <waqas20@gmail.com>
parents:
1183
diff
changeset
|
398 |
1319
812c028a877d
modulemanager: Allow setting priority of stanza handlers
Matthew Wild <mwild1@gmail.com>
parents:
1318
diff
changeset
|
399 function api:hook_stanza(xmlns, name, handler, priority) |
1318
66f76c332a75
modulemanager: Add module:hook([xmlns, ] name, handler) which uses new stanza events
Matthew Wild <mwild1@gmail.com>
parents:
1309
diff
changeset
|
400 if not handler and type(name) == "function" then |
66f76c332a75
modulemanager: Add module:hook([xmlns, ] name, handler) which uses new stanza events
Matthew Wild <mwild1@gmail.com>
parents:
1309
diff
changeset
|
401 -- If only 2 options then they specified no xmlns |
1319
812c028a877d
modulemanager: Allow setting priority of stanza handlers
Matthew Wild <mwild1@gmail.com>
parents:
1318
diff
changeset
|
402 xmlns, name, handler, priority = nil, xmlns, name, handler; |
1321
0698d0d39b35
modulemanager: Don't require xmlns to be non-nil
Matthew Wild <mwild1@gmail.com>
parents:
1320
diff
changeset
|
403 elseif not (handler and name) then |
1320
9c736b993e07
modulemanager: module:log() -> self:log() fix
Matthew Wild <mwild1@gmail.com>
parents:
1319
diff
changeset
|
404 self:log("warn", "Error: Insufficient parameters to module:hook_stanza()"); |
1318
66f76c332a75
modulemanager: Add module:hook([xmlns, ] name, handler) which uses new stanza events
Matthew Wild <mwild1@gmail.com>
parents:
1309
diff
changeset
|
405 return; |
66f76c332a75
modulemanager: Add module:hook([xmlns, ] name, handler) which uses new stanza events
Matthew Wild <mwild1@gmail.com>
parents:
1309
diff
changeset
|
406 end |
1319
812c028a877d
modulemanager: Allow setting priority of stanza handlers
Matthew Wild <mwild1@gmail.com>
parents:
1318
diff
changeset
|
407 return api.hook(self, "stanza/"..(xmlns and (xmlns..":") or "")..name, function (data) return handler(data.origin, data.stanza, data); end, priority); |
1318
66f76c332a75
modulemanager: Add module:hook([xmlns, ] name, handler) which uses new stanza events
Matthew Wild <mwild1@gmail.com>
parents:
1309
diff
changeset
|
408 end |
66f76c332a75
modulemanager: Add module:hook([xmlns, ] name, handler) which uses new stanza events
Matthew Wild <mwild1@gmail.com>
parents:
1309
diff
changeset
|
409 |
438
193f9dd64f17
Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents:
400
diff
changeset
|
410 -------------------------------------------------------------------- |
193f9dd64f17
Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents:
400
diff
changeset
|
411 |
698
d8a678e40a0a
Add core.actions for managing server 'actions'; and make modulemanager register actions 'load' and 'unload'
Matthew Wild <mwild1@gmail.com>
parents:
695
diff
changeset
|
412 local actions = {}; |
d8a678e40a0a
Add core.actions for managing server 'actions'; and make modulemanager register actions 'load' and 'unload'
Matthew Wild <mwild1@gmail.com>
parents:
695
diff
changeset
|
413 |
d8a678e40a0a
Add core.actions for managing server 'actions'; and make modulemanager register actions 'load' and 'unload'
Matthew Wild <mwild1@gmail.com>
parents:
695
diff
changeset
|
414 function actions.load(params) |
d8a678e40a0a
Add core.actions for managing server 'actions'; and make modulemanager register actions 'load' and 'unload'
Matthew Wild <mwild1@gmail.com>
parents:
695
diff
changeset
|
415 --return true, "Module loaded ("..params.module.." on "..params.host..")"; |
d8a678e40a0a
Add core.actions for managing server 'actions'; and make modulemanager register actions 'load' and 'unload'
Matthew Wild <mwild1@gmail.com>
parents:
695
diff
changeset
|
416 return load(params.host, params.module); |
d8a678e40a0a
Add core.actions for managing server 'actions'; and make modulemanager register actions 'load' and 'unload'
Matthew Wild <mwild1@gmail.com>
parents:
695
diff
changeset
|
417 end |
d8a678e40a0a
Add core.actions for managing server 'actions'; and make modulemanager register actions 'load' and 'unload'
Matthew Wild <mwild1@gmail.com>
parents:
695
diff
changeset
|
418 |
d8a678e40a0a
Add core.actions for managing server 'actions'; and make modulemanager register actions 'load' and 'unload'
Matthew Wild <mwild1@gmail.com>
parents:
695
diff
changeset
|
419 function actions.unload(params) |
d8a678e40a0a
Add core.actions for managing server 'actions'; and make modulemanager register actions 'load' and 'unload'
Matthew Wild <mwild1@gmail.com>
parents:
695
diff
changeset
|
420 return unload(params.host, params.module); |
d8a678e40a0a
Add core.actions for managing server 'actions'; and make modulemanager register actions 'load' and 'unload'
Matthew Wild <mwild1@gmail.com>
parents:
695
diff
changeset
|
421 end |
d8a678e40a0a
Add core.actions for managing server 'actions'; and make modulemanager register actions 'load' and 'unload'
Matthew Wild <mwild1@gmail.com>
parents:
695
diff
changeset
|
422 |
d8a678e40a0a
Add core.actions for managing server 'actions'; and make modulemanager register actions 'load' and 'unload'
Matthew Wild <mwild1@gmail.com>
parents:
695
diff
changeset
|
423 register_actions("/modules", actions); |
d8a678e40a0a
Add core.actions for managing server 'actions'; and make modulemanager register actions 'load' and 'unload'
Matthew Wild <mwild1@gmail.com>
parents:
695
diff
changeset
|
424 |
39
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
425 return _M; |