Comparison

mod_adhoc_cmd_modules/mod_adhoc_cmd_modules.lua @ 208:214cb85cdfbf

mod_adhoc_cmd_modules: Add "Load module" command
author Florian Zeitz <florob@babelmonkeys.de>
date Sat, 10 Jul 2010 01:52:43 +0200
parent 207:1ae653712e37
child 235:10a3cca32797
comparison
equal deleted inserted replaced
207:1ae653712e37 208:214cb85cdfbf
26 local modules = array.collect(keys(hosts[data.to].modules)):sort():concat("\n"); 26 local modules = array.collect(keys(hosts[data.to].modules)):sort():concat("\n");
27 27
28 return { status = "completed", result = { layout = result; data = { modules = modules } } }; 28 return { status = "completed", result = { layout = result; data = { modules = modules } } };
29 end 29 end
30 30
31 function load_module_handler(self, data, state)
32 local layout = dataforms_new {
33 title = "Load module";
34 instructions = "Specify the module to be loaded";
35
36 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#load" };
37 { name = "module", type = "text-single", label = "Module to be loaded:"};
38 };
39 if state then
40 if data.action == "cancel" then
41 return { status = "canceled" };
42 end
43 local fields = layout:data(data.form);
44 if modulemanager.is_loaded(data.to, fields.module) then
45 return { status = "completed", info = "Module already loaded" };
46 end
47 local ok, err = modulemanager.load(data.to, fields.module);
48 if ok then
49 return { status = "completed", info = 'Module "'..fields.module..'" successfully loaded on host "'..data.to..'".' };
50 else
51 return { status = "completed", error = { message = 'Failed to load module "'..fields.module..'" on host "'..data.to..
52 '". Error was: "'..tostring(err or "<unspecified>")..'"' } };
53 end
54 else
55 local modules = array.collect(keys(hosts[data.to].modules)):sort();
56 return { status = "executing", form = layout }, "executing";
57 end
58 end
59
31 -- TODO: Allow reloading multiple modules (depends on list-multi) 60 -- TODO: Allow reloading multiple modules (depends on list-multi)
32 function reload_modules_handler(self, data, state) 61 function reload_modules_handler(self, data, state)
33 local layout = dataforms_new { 62 local layout = dataforms_new {
34 title = "Reload module"; 63 title = "Reload module";
35 instructions = "Select the module to be reloaded"; 64 instructions = "Select the module to be reloaded";
39 }; 68 };
40 if state then 69 if state then
41 if data.action == "cancel" then 70 if data.action == "cancel" then
42 return { status = "canceled" }; 71 return { status = "canceled" };
43 end 72 end
44 fields = layout:data(data.form); 73 local fields = layout:data(data.form);
45 local ok, err = modulemanager.reload(data.to, fields.module); 74 local ok, err = modulemanager.reload(data.to, fields.module);
46 if ok then 75 if ok then
47 return { status = "completed", info = 'Module "'..fields.module..'" successfully reloaded on host "'..data.to..'".' }; 76 return { status = "completed", info = 'Module "'..fields.module..'" successfully reloaded on host "'..data.to..'".' };
48 else 77 else
49 return { status = "completed", error = 'Failed to reload module "'..fields.module..'" on host "'..data.to.. 78 return { status = "completed", error = { message = 'Failed to reload module "'..fields.module..'" on host "'..data.to..
50 '". Error was: "'..tostring(err)..'"' }; 79 '". Error was: "'..tostring(err)..'"' } };
51 end 80 end
52 else 81 else
53 local modules = array.collect(keys(hosts[data.to].modules)):sort(); 82 local modules = array.collect(keys(hosts[data.to].modules)):sort();
54 return { status = "executing", form = { layout = layout; data = { module = modules } } }, "executing"; 83 return { status = "executing", form = { layout = layout; data = { module = modules } } }, "executing";
55 end 84 end
66 }; 95 };
67 if state then 96 if state then
68 if data.action == "cancel" then 97 if data.action == "cancel" then
69 return { status = "canceled" }; 98 return { status = "canceled" };
70 end 99 end
71 fields = layout:data(data.form); 100 local fields = layout:data(data.form);
72 local ok, err = modulemanager.unload(data.to, fields.module); 101 local ok, err = modulemanager.unload(data.to, fields.module);
73 if ok then 102 if ok then
74 return { status = "completed", info = 'Module "'..fields.module..'" successfully unloaded on host "'..data.to..'".' }; 103 return { status = "completed", info = 'Module "'..fields.module..'" successfully unloaded on host "'..data.to..'".' };
75 else 104 else
76 return { status = "completed", error = 'Failed to unload module "'..fields.module..'" on host "'..data.to.. 105 return { status = "completed", error = { message = 'Failed to unload module "'..fields.module..'" on host "'..data.to..
77 '". Error was: "'..tostring(err)..'"' }; 106 '". Error was: "'..tostring(err)..'"' } };
78 end 107 end
79 else 108 else
80 local modules = array.collect(keys(hosts[data.to].modules)):sort(); 109 local modules = array.collect(keys(hosts[data.to].modules)):sort();
81 return { status = "executing", form = { layout = layout; data = { module = modules } } }, "executing"; 110 return { status = "executing", form = { layout = layout; data = { module = modules } } }, "executing";
82 end 111 end
83 end 112 end
84 113
85 local list_modules_desc = adhoc_new("List loaded modules", "http://prosody.im/protocol/modules#list", list_modules_handler, "admin"); 114 local list_modules_desc = adhoc_new("List loaded modules", "http://prosody.im/protocol/modules#list", list_modules_handler, "admin");
115 local load_module_desc = adhoc_new("Load module", "http://prosody.im/protocol/modules#load", load_module_handler, "admin");
86 local reload_modules_desc = adhoc_new("Reload module", "http://prosody.im/protocol/modules#reload", reload_modules_handler, "admin"); 116 local reload_modules_desc = adhoc_new("Reload module", "http://prosody.im/protocol/modules#reload", reload_modules_handler, "admin");
87 local unload_modules_desc = adhoc_new("Unload module", "http://prosody.im/protocol/modules#unload", unload_modules_handler, "admin"); 117 local unload_modules_desc = adhoc_new("Unload module", "http://prosody.im/protocol/modules#unload", unload_modules_handler, "admin");
88 118
89 module:add_item("adhoc", list_modules_desc); 119 module:add_item("adhoc", list_modules_desc);
120 module:add_item("adhoc", load_module_desc);
90 module:add_item("adhoc", reload_modules_desc); 121 module:add_item("adhoc", reload_modules_desc);
91 module:add_item("adhoc", unload_modules_desc); 122 module:add_item("adhoc", unload_modules_desc);