Comparison

plugins/mod_admin_adhoc.lua @ 4932:212e81ac6ebb

mod_admin_web: Use util.dataforms' own error checking
author Florian Zeitz <florob@babelmonkeys.de>
date Thu, 05 Jul 2012 00:15:49 +0200
parent 4926:58714123f600
child 4956:bc26e6d519ae
comparison
equal deleted inserted replaced
4931:7a4f00168260 4932:212e81ac6ebb
25 local modulemanager = require "modulemanager"; 25 local modulemanager = require "modulemanager";
26 26
27 module:depends("adhoc"); 27 module:depends("adhoc");
28 local adhoc_new = module:require "adhoc".new; 28 local adhoc_new = module:require "adhoc".new;
29 29
30 local function generate_error_message(errors)
31 local errmsg = {};
32 for name, err in pairs(errors) do
33 errmsg[#errmsg + 1] = name .. ": " .. err;
34 end
35 return { status = "completed", error = { message = t_concat(errmsg, "\n") } };
36 end
37
30 function add_user_command_handler(self, data, state) 38 function add_user_command_handler(self, data, state)
31 local add_user_layout = dataforms_new{ 39 local add_user_layout = dataforms_new{
32 title = "Adding a User"; 40 title = "Adding a User";
33 instructions = "Fill out this form to add a user."; 41 instructions = "Fill out this form to add a user.";
34 42
40 48
41 if state then 49 if state then
42 if data.action == "cancel" then 50 if data.action == "cancel" then
43 return { status = "canceled" }; 51 return { status = "canceled" };
44 end 52 end
45 local fields = add_user_layout:data(data.form); 53 local fields, err = add_user_layout:data(data.form);
46 if not fields.accountjid then 54 if err then
47 return { status = "completed", error = { message = "You need to specify a JID." } }; 55 return generate_error_message(err);
48 end 56 end
49 local username, host, resource = jid.split(fields.accountjid); 57 local username, host, resource = jid.split(fields.accountjid);
50 if data.to ~= host then 58 if data.to ~= host then
51 return { status = "completed", error = { message = "Trying to add a user on " .. host .. " but command was sent to " .. data.to}}; 59 return { status = "completed", error = { message = "Trying to add a user on " .. host .. " but command was sent to " .. data.to}};
52 end 60 end
83 91
84 if state then 92 if state then
85 if data.action == "cancel" then 93 if data.action == "cancel" then
86 return { status = "canceled" }; 94 return { status = "canceled" };
87 end 95 end
88 local fields = change_user_password_layout:data(data.form); 96 local fields, err = change_user_password_layout:data(data.form);
89 if not fields.accountjid or fields.accountjid == "" or not fields.password then 97 if err then
90 return { status = "completed", error = { message = "Please specify username and password" } }; 98 return generate_error_message(err);
91 end 99 end
92 local username, host, resource = jid.split(fields.accountjid); 100 local username, host, resource = jid.split(fields.accountjid);
93 if data.to ~= host then 101 if data.to ~= host then
94 return { status = "completed", error = { message = "Trying to change the password of a user on " .. host .. " but command was sent to " .. data.to}}; 102 return { status = "completed", error = { message = "Trying to change the password of a user on " .. host .. " but command was sent to " .. data.to}};
95 end 103 end
124 132
125 if state then 133 if state then
126 if data.action == "cancel" then 134 if data.action == "cancel" then
127 return { status = "canceled" }; 135 return { status = "canceled" };
128 end 136 end
129 local fields = delete_user_layout:data(data.form); 137 local fields, err = delete_user_layout:data(data.form);
138 if err then
139 return generate_error_message(err);
140 end
130 local failed = {}; 141 local failed = {};
131 local succeeded = {}; 142 local succeeded = {};
132 for _, aJID in ipairs(fields.accountjids) do 143 for _, aJID in ipairs(fields.accountjids) do
133 local username, host, resource = jid.split(aJID); 144 local username, host, resource = jid.split(aJID);
134 if (host == data.to) and usermanager_user_exists(username, host) and disconnect_user(aJID) and usermanager_create_user(username, nil, host) then 145 if (host == data.to) and usermanager_user_exists(username, host) and disconnect_user(aJID) and usermanager_create_user(username, nil, host) then
173 if state then 184 if state then
174 if data.action == "cancel" then 185 if data.action == "cancel" then
175 return { status = "canceled" }; 186 return { status = "canceled" };
176 end 187 end
177 188
178 local fields = end_user_session_layout:data(data.form); 189 local fields, err = end_user_session_layout:data(data.form);
190 if err then
191 return generate_error_message(err);
192 end
179 local failed = {}; 193 local failed = {};
180 local succeeded = {}; 194 local succeeded = {};
181 for _, aJID in ipairs(fields.accountjids) do 195 for _, aJID in ipairs(fields.accountjids) do
182 local username, host, resource = jid.split(aJID); 196 local username, host, resource = jid.split(aJID);
183 if (host == data.to) and usermanager_user_exists(username, host) and disconnect_user(aJID) then 197 if (host == data.to) and usermanager_user_exists(username, host) and disconnect_user(aJID) then
221 235
222 if state then 236 if state then
223 if data.action == "cancel" then 237 if data.action == "cancel" then
224 return { status = "canceled" }; 238 return { status = "canceled" };
225 end 239 end
226 local fields = get_user_password_layout:data(data.form); 240 local fields, err = get_user_password_layout:data(data.form);
227 if not fields.accountjid then 241 if err then
228 return { status = "completed", error = { message = "Please specify a JID." } }; 242 return generate_error_message(err);
229 end 243 end
230 local user, host, resource = jid.split(fields.accountjid); 244 local user, host, resource = jid.split(fields.accountjid);
231 local accountjid = ""; 245 local accountjid = "";
232 local password = ""; 246 local password = "";
233 if host ~= data.to then 247 if host ~= data.to then
259 if state then 273 if state then
260 if data.action == "cancel" then 274 if data.action == "cancel" then
261 return { status = "canceled" }; 275 return { status = "canceled" };
262 end 276 end
263 277
264 local fields = get_user_roster_layout:data(data.form); 278 local fields, err = get_user_roster_layout:data(data.form);
265 279
266 if not fields.accountjid then 280 if err then
267 return { status = "completed", error = { message = "Please specify a JID" } }; 281 return generate_error_message(err);
268 end 282 end
269 283
270 local user, host, resource = jid.split(fields.accountjid); 284 local user, host, resource = jid.split(fields.accountjid);
271 if host ~= data.to then 285 if host ~= data.to then
272 return { status = "completed", error = { message = "Tried to get roster for a user on " .. host .. " but command was sent to " .. data.to } }; 286 return { status = "completed", error = { message = "Tried to get roster for a user on " .. host .. " but command was sent to " .. data.to } };
321 if state then 335 if state then
322 if data.action == "cancel" then 336 if data.action == "cancel" then
323 return { status = "canceled" }; 337 return { status = "canceled" };
324 end 338 end
325 339
326 local fields = get_user_stats_layout:data(data.form); 340 local fields, err = get_user_stats_layout:data(data.form);
327 341
328 if not fields.accountjid then 342 if err then
329 return { status = "completed", error = { message = "Please specify a JID." } }; 343 return generate_error_message(err);
330 end 344 end
331 345
332 local user, host, resource = jid.split(fields.accountjid); 346 local user, host, resource = jid.split(fields.accountjid);
333 if host ~= data.to then 347 if host ~= data.to then
334 return { status = "completed", error = { message = "Tried to get stats for a user on " .. host .. " but command was sent to " .. data.to } }; 348 return { status = "completed", error = { message = "Tried to get stats for a user on " .. host .. " but command was sent to " .. data.to } };
374 if state then 388 if state then
375 if data.action == "cancel" then 389 if data.action == "cancel" then
376 return { status = "canceled" }; 390 return { status = "canceled" };
377 end 391 end
378 392
379 local fields = get_online_users_layout:data(data.form); 393 local fields, err = get_online_users_layout:data(data.form);
394
395 if err then
396 return generate_error_message(err);
397 end
380 398
381 local max_items = nil 399 local max_items = nil
382 if fields.max_items ~= "all" then 400 if fields.max_items ~= "all" then
383 max_items = tonumber(fields.max_items); 401 max_items = tonumber(fields.max_items);
384 end 402 end
434 }; 452 };
435 if state then 453 if state then
436 if data.action == "cancel" then 454 if data.action == "cancel" then
437 return { status = "canceled" }; 455 return { status = "canceled" };
438 end 456 end
439 local fields = layout:data(data.form); 457 local fields, err = layout:data(data.form);
440 if (not fields.module) or (fields.module == "") then 458 if err then
441 return { status = "completed", error = { 459 return generate_error_message(err);
442 message = "Please specify a module."
443 } };
444 end 460 end
445 if modulemanager.is_loaded(data.to, fields.module) then 461 if modulemanager.is_loaded(data.to, fields.module) then
446 return { status = "completed", info = "Module already loaded" }; 462 return { status = "completed", info = "Module already loaded" };
447 end 463 end
448 local ok, err = modulemanager.load(data.to, fields.module); 464 local ok, err = modulemanager.load(data.to, fields.module);
468 }; 484 };
469 if state then 485 if state then
470 if data.action == "cancel" then 486 if data.action == "cancel" then
471 return { status = "canceled" }; 487 return { status = "canceled" };
472 end 488 end
473 local fields = layout:data(data.form); 489 local fields, err = layout:data(data.form);
474 if #fields.modules == 0 then 490 if err then
475 return { status = "completed", error = { 491 return generate_error_message(err);
476 message = "Please specify a module. (This means your client misbehaved, as this field is required)"
477 } };
478 end 492 end
479 local ok_list, err_list = {}, {}; 493 local ok_list, err_list = {}, {};
480 for _, module in ipairs(fields.modules) do 494 for _, module in ipairs(fields.modules) do
481 local ok, err = modulemanager.reload(data.to, module); 495 local ok, err = modulemanager.reload(data.to, module);
482 if ok then 496 if ok then
536 if state then 550 if state then
537 if data.action == "cancel" then 551 if data.action == "cancel" then
538 return { status = "canceled" }; 552 return { status = "canceled" };
539 end 553 end
540 554
541 local fields = shut_down_service_layout:data(data.form); 555 local fields, err = shut_down_service_layout:data(data.form);
556
557 if err then
558 return generate_error_message(err);
559 end
542 560
543 if fields.announcement and #fields.announcement > 0 then 561 if fields.announcement and #fields.announcement > 0 then
544 local message = st.message({type = "headline"}, fields.announcement):up() 562 local message = st.message({type = "headline"}, fields.announcement):up()
545 :tag("subject"):text("Server is shutting down"); 563 :tag("subject"):text("Server is shutting down");
546 send_to_online(message); 564 send_to_online(message);
564 }; 582 };
565 if state then 583 if state then
566 if data.action == "cancel" then 584 if data.action == "cancel" then
567 return { status = "canceled" }; 585 return { status = "canceled" };
568 end 586 end
569 local fields = layout:data(data.form); 587 local fields, err = layout:data(data.form);
570 if #fields.modules == 0 then 588 if err then
571 return { status = "completed", error = { 589 return generate_error_message(err);
572 message = "Please specify a module. (This means your client misbehaved, as this field is required)"
573 } };
574 end 590 end
575 local ok_list, err_list = {}, {}; 591 local ok_list, err_list = {}, {};
576 for _, module in ipairs(fields.modules) do 592 for _, module in ipairs(fields.modules) do
577 local ok, err = modulemanager.unload(data.to, module); 593 local ok, err = modulemanager.unload(data.to, module);
578 if ok then 594 if ok then