Changeset - 84814a1427db
[Not reviewed]
0 2 0
vitalyster - 9 years ago 2016-05-31 15:40:49
vitalyster@gmail.com
libpurple: store OAuth token for prpl-hangouts
2 files changed with 57 insertions and 4 deletions:
0 comments (0 inline, 0 general)
backends/libpurple/CMakeLists.txt
Show inline comments
 
cmake_minimum_required(VERSION 2.6)
 
FILE(GLOB SRC *.cpp)
 

	
 
ADD_EXECUTABLE(spectrum2_libpurple_backend ${SRC})
 

	
 
if(MSVC)
 
target_link_libraries(spectrum2_libpurple_backend sqlite3 ${PURPLE_LIBRARY} ${GLIB2_LIBRARIES} ${LIBXML2_LIBRARIES} ${EVENT_LIBRARIES} transport-plugin ${PROTOBUF_LIBRARY})
 
target_link_libraries(spectrum2_libpurple_backend transport ${PURPLE_LIBRARY} ${GLIB2_LIBRARIES} ${LIBXML2_LIBRARIES} ${EVENT_LIBRARIES} transport-plugin ${PROTOBUF_LIBRARY})
 
else()
 
	if (NOT WIN32)
 
	target_link_libraries(spectrum2_libpurple_backend ${PURPLE_LIBRARY} ${GLIB2_LIBRARIES} ${EVENT_LIBRARIES} transport-plugin pthread)
 
	target_link_libraries(spectrum2_libpurple_backend transport ${PURPLE_LIBRARY} ${GLIB2_LIBRARIES} ${EVENT_LIBRARIES} transport-plugin pthread)
 
	else()
 
	target_link_libraries(spectrum2_libpurple_backend ${PURPLE_LIBRARY} ${GLIB2_LIBRARIES} ${EVENT_LIBRARIES} transport-plugin)
 
	target_link_libraries(spectrum2_libpurple_backend transport ${PURPLE_LIBRARY} ${GLIB2_LIBRARIES} ${EVENT_LIBRARIES} transport-plugin)
 
	endif()
 
endif()
 

	
 
INSTALL(TARGETS spectrum2_libpurple_backend RUNTIME DESTINATION bin)
 

	
backends/libpurple/main.cpp
Show inline comments
 
@@ -11,12 +11,13 @@
 
#include <iostream>
 
#include <fstream> 
 

	
 
#include "transport/NetworkPlugin.h"
 
#include "transport/Logging.h"
 
#include "transport/Config.h"
 
#include "transport/StorageBackend.h"
 
#include "geventloop.h"
 

	
 
// #include "valgrind/memcheck.h"
 
#if !defined(__FreeBSD__) && !defined(__APPLE__)
 
#include "malloc.h"
 
#endif
 
