comparison mod_adhoc_cmd_admin/mod_adhoc_cmd_admin.lua @ 37:6018c0370d89

merge
author Thilo Cestonaro <thilo@cestona.ro>
date Mon, 12 Oct 2009 23:09:21 +0200
parents 58d326d86a9a
children 4c4daa1f8ae7
comparison
equal deleted inserted replaced
31:a0dfa3e5883c 37:6018c0370d89
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 6
7 local st, jid, uuid = require "util.stanza", require "util.jid", require "util.uuid";
8 local usermanager_user_exists = require "core.usermanager".user_exists; 7 local usermanager_user_exists = require "core.usermanager".user_exists;
9 local usermanager_create_user = require "core.usermanager".create_user; 8 local usermanager_create_user = require "core.usermanager".create_user;
9 local is_admin = require "core.usermanager".is_admin;
10 10
11 local is_admin = require "core.usermanager".is_admin; 11 local st, jid, uuid = require "util.stanza", require "util.jid", require "util.uuid";
12 local admins = set.new(config.get(module:get_host(), "core", "admins")); 12 local dataforms_new = require "util.dataforms".new;
13 local adhoc_new = module:require "adhoc".new;
13 14
14 local sessions = {}; 15 local sessions = {};
16
17 local add_user_layout = dataforms_new{
18 title= "Adding a User";
19 instructions = "Fill out this form to add a user.";
20
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" };
23 { name = "password", type = "text-private", label = "The password for this account" };
24 { name = "password-verify", type = "text-private", label = "Retype password" };
25 };
15 26
16 function add_user_command_handler(item, origin, stanza) 27 function add_user_command_handler(item, origin, stanza)
17 if not is_admin(stanza.attr.from) then 28 if not is_admin(stanza.attr.from) then
18 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)));
19 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()
20 :tag("command", {xmlns="http://jabber.org/protocol/commands", 31 :add_child(item:cmdtag("canceled")
21 node="http://jabber.org/protocol/admin#add-user", status="canceled"}) 32 :tag("note", {type="error"}):text("You don't have permission to add a user")));
22 :tag("note", {type="error"}):text("You don't have permission to add a user"));
23 return true; 33 return true;
24 end 34 end
25 if stanza.tags[1].attr.sessionid and sessions[stanza.tags[1].attr.sessionid] then 35 if stanza.tags[1].attr.sessionid and sessions[stanza.tags[1].attr.sessionid] then
26 if stanza.tags[1].attr.action == "cancel" then 36 if stanza.tags[1].attr.action == "cancel" then
27 origin.send(st.reply(stanza):tag("command", {xmlns="http://jabber.org/protocol/commands", 37 origin.send(st.reply(stanza):add_child(item:cmdtag("canceled", stanza.tags[1].attr.sessionid)));
28 node="http://jabber.org/protocol/admin#add-user",
29 sessionid=stanza.tags[1].attr.sessionid, status="canceled"}));
30 sessions[stanza.tags[1].attr.sessionid] = nil; 38 sessions[stanza.tags[1].attr.sessionid] = nil;
31 return true; 39 return true;
32 end 40 end
33 for _, tag in ipairs(stanza.tags[1].tags) do 41 form = stanza.tags[1]:child_with_ns("jabber:x:data");
34 if tag.name == "x" and tag.attr.xmlns == "jabber:x:data" then 42 local fields = add_user_layout:data(form);
35 form = tag;
36 break;
37 end
38 end
39 local fields = {};
40 for _, field in ipairs(form.tags) do
41 if field.name == "field" and field.attr.var then
42 for i, tag in ipairs(field.tags) do
43 if tag.name == "value" and #tag.tags == 0 then
44 fields[field.attr.var] = tag[1] or "";
45 end
46 end
47 end
48 end
49 local username, host, resource = jid.split(fields.accountjid); 43 local username, host, resource = jid.split(fields.accountjid);
50 if (fields.password == fields["password-verify"]) and username and host and host == stanza.attr.to then 44 if (fields.password == fields["password-verify"]) and username and host and host == stanza.attr.to then
51 if usermanager_user_exists(username, host) then 45 if usermanager_user_exists(username, host) then
52 origin.send(st.error_reply(stanza, "cancel", "conflict", "Account already exists"):up() 46 origin.send(st.error_reply(stanza, "cancel", "conflict", "Account already exists"):up()
53 :tag("command", {xmlns="http://jabber.org/protocol/commands", 47 :add_child(item:cmdtag("canceled", stanza.tags[1].attr.sessionid)
54 node="http://jabber.org/protocol/admin#add-user", status="canceled"}) 48 :tag("note", {type="error"}):text("Account already exists")));
55 :tag("note", {type="error"}):text("Account already exists"));
56 sessions[stanza.tags[1].attr.sessionid] = nil; 49 sessions[stanza.tags[1].attr.sessionid] = nil;
57 return true; 50 return true;
58 else 51 else
59 if usermanager_create_user(username, fields.password, host) then 52 if usermanager_create_user(username, fields.password, host) then
60 origin.send(st.reply(stanza):tag("command", {xmlns="http://jabber.org/protocol/commands", 53 origin.send(st.reply(stanza):add_child(item:cmdtag("completed", stanza.tags[1].attr.sessionid)
61 node="http://jabber.org/protocol/admin#add-user", 54 :tag("note", {type="info"}):text("Account successfully created")));
62 sessionid=stanza.tags[1].attr.sessionid, status="completed"})
63 :tag("note", {type="info"}):text("Account successfully created"));
64 sessions[stanza.tags[1].attr.sessionid] = nil; 55 sessions[stanza.tags[1].attr.sessionid] = nil;
65 module:log("debug", "Created new account " .. username.."@"..host); 56 module:log("debug", "Created new account " .. username.."@"..host);
66 return true; 57 return true;
67 else 58 else
68 origin.send(st.error_reply(stanza, "wait", "internal-server-error", 59 origin.send(st.error_reply(stanza, "wait", "internal-server-error",
69 "Failed to write data to disk"):up() 60 "Failed to write data to disk"):up()
70 :tag("command", {xmlns="http://jabber.org/protocol/commands", 61 :add_child(item:cmdtag("canceled", stanza.tags[1].attr.sessionid)
71 node="http://jabber.org/protocol/admin#add-user", status="canceled"}) 62 :tag("note", {type="error"}):text("Failed to write data to disk")));
72 :tag("note", {type="error"}):text("Failed to write data to disk"));
73 sessions[stanza.tags[1].attr.sessionid] = nil; 63 sessions[stanza.tags[1].attr.sessionid] = nil;
74 return true; 64 return true;
75 end 65 end
76 end 66 end
77 else 67 else
78 module:log("debug", fields.accountjid .. " " .. fields.password .. " " .. fields["password-verify"]); 68 module:log("debug", fields.accountjid .. " " .. fields.password .. " " .. fields["password-verify"]);
79 origin.send(st.error_reply(stanza, "cancel", "conflict", 69 origin.send(st.error_reply(stanza, "cancel", "conflict",
80 "Invalid data.\nPasswords missmatch, or empy username"):up() 70 "Invalid data.\nPassword mismatch, or empty username"):up()
81 :tag("command", {xmlns="http://jabber.org/protocol/commands", 71 :add_child(item:cmdtag("canceled", stanza.tags[1].attr.sessionid)
82 node="http://jabber.org/protocol/admin#add-user", status="canceled"}) 72 :tag("note", {type="error"}):text("Invalid data.\nPassword mismatch, or empty username")));
83 :tag("note", {type="error"}):text("Invalid data.\nPasswords missmatch, or empy username"));
84 sessions[stanza.tags[1].attr.sessionid] = nil; 73 sessions[stanza.tags[1].attr.sessionid] = nil;
85 return true; 74 return true;
86 end 75 end
87 else 76 else
88 sessionid=uuid.generate(); 77 local sessionid=uuid.generate();
89 sessions[sessionid] = "executing"; 78 sessions[sessionid] = "executing";
90 origin.send(st.reply(stanza):tag("command", {xmlns="http://jabber.org/protocol/commands", 79 origin.send(st.reply(stanza):add_child(item:cmdtag("executing", sessionid):add_child(add_user_layout:form())));
91 node="http://jabber.org/protocol/admin#add-user", sessionid=sessionid,
92 status="executing"})
93 :tag("x", { xmlns = "jabber:x:data", type = "form" })
94 :tag("title"):text("Adding a User"):up()
95 :tag("instructions"):text("Fill out this form to add a user."):up()
96 :tag("field", { type = "hidden", var = "FORM_TYPE" })
97 :tag("value"):text("http://jabber.org/protocol/admin"):up():up()
98 :tag("field", { label = "The Jabber ID for the account to be added",
99 type = "jid-single", var = "accountjid" })
100 :tag("required"):up():up()
101 :tag("field", { label = "The password for this account",
102 type = "text-private", var = "password" }):up()
103 :tag("field", { label = "Retype password", type = "text-private",
104 var = "password-verify" }):up():up()
105 );
106 end 80 end
107 return true; 81 return true;
108 end 82 end
109 83
110 local descriptor = { name="Add User", node="http://jabber.org/protocol/admin#add-user", handler=add_user_command_handler }; 84 local descriptor = adhoc_new("Add User", "http://jabber.org/protocol/admin#add-user", add_user_command_handler)
111 85
112 function module.unload() 86 function module.unload()
113 module:remove_item("adhoc", descriptor); 87 module:remove_item("adhoc", descriptor);
114 end 88 end
115 89