Merge 0.8->trunk.
[prosody.git] / plugins / mod_register.lua
index 1e916e735cdb80e7f19f9582ee6dc44398b0b111..25c09dfa69a2c0013bd5e474a04ccb50348c7913 100644 (file)
@@ -13,12 +13,16 @@ local datamanager = require "util.datamanager";
 local usermanager_user_exists = require "core.usermanager".user_exists;
 local usermanager_create_user = require "core.usermanager".create_user;
 local usermanager_set_password = require "core.usermanager".set_password;
+local usermanager_delete_user = require "core.usermanager".delete_user;
 local os_time = os.time;
 local nodeprep = require "util.encodings".stringprep.nodeprep;
+local jid_bare = require "util.jid".bare;
+
+local compat = module:get_option_boolean("registration_compat", true);
 
 module:add_feature("jabber:iq:register");
 
-module:hook("iq/self/jabber:iq:register:query", function(event)
+local function handle_registration_stanza(event)
        local session, stanza = event.origin, event.stanza;
 
        local query = stanza.tags[1];
@@ -33,11 +37,15 @@ module:hook("iq/self/jabber:iq:register:query", function(event)
                if query.tags[1] and query.tags[1].name == "remove" then
                        -- TODO delete user auth data, send iq response, kick all user resources with a <not-authorized/>, delete all user data
                        local username, host = session.username, session.host;
-                       --session.send(st.error_reply(stanza, "cancel", "not-allowed"));
-                       --return;
-                       usermanager_set_password(username, nil, host); -- Disable account
-                       -- FIXME the disabling currently allows a different user to recreate the account
-                       -- we should add an in-memory account block mode when we have threading
+                       
+                       local ok, err = usermanager_delete_user(username, host);
+                       
+                       if not ok then
+                               module:log("debug", "Removing user account %s@%s failed: %s", username, host, err);
+                               session.send(st.error_reply(stanza, "cancel", "service-unavailable", err));
+                               return true;
+                       end
+                       
                        session.send(st.reply(stanza));
                        local roster = session.roster;
                        for _, session in pairs(hosts[host].sessions[username].sessions) do -- disconnect all resources
@@ -60,16 +68,12 @@ module:hook("iq/self/jabber:iq:register:query", function(event)
                        end
                        datamanager.store(username, host, "roster", nil);
                        datamanager.store(username, host, "privacy", nil);
-                       datamanager.store(username, host, "accounts", nil); -- delete accounts datastore at the end
                        module:log("info", "User removed their account: %s@%s", username, host);
                        module:fire_event("user-deregistered", { username = username, host = host, source = "mod_register", session = session });
                else
-                       local username = query:child_with_name("username");
-                       local password = query:child_with_name("password");
+                       local username = nodeprep(query:get_child("username"):get_text());
+                       local password = query:get_child("password"):get_text();
                        if username and password then
-                               -- FIXME shouldn't use table.concat
-                               username = nodeprep(table.concat(username));
-                               password = table.concat(password);
                                if username == session.username then
                                        if usermanager_set_password(username, password, session.host) then
                                                session.send(st.reply(stanza));
@@ -86,7 +90,17 @@ module:hook("iq/self/jabber:iq:register:query", function(event)
                end
        end
        return true;
-end);
+end
+
+module:hook("iq/self/jabber:iq:register:query", handle_registration_stanza);
+if compat then
+       module:hook("iq/host/jabber:iq:register:query", function (event)
+               local session, stanza = event.origin, event.stanza;
+               if session.type == "c2s" and jid_bare(stanza.attr.to) == session.host then
+                       return handle_registration_stanza(event);
+               end
+       end);
+end
 
 local recent_ips = {};
 local min_seconds_between_registrations = module:get_option("min_seconds_between_registrations");
@@ -151,7 +165,7 @@ module:hook("stanza/iq/jabber:iq:register:query", function(event)
                                                if usermanager_create_user(username, password, host) then
                                                        session.send(st.reply(stanza)); -- user created!
                                                        module:log("info", "User account created: %s@%s", username, host);
-                                                       module:fire_event("user-registered", { 
+                                                       module:fire_event("user-registered", {
                                                                username = username, host = host, source = "mod_register",
                                                                session = session });
                                                else
@@ -167,4 +181,3 @@ module:hook("stanza/iq/jabber:iq:register:query", function(event)
        end
        return true;
 end);
-