Mercurial > prosody-modules
comparison 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 |
comparison
equal
deleted
inserted
replaced
35:3c49411d4aa3 | 36:58d326d86a9a |
---|---|
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 usermanager_user_exists = require "core.usermanager".user_exists; | |
8 local usermanager_create_user = require "core.usermanager".create_user; | |
9 local is_admin = require "core.usermanager".is_admin; | |
10 | |
7 local st, jid, uuid = require "util.stanza", require "util.jid", require "util.uuid"; | 11 local st, jid, uuid = require "util.stanza", require "util.jid", require "util.uuid"; |
8 local dataforms_new = require "util.dataforms".new; | 12 local dataforms_new = require "util.dataforms".new; |
9 local usermanager_user_exists = require "core.usermanager".user_exists; | 13 local adhoc_new = module:require "adhoc".new; |
10 local usermanager_create_user = require "core.usermanager".create_user; | |
11 | |
12 local is_admin = require "core.usermanager".is_admin; | |
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 = dataforms_new{ | 17 local add_user_layout = dataforms_new{ |
18 title= "Adding a User"; | 18 title= "Adding a User"; |
26 | 26 |
27 function add_user_command_handler(item, origin, stanza) | 27 function add_user_command_handler(item, origin, stanza) |
28 if not is_admin(stanza.attr.from) then | 28 if not is_admin(stanza.attr.from) then |
29 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))); |
30 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() |
31 :tag("command", {xmlns="http://jabber.org/protocol/commands", | 31 :add_child(item:cmdtag("canceled") |
32 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"))); |
33 :tag("note", {type="error"}):text("You don't have permission to add a user")); | |
34 return true; | 33 return true; |
35 end | 34 end |
36 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 |
37 if stanza.tags[1].attr.action == "cancel" then | 36 if stanza.tags[1].attr.action == "cancel" then |
38 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))); |
39 node="http://jabber.org/protocol/admin#add-user", | |
40 sessionid=stanza.tags[1].attr.sessionid, status="canceled"})); | |
41 sessions[stanza.tags[1].attr.sessionid] = nil; | 38 sessions[stanza.tags[1].attr.sessionid] = nil; |
42 return true; | 39 return true; |
43 end | 40 end |
44 form = stanza.tags[1]:find_child_with_ns("jabber:x:data"); | 41 form = stanza.tags[1]:child_with_ns("jabber:x:data"); |
45 local fields = add_user_layout:data(form); | 42 local fields = add_user_layout:data(form); |
46 local username, host, resource = jid.split(fields.accountjid); | 43 local username, host, resource = jid.split(fields.accountjid); |
47 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 |
48 if usermanager_user_exists(username, host) then | 45 if usermanager_user_exists(username, host) then |
49 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() |
50 :tag("command", {xmlns="http://jabber.org/protocol/commands", | 47 :add_child(item:cmdtag("canceled", stanza.tags[1].attr.sessionid) |
51 node="http://jabber.org/protocol/admin#add-user", status="canceled"}) | 48 :tag("note", {type="error"}):text("Account already exists"))); |
52 :tag("note", {type="error"}):text("Account already exists")); | |
53 sessions[stanza.tags[1].attr.sessionid] = nil; | 49 sessions[stanza.tags[1].attr.sessionid] = nil; |
54 return true; | 50 return true; |
55 else | 51 else |
56 if usermanager_create_user(username, fields.password, host) then | 52 if usermanager_create_user(username, fields.password, host) then |
57 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) |
58 node="http://jabber.org/protocol/admin#add-user", | 54 :tag("note", {type="info"}):text("Account successfully created"))); |
59 sessionid=stanza.tags[1].attr.sessionid, status="completed"}) | |
60 :tag("note", {type="info"}):text("Account successfully created")); | |
61 sessions[stanza.tags[1].attr.sessionid] = nil; | 55 sessions[stanza.tags[1].attr.sessionid] = nil; |
62 module:log("debug", "Created new account " .. username.."@"..host); | 56 module:log("debug", "Created new account " .. username.."@"..host); |
63 return true; | 57 return true; |
64 else | 58 else |
65 origin.send(st.error_reply(stanza, "wait", "internal-server-error", | 59 origin.send(st.error_reply(stanza, "wait", "internal-server-error", |
66 "Failed to write data to disk"):up() | 60 "Failed to write data to disk"):up() |
67 :tag("command", {xmlns="http://jabber.org/protocol/commands", | 61 :add_child(item:cmdtag("canceled", stanza.tags[1].attr.sessionid) |
68 node="http://jabber.org/protocol/admin#add-user", status="canceled"}) | 62 :tag("note", {type="error"}):text("Failed to write data to disk"))); |
69 :tag("note", {type="error"}):text("Failed to write data to disk")); | |
70 sessions[stanza.tags[1].attr.sessionid] = nil; | 63 sessions[stanza.tags[1].attr.sessionid] = nil; |
71 return true; | 64 return true; |
72 end | 65 end |
73 end | 66 end |
74 else | 67 else |
75 module:log("debug", fields.accountjid .. " " .. fields.password .. " " .. fields["password-verify"]); | 68 module:log("debug", fields.accountjid .. " " .. fields.password .. " " .. fields["password-verify"]); |
76 origin.send(st.error_reply(stanza, "cancel", "conflict", | 69 origin.send(st.error_reply(stanza, "cancel", "conflict", |
77 "Invalid data.\nPassword mismatch, or empty username"):up() | 70 "Invalid data.\nPassword mismatch, or empty username"):up() |
78 :tag("command", {xmlns="http://jabber.org/protocol/commands", | 71 :add_child(item:cmdtag("canceled", stanza.tags[1].attr.sessionid) |
79 node="http://jabber.org/protocol/admin#add-user", status="canceled"}) | 72 :tag("note", {type="error"}):text("Invalid data.\nPassword mismatch, or empty username"))); |
80 :tag("note", {type="error"}):text("Invalid data.\nPassword mismatch, or empty username")); | |
81 sessions[stanza.tags[1].attr.sessionid] = nil; | 73 sessions[stanza.tags[1].attr.sessionid] = nil; |
82 return true; | 74 return true; |
83 end | 75 end |
84 else | 76 else |
85 local sessionid=uuid.generate(); | 77 local sessionid=uuid.generate(); |
86 sessions[sessionid] = "executing"; | 78 sessions[sessionid] = "executing"; |
87 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()))); |
88 node="http://jabber.org/protocol/admin#add-user", sessionid=sessionid, | |
89 status="executing"}):add_child(add_user_layout:form())); | |
90 end | 80 end |
91 return true; | 81 return true; |
92 end | 82 end |
93 | 83 |
94 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) |
95 | 85 |
96 function module.unload() | 86 function module.unload() |
97 module:remove_item("adhoc", descriptor); | 87 module:remove_item("adhoc", descriptor); |
98 end | 88 end |
99 | 89 |