Files
@ 34207065ceb5
Branch filter:
Location: libtransport.git/src/admininterface.cpp
34207065ceb5
4.1 KiB
text/x-c++hdr
Better AdminInterface
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | /**
* libtransport -- C++ library for easy XMPP Transports development
*
* Copyright (C) 2011, Jan Kaluza <hanzz.k@gmail.com>
*
* 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 "transport/admininterface.h"
#include "transport/user.h"
#include "transport/transport.h"
#include "transport/storagebackend.h"
#include "transport/conversationmanager.h"
#include "transport/rostermanager.h"
#include "transport/usermanager.h"
#include "transport/networkpluginserver.h"
#include "storageresponder.h"
#include "log4cxx/logger.h"
using namespace log4cxx;
namespace Transport {
static LoggerPtr logger = Logger::getLogger("AdminInterface");
static std::string getArg(const std::string &body) {
std::string ret;
if (body.find(" ") == std::string::npos)
return ret;
return body.substr(body.find(" ") + 1);
}
AdminInterface::AdminInterface(Component *component, UserManager *userManager, NetworkPluginServer *server, StorageBackend *storageBackend) {
m_component = component;
m_storageBackend = storageBackend;
m_userManager = userManager;
m_server = server;
m_component->getStanzaChannel()->onMessageReceived.connect(bind(&AdminInterface::handleMessageReceived, this, _1));
}
AdminInterface::~AdminInterface() {
}
void AdminInterface::handleMessageReceived(Swift::Message::ref message) {
if (!message->getTo().getNode().empty())
return;
if (message->getFrom().getNode() != CONFIG_STRING(m_component->getConfig(), "service.admin_username")) {
LOG4CXX_WARN(logger, "Message not from admin user, but from " << message->getFrom().getNode());
return;
}
LOG4CXX_INFO(logger, "Message from admin received");
message->setTo(message->getFrom());
message->setFrom(m_component->getJID());
if (message->getBody() == "status") {
int users = m_userManager->getUserCount();
int backends = m_server->getBackendCount() - 1;
message->setBody("Running (" + boost::lexical_cast<std::string>(users) + " users connected using " + boost::lexical_cast<std::string>(backends) + " backends)");
}
else if (message->getBody() == "online_users_count") {
int users = m_userManager->getUserCount();
message->setBody(boost::lexical_cast<std::string>(users));
}
else if (message->getBody() == "backends_count") {
int backends = m_server->getBackendCount() - 1;
message->setBody(boost::lexical_cast<std::string>(backends));
}
else if (message->getBody().find("has_online_user") == 0) {
User *user = m_userManager->getUser(getArg(message->getBody()));
std::cout << getArg(message->getBody()) << "\n";
message->setBody(boost::lexical_cast<std::string>(user != NULL));
}
else if (message->getBody().find("online_users") == 0) {
std::string lst;
const std::map<std::string, User *> &users = m_userManager->getUsers();
if (users.size() == 0)
lst = "0";
for (std::map<std::string, User *>::const_iterator it = users.begin(); it != users.end(); it ++) {
lst += (*it).first + "\n";
}
message->setBody(lst);
}
else if (message->getBody().find("help") == 0) {
std::string help;
help += "status - shows instance status\n";
help += "online_users - returns list of all online users\n";
help += "online_users_count - number of online users\n";
help += "has_online_user <bare_JID> - returns 1 if user is online\n";
help += "backends_count - number of active backends\n";
message->setBody(help);
}
else {
message->setBody("Unknown command. Try \"help\"");
}
m_component->getStanzaChannel()->sendMessage(message);
}
}
|