diff --git a/CMakeLists.txt b/CMakeLists.txt index 73e3c123..df2f5297 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -123,6 +123,7 @@ ADD_SUBDIRECTORY(include) ADD_SUBDIRECTORY(spectrum) ADD_SUBDIRECTORY(backends) #ADD_SUBDIRECTORY(tests) +ADD_SUBDIRECTORY(spectrum_manager) if(DOXYGEN_FOUND) message("Docs : yes") diff --git a/include/transport/config.h b/include/transport/config.h index ea21f562..3cf03973 100644 --- a/include/transport/config.h +++ b/include/transport/config.h @@ -33,6 +33,7 @@ #define CONFIG_INT(PTR, KEY) (*PTR)[KEY].as() #define CONFIG_BOOL(PTR, KEY) (*PTR)[KEY].as() #define CONFIG_LIST(PTR, KEY) (*PTR)[KEY].as >() +#define CONFIG_VECTOR(PTR, KEY) (*PTR)[KEY].as >() namespace Transport { diff --git a/spectrum_manager/CMakeLists.txt b/spectrum_manager/CMakeLists.txt new file mode 100644 index 00000000..4b7537b5 --- /dev/null +++ b/spectrum_manager/CMakeLists.txt @@ -0,0 +1 @@ +ADD_SUBDIRECTORY(src) diff --git a/spectrum_manager/src/CMakeLists.txt b/spectrum_manager/src/CMakeLists.txt new file mode 100644 index 00000000..73de569d --- /dev/null +++ b/spectrum_manager/src/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 2.6) +FILE(GLOB SRC *.cpp) + +ADD_EXECUTABLE(spectrum_manager ${SRC}) + +target_link_libraries(spectrum_manager transport) + +INSTALL(TARGETS spectrum_manager RUNTIME DESTINATION bin) + +INSTALL(FILES + spectrum_manager.cfg + DESTINATION /etc/spectrum2 + ) diff --git a/spectrum_manager/src/main.cpp b/spectrum_manager/src/main.cpp new file mode 100644 index 00000000..18470817 --- /dev/null +++ b/spectrum_manager/src/main.cpp @@ -0,0 +1,87 @@ +#include "managerconfig.h" +#include "transport/transport.h" +#include "transport/usermanager.h" +#include "transport/logger.h" +#include "transport/sqlite3backend.h" +#include "transport/userregistration.h" +#include "transport/networkpluginserver.h" +#include "Swiften/EventLoop/SimpleEventLoop.h" + +using namespace Transport; + +static int finished; + +static void handleDisconnected(Swift::Client *client, const boost::optional &) { + std::cout << "[ DISCONNECTED ] " << client->getJID().getDomain() << "\n"; + if (--finished == 0) { + exit(0); + } +} + +static void handleConnected(Swift::Client *client) { + std::cout << "[ OK ] " << client->getJID().getDomain() << "\n"; + if (--finished == 0) { + exit(0); + } +} + +int main(int argc, char **argv) +{ + ManagerConfig config; + + boost::program_options::options_description desc("Usage: spectrum_manager \nAllowed options"); + desc.add_options() + ("help,h", "help") + ; + try + { + boost::program_options::variables_map vm; + boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm); + boost::program_options::notify(vm); + if(vm.count("help")) + { + std::cout << desc << "\n"; + return 1; + } + } + catch (std::runtime_error& e) + { + std::cout << desc << "\n"; + return 1; + } + catch (...) + { + std::cout << desc << "\n"; + return 1; + } + + if (argc != 2) { + std::cout << desc << "\n"; + return 1; + } + + + if (!config.load(argv[1])) { + std::cerr << "Can't load configuration file.\n"; + return 1; + } + + Swift::SimpleEventLoop eventLoop; + Swift::BoostNetworkFactories networkFactories(&eventLoop); + + std::vector servers = CONFIG_VECTOR(&config, "servers.server"); + for (std::vector::const_iterator it = servers.begin(); it != servers.end(); it++) { + finished++; + Swift::Client *client = new Swift::Client(CONFIG_STRING(&config, "service.admin_username") + "@" + (*it), CONFIG_STRING(&config, "service.admin_password"), &networkFactories); + client->setAlwaysTrustCertificates(); +// client->onConnected.connect(bind(&handleConnected, client)); + client->onDisconnected.connect(bind(&handleDisconnected, client, _1)); +// client->onMessageReceived.connect(bind(&handleMessageReceived, _1)); + Swift::ClientOptions opt; + opt.allowPLAINWithoutTLS = true; + client->connect(opt); +// std::cout << *it << "\n"; + } + + eventLoop.run(); +} diff --git a/spectrum_manager/src/managerconfig.cpp b/spectrum_manager/src/managerconfig.cpp new file mode 100644 index 00000000..4af0300c --- /dev/null +++ b/spectrum_manager/src/managerconfig.cpp @@ -0,0 +1,50 @@ +/** + * libtransport -- C++ library for easy XMPP Transports development + * + * Copyright (C) 2011, Jan Kaluza + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA + */ + +#include "managerconfig.h" +#include + +using namespace boost::program_options; + +bool ManagerConfig::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.admin_username", value()->default_value(""), "Administrator username.") + ("service.admin_password", value()->default_value(""), "Administrator password.") + ("servers.server", value >()->multitoken(), "Server.") + ; + + store(parse_config_file(ifs, opts), m_variables); + notify(m_variables); + + m_file = configfile; + + onManagerConfigReloaded(); + + return true; +} + +bool ManagerConfig::load(const std::string &configfile) { + options_description opts("Transport options"); + return load(configfile, opts); +} diff --git a/spectrum_manager/src/managerconfig.h b/spectrum_manager/src/managerconfig.h new file mode 100644 index 00000000..933843c3 --- /dev/null +++ b/spectrum_manager/src/managerconfig.h @@ -0,0 +1,80 @@ +/** + * libtransport -- C++ library for easy XMPP Transports development + * + * Copyright (C) 2011, Jan Kaluza + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA + */ + +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +/// Represents variable:value pairs. +typedef boost::program_options::variables_map Variables; + +/// Represents config file. + +/// It's used to load config file and allows others parts of libtransport to be configured +/// properly. ManagerConfig files are text files which use "ini" format. Variables are divided into multiple +/// sections. Every class is configurable with some variables which change its behavior. Check particular +/// class documentation to get a list of all relevant variables for that class. +class ManagerConfig { + public: + /// Constructor. + ManagerConfig() {} + + /// Destructor + virtual ~ManagerConfig() {} + + /// Loads data from config file. + + /// You can pass your extra options which will be recognized by + /// the parser using opts parameter. + /// \param configfile path to config file + /// \param opts extra options which will be recognized by a parser + bool load(const std::string &configfile, boost::program_options::options_description &opts); + + /// Loads data from config file. + + /// This function loads only config variables needed by libtransport. + /// \see load(const std::string &, boost::program_options::options_description &) + /// \param configfile path to config file + bool load(const std::string &configfile); + + /// Returns value of variable defined by key. + + /// For variables in sections you can use "section.variable" key format. + /// \param key config variable name + const boost::program_options::variable_value &operator[] (const std::string &key) { + return m_variables[key]; + } + + /// Returns path to config file from which data were loaded. + const std::string &getManagerConfigFile() { return m_file; } + + /// This signal is emitted when config is loaded/reloaded. + boost::signal onManagerConfigReloaded; + + private: + Variables m_variables; + std::string m_file; +}; diff --git a/spectrum_manager/src/spectrum_manager.cfg b/spectrum_manager/src/spectrum_manager.cfg new file mode 100644 index 00000000..c24383f5 --- /dev/null +++ b/spectrum_manager/src/spectrum_manager.cfg @@ -0,0 +1,7 @@ +[service] +admin_username=admin +admin_password=test + +[servers] +server=localhost +server=icq.spectrum.im \ No newline at end of file