@@ -78,12 +79,13 @@ static std::vector<std::string> split(const std::string &s, char delim) {
 
static void transportDataReceived(gpointer data, gint source, PurpleInputCondition cond);
 

	
 
class SpectrumNetworkPlugin;
 

	
 
boost::shared_ptr<Config> config;
 
SpectrumNetworkPlugin *np;
 
StorageBackend *storagebackend;
 

	
 
static std::string host;
 
static int port = 10000;
 

	
 
struct FTData {
 
	unsigned long id;
 
@@ -175,12 +177,41 @@ static std::string getAlias(PurpleBuddy *m_buddy) {
 
	else {
 
		alias = (std::string) purple_buddy_get_server_alias_wrapped(m_buddy);
 
	}
 
	return alias;
 
}
 

	
 
static boost::mutex dblock;
 
static std::string OAUTH_TOKEN = "hangouts_oauth_token";
 

	
 
static bool getUserOAuthToken(const std::string user, std::string &token)
 
{
 
  boost::mutex::scoped_lock lock(dblock);
 
  UserInfo info;
 
  if(storagebackend->getUser(user, info) == false) {
 
    LOG4CXX_ERROR(logger, "Didn't find entry for " << user << " in the database!")
 
      return false;
 
  }
 
  token = "";
 
  int type = TYPE_STRING;
 
  storagebackend->getUserSetting((long)info.id, OAUTH_TOKEN, type, token);
 
  return true;
 
}
 

	
 
static bool storeUserOAuthToken(const std::string user, const std::string OAuthToken)
 
{
 
  boost::mutex::scoped_lock lock(dblock);
 
  UserInfo info;
 
  if(storagebackend->getUser(user, info) == false) {
 
    LOG4CXX_ERROR(logger, "Didn't find entry for " << user << " in the database!")
 
      return false;
 
  }
 
  storagebackend->updateUserSetting((long)info.id, OAUTH_TOKEN, OAuthToken);
 
  return true;
 
}     
 

	
 
class SpectrumNetworkPlugin : public NetworkPlugin {
 
	public:
 
		SpectrumNetworkPlugin() : NetworkPlugin() {
 

	
 
		}
 

	
 
@@ -364,12 +395,18 @@ class SpectrumNetworkPlugin : public NetworkPlugin {
 
			setDefaultAvatar(account, legacyName);
 

	
 
			purple_account_set_password_wrapped(account, password.c_str());
 
			purple_account_set_bool_wrapped(account, "custom_smileys", FALSE);
 
			purple_account_set_bool_wrapped(account, "direct_connect", FALSE);
 
			purple_account_set_bool_wrapped(account, "compat-verification", TRUE);
 
			if (protocol == "prpl-hangouts") {
 
				std::string token;
 
				if (getUserOAuthToken(user, token)) {
 
					purple_account_set_password_wrapped(account, token.c_str());
 
				}
 
			}
 

	
 
			setDefaultAccountOptions(account);
 

	
 
			// Enable account + privacy lists
 
			purple_account_set_enabled_wrapped(account, "spectrum", TRUE);
 

	
 
@@ -1535,13 +1572,13 @@ void * requestInput(const char *title, const char *primary,const char *secondary
 
            ((PurpleRequestInputCb) ok_cb)(user_data, "Authorization denied.");
 
            return NULL;
 
        }
 
        else if (boost::starts_with(primaryString, "https://accounts.google.com/o/oauth2/auth")) {
 
            LOG4CXX_INFO(logger, "prpl-hangouts oauth request");
 
            np->handleMessage(np->m_accounts[account], np->adminLegacyName, std::string("Please visit the following link and authorize this application: ") + primaryString, "");
 
            np->handleMessage(np->m_accounts[account], np->adminLegacyName, std::string("Reply with code provided by Google: ") + primaryString, "");
 
            np->handleMessage(np->m_accounts[account], np->adminLegacyName, std::string("Reply with code provided by Google: ");
 
            inputRequest *req = new inputRequest;
 
            req->ok_cb = (PurpleRequestInputCb)ok_cb;
 
            req->user_data = user_data;
 
            req->account = account;
 
            req->mainJID = np->m_accounts[account];
 
            np->m_inputRequests[req->mainJID] = req;
 
@@ -1964,12 +2001,15 @@ static void signed_on(PurpleConnection *gc, gpointer unused) {
 
#endif
 
#endif
 
	purple_roomlist_get_list_wrapped(gc);
 
	
 
	// For prpl-gg
 
	execute_purple_plugin_action(gc, "Download buddylist from Server");
 
	if (CONFIG_STRING(config, "service.protocol") == "prpl-hangouts") {
 
		storeUserOAuthToken(np->m_accounts[account], purple_account_get_password_wrapped(account));
 
	}
 
}
 

	
 
static void printDebug(PurpleDebugLevel level, const char *category, const char *arg_s) {
 
	std::string c("");
 
	std::string args(arg_s);
 
	args.erase(args.size() - 1);
 
@@ -2190,12 +2230,25 @@ int main(int argc, char **argv) {
 
		return 1;
 
	}
 

	
 
	config = boost::shared_ptr<Config>(cfg);
 
 
 
	Logging::initBackendLogging(config.get());
 
	if (CONFIG_STRING(config, "service.protocol") == "prpl-hangouts") {
 
		storagebackend = StorageBackend::createBackend(config.get(), error);
 
		if (storagebackend == NULL) {
 
			LOG4CXX_ERROR(logger, "Error creating StorageBackend! " << error);
 
			LOG4CXX_ERROR(logger, "Hangouts backend needs storage backend configured to work! " << error);
 
			return NetworkPlugin::StorageBackendNeeded;
 
		}
 
		else if (!storagebackend->connect()) {
 
			LOG4CXX_ERROR(logger, "Can't connect to database!")
 
				return -1;
 
		}
 
	}
 

	
 
	initPurple();
 
 
 
	main_socket = create_socket(host.c_str(), port);
 
	purple_input_add_wrapped(main_socket, PURPLE_INPUT_READ, &transportDataReceived, NULL);
 
	purple_timeout_add_seconds_wrapped(30, pingTimeout, NULL);
 
 
0 comments (0 inline, 0 general)