changeset 198:b87679fbf7d8

Irccdctl: add brand new plugin-config command, #514
author David Demelier <markand@malikania.fr>
date Wed, 08 Jun 2016 13:41:38 +0200
parents 1b3365343b18
children 15ad726db5c8
files lib/irccd/CMakeSources.cmake lib/irccd/cmd-plugin-config.cpp lib/irccd/cmd-plugin-config.hpp lib/irccd/irccdctl.cpp lib/irccd/service-command.cpp
diffstat 5 files changed, 198 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/lib/irccd/CMakeSources.cmake	Wed Jun 08 12:37:55 2016 +0200
+++ b/lib/irccd/CMakeSources.cmake	Wed Jun 08 13:41:38 2016 +0200
@@ -3,6 +3,7 @@
 	${CMAKE_CURRENT_LIST_DIR}/alias.hpp
 	${CMAKE_CURRENT_LIST_DIR}/connection.hpp
 	${CMAKE_CURRENT_LIST_DIR}/cmd-help.hpp
+	${CMAKE_CURRENT_LIST_DIR}/cmd-plugin-config.hpp
 	${CMAKE_CURRENT_LIST_DIR}/cmd-plugin-info.hpp
 	${CMAKE_CURRENT_LIST_DIR}/cmd-plugin-list.hpp
 	${CMAKE_CURRENT_LIST_DIR}/cmd-plugin-load.hpp
@@ -83,6 +84,7 @@
 	${CMAKE_CURRENT_LIST_DIR}/connection.cpp
 	${CMAKE_CURRENT_LIST_DIR}/config.cpp
 	${CMAKE_CURRENT_LIST_DIR}/cmd-help.cpp
+	${CMAKE_CURRENT_LIST_DIR}/cmd-plugin-config.cpp
 	${CMAKE_CURRENT_LIST_DIR}/cmd-plugin-info.cpp
 	${CMAKE_CURRENT_LIST_DIR}/cmd-plugin-list.cpp
 	${CMAKE_CURRENT_LIST_DIR}/cmd-plugin-load.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/irccd/cmd-plugin-config.cpp	Wed Jun 08 13:41:38 2016 +0200
