Changeset - a06b8a919721
[Not reviewed]
0 1 0
vitalyster - 9 years ago 2016-05-25 02:31:00
vitalyster@gmail.com
libpurple: fix oauth sender
1 file changed with 4 insertions and 2 deletions:
0 comments (0 inline, 0 general)
backends/libpurple/main.cpp
Show inline comments
 
@@ -238,245 +238,247 @@ class SpectrumNetworkPlugin : public NetworkPlugin {
 
					PurplePrefType type = purple_account_option_get_type_wrapped(option);
 
					std::string key2(purple_account_option_get_setting_wrapped(option));
 
					if (strippedKey != key2) {
 
						continue;
 
					}
 
					
 
					found = true;
 
					switch (type) {
 
						case PURPLE_PREF_BOOLEAN:
 
							purple_account_set_bool_wrapped(account, strippedKey.c_str(), fromString<bool>(keyItem.second.as<std::string>()));
 
							break;
 

	
 
						case PURPLE_PREF_INT:
 
							purple_account_set_int_wrapped(account, strippedKey.c_str(), fromString<int>(keyItem.second.as<std::string>()));
 
							break;
 

	
 
						case PURPLE_PREF_STRING:
 
						case PURPLE_PREF_STRING_LIST:
 
							purple_account_set_string_wrapped(account, strippedKey.c_str(), keyItem.second.as<std::string>().c_str());
 
							break;
 
						default:
 
							continue;
 
					}
 
					break;
 
				}
 

	
 
				if (!found) {
 
					purple_account_set_string_wrapped(account, strippedKey.c_str(), keyItem.second.as<std::string>().c_str());
 
				}
 
				i++;
 
			}
 

	
 
			char* contents;
 
			gsize length;
 
			gboolean ret = g_file_get_contents ("gfire.cfg", &contents, &length, NULL);
 
			if (ret) {
 
				purple_account_set_int_wrapped(account, "version", fromString<int>(std::string(contents, length)));
 
			}
 

	
 

	
 
			if (CONFIG_STRING(config, "service.protocol") == "prpl-novell") {
 
				std::string username(purple_account_get_username_wrapped(account));
 
				std::vector <std::string> u = split(username, '@');
 
				purple_account_set_username_wrapped(account, (const char*) u.front().c_str());
 
				std::vector <std::string> s = split(u.back(), ':'); 
 
				purple_account_set_string_wrapped(account, "server", s.front().c_str());
 
				purple_account_set_int_wrapped(account, "port", atoi(s.back().c_str()));  
 
			}
 

	
 
			if (!CONFIG_STRING_DEFAULTED(config, "proxy.type", "").empty()) {
 
				PurpleProxyInfo *info = purple_proxy_info_new_wrapped();
 
				if (CONFIG_STRING_DEFAULTED(config, "proxy.type", "") == "http") {
 
					purple_proxy_info_set_type_wrapped(info, PURPLE_PROXY_HTTP);
 
				}
 
				else if (CONFIG_STRING_DEFAULTED(config, "proxy.type", "") == "socks4") {
 
					purple_proxy_info_set_type_wrapped(info, PURPLE_PROXY_SOCKS4);
 
				}
 
				else if (CONFIG_STRING_DEFAULTED(config, "proxy.type", "") == "socks5") {
 
					purple_proxy_info_set_type_wrapped(info, PURPLE_PROXY_SOCKS5);
 
				}
 
				else {
 
					LOG4CXX_ERROR(logger, "Unknown proxy.type " << CONFIG_STRING_DEFAULTED(config, "proxy.type", ""));
 
				}
 

	
 
				info->username = NULL;
 
				info->password = NULL;
 

	
 
				purple_proxy_info_set_type_wrapped(info, PURPLE_PROXY_SOCKS5);
 
				purple_proxy_info_set_host_wrapped(info, CONFIG_STRING_DEFAULTED(config, "proxy.host", "").c_str());
 
				if (CONFIG_INT_DEFAULTED(config, "proxy.port", 0)) {
 
					purple_proxy_info_set_port_wrapped(info, CONFIG_INT_DEFAULTED(config, "proxy.port", 0));
 
				}
 
				if (!CONFIG_STRING_DEFAULTED(config, "proxy.username", "").empty()) {
 
					purple_proxy_info_set_username_wrapped(info, CONFIG_STRING_DEFAULTED(config, "proxy.username", "").c_str());
 
				}
 

	
 
				if (!CONFIG_STRING_DEFAULTED(config, "proxy.password", "").empty()) {
 
					purple_proxy_info_set_password_wrapped(info, CONFIG_STRING_DEFAULTED(config, "proxy.password", "").c_str());
 
				}
 

	
 
				purple_account_set_proxy_info_wrapped(account, info);
 
			}
 
		}
 

	
 
		void handleLoginRequest(const std::string &user, const std::string &legacyName, const std::string &password) {
 
			PurpleAccount *account = NULL;
 

	
 
			std::string name;
 
			std::string protocol;
 
			getProtocolAndName(legacyName, name, protocol);
 

	
 
			if (password.empty() && protocol != "prpl-telegram" && protocol != "prpl-hangouts") {
 
				LOG4CXX_INFO(logger,  name.c_str() << ": Empty password");
 
				np->handleDisconnected(user, 1, "Empty password.");
 
				return;
 
			}
 
			if (protocol == "prpl-hangouts") {
 
			        adminLegacyName = "hangouts";
 
			        adminAlias = "hangouts";
 
			}
 

	
 
			if (!purple_find_prpl_wrapped(protocol.c_str())) {
 
				LOG4CXX_INFO(logger,  name.c_str() << ": Invalid protocol '" << protocol << "'");
 
				np->handleDisconnected(user, 1, "Invalid protocol " + protocol);
 
				return;
 
			}
 

	
 
			if (purple_accounts_find_wrapped(name.c_str(), protocol.c_str()) != NULL) {
 
				account = purple_accounts_find_wrapped(name.c_str(), protocol.c_str());
 
				if (m_accounts.find(account) != m_accounts.end() && m_accounts[account] != user) {
 
					LOG4CXX_INFO(logger, "Account '" << name << "' is already used by '" << m_accounts[account] << "'");
 
					np->handleDisconnected(user, 1, "Account '" + name + "' is already used by '" + m_accounts[account] + "'");
 
					return;
 
				}
 
				LOG4CXX_INFO(logger, "Using previously created account with name '" << name.c_str() << "' and protocol '" << protocol << "'");
 
			}
 
			else {
 
				LOG4CXX_INFO(logger, "Creating account with name '" << name.c_str() << "' and protocol '" << protocol << "'");
 
				account = purple_account_new_wrapped(name.c_str(), protocol.c_str());
 
				purple_accounts_add_wrapped(account);
 
			}
 

	
 
			m_sessions[user] = account;
 
			m_accounts[account] = user;
 

	
 
			// Default avatar
 
			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);
 

	
 
			setDefaultAccountOptions(account);
 

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

	
 
