Software /
code /
prosody-modules
Comparison
mod_adhoc_cmd_modules/mod_adhoc_cmd_modules.lua @ 205:a6361a1fda5e
mod_adhoc_cmd_modules: Add "Reload module" command
author | Florian Zeitz <florob@babelmonkeys.de> |
---|---|
date | Fri, 09 Jul 2010 22:29:26 +0200 |
parent | 203:4927b9737bb7 |
child | 206:d3498f115fcd |
comparison
equal
deleted
inserted
replaced
204:316d7c8e1fb0 | 205:a6361a1fda5e |
---|---|
10 local hosts = prosody.hosts; | 10 local hosts = prosody.hosts; |
11 | 11 |
12 require "util.iterators"; | 12 require "util.iterators"; |
13 local dataforms_new = require "util.dataforms".new; | 13 local dataforms_new = require "util.dataforms".new; |
14 local array = require "util.array"; | 14 local array = require "util.array"; |
15 local modulemanager = require "modulemanager"; | |
15 local adhoc_new = module:require "adhoc".new; | 16 local adhoc_new = module:require "adhoc".new; |
16 | 17 |
17 function list_modules_handler(self, data, state) | 18 function list_modules_handler(self, data, state) |
18 local list_modules_result = dataforms_new { | 19 local result = dataforms_new { |
19 title = "List of loaded modules"; | 20 title = "List of loaded modules"; |
20 | 21 |
21 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#list" }; | 22 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#list" }; |
22 { name = "modules", type = "text-multi", label = "The following modules are loaded:" }; | 23 { name = "modules", type = "text-multi", label = "The following modules are loaded:" }; |
23 }; | 24 }; |
24 | 25 |
25 local modules = array.collect(keys(hosts[data.to].modules)):sort() | 26 local modules = array.collect(keys(hosts[data.to].modules)):sort():concat("\n"); |
26 local modules_str = nil; | 27 |
27 for _, name in ipairs(modules) do | 28 return { status = "completed", result = { layout = result; data = { modules = modules } } }; |
28 modules_str = ((modules_str and modules_str .. "\n") or "") .. name; | 29 end |
30 | |
31 -- TODO: Allow reloading multiple modules (depends on list-multi | |
32 function reload_modules_handler(self, data, state) | |
33 local modules = {}; | |
34 local layout = dataforms_new { | |
35 title = "Reload module"; | |
36 instructions = "Select the module to be reloaded"; | |
37 | |
38 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#reload" }; | |
39 { name = "module", type = "list-single", value = modules, label = "Module to be reloaded:"}; | |
40 }; | |
41 if state then | |
42 if data.action == "cancel" then | |
43 return { status = "canceled" }; | |
44 end | |
45 fields = layout:data(data.form); | |
46 local ok, err = modulemanager.reload(data.to, fields.module); | |
47 if ok then | |
48 return { status = "completed", info = 'Module "'..fields.module..'" successfully reloaded on host "'..data.to..'".' }; | |
49 else | |
50 return { status = "completed", error = 'Failed to reload module "'..fields.module..'" on host "'..data.to.. | |
51 '". Error was: "'..tostring(err)..'"' }; | |
52 end | |
53 else | |
54 local modules2 = array.collect(keys(hosts[data.to].modules)):sort(); | |
55 for i, val in ipairs(modules2) do | |
56 modules[i] = val; | |
57 end | |
58 return { status = "executing", form = layout }, "executing"; | |
29 end | 59 end |
30 | |
31 return { status = "completed", result = { layout = list_modules_result; data = { modules = modules_str } } }; | |
32 end | 60 end |
33 | 61 |
34 local list_modules_desc = adhoc_new("List loaded modules", "http://prosody.im/protocol/modules#list", list_modules_handler, "admin"); | 62 local list_modules_desc = adhoc_new("List loaded modules", "http://prosody.im/protocol/modules#list", list_modules_handler, "admin"); |
63 local reload_modules_desc = adhoc_new("Reload module", "http://prosody.im/protocol/modules#reload", reload_modules_handler, "admin"); | |
35 | 64 |
36 module:add_item("adhoc", list_modules_desc); | 65 module:add_item("adhoc", list_modules_desc); |
66 module:add_item("adhoc", reload_modules_desc); |