diff --git a/backends/frotz/main.cpp b/backends/frotz/main.cpp index 1e8b2f65..01f67245 100644 --- a/backends/frotz/main.cpp +++ b/backends/frotz/main.cpp @@ -339,56 +339,19 @@ int main (int argc, char* argv[]) { return -1; } - std::string configFile; - boost::program_options::variables_map vm; - boost::program_options::options_description desc("Usage: spectrum \nAllowed options"); - desc.add_options() - ("help", "help") - ("host,h", boost::program_options::value(&host)->default_value(""), "Host to connect to") - ("port,p", boost::program_options::value(&port)->default_value(10000), "Port to connect to") - ("config", boost::program_options::value(&configFile)->default_value(""), "Config file") - ; - - try - { - boost::program_options::positional_options_description p; - p.add("config", -1); - boost::program_options::store(boost::program_options::command_line_parser(argc, argv). - options(desc).positional(p).allow_unregistered().run(), vm); - boost::program_options::notify(vm); - - if(vm.count("help")) - { - std::cout << desc << "\n"; - return 1; - } - - if(vm.count("config") == 0) { - std::cout << desc << "\n"; - return 1; - } - } - catch (std::runtime_error& e) - { - std::cout << desc << "\n"; - return 1; - } - catch (...) - { - std::cout << desc << "\n"; - return 1; - } - - Config config(argc, argv); - if (!config.load(configFile)) { - std::cerr << "Can't open " << argv[1] << " configuration file.\n"; + std::string error; + Config *cfg = Config::createFromArgs(argc, argv, error, host, port); + if (cfg == NULL) { + std::cerr << error; return 1; } Swift::SimpleEventLoop eventLoop; loop_ = &eventLoop; - np = new FrotzNetworkPlugin(&config, &eventLoop, host, port); + np = new FrotzNetworkPlugin(cfg, &eventLoop, host, port); loop_->run(); + delete cfg; + return 0; } diff --git a/backends/libcommuni/main.cpp b/backends/libcommuni/main.cpp index c059714c..4509b9af 100644 --- a/backends/libcommuni/main.cpp +++ b/backends/libcommuni/main.cpp @@ -27,63 +27,24 @@ int main (int argc, char* argv[]) { std::string host; int port; - - std::string configFile; - boost::program_options::variables_map vm; - boost::program_options::options_description desc("Usage: spectrum \nAllowed options"); - desc.add_options() - ("help", "help") - ("host,h", boost::program_options::value(&host)->default_value(""), "Host to connect to") - ("port,p", boost::program_options::value(&port)->default_value(10000), "Port to connect to") - ("config", boost::program_options::value(&configFile)->default_value(""), "Config file") - ; - - try - { - boost::program_options::positional_options_description p; - p.add("config", -1); - boost::program_options::store(boost::program_options::command_line_parser(argc, argv). - options(desc).positional(p).allow_unregistered().run(), vm); - boost::program_options::notify(vm); - - if(vm.count("help")) - { - std::cout << desc << "\n"; - return 1; - } - - if(vm.count("config") == 0) { - std::cout << desc << "\n"; - return 1; - } - } - catch (std::runtime_error& e) - { - std::cout << desc << "\n"; - return 1; - } - catch (...) - { - std::cout << desc << "\n"; + std::string error; + Config *cfg = Config::createFromArgs(argc, argv, error, host, port); + if (cfg == NULL) { + std::cerr << error; return 1; } - Config config(argc, argv); - if (!config.load(configFile)) { - std::cerr << "Can't open " << argv[1] << " configuration file.\n"; - return 1; - } QCoreApplication app(argc, argv); - Logging::initBackendLogging(&config); + Logging::initBackendLogging(cfg); Swift::QtEventLoop eventLoop; - if (!CONFIG_HAS_KEY(&config, "service.irc_server")) { - np = new IRCNetworkPlugin(&config, &eventLoop, host, port); + if (!CONFIG_HAS_KEY(cfg, "service.irc_server")) { + np = new IRCNetworkPlugin(cfg, &eventLoop, host, port); } else { - np = new SingleIRCNetworkPlugin(&config, &eventLoop, host, port); + np = new SingleIRCNetworkPlugin(cfg, &eventLoop, host, port); } return app.exec(); diff --git a/backends/libpurple/main.cpp b/backends/libpurple/main.cpp index db76217f..fc4bc886 100644 --- a/backends/libpurple/main.cpp +++ b/backends/libpurple/main.cpp @@ -1641,51 +1641,14 @@ int main(int argc, char **argv) { } #endif - std::string configFile; - boost::program_options::variables_map vm; - boost::program_options::options_description desc("Usage: spectrum \nAllowed options"); - desc.add_options() - ("help", "help") - ("host,h", boost::program_options::value(&host)->default_value(""), "Host to connect to") - ("port,p", boost::program_options::value(&port)->default_value(10000), "Port to connect to") - ("config", boost::program_options::value(&configFile)->default_value(""), "Config file") - ; - - try - { - boost::program_options::positional_options_description p; - p.add("config", -1); - boost::program_options::store(boost::program_options::command_line_parser(argc, argv). - options(desc).positional(p).allow_unregistered().run(), vm); - boost::program_options::notify(vm); - - if(vm.count("help")) - { - std::cout << desc << "\n"; - return 1; - } - - if(vm.count("config") == 0) { - std::cout << desc << "\n"; - return 1; - } - } - catch (std::runtime_error& e) - { - std::cout << desc << "\n"; - return 1; - } - catch (...) - { - std::cout << desc << "\n"; + std::string error; + Config *cfg = Config::createFromArgs(argc, argv, error, host, port); + if (cfg == NULL) { + std::cerr << error; return 1; } - config = boost::make_shared(argc, argv); - if (!config->load(vm["config"].as())) { - std::cerr << "Can't load configuration file.\n"; - return 1; - } + config = boost::shared_ptr(cfg); Logging::initBackendLogging(config.get()); initPurple(); diff --git a/backends/libyahoo2/main.cpp b/backends/libyahoo2/main.cpp index d7937424..9c133839 100644 --- a/backends/libyahoo2/main.cpp +++ b/backends/libyahoo2/main.cpp @@ -741,60 +741,21 @@ int main (int argc, char* argv[]) { } #endif - std::string configFile; - boost::program_options::variables_map vm; - boost::program_options::options_description desc("Usage: spectrum \nAllowed options"); - desc.add_options() - ("help", "help") - ("host,h", boost::program_options::value(&host)->default_value(""), "Host to connect to") - ("port,p", boost::program_options::value(&port)->default_value(10000), "Port to connect to") - ("config", boost::program_options::value(&configFile)->default_value(""), "Config file") - ; - - try - { - boost::program_options::positional_options_description p; - p.add("config", -1); - boost::program_options::store(boost::program_options::command_line_parser(argc, argv). - options(desc).positional(p).allow_unregistered().run(), vm); - boost::program_options::notify(vm); - - if(vm.count("help")) - { - std::cout << desc << "\n"; - return 1; - } - - if(vm.count("config") == 0) { - std::cout << desc << "\n"; - return 1; - } - } - catch (std::runtime_error& e) - { - std::cout << desc << "\n"; - return 1; - } - catch (...) - { - std::cout << desc << "\n"; + std::string error; + Config *cfg = Config::createFromArgs(argc, argv, error, host, port); + if (cfg == NULL) { + std::cerr << error; return 1; } - Config config(argc, argv); - if (!config.load(configFile)) { - std::cerr << "Can't open " << argv[1] << " configuration file.\n"; - return 1; - } - - Logging::initBackendLogging(&config); + Logging::initBackendLogging(cfg); register_callbacks(); yahoo_set_log_level(YAHOO_LOG_DEBUG); Swift::SimpleEventLoop eventLoop; loop_ = &eventLoop; - np = new YahooPlugin(&config, &eventLoop, host, port); + np = new YahooPlugin(cfg, &eventLoop, host, port); loop_->run(); return 0; diff --git a/backends/skype/main.cpp b/backends/skype/main.cpp index 180e9182..99d77bc2 100644 --- a/backends/skype/main.cpp +++ b/backends/skype/main.cpp @@ -836,54 +836,14 @@ int main(int argc, char **argv) { } #endif - std::string configFile; - boost::program_options::variables_map vm; - boost::program_options::options_description desc("Usage: spectrum \nAllowed options"); - desc.add_options() - ("help", "help") - ("host,h", boost::program_options::value(&host)->default_value(""), "Host to connect to") - ("port,p", boost::program_options::value(&port)->default_value(10000), "Port to connect to") - ("config", boost::program_options::value(&configFile)->default_value(""), "Config file") - ; - - try - { - boost::program_options::positional_options_description p; - p.add("config", -1); - boost::program_options::store(boost::program_options::command_line_parser(argc, argv). - options(desc).positional(p).allow_unregistered().run(), vm); - boost::program_options::notify(vm); - - if(vm.count("help")) - { - std::cout << desc << "\n"; - return 1; - } - - if(vm.count("config") == 0) { - std::cout << desc << "\n"; - return 1; - } - } - catch (std::runtime_error& e) - { - std::cout << desc << "\n"; - return 1; - } - catch (...) - { - std::cout << desc << "\n"; + std::string error; + Config *cfg = Config::createFromArgs(argc, argv, error, host, port); + if (cfg == NULL) { + std::cerr << error; return 1; } - - Config config(argc, argv); - if (!config.load(configFile)) { - std::cout << "Can't open " << argv[1] << " configuration file.\n"; - return 1; - } - - Logging::initBackendLogging(&config); + Logging::initBackendLogging(cfg); g_type_init(); @@ -896,7 +856,7 @@ int main(int argc, char **argv) { channel = g_io_channel_unix_new(m_sock); g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, cond, transportDataReceived, NULL, io_destroy); - np = new SpectrumNetworkPlugin(&config, host, port); + np = new SpectrumNetworkPlugin(cfg, host, port); GMainLoop *m_loop; m_loop = g_main_loop_new(NULL, FALSE); diff --git a/backends/smstools3/main.cpp b/backends/smstools3/main.cpp index 16b172ad..31a22b78 100644 --- a/backends/smstools3/main.cpp +++ b/backends/smstools3/main.cpp @@ -260,56 +260,16 @@ int main (int argc, char* argv[]) { return -1; } - std::string configFile; - boost::program_options::variables_map vm; - boost::program_options::options_description desc("Usage: spectrum \nAllowed options"); - desc.add_options() - ("help", "help") - ("host,h", boost::program_options::value(&host)->default_value(""), "Host to connect to") - ("port,p", boost::program_options::value(&port)->default_value(10000), "Port to connect to") - ("config", boost::program_options::value(&configFile)->default_value(""), "Config file") - ; - - try - { - boost::program_options::positional_options_description p; - p.add("config", -1); - boost::program_options::store(boost::program_options::command_line_parser(argc, argv). - options(desc).positional(p).allow_unregistered().run(), vm); - boost::program_options::notify(vm); - - if(vm.count("help")) - { - std::cout << desc << "\n"; - return 1; - } - - if(vm.count("config") == 0) { - std::cout << desc << "\n"; - return 1; - } - } - catch (std::runtime_error& e) - { - std::cout << desc << "\n"; - return 1; - } - catch (...) - { - std::cout << desc << "\n"; - return 1; - } - - Config config(argc, argv); - if (!config.load(configFile)) { - std::cerr << "Can't open " << argv[1] << " configuration file.\n"; - return 1; - } - - Logging::initBackendLogging(&config); - std::string error; - StorageBackend *storageBackend = StorageBackend::createBackend(&config, error); + Config *cfg = Config::createFromArgs(argc, argv, error, host, port); + if (cfg == NULL) { + std::cerr << error; + return 1; + } + + Logging::initBackendLogging(cfg); + + StorageBackend *storageBackend = StorageBackend::createBackend(cfg, error); if (storageBackend == NULL) { if (!error.empty()) { std::cerr << error << "\n"; @@ -323,7 +283,7 @@ int main (int argc, char* argv[]) { Swift::SimpleEventLoop eventLoop; loop_ = &eventLoop; - np = new SMSNetworkPlugin(&config, &eventLoop, host, port); + np = new SMSNetworkPlugin(cfg, &eventLoop, host, port); loop_->run(); return 0; diff --git a/backends/swiften/main.cpp b/backends/swiften/main.cpp index 65b3fe4f..d9199aeb 100644 --- a/backends/swiften/main.cpp +++ b/backends/swiften/main.cpp @@ -264,57 +264,18 @@ int main (int argc, char* argv[]) { } #endif - std::string configFile; - boost::program_options::variables_map vm; - boost::program_options::options_description desc("Usage: spectrum \nAllowed options"); - desc.add_options() - ("help", "help") - ("host,h", boost::program_options::value(&host)->default_value(""), "Host to connect to") - ("port,p", boost::program_options::value(&port)->default_value(10000), "Port to connect to") - ("config", boost::program_options::value(&configFile)->default_value(""), "Config file") - ; - - try - { - boost::program_options::positional_options_description p; - p.add("config", -1); - boost::program_options::store(boost::program_options::command_line_parser(argc, argv). - options(desc).positional(p).allow_unregistered().run(), vm); - boost::program_options::notify(vm); - - if(vm.count("help")) - { - std::cout << desc << "\n"; - return 1; - } - - if(vm.count("config") == 0) { - std::cout << desc << "\n"; - return 1; - } - } - catch (std::runtime_error& e) - { - std::cout << desc << "\n"; - return 1; - } - catch (...) - { - std::cout << desc << "\n"; + std::string error; + Config *cfg = Config::createFromArgs(argc, argv, error, host, port); + if (cfg == NULL) { + std::cerr << error; return 1; } - Config config(argc, argv); - if (!config.load(configFile)) { - std::cerr << "Can't open " << argv[1] << " configuration file.\n"; - return 1; - } - - Logging::initBackendLogging(&config); + Logging::initBackendLogging(cfg); Swift::SimpleEventLoop eventLoop; loop_ = &eventLoop; - np = new SwiftenPlugin(&config, &eventLoop, host, port); + np = new SwiftenPlugin(cfg, &eventLoop, host, port); loop_->run(); return 0; diff --git a/backends/template/main.cpp b/backends/template/main.cpp index 730ece10..06d2d1ba 100644 --- a/backends/template/main.cpp +++ b/backends/template/main.cpp @@ -113,57 +113,19 @@ int main (int argc, char* argv[]) { return -1; } #endif - std::string configFile; - boost::program_options::variables_map vm; - boost::program_options::options_description desc("Usage: spectrum \nAllowed options"); - desc.add_options() - ("help", "help") - ("host,h", boost::program_options::value(&host)->default_value(""), "Host to connect to") - ("port,p", boost::program_options::value(&port)->default_value(10000), "Port to connect to") - ("config", boost::program_options::value(&configFile)->default_value(""), "Config file") - ; - try - { - boost::program_options::positional_options_description p; - p.add("config", -1); - boost::program_options::store(boost::program_options::command_line_parser(argc, argv). - options(desc).positional(p).allow_unregistered().run(), vm); - boost::program_options::notify(vm); - - if(vm.count("help")) - { - std::cout << desc << "\n"; - return 1; - } - - if(vm.count("config") == 0) { - std::cout << desc << "\n"; - return 1; - } - } - catch (std::runtime_error& e) - { - std::cout << desc << "\n"; - return 1; - } - catch (...) - { - std::cout << desc << "\n"; + std::string error; + Config *cfg = Config::createFromArgs(argc, argv, error, host, port); + if (cfg == NULL) { + std::cerr << error; return 1; } - Config config(argc, argv); - if (!config.load(configFile)) { - std::cerr << "Can't open " << argv[1] << " configuration file.\n"; - return 1; - } - - Logging::initBackendLogging(&config); + Logging::initBackendLogging(cfg); Swift::SimpleEventLoop eventLoop; loop_ = &eventLoop; - np = new TemplatePlugin(&config, &eventLoop, host, port); + np = new TemplatePlugin(cfg, &eventLoop, host, port); loop_->run(); return 0; diff --git a/include/transport/config.h b/include/transport/config.h index dd76bc7b..01c0786b 100644 --- a/include/transport/config.h +++ b/include/transport/config.h @@ -106,6 +106,8 @@ class Config { /// This signal is emitted when config is loaded/reloaded. boost::signal onConfigReloaded; + + static Config *createFromArgs(int argc, char **argv, std::string &error, std::string &host, int &port); private: int m_argc; diff --git a/src/config.cpp b/src/config.cpp index a5f670b2..552c9ff9 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -278,4 +278,56 @@ std::string Config::getCommandLineArgs() const { return commandLineArgs.str(); } +Config *Config::createFromArgs(int argc, char **argv, std::string &error, std::string &host, int &port) { + std::ostringstream os; + std::string configFile; + boost::program_options::variables_map vm; + boost::program_options::options_description desc("Usage: spectrum \nAllowed options"); + desc.add_options() + ("help", "help") + ("host,h", boost::program_options::value(&host)->default_value(""), "Host to connect to") + ("port,p", boost::program_options::value(&port)->default_value(10000), "Port to connect to") + ("config", boost::program_options::value(&configFile)->default_value(""), "Config file") + ; + + os << desc; + try + { + boost::program_options::positional_options_description p; + p.add("config", -1); + boost::program_options::store(boost::program_options::command_line_parser(argc, argv). + options(desc).positional(p).allow_unregistered().run(), vm); + boost::program_options::notify(vm); + + if(vm.count("help")) + { + error = os.str(); + return NULL; + } + + if(vm.count("config") == 0) { + error = os.str(); + return NULL; + } + } + catch (std::runtime_error& e) + { + error = os.str(); + return NULL; + } + catch (...) + { + error = os.str(); + return NULL; + } + + Config *config = new Config(argc, argv); + if (!config->load(configFile)) { + error = "Can't open " + configFile + " configuration file.\n"; + delete config; + return NULL; + } + return config; +} + }