comparison mod_adhoc_cmd_admin/mod_adhoc_cmd_admin.lua @ 84:b47216512a1d

mod_adhoc_cmd_admin: Implement "Delete User" command
author Florian Zeitz <florob@babelmonkeys.de>
date Sat, 07 Nov 2009 00:37:51 +0100
parents e839b4453387
children 63bf00fd8f6e
comparison
equal deleted inserted replaced
83:9d92db30235f 84:b47216512a1d
7 local _G = _G; 7 local _G = _G;
8 8
9 local prosody = _G.prosody; 9 local prosody = _G.prosody;
10 local hosts = prosody.hosts; 10 local hosts = prosody.hosts;
11 11
12 local t_concat = table.concat;
13
12 local usermanager_user_exists = require "core.usermanager".user_exists; 14 local usermanager_user_exists = require "core.usermanager".user_exists;
13 local usermanager_create_user = require "core.usermanager".create_user; 15 local usermanager_create_user = require "core.usermanager".create_user;
14 local is_admin = require "core.usermanager".is_admin; 16 local is_admin = require "core.usermanager".is_admin;
15 17
16 local st, jid, uuid = require "util.stanza", require "util.jid", require "util.uuid"; 18 local st, jid, uuid = require "util.stanza", require "util.jid", require "util.uuid";
27 { name = "accountjid", type = "jid-single", required = true, label = "The Jabber ID for the account to be added" }; 29 { name = "accountjid", type = "jid-single", required = true, label = "The Jabber ID for the account to be added" };
28 { name = "password", type = "text-private", label = "The password for this account" }; 30 { name = "password", type = "text-private", label = "The password for this account" };
29 { name = "password-verify", type = "text-private", label = "Retype password" }; 31 { name = "password-verify", type = "text-private", label = "Retype password" };
30 }; 32 };
31 33
34 local delete_user_layout = dataforms_new{
35 title = "Deleting a User";
36 instructions = "Fill out this form to delete a user.";
37
38 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
39 { name = "accountjids", type = "jid-multi", label = "The Jabber ID(s) to delete" };
40 };
41
32 local get_online_users_layout = dataforms_new{ 42 local get_online_users_layout = dataforms_new{
33 title = "Getting List of Online Users"; 43 title = "Getting List of Online Users";
34 instructions = "How many users should be returned at most?"; 44 instructions = "How many users should be returned at most?";
35 45
36 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" }; 46 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
55 return true; 65 return true;
56 end 66 end
57 local form = stanza.tags[1]:child_with_ns("jabber:x:data"); 67 local form = stanza.tags[1]:child_with_ns("jabber:x:data");
58 local fields = add_user_layout:data(form); 68 local fields = add_user_layout:data(form);
59 local username, host, resource = jid.split(fields.accountjid); 69 local username, host, resource = jid.split(fields.accountjid);
60 if (fields.password == fields["password-verify"]) and username and host and host == stanza.attr.to then 70 if (fields["password"] == fields["password-verify"]) and username and host and host == stanza.attr.to then
61 if usermanager_user_exists(username, host) then 71 if usermanager_user_exists(username, host) then
62 origin.send(st.error_reply(stanza, "cancel", "conflict", "Account already exists"):up() 72 origin.send(st.error_reply(stanza, "cancel", "conflict", "Account already exists"):up()
63 :add_child(item:cmdtag("canceled", stanza.tags[1].attr.sessionid) 73 :add_child(item:cmdtag("canceled", stanza.tags[1].attr.sessionid)
64 :tag("note", {type="error"}):text("Account already exists"))); 74 :tag("note", {type="error"}):text("Account already exists")));
65 sessions[stanza.tags[1].attr.sessionid] = nil; 75 sessions[stanza.tags[1].attr.sessionid] = nil;
95 origin.send(st.reply(stanza):add_child(item:cmdtag("executing", sessionid):add_child(add_user_layout:form()))); 105 origin.send(st.reply(stanza):add_child(item:cmdtag("executing", sessionid):add_child(add_user_layout:form())));
96 end 106 end
97 return true; 107 return true;
98 end 108 end
99 109
110 function delete_user_command_handler(item, origin, stanza)
111 if stanza.tags[1].attr.sessionid and sessions[stanza.tags[1].attr.sessionid] then
112 if stanza.tags[1].attr.action == "cancel" then
113 origin.send(st.reply(stanza):add_child(item:cmdtag("canceled", stanza.tags[1].attr.sessionid)));
114 sessions[stanza.tags[1].attr.sessionid] = nil;
115 return true;
116 end
117 local form = stanza.tags[1]:child_with_ns("jabber:x:data");
118 local fields = delete_user_layout:data(form);
119 local failed = {};
120 local succeeded = {};
121 for _, aJID in ipairs(fields.accountjids) do
122 local username, host, resource = jid.split(aJID);
123 if usermanager_user_exists(username, host) and usermanager_create_user(username, nil, host) then
124 module:log("debug", "User" .. aJID .. "has been deleted");
125 succeeded[#succeeded+1] = aJID;
126 else
127 module:log("debug", "Tried to delete not existing user "..aJID);
128 failed[#failed+1] = aJID;
129 end
130 end
131 origin.send(st.reply(stanza):add_child(item:cmdtag("completed", stanza.tags[1].attr.sessionid)
132 :tag("note", {type="info"})
133 :text((#succeeded ~= 0 and "The following accounts were successfully deleted:\n"..t_concat(succeeded, "\n").."\n" or "")
134 ..(#failed ~= 0 and "The following accounts could not be deleted:\n"..t_concat(failed, "\n") or ""))));
135 sessions[stanza.tags[1].attr.sessionid] = nil;
136 return true;
137 else
138 local sessionid=uuid.generate();
139 sessions[sessionid] = "executing";
140 origin.send(st.reply(stanza):add_child(item:cmdtag("executing", sessionid):add_child(delete_user_layout:form())));
141 end
142 return true;
143 end
144
100 function get_online_users_command_handler(item, origin, stanza) 145 function get_online_users_command_handler(item, origin, stanza)
101 if stanza.tags[1].attr.sessionid and sessions[stanza.tags[1].attr.sessionid] then 146 if stanza.tags[1].attr.sessionid and sessions[stanza.tags[1].attr.sessionid] then
102 if stanza.tags[1].attr.action == "cancel" then 147 if stanza.tags[1].attr.action == "cancel" then
103 origin.send(st.reply(stanza):add_child(item:cmdtag("canceled", stanza.tags[1].attr.sessionid))); 148 origin.send(st.reply(stanza):add_child(item:cmdtag("canceled", stanza.tags[1].attr.sessionid)));
104 sessions[stanza.tags[1].attr.sessionid] = nil; 149 sessions[stanza.tags[1].attr.sessionid] = nil;
174 219
175 return true; 220 return true;
176 end 221 end
177 222
178 local add_user_desc = adhoc_new("Add User", "http://jabber.org/protocol/admin#add-user", add_user_command_handler, "admin"); 223 local add_user_desc = adhoc_new("Add User", "http://jabber.org/protocol/admin#add-user", add_user_command_handler, "admin");
224 local delete_user_desc = adhoc_new("Delete User", "http://jabber.org/protocol/admin#delete-user", delete_user_command_handler, "admin");
179 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"); 225 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");
180 local announce_desc = adhoc_new("Send Announcement to Online Users", "http://jabber.org/protocol/admin#announce", announce_handler, "admin"); 226 local announce_desc = adhoc_new("Send Announcement to Online Users", "http://jabber.org/protocol/admin#announce", announce_handler, "admin");
181 227
182 function module.unload() 228 function module.unload()
183 module:remove_item("adhoc", add_user_desc); 229 module:remove_item("adhoc", add_user_desc);
230 module:remove_item("adhoc", delete_user_desc);
184 module:remove_item("adhoc", get_online_users_desc); 231 module:remove_item("adhoc", get_online_users_desc);
185 module:remove_item("adhoc", announce_desc); 232 module:remove_item("adhoc", announce_desc);
186 end 233 end
187 234
188 module:add_item("adhoc", add_user_desc); 235 module:add_item("adhoc", add_user_desc);
236 module:add_item("adhoc", delete_user_desc);
189 module:add_item("adhoc", get_online_users_desc); 237 module:add_item("adhoc", get_online_users_desc);
190 module:add_item("adhoc", announce_desc); 238 module:add_item("adhoc", announce_desc);