mod_csi_simple: Consider messages encrypted payload as important (fixes part of ...
[prosody.git] / plugins / mod_user_account_management.lua
blob615c1ed6cb7e5aeb6803aa99c683bb6e4d25c810
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 --
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
10 local st = require "util.stanza";
11 local usermanager_set_password = require "core.usermanager".set_password;
12 local usermanager_delete_user = require "core.usermanager".delete_user;
13 local nodeprep = require "util.encodings".stringprep.nodeprep;
14 local jid_bare = require "util.jid".bare;
16 local compat = module:get_option_boolean("registration_compat", true);
18 module:add_feature("jabber:iq:register");
20 -- Password change and account deletion handler
21 local function handle_registration_stanza(event)
22 local session, stanza = event.origin, event.stanza;
23 local log = session.log or module._log;
25 local query = stanza.tags[1];
26 if stanza.attr.type == "get" then
27 local reply = st.reply(stanza);
28 reply:tag("query", {xmlns = "jabber:iq:register"})
29 :tag("registered"):up()
30 :tag("username"):text(session.username):up()
31 :tag("password"):up();
32 session.send(reply);
33 else -- stanza.attr.type == "set"
34 if query.tags[1] and query.tags[1].name == "remove" then
35 local username, host = session.username, session.host;
37 -- This one weird trick sends a reply to this stanza before the user is deleted
38 local old_session_close = session.close;
39 session.close = function(self, ...)
40 self.send(st.reply(stanza));
41 return old_session_close(self, ...);
42 end
44 local ok, err = usermanager_delete_user(username, host);
46 if not ok then
47 log("debug", "Removing user account %s@%s failed: %s", username, host, err);
48 session.close = old_session_close;
49 session.send(st.error_reply(stanza, "cancel", "service-unavailable", err));
50 return true;
51 end
53 log("info", "User removed their account: %s@%s", username, host);
54 module:fire_event("user-deregistered", { username = username, host = host, source = "mod_register", session = session });
55 else
56 local username = nodeprep(query:get_child_text("username"));
57 local password = query:get_child_text("password");
58 if username and password then
59 if username == session.username then
60 if usermanager_set_password(username, password, session.host, session.resource) then
61 session.send(st.reply(stanza));
62 else
63 -- TODO unable to write file, file may be locked, etc, what's the correct error?
64 session.send(st.error_reply(stanza, "wait", "internal-server-error"));
65 end
66 else
67 session.send(st.error_reply(stanza, "modify", "bad-request"));
68 end
69 else
70 session.send(st.error_reply(stanza, "modify", "bad-request"));
71 end
72 end
73 end
74 return true;
75 end
77 module:hook("iq/self/jabber:iq:register:query", handle_registration_stanza);
78 if compat then
79 module:hook("iq/host/jabber:iq:register:query", function (event)
80 local session, stanza = event.origin, event.stanza;
81 if session.type == "c2s" and jid_bare(stanza.attr.to) == session.host then
82 return handle_registration_stanza(event);
83 end
84 end);
85 end