Changeset - 6e1af765cdc0
[Not reviewed]
0 6 0
Jan Kaluza - 9 years ago 2016-03-06 14:50:05
jkaluza@redhat.com
AdminInterface: Support labels for commands
6 files changed with 35 insertions and 17 deletions:
0 comments (0 inline, 0 general)
include/transport/AdminInterfaceCommand.h
Show inline comments
 
@@ -24,106 +24,111 @@
 
#include <map>
 

	
 
#include "Swiften/Elements/Message.h"
 
#include "transport/StorageBackend.h"
 

	
 
namespace Transport {
 

	
 
class User;
 

	
 
class AdminInterfaceCommand {
 
	public:
 
		typedef enum {
 
			GlobalContext,
 
			UserContext
 
		} Context;
 

	
 
		typedef enum {
 
			None = 0,
 
			Get = 1,
 
			Set = 2,
 
			Execute = 4
 
		} Actions;
 

	
 
		typedef enum {
 
			AdminMode,
 
			UserMode
 
		} AccessMode;
 

	
 
		typedef enum {
 
			General,
 
			Users,
 
			Messages,
 
			Frontend,
 
			Backends,
 
			Memory
 
		} Category;
 

	
 
		class Arg {
 
			public:
 
				Arg(const std::string &_name, const std::string &_label, const std::string &_example) :
 
					name(_name), label(_label), example(_example) {}
 
				~Arg() {}
 

	
 
				std::string name;
 
				std::string label;
 
				std::string example;
 
		};
 

	
 
		AdminInterfaceCommand(const std::string &name, Category category, Context context, AccessMode accessMode, Actions actions);
 
		AdminInterfaceCommand(const std::string &name, Category category, Context context, AccessMode accessMode, Actions actions, const std::string &label = "");
 

	
 
		virtual ~AdminInterfaceCommand() { }
 

	
 
		void setDescription(const std::string &desc) {
 
			m_desc = desc;
 
		}
 

	
 
		const std::string &getDescription() {
 
			return m_desc;
 
		}
 

	
 
		const std::string &getName() {
 
			return m_name;
 
		}
 

	
 
		Actions getActions() {
 
			return m_actions;
 
		}
 

	
 
		Category getCategory() {
 
			return m_category;
 
		}
 

	
 
		const std::string getCategoryName(Category category);
 

	
 
		Context getContext() {
 
			return m_context;
 
		}
 

	
 
		AccessMode getAccessMode() {
 
			return m_accessMode;
 
		}
 

	
 
		void addArg(const std::string &name, const std::string &label, const std::string &example = "") {
 
			Arg arg(name, label, example);
 
			m_args.push_back(arg);
 
		}
 

	
 
		const std::list<Arg> &getArgs() {
 
			return m_args;
 
		}
 

	
 
		const std::string &getLabel() {
 
			return m_label;
 
		}
 

	
 
		virtual std::string handleSetRequest(UserInfo &uinfo, User *user, std::vector<std::string> &args);
 
		virtual std::string handleGetRequest(UserInfo &uinfo, User *user, std::vector<std::string> &args);
 
		virtual std::string handleExecuteRequest(UserInfo &uinfo, User *user, std::vector<std::string> &args);
 

	
 
	private:
 
		std::string m_name;
 
		Category m_category;
 
		Context m_context;
 
		AccessMode m_accessMode;
 
		Actions m_actions;
 
		std::string m_desc;
 
		std::list<Arg> m_args;
 
		std::string m_label;
 
};
 

	
 
}
libtransport/AdminInterface.cpp
Show inline comments
 
@@ -202,128 +202,129 @@ class OnlineUsersPerBackendCommand : public AdminInterfaceCommand {
 
						User *user = *u;
 
						lst += "   " + user->getJID().toBare().toString();
 
						lst += " - non-active for " + boost::lexical_cast<std::string>(now - user->getLastActivity()) + " seconds";
 
						lst += "\n";
 
					}
 
				}
 
				id++;
 
			}
 
		}
 

	
 
	private:
 
		NetworkPluginServer *m_server;
 
};
 

	
 
class BackendsCountCommand : public AdminInterfaceCommand {
 
	public:
 
		
 
		BackendsCountCommand(NetworkPluginServer *server) :
 
												AdminInterfaceCommand("backends_count",
 
												AdminInterfaceCommand::Backends,
 
												AdminInterfaceCommand::GlobalContext,
 
												AdminInterfaceCommand::AdminMode,
 
												AdminInterfaceCommand::Get) {
 
			m_server = server;
 
			setDescription("Number of active backends");
 
		}
 

	
 
		virtual std::string handleGetRequest(UserInfo &uinfo, User *user, std::vector<std::string> &args) {
 
			std::string ret = AdminInterfaceCommand::handleGetRequest(uinfo, user, args);
 
			if (!ret.empty()) {
 
				return ret;
 
			}
 

	
 
			int backends = m_server->getBackendCount();
 
			return boost::lexical_cast<std::string>(backends);
 
		}
 

	
 
	private:
 
		NetworkPluginServer *m_server;
 
};
 

	
 
class ReloadCommand : public AdminInterfaceCommand {
 
	public:
 
		
 
		ReloadCommand(Component *component) : AdminInterfaceCommand("reload",
 
							AdminInterfaceCommand::General,
 
							AdminInterfaceCommand::GlobalContext,
 
							AdminInterfaceCommand::AdminMode,
 
							AdminInterfaceCommand::Execute) {
 
							AdminInterfaceCommand::Execute,
 
							"Reload Spectrum 2 configuration") {
 
			m_component = component;
 
			setDescription("Reloads config file");
 
		}
 

	
 
		virtual std::string handleExecuteRequest(UserInfo &uinfo, User *user, std::vector<std::string> &args) {
 
			std::string ret = AdminInterfaceCommand::handleExecuteRequest(uinfo, user, args);
 
			if (!ret.empty()) {
 
				return ret;
 
			}
 

	
 
			bool done = m_component->getConfig()->reload();
 
			if (done) {
 
				return "Config reloaded";
 
			}
 
			else {
 
				return "Error: Error during config reload";
 
			}
 
		}
 

	
 
	private:
 
		Component *m_component;
 
};
 

	
 
class HasOnlineUserCommand : public AdminInterfaceCommand {
 
	public:
 
		
 
