Changeset - 68527d73e2b8
[Not reviewed]
0 5 0
Jan Kaluza - 10 years ago 2016-02-24 17:06:10
jkaluza@redhat.com
Libtransport: Allow unregistered boolean variables in Config
5 files changed with 24 insertions and 4 deletions:
0 comments (0 inline, 0 general)
libtransport/Config.cpp
Show inline comments
 
@@ -198,49 +198,57 @@ bool Config::load(std::istream &ifs, boost::program_options::options_description
 
		parsed.options.push_back(boost::program_options::basic_option<char>("service.pidfile", value));
 
	}
 
	if (!found_portfile) {
 
		std::vector<std::string> value;
 
		value.push_back("/var/run/spectrum2/$jid.port");
 
		parsed.options.push_back(boost::program_options::basic_option<char>("service.portfile", value));
 
	}
 
	if (!found_backend_port) {
 
		std::vector<std::string> value;
 
		std::string p = boost::lexical_cast<std::string>(getRandomPort(_jid.empty() ? jid : _jid));
 
		value.push_back(p);
 
		parsed.options.push_back(boost::program_options::basic_option<char>("service.backend_port", value));
 
	}
 
	if (!found_database) {
 
		std::vector<std::string> value;
 
		value.push_back("/var/lib/spectrum2/$jid/database.sql");
 
		parsed.options.push_back(boost::program_options::basic_option<char>("database.database", value));
 
	}
 

	
 
	std::list<std::string> has_key;
 
	BOOST_FOREACH(option &opt, parsed.options) {
 
		if (opt.unregistered) {
 
			if (std::find(has_key.begin(), has_key.end(), opt.string_key) == has_key.end()) {
 
				has_key.push_back(opt.string_key);
 
				m_unregistered[opt.string_key] = variable_value(opt.value[0], false);
 
				if (opt.value[0] == "true" || opt.value[0] == "1") {
 
					m_unregistered[opt.string_key] = variable_value(true, false);
 
				}
 
				else if (opt.value[0] == "true" || opt.value[0] == "1") {
 
					m_unregistered[opt.string_key] = variable_value(false, false);
 
				}
 
				else {
 
					m_unregistered[opt.string_key] = variable_value(opt.value[0], false);
 
				}
 
			}
 
			else {
 
				std::list<std::string> list;
 
				try {
 
					list = m_unregistered[opt.string_key].as<std::list<std::string> >();
 
				}
 
				catch(...) {
 
					list.push_back(m_unregistered[opt.string_key].as<std::string>());
 
				}
 
				
 
				list.push_back(opt.value[0]);
 
				m_unregistered[opt.string_key] = variable_value(list, false);
 
			}
 
		}
 
		else if (opt.value[0].find("$jid") != std::string::npos) {
 
			boost::replace_all(opt.value[0], "$jid", jid);
 
		}
 
	}
 

	
 
	// Load configs passed by command line
 
	if (m_argc != 0 && m_argv) {
 
		basic_command_line_parser<char> parser = command_line_parser(m_argc, m_argv).options(opts).allow_unregistered();
 
		parsed_options parsed = parser.run();
 
		BOOST_FOREACH(option &opt, parsed.options) {
spectrum/src/frontends/xmpp/adhocmanager.cpp
Show inline comments
 
@@ -52,48 +52,51 @@ AdHocManager::~AdHocManager() {
 
	stop();
 
}
 

	
 
void AdHocManager::start() {
 
	Swift::Responder<Swift::Command>::start();
 
}
 

	
 
void AdHocManager::stop() {
 
	Swift::Responder<Swift::Command>::stop();
 

	
 
	for (SessionsMap::iterator it = m_sessions.begin(); it != m_sessions.end(); it++) {
 
		std::vector<std::string> candidates;
 
		for (CommandsMap::iterator ct = it->second.begin(); ct != it->second.end(); ct++) {
 
			delete ct->second;
 
		}
 
	}
 

	
 
	m_sessions.clear();
 
}
 

	
 
void AdHocManager::handleUserCreated(User *user) {
 
	for (std::map<std::string, AdHocCommandFactory *>::const_iterator it = m_factories.begin(); it != m_factories.end(); it++) {
 
		for (std::map<std::string, std::string>::const_iterator it2 = it->second->getUserSettings().begin(); it2 != it->second->getUserSettings().end(); it2++) {
 
			std::string value = CONFIG_STRING_DEFAULTED(m_component->getConfig(), it->second->getNode() + "." + it2->first, it2->second);
 
			if (it2->second == "true" || it2->second == "1" || it2->second == "false" || it2->second == "0") {
 
				value = CONFIG_BOOL_DEFAULTED(m_component->getConfig(), it->second->getNode() + "." + it2->first, it2->second == "true" || it2->second == "1") ? "1" : "0";
 
			}
 
			if (m_storageBackend) {
 
				int type = (int) TYPE_BOOLEAN;
 
				m_storageBackend->getUserSetting(user->getUserInfo().id, it2->first, type, value);
 
			}
 
			user->addUserSetting(it2->first, value);
 
		}
 
	}
 
}
 

	
 
void AdHocManager::addAdHocCommand(AdHocCommandFactory *factory) {
 
	if (m_factories.find(factory->getNode()) != m_factories.end()) {
 
		LOG4CXX_ERROR(logger, "Command with node " << factory->getNode() << " is already registered. Ignoring this attempt.");
 
		return;
 
	}
 

	
 
	m_factories[factory->getNode()] = factory;
 
	m_discoItemsResponder->addAdHocCommand(factory->getNode(), factory->getName());
 
}
 

	
 
void AdHocManager::removeOldSessions() {
 
	unsigned long removedCommands = 0;
 
	time_t now = time(NULL);
 

	
 
	std::vector<std::string> toRemove;
spectrum/src/frontends/xmpp/settingsadhoccommand.cpp
Show inline comments
 
@@ -46,53 +46,53 @@ boost::shared_ptr<Swift::Command> SettingsAdHocCommand::getForm() {
 
		FormUtils::addTextFixedField(form, "This server does not support transport settings. There is no storage backend configured");
 
		response->setForm(form);
 
		return response;
 
	}
 

	
 
	UserInfo user;
 
	if (m_storageBackend->getUser(m_initiator.toBare().toString(), user) == false) {
 
		boost::shared_ptr<Swift::Command> response(new Swift::Command("settings", m_id, Swift::Command::Completed));
 
		boost::shared_ptr<Swift::Form> form(new Swift::Form());
 
		FormUtils::addTextFixedField(form, "You are not registered.");
 
		response->setForm(form);
 
		return response;
 
	}
 

	
 
	boost::shared_ptr<Swift::Command> response(new Swift::Command("settings", m_id, Swift::Command::Executing));
 
	boost::shared_ptr<Swift::Form> form(new Swift::Form());
 

	
 
	std::string value;
 
	int type = (int) TYPE_BOOLEAN;
 

	
 
	value = "1";
 
	m_storageBackend->getUserSetting(user.id, "enable_transport", type, value);
 
	FormUtils::addBooleanField(form, "enable_transport", value, "Enable transport");
 

	
 
	value = CONFIG_STRING_DEFAULTED(m_component->getConfig(), "settings.send_headlines", "0");
 
	value = CONFIG_BOOL_DEFAULTED(m_component->getConfig(), "settings.send_headlines", false) ? "1" : "0";
 
	m_storageBackend->getUserSetting(user.id, "send_headlines", type, value);
 
	FormUtils::addBooleanField(form, "send_headlines", value, "Allow sending messages as headlines");
 

	
 
	value = CONFIG_STRING_DEFAULTED(m_component->getConfig(), "settings.stay_connected", "0");
 
	value = CONFIG_BOOL_DEFAULTED(m_component->getConfig(), "settings.stay_connected", false) ? "1" : "0";
 
	m_storageBackend->getUserSetting(user.id, "stay_connected", type, value);
 
	FormUtils::addBooleanField(form, "stay_connected", value, "Stay connected to legacy network when offline on XMPP");
 

	
 
	response->setForm(form);
 
	return response;
 
}
 

	
 
void SettingsAdHocCommand::updateUserSetting(Swift::Form::ref form, UserInfo &user, const std::string &name) {
 
	std::string value = FormUtils::fieldValue(form, name, "");
 
	if (value.empty()) {
 
		return;
 
	}
 

	
 
	m_storageBackend->updateUserSetting(user.id, name, value);
 
}
 

	
 
boost::shared_ptr<Swift::Command> SettingsAdHocCommand::handleResponse(boost::shared_ptr<Swift::Command> payload) {
 
	UserInfo user;
 
	bool registered = m_storageBackend->getUser(m_initiator.toBare().toString(), user);
 

	
 
	if (registered && payload->getForm()) {
 
		updateUserSetting(payload->getForm(), user, "enable_transport");
 
		updateUserSetting(payload->getForm(), user, "send_headlines");
 
		updateUserSetting(payload->getForm(), user, "stay_connected");
tests/libtransport/config.cpp
Show inline comments
 
#include <cppunit/TestFixture.h>
 
#include <cppunit/extensions/HelperMacros.h>
 
#include <Swiften/Swiften.h>
 
#include <Swiften/EventLoop/DummyEventLoop.h>
 
#include <Swiften/Server/Server.h>
 
#include <Swiften/Network/DummyNetworkFactories.h>
 
#include <Swiften/Network/DummyConnectionServer.h>
 
#include "Swiften/Server/ServerStanzaChannel.h"
 
#include "Swiften/Server/ServerFromClientSession.h"
 
#include "Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.h"
 
#include "basictest.h"
 

	
 

	
 
using namespace Transport;
 

	
 
class ConfigTest : public CPPUNIT_NS :: TestFixture{
 
	CPPUNIT_TEST_SUITE(ConfigTest);
 
	CPPUNIT_TEST(setStringTwice);
 
	CPPUNIT_TEST(setUnknownBool);
 
	CPPUNIT_TEST(updateBackendConfig);
 
	CPPUNIT_TEST(updateBackendConfigJIDEscaping);
 
	CPPUNIT_TEST(unregisteredList);
 
	CPPUNIT_TEST(unregisteredString);
 
	CPPUNIT_TEST(unregisteredListAsString);
 
	CPPUNIT_TEST(unregisteredStringAsList);
 
	CPPUNIT_TEST_SUITE_END();
 

	
 
	public:
 
		void setUp (void) {
 
		}
 

	
 
		void tearDown (void) {
 

	
 
		}
 

	
 
	void setStringTwice() {
 
		char *argv[3] = {"binary", "--service.jids=localhost", NULL};
 
		Config cfg(2, argv);
 
		std::istringstream ifs("service.jids = irc.freenode.org\n");
 
		cfg.load(ifs);
 
		CPPUNIT_ASSERT_EQUAL(std::string("localhost"), CONFIG_STRING(&cfg, "service.jids"));
 
	}
 

	
 
	void setUnknownBool() {
 
		char *argv[3] = {"binary", "--service.jids=localhost", NULL};
 
		Config cfg(2, argv);
 
		std::istringstream ifs("service.irc_send_pass = 1\n");
 
		cfg.load(ifs);
 
		CPPUNIT_ASSERT_EQUAL(true, CONFIG_BOOL_DEFAULTED(&cfg, "service.irc_send_pass", false));
 
	}
 

	
 
	void updateBackendConfig() {
 
		Config cfg;
 
		CPPUNIT_ASSERT(!cfg.hasKey("registration.needPassword"));
 

	
 
		cfg.updateBackendConfig("[registration]\nneedPassword=0\n");
 
		CPPUNIT_ASSERT(cfg.hasKey("registration.needPassword"));
 
		CPPUNIT_ASSERT_EQUAL(false, CONFIG_BOOL(&cfg, "registration.needPassword"));
 
	}
 

	
 
	void updateBackendConfigJIDEscaping() {
 
		Config cfg;
 
		std::istringstream ifs("service.jids = irc.freenode.org\n");
 
		cfg.load(ifs);
 
		CPPUNIT_ASSERT_EQUAL(true, CONFIG_BOOL(&cfg, "service.jid_escaping"));
 

	
 
		cfg.updateBackendConfig("[features]\ndisable_jid_escaping=1\n");
 
		CPPUNIT_ASSERT_EQUAL(false, CONFIG_BOOL(&cfg, "service.jid_escaping"));
 
	}
 

	
 
	void unregisteredList() {
 
		Config cfg;
 
		std::istringstream ifs("service.irc_server = irc.freenode.org\nservice.irc_server=localhost\n");
 
		cfg.load(ifs);
 
		CPPUNIT_ASSERT_EQUAL(2, (int) CONFIG_LIST(&cfg, "service.irc_server").size());
tests/tests_output/localhost/vcard/client.dat
Show inline comments
 
return {
 
	[1] = {
 
		[1] = {
 
			[1] = "image/png";
 
			["attr"] = {
 
				["xmlns"] = "vcard-temp";
 
			};
 
			["name"] = "TYPE";
 
		};
 
		[2] = {
 
			[1] = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQAAAAAAAAAKSURBVHicY2AAAAACAAEAAAAASUVORA==";
 
			["attr"] = {
 
				["xmlns"] = "vcard-temp";
 
			};
 
			["name"] = "BINVAL";
 
		};
 
		["name"] = "PHOTO";
 
		["attr"] = {
 
			["xmlns"] = "vcard-temp";
 
		};
 
	};
 
	["attr"] = {
 
		["prodid"] = "-//HandGen//NONSGML vGen v1.0//EN";
 
		["version"] = "2.0";
 
		["prodid"] = "-//HandGen//NONSGML vGen v1.0//EN";
 
		["xmlns"] = "vcard-temp";
 
	};
 
	["name"] = "vCard";
 
};
0 comments (0 inline, 0 general)