From bf4183c181ebd3e1851f98f4ad0f773a652482a8 Mon Sep 17 00:00:00 2001 From: HanzZ Date: Sun, 13 Feb 2011 22:47:28 +0100 Subject: [PATCH] Better Config class --- examples/server_connect/main.cpp | 6 ++--- examples/usermanager/main.cpp | 9 +++---- include/transport/config.h | 30 ++++++++++++++++----- include/transport/sqlite3backend.h | 4 +-- include/transport/transport.h | 4 +-- include/transport/userregistration.h | 2 ++ src/config.cpp | 25 ++++++++---------- src/sqlite3backend.cpp | 6 ++--- src/transport.cpp | 13 +++------- src/userregistration.cpp | 39 ++++++++++++++-------------- 10 files changed, 74 insertions(+), 64 deletions(-) diff --git a/examples/server_connect/main.cpp b/examples/server_connect/main.cpp index 3f6febc5..dc6f4690 100644 --- a/examples/server_connect/main.cpp +++ b/examples/server_connect/main.cpp @@ -7,15 +7,15 @@ 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); diff --git a/examples/usermanager/main.cpp b/examples/usermanager/main.cpp index abd00d23..f41b7eab 100644 --- a/examples/usermanager/main.cpp +++ b/examples/usermanager/main.cpp @@ -10,17 +10,17 @@ 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"; @@ -29,7 +29,6 @@ int main(void) transport.setStorageBackend(&sql); UserManager userManager(&transport); - UserRegistration userRegistration(&transport, &userManager, &sql); transport.connect(); diff --git a/include/transport/config.h b/include/transport/config.h index 5d61e86b..2e0e77fe 100644 --- a/include/transport/config.h +++ b/include/transport/config.h @@ -26,12 +26,28 @@ #include #include +#define CONFIG_STRING(PTR, KEY) (*PTR)[KEY].as() +#define CONFIG_INT(PTR, KEY) (*PTR)[KEY].as() +#define CONFIG_BOOL(PTR, KEY) (*PTR)[KEY].as() +#define CONFIG_LIST(PTR, KEY) (*PTR)[KEY].as >() + 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; +}; + } diff --git a/include/transport/sqlite3backend.h b/include/transport/sqlite3backend.h index dbb53db2..f1acb919 100644 --- a/include/transport/sqlite3backend.h +++ b/include/transport/sqlite3backend.h @@ -32,7 +32,7 @@ namespace Transport { class SQLite3Backend : public StorageBackend { public: - SQLite3Backend(Config::Variables &config); + SQLite3Backend(Config *config); ~SQLite3Backend(); bool connect(); @@ -49,7 +49,7 @@ class SQLite3Backend : public StorageBackend bool exec(const std::string &query); sqlite3 *m_db; - Config::Variables m_config; + Config *m_config; std::string m_prefix; }; diff --git a/include/transport/transport.h b/include/transport/transport.h index c885e461..82ca4816 100644 --- a/include/transport/transport.h +++ b/include/transport/transport.h @@ -49,7 +49,7 @@ namespace Transport { class Component { public: - Component(Swift::EventLoop *loop, Config::Variables &config); + Component(Swift::EventLoop *loop, Config *config); ~Component(); // Connect to server @@ -94,7 +94,7 @@ namespace Transport { DiscoInfoResponder *m_discoInfoResponder; // SpectrumRegisterHandler *m_registerHandler; int m_reconnectCount; - Config::Variables m_config; + Config* m_config; std::string m_protocol; Swift::JID m_jid; diff --git a/include/transport/userregistration.h b/include/transport/userregistration.h index 368cecc7..21ac00d0 100644 --- a/include/transport/userregistration.h +++ b/include/transport/userregistration.h @@ -31,6 +31,7 @@ struct UserInfo; class Component; class StorageBackend; class UserManager; +class Config; class UserRegistration : Swift::GetResponder, Swift::SetResponder { public: @@ -54,6 +55,7 @@ class UserRegistration : Swift::GetResponder, Component *m_component; StorageBackend *m_storageBackend; UserManager *m_userManager; + Config *m_config; }; diff --git a/src/config.cpp b/src/config.cpp index aba38ebc..2a4b27ce 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -24,33 +24,30 @@ 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(), "set compression level") - ("service.server", value(), "set compression level") - ("service.password", value(), "set compression level") - ("service.port", value(), "set compression level") - ("database.database", value(), "set compression level") - ("database.prefix", value(), "set compression level") + ("service.jid", value()->default_value(""), "Transport Jabber ID") + ("service.server", value()->default_value(""), "Server to connect to") + ("service.password", value()->default_value(""), "Password used to auth the server") + ("service.port", value()->default_value(0), "Port the server is listening on") + ("database.database", value()->default_value(""), "Database used to store data") + ("database.prefix", value()->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); } } -} diff --git a/src/sqlite3backend.cpp b/src/sqlite3backend.cpp index 73da62ee..8682f21e 100644 --- a/src/sqlite3backend.cpp +++ b/src/sqlite3backend.cpp @@ -27,10 +27,10 @@ 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(); + m_prefix = CONFIG_STRING(m_config, "database.prefix"); } SQLite3Backend::~SQLite3Backend(){ @@ -40,7 +40,7 @@ SQLite3Backend::~SQLite3Backend(){ } bool SQLite3Backend::connect() { - if (sqlite3_open(m_config["database.database"].as().c_str(), &m_db)) { + if (sqlite3_open(CONFIG_STRING(m_config, "database.database").c_str(), &m_db)) { sqlite3_close(m_db); return false; } diff --git a/src/transport.cpp b/src/transport.cpp index 3c15d24e..7123482d 100644 --- a/src/transport.cpp +++ b/src/transport.cpp @@ -28,24 +28,19 @@ 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()); + 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()); + 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)); @@ -95,7 +90,7 @@ void Component::setBuddyFeatures(std::list &features) { void Component::connect() { m_reconnectCount++; - m_component->connect(m_config["service.server"].as(), m_config["service.port"].as()); + m_component->connect(CONFIG_STRING(m_config, "service.server"), CONFIG_INT(m_config, "service.port")); m_reconnectTimer->stop(); } diff --git a/src/userregistration.cpp b/src/userregistration.cpp index cd84c35c..c48ce460 100644 --- a/src/userregistration.cpp +++ b/src/userregistration.cpp @@ -32,6 +32,7 @@ namespace Transport { UserRegistration::UserRegistration(Component *component, UserManager *userManager, StorageBackend *storageBackend) : Swift::GetResponder(component->m_component->getIQRouter()), Swift::SetResponder(component->m_component->getIQRouter()) { m_component = component; + m_config = m_component->m_config; m_storageBackend = storageBackend; m_userManager = userManager; Swift::GetResponder::start(); @@ -119,7 +120,7 @@ bool UserRegistration::unregisterUser(const std::string &barejid) { } bool UserRegistration::handleGetRequest(const Swift::JID& from, const Swift::JID& to, const Swift::String& id, boost::shared_ptr payload) { - if (m_component->m_config["service.protocol"].as() == "irc") { + if (CONFIG_STRING(m_config, "service.protocol") == "irc") { Swift::GetResponder::sendError(from, id, ErrorPayload::BadRequest, ErrorPayload::Modify); return true; } @@ -127,8 +128,8 @@ bool UserRegistration::handleGetRequest(const Swift::JID& from, const Swift::JID std::string barejid = from.toBare().toString().getUTF8String(); // User *user = m_userManager->getUserByJID(barejid); - if (!m_component->m_config["registration.enable_public_registration"].as()) { - std::list const &x = m_component->m_config["registration.enable_public_registration"].as >(); + if (!CONFIG_BOOL(m_config,"registration.enable_public_registration")) { + std::list 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::sendError(from, id, ErrorPayload::BadRequest, ErrorPayload::Modify); @@ -136,22 +137,22 @@ bool UserRegistration::handleGetRequest(const Swift::JID& from, const Swift::JID } } -// const char *_language = user ? user->getLang() : m_component->m_config["registration.language"].as().c_str(); +// const char *_language = user ? user->getLang() : CONFIG_STRING(m_config, "registration.language").c_str(); boost::shared_ptr 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 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() != "twitter" && m_component->m_config["service.protocol"].as() != "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 usernameField = CONFIG_STRING(m_config, "registration.reg_username_field"); Form::ref form(new Form(Form::FormType)); form->setTitle(tr(_language, _("Registration"))); @@ -169,7 +170,7 @@ bool UserRegistration::handleGetRequest(const Swift::JID& from, const Swift::JID username->setRequired(true); form->addField(username); - if (m_component->m_config["service.protocol"].as() != "twitter" && m_component->m_config["service.protocol"].as() != "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"))); @@ -183,7 +184,7 @@ bool UserRegistration::handleGetRequest(const Swift::JID& from, const Swift::JID if (registered) language->setValue(res.language); else - language->setValue(m_component->m_config["registration.language"].as()); + language->setValue(CONFIG_STRING(m_config, "registration.language")); // std::map languages = localization.getLanguages(); // for (std::map ::iterator it = languages.begin(); it != languages.end(); it++) { // language->addOption(FormField::Option((*it).second, (*it).first)); @@ -196,7 +197,7 @@ bool UserRegistration::handleGetRequest(const Swift::JID& from, const Swift::JID if (registered) encoding->setValue(res.encoding); else - encoding->setValue(m_component->m_config["registration.encoding"].as()); + encoding->setValue(CONFIG_STRING(m_config, "registration.encoding")); form->addField(encoding); if (registered) { @@ -215,7 +216,7 @@ bool UserRegistration::handleGetRequest(const Swift::JID& from, const Swift::JID } bool UserRegistration::handleSetRequest(const Swift::JID& from, const Swift::JID& to, const Swift::String& id, boost::shared_ptr payload) { - if (m_component->m_config["service.protocol"].as() == "irc") { + if (CONFIG_STRING(m_config, "service.protocol") == "irc") { Swift::GetResponder::sendError(from, id, ErrorPayload::BadRequest, ErrorPayload::Modify); return true; } @@ -223,8 +224,8 @@ bool UserRegistration::handleSetRequest(const Swift::JID& from, const Swift::JID std::string barejid = from.toBare().toString().getUTF8String(); // AbstractUser *user = m_component->userManager()->getUserByJID(barejid); - if (!m_component->m_config["registration.enable_public_registration"].as()) { - std::list const &x = m_component->m_config["registration.enable_public_registration"].as >(); + if (!CONFIG_BOOL(m_config,"registration.enable_public_registration")) { + std::list 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::sendError(from, id, ErrorPayload::BadRequest, ErrorPayload::Modify); @@ -294,7 +295,7 @@ bool UserRegistration::handleSetRequest(const Swift::JID& from, const Swift::JID // Register or change password if (payload->getUsername()->isEmpty() || - (payload->getPassword()->isEmpty() && m_component->m_config["service.protocol"].as() != "twitter" && m_component->m_config["service.protocol"].as() != "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() ) { @@ -302,7 +303,7 @@ bool UserRegistration::handleSetRequest(const Swift::JID& from, const Swift::JID return true; } - if (m_component->m_config["service.protocol"].as() == "xmpp") { + if (CONFIG_STRING(m_config, "service.protocol") == "xmpp") { // User tries to register himself. if ((Swift::JID(*payload->getUsername()).toBare() == from.toBare())) { Swift::SetResponder::sendError(from, id, ErrorPayload::NotAcceptable, ErrorPayload::Modify); @@ -322,8 +323,8 @@ bool UserRegistration::handleSetRequest(const Swift::JID& from, const Swift::JID // m_component->protocol()->prepareUsername(username); std::string newUsername(username); - if (!m_component->m_config["registration.username_mask"].as().empty()) { - newUsername = m_component->m_config["registration.username_mask"].as(); + if (!CONFIG_STRING(m_config, "registration.username_mask").empty()) { + newUsername = CONFIG_STRING(m_config, "registration.username_mask"); // replace(newUsername, "$username", username.c_str()); } @@ -334,8 +335,8 @@ bool UserRegistration::handleSetRequest(const Swift::JID& from, const Swift::JID // } // #if GLIB_CHECK_VERSION(2,14,0) -// if (!m_component->m_config["registration.reg_allowed_usernames"].as().empty() && -// !g_regex_match_simple(m_component->m_config["registration.reg_allowed_usernames"].as(), 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::sendError(from, id, ErrorPayload::NotAcceptable, ErrorPayload::Modify); // return true;