Mercurial > prosody-modules
comparison mod_adhoc_cmd_admin/mod_adhoc_cmd_admin.lua @ 144:b4f9518d5a00
mod_adhoc_cmd_admin: "Get User Roster" command (with not very good™ XML formatting for now)
author | Florian Zeitz <florob@babelmonkeys.de> |
---|---|
date | Fri, 12 Mar 2010 03:15:49 +0100 |
parents | 8945153321a1 |
children | 890140ade4e5 |
comparison
equal
deleted
inserted
replaced
143:445efff314c4 | 144:b4f9518d5a00 |
---|---|
14 local usermanager_user_exists = require "core.usermanager".user_exists; | 14 local usermanager_user_exists = require "core.usermanager".user_exists; |
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 rm_load_roster = require "core.rostermanager".load_roster; | |
20 | |
19 local st, jid, uuid = require "util.stanza", require "util.jid", require "util.uuid"; | 21 local st, jid, uuid = require "util.stanza", require "util.jid", require "util.uuid"; |
20 local timer_add_task = require "util.timer".add_task; | 22 local timer_add_task = require "util.timer".add_task; |
21 local dataforms_new = require "util.dataforms".new; | 23 local dataforms_new = require "util.dataforms".new; |
22 module:log("debug", module:get_name()); | 24 module:log("debug", module:get_name()); |
23 local adhoc_new = module:require "adhoc".new; | 25 local adhoc_new = module:require "adhoc".new; |
67 | 69 |
68 local get_user_password_result_layout = dataforms_new{ | 70 local get_user_password_result_layout = dataforms_new{ |
69 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" }; | 71 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" }; |
70 { name = "accountjid", type = "jid-single", label = "JID" }; | 72 { name = "accountjid", type = "jid-single", label = "JID" }; |
71 { name = "password", type = "text-single", label = "Password" }; | 73 { name = "password", type = "text-single", label = "Password" }; |
74 }; | |
75 | |
76 local get_user_roster_layout = dataforms_new{ | |
77 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" }; | |
78 { name = "accountjid", type = "jid-single", label = "The Jabber ID for which to retrieve the roster" }; | |
79 }; | |
80 | |
81 local get_user_roster_result_layout = dataforms_new{ | |
82 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" }; | |
83 { name = "accountjid", type = "jid-single", label = "This is the roster for" }; | |
84 { name = "roster", type = "text-multi", label = "Roster XML" }; | |
72 }; | 85 }; |
73 | 86 |
74 local get_online_users_layout = dataforms_new{ | 87 local get_online_users_layout = dataforms_new{ |
75 title = "Getting List of Online Users"; | 88 title = "Getting List of Online Users"; |
76 instructions = "How many users should be returned at most?"; | 89 instructions = "How many users should be returned at most?"; |
234 else | 247 else |
235 return { status = "executing", form = get_user_password_layout }, "executing"; | 248 return { status = "executing", form = get_user_password_layout }, "executing"; |
236 end | 249 end |
237 end | 250 end |
238 | 251 |
252 function get_user_roster_handler(self, data, state) | |
253 if state then | |
254 if data.action == "cancel" then | |
255 return { status = "canceled" }; | |
256 end | |
257 | |
258 local fields = add_user_layout:data(data.form); | |
259 | |
260 local user, host, resource = jid.split(fields.accountjid); | |
261 if not usermanager_user_exists(user, host) then | |
262 return { status = "error", error = { type = "cancel", condition = "item-not-found", message = "User does not exist" } }; | |
263 end | |
264 local roster = rm_load_roster(user, host); | |
265 | |
266 local query = st.stanza("query", { xmlns = "jabber:iq:roster" }); | |
267 for jid in pairs(roster) do | |
268 if jid ~= "pending" and jid then | |
269 query:tag("item", { | |
270 jid = jid, | |
271 subscription = roster[jid].subscription, | |
272 ask = roster[jid].ask, | |
273 name = roster[jid].name, | |
274 }); | |
275 for group in pairs(roster[jid].groups) do | |
276 query:tag("group"):text(group):up(); | |
277 end | |
278 query:up(); | |
279 end | |
280 end | |
281 | |
282 local query_text = query:__tostring(); -- TODO: Use upcoming pretty_print() function | |
283 query_text = query_text:gsub("><", ">\n<"); | |
284 | |
285 local result = get_user_roster_result_layout:form({ accountjid = user.."@"..host, roster = query_text }, "result"); | |
286 result:add_child(query); | |
287 return { status = "completed", other = result }; | |
288 else | |
289 return { status = "executing", form = get_user_roster_layout }, "executing"; | |
290 end | |
291 end | |
292 | |
239 function get_online_users_command_handler(self, data, state) | 293 function get_online_users_command_handler(self, data, state) |
240 if state then | 294 if state then |
241 if data.action == "cancel" then | 295 if data.action == "cancel" then |
242 return { status = "canceled" }; | 296 return { status = "canceled" }; |
243 end | 297 end |
334 local announce_desc = adhoc_new("Send Announcement to Online Users", "http://jabber.org/protocol/admin#announce", announce_handler, "admin"); | 388 local announce_desc = adhoc_new("Send Announcement to Online Users", "http://jabber.org/protocol/admin#announce", announce_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"); | 389 local change_user_password_desc = adhoc_new("Change User Password", "http://jabber.org/protocol/admin#change-user-password", change_user_password_command_handler, "admin"); |
336 local delete_user_desc = adhoc_new("Delete User", "http://jabber.org/protocol/admin#delete-user", delete_user_command_handler, "admin"); | 390 local delete_user_desc = adhoc_new("Delete User", "http://jabber.org/protocol/admin#delete-user", delete_user_command_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"); | 391 local end_user_session_desc = adhoc_new("End User Session", "http://jabber.org/protocol/admin#end-user-session", end_user_session_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"); | 392 local get_user_password_desc = adhoc_new("Get User Password", "http://jabber.org/protocol/admin#get-user-password", get_user_password_handler, "admin"); |
393 local get_user_roster_desc = adhoc_new("Get User Roster","http://jabber.org/protocol/admin#get-user-roster", get_user_roster_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"); | 394 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"); |
340 local shut_down_service_desc = adhoc_new("Shut Down Service", "http://jabber.org/protocol/admin#shutdown", shut_down_service_handler, "admin"); | 395 local shut_down_service_desc = adhoc_new("Shut Down Service", "http://jabber.org/protocol/admin#shutdown", shut_down_service_handler, "admin"); |
341 | 396 |
342 module:add_item("adhoc", add_user_desc); | 397 module:add_item("adhoc", add_user_desc); |
343 module:add_item("adhoc", announce_desc); | 398 module:add_item("adhoc", announce_desc); |
344 module:add_item("adhoc", change_user_password_desc); | 399 module:add_item("adhoc", change_user_password_desc); |
345 module:add_item("adhoc", delete_user_desc); | 400 module:add_item("adhoc", delete_user_desc); |
346 module:add_item("adhoc", end_user_session_desc); | 401 module:add_item("adhoc", end_user_session_desc); |
347 module:add_item("adhoc", get_user_password_desc); | 402 module:add_item("adhoc", get_user_password_desc); |
403 module:add_item("adhoc", get_user_roster_desc); | |
348 module:add_item("adhoc", get_online_users_desc); | 404 module:add_item("adhoc", get_online_users_desc); |
349 module:add_item("adhoc", shut_down_service_desc); | 405 module:add_item("adhoc", shut_down_service_desc); |