Changeset - 5dd1aa90b4c0
[Not reviewed]
0 3 0
Jan Kaluza - 9 years ago 2016-02-21 19:43:39
jkaluza@redhat.com
Slack: Handle 'account_inactive' error
3 files changed with 20 insertions and 6 deletions:
0 comments (0 inline, 0 general)
include/transport/HTTPRequest.h
Show inline comments
 

	
 
#pragma once
 

	
 
#include "curl/curl.h"
 
#include "transport/Logging.h"
 
#include "transport/ThreadPool.h"
 
#include <iostream>
 
#include <sstream>
 
#include <string.h>
 
#include "rapidjson/document.h"
 

	
 
#include <boost/signal.hpp>
 

	
 
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;
 

	
 
		HTTPRequest(ThreadPool *tp, Type type, const std::string &url, Callback callback);
 
		HTTPRequest(Type type, const std::string &url);
 

	
 
		virtual ~HTTPRequest() {
 
			if(curlhandle) {
 
				curl_easy_cleanup(curlhandle);
 
				curlhandle = NULL;
 
			}
 
		}
 
		virtual ~HTTPRequest();
 

	
 
		void setProxy(std::string, std::string, std::string, std::string);
 
		bool execute();
 
		bool execute(rapidjson::Document &json);
 
		std::string getError() {return std::string(curl_errorbuffer);}
 
		const std::string &getRawData() {
 
			return m_data;
 
		}
 

	
 
		void run();
 
		void finalize();
 

	
 
		const std::string &getURL() {
 
			return m_url;
 
		}
 

	
 
		boost::signal<void ()> onRequestFinished;
 

	
 
	private:
 
		bool init();
 
		bool GET(std::string url, std::string &output);
 
		bool GET(std::string url, rapidjson::Document &json);
 

	
 

	
libtransport/HTTPRequest.cpp
Show inline comments
 
#include "transport/HTTPRequest.h"
 

	
 
