Changeset - bf4183c181eb
[Not reviewed]
0 10 0
HanzZ - 14 years ago 2011-02-13 22:47:28
hanzz.k@gmail.com
Better Config class
10 files changed with 74 insertions and 64 deletions:
0 comments (0 inline, 0 general)
examples/server_connect/main.cpp
Show inline comments
 
@@ -4,21 +4,21 @@
 
#include "Swiften/EventLoop/SimpleEventLoop.h"
 
 
using namespace Transport;
 
 
int main(void)
 
{
 
	Config::Variables config;
 
	if (!Config::load("sample.cfg", config)) {
 
	Config config;
 
	if (!config.load("sample.cfg")) {
 
		std::cout << "Can't open sample.cfg configuration file.\n";
 
		return 1;
 
	}
 
	Swift::logging = true;
 
 
	Swift::SimpleEventLoop eventLoop;
 
	Component transport(&eventLoop, config);
 
	Component transport(&eventLoop, &config);
 
 
	Logger logger(&transport);
 
 
	transport.connect();
 
	eventLoop.run();
 
}
examples/usermanager/main.cpp
Show inline comments
 
@@ -7,31 +7,30 @@
 
#include "Swiften/EventLoop/SimpleEventLoop.h"
 
 
using namespace Transport;
 
 
int main(void)
 
{
 
	Config::Variables config;
 
	if (!Config::load("sample.cfg", config)) {
 
	Config config;
 
	if (!config.load("sample.cfg")) {
 
		std::cout << "Can't open sample.cfg configuration file.\n";
 
		return 1;
 
	}
 
 
	Swift::SimpleEventLoop eventLoop;
 
	Component transport(&eventLoop, config);
 
	Component transport(&eventLoop, &config);
 
	Logger logger(&transport);
 
 
	SQLite3Backend sql(config);
 
	SQLite3Backend sql(&config);
 
	logger.setStorageBackend(&sql);
 
	if (!sql.connect()) {
 
		std::cout << "Can't connect to database.\n";
 
	}
 
 
	transport.setStorageBackend(&sql);
 
 
	UserManager userManager(&transport);
 
 
	UserRegistration userRegistration(&transport, &userManager, &sql);
 
 
	transport.connect();
 
	eventLoop.run();
 
}
include/transport/config.h
Show inline comments
 
@@ -23,15 +23,31 @@
 
#include <boost/program_options.hpp>
 
#include <boost/foreach.hpp>
 
#include <boost/format.hpp>
 
#include <boost/algorithm/string.hpp>
 
#include <boost/assign.hpp>
 

	
 
#define CONFIG_STRING(PTR, KEY) (*PTR)[KEY].as<std::string>()
 
#define CONFIG_INT(PTR, KEY) (*PTR)[KEY].as<int>()
 
#define CONFIG_BOOL(PTR, KEY) (*PTR)[KEY].as<bool>()
 
#define CONFIG_LIST(PTR, KEY) (*PTR)[KEY].as<std::list<std::string> >()
 

	
 
namespace Transport {
 
	namespace Config {
 
		typedef boost::program_options::variables_map Variables;
 
		
 
		bool load(const std::string &configfile, Variables &variables, boost::program_options::options_description &opts);
 
		bool load(const std::string &configfile, Variables &variables);
 
		
 
	}
 

	
 
typedef boost::program_options::variables_map Variables;
 

	
 
class Config {
 
	public:
 
		Config() {}
 
		virtual ~Config() {}
 
		bool load(const std::string &configfile, boost::program_options::options_description &opts);
 
		bool load(const std::string &configfile);
 

	
 
		const boost::program_options::variable_value &operator[] (const std::string &key) {
 
			return m_variables[key];
 
		}
 
	
 
	private:
 
		Variables m_variables;
 
};
 

	
 
}
include/transport/sqlite3backend.h
Show inline comments
 
@@ -29,13 +29,13 @@
 

	
 
namespace Transport {
 

	
 
class SQLite3Backend : public StorageBackend
 
{
 
	public:
 
		SQLite3Backend(Config::Variables &config);
 
		SQLite3Backend(Config *config);
 
		~SQLite3Backend();
 

	
 
		bool connect();
 
		bool createDatabase();
 

	
 
		void setUser(const UserInfo &user);
 
@@ -46,11 +46,11 @@ class SQLite3Backend : public StorageBackend
 
		bool getBuddies(long id, std::list<std::string> &roster);
 

	
 
	private:
 
		bool exec(const std::string &query);
 

	
 
		sqlite3 *m_db;
 
		Config::Variables m_config;
 
		Config *m_config;
 
		std::string m_prefix;
 
};
 

	
 
}
include/transport/transport.h
Show inline comments
 
@@ -46,13 +46,13 @@ namespace Transport {
 
	// class SpectrumRegisterHandler;
 
	class StorageBackend;
 
	class DiscoInfoResponder;
 

	
 
	class Component {
 
		public:
 
			Component(Swift::EventLoop *loop, Config::Variables &config);
 
			Component(Swift::EventLoop *loop, Config *config);
 
			~Component();
 

	
 
			// Connect to server
 
			void connect();
 

	
 
			void setStorageBackend(StorageBackend *backend);
 
@@ -91,13 +91,13 @@ namespace Transport {
 
			Swift::CapsMemoryStorage *m_capsMemoryStorage;
 
			Swift::PresenceOracle *m_presenceOracle;
 
			StorageBackend *m_storageBackend;
 
 			DiscoInfoResponder *m_discoInfoResponder;
 
// 			SpectrumRegisterHandler *m_registerHandler;
 
			int m_reconnectCount;
 
			Config::Variables m_config;
 
			Config* m_config;
 
			std::string m_protocol;
 
			Swift::JID m_jid;
 

	
 
		friend class User;
 
		friend class UserRegistration;
 
	};
include/transport/userregistration.h
Show inline comments
 
@@ -28,12 +28,13 @@
 
namespace Transport {
 

	
 
struct UserInfo;
 
class Component;
 
class StorageBackend;
 
class UserManager;
 
class Config;
 

	
 
class UserRegistration : Swift::GetResponder<Swift::InBandRegistrationPayload>, Swift::SetResponder<Swift::InBandRegistrationPayload> {
 
	public:
 
		UserRegistration(Component *component, UserManager *userManager, StorageBackend *storageBackend);
 
		~UserRegistration();
 

	
 
@@ -51,10 +52,11 @@ class UserRegistration : Swift::GetResponder<Swift::InBandRegistrationPayload>,
 
		bool handleGetRequest(const Swift::JID& from, const Swift::JID& to, const Swift::String& id, boost::shared_ptr<Swift::InBandRegistrationPayload> payload);
 
		bool handleSetRequest(const Swift::JID& from, const Swift::JID& to, const Swift::String& id, boost::shared_ptr<Swift::InBandRegistrationPayload> payload);
 
		
 
		Component *m_component;
 
		StorageBackend *m_storageBackend;
 
		UserManager *m_userManager;
 
		Config *m_config;
 

	
 
};
 

	
 
}
src/config.cpp
Show inline comments
 
@@ -21,36 +21,33 @@
 
#include "transport/config.h"
 
#include <fstream>
 

	
 
using namespace boost::program_options;
 

	
 
namespace Transport {
 
namespace Config {
 

	
 
bool load(const std::string &configfile, Variables &variables, boost::program_options::options_description &opts) {
 
bool Config::load(const std::string &configfile, boost::program_options::options_description &opts) {
 
	std::ifstream ifs(configfile.c_str());
 
	if (!ifs.is_open())
 
		return false;
 

	
 
	opts.add_options()
 
		("service.jid", value<std::string>(), "set compression level")
 
		("service.server", value<std::string>(), "set compression level")
 
		("service.password", value<std::string>(), "set compression level")
 
		("service.port", value<int>(), "set compression level")
 
		("database.database", value<std::string>(), "set compression level")
 
		("database.prefix", value<std::string>(), "set compression level")
 
		("service.jid", value<std::string>()->default_value(""), "Transport Jabber ID")
 
		("service.server", value<std::string>()->default_value(""), "Server to connect to")
 
		("service.password", value<std::string>()->default_value(""), "Password used to auth the server")
 
		("service.port", value<int>()->default_value(0), "Port the server is listening on")
 
		("database.database", value<std::string>()->default_value(""), "Database used to store data")
 
		("database.prefix", value<std::string>()->default_value(""), "Prefix of tables in database")
 
	;
 

	
 

	
 
    store(parse_config_file(ifs, opts), variables);
 
	notify(variables);
 
    store(parse_config_file(ifs, opts), m_variables);
 
	notify(m_variables);
 

	
 
	return true;
 
}
 

	
 
bool load(const std::string &configfile, Variables &variables) {
 
bool Config::load(const std::string &configfile) {
 
	options_description opts("Transport options");
 
	return load(configfile, variables, opts);
 
	return load(configfile, opts);
 
}
 

	
 
}
 
}
src/sqlite3backend.cpp
Show inline comments
 
@@ -24,26 +24,26 @@
 
#define SQLITE_DB_VERSION 3
 

	
 
using namespace boost;
 

	
 
namespace Transport {
 

	
 
SQLite3Backend::SQLite3Backend(Config::Variables &config) {
 
SQLite3Backend::SQLite3Backend(Config *config) {
 
	m_config = config;
 
	m_db = NULL;
 
	m_prefix = m_config["database.prefix"].as<std::string>();
 
	m_prefix = CONFIG_STRING(m_config, "database.prefix");
 
}
 

	
 
SQLite3Backend::~SQLite3Backend(){
 
	if (m_db) {
 
		sqlite3_close(m_db);
 
	}
 
}
 

	
 
bool SQLite3Backend::connect() {
 
	if (sqlite3_open(m_config["database.database"].as<std::string>().c_str(), &m_db)) {
 
	if (sqlite3_open(CONFIG_STRING(m_config, "database.database").c_str(), &m_db)) {
 
		 sqlite3_close(m_db);
 
		 return false;
 
	}
 
	return createDatabase();
 
}
 

	
src/transport.cpp
Show inline comments
 
@@ -25,30 +25,25 @@
 

	
 
using namespace Swift;
 
using namespace boost;
 

	
 
namespace Transport {
 

	
 
Component::Component(Swift::EventLoop *loop, Config::Variables &config) {
 
Component::Component(Swift::EventLoop *loop, Config *config) {
 
	m_reconnectCount = 0;
 
	m_config = config;
 
	m_storageBackend = NULL;
 

	
 
	for (Config::Variables::iterator i = config.begin() ; i != config.end() ; ++i )
 
	{
 
		std::cout << (*i).first << "\n";
 
	} 
 

	
 
	m_jid = Swift::JID(m_config["service.jid"].as<std::string>());
 
	m_jid = Swift::JID(CONFIG_STRING(m_config, "service.jid"));
 

	
 
	m_factories = new BoostNetworkFactories(loop);
 

	
 
	m_reconnectTimer = m_factories->getTimerFactory()->createTimer(1000);
 
	m_reconnectTimer->onTick.connect(bind(&Component::connect, this)); 
 

	
 
	m_component = new Swift::Component(loop, m_factories, m_jid, m_config["service.password"].as<std::string>());
 
	m_component = new Swift::Component(loop, m_factories, m_jid, CONFIG_STRING(m_config, "service.password"));
 
	m_component->setSoftwareVersion("", "");
 
	m_component->onConnected.connect(bind(&Component::handleConnected, this));
 
	m_component->onError.connect(bind(&Component::handleConnectionError, this, _1));
 
	m_component->onDataRead.connect(bind(&Component::handleDataRead, this, _1));
 
	m_component->onDataWritten.connect(bind(&Component::handleDataWritten, this, _1));
 
	m_component->onPresenceReceived.connect(bind(&Component::handlePresenceReceived, this, _1));
 
@@ -92,13 +87,13 @@ void Component::setBuddyFeatures(std::list<std::string> &features) {
 
	// TODO: handle caps change
 
	m_discoInfoResponder->setBuddyFeatures(features);
 
}
 

	
 
void Component::connect() {
 
	m_reconnectCount++;
 
	m_component->connect(m_config["service.server"].as<std::string>(), m_config["service.port"].as<int>());
 
	m_component->connect(CONFIG_STRING(m_config, "service.server"), CONFIG_INT(m_config, "service.port"));
 
	m_reconnectTimer->stop();
 
}
 

	
 
void Component::handleConnected() {
 
	onConnected();
 
	m_reconnectCount = 0;
src/userregistration.cpp
Show inline comments
 
@@ -29,12 +29,13 @@
 
using namespace Swift;
 

	
 
namespace Transport {
 

	
 
UserRegistration::UserRegistration(Component *component, UserManager *userManager, StorageBackend *storageBackend) : Swift::GetResponder<Swift::InBandRegistrationPayload>(component->m_component->getIQRouter()), Swift::SetResponder<Swift::InBandRegistrationPayload>(component->m_component->getIQRouter()) {
 
	m_component = component;
 
	m_config = m_component->m_config;
 
	m_storageBackend = storageBackend;
 
	m_userManager = userManager;
 
	Swift::GetResponder<Swift::InBandRegistrationPayload>::start();
 
	Swift::SetResponder<Swift::InBandRegistrationPayload>::start();
 
}
 

	
 
@@ -116,45 +117,45 @@ bool UserRegistration::unregisterUser(const std::string &barejid) {
 
	m_component->m_component->sendPresence(response);
 

	
 
	return true;
 
}
 

	
 
bool UserRegistration::handleGetRequest(const Swift::JID& from, const Swift::JID& to, const Swift::String& id, boost::shared_ptr<Swift::InBandRegistrationPayload> payload) {
 
	if (m_component->m_config["service.protocol"].as<std::string>() == "irc") {
 
	if (CONFIG_STRING(m_config, "service.protocol") == "irc") {
 
		Swift::GetResponder<Swift::InBandRegistrationPayload>::sendError(from, id, ErrorPayload::BadRequest, ErrorPayload::Modify);
 
		return true;
 
	}
 

	
 
	std::string barejid = from.toBare().toString().getUTF8String();
 

	
 
// 	User *user = m_userManager->getUserByJID(barejid);
 
	if (!m_component->m_config["registration.enable_public_registration"].as<bool>()) {
 
		std::list<std::string> const &x = m_component->m_config["registration.enable_public_registration"].as<std::list<std::string> >();
 
	if (!CONFIG_BOOL(m_config,"registration.enable_public_registration")) {
 
		std::list<std::string> const &x = CONFIG_LIST(m_config,"registration.enable_public_registration");
 
		if (std::find(x.begin(), x.end(), from.getDomain().getUTF8String()) == x.end()) {
 
// 			Log("UserRegistration", "This user has no permissions to register an account");
 
			Swift::GetResponder<Swift::InBandRegistrationPayload>::sendError(from, id, ErrorPayload::BadRequest, ErrorPayload::Modify);
 
			return true;
 
		}
 
	}
 

	
 
// 	const char *_language = user ? user->getLang() : m_component->m_config["registration.language"].as<std::string>().c_str();
 
// 	const char *_language = user ? user->getLang() : CONFIG_STRING(m_config, "registration.language").c_str();
 

	
 
	boost::shared_ptr<InBandRegistrationPayload> reg(new InBandRegistrationPayload());
 

	
 
	UserInfo res;
 
	bool registered = m_storageBackend->getUser(barejid, res);
 

	
 
	std::string instructions = m_component->m_config["registration.reg_instructions"].as<std::string>();
 
	std::string instructions = CONFIG_STRING(m_config, "registration.reg_instructions");
 

	
 
	reg->setInstructions(instructions);
 
	reg->setRegistered(res.id != -1);
 
	reg->setUsername(res.uin);
 
	if (m_component->m_config["service.protocol"].as<std::string>() != "twitter" && m_component->m_config["service.protocol"].as<std::string>() != "bonjour")
 
	if (CONFIG_STRING(m_config, "service.protocol") != "twitter" && CONFIG_STRING(m_config, "service.protocol") != "bonjour")
 
		reg->setPassword(res.password);
 

	
 
	std::string usernameField = m_component->m_config["registration.reg_username_field"].as<std::string>();
 
	std::string usernameField = CONFIG_STRING(m_config, "registration.reg_username_field");
 

	
 
	Form::ref form(new Form(Form::FormType));
 
	form->setTitle(tr(_language, _("Registration")));
 
	form->setInstructions(tr(_language, instructions));
 

	
 
	HiddenFormField::ref type = HiddenFormField::create();
 
@@ -166,13 +167,13 @@ bool UserRegistration::handleGetRequest(const Swift::JID& from, const Swift::JID
 
	username->setName("username");
 
	username->setLabel(tr(_language, usernameField));
 
	username->setValue(res.uin);
 
	username->setRequired(true);
 
	form->addField(username);
 

	
 
	if (m_component->m_config["service.protocol"].as<std::string>() != "twitter" && m_component->m_config["service.protocol"].as<std::string>() != "bonjour") {
 
	if (CONFIG_STRING(m_config, "service.protocol") != "twitter" && CONFIG_STRING(m_config, "service.protocol") != "bonjour") {
 
		TextPrivateFormField::ref password = TextPrivateFormField::create();
 
		password->setName("password");
 
		password->setLabel(tr(_language, _("Password")));
 
		password->setRequired(true);
 
		form->addField(password);
 
	}
 
@@ -180,26 +181,26 @@ bool UserRegistration::handleGetRequest(const Swift::JID& from, const Swift::JID
 
	ListSingleFormField::ref language = ListSingleFormField::create();
 
	language->setName("language");
 
	language->setLabel(tr(_language, _("Language")));
 
	if (registered)
 
		language->setValue(res.language);
 
	else
 
		language->setValue(m_component->m_config["registration.language"].as<std::string>());
 
		language->setValue(CONFIG_STRING(m_config, "registration.language"));
 
// 	std::map <std::string, std::string> languages = localization.getLanguages();
 
// 	for (std::map <std::string, std::string>::iterator it = languages.begin(); it != languages.end(); it++) {
 
// 		language->addOption(FormField::Option((*it).second, (*it).first));
 
// 	}
 
	form->addField(language);
 

	
 
	TextSingleFormField::ref encoding = TextSingleFormField::create();
 
	encoding->setName("encoding");
 
	encoding->setLabel(tr(_language, _("Encoding")));
 
	if (registered)
 
		encoding->setValue(res.encoding);
 
	else
 
		encoding->setValue(m_component->m_config["registration.encoding"].as<std::string>());
 
		encoding->setValue(CONFIG_STRING(m_config, "registration.encoding"));
 
	form->addField(encoding);
 

	
 
	if (registered) {
 
		BooleanFormField::ref boolean = BooleanFormField::create();
 
		boolean->setName("unregister");
 
		boolean->setLabel(tr(_language, _("Remove your registration")));
 
@@ -212,22 +213,22 @@ bool UserRegistration::handleGetRequest(const Swift::JID& from, const Swift::JID
 
	Swift::GetResponder<Swift::InBandRegistrationPayload>::sendResponse(from, id, reg);
 

	
 
	return true;
 
}
 

	
 
bool UserRegistration::handleSetRequest(const Swift::JID& from, const Swift::JID& to, const Swift::String& id, boost::shared_ptr<Swift::InBandRegistrationPayload> payload) {
 
	if (m_component->m_config["service.protocol"].as<std::string>() == "irc") {
 
	if (CONFIG_STRING(m_config, "service.protocol") == "irc") {
 
		Swift::GetResponder<Swift::InBandRegistrationPayload>::sendError(from, id, ErrorPayload::BadRequest, ErrorPayload::Modify);
 
		return true;
 
	}
 

	
 
	std::string barejid = from.toBare().toString().getUTF8String();
 

	
 
// 	AbstractUser *user = m_component->userManager()->getUserByJID(barejid);
 
	if (!m_component->m_config["registration.enable_public_registration"].as<bool>()) {
 
		std::list<std::string> const &x = m_component->m_config["registration.enable_public_registration"].as<std::list<std::string> >();
 
	if (!CONFIG_BOOL(m_config,"registration.enable_public_registration")) {
 
		std::list<std::string> const &x = CONFIG_LIST(m_config,"registration.enable_public_registration");
 
		if (std::find(x.begin(), x.end(), from.getDomain().getUTF8String()) == x.end()) {
 
// 			Log("UserRegistration", "This user has no permissions to register an account");
 
			Swift::SetResponder<Swift::InBandRegistrationPayload>::sendError(from, id, ErrorPayload::BadRequest, ErrorPayload::Modify);
 
			return true;
 
		}
 
	}
 
@@ -291,21 +292,21 @@ bool UserRegistration::handleSetRequest(const Swift::JID& from, const Swift::JID
 
		Swift::SetResponder<Swift::InBandRegistrationPayload>::sendError(from, id, ErrorPayload::NotAcceptable, ErrorPayload::Modify);
 
		return true;
 
	}
 

	
 
	// Register or change password
 
	if (payload->getUsername()->isEmpty() ||
 
		(payload->getPassword()->isEmpty() && m_component->m_config["service.protocol"].as<std::string>() != "twitter" && m_component->m_config["service.protocol"].as<std::string>() != "bonjour")
 
		(payload->getPassword()->isEmpty() && CONFIG_STRING(m_config, "service.protocol") != "twitter" && CONFIG_STRING(m_config, "service.protocol") != "bonjour")
 
// 		|| localization.getLanguages().find(language) == localization.getLanguages().end()
 
	)
 
	{
 
		Swift::SetResponder<Swift::InBandRegistrationPayload>::sendError(from, id, ErrorPayload::NotAcceptable, ErrorPayload::Modify);
 
		return true;
 
	}
 

	
 
	if (m_component->m_config["service.protocol"].as<std::string>() == "xmpp") {
 
	if (CONFIG_STRING(m_config, "service.protocol") == "xmpp") {
 
		// User tries to register himself.
 
		if ((Swift::JID(*payload->getUsername()).toBare() == from.toBare())) {
 
			Swift::SetResponder<Swift::InBandRegistrationPayload>::sendError(from, id, ErrorPayload::NotAcceptable, ErrorPayload::Modify);
 
			return true;
 
		}
 

	
 
@@ -319,26 +320,26 @@ bool UserRegistration::handleSetRequest(const Swift::JID& from, const Swift::JID
 
	}
 

	
 
	std::string username = payload->getUsername()->getUTF8String();
 
// 	m_component->protocol()->prepareUsername(username);
 

	
 
	std::string newUsername(username);
 
	if (!m_component->m_config["registration.username_mask"].as<std::string>().empty()) {
 
		newUsername = m_component->m_config["registration.username_mask"].as<std::string>();
 
	if (!CONFIG_STRING(m_config, "registration.username_mask").empty()) {
 
		newUsername = CONFIG_STRING(m_config, "registration.username_mask");
 
// 		replace(newUsername, "$username", username.c_str());
 
	}
 

	
 
// 	if (!m_component->protocol()->isValidUsername(newUsername)) {
 
// 		Log("UserRegistration", "This is not valid username: "<< newUsername);
 
// 		Swift::SetResponder<Swift::InBandRegistrationPayload>::sendError(from, id, ErrorPayload::NotAcceptable, ErrorPayload::Modify);
 
// 		return true;
 
// 	}
 

	
 
// #if GLIB_CHECK_VERSION(2,14,0)
 
// 	if (!m_component->m_config["registration.reg_allowed_usernames"].as<std::string>().empty() &&
 
// 		!g_regex_match_simple(m_component->m_config["registration.reg_allowed_usernames"].as<std::string>(), newUsername.c_str(),(GRegexCompileFlags) (G_REGEX_CASELESS | G_REGEX_EXTENDED), (GRegexMatchFlags) 0)) {
 
// 	if (!CONFIG_STRING(m_config, "registration.reg_allowed_usernames").empty() &&
 
// 		!g_regex_match_simple(CONFIG_STRING(m_config, "registration.reg_allowed_usernames"), newUsername.c_str(),(GRegexCompileFlags) (G_REGEX_CASELESS | G_REGEX_EXTENDED), (GRegexMatchFlags) 0)) {
 
// 		Log("UserRegistration", "This is not valid username: "<< newUsername);
 
// 		Swift::SetResponder<Swift::InBandRegistrationPayload>::sendError(from, id, ErrorPayload::NotAcceptable, ErrorPayload::Modify);
 
// 		return true;
 
// 	}
 
// #endif
 
	if (!registered) {
0 comments (0 inline, 0 general)