comparison mod_adhoc_cmd_admin/mod_adhoc_cmd_admin.lua @ 123:c04443ea114c

mod_adhoc, mod_adhoc_cmd_admin: use util.datforms for type="result" forms
author Florian Zeitz <florob@babelmonkeys.de>
date Fri, 22 Jan 2010 19:05:28 +0100
parents a9898f13c89e
children 843cadf36306
comparison
equal deleted inserted replaced
122:c3a874eec712 123:c04443ea114c
56 56
57 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" }; 57 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
58 { name = "accountjid", type = "jid-single", label = "The Jabber ID for which to retrieve the password" }; 58 { name = "accountjid", type = "jid-single", label = "The Jabber ID for which to retrieve the password" };
59 }; 59 };
60 60
61 local get_user_password_result_layout = dataforms_new{
62 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
63 { name = "accountjid", type = "jid-single", label = "JID" };
64 { name = "password", type = "text-single", label = "Password" };
65 };
66
61 local get_online_users_layout = dataforms_new{ 67 local get_online_users_layout = dataforms_new{
62 title = "Getting List of Online Users"; 68 title = "Getting List of Online Users";
63 instructions = "How many users should be returned at most?"; 69 instructions = "How many users should be returned at most?";
64 70
65 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" }; 71 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
66 { name = "max_items", type = "list-single", label = "Maximum number of users", 72 { name = "max_items", type = "list-single", label = "Maximum number of users",
67 value = { "25", "50", "75", "100", "150", "200", "all" } }; 73 value = { "25", "50", "75", "100", "150", "200", "all" } };
74 };
75
76 local get_online_users_result_layout = dataforms_new{
77 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
78 { name = "onlineuserjids", type = "text-multi", label = "The list of all online users" };
68 }; 79 };
69 80
70 local announce_layout = dataforms_new{ 81 local announce_layout = dataforms_new{
71 title = "Making an Announcement"; 82 title = "Making an Announcement";
72 instructions = "Fill out this form to make an announcement to all\nactive users of this service."; 83 instructions = "Fill out this form to make an announcement to all\nactive users of this service.";
171 if data.action == "cancel" then 182 if data.action == "cancel" then
172 sessions[sessid] = nil; 183 sessions[sessid] = nil;
173 return { status = "canceled" }, sessid; 184 return { status = "canceled" }, sessid;
174 end 185 end
175 local fields = get_user_password_layout:data(data.form); 186 local fields = get_user_password_layout:data(data.form);
176 local accountjid = st.stanza("field", {var="accountjid", label = "JID", type="jid-single"});
177 local password = st.stanza("field", {var="password", label = "Password", type="text-single"});
178 local user, host, resource = jid.split(fields.accountjid); 187 local user, host, resource = jid.split(fields.accountjid);
188 local accountjid = "";
189 local password = "";
179 if usermanager_user_exists(user, host) then 190 if usermanager_user_exists(user, host) then
180 accountjid:tag("value"):text(fields.accountjid):up(); 191 accountjid = fields.accountjid;
181 password:tag("value"):text(usermanager_get_password(user, host)):up(); 192 password = usermanager_get_password(user, host);
182 else 193 else
183 sessions[sessid] = nil; 194 sessions[sessid] = nil;
184 return { status = "error", error = { type = "cancel", condition = "item-not-found", message = "User does not exist" } }, sessid; 195 return { status = "error", error = { type = "cancel", condition = "item-not-found", message = "User does not exist" } }, sessid;
185 end 196 end
186 sessions[sessid] = nil; 197 sessions[sessid] = nil;
187 return { status = "completed", other = st.stanza("x", {xmlns="jabber:x:data", type="result"}) 198 return { status = "completed", result = { layout = get_user_password_result_layout, data = {accountjid = accountjid, password = password} } }, sessid;
188 :tag("field", {type="hidden", var="FORM_TYPE"})
189 :tag("value"):text("http://jabber.org/protocol/admin"):up():up()
190 :add_child(accountjid)
191 :add_child(password) }, sessid;
192 else 199 else
193 local sessionid=uuid.generate(); 200 local sessionid=uuid.generate();
194 sessions[sessionid] = "executing"; 201 sessions[sessionid] = "executing";
195 return { status = "executing", form = get_user_password_layout }, sessionid; 202 return { status = "executing", form = get_user_password_layout }, sessionid;
196 end 203 end
208 local max_items = nil 215 local max_items = nil
209 if fields.max_items ~= "all" then 216 if fields.max_items ~= "all" then
210 max_items = tonumber(fields.max_items); 217 max_items = tonumber(fields.max_items);
211 end 218 end
212 local count = 0; 219 local count = 0;
213 local field = st.stanza("field", {label="The list of all online users", var="onlineuserjids", type="text-multi"}); 220 local users = nil;
214 for username, user in pairs(hosts[data.to].sessions or {}) do 221 for username, user in pairs(hosts[data.to].sessions or {}) do
215 if (max_items ~= nil) and (count >= max_items) then 222 if (max_items ~= nil) and (count >= max_items) then
216 break; 223 break;
217 end 224 end
218 field:tag("value"):text(username.."@"..data.to):up(); 225 users = ((users and users.."\n") or "")..(username.."@"..data.to);
219 count = count + 1; 226 count = count + 1;
220 end 227 end
221 sessions[sessid] = nil; 228 sessions[sessid] = nil;
222 return { status = "completed", other = st.stanza("x", {xmlns="jabber:x:data", type="result"}) 229 return { status = "completed", result = {layout = get_online_users_result_layout, data = {onlineuserjids=users}} }, sessid;
223 :tag("field", {type="hidden", var="FORM_TYPE"})
224 :tag("value"):text("http://jabber.org/protocol/admin"):up():up()
225 :add_child(field) }, sessid;
226 else 230 else
227 local sessionid=uuid.generate(); 231 local sessionid=uuid.generate();
228 sessions[sessionid] = "executing"; 232 sessions[sessionid] = "executing";
229 return { status = "executing", form = get_online_users_layout }, sessionid; 233 return { status = "executing", form = get_online_users_layout }, sessionid;
230 end 234 end