#if PURPLE_MAJOR_VERSION >= 2 && PURPLE_MINOR_VERSION >= 7
 
			if (CONFIG_BOOL(config, "service.enable_privacy_lists")) {
 
				purple_account_set_privacy_type_wrapped(account, PURPLE_PRIVACY_DENY_USERS);
 
			}
 
#endif
 

	
 
			// Set the status
 
			const PurpleStatusType *status_type = purple_account_get_status_type_with_primitive_wrapped(account, PURPLE_STATUS_AVAILABLE);
 
			if (status_type != NULL) {
 
				purple_account_set_status_wrapped(account, purple_status_type_get_id_wrapped(status_type), TRUE, NULL);
 
			}
 
			// OAuth helper
 
			if (protocol == "prpl-hangouts") {
 
				adminLegacyName = "hangouts";
 
				adminAlias = "hangouts";
 
				LOG4CXX_INFO(logger, user << ": Adding Buddy " << adminLegacyName << " " << adminAlias)
 
				handleBuddyChanged(user, adminLegacyName, adminAlias, std::vector<std::string>(), pbnetwork::STATUS_ONLINE);
 
			}
 
		}
 

	
 
		void handleLogoutRequest(const std::string &user, const std::string &legacyName) {
 
			PurpleAccount *account = m_sessions[user];
 
			if (account) {
 
				if (account->ui_data) {
 
					NodeCache *cache = (NodeCache *) account->ui_data;
 
					purple_timeout_remove_wrapped(cache->timer);
 
					delete cache;
 
					account->ui_data = NULL;
 
				}
 
				if (purple_account_get_int_wrapped(account, "version", 0) != 0) {
 
					std::string data = stringOf(purple_account_get_int_wrapped(account, "version", 0));
 
					g_file_set_contents ("gfire.cfg", data.c_str(), data.size(), NULL);
 
				}
 
// 				VALGRIND_DO_LEAK_CHECK;
 
				m_sessions.erase(user);
 
				purple_account_disconnect_wrapped(account);
 
				purple_account_set_enabled_wrapped(account, "spectrum", FALSE);
 

	
 
				m_accounts.erase(account);
 

	
 
				purple_accounts_delete_wrapped(account);
 
#ifndef WIN32
 
#if !defined(__FreeBSD__) && !defined(__APPLE__)
 
				malloc_trim(0);
 
#endif
 
#endif
 
// 				VALGRIND_DO_LEAK_CHECK;
 
			}
 
		}
 

	
 
		void handleStatusChangeRequest(const std::string &user, int status, const std::string &statusMessage) {
 
			PurpleAccount *account = m_sessions[user];
 
			if (account) {
 
				int st;
 
				switch(status) {
 
					case pbnetwork::STATUS_AWAY: {
 
						st = PURPLE_STATUS_AWAY;
 
						if (!purple_account_get_status_type_with_primitive_wrapped(account, PURPLE_STATUS_AWAY))
 
							st = PURPLE_STATUS_EXTENDED_AWAY;
 
						else
 
							st = PURPLE_STATUS_AWAY;
 
						break;
 
					}
 
					case pbnetwork::STATUS_DND: {
 
						st = PURPLE_STATUS_UNAVAILABLE;
 
						break;
 
					}
 
					case pbnetwork::STATUS_XA: {
 
						if (!purple_account_get_status_type_with_primitive_wrapped(account, PURPLE_STATUS_EXTENDED_AWAY))
 
							st = PURPLE_STATUS_AWAY;
 
						else
 
							st = PURPLE_STATUS_EXTENDED_AWAY;
 
						break;
 
					}
 
					case pbnetwork::STATUS_NONE: {
 
						st = PURPLE_STATUS_OFFLINE;
 
						break;
 
					}
 
					case pbnetwork::STATUS_INVISIBLE:
 
						st = PURPLE_STATUS_INVISIBLE;
 
						break;
 
					default:
 
						st = PURPLE_STATUS_AVAILABLE;
 
						break;
 
				}
 
				gchar *_markup = purple_markup_escape_text_wrapped(statusMessage.c_str(), -1);
 
				std::string markup(_markup);
 
				g_free(_markup);
 

	
 
				// we are already connected so we have to change status
 
				const PurpleStatusType *status_type = purple_account_get_status_type_with_primitive_wrapped(account, (PurpleStatusPrimitive) st);
 
				if (status_type != NULL) {
 
					// send presence to legacy network
 
					if (!markup.empty()) {
 
						purple_account_set_status_wrapped(account, purple_status_type_get_id_wrapped(status_type), TRUE, "message", markup.c_str(), NULL);
 
					}
 
					else {
 
						purple_account_set_status_wrapped(account, purple_status_type_get_id_wrapped(status_type), TRUE, NULL);
 
					}
 
				}
 
			}
 
		}
 

	
 
		void handleMessageSendRequest(const std::string &user, const std::string &legacyName, const std::string &message, const std::string &xhtml, const std::string &id) {
 
			PurpleAccount *account = m_sessions[user];
 
			if (account) {
 
				LOG4CXX_INFO(logger, "Sending message to '" << legacyName << "'");
 
				PurpleConversation *conv = purple_find_conversation_with_account_wrapped(PURPLE_CONV_TYPE_CHAT, legacyName.c_str(), account);
 
	                	if (legacyName == adminLegacyName) {
 
					// expect OAuth code
 
					if (m_inputRequests.find(user) != m_inputRequests.end()) {
0 comments (0 inline, 0 general)