diff --git a/backends/libcommuni/singleircnetworkplugin.cpp b/backends/libcommuni/singleircnetworkplugin.cpp index 378df949..276ac5ec 100644 --- a/backends/libcommuni/singleircnetworkplugin.cpp +++ b/backends/libcommuni/singleircnetworkplugin.cpp @@ -58,6 +58,7 @@ void SingleIRCNetworkPlugin::handleLoginRequest(const std::string &user, const s session->setRealName(FROM_UTF8(legacyName)); session->setHost(FROM_UTF8(m_server)); session->setPort(6667); + session->setEncoding( "utf-8" ); if (!password.empty()) { std::string identify = m_identify; diff --git a/include/transport/adhoccommand.h b/include/transport/adhoccommand.h index e9c1adfe..03bc21b8 100644 --- a/include/transport/adhoccommand.h +++ b/include/transport/adhoccommand.h @@ -41,6 +41,8 @@ class AdHocCommand { virtual boost::shared_ptr handleRequest(boost::shared_ptr payload) = 0; + void addFormField(Swift::FormField::ref field); + const std::string &getId() { return m_id; } @@ -57,9 +59,10 @@ class AdHocCommand { Component *m_component; Swift::JID m_initiator; Swift::JID m_to; + std::vector m_fields; + std::string m_id; private: - std::string m_id; // This is used to remove AdHocCommand after long inactivity to prevent memory leaks // caused by users which disconnect before they finish the command. // AdHocManager uses this to garbage collect old AdHocCommands. diff --git a/include/transport/admininterface.h b/include/transport/admininterface.h index 393394eb..64082dd3 100644 --- a/include/transport/admininterface.h +++ b/include/transport/admininterface.h @@ -30,13 +30,16 @@ class Component; class StorageBackend; class UserManager; class NetworkPluginServer; +class UserRegistration; class AdminInterface { public: - AdminInterface(Component *component, UserManager *userManager, NetworkPluginServer *server = NULL, StorageBackend *storageBackend = NULL); + AdminInterface(Component *component, UserManager *userManager, NetworkPluginServer *server = NULL, StorageBackend *storageBackend = NULL, UserRegistration *userRegistration = NULL); ~AdminInterface(); + void handleQuery(Swift::Message::ref message); + private: void handleMessageReceived(Swift::Message::ref message); @@ -44,6 +47,7 @@ class AdminInterface { StorageBackend *m_storageBackend; UserManager *m_userManager; NetworkPluginServer *m_server; + UserRegistration *m_userRegistration; }; } diff --git a/include/transport/networkpluginserver.h b/include/transport/networkpluginserver.h index 7e43ce33..f8a601be 100644 --- a/include/transport/networkpluginserver.h +++ b/include/transport/networkpluginserver.h @@ -42,6 +42,7 @@ class VCardResponder; class RosterResponder; class BlockResponder; class DummyReadBytestream; +class AdminInterface; class NetworkPluginServer { public: @@ -63,6 +64,10 @@ class NetworkPluginServer { virtual ~NetworkPluginServer(); + void setAdminInterface(AdminInterface *adminInterface) { + m_adminInterface = adminInterface; + } + int getBackendCount() { return m_clients.size(); } @@ -84,6 +89,7 @@ class NetworkPluginServer { private: void handleNewClientConnection(boost::shared_ptr c); void handleSessionFinished(Backend *c); + void handlePongReceived(Backend *c); void handleDataRead(Backend *c, boost::shared_ptr data); void handleConnectedPayload(const std::string &payload); @@ -100,7 +106,8 @@ class NetworkPluginServer { void handleStatsPayload(Backend *c, const std::string &payload); void handleFTStartPayload(const std::string &payload); void handleFTFinishPayload(const std::string &payload); - void handleFTDataPayload(Backend *b ,const std::string &payload); + void handleFTDataPayload(Backend *b, const std::string &payload); + void handleQueryPayload(Backend *b, const std::string &payload); void handleUserCreated(User *user); void handleRoomJoined(User *user, const Swift::JID &who, const std::string &room, const std::string &nickname, const std::string &password); @@ -144,6 +151,7 @@ class NetworkPluginServer { std::map m_filetransfers; FileTransferManager *m_ftManager; std::vector m_crashedBackends; + AdminInterface *m_adminInterface; }; } diff --git a/include/transport/protocol.proto b/include/transport/protocol.proto index 8dddaf7e..929c02b7 100644 --- a/include/transport/protocol.proto +++ b/include/transport/protocol.proto @@ -158,6 +158,7 @@ message WrapperMessage { TYPE_FT_CONTINUE = 28; TYPE_EXIT = 29; TYPE_BACKEND_CONFIG = 30; + TYPE_QUERY = 31; } required Type type = 1; optional bytes payload = 2; diff --git a/include/transport/settingsadhoccommand.h b/include/transport/settingsadhoccommand.h index 8e653ecc..b34a7804 100644 --- a/include/transport/settingsadhoccommand.h +++ b/include/transport/settingsadhoccommand.h @@ -34,12 +34,19 @@ class Component; class SettingsAdHocCommand : public AdHocCommand { public: + typedef enum { Init, WaitingForResponse } State; + SettingsAdHocCommand(Component *component, const Swift::JID &initiator, const Swift::JID &to); /// Destructor. virtual ~SettingsAdHocCommand(); virtual boost::shared_ptr handleRequest(boost::shared_ptr payload); + + private: + boost::shared_ptr getForm(); + boost::shared_ptr handleResponse(boost::shared_ptr payload); + State m_state; }; class SettingsAdHocCommandFactory : public AdHocCommandFactory { diff --git a/spectrum/src/main.cpp b/spectrum/src/main.cpp index 45d8b594..8e1c1e1a 100644 --- a/spectrum/src/main.cpp +++ b/spectrum/src/main.cpp @@ -207,6 +207,16 @@ int main(int argc, char **argv) std::cerr << "Can't create service.working_dir directory " << CONFIG_STRING(&config, "service.working_dir") << ".\n"; return 1; } + // create directories + try { + boost::filesystem::create_directories( + boost::filesystem::path(CONFIG_STRING(&config, "service.portfile")).parent_path().string() + ); + } + catch (...) { + std::cerr << "Can't create service.portfile directory " << CONFIG_STRING(&config, "service.portfile") << ".\n"; + return 1; + } #ifndef WIN32 if (!CONFIG_STRING(&config, "service.group").empty() ||!CONFIG_STRING(&config, "service.user").empty() ) { @@ -223,6 +233,20 @@ int main(int argc, char **argv) chown(CONFIG_STRING(&config, "service.working_dir").c_str(), pw->pw_uid, gr->gr_gid); } + char backendport[20]; + FILE* port_file_f; + port_file_f = fopen(CONFIG_STRING(&config, "service.portfile").c_str(), "w+"); + if (port_file_f == NULL) { + std::cerr << "Cannot create port_file file " << CONFIG_STRING(&config, "service.portfile").c_str() << ". Exiting\n"; + exit(1); + } + sprintf(backendport,"%s\n",CONFIG_STRING(&config, "service.backend_port").c_str()); + if (fwrite(backendport,1,strlen(backendport),port_file_f) < strlen(backendport)) { + std::cerr << "Cannot write to port file " << CONFIG_STRING(&config, "service.portfile") << ". Exiting\n"; + exit(1); + } + fclose(port_file_f); + if (!no_daemon) { // daemonize daemonize(CONFIG_STRING(&config, "service.working_dir").c_str(), CONFIG_STRING(&config, "service.pidfile").c_str()); @@ -309,7 +333,9 @@ int main(int argc, char **argv) NetworkPluginServer plugin(&transport, &config, &userManager, &ftManager); - AdminInterface adminInterface(&transport, &userManager, &plugin, storageBackend); + AdminInterface adminInterface(&transport, &userManager, &plugin, storageBackend, userRegistration); + plugin.setAdminInterface(&adminInterface); + StatsResponder statsResponder(&transport, &userManager, &plugin, storageBackend); statsResponder.start(); diff --git a/spectrum_manager/src/CMakeLists.txt b/spectrum_manager/src/CMakeLists.txt index 5f93dd9c..ac9f30f3 100644 --- a/spectrum_manager/src/CMakeLists.txt +++ b/spectrum_manager/src/CMakeLists.txt @@ -1,9 +1,9 @@ cmake_minimum_required(VERSION 2.6) FILE(GLOB SRC *.cpp) -ADD_EXECUTABLE(spectrum2_manager ${SRC} ../../src/config.cpp ../../src/util.cpp) +ADD_EXECUTABLE(spectrum2_manager ${SRC} ../../src/config.cpp ../../src/util.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../include/transport/protocol.pb.cc) -target_link_libraries(spectrum2_manager ${SWIFTEN_LIBRARY}) +target_link_libraries(spectrum2_manager ${SWIFTEN_LIBRARY} ${PROTOBUF_LIBRARIES}) INSTALL(TARGETS spectrum2_manager RUNTIME DESTINATION bin) diff --git a/spectrum_manager/src/main.cpp b/spectrum_manager/src/main.cpp index 0ab40980..2c485490 100644 --- a/spectrum_manager/src/main.cpp +++ b/spectrum_manager/src/main.cpp @@ -1,5 +1,6 @@ #include "managerconfig.h" #include "transport/config.h" +#include "transport/protocol.pb.h" #include "Swiften/Swiften.h" #include "Swiften/EventLoop/SimpleEventLoop.h" @@ -12,6 +13,11 @@ #include "signal.h" #include "sys/wait.h" +#define WRAP(MESSAGE, TYPE) pbnetwork::WrapperMessage wrap; \ + wrap.set_type(TYPE); \ + wrap.set_payload(MESSAGE); \ + wrap.SerializeToString(&MESSAGE); + using namespace Transport; @@ -19,33 +25,7 @@ using namespace boost::filesystem; using namespace boost; -static int finished; -static std::string *m; - -static void handleDisconnected(Swift::Client *client, const boost::optional &, const std::string &server) { - std::cout << "[ DISCONNECTED ] " << server << "\n"; - if (--finished == 0) { - exit(0); - } -} - -static void handleConnected(Swift::Client *client, const std::string &server) { - boost::shared_ptr message(new Swift::Message()); - message->setTo(server); - message->setFrom(client->getJID()); - message->setBody(*m); - - client->sendMessage(message); -} - -static void handleMessageReceived(Swift::Client *client, Swift::Message::ref message, const std::string &server) { - std::string body = message->getBody(); - boost::replace_all(body, "\n", "\n[ OK ] " + server + ": "); - std::cout << "[ OK ] " << server << ": " << body << "\n"; - if (--finished == 0) { - exit(0); - } -} +std::string _data; static std::string searchForBinary(const std::string &binary) { std::vector path_list; @@ -103,6 +83,22 @@ static unsigned long exec_(std::string path, std::string config, std::string jid return (unsigned long) pid; } +static int getPort(const std::string &portfile) { + path p(portfile); + if (!exists(p) || is_directory(p)) { + return 0; + } + + std::ifstream f(p.string().c_str(), std::ios_base::in); + std::string port; + f >> port; + + if (port.empty()) + return 0; + + return boost::lexical_cast(port); +} + static int isRunning(const std::string &pidfile) { path p(pidfile); if (!exists(p) || is_directory(p)) { @@ -288,7 +284,75 @@ static int show_status(ManagerConfig *config) { return ret; } -static void ask_local_servers(ManagerConfig *config, Swift::BoostNetworkFactories &networkFactories, const std::string &message) { +static void handleDataRead(boost::shared_ptr m_conn, boost::shared_ptr data) { + _data += std::string(data->begin(), data->end()); + + // Parse data while there are some + while (_data.size() != 0) { + // expected_size of wrapper message + unsigned int expected_size; + + // if data is >= 4, we have whole header and we can + // read expected_size. + if (_data.size() >= 4) { + expected_size = *((unsigned int*) &_data[0]); + expected_size = ntohl(expected_size); + // If we don't have whole wrapper message, wait for next + // handleDataRead call. + if (_data.size() - 4 < expected_size) + return; + } + else { + return; + } + + // Parse wrapper message and erase it from buffer. + pbnetwork::WrapperMessage wrapper; + if (wrapper.ParseFromArray(&_data[4], expected_size) == false) { + std::cout << "PARSING ERROR " << expected_size << "\n"; + _data.erase(_data.begin(), _data.begin() + 4 + expected_size); + continue; + } + _data.erase(_data.begin(), _data.begin() + 4 + expected_size); + + if (wrapper.type() == pbnetwork::WrapperMessage_Type_TYPE_QUERY) { + pbnetwork::BackendConfig payload; + if (payload.ParseFromString(wrapper.payload()) == false) { + std::cout << "PARSING ERROR\n"; + // TODO: ERROR + continue; + } + + std::cout << payload.config() << "\n"; + exit(0); + } + } +} + +static void handleConnected(boost::shared_ptr m_conn, const std::string &msg, bool error) { + if (error) { + std::cerr << "Can't connect the server\n"; + exit(50); + } + else { + pbnetwork::BackendConfig m; + m.set_config(msg); + + std::string message; + m.SerializeToString(&message); + + WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_QUERY); + + uint32_t size = htonl(message.size()); + char *header = (char *) &size; + + + // send header together with wrapper message + m_conn->write(Swift::createSafeByteArray(std::string(header, 4) + message)); + } +} + +static void ask_local_server(ManagerConfig *config, Swift::BoostNetworkFactories &networkFactories, const std::string &jid, const std::string &message) { path p(CONFIG_STRING(config, "service.config_directory")); try { @@ -302,6 +366,7 @@ static void ask_local_servers(ManagerConfig *config, Swift::BoostNetworkFactorie exit(7); } + bool found = false; directory_iterator end_itr; for (directory_iterator itr(p); itr != end_itr; ++itr) { if (is_regular(itr->path()) && extension(itr->path()) == ".cfg") { @@ -311,22 +376,34 @@ static void ask_local_servers(ManagerConfig *config, Swift::BoostNetworkFactorie continue; } - if (CONFIG_VECTOR(&cfg, "service.admin_jid").empty() || CONFIG_STRING(&cfg, "service.admin_password").empty()) { - std::cerr << itr->path().string() << ": service.admin_jid or service.admin_password empty. This server can't be queried over XMPP.\n"; + if (CONFIG_STRING(&cfg, "service.jid") != jid) { continue; } - finished++; - Swift::Client *client = new Swift::Client(CONFIG_VECTOR(&cfg, "service.admin_jid")[0], CONFIG_STRING(&cfg, "service.admin_password"), &networkFactories); - client->setAlwaysTrustCertificates(); - client->onConnected.connect(boost::bind(&handleConnected, client, CONFIG_STRING(&cfg, "service.jid"))); - client->onDisconnected.connect(bind(&handleDisconnected, client, _1, CONFIG_STRING(&cfg, "service.jid"))); - client->onMessageReceived.connect(bind(&handleMessageReceived, client, _1, CONFIG_STRING(&cfg, "service.jid"))); - Swift::ClientOptions opt; - opt.allowPLAINWithoutTLS = true; - client->connect(opt); + found = true; + + boost::shared_ptr m_conn; + m_conn = networkFactories.getConnectionFactory()->createConnection(); + m_conn->onDataRead.connect(boost::bind(&handleDataRead, m_conn, _1)); + m_conn->onConnectFinished.connect(boost::bind(&handleConnected, m_conn, message, _1)); + m_conn->connect(Swift::HostAddressPort(Swift::HostAddress(CONFIG_STRING(&cfg, "service.backend_host")), getPort(CONFIG_STRING(&cfg, "service.portfile")))); + +// finished++; +// Swift::Client *client = new Swift::Client(CONFIG_VECTOR(&cfg, "service.admin_jid")[0], CONFIG_STRING(&cfg, "service.admin_password"), &networkFactories); +// client->setAlwaysTrustCertificates(); +// client->onConnected.connect(boost::bind(&handleConnected, client, CONFIG_STRING(&cfg, "service.jid"))); +// client->onDisconnected.connect(bind(&handleDisconnected, client, _1, CONFIG_STRING(&cfg, "service.jid"))); +// client->onMessageReceived.connect(bind(&handleMessageReceived, client, _1, CONFIG_STRING(&cfg, "service.jid"))); +// Swift::ClientOptions opt; +// opt.allowPLAINWithoutTLS = true; +// client->connect(opt); } } + + if (!found) { + std::cerr << "Config file for Spectrum instance with this JID was not found\n"; + exit(20) + } } catch (const filesystem_error& ex) { std::cerr << "boost filesystem error\n"; @@ -334,24 +411,71 @@ static void ask_local_servers(ManagerConfig *config, Swift::BoostNetworkFactorie } } +// static void ask_local_servers(ManagerConfig *config, Swift::BoostNetworkFactories &networkFactories, const std::string &message) { +// path p(CONFIG_STRING(config, "service.config_directory")); +// +// try { +// if (!exists(p)) { +// std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n"; +// exit(6); +// } +// +// if (!is_directory(p)) { +// std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n"; +// exit(7); +// } +// +// directory_iterator end_itr; +// for (directory_iterator itr(p); itr != end_itr; ++itr) { +// if (is_regular(itr->path()) && extension(itr->path()) == ".cfg") { +// Config cfg; +// if (cfg.load(itr->path().string()) == false) { +// std::cerr << "Can't load config file " << itr->path().string() << ". Skipping...\n"; +// continue; +// } +// +// if (CONFIG_VECTOR(&cfg, "service.admin_jid").empty() || CONFIG_STRING(&cfg, "service.admin_password").empty()) { +// std::cerr << itr->path().string() << ": service.admin_jid or service.admin_password empty. This server can't be queried over XMPP.\n"; +// continue; +// } +// +// finished++; +// Swift::Client *client = new Swift::Client(CONFIG_VECTOR(&cfg, "service.admin_jid")[0], CONFIG_STRING(&cfg, "service.admin_password"), &networkFactories); +// client->setAlwaysTrustCertificates(); +// client->onConnected.connect(boost::bind(&handleConnected, client, CONFIG_STRING(&cfg, "service.jid"))); +// client->onDisconnected.connect(bind(&handleDisconnected, client, _1, CONFIG_STRING(&cfg, "service.jid"))); +// client->onMessageReceived.connect(bind(&handleMessageReceived, client, _1, CONFIG_STRING(&cfg, "service.jid"))); +// Swift::ClientOptions opt; +// opt.allowPLAINWithoutTLS = true; +// client->connect(opt); +// } +// } +// } +// catch (const filesystem_error& ex) { +// std::cerr << "boost filesystem error\n"; +// exit(5); +// } +// } + int main(int argc, char **argv) { ManagerConfig config; std::string config_file; - std::string command; + std::vector command; boost::program_options::variables_map vm; - boost::program_options::options_description desc("Usage: spectrum [OPTIONS] \nCommands:\n" + boost::program_options::options_description desc("Usage: spectrum [OPTIONS] \n" + " spectrum [OPTIONS] \nCommands:\n" " start - start all local Spectrum2 instances\n" - " stop - stop all local Spectrum2 instances\n" + " stop - stop all local Spectrum2 instances\n" " status - status of local Spectrum2 instances\n" - " - send command to all local + remote Spectrum2 instances and print output\n" + " - send command to local Spectrum2 instance and print output\n" "Allowed options"); desc.add_options() ("help,h", "Show help output") ("config,c", boost::program_options::value(&config_file)->default_value("/etc/spectrum2/spectrum_manager.cfg"), "Spectrum manager config file") - ("command", boost::program_options::value(&command)->default_value(""), "Command") + ("command", boost::program_options::value >(&command), "Command") ; try { @@ -388,37 +512,32 @@ int main(int argc, char **argv) return 1; } - if (command == "start") { + if (command[0] == "start") { start_all_instances(&config); } - else if (command == "stop") { + else if (command[0] == "stop") { stop_all_instances(&config); } - else if (command == "status") { + else if (command[0] == "status") { return show_status(&config); } else { + if (command.size() < 2) { + std::cout << desc << "\n"; + return 11; + } Swift::SimpleEventLoop eventLoop; Swift::BoostNetworkFactories networkFactories(&eventLoop); - std::string message = command; - m = &message; + std::string jid = command[0]; + command.erase(command.begin()); + std::string cmd = boost::algorithm::join(command, " "); - ask_local_servers(&config, networkFactories, message); + ask_local_server(&config, networkFactories, jid, cmd); +// std::string message = command; +// m = &message; - 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(boost::bind(&handleConnected, client, *it)); - client->onDisconnected.connect(bind(&handleDisconnected, client, _1, *it)); - client->onMessageReceived.connect(bind(&handleMessageReceived, client, _1, *it)); - Swift::ClientOptions opt; - opt.allowPLAINWithoutTLS = true; - client->connect(opt); - // std::cout << *it << "\n"; - } +// ask_local_server(&config, networkFactories, message); eventLoop.run(); } diff --git a/src/adhoccommand.cpp b/src/adhoccommand.cpp index e121beaf..badc5dc2 100644 --- a/src/adhoccommand.cpp +++ b/src/adhoccommand.cpp @@ -45,4 +45,8 @@ AdHocCommand::AdHocCommand(Component *component, const Swift::JID &initiator, co AdHocCommand::~AdHocCommand() { } +void AdHocCommand::addFormField(Swift::FormField::ref field) { + m_fields.push_back(field); +} + } diff --git a/src/admininterface.cpp b/src/admininterface.cpp index db3c53a6..85dca619 100644 --- a/src/admininterface.cpp +++ b/src/admininterface.cpp @@ -27,6 +27,7 @@ #include "transport/usermanager.h" #include "transport/networkpluginserver.h" #include "transport/logging.h" +#include "transport/userregistration.h" #include "storageresponder.h" #include "transport/memoryusage.h" #include @@ -43,11 +44,12 @@ static std::string getArg(const std::string &body) { return body.substr(body.find(" ") + 1); } -AdminInterface::AdminInterface(Component *component, UserManager *userManager, NetworkPluginServer *server, StorageBackend *storageBackend) { +AdminInterface::AdminInterface(Component *component, UserManager *userManager, NetworkPluginServer *server, StorageBackend *storageBackend, UserRegistration *userRegistration) { m_component = component; m_storageBackend = storageBackend; m_userManager = userManager; m_server = server; + m_userRegistration = userRegistration; m_component->getStanzaChannel()->onMessageReceived.connect(bind(&AdminInterface::handleMessageReceived, this, _1)); } @@ -55,22 +57,7 @@ AdminInterface::AdminInterface(Component *component, UserManager *userManager, N AdminInterface::~AdminInterface() { } -void AdminInterface::handleMessageReceived(Swift::Message::ref message) { - if (!message->getTo().getNode().empty()) - return; - - std::vector const &x = CONFIG_VECTOR(m_component->getConfig(),"service.admin_jid"); - if (std::find(x.begin(), x.end(), message->getFrom().toBare().toString()) == x.end()) { - LOG4CXX_WARN(logger, "Message not from admin user, but from " << message->getFrom().toBare().toString()); - return; - - } - - // Ignore empty messages - if (message->getBody().empty()) { - return; - } - +void AdminInterface::handleQuery(Swift::Message::ref message) { LOG4CXX_INFO(logger, "Message from admin received"); message->setTo(message->getFrom()); message->setFrom(m_component->getJID()); @@ -267,6 +254,46 @@ void AdminInterface::handleMessageReceived(Swift::Message::ref message) { int msgCount = m_userManager->getMessagesToXMPP(); message->setBody(boost::lexical_cast(msgCount)); } + else if (message->getBody().find("register ") == 0 && m_userRegistration) { + std::string body = message->getBody(); + std::vector args; + boost::split(args, body, boost::is_any_of(" ")); + if (args.size() == 4) { + UserInfo res; + res.jid = args[1]; + res.uin = args[2]; + res.password = args[3]; + res.language = "en"; + res.encoding = "utf-8"; + res.vip = 0; + + if (m_userRegistration->registerUser(res)) { + message->setBody("User registered."); + } + else { + message->setBody("Registration failed: User is already registered"); + } + } + else { + message->setBody("Bad argument count. See 'help'."); + } + } + else if (message->getBody().find("unregister ") == 0 && m_userRegistration) { + std::string body = message->getBody(); + std::vector args; + boost::split(args, body, boost::is_any_of(" ")); + if (args.size() == 2) { + if (m_userRegistration->unregisterUser(args[1])) { + message->setBody("User unregistered."); + } + else { + message->setBody("Registration failed: User is not registered"); + } + } + else { + message->setBody("Bad argument count. See 'help'."); + } + } else if (message->getBody().find("help") == 0) { std::string help; help += "General:\n"; @@ -277,6 +304,10 @@ void AdminInterface::handleMessageReceived(Swift::Message::ref message) { help += " online_users_count - number of online users\n"; help += " online_users_per_backend - shows online users per backends\n"; help += " has_online_user - returns 1 if user is online\n"; + if (m_userRegistration) { + help += " register - registers the new user\n"; + help += " unregister - unregisters existing user\n"; + } help += "Messages:\n"; help += " messages_from_xmpp - get number of messages received from XMPP users\n"; help += " messages_to_xmpp - get number of messages sent to XMPP users\n"; @@ -299,6 +330,25 @@ void AdminInterface::handleMessageReceived(Swift::Message::ref message) { else { message->setBody("Unknown command. Try \"help\""); } +} + +void AdminInterface::handleMessageReceived(Swift::Message::ref message) { + if (!message->getTo().getNode().empty()) + return; + + std::vector const &x = CONFIG_VECTOR(m_component->getConfig(),"service.admin_jid"); + if (std::find(x.begin(), x.end(), message->getFrom().toBare().toString()) == x.end()) { + LOG4CXX_WARN(logger, "Message not from admin user, but from " << message->getFrom().toBare().toString()); + return; + + } + + // Ignore empty messages + if (message->getBody().empty()) { + return; + } + + handleQuery(message); m_component->getStanzaChannel()->sendMessage(message); } diff --git a/src/config.cpp b/src/config.cpp index e9d31872..86078320 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -75,6 +75,7 @@ bool Config::load(std::istream &ifs, boost::program_options::options_description ("service.backend", value()->default_value("libpurple_backend"), "Backend") ("service.protocol", value()->default_value(""), "Protocol") ("service.pidfile", value()->default_value("/var/run/spectrum2/$jid.pid"), "Full path to pid file") + ("service.portfile", value()->default_value("/var/run/spectrum2/$jid.port"), "File to store backend_port to. It's used by spectrum2_manager.") ("service.working_dir", value()->default_value("/var/lib/spectrum2/$jid"), "Working dir") ("service.allowed_servers", value >()->multitoken(), "Only users from these servers can connect") ("service.server_mode", value()->default_value(false), "True if Spectrum should behave as server") diff --git a/src/networkpluginserver.cpp b/src/networkpluginserver.cpp index 1f77af16..8a514b0a 100644 --- a/src/networkpluginserver.cpp +++ b/src/networkpluginserver.cpp @@ -32,6 +32,7 @@ #include "transport/rosterresponder.h" #include "transport/memoryreadbytestream.h" #include "transport/logging.h" +#include "transport/admininterface.h" #include "blockresponder.h" #include "Swiften/Swiften.h" #include "Swiften/Server/ServerStanzaChannel.h" @@ -227,6 +228,7 @@ NetworkPluginServer::NetworkPluginServer(Component *component, Config *config, U m_config = config; m_component = component; m_isNextLongRun = false; + m_adminInterface = NULL; m_component->m_factory = new NetworkFactory(this); m_userManager->onUserCreated.connect(boost::bind(&NetworkPluginServer::handleUserCreated, this, _1)); m_userManager->onUserDestroyed.connect(boost::bind(&NetworkPluginServer::handleUserDestroyed, this, _1)); @@ -317,18 +319,14 @@ void NetworkPluginServer::handleNewClientConnection(boost::shared_ptrres = 0; client->init_res = 0; client->shared = 0; - client->willDie = 0; + // Until we receive first PONG from backend, backend is in willDie state. + client->willDie = true; // Backend does not accept new clients automatically if it's long-running client->acceptUsers = !m_isNextLongRun; client->longRun = m_isNextLongRun; LOG4CXX_INFO(logger, "New" + (client->longRun ? std::string(" long-running") : "") + " backend " << client << " connected. Current backend count=" << (m_clients.size() + 1)); - if (m_clients.size() == 0) { - // first backend connected, start the server, we're ready. - m_component->start(); - } - m_clients.push_front(client); c->onDisconnected.connect(boost::bind(&NetworkPluginServer::handleSessionFinished, this, client)); @@ -339,28 +337,6 @@ void NetworkPluginServer::handleNewClientConnection(boost::shared_ptrpongReceived = -1; - - // some users are in queue waiting for this backend - while(!m_waitingUsers.empty()) { - // There's no new backend, so stop associating users and wait for new backend, - // which has been already spawned in getFreeClient() call. - if (getFreeClient() == NULL) - break; - - User *u = m_waitingUsers.front(); - m_waitingUsers.pop_front(); - - LOG4CXX_INFO(logger, "Associating " << u->getJID().toString() << " with this backend"); - - // associate backend with user - handleUserCreated(u); - - // connect user if it's ready - if (u->isReadyToConnect()) { - handleUserReadyToConnect(u); - } - - } } void NetworkPluginServer::handleSessionFinished(Backend *c) { @@ -752,6 +728,68 @@ void NetworkPluginServer::handleFTDataNeeded(Backend *b, unsigned long ftid) { send(b->connection, message); } +void NetworkPluginServer::handlePongReceived(Backend *c) { + // This could be first PONG from the backend + if (c->pongReceived == -1) { + // Backend is fully ready to handle requests + c->willDie = false; + + if (m_clients.size() == 1) { + // first backend connected, start the server, we're ready. + m_component->start(); + } + + // some users are in queue waiting for this backend + while(!m_waitingUsers.empty()) { + // There's no new backend, so stop associating users and wait for new backend, + // which has been already spawned in getFreeClient() call. + if (getFreeClient() == NULL) + break; + + User *u = m_waitingUsers.front(); + m_waitingUsers.pop_front(); + + LOG4CXX_INFO(logger, "Associating " << u->getJID().toString() << " with this backend"); + + // associate backend with user + handleUserCreated(u); + + // connect user if it's ready + if (u->isReadyToConnect()) { + handleUserReadyToConnect(u); + } + } + } + + c->pongReceived = true; +} + +void NetworkPluginServer::handleQueryPayload(Backend *b, const std::string &data) { + pbnetwork::BackendConfig payload; + if (payload.ParseFromString(data) == false) { + // TODO: ERROR + return; + } + + if (!m_adminInterface) { + return; + } + + boost::shared_ptr msg(new Swift::Message()); + msg->setBody(payload.config()); + m_adminInterface->handleQuery(msg); + + pbnetwork::BackendConfig vcard; + vcard.set_config(msg->getBody()); + + std::string message; + vcard.SerializeToString(&message); + + WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_QUERY); + + send(b->connection, message); +} + void NetworkPluginServer::handleDataRead(Backend *c, boost::shared_ptr data) { // Append data to buffer c->data.insert(c->data.end(), data->begin(), data->end()); @@ -802,7 +840,7 @@ void NetworkPluginServer::handleDataRead(Backend *c, boost::shared_ptrpongReceived = true; + handlePongReceived(c); break; case pbnetwork::WrapperMessage_Type_TYPE_PARTICIPANT_CHANGED: handleParticipantChangedPayload(wrapper.payload()); @@ -843,6 +881,9 @@ void NetworkPluginServer::handleDataRead(Backend *c, boost::shared_ptrpongReceived || (*it)->pongReceived == -1) { - sendPing((*it)); + // Don't send another ping if pongReceived == -1, because we've already sent one + // when registering backend. + if ((*it)->pongReceived) { + sendPing((*it)); + } } else { LOG4CXX_INFO(logger, "Disconnecting backend " << (*it) << " (ID=" << (*it)->id << "). PING response not received."); diff --git a/src/rosterstorage.cpp b/src/rosterstorage.cpp index 30679cb9..8716245b 100644 --- a/src/rosterstorage.cpp +++ b/src/rosterstorage.cpp @@ -86,6 +86,13 @@ RosterStorage::~RosterStorage() { } void RosterStorage::storeBuddy(Buddy *buddy) { + if (!buddy) { + return; + } + if (buddy->getName().empty()) { + return; + } + m_buddies[buddy->getName()] = buddy; m_storageTimer->start(); } @@ -129,6 +136,7 @@ bool RosterStorage::storeBuddies() { // } } + m_buddies.clear(); m_storageBackend->commitTransaction(); return true; } diff --git a/src/settingsadhoccommand.cpp b/src/settingsadhoccommand.cpp index dd90540b..00aacece 100644 --- a/src/settingsadhoccommand.cpp +++ b/src/settingsadhoccommand.cpp @@ -31,15 +31,47 @@ namespace Transport { DEFINE_LOGGER(logger, "SettingsAdHocCommand"); SettingsAdHocCommand::SettingsAdHocCommand(Component *component, const Swift::JID &initiator, const Swift::JID &to) : AdHocCommand(component, initiator, to) { + m_state = Init; } SettingsAdHocCommand::~SettingsAdHocCommand() { } +boost::shared_ptr SettingsAdHocCommand::getForm() { + boost::shared_ptr response(new Swift::Command("settings", m_id, Swift::Command::Executing)); + boost::shared_ptr form(new Swift::Form()); + + BOOST_FOREACH(Swift::FormField::ref field, m_fields) { + form->addField(field); + } + + response->setForm(form); + return response; +} + +boost::shared_ptr SettingsAdHocCommand::handleResponse(boost::shared_ptr payload) { + + + + boost::shared_ptr response; + response->setStatus(Swift::Command::Completed); + return response; +} + boost::shared_ptr SettingsAdHocCommand::handleRequest(boost::shared_ptr payload) { boost::shared_ptr response; - - + + switch (m_state) { + case Init: + response = getForm(); + m_state = WaitingForResponse; + break; + case WaitingForResponse: + response = handleResponse(payload); + break; + default: + break; + } return response; } diff --git a/src/sqlite3backend.cpp b/src/sqlite3backend.cpp index e9a11dac..a079c90b 100644 --- a/src/sqlite3backend.cpp +++ b/src/sqlite3backend.cpp @@ -199,7 +199,11 @@ bool SQLite3Backend::exec(const std::string &query) { char *errMsg = 0; int rc = sqlite3_exec(m_db, query.c_str(), NULL, 0, &errMsg); if (rc != SQLITE_OK) { - LOG4CXX_ERROR(logger, errMsg << " during statement " << query); + // This error is OK, because we try to create buddies table every time + // to detect if DB is created properly. + if (errMsg != "table buddies already exists") { + LOG4CXX_ERROR(logger, errMsg << " during statement " << query); + } sqlite3_free(errMsg); return false; } diff --git a/src/transport.cpp b/src/transport.cpp index 391c8ffa..094ccf37 100644 --- a/src/transport.cpp +++ b/src/transport.cpp @@ -191,6 +191,9 @@ void Component::setBuddyFeatures(std::list &features) { void Component::start() { if (m_component && !m_component->isAvailable()) { LOG4CXX_INFO(logger, "Connecting XMPP server " << CONFIG_STRING(m_config, "service.server") << " port " << CONFIG_INT(m_config, "service.port")); + if (CONFIG_INT(m_config, "service.port") == 5222) { + LOG4CXX_WARN(logger, "Port 5222 is usually used for client connections, not for component connections! Are you sure you are using right port?"); + } m_reconnectCount++; m_component->connect(CONFIG_STRING(m_config, "service.server"), CONFIG_INT(m_config, "service.port")); m_reconnectTimer->stop();