Changeset - fe69cbd7a13f
[Not reviewed]
0 6 0
Jan Kaluza - 9 years ago 2016-03-06 20:13:52
jkaluza@redhat.com
Web interface: support diplaying tables generated on transport's side
6 files changed with 111 insertions and 28 deletions:
0 comments (0 inline, 0 general)
include/transport/AdminInterfaceCommand.h
Show inline comments
 
@@ -39,93 +39,94 @@ class AdminInterfaceCommand {
 

	
 
		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(const std::string &_name, const std::string &_label, const std::string &_type, const std::string &_example) :
 
					name(_name), label(_label), type(_type), example(_example) {}
 
				~Arg() {}
 

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

	
 
		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);
 
		void addArg(const std::string &name, const std::string &label, const std::string &type = "string", const std::string &example = "") {
 
			Arg arg(name, label, type, 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
 
@@ -91,66 +91,67 @@ class UptimeCommand : public AdminInterfaceCommand {
 
			m_start = time(NULL);
 
			setDescription("Returns ptime in seconds");
 
		}
 

	
 
		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;
 
			}
 

	
 
			return boost::lexical_cast<std::string>(time(0) - m_start);
 
		}
 

	
 
	private:
 
		time_t m_start;
 
};
 

	
 
class OnlineUsersCommand : public AdminInterfaceCommand {
 
	public:
 
		
 
		OnlineUsersCommand(UserManager *userManager) : AdminInterfaceCommand("online_users",
 
							AdminInterfaceCommand::Users,
 
							AdminInterfaceCommand::GlobalContext,
 
							AdminInterfaceCommand::AdminMode,
 
							AdminInterfaceCommand::Get) {
 
							AdminInterfaceCommand::Execute,
 
							"Online users") {
 
			m_userManager = userManager;
 
			setDescription("Returns list of all online users");
 
		}
 

	
 
		virtual std::string handleGetRequest(UserInfo &uinfo, User *user, std::vector<std::string> &args) {
 
			std::string ret = AdminInterfaceCommand::handleGetRequest(uinfo, user, args);
 
		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;
 
			}
 

	
 
			const std::map<std::string, User *> &users = m_userManager->getUsers();
 
			if (users.empty()) {
 
				ret = "0";
 
				ret = "hanzz@njs.netlab.cz \"3rd-party network username:\" \"me\"\n";
 
			}
 

	
 
			for (std::map<std::string, User *>::const_iterator it = users.begin(); it != users.end(); it ++) {
 
				ret += (*it).first + "\n";
 
				ret += (*it).first + " \"3rd-party network username:\" \"" + user->getUserInfo().uin + "\"\n";
 
			}
 
			return ret;
 
		}
 

	
 
	private:
 
		UserManager *m_userManager;
 
};
 

	
 
class OnlineUsersCountCommand : public AdminInterfaceCommand {
 
	public:
 
		
 
		OnlineUsersCountCommand(UserManager *userManager) : AdminInterfaceCommand("online_users_count",
 
							AdminInterfaceCommand::Users,
 
							AdminInterfaceCommand::GlobalContext,
 
							AdminInterfaceCommand::AdminMode,
 
							AdminInterfaceCommand::Get) {
 
			m_userManager = userManager;
 
			setDescription("Number of online users");
 
		}
 

	
 
		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;
 
@@ -261,49 +262,49 @@ class ReloadCommand : public AdminInterfaceCommand {
 

	
 
			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, "Has online user") {
 
			m_userManager = userManager;
 
			setDescription("Returns 1 if user is online");
 
			addArg("username", "Username", "user@domain.tld");
 
			addArg("username", "Username", "string", "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) :
 
@@ -696,49 +697,49 @@ class MessagesToXMPPCommand : public AdminInterfaceCommand {
 

	
 
	private:
 
		UserManager *m_userManager;
 
};
 

	
 
class RegisterCommand : public AdminInterfaceCommand {
 
	public:
 
		RegisterCommand(UserRegistration *userRegistration, Component *component) : AdminInterfaceCommand("register",
 
							AdminInterfaceCommand::Users,
 
							AdminInterfaceCommand::GlobalContext,
 
							AdminInterfaceCommand::UserMode,
 
							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]);
 
				addArg("legacy_password", args[2], "password");
 
			}
 
		}
 

	
 
		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";
 
@@ -823,49 +824,49 @@ 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]);
 
				addArg("legacy_password", args[2], "password");
 
			}
 
		}
 

	
 
		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,
 
