net.http.server: Properly handle persistent connections
[prosody.git] / plugins / mod_admin_adhoc.lua
index efb7f72bea7a2bf6493cac42194ac2bd7fb7192f..9f3175d06804d3657719e2c6df0c8ab8e03b5d19 100644 (file)
@@ -14,8 +14,11 @@ local iterators = require "util.iterators";
 local keys, values = iterators.keys, iterators.values;
 local usermanager_user_exists = require "core.usermanager".user_exists;
 local usermanager_create_user = require "core.usermanager".create_user;
+local usermanager_delete_user = require "core.usermanager".delete_user;
 local usermanager_get_password = require "core.usermanager".get_password;
 local usermanager_set_password = require "core.usermanager".set_password;
+local hostmanager_activate = require "core.hostmanager".activate;
+local hostmanager_deactivate = require "core.hostmanager".deactivate;
 local is_admin = require "core.usermanager".is_admin;
 local rm_load_roster = require "core.rostermanager".load_roster;
 local st, jid, uuid = require "util.stanza", require "util.jid", require "util.uuid";
@@ -23,6 +26,7 @@ local timer_add_task = require "util.timer".add_task;
 local dataforms_new = require "util.dataforms".new;
 local array = require "util.array";
 local modulemanager = require "modulemanager";
+local core_post_stanza = prosody.core_post_stanza;
 
 module:depends("adhoc");
 local adhoc_new = module:require "adhoc".new;
@@ -63,19 +67,18 @@ function add_user_command_handler(self, data, state)
                                return { status = "completed", error = { message = "Account already exists" } };
                        else
                                if usermanager_create_user(username, fields.password, host) then
-                                       module:log("info", "Created new account " .. username.."@"..host);
+                                       module:log("info", "Created new account %s@%s", username, host);
                                        return { status = "completed", info = "Account successfully created" };
                                else
                                        return { status = "completed", error = { message = "Failed to write data to disk" } };
                                end
                        end
                else
-                       module:log("debug", (fields.accountjid or "<nil>") .. " " .. (fields.password or "<nil>") .. " "
-                               .. (fields["password-verify"] or "<nil>"));
+                       module:log("debug", "Invalid data, password mismatch or empty username while creating account for %s", fields.accountjid or "<nil>");
                        return { status = "completed", error = { message = "Invalid data.\nPassword mismatch, or empty username" } };
                end
        else
-               return { status = "executing", form = add_user_layout }, "executing";
+               return { status = "executing", actions = {"next", "complete", default = "complete"}, form = add_user_layout }, "executing";
        end
 end
 
@@ -107,7 +110,7 @@ function change_user_password_command_handler(self, data, state)
                        return { status = "completed", error = { message = "User does not exist" } };
                end
        else
-               return { status = "executing", form = change_user_password_layout }, "executing";
+               return { status = "executing", actions = {"next", "complete", default = "complete"}, form = change_user_password_layout }, "executing";
        end
 end
 
@@ -142,11 +145,11 @@ function delete_user_command_handler(self, data, state)
                local succeeded = {};
                for _, aJID in ipairs(fields.accountjids) do
                        local username, host, resource = jid.split(aJID);
-                       if (host == data.to) and  usermanager_user_exists(username, host) and disconnect_user(aJID) and usermanager_create_user(username, nil, host) then
-                               module:log("debug", "User " .. aJID .. " has been deleted");
+                       if (host == data.to) and  usermanager_user_exists(username, host) and usermanager_delete_user(username, host) then
+                               module:log("debug", "User %s has been deleted", aJID);
                                succeeded[#succeeded+1] = aJID;
                        else
-                               module:log("debug", "Tried to delete non-existant user "..aJID);
+                               module:log("debug", "Tried to delete non-existant user %s", aJID);
                                failed[#failed+1] = aJID;
                        end
                end
@@ -155,7 +158,7 @@ function delete_user_command_handler(self, data, state)
                                (#failed ~= 0 and
                                "The following accounts could not be deleted:\n"..t_concat(failed, "\n") or "") };
        else
-               return { status = "executing", form = delete_user_layout }, "executing";
+               return { status = "executing", actions = {"next", "complete", default = "complete"}, form = delete_user_layout }, "executing";
        end
 end
 
@@ -165,7 +168,7 @@ function disconnect_user(match_jid)
        local sessions = host.sessions[node] and host.sessions[node].sessions;
        for resource, session in pairs(sessions or {}) do
                if not givenResource or (resource == givenResource) then
-                       module:log("debug", "Disconnecting "..node.."@"..hostname.."/"..resource);
+                       module:log("debug", "Disconnecting %s@%s/%s", node, hostname, resource);
                        session:close();
                end
        end
@@ -205,19 +208,10 @@ function end_user_session_handler(self, data, state)
                                (#failed ~= 0 and
                                "The following accounts could not be disconnected:\n"..t_concat(failed, "\n") or "") };
        else
