Better Config class

This commit is contained in:
HanzZ 2011-02-13 22:47:28 +01:00
parent c5edfd19b1
commit bf4183c181
10 changed files with 74 additions and 64 deletions

View file

@ -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);

View file

@ -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();

View file

@ -26,12 +26,28 @@
#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;
};
}

View file

@ -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;
};

View file

@ -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;

View file

@ -31,6 +31,7 @@ struct UserInfo;
class Component;
class StorageBackend;
class UserManager;
class Config;
class UserRegistration : Swift::GetResponder<Swift::InBandRegistrationPayload>, Swift::SetResponder<Swift::InBandRegistrationPayload> {
public:
@ -54,6 +55,7 @@ class UserRegistration : Swift::GetResponder<Swift::InBandRegistrationPayload>,
Component *m_component;
StorageBackend *m_storageBackend;
UserManager *m_userManager;
Config *m_config;
};

View file

@ -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<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);
}
}
}

View file

@ -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<std::string>();
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<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;
}

View file

@ -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<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));
@ -95,7 +90,7 @@ void Component::setBuddyFeatures(std::list<std::string> &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();
}

View file

@ -32,6 +32,7 @@ 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();
@ -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<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;
}
@ -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<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);
@ -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<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")));
@ -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<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")));
@ -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<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));
@ -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<std::string>());
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<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;
}
@ -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<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);
@ -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<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()
)
{
@ -302,7 +303,7 @@ bool UserRegistration::handleSetRequest(const Swift::JID& from, const Swift::JID
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);
@ -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<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());
}
@ -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<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;