Software /
code /
prosody-modules
Comparison
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 |
child | 32:00d5e133c84d |
comparison
equal
deleted
inserted
replaced
28:b9d063dd16d5 | 29:b384999b047f |
---|---|
1 -- Copyright (C) 2009 Florian Zeitz | |
2 -- | |
3 -- This file is MIT/X11 licensed. Please see the | |
4 -- COPYING file in the source package for more information. | |
5 -- | |
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; | |
9 local usermanager_create_user = require "core.usermanager".create_user; | |
10 | |
11 local is_admin = require "core.usermanager".is_admin; | |
12 local admins = set.new(config.get(module:get_host(), "core", "admins")); | |
13 | |
14 local sessions = {}; | |
15 | |
16 function add_user_command_handler(item, origin, stanza) | |
17 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))); | |
19 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", | |
21 node="http://jabber.org/protocol/admin#add-user", status="canceled"}) | |
22 :tag("note", {type="error"}):text("You don't have permission to add a user")); | |
23 return true; | |
24 end | |
25 if stanza.tags[1].attr.sessionid and sessions[stanza.tags[1].attr.sessionid] then | |
26 if stanza.tags[1].attr.action == "cancel" then | |
27 origin.send(st.reply(stanza):tag("command", {xmlns="http://jabber.org/protocol/commands", | |
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; | |
31 return true; | |
32 end | |
33 for _, tag in ipairs(stanza.tags[1].tags) do | |
34 if tag.name == "x" and tag.attr.xmlns == "jabber:x:data" then | |
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); | |
50 if (fields.password == fields["password-verify"]) and username and host and host == stanza.attr.to then | |
51 if usermanager_user_exists(username, host) then | |
52 origin.send(st.error_reply(stanza, "cancel", "conflict", "Account already exists"):up() | |
53 :tag("command", {xmlns="http://jabber.org/protocol/commands", | |
54 node="http://jabber.org/protocol/admin#add-user", status="canceled"}) | |
55 :tag("note", {type="error"}):text("Account already exists")); | |
56 sessions[stanza.tags[1].attr.sessionid] = nil; | |
57 return true; | |
58 else | |
59 if usermanager_create_user(username, fields.password, host) then | |
60 origin.send(st.reply(stanza):tag("command", {xmlns="http://jabber.org/protocol/commands", | |
61 node="http://jabber.org/protocol/admin#add-user", | |
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; | |
65 module:log("debug", "Created new account " .. username.."@"..host); | |
66 return true; | |
67 else | |
68 origin.send(st.error_reply(stanza, "wait", "internal-server-error", | |
69 "Failed to write data to disk"):up() | |
70 :tag("command", {xmlns="http://jabber.org/protocol/commands", | |
71 node="http://jabber.org/protocol/admin#add-user", status="canceled"}) | |
72 :tag("note", {type="error"}):text("Failed to write data to disk")); | |
73 sessions[stanza.tags[1].attr.sessionid] = nil; | |
74 return true; | |
75 end | |
76 end | |
77 else | |
78 module:log("debug", fields.accountjid .. " " .. fields.password .. " " .. fields["password-verify"]); | |
79 origin.send(st.error_reply(stanza, "cancel", "conflict", | |
80 "Invalid data.\nPasswords missmatch, or empy username"):up() | |
81 :tag("command", {xmlns="http://jabber.org/protocol/commands", | |
82 node="http://jabber.org/protocol/admin#add-user", status="canceled"}) | |
83 :tag("note", {type="error"}):text("Invalid data.\nPasswords missmatch, or empy username")); | |
84 sessions[stanza.tags[1].attr.sessionid] = nil; | |
85 return true; | |
86 end | |
87 else | |
88 sessionid=uuid.generate(); | |
89 sessions[sessionid] = "executing"; | |
90 origin.send(st.reply(stanza):tag("command", {xmlns="http://jabber.org/protocol/commands", | |
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 | |
107 return true; | |
108 end | |
109 | |
110 local descriptor = { name="Add User", node="http://jabber.org/protocol/admin#add-user", handler=add_user_command_handler }; | |
111 | |
112 function module.unload() | |
113 module:remove_item("adhoc", descriptor); | |
114 end | |
115 | |
116 module:add_item ("adhoc", descriptor); |