-               return { status = "executing", form = end_user_session_layout }, "executing";
+               return { status = "executing", actions = {"next", "complete", default = "complete"}, form = end_user_session_layout }, "executing";
        end
 end
 
-local end_user_session_layout = dataforms_new{
-       title = "Ending a User Session";
-       instructions = "Fill out this form to end a user's session.";
-
-       { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
-       { name = "accountjids", type = "jid-multi", label = "The Jabber ID(s) for which to end sessions" };
-};
-
-
 function get_user_password_handler(self, data, state)
        local get_user_password_layout = dataforms_new{
                title = "Getting User's Password";
@@ -254,7 +248,7 @@ function get_user_password_handler(self, data, state)
                end
                return { status = "completed", result = { layout = get_user_password_result_layout, values = {accountjid = accountjid, password = password} } };
        else
-               return { status = "executing", form = get_user_password_layout }, "executing";
+               return { status = "executing", actions = {"next", "complete", default = "complete"}, form = get_user_password_layout }, "executing";
        end
 end
 
@@ -312,7 +306,7 @@ function get_user_roster_handler(self, data, state)
                result:add_child(query);
                return { status = "completed", other = result };
        else
-               return { status = "executing", form = get_user_roster_layout }, "executing";
+               return { status = "executing", actions = {"next", "complete", default = "complete"}, form = get_user_roster_layout }, "executing";
        end
 end
 
@@ -365,7 +359,7 @@ function get_user_stats_handler(self, data, state)
                return { status = "completed", result = {layout = get_user_stats_result_layout, values = {ipaddresses = IPs, rostersize = tostring(rostersize),
                        onlineresources = resources}} };
        else
-               return { status = "executing", form = get_user_stats_layout }, "executing";
+               return { status = "executing", actions = {"next", "complete", default = "complete"}, form = get_user_stats_layout }, "executing";
        end
 end
 
@@ -425,7 +419,7 @@ function get_online_users_command_handler(self, data, state)
                end
                return { status = "completed", result = {layout = get_online_users_result_layout, values = {onlineuserjids=t_concat(users, "\n")}} };
        else
-               return { status = "executing", form = get_online_users_layout }, "executing";
+               return { status = "executing", actions = {"next", "complete", default = "complete"}, form = get_online_users_layout }, "executing";
        end
 end
 
@@ -469,8 +463,7 @@ function load_module_handler(self, data, state)
                        '". Error was: "'..tostring(err or "<unspecified>")..'"' } };
                end
        else
-               local modules = array.collect(keys(hosts[data.to].modules)):sort();
-               return { status = "executing", form = layout }, "executing";
+               return { status = "executing", actions = {"next", "complete", default = "complete"}, form = layout }, "executing";
        end
 end
 
@@ -504,7 +497,7 @@ function reload_modules_handler(self, data, state)
                return { status = "completed", info = info };
        else
                local modules = array.collect(keys(hosts[data.to].modules)):sort();
-               return { status = "executing", form = { layout = layout; values = { modules = modules } } }, "executing";
+               return { status = "executing", actions = {"next", "complete", default = "complete"}, form = { layout = layout; values = { modules = modules } } }, "executing";
        end
 end
 
@@ -568,7 +561,7 @@ function shut_down_service_handler(self, data, state)
 
                return { status = "completed", info = "Server is about to shut down" };
        else
