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