Software /
code /
prosody-modules
Annotate
mod_adhoc_cmd_modules/mod_adhoc_cmd_modules.lua @ 207:1ae653712e37
mod_adhoc: Add capability to pass data to forms
mod_adhoc_cmd_modules: Update to use afforementioned functionality
author | Florian Zeitz <florob@babelmonkeys.de> |
---|---|
date | Sat, 10 Jul 2010 01:26:41 +0200 |
parent | 206:d3498f115fcd |
child | 208:214cb85cdfbf |
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 layout = dataforms_new { |
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
34 title = "Reload module"; |
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
35 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
|
36 |
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
37 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#reload" }; |
207
1ae653712e37
mod_adhoc: Add capability to pass data to forms
Florian Zeitz <florob@babelmonkeys.de>
parents:
206
diff
changeset
|
38 { name = "module", type = "list-single", label = "Module to be reloaded:"}; |
205
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
39 }; |
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
40 if state then |
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
41 if data.action == "cancel" then |
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
42 return { status = "canceled" }; |
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
43 end |
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
44 fields = layout:data(data.form); |
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
45 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
|
46 if ok then |
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
47 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
|
48 else |
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
49 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
|
50 '". Error was: "'..tostring(err)..'"' }; |
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
51 end |
a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
203
diff
changeset
|
52 else |
207
1ae653712e37
mod_adhoc: Add capability to pass data to forms
Florian Zeitz <florob@babelmonkeys.de>
parents:
206
diff
changeset
|
53 local modules = array.collect(keys(hosts[data.to].modules)):sort(); |
1ae653712e37
mod_adhoc: Add capability to pass data to forms
Florian Zeitz <florob@babelmonkeys.de>
parents:
206
diff
changeset
|
54 return { status = "executing", form = { layout = layout; data = { module = modules } } }, "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
|
55 end |
4927b9737bb7
mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
56 end |
4927b9737bb7
mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
57 |
206
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
58 -- 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
|
59 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
|
60 local layout = dataforms_new { |
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
61 title = "Unload module"; |
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
62 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
|
63 |
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
64 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#unload" }; |
207
1ae653712e37
mod_adhoc: Add capability to pass data to forms
Florian Zeitz <florob@babelmonkeys.de>
parents:
206
diff
changeset
|
65 { name = "module", type = "list-single", label = "Module to be unloaded:"}; |
206
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
66 }; |
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
67 if state then |
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
68 if data.action == "cancel" then |
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
69 return { status = "canceled" }; |
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
70 end |
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
71 fields = layout:data(data.form); |
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
72 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
|
73 if ok then |
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
74 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
|
75 else |
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
76 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
|
77 '". Error was: "'..tostring(err)..'"' }; |
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
78 end |
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
79 else |
207
1ae653712e37
mod_adhoc: Add capability to pass data to forms
Florian Zeitz <florob@babelmonkeys.de>
parents:
206
diff
changeset
|
80 local modules = array.collect(keys(hosts[data.to].modules)):sort(); |
1ae653712e37
mod_adhoc: Add capability to pass data to forms
Florian Zeitz <florob@babelmonkeys.de>
parents:
206
diff
changeset
|
81 return { status = "executing", form = { layout = layout; data = { module = modules } } }, "executing"; |
206
d3498f115fcd
mod_adhoc_cmd_modules: Add "Unload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents:
205
diff
changeset
|
82 end |
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 |
203
4927b9737bb7
mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
85 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
|
86 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
|
87 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
|
88 |
4927b9737bb7
mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
89 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
|
90 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
|
91 module:add_item("adhoc", unload_modules_desc); |