annotate mod_adhoc_cmd_admin/mod_adhoc_cmd_admin.lua @ 36:58d326d86a9a

mod_adhoc: add adhoc.lib.lua to ease implementing new commands (as a consequence mod_adhoc is a directory now) mod_adhoc_cmd_*: Convert to use adhoc.lib.lua
author Florian Zeitz <florob@babelmonkeys.de>
date Sun, 11 Oct 2009 01:20:16 +0200
parents 3c49411d4aa3
children 4c4daa1f8ae7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
29
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
1 -- Copyright (C) 2009 Florian Zeitz
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
2 --
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
3 -- This file is MIT/X11 licensed. Please see the
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
4 -- COPYING file in the source package for more information.
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
5 --
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
6
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
7 local usermanager_user_exists = require "core.usermanager".user_exists;
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
8 local usermanager_create_user = require "core.usermanager".create_user;
36
58d326d86a9a mod_adhoc: add adhoc.lib.lua to ease implementing new commands (as a consequence mod_adhoc is a directory now)
Florian Zeitz <florob@babelmonkeys.de>
parents: 35
diff changeset
9 local is_admin = require "core.usermanager".is_admin;
29
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
10
36
58d326d86a9a mod_adhoc: add adhoc.lib.lua to ease implementing new commands (as a consequence mod_adhoc is a directory now)
Florian Zeitz <florob@babelmonkeys.de>
parents: 35
diff changeset
11 local st, jid, uuid = require "util.stanza", require "util.jid", require "util.uuid";
58d326d86a9a mod_adhoc: add adhoc.lib.lua to ease implementing new commands (as a consequence mod_adhoc is a directory now)
Florian Zeitz <florob@babelmonkeys.de>
parents: 35
diff changeset
12 local dataforms_new = require "util.dataforms".new;
58d326d86a9a mod_adhoc: add adhoc.lib.lua to ease implementing new commands (as a consequence mod_adhoc is a directory now)
Florian Zeitz <florob@babelmonkeys.de>
parents: 35
diff changeset
13 local adhoc_new = module:require "adhoc".new;
29
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
14
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
15 local sessions = {};
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
16
35
3c49411d4aa3 mod_adhoc_cmd_admin: Fixed style, some typos, and got down to <100LOC. Perhaps we need util.adhoc?
Matthew Wild <mwild1@gmail.com>
parents: 34
diff changeset
17 local add_user_layout = dataforms_new{
34
fc374b724270 mod_adhoc_cmd_admin: Define form layout only once.
Florian Zeitz <florob@babelmonkeys.de>
parents: 33
diff changeset
18 title= "Adding a User";
fc374b724270 mod_adhoc_cmd_admin: Define form layout only once.
Florian Zeitz <florob@babelmonkeys.de>
parents: 33
diff changeset
19 instructions = "Fill out this form to add a user.";
fc374b724270 mod_adhoc_cmd_admin: Define form layout only once.
Florian Zeitz <florob@babelmonkeys.de>
parents: 33
diff changeset
20
fc374b724270 mod_adhoc_cmd_admin: Define form layout only once.
Florian Zeitz <florob@babelmonkeys.de>
parents: 33
diff changeset
21 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
fc374b724270 mod_adhoc_cmd_admin: Define form layout only once.
Florian Zeitz <florob@babelmonkeys.de>
parents: 33
diff changeset
22 { name = "accountjid", type = "jid-single", required = true, label = "The Jabber ID for the account to be added" };
fc374b724270 mod_adhoc_cmd_admin: Define form layout only once.
Florian Zeitz <florob@babelmonkeys.de>
parents: 33
diff changeset
23 { name = "password", type = "text-private", label = "The password for this account" };
fc374b724270 mod_adhoc_cmd_admin: Define form layout only once.
Florian Zeitz <florob@babelmonkeys.de>
parents: 33
diff changeset
24 { name = "password-verify", type = "text-private", label = "Retype password" };
fc374b724270 mod_adhoc_cmd_admin: Define form layout only once.
Florian Zeitz <florob@babelmonkeys.de>
parents: 33
diff changeset
25 };
fc374b724270 mod_adhoc_cmd_admin: Define form layout only once.
Florian Zeitz <florob@babelmonkeys.de>
parents: 33
diff changeset
26
29
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
27 function add_user_command_handler(item, origin, stanza)
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
28 if not is_admin(stanza.attr.from) then
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
29 module:log("warn", "Non-admin %s tried to add a user", tostring(jid.bare(stanza.attr.from)));
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
30 origin.send(st.error_reply(stanza, "auth", "forbidden", "You don't have permission to add a user"):up()
36
58d326d86a9a mod_adhoc: add adhoc.lib.lua to ease implementing new commands (as a consequence mod_adhoc is a directory now)
Florian Zeitz <florob@babelmonkeys.de>
parents: 35
diff changeset
31 :add_child(item:cmdtag("canceled")
58d326d86a9a mod_adhoc: add adhoc.lib.lua to ease implementing new commands (as a consequence mod_adhoc is a directory now)
Florian Zeitz <florob@babelmonkeys.de>
parents: 35
diff changeset
32 :tag("note", {type="error"}):text("You don't have permission to add a user")));
29
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
33 return true;
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
34 end
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
35 if stanza.tags[1].attr.sessionid and sessions[stanza.tags[1].attr.sessionid] then
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
36 if stanza.tags[1].attr.action == "cancel" then
36
58d326d86a9a mod_adhoc: add adhoc.lib.lua to ease implementing new commands (as a consequence mod_adhoc is a directory now)
Florian Zeitz <florob@babelmonkeys.de>
parents: 35
diff changeset
37 origin.send(st.reply(stanza):add_child(item:cmdtag("canceled", stanza.tags[1].attr.sessionid)));
29
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
38 sessions[stanza.tags[1].attr.sessionid] = nil;
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
39 return true;
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
40 end
36
58d326d86a9a mod_adhoc: add adhoc.lib.lua to ease implementing new commands (as a consequence mod_adhoc is a directory now)
Florian Zeitz <florob@babelmonkeys.de>
parents: 35
diff changeset
41 form = stanza.tags[1]:child_with_ns("jabber:x:data");
34
fc374b724270 mod_adhoc_cmd_admin: Define form layout only once.
Florian Zeitz <florob@babelmonkeys.de>
parents: 33
diff changeset
42 local fields = add_user_layout:data(form);
29
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
43 local username, host, resource = jid.split(fields.accountjid);
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
44 if (fields.password == fields["password-verify"]) and username and host and host == stanza.attr.to then
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
45 if usermanager_user_exists(username, host) then
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
46 origin.send(st.error_reply(stanza, "cancel", "conflict", "Account already exists"):up()
36
58d326d86a9a mod_adhoc: add adhoc.lib.lua to ease implementing new commands (as a consequence mod_adhoc is a directory now)
Florian Zeitz <florob@babelmonkeys.de>
parents: 35
diff changeset
47 :add_child(item:cmdtag("canceled", stanza.tags[1].attr.sessionid)
58d326d86a9a mod_adhoc: add adhoc.lib.lua to ease implementing new commands (as a consequence mod_adhoc is a directory now)
Florian Zeitz <florob@babelmonkeys.de>
parents: 35
diff changeset
48 :tag("note", {type="error"}):text("Account already exists")));
29
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
49 sessions[stanza.tags[1].attr.sessionid] = nil;
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
50 return true;
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
51 else
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
52 if usermanager_create_user(username, fields.password, host) then
36
58d326d86a9a mod_adhoc: add adhoc.lib.lua to ease implementing new commands (as a consequence mod_adhoc is a directory now)
Florian Zeitz <florob@babelmonkeys.de>
parents: 35
diff changeset
53 origin.send(st.reply(stanza):add_child(item:cmdtag("completed", stanza.tags[1].attr.sessionid)
58d326d86a9a mod_adhoc: add adhoc.lib.lua to ease implementing new commands (as a consequence mod_adhoc is a directory now)
Florian Zeitz <florob@babelmonkeys.de>
parents: 35
diff changeset
54 :tag("note", {type="info"}):text("Account successfully created")));
29
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
55 sessions[stanza.tags[1].attr.sessionid] = nil;
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
56 module:log("debug", "Created new account " .. username.."@"..host);
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
57 return true;
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
58 else
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
59 origin.send(st.error_reply(stanza, "wait", "internal-server-error",
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
60 "Failed to write data to disk"):up()
36
58d326d86a9a mod_adhoc: add adhoc.lib.lua to ease implementing new commands (as a consequence mod_adhoc is a directory now)
Florian Zeitz <florob@babelmonkeys.de>
parents: 35
diff changeset
61 :add_child(item:cmdtag("canceled", stanza.tags[1].attr.sessionid)
58d326d86a9a mod_adhoc: add adhoc.lib.lua to ease implementing new commands (as a consequence mod_adhoc is a directory now)
Florian Zeitz <florob@babelmonkeys.de>
parents: 35
diff changeset
62 :tag("note", {type="error"}):text("Failed to write data to disk")));
29
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
63 sessions[stanza.tags[1].attr.sessionid] = nil;
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
64 return true;
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
65 end
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
66 end
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
67 else
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
68 module:log("debug", fields.accountjid .. " " .. fields.password .. " " .. fields["password-verify"]);
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
69 origin.send(st.error_reply(stanza, "cancel", "conflict",
35
3c49411d4aa3 mod_adhoc_cmd_admin: Fixed style, some typos, and got down to <100LOC. Perhaps we need util.adhoc?
Matthew Wild <mwild1@gmail.com>
parents: 34
diff changeset
70 "Invalid data.\nPassword mismatch, or empty username"):up()
36
58d326d86a9a mod_adhoc: add adhoc.lib.lua to ease implementing new commands (as a consequence mod_adhoc is a directory now)
Florian Zeitz <florob@babelmonkeys.de>
parents: 35
diff changeset
71 :add_child(item:cmdtag("canceled", stanza.tags[1].attr.sessionid)
58d326d86a9a mod_adhoc: add adhoc.lib.lua to ease implementing new commands (as a consequence mod_adhoc is a directory now)
Florian Zeitz <florob@babelmonkeys.de>
parents: 35
diff changeset
72 :tag("note", {type="error"}):text("Invalid data.\nPassword mismatch, or empty username")));
29
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
73 sessions[stanza.tags[1].attr.sessionid] = nil;
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
74 return true;
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
75 end
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
76 else
33
f3225c55288f mod_adhoc_cmd_admin: More utils.dataforms sugar
Florian Zeitz <florob@babelmonkeys.de>
parents: 32
diff changeset
77 local sessionid=uuid.generate();
29
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
78 sessions[sessionid] = "executing";
36
58d326d86a9a mod_adhoc: add adhoc.lib.lua to ease implementing new commands (as a consequence mod_adhoc is a directory now)
Florian Zeitz <florob@babelmonkeys.de>
parents: 35
diff changeset
79 origin.send(st.reply(stanza):add_child(item:cmdtag("executing", sessionid):add_child(add_user_layout:form())));
29
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
80 end
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
81 return true;
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
82 end
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
83
36
58d326d86a9a mod_adhoc: add adhoc.lib.lua to ease implementing new commands (as a consequence mod_adhoc is a directory now)
Florian Zeitz <florob@babelmonkeys.de>
parents: 35
diff changeset
84 local descriptor = adhoc_new("Add User", "http://jabber.org/protocol/admin#add-user", add_user_command_handler)
29
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
85
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
86 function module.unload()
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
87 module:remove_item("adhoc", descriptor);
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
88 end
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
89
b384999b047f mod_adhoc_cmd_admin: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
90 module:add_item ("adhoc", descriptor);