Added 'register' and 'unregister' command to AdminInterface

This commit is contained in:
Jan Kaluza 2012-07-18 13:05:50 +02:00
parent ff62fff115
commit 981513b2a5
3 changed files with 51 additions and 3 deletions

View file

@ -30,10 +30,11 @@ 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();
@ -44,6 +45,7 @@ class AdminInterface {
StorageBackend *m_storageBackend;
UserManager *m_userManager;
NetworkPluginServer *m_server;
UserRegistration *m_userRegistration;
};
}

View file

@ -309,7 +309,7 @@ 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);
StatsResponder statsResponder(&transport, &userManager, &plugin, storageBackend);
statsResponder.start();

View file

@ -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 <boost/foreach.hpp>
@ -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));
}
@ -267,6 +269,46 @@ void AdminInterface::handleMessageReceived(Swift::Message::ref message) {
int msgCount = m_userManager->getMessagesToXMPP();
message->setBody(boost::lexical_cast<std::string>(msgCount));
}
else if (message->getBody().find("register ") == 0 && m_userRegistration) {
std::string body = message->getBody();
std::vector<std::string> 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<std::string> 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 +319,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 <bare_JID> - returns 1 if user is online\n";
if (m_userRegistration) {
help += " register <bare_JID> <legacyName> <password> - registers the new user\n";
help += " unregister <bare_JID> - 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";