comparison mod_adhoc_cmd_admin/mod_adhoc_cmd_admin.lua @ 133:8945153321a1

mod_adhoc_cmd_admin: Add "Shut Down Service" command
author Florian Zeitz <florob@babelmonkeys.de>
date Thu, 25 Feb 2010 22:43:54 +0100
parents bdd1641c159d
children b4f9518d5a00
comparison
equal deleted inserted replaced
132:d4ff1cd414e5 133:8945153321a1
15 local usermanager_get_password = require "core.usermanager".get_password; 15 local usermanager_get_password = require "core.usermanager".get_password;
16 local usermanager_create_user = require "core.usermanager".create_user; 16 local usermanager_create_user = require "core.usermanager".create_user;
17 local is_admin = require "core.usermanager".is_admin; 17 local is_admin = require "core.usermanager".is_admin;
18 18
19 local st, jid, uuid = require "util.stanza", require "util.jid", require "util.uuid"; 19 local st, jid, uuid = require "util.stanza", require "util.jid", require "util.uuid";
20 local timer_add_task = require "util.timer".add_task;
20 local dataforms_new = require "util.dataforms".new; 21 local dataforms_new = require "util.dataforms".new;
21 module:log("debug", module:get_name()); 22 module:log("debug", module:get_name());
22 local adhoc_new = module:require "adhoc".new; 23 local adhoc_new = module:require "adhoc".new;
23 24
24 local add_user_layout = dataforms_new{ 25 local add_user_layout = dataforms_new{
89 instructions = "Fill out this form to make an announcement to all\nactive users of this service."; 90 instructions = "Fill out this form to make an announcement to all\nactive users of this service.";
90 91
91 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" }; 92 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
92 { name = "subject", type = "text-single", label = "Subject" }; 93 { name = "subject", type = "text-single", label = "Subject" };
93 { name = "announcement", type = "text-multi", required = true, label = "Announcement" }; 94 { name = "announcement", type = "text-multi", required = true, label = "Announcement" };
95 };
96
97 local shut_down_service_layout = dataforms_new{
98 title = "Shutting Down the Service";
99 instructions = "Fill out this form to shut down the service.";
100
101 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
102 { name = "delay", type = "list-single", label = "Time delay before shutting down",
103 value = { {label = "30 seconds", value = "30"},
104 {label = "60 seconds", value = "60"},
105 {label = "90 seconds", value = "90"},
106 {label = "2 minutes", value = "120"},
107 {label = "3 minutes", value = "180"},
108 {label = "4 minutes", value = "240"},
109 {label = "5 minutes", value = "300"},
110 };
111 };
112 { name = "announcement", type = "text-multi", label = "Announcement" };
94 }; 113 };
95 114
96 function add_user_command_handler(self, data, state) 115 function add_user_command_handler(self, data, state)
97 if state then 116 if state then
98 if data.action == "cancel" then 117 if data.action == "cancel" then
242 else 261 else
243 return { status = "executing", form = get_online_users_layout }, "executing"; 262 return { status = "executing", form = get_online_users_layout }, "executing";
244 end 263 end
245 end 264 end
246 265
266 function send_to_online(message, server)
267 if server then
268 sessions = { [server] = hosts[server] };
269 else
270 sessions = hosts;
271 end
272
273 local c = 0;
274 for domain, session in pairs(sessions) do
275 for user in pairs(session.sessions or {}) do
276 c = c + 1;
277 message.attr.from = domain;
278 message.attr.to = user.."@"..domain;
279 core_post_stanza(session, message);
280 end
281 end
282
283 return c;
284 end
285
247 function announce_handler(self, data, state) 286 function announce_handler(self, data, state)
248 if state then 287 if state then
249 if data.action == "cancel" then 288 if data.action == "cancel" then
250 return { status = "canceled" }; 289 return { status = "canceled" };
251 end 290 end
252 291
253 local fields = announce_layout:data(data.form); 292 local fields = announce_layout:data(data.form);
254 293
255 module:log("info", "Sending server announcement to all online users"); 294 module:log("info", "Sending server announcement to all online users");
256 local host_session = hosts[data.to]; 295 local message = st.message({type = "headline"}, fields.announcement):up()
257 local message = st.message({type = "headline", from = data.to}, fields.announcement):up()
258 :tag("subject"):text(fields.subject or "Announcement"); 296 :tag("subject"):text(fields.subject or "Announcement");
259 297
260 local c = 0; 298 local count = send_to_online(message, data.to);
261 for user in pairs(host_session.sessions) do
262 c = c + 1;
263 message.attr.to = user.."@"..data.to;
264 core_post_stanza(host_session, message);
265 end
266 299
267 module:log("info", "Announcement sent to %d online users", c); 300 module:log("info", "Announcement sent to %d online users", count);
268 return { status = "completed", info = "Announcement sent." }; 301 return { status = "completed", info = "Announcement sent." };
269 else 302 else
270 return { status = "executing", form = announce_layout }, "executing"; 303 return { status = "executing", form = announce_layout }, "executing";
271 end 304 end
272 305
273 return true; 306 return true;
274 end 307 end
275 308
309 function shut_down_service_handler(self, data, state)
310 if state then
311 if data.action == "cancel" then
312 return { status = "canceled" };
313 end
314
315 local fields = shut_down_service_layout:data(data.form);
316
317 if fields.announcement then
318 local message = st.message({type = "headline"}, fields.announcement):up()
319 :tag("subject"):text("Server is shutting down");
320 send_to_online(message);
321 end
322
323 timer_add_task(tonumber(fields.delay or "5"), prosody.shutdown);
324
325 return { status = "completed", info = "Server is about to shut down" };
326 else
327 return { status = "executing", form = shut_down_service_layout }, "executing";
328 end
329
330 return true;
331 end
332
276 local add_user_desc = adhoc_new("Add User", "http://jabber.org/protocol/admin#add-user", add_user_command_handler, "admin"); 333 local add_user_desc = adhoc_new("Add User", "http://jabber.org/protocol/admin#add-user", add_user_command_handler, "admin");
334 local announce_desc = adhoc_new("Send Announcement to Online Users", "http://jabber.org/protocol/admin#announce", announce_handler, "admin");
277 local change_user_password_desc = adhoc_new("Change User Password", "http://jabber.org/protocol/admin#change-user-password", change_user_password_command_handler, "admin"); 335 local change_user_password_desc = adhoc_new("Change User Password", "http://jabber.org/protocol/admin#change-user-password", change_user_password_command_handler, "admin");
278 local delete_user_desc = adhoc_new("Delete User", "http://jabber.org/protocol/admin#delete-user", delete_user_command_handler, "admin"); 336 local delete_user_desc = adhoc_new("Delete User", "http://jabber.org/protocol/admin#delete-user", delete_user_command_handler, "admin");
279 local end_user_session_desc = adhoc_new("End User Session", "http://jabber.org/protocol/admin#end-user-session", end_user_session_handler, "admin"); 337 local end_user_session_desc = adhoc_new("End User Session", "http://jabber.org/protocol/admin#end-user-session", end_user_session_handler, "admin");
280 local get_user_password_desc = adhoc_new("Get User Password", "http://jabber.org/protocol/admin#get-user-password", get_user_password_handler, "admin"); 338 local get_user_password_desc = adhoc_new("Get User Password", "http://jabber.org/protocol/admin#get-user-password", get_user_password_handler, "admin");
281 local get_online_users_desc = adhoc_new("Get List of Online Users", "http://jabber.org/protocol/admin#get-online-users", get_online_users_command_handler, "admin"); 339 local get_online_users_desc = adhoc_new("Get List of Online Users", "http://jabber.org/protocol/admin#get-online-users", get_online_users_command_handler, "admin");
282 local announce_desc = adhoc_new("Send Announcement to Online Users", "http://jabber.org/protocol/admin#announce", announce_handler, "admin"); 340 local shut_down_service_desc = adhoc_new("Shut Down Service", "http://jabber.org/protocol/admin#shutdown", shut_down_service_handler, "admin");
283 341
284 module:add_item("adhoc", add_user_desc); 342 module:add_item("adhoc", add_user_desc);
343 module:add_item("adhoc", announce_desc);
285 module:add_item("adhoc", change_user_password_desc); 344 module:add_item("adhoc", change_user_password_desc);
286 module:add_item("adhoc", delete_user_desc); 345 module:add_item("adhoc", delete_user_desc);
287 module:add_item("adhoc", end_user_session_desc); 346 module:add_item("adhoc", end_user_session_desc);
288 module:add_item("adhoc", get_user_password_desc); 347 module:add_item("adhoc", get_user_password_desc);
289 module:add_item("adhoc", get_online_users_desc); 348 module:add_item("adhoc", get_online_users_desc);
290 module:add_item("adhoc", announce_desc); 349 module:add_item("adhoc", shut_down_service_desc);