@@ -1030,100 +1031,100 @@ class VariablesCommand : public AdminInterfaceCommand {
 
				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, "Command's arguments") {
 
			m_commands = commands;
 
			setDescription("Shows descripton of arguments for command");
 
			addArg("command", "Command", "register");
 
			addArg("command", "Command", "string", "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";
 
				ret += arg.name + " - \"" + arg.label + "\" " + "Example: \"" + arg.example + "\" Type: \"" + arg.type + "\"\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 OnlineUsersPerBackendCommand(m_server));
 
	addCommand(new HasOnlineUserCommand(m_userManager));
 
	addCommand(new BackendsCountCommand(m_server));
 
	addCommand(new ResMemoryCommand(m_server));
 
	addCommand(new ShrMemoryCommand(m_server));
 
	addCommand(new UsedMemoryCommand(m_server));
 
	addCommand(new AverageMemoryPerUserCommand(m_server, m_userManager));
 
	addCommand(new ResMemoryPerBackendCommand(m_server));
 
	addCommand(new ShrMemoryPerBackendCommand(m_server));
 
	addCommand(new UsedMemoryPerBackendCommand(m_server));
 
	addCommand(new AverageMemoryPerUserPerBackendCommand(m_server));
 
// 	addCommand(new ResMemoryPerBackendCommand(m_server));
 
// 	addCommand(new ShrMemoryPerBackendCommand(m_server));
 
// 	addCommand(new UsedMemoryPerBackendCommand(m_server));
 
// 	addCommand(new AverageMemoryPerUserPerBackendCommand(m_server));
 
	addCommand(new CrashedBackendsCountCommand(m_server));
 
	addCommand(new CrashedBackendsCommand(m_server));
 
	addCommand(new MessagesFromXMPPCommand(m_userManager));
 
	addCommand(new MessagesToXMPPCommand(m_userManager));
 
	addCommand(new SetOAuth2CodeCommand(m_component));
 
	addCommand(new GetOAuth2URLCommand(m_component));
 
	addCommand(new HelpCommand(&m_commands));
 
	addCommand(new ArgsCommand(&m_commands));
 
	addCommand(new CommandsCommand(&m_commands));
 
	addCommand(new VariablesCommand(&m_commands));
 

	
 
	if (m_userRegistration) {
 
		addCommand(new RegisterCommand(m_userRegistration, m_component));
 
		addCommand(new UnregisterCommand(m_userRegistration, m_component));
 
	}
 
}
 

	
 
AdminInterface::~AdminInterface() {
 
	for (std::map<std::string, AdminInterfaceCommand *>::iterator it = m_commands.begin(); it != m_commands.end(); it++) {
 
		delete it->second;
 
	}
 
}
 

	
 
void AdminInterface::addCommand(AdminInterfaceCommand *command) {
spectrum/src/frontends/slack/SlackUserManager.cpp
Show inline comments
 
@@ -78,97 +78,99 @@ class ListRoomsCommand : public AdminInterfaceCommand {
 
class JoinRoomCommand : public AdminInterfaceCommand {
 
	public:
 
		
 
		JoinRoomCommand(StorageBackend *storageBackend, Config *cfg) : AdminInterfaceCommand("join_room",
 
							AdminInterfaceCommand::Frontend,
 
							AdminInterfaceCommand::UserContext,
 
							AdminInterfaceCommand::UserMode,
 
							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"),
 
				   "string",
 
				   CONFIG_STRING_DEFAULTED(cfg, "service.join_room_nickname_example", "BotNickname"));
 
			addArg("legacy_room", legacyRoomLabel, legacyRoomExample);
 
			addArg("legacy_room", legacyRoomLabel, "string", legacyRoomExample);
 
			addArg("legacy_server",
 
				   CONFIG_STRING_DEFAULTED(cfg, "service.join_room_server_label", "3rd-party server"),
 
				   "string",
 
				   CONFIG_STRING_DEFAULTED(cfg, "service.join_room_server_example", "3rd.party.server.org"));
 
			addArg("slack_channel", "Slack Chanel", "mychannel");
 
			addArg("slack_channel", "Slack Chanel", "string", "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,
 
							"Leave 3rd-party network room") {
 
			m_storageBackend = storageBackend;
 
			setDescription("Leave the room");
 

	
 
			addArg("slack_channel", "Slack Chanel", "mychannel");
 
			addArg("slack_channel", "Slack Chanel", "string", "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(" "));
spectrum_manager/src/APIServer.cpp
Show inline comments
 
@@ -464,59 +464,60 @@ void APIServer::serve_instances_command_args(Server *server, Server::session *se
 
	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());
 
		arg.AddMember("example", "", json.GetAllocator());
 
		argList.PushBack(arg, json.GetAllocator());
 
	}
 

	
 
	BOOST_FOREACH(const std::string &argument, args) {
 
		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) {
 
		if (tokens.size() != 7) {
 
			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());
 
		arg.AddMember("type", tokens[6].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);
 

	
 
@@ -553,53 +554,106 @@ void APIServer::serve_instances_execute(Server *server, Server::session *session
 

	
 
	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);
 
	boost::replace_all(response, "\n", "<br/>");
 
	if (response.find("Error:") == 0) {
 
		send_ack(conn, false, response);
 
	}
 

	
 
	std::vector<std::string> fields;
 
	boost::split(fields, response, boost::is_any_of("\n"));
 
	if (!fields.empty() && /*fields[0].find(" - ") != std::string::npos &&*/ (fields[0].find(": ") != std::string::npos || fields[0].find(":\"") != std::string::npos)) {
 
		Document json;
 
		json.SetObject();
 
		json.AddMember("error", 0, json.GetAllocator());
 

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

	
 
		BOOST_FOREACH(const std::string &line, fields) {
 
			escaped_list_separator<char> els('\\', ' ', '\"');
 
			tokenizer<escaped_list_separator<char> > tok(line, els);
 

	
 
			Value arg;
 
			arg.SetObject();
 

	
 
			std::string key;
 
			int i = 0;
 
			bool hasDesc = true;
 
			for(tokenizer<escaped_list_separator<char> >::iterator beg=tok.begin(); beg!=tok.end(); ++beg, ++i) {
 
				if (i == 1 && *beg != "-") {
 
					hasDesc = false;
 
				}
 
				if (i == 0) {
 
					tmp.push_back(*beg);
 
					arg.AddMember("Key", tmp.back().c_str(), json.GetAllocator());
 
				}
 
				else if (i == 2 && hasDesc) {
 
					tmp.push_back(*beg);
 
					arg.AddMember("Description", tmp.back().c_str(), json.GetAllocator());
 
				}
 
				else if (i > 1 || (!hasDesc && i > 0)) {
 
					if (key.empty()) {
 
						key = *beg;
 
					}
 
					else {
 
						tmp.push_back(key);
 
						tmp2.push_back(*beg);
 
						arg.AddMember(tmp.back().c_str(), tmp2.back().c_str(), json.GetAllocator());
 
						key = "";
 
					}
 
				}
 
			}
 
			table.PushBack(arg, json.GetAllocator());
 
		}
 

	
 
		json.AddMember("table", table, json.GetAllocator());
 
		json.AddMember("message", response.c_str(), json.GetAllocator());
 
		send_json(conn, json);
 
	}
 
	else {
 
		boost::replace_all(response, "\n", "<br/>");
 
		send_ack(conn, true, response);
 
	}
 
}
 

	
 
void APIServer::serve_users(Server *server, Server::session *session, struct mg_connection *conn, struct http_message *hm) {
 
	ALLOW_ONLY_ADMIN();
 

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

	
 
	std::vector<std::string> list;
 
	m_storage->getUsers(list);
 

	
 
	Value users(kArrayType);
 
	BOOST_FOREACH(std::string &id, list) {
 
		Value user;
 
		user.SetObject();
 
		user.AddMember("username", id.c_str(), json.GetAllocator());
 
		users.PushBack(user, json.GetAllocator());
 
	}
 

	
 
	json.AddMember("users", users, json.GetAllocator());
 
	send_json(conn, json);
spectrum_manager/src/html/js/app.js
Show inline comments
 
@@ -114,49 +114,54 @@ function fill_users_register_form() {
 
			}
 
			else {
 
				var query = getQueryParams(document.location.search);
 
				if (query.back_to_list == "1") {
 
					window.location.replace("list.shtml");
 
				}
 
				else {
 
					window.location.replace("../login/");
 
				}
 
			}
 
		});
 
	})
 
}
 

	
 
function execute_command(instance, command) {
 
	$.get($.cookie("base_location") +  'api/v1/instances/command_args/' + instance + '?command=' + command, function(data) {
 
		var form = '<div class="row">';
 

	
 
		if (data.args.length != 0) {
 
			form += '<div class="col-md-12"><form class="form-horizontal">';
 
			$.each(data.args, function(i, arg) {
 
				form += '<div class="form-group">';
 
				form += '<label class="col-md-4 control-label" for="' + arg.name + '">' + arg.label + ':</label>';
 
				form += '<div class="col-md-4">';
 
				if (arg.type == "password") {
 
					form += '<input id="command_arg' + i + '" name="command_arg' + i + '" type="password" placeholder="' + arg.example + '" class="form-control input-md"/>';
 
				}
 
				else {
 
					form += '<input id="command_arg' + i + '" name="command_arg' + i + '" type="text" placeholder="' + arg.example + '" class="form-control input-md"/>';
 
				}
 
				form += '</div></div>';
 
				console.log('command_arg' + i );
 
			});
 
		}
 
		else {
 
			form += '<div><form class="form-horizontal">';
 
			form += '<div class="form-group">';
 
			form += '<label class="control-label">No arguments needed for this command, you can just execute it.</label>';
 
			form += '</div>';
 
		}
 

	
 
		form += '</form></div></div>'
 

	
 
		bootbox.dialog({
 
			title: "Command execution: " + command + ".",
 
			message: form,
 
			buttons: {
 
				cancel: {
 
					label: "Cancel",
 
					className: "btn-cancel"
 
				},
 
				success: {
 
					label: "Execute",
 
					className: "btn-success",
 
@@ -190,48 +195,68 @@ function execute_command(instance, command) {
 
												}
 
											}
 
										}
 
									})
 
									dialog.find("div.modal-dialog").addClass("largeWidth");
 
									dialog.find("div.modal-body").addClass("maxHeight");
 
								}
 
							});
 
						}
 
						else {
 
							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) {
 
								if (data.table) {
 
									data.message = "<table>";
 
									data.message += "<tr>";
 
									for (var key in data.table[0]) {
 
										if (data.table[0].hasOwnProperty(key)) {
 
											data.message += "<th>" + key + "</th>";
 
										}
 
									}
 
									data.message += "</tr>";
 
									$.each(data.table, function(i, line) {
 
										data.message += "<tr>";
 
										for (var key in line) {
 
											if (line.hasOwnProperty(key)) {
 
												data.message += "<td>" + line[key] + "</td>";
 
											}
 
										}
 
										data.message += "</tr>";
 
									})
 
									data.message += "</table>";
 
								}
 
								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");
 
							});
 
						}
 
					}
 
				}
 
			}
 
		})
 
	});