		HasOnlineUserCommand(UserManager *userManager) : AdminInterfaceCommand("has_online_user",
 
							AdminInterfaceCommand::Users,
 
							AdminInterfaceCommand::GlobalContext,
 
							AdminInterfaceCommand::AdminMode,
 
							AdminInterfaceCommand::Execute) {
 
							AdminInterfaceCommand::Execute, "Has online user") {
 
			m_userManager = userManager;
 
			setDescription("Returns 1 if user is online");
 
			addArg("username", "Username", "user@domain.tld");
 
		}
 

	
 
		virtual std::string handleExecuteRequest(UserInfo &uinfo, User *user, std::vector<std::string> &args) {
 
			std::string ret = AdminInterfaceCommand::handleExecuteRequest(uinfo, user, args);
 
			if (!ret.empty()) {
 
				return ret;
 
			}
 

	
 
			if (args.empty()) {
 
				return "Error: Missing user name as an argument";
 
			}
 

	
 
			user = m_userManager->getUser(args[0]);
 
			return boost::lexical_cast<std::string>(user != NULL);
 
		}
 

	
 
	private:
 
		UserManager *m_userManager;
 
};
 

	
 
class ResMemoryCommand : public AdminInterfaceCommand {
 
	public:
 
		
 
		ResMemoryCommand(NetworkPluginServer *server) :
 
												AdminInterfaceCommand("res_memory",
 
												AdminInterfaceCommand::Memory,
 
												AdminInterfaceCommand::GlobalContext,
 
												AdminInterfaceCommand::AdminMode,
 
												AdminInterfaceCommand::Get) {
 
			m_server = server;
 
			setDescription("Total RESident memory Spectrum 2 and its backends use in KB");
 
		}
 

	
 
