# HG changeset patch # User Florian Zeitz # Date 1257550671 -3600 # Node ID b47216512a1d15b6772088bdd13f7d61d27d2325 # Parent 9d92db30235f9a96b0ed2ae187889dc21341788c mod_adhoc_cmd_admin: Implement "Delete User" command diff -r 9d92db30235f -r b47216512a1d mod_adhoc_cmd_admin/mod_adhoc_cmd_admin.lua --- a/mod_adhoc_cmd_admin/mod_adhoc_cmd_admin.lua Sun Nov 01 21:28:14 2009 +0100 +++ b/mod_adhoc_cmd_admin/mod_adhoc_cmd_admin.lua Sat Nov 07 00:37:51 2009 +0100 @@ -9,6 +9,8 @@ local prosody = _G.prosody; local hosts = prosody.hosts; +local t_concat = table.concat; + local usermanager_user_exists = require "core.usermanager".user_exists; local usermanager_create_user = require "core.usermanager".create_user; local is_admin = require "core.usermanager".is_admin; @@ -29,6 +31,14 @@ { name = "password-verify", type = "text-private", label = "Retype password" }; }; +local delete_user_layout = dataforms_new{ + title = "Deleting a User"; + instructions = "Fill out this form to delete a user."; + + { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" }; + { name = "accountjids", type = "jid-multi", label = "The Jabber ID(s) to delete" }; +}; + local get_online_users_layout = dataforms_new{ title = "Getting List of Online Users"; instructions = "How many users should be returned at most?"; @@ -57,7 +67,7 @@ local form = stanza.tags[1]:child_with_ns("jabber:x:data"); local fields = add_user_layout:data(form); local username, host, resource = jid.split(fields.accountjid); - if (fields.password == fields["password-verify"]) and username and host and host == stanza.attr.to then + if (fields["password"] == fields["password-verify"]) and username and host and host == stanza.attr.to then if usermanager_user_exists(username, host) then origin.send(st.error_reply(stanza, "cancel", "conflict", "Account already exists"):up() :add_child(item:cmdtag("canceled", stanza.tags[1].attr.sessionid) @@ -97,6 +107,41 @@ return true; end +function delete_user_command_handler(item, origin, stanza) + if stanza.tags[1].attr.sessionid and sessions[stanza.tags[1].attr.sessionid] then + if stanza.tags[1].attr.action == "cancel" then + origin.send(st.reply(stanza):add_child(item:cmdtag("canceled", stanza.tags[1].attr.sessionid))); + sessions[stanza.tags[1].attr.sessionid] = nil; + return true; + end + local form = stanza.tags[1]:child_with_ns("jabber:x:data"); + local fields = delete_user_layout:data(form); + local failed = {}; + local succeeded = {}; + for _, aJID in ipairs(fields.accountjids) do + local username, host, resource = jid.split(aJID); + if usermanager_user_exists(username, host) and usermanager_create_user(username, nil, host) then + module:log("debug", "User" .. aJID .. "has been deleted"); + succeeded[#succeeded+1] = aJID; + else + module:log("debug", "Tried to delete not existing user "..aJID); + failed[#failed+1] = aJID; + end + end + origin.send(st.reply(stanza):add_child(item:cmdtag("completed", stanza.tags[1].attr.sessionid) + :tag("note", {type="info"}) + :text((#succeeded ~= 0 and "The following accounts were successfully deleted:\n"..t_concat(succeeded, "\n").."\n" or "") + ..(#failed ~= 0 and "The following accounts could not be deleted:\n"..t_concat(failed, "\n") or "")))); + sessions[stanza.tags[1].attr.sessionid] = nil; + return true; + else + local sessionid=uuid.generate(); + sessions[sessionid] = "executing"; + origin.send(st.reply(stanza):add_child(item:cmdtag("executing", sessionid):add_child(delete_user_layout:form()))); + end + return true; +end + function get_online_users_command_handler(item, origin, stanza) if stanza.tags[1].attr.sessionid and sessions[stanza.tags[1].attr.sessionid] then if stanza.tags[1].attr.action == "cancel" then @@ -176,15 +221,18 @@ end local add_user_desc = adhoc_new("Add User", "http://jabber.org/protocol/admin#add-user", add_user_command_handler, "admin"); +local delete_user_desc = adhoc_new("Delete User", "http://jabber.org/protocol/admin#delete-user", delete_user_command_handler, "admin"); 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"); local announce_desc = adhoc_new("Send Announcement to Online Users", "http://jabber.org/protocol/admin#announce", announce_handler, "admin"); function module.unload() module:remove_item("adhoc", add_user_desc); + module:remove_item("adhoc", delete_user_desc); module:remove_item("adhoc", get_online_users_desc); module:remove_item("adhoc", announce_desc); end module:add_item("adhoc", add_user_desc); +module:add_item("adhoc", delete_user_desc); module:add_item("adhoc", get_online_users_desc); module:add_item("adhoc", announce_desc);