<commit_before>-- Copyright (C) 2009-2010 <NAME>
--
-- This file is MIT/X11 licensed. Please see the
-- COPYING file in the source package for more information.
--

local _G = _G;

local prosody = _G.prosody;
local hosts = prosody.hosts;

require "util.iterators";
local dataforms_new = require "util.dataforms".new;
local array = require "util.array";
local adhoc_new = module:require "adhoc".new;

function list_modules_handler(self, data, state)
	local list_modules_result = dataforms_new {
		title = "List of loaded modules";

		{ name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#list" };
		{ name = "modules", type = "text-multi", label = "The following modules are loaded:" };
	};

	local modules = array.collect(keys(hosts[data.to].modules)):sort()
	local modules_str = nil;
	for _, name in ipairs(modules) do
		modules_str = ((modules_str and modules_str .. "\n") or "") .. name;
	end

	return { status = "completed", result = { layout = list_modules_result; data = { modules = modules_str } } };
end

local list_modules_desc = adhoc_new("List loaded modules", "http://prosody.im/protocol/modules#list", list_modules_handler, "admin");

module:add_item("adhoc", list_modules_desc);<commit_msg>mod_adhoc_cmd_modules: Add "Reload module" command<commit_after>-- Copyright (C) 2009-2010 <NAME>
--
-- This file is MIT/X11 licensed. Please see the
-- COPYING file in the source package for more information.
--

local _G = _G;

local prosody = _G.prosody;
local hosts = prosody.hosts;

require "util.iterators";
local dataforms_new = require "util.dataforms".new;
local array = require "util.array";
local modulemanager = require "modulemanager";
local adhoc_new = module:require "adhoc".new;

function list_modules_handler(self, data, state)
	local result = dataforms_new {
		title = "List of loaded modules";

		{ name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#list" };
		{ name = "modules", type = "text-multi", label = "The following modules are loaded:" };
	};

	local modules = array.collect(keys(hosts[data.to].modules)):sort():concat("\n");

	return { status = "completed", result = { layout = result; data = { modules = modules } } };
end

-- TODO: Allow reloading multiple modules (depends on list-multi
function reload_modules_handler(self, data, state)
	local modules = {};
	local layout = dataforms_new {
		title = "Reload module";
		instructions = "Select the module to be reloaded";

		{ name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#reload" };
		{ name = "module", type = "list-single", value = modules, label = "Module to be reloaded:"};
	};
	if state then
		if data.action == "cancel" then
			return { status = "canceled" };
		end
		fields = layout:data(data.form);
		local ok, err = modulemanager.reload(data.to, fields.module);
		if ok then
			return { status = "completed", info = 'Module "'..fields.module..'" successfully reloaded on host "'..data.to..'".' };
		else
			return { status = "completed", error = 'Failed to reload module "'..fields.module..'" on host "'..data.to..
			'". Error was: "'..tostring(err)..'"' };
		end
	else
		local modules2 = array.collect(keys(hosts[data.to].modules)):sort();
		for i, val in ipairs(modules2) do
			modules[i] = val;
		end
		return { status = "executing", form = layout }, "executing";
	end
end

local list_modules_desc = adhoc_new("List loaded modules", "http://prosody.im/protocol/modules#list", list_modules_handler, "admin");
local reload_modules_desc = adhoc_new("Reload module", "http://prosody.im/protocol/modules#reload", reload_modules_handler, "admin");

module:add_item("adhoc", list_modules_desc);
module:add_item("adhoc", reload_modules_desc);
{'fraction_non_alphanumeric': 0.15075107296137338, 'fraction_numerical': 0.005901287553648069, 'mean_word_length': 4.491899852724595, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '970675', 'n_tokens_mistral': 1260, 'n_tokens_neox': 1179, 'n_words': 338}