Better AdminInterface

This commit is contained in:
HanzZ 2011-06-29 19:25:57 +02:00
parent 9477e3f838
commit 34207065ce
3 changed files with 51 additions and 1 deletions

View file

@ -52,6 +52,10 @@ class UserManager {
/// \return User class associated with this user
User *getUser(const std::string &barejid);
const std::map<std::string, User *> &getUsers() {
return m_users;
}
/// Returns number of online users.
/// \return number of online users
int getUserCount();

View file

@ -35,6 +35,14 @@ 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;
@ -65,8 +73,42 @@ void AdminInterface::handleMessageReceived(Swift::Message::ref message) {
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");
message->setBody("Unknown command. Try \"help\"");
}
m_component->getStanzaChannel()->sendMessage(message);

View file

@ -98,6 +98,10 @@ void UserManager::handlePresence(Swift::Presence::ref presence) {
User *user = getUser(userkey);
if (!user) {
if (CONFIG_STRING(m_component->getConfig(), "service.admin_username") == presence->getFrom().getNode()) {
return;
}
// No user and unavailable presence -> answer with unavailable
if (presence->getType() == Swift::Presence::Unavailable) {
Swift::Presence::ref response = Swift::Presence::create();