-               return { status = "executing", form = shut_down_service_layout }, "executing";
+               return { status = "executing", actions = {"next", "complete", default = "complete"}, form = shut_down_service_layout }, "executing";
        end
 end
 
@@ -602,10 +595,67 @@ function unload_modules_handler(self, data, state)
                return { status = "completed", info = info };
        else
                local modules = array.collect(keys(hosts[data.to].modules)):sort();
-               return { status = "executing", form = { layout = layout; values = { modules = modules } } }, "executing";
+               return { status = "executing", actions = {"next", "complete", default = "complete"}, form = { layout = layout; values = { modules = modules } } }, "executing";
        end
 end
 
+function activate_host_handler(self, data, state)
+       local layout = dataforms_new {
+               title = "Activate host";
+               instructions = "";
+
+               { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/hosts#activate" };
+               { name = "host", type = "text-single", required = true, label = "Host:"};
+       };
+       if state then
+               if data.action == "cancel" then
+                       return { status = "canceled" };
+               end
+               local fields, err = layout:data(data.form);
+               if err then
+                       return generate_error_message(err);
+               end
+               local ok, err = hostmanager_activate(fields.host);
+
+               if ok then
+                       return { status = "completed", info = fields.host .. " activated" };
+               else
+                       return { status = "canceled", error = err }
+               end
+       else
+               return { status = "executing", actions = {"next", "complete", default = "complete"}, form = { layout = layout } }, "executing";
+       end
+end
+
+function deactivate_host_handler(self, data, state)
+       local layout = dataforms_new {
+               title = "Deactivate host";
+               instructions = "";
+
+               { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/hosts#activate" };
+               { name = "host", type = "text-single", required = true, label = "Host:"};
+       };
+       if state then
+               if data.action == "cancel" then
+                       return { status = "canceled" };
+               end
+               local fields, err = layout:data(data.form);
+               if err then
+                       return generate_error_message(err);
+               end
+               local ok, err = hostmanager_deactivate(fields.host);
+
+               if ok then
+                       return { status = "completed", info = fields.host .. " deactivated" };
+               else
+                       return { status = "canceled", error = err }
+               end
+       else
+               return { status = "executing", actions = {"next", "complete", default = "complete"}, form = { layout = layout } }, "executing";
+       end
+end
+
+
 local add_user_desc = adhoc_new("Add User", "http://jabber.org/protocol/admin#add-user", add_user_command_handler, "admin");
 local change_user_password_desc = adhoc_new("Change User Password", "http://jabber.org/protocol/admin#change-user-password", change_user_password_command_handler, "admin");
 local config_reload_desc = adhoc_new("Reload configuration", "http://prosody.im/protocol/config#reload", config_reload_handler, "global_admin");
@@ -620,6 +670,8 @@ local load_module_desc = adhoc_new("Load module", "http://prosody.im/protocol/mo
 local reload_modules_desc = adhoc_new("Reload modules", "http://prosody.im/protocol/modules#reload", reload_modules_handler, "admin");
 local shut_down_service_desc = adhoc_new("Shut Down Service", "http://jabber.org/protocol/admin#shutdown", shut_down_service_handler, "global_admin");
 local unload_modules_desc = adhoc_new("Unload modules", "http://prosody.im/protocol/modules#unload", unload_modules_handler, "admin");
+local activate_host_desc = adhoc_new("Activate host", "http://prosody.im/protocol/hosts#activate", activate_host_handler, "global_admin");
+local deactivate_host_desc = adhoc_new("Deactivate host", "http://prosody.im/protocol/hosts#deactivate", deactivate_host_handler, "global_admin");
 
 module:provides("adhoc", add_user_desc);
 module:provides("adhoc", change_user_password_desc);
@@ -635,3 +687,5 @@ module:provides("adhoc", load_module_desc);
 module:provides("adhoc", reload_modules_desc);
 module:provides("adhoc", shut_down_service_desc);
 module:provides("adhoc", unload_modules_desc);
+module:provides("adhoc", activate_host_desc);
+module:provides("adhoc", deactivate_host_desc);