Software /
code /
prosody-modules
Annotate
mod_adhoc_cmd_modules/mod_adhoc_cmd_modules.lua @ 206:d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
author | Florian Zeitz <florob@babelmonkeys.de> |
---|---|
date | Fri, 09 Jul 2010 22:38:05 +0200 |
parent | 205:a6361a1fda5e |
child | 207:1ae653712e37 |
rev | line source |
---|---|
203
4927b9737bb7
mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
1 -- Copyright (C) 2009-2010 Florian Zeitz |
4927b9737bb7
mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
2 -- |
4927b9737bb7
mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
3 -- This file is MIT/X11 licensed. Please see the |
4927b9737bb7
mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
4 -- COPYING file in the source package for more information. |
4927b9737bb7
mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
5 -- |
4927b9737bb7
mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
6 |
4927b9737bb7
mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
7 local _G = _G; |
4927b9737bb7
mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
8 |
4927b9737bb7
mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
9 local prosody = _G.prosody; |
4927b9737bb7
mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
10 local hosts = prosody.hosts; |
4927b9737bb7
mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
11 |
4927b9737bb7
mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
12 require "util.iterators"; |
4927b9737bb7
mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
13 local dataforms_new = require "util.dataforms".new; |
4927b9737bb7
mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
14 local array = require "util.array"; |
205
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
15 local modulemanager = require "modulemanager"; |
203
4927b9737bb7
mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
16 local adhoc_new = module:require "adhoc".new; |
4927b9737bb7
mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
17 |
4927b9737bb7
mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
18 function list_modules_handler(self, data, state) |
205
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
19 local result = dataforms_new { |
203
4927b9737bb7
mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
20 title = "List of loaded modules"; |
4927b9737bb7
mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
21 |
4927b9737bb7
mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
22 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#list" }; |
4927b9737bb7
mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
23 { name = "modules", type = "text-multi", label = "The following modules are loaded:" }; |
4927b9737bb7
mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
24 }; |
4927b9737bb7
mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
25 |
205
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
26 local modules = array.collect(keys(hosts[data.to].modules)):sort():concat("\n"); |
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
27 |
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
28 return { status = "completed", result = { layout = result; data = { modules = modules } } }; |
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
29 end |
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
30 |
206
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
31 -- TODO: Allow reloading multiple modules (depends on list-multi) |
205
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
32 function reload_modules_handler(self, data, state) |
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
33 local modules = {}; |
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
34 local layout = dataforms_new { |
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
35 title = "Reload module"; |
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
36 instructions = "Select the module to be reloaded"; |
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
37 |
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
38 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#reload" }; |
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
39 { name = "module", type = "list-single", value = modules, label = "Module to be reloaded:"}; |
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
40 }; |
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
41 if state then |
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
42 if data.action == "cancel" then |
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
43 return { status = "canceled" }; |
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
44 end |
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
45 fields = layout:data(data.form); |
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
46 local ok, err = modulemanager.reload(data.to, fields.module); |
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
47 if ok then |
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
48 return { status = "completed", info = 'Module "'..fields.module..'" successfully reloaded on host "'..data.to..'".' }; |
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
49 else |
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
50 return { status = "completed", error = 'Failed to reload module "'..fields.module..'" on host "'..data.to.. |
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
51 '". Error was: "'..tostring(err)..'"' }; |
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
52 end |
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
53 else |
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
54 local modules2 = array.collect(keys(hosts[data.to].modules)):sort(); |
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
55 for i, val in ipairs(modules2) do |
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
56 modules[i] = val; |
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
57 end |
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
58 return { status = "executing", form = layout }, "executing"; |
203
4927b9737bb7
mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
59 end |
4927b9737bb7
mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
60 end |
4927b9737bb7
mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
61 |
206
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
62 -- TODO: Allow unloading multiple modules (depends on list-multi) |
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
63 function unload_modules_handler(self, data, state) |
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
64 local modules = {}; |
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
65 local layout = dataforms_new { |
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
66 title = "Unload module"; |
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
67 instructions = "Select the module to be unloaded"; |
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
68 |
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
69 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#unload" }; |
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
70 { name = "module", type = "list-single", value = modules, label = "Module to be unloaded:"}; |
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
71 }; |
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
72 if state then |
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
73 if data.action == "cancel" then |
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
74 return { status = "canceled" }; |
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
75 end |
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
76 fields = layout:data(data.form); |
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
77 local ok, err = modulemanager.unload(data.to, fields.module); |
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
78 if ok then |
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
79 return { status = "completed", info = 'Module "'..fields.module..'" successfully unloaded on host "'..data.to..'".' }; |
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
80 else |
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
81 return { status = "completed", error = 'Failed to unload module "'..fields.module..'" on host "'..data.to.. |
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
82 '". Error was: "'..tostring(err)..'"' }; |
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
83 end |
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
84 else |
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
85 local modules2 = array.collect(keys(hosts[data.to].modules)):sort(); |
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
86 for i, val in ipairs(modules2) do |
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
87 modules[i] = val; |
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
88 end |
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
89 return { status = "executing", form = layout }, "executing"; |
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
90 end |
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
91 end |
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
92 |
203
4927b9737bb7
mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
93 local list_modules_desc = adhoc_new("List loaded modules", "http://prosody.im/protocol/modules#list", list_modules_handler, "admin"); |
205
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
94 local reload_modules_desc = adhoc_new("Reload module", "http://prosody.im/protocol/modules#reload", reload_modules_handler, "admin"); |
206
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
95 local unload_modules_desc = adhoc_new("Unload module", "http://prosody.im/protocol/modules#unload", unload_modules_handler, "admin"); |
203
4927b9737bb7
mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
96 |
4927b9737bb7
mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
97 module:add_item("adhoc", list_modules_desc); |
205
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
98 module:add_item("adhoc", reload_modules_desc); |
206
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
99 module:add_item("adhoc", unload_modules_desc); |