Changeset - 19106c7ec482
[Not reviewed]
36 17 0
Vitaly Takmazov - 8 years ago 2018-03-23 12:28:26
vitalyster@gmail.com
Switch to jsoncpp
53 files changed with 218 insertions and 16602 deletions:
0 comments (0 inline, 0 general)
CMakeLists.txt
Show inline comments
 
@@ -172,6 +172,10 @@ if (NOT PROTOBUF_FOUND AND PROTOBUF_INCLUDE_DIR AND PROTOBUF_LIBRARY)
 
	message(STATUS "Using protobuf: ${PROTOBUF_INCLUDE_DIRS} ${PROTOBUF_LIBRARY}")
 
endif()
 

	
 
find_package(PkgConfig REQUIRED)
 
pkg_check_modules(JSONCPP jsoncpp)
 
include_directories(${JSONCPP_INCLUDE_DIRS})
 

	
 
if (WIN32)
 
	add_definitions(-DSWIFTEN_STATIC=1)
 
	ADD_DEFINITIONS(-D_WIN32_WINNT=0x0600)
include/rapidjson/allocators.h
Show inline comments
 
deleted file
include/rapidjson/cursorstreamwrapper.h
Show inline comments
 
deleted file
include/rapidjson/document.h
Show inline comments
 
deleted file
include/rapidjson/encodedstream.h
Show inline comments
 
deleted file
include/rapidjson/encodings.h
Show inline comments
 
deleted file
include/rapidjson/error/en.h
Show inline comments
 
deleted file
include/rapidjson/error/error.h
Show inline comments
 
deleted file
include/rapidjson/filereadstream.h
Show inline comments
 
deleted file
include/rapidjson/filewritestream.h
Show inline comments
 
deleted file
include/rapidjson/fwd.h
Show inline comments
 
deleted file
include/rapidjson/internal/biginteger.h
Show inline comments
 
deleted file
include/rapidjson/internal/diyfp.h
Show inline comments
 
deleted file
include/rapidjson/internal/dtoa.h
Show inline comments
 
deleted file
include/rapidjson/internal/ieee754.h
Show inline comments
 
deleted file
include/rapidjson/internal/itoa.h
Show inline comments
 
deleted file
include/rapidjson/internal/meta.h
Show inline comments
 
deleted file
include/rapidjson/internal/pow10.h
Show inline comments
 
deleted file
include/rapidjson/internal/regex.h
Show inline comments
 
deleted file
include/rapidjson/internal/stack.h
Show inline comments
 
deleted file
include/rapidjson/internal/strfunc.h
Show inline comments
 
deleted file
include/rapidjson/internal/strtod.h
Show inline comments
 
deleted file
include/rapidjson/internal/swap.h
Show inline comments
 
deleted file
include/rapidjson/istreamwrapper.h
Show inline comments
 
deleted file
include/rapidjson/memorybuffer.h
Show inline comments
 
deleted file
include/rapidjson/memorystream.h
Show inline comments
 
deleted file
include/rapidjson/msinttypes/inttypes.h
Show inline comments
 
deleted file
include/rapidjson/msinttypes/stdint.h
Show inline comments
 
deleted file
include/rapidjson/ostreamwrapper.h
Show inline comments
 
deleted file
include/rapidjson/pointer.h
Show inline comments
 
deleted file
include/rapidjson/prettywriter.h
Show inline comments
 
deleted file
include/rapidjson/rapidjson.h
Show inline comments
 
deleted file
include/rapidjson/reader.h
Show inline comments
 
deleted file
include/rapidjson/schema.h
Show inline comments
 
deleted file
include/rapidjson/stream.h
Show inline comments
 
deleted file
include/rapidjson/stringbuffer.h
Show inline comments
 
deleted file
include/rapidjson/writer.h
Show inline comments
 
deleted file
include/transport/HTTPRequest.h
Show inline comments
 
@@ -7,7 +7,7 @@
 
#include <iostream>
 
#include <sstream>
 
#include <string.h>
 
#include "rapidjson/document.h"
 
#include <json/json.h>
 

	
 
#include <Swiften/SwiftenCompat.h>
 

	
 
