mod_adhoc_cmd_admin/mod_adhoc_cmd_admin.lua
changeset 40 4c4daa1f8ae7
parent 36 58d326d86a9a
child 43 adc9eff8adb2
equal deleted inserted replaced
39:b84b2b026eb4 40:4c4daa1f8ae7
     1 -- Copyright (C) 2009 Florian Zeitz
     1 -- Copyright (C) 2009 Florian Zeitz
     2 -- 
     2 -- 
     3 -- This file is MIT/X11 licensed. Please see the
     3 -- This file is MIT/X11 licensed. Please see the
     4 -- COPYING file in the source package for more information.
     4 -- COPYING file in the source package for more information.
     5 --
     5 --
       
     6 
       
     7 local _G = _G;
       
     8 
       
     9 local prosody = _G.prosody;
       
    10 local hosts = prosody.hosts;
     6 
    11 
     7 local usermanager_user_exists = require "core.usermanager".user_exists;
    12 local usermanager_user_exists = require "core.usermanager".user_exists;
     8 local usermanager_create_user = require "core.usermanager".create_user;
    13 local usermanager_create_user = require "core.usermanager".create_user;
     9 local is_admin = require "core.usermanager".is_admin;
    14 local is_admin = require "core.usermanager".is_admin;
    10 
    15 
    79 		origin.send(st.reply(stanza):add_child(item:cmdtag("executing", sessionid):add_child(add_user_layout:form())));
    84 		origin.send(st.reply(stanza):add_child(item:cmdtag("executing", sessionid):add_child(add_user_layout:form())));
    80 	end
    85 	end
    81 	return true;
    86 	return true;
    82 end
    87 end
    83 
    88 
    84 local descriptor = adhoc_new("Add User", "http://jabber.org/protocol/admin#add-user", add_user_command_handler)
    89 function get_online_users_command_handler(item, origin, stanza)
       
    90 	if not is_admin(stanza.attr.from) then
       
    91 		origin.send(st.error_reply(stanza, "auth", "forbidden", "You don't have permission to request a list of online users"):up()
       
    92 			:add_child(item:cmdtag("canceled")
       
    93 				:tag("note", {type="error"}):text("You don't have permission to request a list of online users")));
       
    94 		return true;
       
    95 	end
       
    96 	local field = st.stanza("field", {label="The list of all online users", var="onlineuserjids", type="text-multi"});
       
    97 	for username, user in pairs(hosts[stanza.attr.to].sessions or {}) do
       
    98 		field:tag("value"):text(username.."@"..stanza.attr.to):up();
       
    99 	end
       
   100 	origin.send(st.reply(stanza):add_child(item:cmdtag("completed", uuid:generate())
       
   101 		:tag("x", {xmlns="jabber:x:data", type="result"})
       
   102 			:tag("field", {type="hidden", var="FORM_TYPE"})
       
   103 				:tag("value"):text("http://jabber.org/protocol/admin"):up():up()
       
   104 			:add_child(field)));
       
   105 
       
   106 	return true;
       
   107 end
       
   108 
       
   109 local add_user_desc = adhoc_new("Add User", "http://jabber.org/protocol/admin#add-user", add_user_command_handler);
       
   110 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); 
    85 
   111 
    86 function module.unload()
   112 function module.unload()
    87 	module:remove_item("adhoc", descriptor);
   113 	module:remove_item("adhoc", add_user_desc);
       
   114 	module:remove_item("adhoc", get_online_users_desc);
    88 end
   115 end
    89 
   116 
    90 module:add_item ("adhoc", descriptor);
   117 module:add_item("adhoc", add_user_desc);
       
   118 module:add_item("adhoc", get_online_users_desc);