@@ -0,0 +1,120 @@
+/*
+ * cmd-plugin-config.cpp -- implementation of plugin-config command
+ *
+ * Copyright (c) 2013-2016 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <iomanip>
+#include <iostream>
+
+#include "irccd.hpp"
+#include "cmd-plugin-config.hpp"
+#include "service-plugin.hpp"
+
+namespace irccd {
+
+namespace command {
+
+namespace {
+
+json::Value execSet(Irccd &irccd, const json::Value &request)
+{
+	auto plugin = irccd.pluginService().require(request.at("plugin").toString());
+	auto config = plugin->config();
+
+	config[request.at("variable").toString()] = request.at("value").toString(true);
+	plugin->setConfig(config);
+
+	return nullptr;
+}
+
+json::Value execGet(Irccd &irccd, const json::Value &request)
+{
+	auto config = irccd.pluginService().require(request.at("plugin").toString())->config();
+	auto var = request.find("variable");
+
+	// 'vars' property.
+	std::map<std::string, json::Value> vars;
+
+	if (var != request.end())
+		vars.emplace(var->toString(), config[var->toString()]);
+	else
+		for (const auto &pair : config)
+			vars.emplace(pair.first, pair.second);
+
+	return json::object({{ "vars", json::Value(vars) }});
+}
+
+} // !namespace
+
+PluginConfig::PluginConfig()
+	: RemoteCommand("plugin-config", "Plugins")
+{
+}
+
+std::string PluginConfig::help() const
+{
+	return "Get or set a plugin configuration option.";
+}
+
+std::vector<RemoteCommand::Arg> PluginConfig::args() const
+{
+	return {
+		{ "plugin", true },
+		{ "variable", false },
+		{ "value", false }
+	};
+}
+
+json::Value PluginConfig::request(Irccdctl &, const RemoteCommandRequest &args) const
+{
+	auto object = json::object({
+		{ "plugin", args.arg(0) }
+	});
+
+	if (args.length() >= 2U) {
+		object.insert("variable", args.arg(1));
+
+		if (args.length() == 3U)
+			object.insert("value", args.arg(2));
+	}
+
+	return object;
+}
+
+json::Value PluginConfig::exec(Irccd &irccd, const json::Value &request) const
+{
+	return request.contains("value") ? execSet(irccd, request) : execGet(irccd, request);
+}
+
+void PluginConfig::result(Irccdctl &irccdctl, const json::Value &response) const
+{
+	RemoteCommand::result(irccdctl, response);
+
+	auto it = response.find("vars");
+
+	if (it == response.end() || !it->isObject())
+		return;
+
+	if (it->size() > 1U)
+		for (auto v = it->begin(); v != it->end(); ++v)
+			std::cout << std::setw(16) << std::left << v.key() << " : " << v->toString(true) << std::endl;
+	else
+		std::cout << it->begin()->toString(true) << std::endl;
+}
+
+} // !command
+
+} // !irccd
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/irccd/cmd-plugin-config.hpp	Wed Jun 08 13:41:38 2016 +0200
@@ -0,0 +1,73 @@
+/*
+ * cmd-plugin-config.hpp -- implementation of plugin-config command
+ *
+ * Copyright (c) 2013-2016 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef IRCCD_CMD_PLUGIN_CONFIG_HPP
+#define IRCCD_CMD_PLUGIN_CONFIG_HPP
+
+/**
+ * \file cmd-plugin-config.hpp
+ * \brief Implementation of plugin-config transport command.
+ */
+
+#include "command.hpp"
+
+namespace irccd {
+
+namespace command {
+
+/**
+ * \brief Implementation of plugin-config transport command.
+ */
+class PluginConfig : public RemoteCommand {
+public:
+	/**
+	 * Constructor.
+	 */
+	IRCCD_EXPORT PluginConfig();
+
+	/**
+	 * \copydoc RemoteCommand::help
+	 */
+	IRCCD_EXPORT std::string help() const override;
+
+	/**
+	 * \copydoc RemoteCommand::args
+	 */
+	IRCCD_EXPORT std::vector<Arg> args() const override;
+
+	/**
+	 * \copydoc RemoteCommand::request
+	 */
+	IRCCD_EXPORT json::Value request(Irccdctl &irccdctl, const RemoteCommandRequest &args) const override;
+
+	/**
+	 * \copydoc RemoteCommand::exec
+	 */
+	IRCCD_EXPORT json::Value exec(Irccd &irccd, const json::Value &request) const override;
+
+	/**
+	 * \copydoc RemoteCommand::result
+	 */
+	IRCCD_EXPORT void result(Irccdctl &irccdctl, const json::Value &response) const override;
+};
+
+} // !command
+
+} // !irccd
+
+#endif // !IRCCD_CMD_PLUGIN_CONFIG_HPP
--- a/lib/irccd/irccdctl.cpp	Wed Jun 08 12:37:55 2016 +0200
+++ b/lib/irccd/irccdctl.cpp	Wed Jun 08 13:41:38 2016 +0200
@@ -94,6 +94,7 @@
 	log::warning() << "\thelp\t\t\tShow an help topic\n";
 	log::warning() << "\twatch\t\t\tStart listening to irccd\n\n";
 	log::warning() << "Plugin management:\n";
+	log::warning() << "\tplugin-config\t\tGet or set a plugin configuration option\n";
 	log::warning() << "\tplugin-info\t\tGet plugin information\n";
 	log::warning() << "\tplugin-list\t\tList all loaded plugins\n";
 	log::warning() << "\tplugin-load\t\tLoad a plugin\n";
--- a/lib/irccd/service-command.cpp	Wed Jun 08 12:37:55 2016 +0200
+++ b/lib/irccd/service-command.cpp	Wed Jun 08 13:41:38 2016 +0200
@@ -19,6 +19,7 @@
 #include <algorithm>
 
 #include "cmd-help.hpp"
+#include "cmd-plugin-config.hpp"
 #include "cmd-plugin-info.hpp"
 #include "cmd-plugin-list.hpp"
 #include "cmd-plugin-load.hpp"
@@ -49,6 +50,7 @@
 CommandService::CommandService()
 	: m_commands{
 		std::make_shared<command::Help>(),
+		std::make_shared<command::PluginConfig>(),
 		std::make_shared<command::PluginInfo>(),
 		std::make_shared<command::PluginList>(),
 		std::make_shared<command::PluginLoad>(),