		virtual std::string handleGetRequest(UserInfo &uinfo, User *user, std::vector<std::string> &args) {
 
			std::string ret = AdminInterfaceCommand::handleGetRequest(uinfo, user, args);
 
			if (!ret.empty()) {
 
				return ret;
 
			}
 

	
 
			double shared = 0;
 
			double rss = 0;
 
			process_mem_usage(shared, rss);
 
			const std::list <NetworkPluginServer::Backend *> &backends = m_server->getBackends();
 
			BOOST_FOREACH(NetworkPluginServer::Backend * backend, backends) {
 
				rss += backend->res;
 
@@ -658,155 +659,157 @@ class MessagesFromXMPPCommand : public AdminInterfaceCommand {
 
		}
 

	
 
		virtual std::string handleGetRequest(UserInfo &uinfo, User *user, std::vector<std::string> &args) {
 
			std::string ret = AdminInterfaceCommand::handleGetRequest(uinfo, user, args);
 
			if (!ret.empty()) {
 
				return ret;
 
			}
 

	
 
			int msgCount = m_userManager->getMessagesToBackend();
 
			return boost::lexical_cast<std::string>(msgCount);
 
		}
 

	
 
	private:
 
		UserManager *m_userManager;
 
};
 

	
 
class MessagesToXMPPCommand : public AdminInterfaceCommand {
 
	public:
 
		
 
		MessagesToXMPPCommand(UserManager *userManager) : AdminInterfaceCommand("messages_to_xmpp",
 
							AdminInterfaceCommand::Messages,
 
							AdminInterfaceCommand::GlobalContext,
 
							AdminInterfaceCommand::AdminMode,
 
							AdminInterfaceCommand::Get) {
 
			m_userManager = userManager;
 
			setDescription("Returns number of messages sent to Front network");
 
		}
 

	
 
		virtual std::string handleGetRequest(UserInfo &uinfo, User *user, std::vector<std::string> &args) {
 
			std::string ret = AdminInterfaceCommand::handleGetRequest(uinfo, user, args);
 
			if (!ret.empty()) {
 
				return ret;
 
			}
 

	
 
			int msgCount = m_userManager->getMessagesToXMPP();
 
			return boost::lexical_cast<std::string>(msgCount);
 
		}
 

	
 
	private:
 
		UserManager *m_userManager;
 
};
 

	
 
class RegisterCommand : public AdminInterfaceCommand {
 
	public:
 
		RegisterCommand(UserRegistration *userRegistration, Component *component) : AdminInterfaceCommand("register",
 
							AdminInterfaceCommand::Users,
 
							AdminInterfaceCommand::GlobalContext,
 
							AdminInterfaceCommand::UserMode,
 
							AdminInterfaceCommand::Execute) {
 
							AdminInterfaceCommand::Execute,
 
							"Register") {
 
			m_userRegistration = userRegistration;
 
			setDescription("Registers the new user");
 

	
 
			std::string fields = component->getFrontend()->getRegistrationFields();
 
			std::vector<std::string> args;
 
			boost::split(args, fields, boost::is_any_of("\n"));
 
			addArg("username", args[0]);
 
			if (fields.size() > 1) {
 
				addArg("legacy_username", args[1]);
 
			}
 
			if (fields.size() > 2) {
 
				addArg("legacy_password", args[2]);
 
			}
 
		}
 

	
 
		virtual std::string handleExecuteRequest(UserInfo &uinfo, User *user, std::vector<std::string> &args) {
 
			std::string ret = AdminInterfaceCommand::handleExecuteRequest(uinfo, user, args);
 
			if (!ret.empty()) {
 
				return ret;
 
			}
 

	
 
			if (args.size() != 2 && args.size() != 3) {
 
				return "Error: Bad argument count";
 
			}
 

	
 
			UserInfo res;
 
			res.jid = args[0];
 
			res.uin = args[1];
 
			if (args.size() == 2) {
 
				res.password = "";
 
			}
 
			else {
 
				res.password = args[2];
 
			}
 
			res.language = "en";
 
			res.encoding = "utf-8";
 
			res.vip = 0;
 

	
 
			if (m_userRegistration->registerUser(res)) {
 
				return "User registered";
 
			}
 
			else {
 
				return "Error: User is already registered";
 
			}
 
		}
 

	
 
	private:
 
		UserRegistration *m_userRegistration;
 
};
 

	
 
class UnregisterCommand : public AdminInterfaceCommand {
 
	public:
 
		
 
		UnregisterCommand(UserRegistration *userRegistration, Component *component) : AdminInterfaceCommand("unregister",
 
							AdminInterfaceCommand::Users,
 
							AdminInterfaceCommand::UserContext,
 
							AdminInterfaceCommand::UserMode,
 
							AdminInterfaceCommand::Execute) {
 
							AdminInterfaceCommand::Execute,
 
							"Unregister") {
 
			m_userRegistration = userRegistration;
 
			setDescription("Unregisters existing user");
 

	
 
// 			std::string fields = component->getFrontend()->getRegistrationFields();
 
// 			std::vector<std::string> args;
 
// 			boost::split(args, fields, boost::is_any_of("\n"));
 
// 			addArg("username", args[0]);
 
		}
 

	
 
		virtual std::string handleExecuteRequest(UserInfo &uinfo, User *user, std::vector<std::string> &args) {
 
			std::string ret = AdminInterfaceCommand::handleExecuteRequest(uinfo, user, args);
 
			if (!ret.empty()) {
 
				return ret;
 
			}
 

	
 
			if (m_userRegistration->unregisterUser(uinfo.jid)) {
 
				return "User '" + args[0] + "' unregistered.";
 
			}
 
			else {
 
				return "Error: User '" + args[0] + "' is not registered";
 
			}
 
		}
 

	
 
	private:
 
		UserRegistration *m_userRegistration;
 
};
 

	
 
class SetOAuth2CodeCommand : public AdminInterfaceCommand {
 
	public:
 
		
 
		SetOAuth2CodeCommand(Component *component) : AdminInterfaceCommand("set_oauth2_code",
 
							AdminInterfaceCommand::Frontend,
 
							AdminInterfaceCommand::GlobalContext,
 
							AdminInterfaceCommand::AdminMode,
 
							AdminInterfaceCommand::Execute) {
 
			m_component = component;
 
			setDescription("set_oauth2_code <code> <state> - sets the OAuth2 code and state for this instance");
 
		}
 

	
 
		virtual std::string handleExecuteRequest(UserInfo &uinfo, User *user, std::vector<std::string> &args) {
 
			std::string ret = AdminInterfaceCommand::handleExecuteRequest(uinfo, user, args);
 
			if (!ret.empty()) {
 
				return ret;
 
			}
 

	
 
			if (args.size() != 2) {
 
				return "Error: Bad argument count";
 
			}
 
@@ -821,271 +824,276 @@ class SetOAuth2CodeCommand : public AdminInterfaceCommand {
 

	
 
	private:
 
		Component *m_component;
 
};
 

	
 
class GetOAuth2URLCommand : public AdminInterfaceCommand {
 
	public:
 
		
 
		GetOAuth2URLCommand(Component *component) : AdminInterfaceCommand("get_oauth2_url",
 
							AdminInterfaceCommand::Frontend,
 
							AdminInterfaceCommand::GlobalContext,
 
							AdminInterfaceCommand::AdminMode,
 
							AdminInterfaceCommand::Execute) {
 
			m_component = component;
 
			setDescription("get_oauth2_code - Get OAUth2 URL");
 
			std::string fields = component->getFrontend()->getRegistrationFields();
 
			std::vector<std::string> args;
 
			boost::split(args, fields, boost::is_any_of("\n"));
 
			addArg("username", args[0]);
 
			if (fields.size() > 1) {
 
				addArg("legacy_username", args[1]);
 
			}
 
			if (fields.size() > 2) {
 
				addArg("legacy_password", args[2]);
 
			}
 
		}
 

	
 
		virtual std::string handleExecuteRequest(UserInfo &uinfo, User *user, std::vector<std::string> &args) {
 
			std::string ret = AdminInterfaceCommand::handleExecuteRequest(uinfo, user, args);
 
			if (!ret.empty()) {
 
				return ret;
 
			}
 

	
 
			std::string url = m_component->getFrontend()->getOAuth2URL(args);
 
			return url;
 
		}
 

	
 
	private:
 
		Component *m_component;
 
};
 

	
 
class HelpCommand : public AdminInterfaceCommand {
 
	public:
 
		
 
		HelpCommand(std::map<std::string, AdminInterfaceCommand *> *commands) : AdminInterfaceCommand("help",
 
							AdminInterfaceCommand::General,
 
							AdminInterfaceCommand::GlobalContext,
 
							AdminInterfaceCommand::AdminMode,
 
							AdminInterfaceCommand::Execute) {
 
							AdminInterfaceCommand::Execute,
 
							"Help") {
 
			m_commands = commands;
 
			setDescription("Shows help message");
 
		}
 

	
 
		void generateCategory(AdminInterfaceCommand::Category category, std::string &output) {
 
			output += getCategoryName(category) + ":\n";
 

	
 
			for (std::map<std::string, AdminInterfaceCommand *>::iterator it = m_commands->begin(); it != m_commands->end(); it++) {
 
				AdminInterfaceCommand *command = it->second;
 
				if (command->getCategory() != category) {
 
					continue;
 
				}
 

	
 
				if (command->getActions() & AdminInterfaceCommand::Execute) {
 
					output += "   CMD   ";
 
				}
 
				else {
 
					output += "   VAR   ";
 
				}
 

	
 
				output += command->getName() + " - ";
 

	
 
				output += command->getDescription() + "\n";
 
			}
 
		}
 

	
 
		virtual std::string handleExecuteRequest(UserInfo &uinfo, User *user, std::vector<std::string> &args) {
 
			std::string ret = AdminInterfaceCommand::handleExecuteRequest(uinfo, user, args);
 
			if (!ret.empty()) {
 
				return ret;
 
			}
 

	
 
			std::string help;
 
			generateCategory(AdminInterfaceCommand::General, help);
 
			generateCategory(AdminInterfaceCommand::Users, help);
 
			generateCategory(AdminInterfaceCommand::Messages, help);
 
			generateCategory(AdminInterfaceCommand::Frontend, help);
 
			generateCategory(AdminInterfaceCommand::Backends, help);
 
			generateCategory(AdminInterfaceCommand::Memory, help);
 
			return help;
 
		}
 

	
 
	private:
 
		std::map<std::string, AdminInterfaceCommand *> *m_commands;
 
};
 

	
 
class CommandsCommand : public AdminInterfaceCommand {
 
	public:
 
		
 
		CommandsCommand(std::map<std::string, AdminInterfaceCommand *> *commands) : AdminInterfaceCommand("commands",
 
							AdminInterfaceCommand::General,
 
							AdminInterfaceCommand::GlobalContext,
 
							AdminInterfaceCommand::AdminMode,
 
							AdminInterfaceCommand::Execute) {
 
							AdminInterfaceCommand::Execute,
 
							"Available commands") {
 
			m_commands = commands;
 
			setDescription("Shows all the available commands with extended information.");
 
		}
 

	
 
		virtual std::string handleExecuteRequest(UserInfo &uinfo, User *user, std::vector<std::string> &args) {
 
			std::string ret = AdminInterfaceCommand::handleExecuteRequest(uinfo, user, args);
 
			if (!ret.empty()) {
 
				return ret;
 
			}
 

	
 
			std::string output;
 
			for (std::map<std::string, AdminInterfaceCommand *>::iterator it = m_commands->begin(); it != m_commands->end(); it++) {
 
				AdminInterfaceCommand *command = it->second;
 
				if ((command->getActions() & AdminInterfaceCommand::Execute) == 0) {
 
					continue;
 
				}
 

	
 
				output += command->getName();
 
				output += " - \"" + command->getDescription() + "\"";
 
				output += " Category: " + command->getCategoryName(command->getCategory());
 

	
 
				output += " AccesMode:";
 
				if (command->getAccessMode() == AdminInterfaceCommand::UserMode) {
 
					output += " User";
 
				}
 
				else {
 
					output += " Admin";
 
				}
 

	
 
				output += " Context:";
 
				if (command->getContext() == AdminInterfaceCommand::UserContext) {
 
					output += " User";
 
				}
 
				else {
 
					output += " Global";
 
				}
 

	
 
				output += " Label: \"" + (command->getLabel().empty() ? command->getName() : command->getLabel()) + "\"";
 

	
 
				output += "\n";
 
			}
 

	
 
			return output;
 
		}
 

	
 
	private:
 
		std::map<std::string, AdminInterfaceCommand *> *m_commands;
 
};
 

	
 

	
 
class VariablesCommand : public AdminInterfaceCommand {
 
	public:
 
		
 
		VariablesCommand(std::map<std::string, AdminInterfaceCommand *> *commands) : AdminInterfaceCommand("variables",
 
							AdminInterfaceCommand::General,
 
							AdminInterfaceCommand::GlobalContext,
 
							AdminInterfaceCommand::AdminMode,
 
							AdminInterfaceCommand::Execute) {
 
							AdminInterfaceCommand::Execute,
 
							"Available variables") {
 
			m_commands = commands;
 
			setDescription("Shows all the available variables.");
 
		}
 

	
 
		virtual std::string handleExecuteRequest(UserInfo &uinfo, User *user, std::vector<std::string> &args) {
 
			std::string ret = AdminInterfaceCommand::handleExecuteRequest(uinfo, user, args);
 
			if (!ret.empty()) {
 
				return ret;
 
			}
 

	
 
			std::string output;
 
			for (std::map<std::string, AdminInterfaceCommand *>::iterator it = m_commands->begin(); it != m_commands->end(); it++) {
 
				AdminInterfaceCommand *command = it->second;
 
				if ((command->getActions() & AdminInterfaceCommand::Get) == 0) {
 
					continue;
 
				}
 

	
 
				output += command->getName();
 
				output += " - \"" + command->getDescription() + "\"";
 
				output += " Value: \"" + command->handleGetRequest(uinfo, user, args) + "\"";
 

	
 
				if ((command->getActions() & AdminInterfaceCommand::Set) == 0) {
 
					output += " Read-only: true";
 
				}
 
				else {
 
					output += " Read-only: false";
 
				}
 

	
 
				output += " Category: " + command->getCategoryName(command->getCategory());
 

	
 
				output += " AccesMode:";
 
				if (command->getAccessMode() == AdminInterfaceCommand::UserMode) {
 
					output += " User";
 
				}
 
				else {
 
					output += " Admin";
 
				}
 

	
 
				output += " Context:";
 
				if (command->getContext() == AdminInterfaceCommand::UserContext) {
 
					output += " User";
 
				}
 
				else {
 
					output += " Global";
 
				}
 

	
 
				output += "\n";
 
			}
 

	
 
			return output;
 
		}
 

	
 
	private:
 
		std::map<std::string, AdminInterfaceCommand *> *m_commands;
 
};
 

	
 
class ArgsCommand : public AdminInterfaceCommand {
 
	public:
 
		
 
		ArgsCommand(std::map<std::string, AdminInterfaceCommand *> *commands) : AdminInterfaceCommand("args",
 
							AdminInterfaceCommand::General,
 
							AdminInterfaceCommand::GlobalContext,
 
							AdminInterfaceCommand::AdminMode,
 
							AdminInterfaceCommand::Execute) {
 
							AdminInterfaceCommand::Execute, "Command's arguments") {
 
			m_commands = commands;
 
			setDescription("Shows descripton of arguments for command");
 
			addArg("command", "Command", "register");
 
		}
 

	
 
		virtual std::string handleExecuteRequest(UserInfo &uinfo, User *user, std::vector<std::string> &args) {
 
			std::string ret = AdminInterfaceCommand::handleExecuteRequest(uinfo, user, args);
 
			if (!ret.empty()) {
 
				return ret;
 
			}
 

	
 
			std::map<std::string, AdminInterfaceCommand *>::iterator it = m_commands->find(args[0]);
 
			if (it == m_commands->end()) {
 
				return "Error: Unknown command passed as an argument.";
 
			}
 
			AdminInterfaceCommand *command = it->second;
 

	
 
			BOOST_FOREACH(const AdminInterfaceCommand::Arg &arg, command->getArgs()) {
 
				ret += arg.name + " - \"" + arg.label + "\" " + "Example: \"" + arg.example + "\"\n";
 
			}
 

	
 
			return ret;
 
		}
 

	
 
	private:
 
		std::map<std::string, AdminInterfaceCommand *> *m_commands;
 
};
 

	
 

	
 
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->getFrontend()->onMessageReceived.connect(bind(&AdminInterface::handleMessageReceived, this, _1));
 

	
 
	addCommand(new StatusCommand(m_server, m_userManager));
 
	addCommand(new UptimeCommand());
 
	addCommand(new OnlineUsersCommand(m_userManager));
 
	addCommand(new OnlineUsersCountCommand(m_userManager));
 
	addCommand(new ReloadCommand(m_component));
 
	addCommand(new OnlineUsersPerBackendCommand(m_server));
 
	addCommand(new HasOnlineUserCommand(m_userManager));
 
	addCommand(new BackendsCountCommand(m_server));
 
	addCommand(new ResMemoryCommand(m_server));
 
	addCommand(new ShrMemoryCommand(m_server));
libtransport/AdminInterfaceCommand.cpp
Show inline comments
 
/**
 
 * 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/AdminInterfaceCommand.h"
 
#include "transport/User.h"
 

	
 
#include <boost/foreach.hpp>
 
#include <boost/lexical_cast.hpp>
 
#include <iostream>
 

	
 
#include <Swiften/Version.h>
 
#define HAVE_SWIFTEN_3  (SWIFTEN_VERSION >= 0x030000)
 

	
 
namespace Transport {
 

	
 
AdminInterfaceCommand::AdminInterfaceCommand(const std::string &name, Category category, Context context, AccessMode accessMode, Actions actions) {
 
AdminInterfaceCommand::AdminInterfaceCommand(const std::string &name, Category category, Context context, AccessMode accessMode, Actions actions, const std::string &label) {
 
	m_name = name;
 
	m_category = category;
 
	m_context = context;
 
	m_accessMode = accessMode;
 
	m_actions = actions;
 
	m_label = label;
 
}
 

	
 
const std::string AdminInterfaceCommand::getCategoryName(Category category) {
 
	switch (category) {
 
		case AdminInterfaceCommand::General:
 
			return "General";
 
		case AdminInterfaceCommand::Users:
 
			return "Users";
 
		case AdminInterfaceCommand::Messages:
 
			return "Messages";
 
		case AdminInterfaceCommand::Frontend:
 
			return "Frontend";
 
		case AdminInterfaceCommand::Backends:
 
			return "Backends";
 
		case AdminInterfaceCommand::Memory:
 
			return "Memory";
 
		default:
 
			return "Unknown";
 
	}
 
}
 

	
 
std::string AdminInterfaceCommand::handleSetRequest(UserInfo &uinfo, User *user, std::vector<std::string> &args) {
 
	if ((m_actions & Set) == 0) {
 
		return "Error: This variable cannot be set.";
 
	}
 

	
 
	if (user && (m_accessMode & AdminMode) != 0) {
 
		return "Error: You do not have rights to set this variable.";
 
	}
 

	
 
	if ((!user && uinfo.id == -1) && (m_context & UserContext)) {
 
		return "Error: This variable can be set only in user context.";
 
	}
 

	
 
	if (args.empty()) {
 
		return "Error: Value is missing.";
 
	}
 

	
 
	return "";
 
}
 

	
 
std::string AdminInterfaceCommand::handleGetRequest(UserInfo &uinfo, User *user, std::vector<std::string> &args) {
 
	if ((m_actions & Get) == 0) {
 
		return "Error: This variable cannot be get.";
 
	}
 

	
 
	if (user && (m_accessMode & AdminMode) != 0) {
 
		return "Error: You do not have rights to get this variable.";
spectrum/src/frontends/slack/SlackUserManager.cpp
Show inline comments
 
@@ -4,188 +4,191 @@
 
 * 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 "SlackUserManager.h"
 
#include "SlackUserRegistration.h"
 
#include "SlackFrontend.h"
 
#include "SlackSession.h"
 
#include "SlackUser.h"
 

	
 
#include "transport/User.h"
 
#include "transport/Transport.h"
 
#include "transport/StorageBackend.h"
 
#include "transport/Logging.h"
 
#include "transport/Config.h"
 
#include "transport/AdminInterface.h"
 
#include "transport/AdminInterfaceCommand.h"
 

	
 
#include <boost/algorithm/string.hpp>
 
#include <boost/foreach.hpp>
 

	
 
#include <Swiften/Version.h>
 
#define HAVE_SWIFTEN_3  (SWIFTEN_VERSION >= 0x030000)
 

	
 
namespace Transport {
 

	
 
DEFINE_LOGGER(logger, "SlackUserManager");
 

	
 
class ListRoomsCommand : public AdminInterfaceCommand {
 
	public:
 
		
 
		ListRoomsCommand(StorageBackend *storageBackend) : AdminInterfaceCommand("list_rooms",
 
							AdminInterfaceCommand::Frontend,
 
							AdminInterfaceCommand::UserContext,
 
							AdminInterfaceCommand::UserMode,
 
							AdminInterfaceCommand::Execute) {
 
							AdminInterfaceCommand::Execute,
 
							"List joined 3rd-party network rooms") {
 
			m_storageBackend = storageBackend;
 
			setDescription("List connected rooms");
 
		}
 

	
 
		virtual std::string handleExecuteRequest(UserInfo &uinfo, User *user, std::vector<std::string> &args) {
 
			std::string ret = AdminInterfaceCommand::handleExecuteRequest(uinfo, user, args);
 
			if (!ret.empty()) {
 
				return ret;
 
			}
 

	
 
			if (uinfo.id == -1) {
 
				return "Error: Unknown user";
 
			}
 

	
 
			std::string rooms = "";
 
			int type = (int) TYPE_STRING;
 
			m_storageBackend->getUserSetting(uinfo.id, "rooms", type, rooms);
 
			return rooms;
 
		}
 

	
 
	private:
 
		StorageBackend *m_storageBackend;
 
};
 

	
 
class JoinRoomCommand : public AdminInterfaceCommand {
 
	public:
 
		
 
		JoinRoomCommand(StorageBackend *storageBackend, Config *cfg) : AdminInterfaceCommand("join_room",
 
							AdminInterfaceCommand::Frontend,
 
							AdminInterfaceCommand::UserContext,
 
							AdminInterfaceCommand::UserMode,
 
							AdminInterfaceCommand::Execute) {
 
							AdminInterfaceCommand::Execute,
 
							"Join 3rd-party network room") {
 
			m_storageBackend = storageBackend;
 
			setDescription("Join the room");
 

	
 
			std::string legacyRoomLabel = CONFIG_STRING_DEFAULTED(cfg, "service.join_room_room_label", "3rd-party room name");
 
			if (legacyRoomLabel[0] == '%') {
 
				legacyRoomLabel[0] = '#';
 
			}
 

	
 
			std::string legacyRoomExample = CONFIG_STRING_DEFAULTED(cfg, "service.join_room_room_example", "3rd-party room name");
 
			if (legacyRoomExample[0] == '%') {
 
				legacyRoomExample[0] = '#';
 
			}
 

	
 
			addArg("nickname",
 
				   CONFIG_STRING_DEFAULTED(cfg, "service.join_room_nickname_label", "Nickname in 3rd-party room"),
 
				   CONFIG_STRING_DEFAULTED(cfg, "service.join_room_nickname_example", "BotNickname"));
 
			addArg("legacy_room", legacyRoomLabel, legacyRoomExample);
 
			addArg("legacy_server",
 
				   CONFIG_STRING_DEFAULTED(cfg, "service.join_room_server_label", "3rd-party server"),
 
				   CONFIG_STRING_DEFAULTED(cfg, "service.join_room_server_example", "3rd.party.server.org"));
 
			addArg("slack_channel", "Slack Chanel", "mychannel");
 
		}
 

	
 
		virtual std::string handleExecuteRequest(UserInfo &uinfo, User *u, std::vector<std::string> &args) {
 
			std::string ret = AdminInterfaceCommand::handleExecuteRequest(uinfo, u, args);
 
			if (!ret.empty()) {
 
				return ret;
 
			}
 

	
 
			if (uinfo.id == -1) {
 
				return "Error: Unknown user";
 
			}
 

	
 
			std::string rooms = "";
 
			int type = (int) TYPE_STRING;
 
			m_storageBackend->getUserSetting(uinfo.id, "rooms", type, rooms);
 
			// 'unknown' is here to stay compatible in args.size() with older version.
 
			rooms += "connected room " + args[0] + " " + args[1] + " " + args[2] + " " + args[3] + "\n";
 
			m_storageBackend->updateUserSetting(uinfo.id, "rooms", rooms);
 

	
 
			SlackUser *user = static_cast<SlackUser *>(u);
 
			if (user) {
 
				user->getSession()->handleJoinMessage("", args, true);
 
			}
 
			return "Joined the room";
 
		}
 

	
 
	private:
 
		StorageBackend *m_storageBackend;
 
};
 

	
 
class LeaveRoomCommand : public AdminInterfaceCommand {
 
	public:
 
		
 
		LeaveRoomCommand(StorageBackend *storageBackend) : AdminInterfaceCommand("leave_room",
 
							AdminInterfaceCommand::Frontend,
 
							AdminInterfaceCommand::UserContext,
 
							AdminInterfaceCommand::UserMode,
 
							AdminInterfaceCommand::Execute) {
 
							AdminInterfaceCommand::Execute,
 
							"Leave 3rd-party network room") {
 
			m_storageBackend = storageBackend;
 
			setDescription("Leave the room");
 

	
 
			addArg("slack_channel", "Slack Chanel", "mychannel");
 
		}
 

	
 
		virtual std::string handleExecuteRequest(UserInfo &uinfo, User *u, std::vector<std::string> &args) {
 
			std::string ret = AdminInterfaceCommand::handleExecuteRequest(uinfo, u, args);
 
			if (!ret.empty()) {
 
				return ret;
 
			}
 

	
 
			if (uinfo.id == -1) {
 
				return "Error: Unknown user";
 
			}
 

	
 
			std::string rooms = "";
 
			int type = (int) TYPE_STRING;
 
			m_storageBackend->getUserSetting(uinfo.id, "rooms", type, rooms);
 

	
 
			std::vector<std::string> commands;
 
			boost::split(commands, rooms, boost::is_any_of("\n"));
 
			rooms = "";
 

	
 
			BOOST_FOREACH(const std::string &command, commands) {
 
				if (command.size() > 5) {
 
					std::vector<std::string> args2;
 
					boost::split(args2, command, boost::is_any_of(" "));
 
					if (args2.size() == 6) {
 
						if (args[0] != args2[5]) {
 
							rooms += command + "\n";
 
						}
 
					}
 
				}
 
			}
 

	
 
			m_storageBackend->updateUserSetting(uinfo.id, "rooms", rooms);
 

	
 
			SlackUser *user = static_cast<SlackUser *>(u);
 
			if (user) {
 
				user->getSession()->leaveRoom(args[0]);
 
			}
 
			return "Left the room";
 
		}
 

	
 
	private:
 
		StorageBackend *m_storageBackend;
 
};
spectrum_manager/src/APIServer.cpp
Show inline comments
 
@@ -265,209 +265,210 @@ void APIServer::serve_instances_register(Server *server, Server::session *sessio
 
// 	boost::split(fields, response, boost::is_any_of("\n"));
 
// 
 
// 	if (fields.empty()) {
 
// 		fields.push_back("Jabber ID");
 
// 		fields.push_back("3rd-party network username");
 
// 		fields.push_back("3rd-party network password");
 
// 	}
 
// 
 
// 	Document json;
 
// 	json.SetObject();
 
// 	json.AddMember("error", 0, json.GetAllocator());
 
// 	json.AddMember("username_label", fields[0].c_str(), json.GetAllocator());
 
// 	json.AddMember("legacy_username_label", fields.size() >= 2 ? fields[1].c_str() : "", json.GetAllocator());
 
// 	json.AddMember("password_label", fields.size() >= 3 ? fields[2].c_str() : "", json.GetAllocator());
 
// 	send_json(conn, json);
 
// }
 

	
 
void APIServer::serve_instances_commands(Server *server, Server::session *session, struct mg_connection *conn, struct http_message *hm) {
 
	std::string uri(hm->uri.p, hm->uri.len);
 
	std::string instance = uri.substr(uri.rfind("/") + 1);
 

	
 
	UserInfo info;
 
	m_storage->getUser(session->user, info);
 

	
 
	std::string username = "";
 
	int type = (int) TYPE_STRING;
 
	m_storage->getUserSetting(info.id, instance, type, username);
 

	
 
	std::string response = server->send_command(instance, "commands");
 

	
 
	std::vector<std::string> commands;
 
	boost::split(commands, response, boost::is_any_of("\n"));
 

	
 
	Document json;
 
	json.SetObject();
 
	json.AddMember("error", 0, json.GetAllocator());
 

	
 
	std::vector<std::vector<std::string> > tmp;
 
	Value cmds(kArrayType);
 
	BOOST_FOREACH(const std::string &command, commands) {
 
		escaped_list_separator<char> els('\\', ' ', '\"');
 
		tokenizer<escaped_list_separator<char> > tok(command, els);
 

	
 
		std::vector<std::string> tokens;
 
		for(tokenizer<escaped_list_separator<char> >::iterator beg=tok.begin(); beg!=tok.end(); ++beg) {
 
			tokens.push_back(*beg);
 
		}
 

	
 
		if (tokens.size() != 9) {
 
		if (tokens.size() != 11) {
 
			continue;
 
		}
 

	
 
		if (!session->admin && tokens[6] == "Admin") {
 
			continue;
 
		}
 

	
 
		// Skip command which needs registered users.
 
		if (!session->admin && username.empty() && tokens[8] == "User") {
 
			continue;
 
		}
 

	
 

	
 
		// Skip 'register' command when user is registered.
 
		if (!session->admin && !username.empty() && tokens[0] == "register") {
 
			continue;
 
		}
 

	
 
		tmp.push_back(tokens);
 

	
 
		Value cmd;
 
		cmd.SetObject();
 
		cmd.AddMember("name", tokens[0].c_str(), json.GetAllocator());
 
		cmd.AddMember("desc", tokens[2].c_str(), json.GetAllocator());
 
		cmd.AddMember("category", tokens[4].c_str(), json.GetAllocator());
 
		cmd.AddMember("context", tokens[8].c_str(), json.GetAllocator());
 
		cmd.AddMember("label", tokens[10].c_str(), json.GetAllocator());
 
		cmds.PushBack(cmd, json.GetAllocator());
 
	}
 

	
 
	json.AddMember("commands", cmds, json.GetAllocator());
 
	send_json(conn, json);
 
}
 

	
 
void APIServer::serve_instances_variables(Server *server, Server::session *session, struct mg_connection *conn, struct http_message *hm) {
 
	std::string uri(hm->uri.p, hm->uri.len);
 
	std::string instance = uri.substr(uri.rfind("/") + 1);
 

	
 
	UserInfo info;
 
	m_storage->getUser(session->user, info);
 

	
 
	std::string username = "";
 
	int type = (int) TYPE_STRING;
 
	m_storage->getUserSetting(info.id, instance, type, username);
 

	
 
	std::string response = server->send_command(instance, "variables");
 

	
 
	std::vector<std::string> commands;
 
	boost::split(commands, response, boost::is_any_of("\n"));
 

	
 
	Document json;
 
	json.SetObject();
 
	json.AddMember("error", 0, json.GetAllocator());
 

	
 
	std::vector<std::vector<std::string> > tmp;
 
	Value cmds(kArrayType);
 
	BOOST_FOREACH(const std::string &command, commands) {
 
		escaped_list_separator<char> els('\\', ' ', '\"');
 
		tokenizer<escaped_list_separator<char> > tok(command, els);
 

	
 
		std::vector<std::string> tokens;
 
		for(tokenizer<escaped_list_separator<char> >::iterator beg=tok.begin(); beg!=tok.end(); ++beg) {
 
			tokens.push_back(*beg);
 
		}
 

	
 
		if (tokens.size() != 13) {
 
			continue;
 
		}
 

	
 
		if (!session->admin && tokens[10] == "Admin") {
 
			continue;
 
		}
 

	
 
		tmp.push_back(tokens);
 

	
 
		Value cmd;
 
		cmd.SetObject();
 
		cmd.AddMember("name", tokens[0].c_str(), json.GetAllocator());
 
		cmd.AddMember("desc", tokens[2].c_str(), json.GetAllocator());
 
		cmd.AddMember("value", tokens[4].c_str(), json.GetAllocator());
 
		cmd.AddMember("read_only", tokens[6].c_str(), json.GetAllocator());
 
		cmd.AddMember("category", tokens[8].c_str(), json.GetAllocator());
 
		cmd.AddMember("context", tokens[12].c_str(), json.GetAllocator());
 
		cmds.PushBack(cmd, json.GetAllocator());
 
	}
 

	
 
	json.AddMember("variables", cmds, json.GetAllocator());
 
	send_json(conn, json);
 
}
 

	
 

	
 
void APIServer::serve_instances_command_args(Server *server, Server::session *session, struct mg_connection *conn, struct http_message *hm) {
 
	std::string uri(hm->uri.p, hm->uri.len);
 
	std::string instance = uri.substr(uri.rfind("/") + 1);
 
	std::string command = get_http_var(hm, "command");
 
	boost::trim(command);
 

	
 
	std::string response = server->send_command(instance, "commands");
 

	
 
	bool found = false;
 
	bool userContext = false;
 
	std::vector<std::string> commands;
 
	boost::split(commands, response, boost::is_any_of("\n"));
 
	BOOST_FOREACH(const std::string &cmd, commands) {
 
		escaped_list_separator<char> els('\\', ' ', '\"');
 
		tokenizer<escaped_list_separator<char> > tok(cmd, els);
 

	
 
		std::vector<std::string> tokens;
 
		for(tokenizer<escaped_list_separator<char> >::iterator beg=tok.begin(); beg!=tok.end(); ++beg) {
 
			tokens.push_back(*beg);
 
		}
 

	
 
		if (tokens.size() != 9) {
 
		if (tokens.size() != 11) {
 
			continue;
 
		}
 

	
 
		std::cout << tokens[0] << " " << command << "\n";
 
		if (tokens[0] != command) {
 
			continue;
 
		}
 

	
 
		if (!session->admin && tokens[6] == "Admin") {
 
			send_ack(conn, false, "Only admin is able to query this command.");
 
			return;
 
		}
 

	
 
		if (tokens[8] == "User") {
 
			userContext = true;
 
		}
 

	
 
		found = true;
 
		break;
 
	}
 

	
 
	if (!found) {
 
		command = "unknown";
 
	}
 

	
 
	response = server->send_command(instance, "args " + command);
 
	if (response.find("Error:") == 0) {
 
		send_ack(conn, false, response);
 
		return;
 
	}
 

	
 
	
 

	
 
	std::vector<std::string> args;
 
	boost::split(args, response, boost::is_any_of("\n"));
 

	
 
	Document json;
 
	json.SetObject();
 
	json.AddMember("error", 0, json.GetAllocator());
 

	
 
	std::vector<std::vector<std::string> > tmp;
 
	Value argList(kArrayType);
 

	
 
	if (userContext && session->admin) {
 
		Value arg;
 
		arg.SetObject();
 
		arg.AddMember("name", "username", json.GetAllocator());
 
		arg.AddMember("label", "Username", json.GetAllocator());
 
@@ -479,97 +480,97 @@ void APIServer::serve_instances_command_args(Server *server, Server::session *se
 
		escaped_list_separator<char> els('\\', ' ', '\"');
 
		tokenizer<escaped_list_separator<char> > tok(argument, els);
 

	
 
		std::vector<std::string> tokens;
 
		for(tokenizer<escaped_list_separator<char> >::iterator beg=tok.begin(); beg!=tok.end(); ++beg) {
 
			tokens.push_back(*beg);
 
		}
 

	
 
		if (tokens.size() != 5) {
 
			continue;
 
		}
 

	
 
		tmp.push_back(tokens);
 

	
 
		Value arg;
 
		arg.SetObject();
 
		arg.AddMember("name", tokens[0].c_str(), json.GetAllocator());
 
		arg.AddMember("label", tokens[2].c_str(), json.GetAllocator());
 
		arg.AddMember("example", tokens[4].c_str(), json.GetAllocator());
 
		argList.PushBack(arg, json.GetAllocator());
 
	}
 

	
 
	json.AddMember("args", argList, json.GetAllocator());
 
	send_json(conn, json);
 
}
 

	
 

	
 
void APIServer::serve_instances_execute(Server *server, Server::session *session, struct mg_connection *conn, struct http_message *hm) {
 
	std::string uri(hm->uri.p, hm->uri.len);
 
	std::string instance = uri.substr(uri.rfind("/") + 1);
 
	std::string command = get_http_var(hm, "command");
 
	boost::trim(command);
 

	
 
	std::string response = server->send_command(instance, "commands");
 

	
 
	bool found = false;
 
	bool userContext = false;
 
	std::vector<std::string> commands;
 
	boost::split(commands, response, boost::is_any_of("\n"));
 
	BOOST_FOREACH(const std::string &cmd, commands) {
 
		escaped_list_separator<char> els('\\', ' ', '\"');
 
		tokenizer<escaped_list_separator<char> > tok(cmd, els);
 

	
 
		std::vector<std::string> tokens;
 
		for(tokenizer<escaped_list_separator<char> >::iterator beg=tok.begin(); beg!=tok.end(); ++beg) {
 
			tokens.push_back(*beg);
 
		}
 

	
 
		if (tokens.size() != 9) {
 
		if (tokens.size() != 11) {
 
			continue;
 
		}
 

	
 
		std::cout << tokens[0] << " " << command << "\n";
 
		if (tokens[0] != command) {
 
			continue;
 
		}
 

	
 
		if (!session->admin && tokens[6] == "Admin") {
 
			send_ack(conn, false, "Only admin is able to execute.");
 
			return;
 
		}
 

	
 
		if (tokens[8] == "User") {
 
			userContext = true;
 
		}
 

	
 
		found = true;
 
		break;
 
	}
 

	
 
	if (!found) {
 
		command = "unknown";
 
	}
 

	
 
	UserInfo info;
 
	m_storage->getUser(session->user, info);
 
	std::string username = "";
 
	int type = (int) TYPE_STRING;
 
	m_storage->getUserSetting(info.id, instance, type, username);
 

	
 
	if (userContext && !session->admin) {
 
		if (username.empty()) {
 
			send_ack(conn, false, "Error: You are not registered to this transport instance.");
 
			return;
 
		}
 

	
 
		command += " " + username;
 
	}
 

	
 
	for (int i = 0; i < 10; ++i) {
 
		std::string var = get_http_var(hm, std::string(std::string("command_arg") + boost::lexical_cast<std::string>(i)).c_str());
 
		if (!var.empty()) {
 
			command += " " + var;
 
		}
 
	}
 

	
 
	response = server->send_command(instance, command);
spectrum_manager/src/html/js/app.js
Show inline comments
 
@@ -200,79 +200,79 @@ function execute_command(instance, command) {
 
							if (command == "unregister") {
 
								var posturl = $.cookie("base_location") + "api/v1/instances/unregister/" + instance;
 
							}
 
							else {
 
								var posturl = $.cookie("base_location") + "api/v1/instances/execute/" + instance + "?command=" + command;
 
							}
 
							var postdata = {}
 
							for (i = 0; i < 10; i++) {
 
								var val = $('#command_arg' + i).val();
 
								if (val) {
 
									postdata["command_arg" + i] = val;
 
								}
 
							}
 
							$.post(posturl, postdata, function(data) {
 
								var dialog = bootbox.dialog({
 
									title: "Command result: " + command + ".",
 
									message: "<pre>" + data.message + "</pre>",
 
									buttons: {
 
										success: {
 
											label: "OK",
 
											className: "btn-success",
 
											callback: function () {
 
												if (command == "unregister") {
 
													location.reload(); 
 
												}
 
											}
 
										}
 
									}
 
								})
 
								dialog.find("div.modal-dialog").addClass("largeWidth");
 
								dialog.find("div.modal-body").addClass("maxHeight");
 
							});
 
						}
 
					}
 
				}
 
			}
 
		})
 
	});
 
}
 

	
 
function show_instance() {
 
	var query = getQueryParams(document.location.search);
 

	
 
	$("#main_content").html("<h2>Instance: " + query.id + "</h2><h4>Available commands:</h4><table id='commands'><tr><th>Name<th>Category</th><th>Description</th></tr></table><h4>Available variables:</h4><table id='variables'><tr><th>Name<th>Value</th><th>Read-only</th><th>Desc</th></tr></table>");
 

	
 
	$.get($.cookie("base_location") + "api/v1/instances/commands/" + query.id, function(data) {
 
		$.each(data.commands, function(i, command) {
 
			var row = '<tr>'
 
			row += '<td><a class="button_command" command="' + command.name + '" instance="' + query.id + '" href="' + $.cookie("base_location") +  'api/v1/instances/command_args/' + query.id + '?command=' + command.name +'">' + command.name + '</a></td>';
 
			row += '<td><a class="button_command" command="' + command.name + '" instance="' + query.id + '" href="' + $.cookie("base_location") +  'api/v1/instances/command_args/' + query.id + '?command=' + command.name +'">' + command.label + '</a></td>';
 
			row += '<td>' + command.category + '</td>';
 
			row += '<td>' + command.desc + '</td>';
 
			row += '</tr>';
 
			$("#commands  > tbody:last-child").append(row);
 
		});
 

	
 
		$(".button_command").click(function(e) {
 
			e.preventDefault();
 

	
 
			var command = $(this).attr('command');
 
			var instance = $(this).attr('instance');
 
			execute_command(instance, command);
 
		})
 
	});
 

	
 
	$.get($.cookie("base_location") + "api/v1/instances/variables/" + query.id, function(data) {
 
		$.each(data.variables, function(i, variable) {
 
			var row = '<tr>'
 
			row += '<td>' + variable.name + '</td>';
 
			row += '<td>' + variable.value + '</td>';
 
			row += '<td>' + variable.read_only + '</td>';
 
			row += '<td>' + variable.desc + '</td>';
 
			row += '</tr>';
 
			$("#variables  > tbody:last-child").append(row);
 
		});
 
	});
 

	
 

	
 
}
 

	
0 comments (0 inline, 0 general)