@@ -16,7 +16,7 @@ namespace Transport {
 
class HTTPRequest : public Thread {
 
	public:
 
		typedef enum { Get } Type;
 
		typedef boost::function< void (HTTPRequest *, bool, rapidjson::Document &json, const std::string &data) > Callback;
 
		typedef boost::function< void (HTTPRequest *, bool, Json::Value &json, const std::string &data) > Callback;
 

	
 
		HTTPRequest(ThreadPool *tp, Type type, const std::string &url, Callback callback);
 
		HTTPRequest(Type type, const std::string &url);
 
@@ -25,7 +25,7 @@ class HTTPRequest : public Thread {
 

	
 
		void setProxy(std::string, std::string, std::string, std::string);
 
		bool execute();
 
		bool execute(rapidjson::Document &json);
 
		bool execute(Json::Value &json);
 
		std::string getError() {return std::string(curl_errorbuffer);}
 
		const std::string &getRawData() {
 
			return m_data;
 
@@ -51,7 +51,7 @@ class HTTPRequest : public Thread {
 
	private:
 
		bool init();
 
		bool GET(std::string url, std::string &output);
 
		bool GET(std::string url, rapidjson::Document &json);
 
		bool GET(std::string url, Json::Value &json);
 

	
 

	
 
		CURL *curlhandle;
 
@@ -61,7 +61,7 @@ class HTTPRequest : public Thread {
 
		ThreadPool *m_tp;
 
		std::string m_url;
 
		bool m_ok;
 
		rapidjson::Document m_json;
 
		Json::Value m_json;
 
		std::string m_data;
 
		Callback m_callback;
 
		Type m_type;
include/transport/HTTPRequestQueue.h
Show inline comments
 
@@ -7,7 +7,6 @@
 
#include <iostream>
 
#include <sstream>
 
#include <string.h>
 
#include "rapidjson/document.h"
 

	
 
#include "Swiften/Network/Timer.h"
 

	
libtransport/CMakeLists.txt
Show inline comments
 
@@ -47,9 +47,9 @@ find_package(CURL)
 

	
 
if (WIN32)
 
	include_directories("${CMAKE_SOURCE_DIR}/msvc-deps/sqlite3")
 
	TARGET_LINK_LIBRARIES(transport transport-plugin ${PQXX_LIBRARY} ${CURL_LIBRARIES} ${PQ_LIBRARY} ${MYSQL_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES} ${PROTOBUF_LIBRARY} psapi.lib)
 
	TARGET_LINK_LIBRARIES(transport transport-plugin ${PQXX_LIBRARY} ${CURL_LIBRARIES} ${PQ_LIBRARY} ${MYSQL_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES} ${PROTOBUF_LIBRARY} ${JSONCPP_LIBRARIES} psapi.lib)
 
else()
 
	TARGET_LINK_LIBRARIES(transport transport-plugin ${PQXX_LIBRARY} ${CURL_LIBRARIES} ${PQ_LIBRARY} ${SQLITE3_LIBRARIES} ${MYSQL_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES} ${POPT_LIBRARY} ${PROTOBUF_LIBRARY})
 
	TARGET_LINK_LIBRARIES(transport transport-plugin ${PQXX_LIBRARY} ${CURL_LIBRARIES} ${PQ_LIBRARY} ${SQLITE3_LIBRARIES} ${MYSQL_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES} ${POPT_LIBRARY} ${PROTOBUF_LIBRARY} ${JSONCPP_LIBRARIES})
 
endif()
 

	
 
SET_TARGET_PROPERTIES(transport PROPERTIES
libtransport/HTTPRequest.cpp
Show inline comments
 
@@ -98,14 +98,14 @@ bool HTTPRequest::GET(std::string url, 	std::string &data) {
 
	return false;
 
}
 

	
 
bool HTTPRequest::GET(std::string url, rapidjson::Document &json) {
 
bool HTTPRequest::GET(std::string url, Json::Value &json) {
 
	if (!GET(url, m_data)) {
 
		return false;
 
	}
 

	
 
	if(json.Parse<0>(m_data.c_str()).HasParseError()) {
 
	Json::Reader reader;
 
	if(!reader.parse(m_data.c_str(), json)) {
 
		LOG4CXX_ERROR(logger, "Error while parsing JSON");
 
        LOG4CXX_ERROR(logger, m_data);
 
	        LOG4CXX_ERROR(logger, m_data);
 
		strcpy(curl_errorbuffer, "Error while parsing JSON");
 
		return false;
 
	}
 
@@ -143,7 +143,7 @@ bool HTTPRequest::execute() {
 
	return true;
 
}
 

	
 
bool HTTPRequest::execute(rapidjson::Document &json) {
 
bool HTTPRequest::execute(Json::Value &json) {
 
	init();
 
	switch (m_type) {
 
		case Get:
libtransport/OAuth2.cpp
Show inline comments
 
@@ -81,7 +81,7 @@ std::string OAuth2::requestToken(const std::string &code, std::string &token, st
 
		url += "&redirect_uri=" + Util::urlencode(m_redirectURL);
 
	}
 

	
 
	rapidjson::Document resp;
 
	Json::Value resp;
 
	HTTPRequest req(HTTPRequest::Get, url);
 
	if (!req.execute(resp)) {
 
		LOG4CXX_ERROR(logger, url);
 
@@ -90,15 +90,15 @@ std::string OAuth2::requestToken(const std::string &code, std::string &token, st
 
	}
 

	
 
	LOG4CXX_ERROR(logger, req.getRawData());
 
	rapidjson::Value& access_token = resp["access_token"];
 
	if (!access_token.IsString()) {
 
	Json::Value& access_token = resp["access_token"];
 
	if (!access_token.isString()) {
 
		LOG4CXX_ERROR(logger, "No 'access_token' object in the reply.");
 
		LOG4CXX_ERROR(logger, url);
 
		LOG4CXX_ERROR(logger, req.getRawData());
 
		return "No 'access_token' object in the reply.";
 
	}
 

	
 
	token = access_token.GetString();
 
	token = access_token.asString();
 
	if (token.empty()) {
 
		LOG4CXX_ERROR(logger, "Empty 'access_token' object in the reply.");
 
		LOG4CXX_ERROR(logger, url);
 
@@ -106,11 +106,11 @@ std::string OAuth2::requestToken(const std::string &code, std::string &token, st
 
		return "Empty 'access_token' object in the reply.";
 
	}
 

	
 
	rapidjson::Value& bot = resp["bot"];
 
	if (bot.IsObject()) {
 
		rapidjson::Value& bot_access_token = bot["bot_access_token"];
 
		if (bot_access_token.IsString()) {
 
			bot_token = bot_access_token.GetString();
 
	Json::Value& bot = resp["bot"];
 
	if (bot.isObject()) {
 
		Json::Value& bot_access_token = bot["bot_access_token"];
 
		if (bot_access_token.isString()) {
 
			bot_token = bot_access_token.asString();
 
		}
 
	}
 

	
spectrum/src/frontends/slack/SlackAPI.cpp
Show inline comments
 
@@ -37,38 +37,38 @@ namespace Transport {
 

	
 
DEFINE_LOGGER(logger, "SlackAPI");
 

	
 
#define GET_ARRAY(FROM, NAME) rapidjson::Value &NAME = FROM[#NAME]; \
 
	if (!NAME.IsArray()) { \
 
#define GET_ARRAY(FROM, NAME) Json::Value &NAME = FROM[#NAME]; \
 
	if (!NAME.isArray()) { \
 
		LOG4CXX_ERROR(logger, "No '" << #NAME << "' object in the reply."); \
 
		return; \
 
	}
 
	
 
#define STORE_STRING(FROM, NAME) rapidjson::Value &NAME##_tmp = FROM[#NAME]; \
 
	if (!NAME##_tmp.IsString()) {  \
 
#define STORE_STRING(FROM, NAME) Json::Value &NAME##_tmp = FROM[#NAME]; \
 
	if (!NAME##_tmp.isString()) {  \
 
		LOG4CXX_ERROR(logger, "No '" << #NAME << "' string in the reply."); \
 
		LOG4CXX_ERROR(logger, data); \
 
		return; \
 
	} \
 
	std::string NAME = NAME##_tmp.GetString();
 
	std::string NAME = NAME##_tmp.asString();
 

	
 
#define STORE_BOOL(FROM, NAME) rapidjson::Value &NAME##_tmp = FROM[#NAME]; \
 
	if (!NAME##_tmp.IsBool()) {  \
 
#define STORE_BOOL(FROM, NAME) Json::Value &NAME##_tmp = FROM[#NAME]; \
 
	if (!NAME##_tmp.isBool()) {  \
 
		LOG4CXX_ERROR(logger, "No '" << #NAME << "' string in the reply."); \
 
		LOG4CXX_ERROR(logger, data); \
 
		return; \
 
	} \
 
	bool NAME = NAME##_tmp.GetBool();
 
	bool NAME = NAME##_tmp.asBool();
 

	
 
#define GET_OBJECT(FROM, NAME) rapidjson::Value &NAME = FROM[#NAME]; \
 
	if (!NAME.IsObject()) { \
 
#define GET_OBJECT(FROM, NAME) Json::Value &NAME = FROM[#NAME]; \
 
	if (!NAME.isObject()) { \
 
		LOG4CXX_ERROR(logger, "No '" << #NAME << "' object in the reply."); \
 
		return; \
 
	}
 

	
 
#define STORE_STRING_OPTIONAL(FROM, NAME) rapidjson::Value &NAME##_tmp = FROM[#NAME]; \
 
#define STORE_STRING_OPTIONAL(FROM, NAME) Json::Value &NAME##_tmp = FROM[#NAME]; \
 
	std::string NAME; \
 
	if (NAME##_tmp.IsString()) {  \
 
		 NAME = NAME##_tmp.GetString(); \
 
	if (NAME##_tmp.isString()) {  \
 
		 NAME = NAME##_tmp.asString(); \
 
	}
 

	
 
SlackAPI::SlackAPI(Component *component, SlackIdManager *idManager, const std::string &token, const std::string &domain) : HTTPRequestQueue(component, domain) {
 
@@ -81,7 +81,7 @@ SlackAPI::SlackAPI(Component *component, SlackIdManager *idManager, const std::s
 
SlackAPI::~SlackAPI() {
 
}
 

	
 
void SlackAPI::handleSendMessage(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data) {
 
void SlackAPI::handleSendMessage(HTTPRequest *req, bool ok, Json::Value &resp, const std::string &data) {
 
	LOG4CXX_INFO(logger, data);
 
}
 

	
 
@@ -120,28 +120,28 @@ void SlackAPI::setPurpose(const std::string &channel, const std::string &purpose
 
	queueRequest(req);
 
}
 

	
 
std::string SlackAPI::getChannelId(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data) {
 
std::string SlackAPI::getChannelId(HTTPRequest *req, bool ok, Json::Value &resp, const std::string &data) {
 
	if (!ok) {
 
		LOG4CXX_ERROR(logger, req->getError());
 
		LOG4CXX_ERROR(logger, data);
 
		return "";
 
	}
 

	
 
	rapidjson::Value &channel = resp["channel"];
 
	if (!channel.IsObject()) {
 
	Json::Value &channel = resp["channel"];
 
	if (!channel.isObject()) {
 
		LOG4CXX_ERROR(logger, "No 'channel' object in the reply.");
 
		LOG4CXX_ERROR(logger, data);
 
		return "";
 
	}
 

	
 
	rapidjson::Value &id = channel["id"];
 
	if (!id.IsString()) {
 
	Json::Value &id = channel["id"];
 
	if (!id.isString()) {
 
		LOG4CXX_ERROR(logger, "No 'id' string in the reply.");
 
		LOG4CXX_ERROR(logger, data);
 
		return "";
 
	}
 

	
 
	return id.GetString();
 
	return id.asString();
 
}
 

	
 
void SlackAPI::channelsList( HTTPRequest::Callback callback) {
 
@@ -171,35 +171,35 @@ void SlackAPI::imOpen(const std::string &uid, HTTPRequest::Callback callback) {
 
	queueRequest(req);
 
}
 

	
 
std::string SlackAPI::getOwnerId(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data) {
 
std::string SlackAPI::getOwnerId(HTTPRequest *req, bool ok, Json::Value &resp, const std::string &data) {
 
	if (!ok) {
 
		LOG4CXX_ERROR(logger, req->getError());
 
		return "";
 
	}
 

	
 
	rapidjson::Value &members = resp["members"];
 
	if (!members.IsArray()) {
 
	Json::Value &members = resp["members"];
 
	if (!members.isArray()) {
 
		LOG4CXX_ERROR(logger, "No 'members' object in the reply.");
 
		return "";
 
	}
 

	
 
	for (unsigned i = 0; i < members.Size(); i++) {
 
		if (!members[i].IsObject()) {
 
	for (unsigned i = 0; i < members.size(); i++) {
 
		if (!members[i].isObject()) {
 
			continue;
 
		}
 

	
 
		rapidjson::Value &is_primary_owner = members[i]["is_primary_owner"];
 
		if (!is_primary_owner.IsBool()) {
 
		Json::Value &is_primary_owner = members[i]["is_primary_owner"];
 
		if (!is_primary_owner.isBool()) {
 
			continue;
 
		}
 

	
 
		if (is_primary_owner.GetBool()) {
 
			rapidjson::Value &name = members[i]["id"];
 
			if (!name.IsString()) {
 
		if (is_primary_owner.asBool()) {
 
			Json::Value &name = members[i]["id"];
 
			if (!name.isString()) {
 
				LOG4CXX_ERROR(logger, "No 'name' string in the reply.");
 
				return "";
 
			}
 
			return name.GetString();
 
			return name.asString();
 
		}
 
	}
 

	
 
@@ -212,16 +212,16 @@ void SlackAPI::usersList(HTTPRequest::Callback callback) {
 
	queueRequest(req);
 
}
 

	
 
void SlackAPI::getSlackChannelInfo(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data, std::map<std::string, SlackChannelInfo> &ret) {
 
void SlackAPI::getSlackChannelInfo(HTTPRequest *req, bool ok, Json::Value &resp, const std::string &data, std::map<std::string, SlackChannelInfo> &ret) {
 
	if (!ok) {
 
		LOG4CXX_ERROR(logger, req->getError());
 
		return;
 
	}
 

	
 
	rapidjson::Value &channels = resp["channels"];
 
	if (!channels.IsArray()) {
 
		rapidjson::Value &channel = resp["channel"];
 
		if (channel.IsObject()) {
 
	Json::Value &channels = resp["channels"];
 
	if (!channels.isArray()) {
 
		Json::Value &channel = resp["channel"];
 
		if (channel.isObject()) {
 
			SlackChannelInfo info;
 

	
 
			STORE_STRING(channel, id);
 
@@ -230,13 +230,13 @@ void SlackAPI::getSlackChannelInfo(HTTPRequest *req, bool ok, rapidjson::Documen
 
			STORE_STRING(channel, name);
 
			info.name = name;
 

	
 
			rapidjson::Value &members = channel["members"];
 
			for (unsigned y = 0; members.IsArray() && y < members.Size(); y++) {
 
				if (!members[y].IsString()) {
 
			Json::Value &members = channel["members"];
 
			for (unsigned y = 0; members.isArray() && y < members.size(); y++) {
 
				if (!members[y].isString()) {
 
					continue;
 
				}
 

	
 
				info.members.push_back(members[y].GetString());
 
				info.members.push_back(members[y].asString());
 
			}
 

	
 
			ret[info.name] = info;
 
@@ -244,8 +244,8 @@ void SlackAPI::getSlackChannelInfo(HTTPRequest *req, bool ok, rapidjson::Documen
 
		return;
 
	}
 

	
 
	for (unsigned i = 0; i < channels.Size(); i++) {
 
		if (!channels[i].IsObject()) {
 
	for (unsigned i = 0; i < channels.size(); i++) {
 
		if (!channels[i].isObject()) {
 
			continue;
 
		}
 

	
 
@@ -257,13 +257,13 @@ void SlackAPI::getSlackChannelInfo(HTTPRequest *req, bool ok, rapidjson::Documen
 
		STORE_STRING(channels[i], name);
 
		info.name = name;
 

	
 
		rapidjson::Value &members = channels[i]["members"];
 
		for (unsigned y = 0; members.IsArray() && y < members.Size(); y++) {
 
			if (!members[y].IsString()) {
 
		Json::Value &members = channels[i]["members"];
 
		for (unsigned y = 0; members.isArray() && y < members.size(); y++) {
 
			if (!members[y].isString()) {
 
				continue;
 
			}
 

	
 
			info.members.push_back(members[y].GetString());
 
			info.members.push_back(members[y].asString());
 
		}
 

	
 
		ret[info.name] = info;
 
@@ -272,7 +272,7 @@ void SlackAPI::getSlackChannelInfo(HTTPRequest *req, bool ok, rapidjson::Documen
 
	return;
 
}
 

	
 
void SlackAPI::getSlackImInfo(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data, std::map<std::string, SlackImInfo> &ret) {
 
void SlackAPI::getSlackImInfo(HTTPRequest *req, bool ok, Json::Value &resp, const std::string &data, std::map<std::string, SlackImInfo> &ret) {
 
	if (!ok) {
 
		LOG4CXX_ERROR(logger, req->getError());
 
		return;
 
@@ -280,8 +280,8 @@ void SlackAPI::getSlackImInfo(HTTPRequest *req, bool ok, rapidjson::Document &re
 

	
 
	GET_ARRAY(resp, ims);
 

	
 
	for (unsigned i = 0; i < ims.Size(); i++) {
 
		if (!ims[i].IsObject()) {
 
	for (unsigned i = 0; i < ims.size(); i++) {
 
		if (!ims[i].isObject()) {
 
			continue;
 
		}
 

	
 
@@ -300,7 +300,7 @@ void SlackAPI::getSlackImInfo(HTTPRequest *req, bool ok, rapidjson::Document &re
 
	return;
 
}
 

	
 
void SlackAPI::getSlackUserInfo(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data, std::map<std::string, SlackUserInfo> &ret) {
 
void SlackAPI::getSlackUserInfo(HTTPRequest *req, bool ok, Json::Value &resp, const std::string &data, std::map<std::string, SlackUserInfo> &ret) {
 
	if (!ok) {
 
		LOG4CXX_ERROR(logger, req->getError());
 
		return;
 
@@ -308,8 +308,8 @@ void SlackAPI::getSlackUserInfo(HTTPRequest *req, bool ok, rapidjson::Document &
 

	
 
	GET_ARRAY(resp, users);
 

	
 
	for (unsigned i = 0; i < users.Size(); i++) {
 
		if (!users[i].IsObject()) {
 
	for (unsigned i = 0; i < users.size(); i++) {
 
		if (!users[i].isObject()) {
 
			continue;
 
		}
 

	
 
@@ -337,8 +337,8 @@ void SlackAPI::getSlackUserInfo(HTTPRequest *req, bool ok, rapidjson::Document &
 

	
 
	GET_ARRAY(resp, bots);
 

	
 
	for (unsigned i = 0; i < bots.Size(); i++) {
 
		if (!bots[i].IsObject()) {
 
	for (unsigned i = 0; i < bots.size(); i++) {
 
		if (!bots[i].isObject()) {
 
			continue;
 
		}
 

	
 
@@ -380,11 +380,11 @@ std::string SlackAPI::SlackObjectToPlainText(const std::string &object, bool isC
 
	return ret;
 
}
 

	
 
void SlackAPI::handleSlackChannelInvite(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data, const std::string &channel, const std::string &userId, CreateChannelCallback callback) {
 
void SlackAPI::handleSlackChannelInvite(HTTPRequest *req, bool ok, Json::Value &resp, const std::string &data, const std::string &channel, const std::string &userId, CreateChannelCallback callback) {
 
	callback(channel);
 
}
 

	
 
void SlackAPI::handleSlackChannelCreate(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data, const std::string &channel, const std::string &userId, CreateChannelCallback callback) {
 
void SlackAPI::handleSlackChannelCreate(HTTPRequest *req, bool ok, Json::Value &resp, const std::string &data, const std::string &channel, const std::string &userId, CreateChannelCallback callback) {
 
	std::map<std::string, SlackChannelInfo> &channels = m_idManager->getChannels();
 
	SlackAPI::getSlackChannelInfo(req, ok, resp, data, channels);
 

	
 
@@ -398,7 +398,7 @@ void SlackAPI::handleSlackChannelCreate(HTTPRequest *req, bool ok, rapidjson::Do
 
	channelsInvite(channelId, userId, boost::bind(&SlackAPI::handleSlackChannelInvite, this, _1, _2, _3, _4, channelId, userId, callback));
 
}
 

	
 
void SlackAPI::handleSlackChannelList(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data, const std::string &channel, const std::string &userId, CreateChannelCallback callback) {
 
void SlackAPI::handleSlackChannelList(HTTPRequest *req, bool ok, Json::Value &resp, const std::string &data, const std::string &channel, const std::string &userId, CreateChannelCallback callback) {
 
	std::map<std::string, SlackChannelInfo> &channels = m_idManager->getChannels();
 
	SlackAPI::getSlackChannelInfo(req, ok, resp, data, channels);
 

	
spectrum/src/frontends/slack/SlackAPI.h
Show inline comments
 
@@ -22,7 +22,7 @@
 

	
 
#include "transport/HTTPRequestQueue.h"
 
#include "transport/HTTPRequest.h"
 
#include "rapidjson/document.h"
 
#include <json/json.h>
 

	
 
#include <string>
 
#include <algorithm>
 
@@ -72,22 +72,22 @@ class SlackAPI : public HTTPRequestQueue {
 
		virtual ~SlackAPI();
 

	
 
		void usersList(HTTPRequest::Callback callback);
 
		std::string getOwnerId(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data);
 
		std::string getOwnerId(HTTPRequest *req, bool ok, Json::Value &resp, const std::string &data);
 

	
 
		void channelsCreate(const std::string &name, HTTPRequest::Callback callback);
 
		void channelsInvite(const std::string &channel, const std::string &user, HTTPRequest::Callback callback);
 
		void channelsList(HTTPRequest::Callback callback);
 

	
 
		void imOpen(const std::string &uid, HTTPRequest::Callback callback);
 
		std::string getChannelId(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data);
 
		std::string getChannelId(HTTPRequest *req, bool ok, Json::Value &resp, const std::string &data);
 

	
 
		void deleteMessage(const std::string &channel, const std::string &ts);
 
		void sendMessage(const std::string &from, const std::string &to, const std::string &text);
 
		void setPurpose(const std::string &channel, const std::string &purpose);
 

	
 
		static void getSlackChannelInfo(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data, std::map<std::string, SlackChannelInfo> &channels);
 
		static void getSlackImInfo(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data, std::map<std::string, SlackImInfo> &ims);
 
		static void getSlackUserInfo(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data, std::map<std::string, SlackUserInfo> &users);
 
		static void getSlackChannelInfo(HTTPRequest *req, bool ok, Json::Value &resp, const std::string &data, std::map<std::string, SlackChannelInfo> &channels);
 
		static void getSlackImInfo(HTTPRequest *req, bool ok, Json::Value &resp, const std::string &data, std::map<std::string, SlackImInfo> &ims);
 
		static void getSlackUserInfo(HTTPRequest *req, bool ok, Json::Value &resp, const std::string &data, std::map<std::string, SlackUserInfo> &users);
 
		static std::string SlackObjectToPlainText(const std::string &object, bool isChannel = false, bool returnName = false);
 

	
 
		// Creates channel if it does not exist and invites the user to the channel.
 
@@ -95,11 +95,11 @@ class SlackAPI : public HTTPRequestQueue {
 
		void createChannel(const std::string &channel, const std::string &user, CreateChannelCallback callback);
 

	
 
	private:
 
		void handleSendMessage(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data);
 
		void handleSendMessage(HTTPRequest *req, bool ok, Json::Value &resp, const std::string &data);
 

	
 
		void handleSlackChannelCreate(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data, const std::string &channel, const std::string &user, CreateChannelCallback callback);
 
		void handleSlackChannelList(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data, const std::string &channel, const std::string &user, CreateChannelCallback callback);
 
		void handleSlackChannelInvite(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data, const std::string &channel, const std::string &user, CreateChannelCallback callback);
 
		void handleSlackChannelCreate(HTTPRequest *req, bool ok, Json::Value &resp, const std::string &data, const std::string &channel, const std::string &user, CreateChannelCallback callback);
 
		void handleSlackChannelList(HTTPRequest *req, bool ok, Json::Value &resp, const std::string &data, const std::string &channel, const std::string &user, CreateChannelCallback callback);
 
		void handleSlackChannelInvite(HTTPRequest *req, bool ok, Json::Value &resp, const std::string &data, const std::string &channel, const std::string &user, CreateChannelCallback callback);
 

	
 
	private:
 
		Component *m_component;
spectrum/src/frontends/slack/SlackIdManager.h
Show inline comments
 
@@ -23,7 +23,6 @@
 
#include "SlackAPI.h"
 

	
 
#include "transport/StorageBackend.h"
 
#include "rapidjson/document.h"
 

	
 
#include <Swiften/Network/TLSConnectionFactory.h>
 
#include <Swiften/Network/HostAddressPort.h>
spectrum/src/frontends/slack/SlackRTM.cpp
Show inline comments
 
@@ -72,37 +72,38 @@ void SlackRTM::start() {
 
	req->execute();
 
}
 

	
 
#define STORE_STRING(FROM, NAME) rapidjson::Value &NAME##_tmp = FROM[#NAME]; \
 
	if (!NAME##_tmp.IsString()) {  \
 
#define STORE_STRING(FROM, NAME) Json::Value &NAME##_tmp = FROM[#NAME]; \
 
	if (!NAME##_tmp.isString()) {  \
 
		LOG4CXX_ERROR(logger, "No '" << #NAME << "' string in the reply."); \
 
		LOG4CXX_ERROR(logger, payload); \
 
		return; \
 
	} \
 
	std::string NAME = NAME##_tmp.GetString();
 
	std::string NAME = NAME##_tmp.asString();
 

	
 
#define STORE_STRING_OPTIONAL(FROM, NAME) rapidjson::Value &NAME##_tmp = FROM[#NAME]; \
 
#define STORE_STRING_OPTIONAL(FROM, NAME) Json::Value &NAME##_tmp = FROM[#NAME]; \
 
	std::string NAME; \
 
	if (NAME##_tmp.IsString()) {  \
 
		 NAME = NAME##_tmp.GetString(); \
 
	if (NAME##_tmp.isString()) {  \
 
		 NAME = NAME##_tmp.asString(); \
 
	}
 

	
 
#define GET_OBJECT(FROM, NAME) rapidjson::Value &NAME = FROM[#NAME]; \
 
	if (!NAME.IsObject()) { \
 
#define GET_OBJECT(FROM, NAME) Json::Value &NAME = FROM[#NAME]; \
 
	if (!NAME.isObject()) { \
 
		LOG4CXX_ERROR(logger, "No '" << #NAME << "' object in the reply."); \
 
		return; \
 
	}
 

	
 
#define STORE_INT(FROM, NAME) rapidjson::Value &NAME##_tmp = FROM[#NAME]; \
 
	if (!NAME##_tmp.IsInt()) {  \
 
#define STORE_INT(FROM, NAME) Json::Value &NAME##_tmp = FROM[#NAME]; \
 
	if (!NAME##_tmp.isInt()) {  \
 
		LOG4CXX_ERROR(logger, "No '" << #NAME << "' number in the reply."); \
 
		LOG4CXX_ERROR(logger, payload); \
 
		return; \
 
	} \
 
	int NAME = NAME##_tmp.GetInt();
 
	int NAME = NAME##_tmp.asInt();
 

	
 
void SlackRTM::handlePayloadReceived(const std::string &payload) {
 
	rapidjson::Document d;
 
	if (d.Parse<0>(payload.c_str()).HasParseError()) {
 
	Json::Value d;
 
	Json::Reader reader;
 
	if (!reader.parse(payload.c_str(), d)) {
 
		LOG4CXX_ERROR(logger, "Error while parsing JSON");
 
		LOG4CXX_ERROR(logger, payload);
 
		return;
 
@@ -117,9 +118,9 @@ void SlackRTM::handlePayloadReceived(const std::string &payload) {
 
		STORE_STRING_OPTIONAL(d, subtype);
 
		STORE_STRING_OPTIONAL(d, purpose);
 

	
 
		rapidjson::Value &attachments = d["attachments"];
 
		if (attachments.IsArray()) {
 
			for (unsigned i = 0; i < attachments.Size(); i++) {
 
		Json::Value &attachments = d["attachments"];
 
		if (attachments.isArray()) {
 
			for (unsigned i = 0; i < attachments.size(); i++) {
 
				STORE_STRING_OPTIONAL(attachments[i], fallback);
 
				if (!fallback.empty()) {
 
					text += fallback;
 
@@ -181,13 +182,12 @@ void SlackRTM::sendPing() {
 
	m_pingTimer->start();
 
}
 

	
 
void SlackRTM::handleRTMStart(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data) {
 
void SlackRTM::handleRTMStart(HTTPRequest *req, bool ok, Json::Value &resp, const std::string &data) {
 
	if (!ok) {
 
		LOG4CXX_ERROR(logger, req->getError());
 
		LOG4CXX_ERROR(logger, data);
 
		return;
 
	}
 

	
 
	STORE_STRING_OPTIONAL(resp, error);
 
	if (!error.empty()) {
 
		if (error == "account_inactive") {
 
@@ -197,44 +197,43 @@ void SlackRTM::handleRTMStart(HTTPRequest *req, bool ok, rapidjson::Document &re
 
			return;
 
		}
 
	}
 

	
 
	rapidjson::Value &url = resp["url"];
 
	if (!url.IsString()) {
 
	Json::Value &url = resp["url"];
 
	if (!url.isString()) {
 
		LOG4CXX_ERROR(logger, "No 'url' object in the reply.");
 
		LOG4CXX_ERROR(logger, data);
 
		return;
 
	}
 

	
 
	rapidjson::Value &self = resp["self"];
 
	if (!self.IsObject()) {
 
	Json::Value &self = resp["self"];
 
	if (!self.isObject()) {
 
		LOG4CXX_ERROR(logger, "No 'self' object in the reply.");
 
		LOG4CXX_ERROR(logger, data);
 
		return;
 
	}
 

	
 
	rapidjson::Value &selfName = self["name"];
 
	if (!selfName.IsString()) {
 
	Json::Value &selfName = self["name"];
 
	if (!selfName.isString()) {
 
		LOG4CXX_ERROR(logger, "No 'name' string in the reply.");
 
		LOG4CXX_ERROR(logger, data);
 
		return;
 
	}
 

	
 
	m_idManager->setSelfName(selfName.GetString());
 
	m_idManager->setSelfName(selfName.asString());
 

	
 
	rapidjson::Value &selfId = self["id"];
 
	if (!selfId.IsString()) {
 
	Json::Value &selfId = self["id"];
 
	if (!selfId.isString()) {
 
		LOG4CXX_ERROR(logger, "No 'id' string in the reply.");
 
		LOG4CXX_ERROR(logger, data);
 
		return;
 
	}
 

	
 
	m_idManager->setSelfId(selfId.GetString());
 
	m_idManager->setSelfId(selfId.asString());
 

	
 
	SlackAPI::getSlackChannelInfo(req, ok, resp, data, m_idManager->getChannels());
 
	SlackAPI::getSlackImInfo(req, ok, resp, data, m_idManager->getIMs());
 
	SlackAPI::getSlackUserInfo(req, ok, resp, data, m_idManager->getUsers());
 

	
 
	std::string u = url.GetString();
 
	std::string u = url.asString();
 
	LOG4CXX_INFO(logger, "Started RTM, WebSocket URL is " << u);
 
	LOG4CXX_INFO(logger, data);
 

	
spectrum/src/frontends/slack/SlackRTM.h
Show inline comments
 
@@ -23,7 +23,7 @@
 
#include "SlackAPI.h"
 

	
 
#include "transport/StorageBackend.h"
 
#include "rapidjson/document.h"
 
#include <json/json.h>
 

	
 
#include <Swiften/Network/TLSConnectionFactory.h>
 
#include <Swiften/Network/HostAddressPort.h>
 
@@ -80,7 +80,7 @@ class SlackRTM {
 
	private:
 
#endif
 
		void handlePayloadReceived(const std::string &payload);
 
		void handleRTMStart(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data);
 
		void handleRTMStart(HTTPRequest *req, bool ok, Json::Value &resp, const std::string &data);
 
		void handleWebSocketConnected();
 
		void handleWebSocketDisconnected(const boost::optional<Swift::Connection::Error> &error);
 

	
spectrum/src/frontends/slack/SlackSession.h
Show inline comments
 
@@ -21,7 +21,6 @@
 
#pragma once
 

	
 
#include "transport/StorageBackend.h"
 
#include "rapidjson/document.h"
 

	
 
#include <string>
 
#include <algorithm>
spectrum/src/frontends/slack/SlackUserRegistration.cpp
Show inline comments
 
@@ -34,7 +34,7 @@
 
#include "transport/Util.h"
 
#include "transport/HTTPRequest.h"
 

	
 
#include "rapidjson/document.h"
 
#include <json/json.h>
 

	
 
#include <boost/shared_ptr.hpp>
 
#include <boost/thread.hpp>
 
@@ -87,7 +87,7 @@ std::string SlackUserRegistration::createOAuth2URL(const std::vector<std::string
 
std::string SlackUserRegistration::getTeamDomain(const std::string &token) {
 
	std::string url = "https://slack.com/api/team.info?token=" + Util::urlencode(token);
 

	
 
	rapidjson::Document resp;
 
	Json::Value resp;
 
	HTTPRequest req(HTTPRequest::Get, url);
 
	if (!req.execute(resp)) {
 
		LOG4CXX_ERROR(logger, url);
 
@@ -95,23 +95,23 @@ std::string SlackUserRegistration::getTeamDomain(const std::string &token) {
 
		return "";
 
	}
 

	
 
	rapidjson::Value &team = resp["team"];
 
	if (!team.IsObject()) {
 
	Json::Value &team = resp["team"];
 
	if (!team.isObject()) {
 
		LOG4CXX_ERROR(logger, "No 'team' object in the reply.");
 
		LOG4CXX_ERROR(logger, url);
 
		LOG4CXX_ERROR(logger, req.getRawData());
 
		return "";
 
	}
 

	
 
	rapidjson::Value &domain = team["domain"];
 
	if (!domain.IsString()) {
 
	Json::Value &domain = team["domain"];
 
	if (!domain.isString()) {
 
		LOG4CXX_ERROR(logger, "No 'domain' string in the reply.");
 
		LOG4CXX_ERROR(logger, url);
 
		LOG4CXX_ERROR(logger, req.getRawData());
 
		return "";
 
	}
 

	
 
	return domain.GetString();
 
	return domain.asString();
 
}
 

	
 
std::string SlackUserRegistration::handleOAuth2Code(const std::string &code, const std::string &state) {
spectrum_manager/src/APIServer.cpp
Show inline comments
 
@@ -12,9 +12,7 @@
 
#include <string>
 
#include <cerrno>
 

	
 
#include "rapidjson/rapidjson.h"
 
#include "rapidjson/stringbuffer.h"
 
#include "rapidjson/writer.h"
 
#include <json/json.h>
 

	
 
#include <boost/tokenizer.hpp>
 
using boost::tokenizer;
 
@@ -55,11 +53,9 @@ APIServer::APIServer(ManagerConfig *config, StorageBackend *storage) {
 
APIServer::~APIServer() {
 
}
 

	
 
void APIServer::send_json(struct mg_connection *conn, const Document &d) {
 
	StringBuffer buffer;
 
	Writer<StringBuffer> writer(buffer);
 
	d.Accept(writer);
 
	std::string json(buffer.GetString());
 
void APIServer::send_json(struct mg_connection *conn, const Json::Value &d) {
 
	Json::FastWriter writer;
 
	std::string json = writer.write(d);
 

	
 
	std::cout << "Sending JSON:\n";
 
	std::cout << json << "\n";
 
@@ -73,10 +69,9 @@ void APIServer::send_json(struct mg_connection *conn, const Document &d) {
 
}
 

	
 
void APIServer::send_ack(struct mg_connection *conn, bool error, const std::string &message) {
 
	Document json;
 
	json.SetObject();
 
	json.AddMember("error", error, json.GetAllocator());
 
	json.AddMember("message", StringRef(message.c_str()), json.GetAllocator());
 
	Json::Value json;
 
	json["error"] = error;
 
	json["message"] = message;
 

	
 
	send_json(conn, json);
 
}
 
@@ -89,23 +84,21 @@ void APIServer::serve_instances(Server *server, Server::session *session, struct
 
	std::vector<std::string> usernames;
 
	std::vector<std::string> list = show_list(m_config, false);
 

	
 
	Document json;
 
	json.SetObject();
 
	json.AddMember("error", 0, json.GetAllocator());
 
	Json::Value json;
 
	json["error"] = 0;
 

	
 
	Value instances(kArrayType);
 
	Json::Value instances(Json::ValueType::arrayValue);
 
	BOOST_FOREACH(std::string &id, list) {
 
		Value instance;
 
		instance.SetObject();
 
		instance.AddMember("id", StringRef(id.c_str()), json.GetAllocator());
 
		Json::Value instance;
 
		instance["id"] = id;
 

	
 
		std::string name = get_config(m_config, id, "identity.name");
 
		if (name.empty() || name == "Spectrum 2 Transport") {
 
			instance.AddMember("name", StringRef(id.c_str()), json.GetAllocator());
 
			instance["name"] = id;
 
		}
 
		else {
 
			statuses.push_back(name);
 
			instance.AddMember("name", StringRef(statuses.back().c_str()), json.GetAllocator());
 
			instance["name"] = statuses.back();
 
		}
 

	
 
		std::string status = server->send_command(id, "status");
 
@@ -114,13 +107,13 @@ void APIServer::serve_instances(Server *server, Server::session *session, struct
 
		}
 

	
 
		statuses.push_back(status);
 
		instance.AddMember("status", StringRef(statuses.back().c_str()), json.GetAllocator());
 
		instance["status"] = statuses.back();
 

	
 
		bool running = true;
 
		if (status.find("Running") == std::string::npos) {
 
			running = false;
 
		}
 
		instance.AddMember("running", running, json.GetAllocator());
 
		instance["running"] = running;
 

	
 
		UserInfo info;
 
		m_storage->getUser(session->user, info);
 
@@ -129,16 +122,16 @@ void APIServer::serve_instances(Server *server, Server::session *session, struct
 
		m_storage->getUserSetting(info.id, id, type, username);
 

	
 
		usernames.push_back(username);
 
		instance.AddMember("registered", !username.empty(), json.GetAllocator());
 
		instance.AddMember("username", StringRef(usernames.back().c_str()), json.GetAllocator());
 
		instance["registered"] = !username.empty();
 
		instance["username"] = usernames.back();
 

	
 
		usernames.push_back(get_config(m_config, id, "service.frontend"));
 
		instance.AddMember("frontend", StringRef(usernames.back().c_str()), json.GetAllocator());
 
		instance["frontend"] = usernames.back();
 

	
 
		instances.PushBack(instance, json.GetAllocator());
 
		instances.append(instance);
 
	}
 

	
 
	json.AddMember("instances", instances, json.GetAllocator());
 
	json["instances"] = instances;
 
	send_json(conn, json);
 
}
 

	
 
@@ -232,10 +225,9 @@ void APIServer::serve_instances_register(Server *server, Server::session *sessio
 
		// Check if the frontend wants to use OAuth2 (Slack for example).
 
		std::string response = server->send_command(instance, "get_oauth2_url " + jid + " " + uin + " " + password);
 
		if (!response.empty() && response.find("Error:") != 0) {
 
			Document json;
 
			json.SetObject();
 
			json.AddMember("error", false, json.GetAllocator());
 
			json.AddMember("oauth2_url", StringRef(response.c_str()), json.GetAllocator());
 
			Json::Value json;
 
			json["error"] = false;
 
			json["oauth2_url"] = response;
 
			send_json(conn, json);
 
		}
 
		else {
 
@@ -294,12 +286,11 @@ void APIServer::serve_instances_commands(Server *server, Server::session *sessio
 
	std::vector<std::string> commands;
 
	boost::split(commands, response, boost::is_any_of("\n"));
 

	
 
	Document json;
 
	json.SetObject();
 
	json.AddMember("error", 0, json.GetAllocator());
 
	Json::Value json;
 
	json["error"] = 0;
 

	
 
	std::vector<std::vector<std::string> > tmp;
 
	Value cmds(kArrayType);
 
	Json::Value cmds(Json::ValueType::arrayValue);
 
	BOOST_FOREACH(const std::string &command, commands) {
 
		escaped_list_separator<char> els('\\', ' ', '\"');
 
		tokenizer<escaped_list_separator<char> > tok(command, els);
 
@@ -330,17 +321,16 @@ void APIServer::serve_instances_commands(Server *server, Server::session *sessio
 

	
 
		tmp.push_back(tokens);
 

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

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

	
 
@@ -360,12 +350,11 @@ void APIServer::serve_instances_variables(Server *server, Server::session *sessi
 
	std::vector<std::string> commands;
 
	boost::split(commands, response, boost::is_any_of("\n"));
 

	
 
	Document json;
 
	json.SetObject();
 
	json.AddMember("error", 0, json.GetAllocator());
 
	Json::Value json;
 
	json["error"] = 0;
 

	
 
	std::vector<std::vector<std::string> > tmp;
 
	Value cmds(kArrayType);
 
	Json::Value cmds(Json::ValueType::arrayValue);
 
	BOOST_FOREACH(const std::string &command, commands) {
 
		escaped_list_separator<char> els('\\', ' ', '\"');
 
		tokenizer<escaped_list_separator<char> > tok(command, els);
 
@@ -385,18 +374,17 @@ void APIServer::serve_instances_variables(Server *server, Server::session *sessi
 

	
 
		tmp.push_back(tokens);
 

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

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

	
 
@@ -459,20 +447,18 @@ void APIServer::serve_instances_command_args(Server *server, Server::session *se
 
	std::vector<std::string> args;
 
	boost::split(args, response, boost::is_any_of("\n"));
 

	
 
	Document json;
 
	json.SetObject();
 
	json.AddMember("error", 0, json.GetAllocator());
 
	Json::Value json;
 
	json["error"] = 0;
 

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

	
 
	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());
 
		Json::Value arg;
 
		arg["name"] = "username";
 
		arg["label"] = "Username";
 
		arg["example"] = "";
 
		argList.append(arg);
 
	}
 

	
 
	BOOST_FOREACH(const std::string &argument, args) {
 
@@ -490,16 +476,15 @@ void APIServer::serve_instances_command_args(Server *server, Server::session *se
 

	
 
		tmp.push_back(tokens);
 

	
 
		Value arg;
 
		arg.SetObject();
 
		arg.AddMember("name", StringRef(tokens[0].c_str()), json.GetAllocator());
 
		arg.AddMember("label", StringRef(tokens[2].c_str()), json.GetAllocator());
 
		arg.AddMember("example", StringRef(tokens[4].c_str()), json.GetAllocator());
 
		arg.AddMember("type", StringRef(tokens[6].c_str()), json.GetAllocator());
 
		argList.PushBack(arg, json.GetAllocator());
 
		Json::Value arg;
 
		arg["name"] = tokens[0];
 
		arg["label"] = tokens[2];
 
		arg["example"] = tokens[4];
 
		arg["type"] = tokens[6];
 
		argList.append(arg);
 
	}
 

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

	
 
@@ -581,20 +566,18 @@ void APIServer::serve_instances_execute(Server *server, Server::session *session
 
	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());
 
		Json::Value json;
 
		json["error"] = 0;
 

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

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

	
 
			Value arg;
 
			arg.SetObject();
 
			Json::Value arg;
 

	
 
			std::string key;
 
			int i = 0;
 
@@ -605,11 +588,11 @@ void APIServer::serve_instances_execute(Server *server, Server::session *session
 
				}
 
				if (i == 0) {
 
					tmp.push_back(*beg);
 
					arg.AddMember("Key", StringRef(tmp.back().c_str()), json.GetAllocator());
 
					arg["Key"] = tmp.back();
 
				}
 
				else if (i == 2 && hasDesc) {
 
					tmp.push_back(*beg);
 
					arg.AddMember("Description", StringRef(tmp.back().c_str()), json.GetAllocator());
 
					arg["Description"] = tmp.back();
 
				}
 
				else if (i > 1 || (!hasDesc && i > 0)) {
 
					if (key.empty()) {
 
@@ -618,16 +601,16 @@ void APIServer::serve_instances_execute(Server *server, Server::session *session
 
					else {
 
						tmp.push_back(key);
 
						tmp2.push_back(*beg);
 
						arg.AddMember(StringRef(tmp.back().c_str()), StringRef(tmp2.back().c_str()), json.GetAllocator());
 
						arg[tmp.back()] = tmp2.back();
 
						key = "";
 
					}
 
				}
 
			}
 
			table.PushBack(arg, json.GetAllocator());
 
			table.append(arg);
 
		}
 

	
 
		json.AddMember("table", table, json.GetAllocator());
 
		json.AddMember("message", StringRef(response.c_str()), json.GetAllocator());
 
		json["table"] = table;
 
		json["message"] = response;
 
		send_json(conn, json);
 
	}
 
	else {
 
@@ -639,22 +622,20 @@ void APIServer::serve_instances_execute(Server *server, Server::session *session
 
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());
 
	Json::Value json;
 
	json["error"] = 0;
 

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

	
 
	Value users(kArrayType);
 
	Json::Value users(Json::ValueType::arrayValue);
 
	BOOST_FOREACH(std::string &id, list) {
 
		Value user;
 
		user.SetObject();
 
		user.AddMember("username", StringRef(id.c_str()), json.GetAllocator());
 
		users.PushBack(user, json.GetAllocator());
 
		Json::Value user;
 
		user["username"] = id;
 
		users.append(user);
 
	}
 

	
 
	json.AddMember("users", users, json.GetAllocator());
 
	json["users"] = users;
 
	send_json(conn, json);
 
}
 

	
spectrum_manager/src/APIServer.h
Show inline comments
 
@@ -27,7 +27,7 @@
 
#include <boost/assign.hpp>
 
#include <boost/bind.hpp>
 

	
 
#include "rapidjson/document.h"
 
#include <json/json.h>
 

	
 
#include "mongoose.h"
 
#include "managerconfig.h"
 
@@ -40,7 +40,6 @@
 
#include "transport/StorageBackend.h"
 

	
 
using namespace Transport;
 
using namespace rapidjson;
 

	
 
class APIServer {
 
	public:
 
@@ -63,7 +62,7 @@ class APIServer {
 
		void serve_users(Server *server, Server::session *sess, struct mg_connection *conn, struct http_message *hm);
 
		void serve_users_add(Server *server, Server::session *sess, struct mg_connection *conn, struct http_message *hm);
 
		void serve_users_remove(Server *server, Server::session *sess, struct mg_connection *conn, struct http_message *hm);
 
		void send_json(struct mg_connection *conn, const Document &d);
 
		void send_json(struct mg_connection *conn, const Json::Value &d);
 
		void send_ack(struct mg_connection *conn, bool error, const std::string &message);
 

	
 
	private:
tests/libtransport/HTTPRequest.cpp
Show inline comments
 
@@ -41,12 +41,12 @@ class HTTPRequestTest : public CPPUNIT_NS :: TestFixture, public BasicTest {
 
			delete tp;
 
		}
 

	
 
	void handleResult(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data) {
 
	void handleResult(HTTPRequest *req, bool ok, Json::Value &resp, const std::string &data) {
 
		result = true;
 
	}
 

	
 
	void GET() {
 
		rapidjson::Document resp;
 
		Json::Value resp;
 
		HTTPRequest *req = new HTTPRequest(tp, HTTPRequest::Get, "http://spectrum.im/params.json", boost::bind(&HTTPRequestTest::handleResult, this, _1, _2, _3, _4));
 
		req->execute(resp);
 
		delete req;
tests/libtransport/SlackRTM.cpp
Show inline comments
 
@@ -35,16 +35,17 @@ class SlackRTMTest : public CPPUNIT_NS :: TestFixture, public BasicTest {
 
		}
 

	
 
		void handleRTMStart() {
 
			rapidjson::Document json;
 
			json.Parse<0>(rtmStartPayload.c_str());
 
			Json::Value json;
 
			Json::Reader reader;
 
			bool ok = reader.parse(rtmStartPayload.c_str(), json);
 
			CPPUNIT_ASSERT_EQUAL(true, ok);
 
			m_rtm->handleRTMStart(NULL, true, json, rtmStartPayload);
 

	
 
			CPPUNIT_ASSERT_EQUAL(std::string("spectrum2"), m_idManager->getSelfName());
 
			CPPUNIT_ASSERT_EQUAL(std::string("U0KECRDJB"), m_idManager->getSelfId());
 
			CPPUNIT_ASSERT_EQUAL(std::string("owner"), m_idManager->getName("U0H6EEWNN"));
 
			CPPUNIT_ASSERT_EQUAL(std::string("C0H6B0SQM"), m_idManager->getId("spectrum2_contactlist"));
 
			CPPUNIT_ASSERT_EQUAL(true, m_idManager->hasMember("C0KH09UQ2", "U0KECRDJB"));
 
			CPPUNIT_ASSERT_EQUAL(false, m_idManager->hasMember("C0KH09UQ2", "U1KECRDJB"));
 
			CPPUNIT_ASSERT_MESSAGE("Has member", m_idManager->hasMember("C0KH09UQ2", "U1KECRDJB") == false);
 
		}
 

	
 
		void handlePayloadReceivedChannelJoined() {
0 comments (0 inline, 0 general)