Comparison

mod_adhoc_cmd_admin/mod_adhoc_cmd_admin.lua @ 35:3c49411d4aa3

mod_adhoc_cmd_admin: Fixed style, some typos, and got down to <100LOC. Perhaps we need util.adhoc?
author Matthew Wild <mwild1@gmail.com>
date Sat, 10 Oct 2009 09:33:44 +0100
parent 34:fc374b724270
child 36:58d326d86a9a
comparison
equal deleted inserted replaced
34:fc374b724270 35:3c49411d4aa3
12 local is_admin = require "core.usermanager".is_admin; 12 local is_admin = require "core.usermanager".is_admin;
13 local admins = set.new(config.get(module:get_host(), "core", "admins")); 13 local admins = set.new(config.get(module:get_host(), "core", "admins"));
14 14
15 local sessions = {}; 15 local sessions = {};
16 16
17 local add_user_layout = { 17 local add_user_layout = dataforms_new{
18 title= "Adding a User"; 18 title= "Adding a User";
19 instructions = "Fill out this form to add a user."; 19 instructions = "Fill out this form to add a user.";
20 20
21 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" }; 21 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
22 { name = "accountjid", type = "jid-single", required = true, label = "The Jabber ID for the account to be added" }; 22 { name = "accountjid", type = "jid-single", required = true, label = "The Jabber ID for the account to be added" };
23 { name = "password", type = "text-private", label = "The password for this account" }; 23 { name = "password", type = "text-private", label = "The password for this account" };
24 { name = "password-verify", type = "text-private", label = "Retype password" }; 24 { name = "password-verify", type = "text-private", label = "Retype password" };
25 }; 25 };
26 dataforms_new(add_user_layout)
27 26
28 function add_user_command_handler(item, origin, stanza) 27 function add_user_command_handler(item, origin, stanza)
29 if not is_admin(stanza.attr.from) then 28 if not is_admin(stanza.attr.from) then
30 module:log("warn", "Non-admin %s tried to add a user", tostring(jid.bare(stanza.attr.from))); 29 module:log("warn", "Non-admin %s tried to add a user", tostring(jid.bare(stanza.attr.from)));
31 origin.send(st.error_reply(stanza, "auth", "forbidden", "You don't have permission to add a user"):up() 30 origin.send(st.error_reply(stanza, "auth", "forbidden", "You don't have permission to add a user"):up()
40 node="http://jabber.org/protocol/admin#add-user", 39 node="http://jabber.org/protocol/admin#add-user",
41 sessionid=stanza.tags[1].attr.sessionid, status="canceled"})); 40 sessionid=stanza.tags[1].attr.sessionid, status="canceled"}));
42 sessions[stanza.tags[1].attr.sessionid] = nil; 41 sessions[stanza.tags[1].attr.sessionid] = nil;
43 return true; 42 return true;
44 end 43 end
45 for _, tag in ipairs(stanza.tags[1].tags) do 44 form = stanza.tags[1]:find_child_with_ns("jabber:x:data");
46 if tag.name == "x" and tag.attr.xmlns == "jabber:x:data" then
47 form = tag;
48 break;
49 end
50 end
51 local fields = add_user_layout:data(form); 45 local fields = add_user_layout:data(form);
52 local username, host, resource = jid.split(fields.accountjid); 46 local username, host, resource = jid.split(fields.accountjid);
53 if (fields.password == fields["password-verify"]) and username and host and host == stanza.attr.to then 47 if (fields.password == fields["password-verify"]) and username and host and host == stanza.attr.to then
54 if usermanager_user_exists(username, host) then 48 if usermanager_user_exists(username, host) then
55 origin.send(st.error_reply(stanza, "cancel", "conflict", "Account already exists"):up() 49 origin.send(st.error_reply(stanza, "cancel", "conflict", "Account already exists"):up()
78 end 72 end
79 end 73 end
80 else 74 else
81 module:log("debug", fields.accountjid .. " " .. fields.password .. " " .. fields["password-verify"]); 75 module:log("debug", fields.accountjid .. " " .. fields.password .. " " .. fields["password-verify"]);
82 origin.send(st.error_reply(stanza, "cancel", "conflict", 76 origin.send(st.error_reply(stanza, "cancel", "conflict",
83 "Invalid data.\nPasswords missmatch, or empy username"):up() 77 "Invalid data.\nPassword mismatch, or empty username"):up()
84 :tag("command", {xmlns="http://jabber.org/protocol/commands", 78 :tag("command", {xmlns="http://jabber.org/protocol/commands",
85 node="http://jabber.org/protocol/admin#add-user", status="canceled"}) 79 node="http://jabber.org/protocol/admin#add-user", status="canceled"})
86 :tag("note", {type="error"}):text("Invalid data.\nPasswords missmatch, or empy username")); 80 :tag("note", {type="error"}):text("Invalid data.\nPassword mismatch, or empty username"));
87 sessions[stanza.tags[1].attr.sessionid] = nil; 81 sessions[stanza.tags[1].attr.sessionid] = nil;
88 return true; 82 return true;
89 end 83 end
90 else 84 else
91 local sessionid=uuid.generate(); 85 local sessionid=uuid.generate();