namespace Transport {
 

	
 
DEFINE_LOGGER(logger, "HTTPRequest")
 

	
 
HTTPRequest::HTTPRequest(ThreadPool *tp, Type type, const std::string &url, Callback callback) {
 
	m_type = type;
 
	m_url = url;
 
	m_tp = tp;
 
	m_callback = callback;
 

	
 
	init();
 
}
 

	
 
HTTPRequest::HTTPRequest(Type type, const std::string &url) {
 
	m_type = type;
 
	m_url = url;
 
	m_tp = NULL;
 

	
 
	init();
 
}
 

	
 
HTTPRequest::~HTTPRequest() {
 
	if (curlhandle) {
 
		LOG4CXX_INFO(logger, "Cleaning up CURL handle");
 
		curl_easy_cleanup(curlhandle);
 
		curlhandle = NULL;
 
	}
 
}
 

	
 
bool HTTPRequest::init() {
 
	curlhandle = curl_easy_init();
 
	if (curlhandle) {
 
		curlhandle = curl_easy_init();
 
		curl_easy_setopt(curlhandle, CURLOPT_PROXY, NULL);
 
		curl_easy_setopt(curlhandle, CURLOPT_PROXYUSERPWD, NULL);
 
		curl_easy_setopt(curlhandle, CURLOPT_PROXYAUTH, (long)CURLAUTH_ANY);
 
		return true;
 
	}
 

	
 
	LOG4CXX_ERROR(logger, "Couldn't Initialize curl!")
 
	return false;
 
}
 

	
 
void HTTPRequest::setProxy(std::string IP, std::string port, std::string username, std::string password) {
 
	if (curlhandle) {
 
		std::string proxyIpPort = IP + ":" + port;
 
		curl_easy_setopt(curlhandle, CURLOPT_PROXY, proxyIpPort.c_str());
 
		if(username.length() && password.length()) {
 
			std::string proxyUserPass = username + ":" + password;
 
			curl_easy_setopt(curlhandle, CURLOPT_PROXYUSERPWD, proxyUserPass.c_str());
 
		}
 
	} else {
 
		LOG4CXX_ERROR(logger, "Trying to set proxy while CURL isn't initialized")
 
@@ -64,48 +72,49 @@ bool HTTPRequest::GET(std::string url, 	std::string &data) {
 
		curl_easy_setopt(curlhandle, CURLOPT_ENCODING, "");
 
		
 
		data = "";
 
		callbackdata = "";
 
		memset(curl_errorbuffer, 0, 1024);
 
		
 
		curl_easy_setopt(curlhandle, CURLOPT_ERRORBUFFER, curl_errorbuffer);
 
		curl_easy_setopt(curlhandle, CURLOPT_WRITEFUNCTION, curlCallBack);
 
		curl_easy_setopt(curlhandle, CURLOPT_WRITEDATA, this);
 
			
 
		/* Set http request and url */
 
		curl_easy_setopt(curlhandle, CURLOPT_HTTPGET, 1);
 
		curl_easy_setopt(curlhandle, CURLOPT_VERBOSE, 0);
 
		curl_easy_setopt(curlhandle, CURLOPT_URL, url.c_str());
 
		
 
		/* Send http request and return status*/
 
		if(CURLE_OK == curl_easy_perform(curlhandle)) {
 
			data = callbackdata;
 
			return true;
 
		}
 
	} else {
 
		LOG4CXX_ERROR(logger, "CURL not initialized!")
 
		strcpy(curl_errorbuffer, "CURL not initialized!");
 
	}
 
	LOG4CXX_ERROR(logger, "Error fetching " << url);
 
	return false;
 
}
 

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

	
 
	if(json.Parse<0>(m_data.c_str()).HasParseError()) {
 
		LOG4CXX_ERROR(logger, "Error while parsing JSON");
 
        LOG4CXX_ERROR(logger, m_data);
 
		strcpy(curl_errorbuffer, "Error while parsing JSON");
 
		return false;
 
	}
 

	
 
	return true;
 
}
 

	
 
void HTTPRequest::run() {
 
	switch (m_type) {
 
		case Get:
 
			m_ok = GET(m_url, m_json);
 
			break;
 
	}
spectrum/src/frontends/slack/SlackRTM.cpp
Show inline comments
 
@@ -167,48 +167,58 @@ void SlackRTM::handlePayloadReceived(const std::string &payload) {
 

	
 
void SlackRTM::sendMessage(const std::string &channel, const std::string &message) {
 
	m_counter++;
 

	
 
	std::string m = message;
 
	boost::replace_all(m, "\"", "\\\"");
 
	std::string msg = "{\"id\": " + boost::lexical_cast<std::string>(m_counter) + ", \"type\": \"message\", \"channel\":\"" + channel + "\", \"text\":\"" + m + "\"}";
 
	m_client->write(msg);
 
}
 

	
 
void SlackRTM::sendPing() {
 
	m_counter++;
 
	std::string msg = "{\"id\": " + boost::lexical_cast<std::string>(m_counter) + ", \"type\": \"ping\"}";
 
	m_client->write(msg);
 
	m_pingTimer->start();
 
}
 

	
 
void SlackRTM::handleRTMStart(HTTPRequest *req, bool ok, rapidjson::Document &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") {
 
			LOG4CXX_INFO(logger, "Account inactive, will not try connecting again");
 
			m_pingTimer->stop();
 
			m_client->disconnectServer();
 
			return;
 
		}
 
	}
 

	
 
	rapidjson::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()) {
 
		LOG4CXX_ERROR(logger, "No 'self' object in the reply.");
 
		LOG4CXX_ERROR(logger, data);
 
		return;
 
	}
 

	
 
	rapidjson::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());
 

	
 
	rapidjson::Value &selfId = self["id"];
0 comments (0 inline, 0 general)