tests/libtransport/AdminInterface.cpp
Show inline comments
 
@@ -48,52 +48,52 @@ class AdminInterfaceTest : public CPPUNIT_NS :: TestFixture, public BasicSlackTe
 
		}
 

	
 
	std::string sendAdminMessage(const std::string &cmd) {
 
		Swift::Message::ref msg = boost::shared_ptr<Swift::Message>(new Swift::Message());
 
		msg->setFrom(Swift::JID("me@localhost"));
 
		msg->setTo(Swift::JID("localhost"));
 
		msg->setBody(cmd);
 

	
 
		admin->handleMessageReceived(msg);
 
		return msg->getBody().get_value_or("");
 
	}
 

	
 
	void helpCommand() {
 
		std::string resp = sendAdminMessage("help");
 
		CPPUNIT_ASSERT(resp.find("   VAR   status - Shows instance status\n") != std::string::npos);
 
	}
 

	
 
	void statusCommand() {
 
		std::string resp = sendAdminMessage("status");
 
		CPPUNIT_ASSERT_EQUAL(std::string("Running (0 users connected using 0 backends)"), resp);
 
	}
 

	
 
	void joinRoomArgs() {
 
		std::string resp = sendAdminMessage("args join_room");
 
		CPPUNIT_ASSERT_EQUAL(std::string("nickname - \"Nickname in 3rd-party room\" Example: \"BotNickname\"\n"
 
							"legacy_room - \"3rd-party room name\" Example: \"3rd-party room name\"\n"
 
							"legacy_server - \"3rd-party server\" Example: \"3rd.party.server.org\"\n"
 
							"slack_channel - \"Slack Chanel\" Example: \"mychannel\"\n"), resp);
 
		CPPUNIT_ASSERT_EQUAL(std::string("nickname - \"Nickname in 3rd-party room\" Example: \"BotNickname\" Type: \"string\"\n"
 
							"legacy_room - \"3rd-party room name\" Example: \"3rd-party room name\" Type: \"string\"\n"
 
							"legacy_server - \"3rd-party server\" Example: \"3rd.party.server.org\" Type: \"string\"\n"
 
							"slack_channel - \"Slack Chanel\" Example: \"mychannel\" Type: \"string\"\n"), resp);
 
	}
 

	
 
	void getOAuth2URLCommand() {
 
		std::string resp = sendAdminMessage("get_oauth2_url x y z");
 
		CPPUNIT_ASSERT(resp.find("https://slack.com/oauth/authorize?client_id=&scope=channels%3Aread%20channels%3Awrite%20team%3Aread%20im%3Aread%20im%3Awrite%20chat%3Awrite%3Abot%20bot&redirect_uri=https%3A%2F%2Fslack.spectrum.im%2Foauth2%2Flocalhost&state=") != std::string::npos);
 
	}
 

	
 
	void unknownCommand() {
 
		std::string resp = sendAdminMessage("unknown_command test");
 
		CPPUNIT_ASSERT_EQUAL(std::string("Error: Unknown variable or command"), resp);
 
	}
 

	
 
	void listJoinLeaveRoomsCommand() {
 
		addUser();
 

	
 
		std::string resp = sendAdminMessage("list_rooms user@localhost");
 
		CPPUNIT_ASSERT_EQUAL(std::string(""), resp);
 

	
 
		resp = sendAdminMessage("join_room user@localhost SlackBot spectrum conference.spectrum.im slack_channel");
 
		CPPUNIT_ASSERT_EQUAL(std::string("Joined the room"), resp);
 

	
 
		resp = sendAdminMessage("list_rooms user@localhost");
 
		CPPUNIT_ASSERT_EQUAL(std::string("connected room SlackBot spectrum conference.spectrum.im slack_channel\n"), resp);
 

	
0 comments (0 inline, 0 general)