From 638fdb5d710a10ef611f518303823c5b005ce3e4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Robert=20P=C3=B6sel?= <robyer@seznam.cz>
Date: Sat, 14 Feb 2015 16:30:24 +0000
Subject: Facebook: Fix (hopefully) for not receiving messages sometimes; some
 tiny other fixes

This ancient "bug" was here since beginning of Facebook RM. But it's not a bug, it just Facebook return wrong sequence number which then causes this.

git-svn-id: http://svn.miranda-ng.org/main/trunk@12103 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
---
 protocols/FacebookRM/src/communication.cpp | 32 ++++++++++++++++++++++++------
 protocols/FacebookRM/src/process.cpp       |  3 ++-
 protocols/FacebookRM/src/utils.cpp         |  2 +-
 3 files changed, 29 insertions(+), 8 deletions(-)

(limited to 'protocols')

diff --git a/protocols/FacebookRM/src/communication.cpp b/protocols/FacebookRM/src/communication.cpp
index 27e8009f18..c9f9bfd9e5 100644
--- a/protocols/FacebookRM/src/communication.cpp
+++ b/protocols/FacebookRM/src/communication.cpp
@@ -960,17 +960,17 @@ bool facebook_client::login(const char *username, const char *password)
 	case HTTP_CODE_FOUND: // Found and redirected somewhere
 	{
 		if (resp.headers.find("Location") != resp.headers.end()) {
-			std::string redirectUrl = resp.headers["location"];
+			std::string redirectUrl = resp.headers["Location"];
 			std::string expectedUrl = (this->https_ ? "https://"FACEBOOK_SERVER_REGULAR"/" : "http://"FACEBOOK_SERVER_REGULAR"/");
 
 			// Remove eventual parameters
 			std::string::size_type pos = redirectUrl.rfind("?");
-			if (pos != std::tstring::npos)
+			if (pos != std::string::npos)
 				redirectUrl = redirectUrl.substr(0, pos);
 			
 			if (redirectUrl != expectedUrl) {
 				// Unexpected redirect, but we try to ignore it - maybe we were logged in anyway
-				parent->debugLogA(" ! !  Login error: Unexpected redirect: %s", resp.headers["Location"].c_str());
+				parent->debugLogA(" ! !  Login error: Unexpected redirect: %s (Original: %s) (Expected: %s)", redirectUrl.c_str(), resp.headers["Location"].c_str(), expectedUrl.c_str());
 			}
 		}
 
@@ -1213,9 +1213,29 @@ bool facebook_client::channel()
 		std::string* response_data = new std::string(resp.data);
 		parent->ForkThread(&FacebookProto::ProcessMessages, response_data);
 
-		// Increment sequence number
-		this->chat_sequence_num_ = utils::text::source_get_value2(&resp.data, "\"seq\":", ",}");
-		parent->debugLogA("      Got self sequence number: %s", this->chat_sequence_num_.c_str());
+		// Get new sequence number
+		std::string seq = utils::text::source_get_value2(&resp.data, "\"seq\":", ",}");
+		parent->debugLogA("      Got self sequence number: %s", seq.c_str());
+
+		// Check if it's different from our old one (which means we should increment our old one)
+		if (seq != this->chat_sequence_num_) {
+			// Facebook now often return much bigger number which results in skipping few data requests, so we increment it manually
+			// Bigger skips (when there is some problem or something) are handled when fullreload/refresh response type
+			int iseq = 0;
+			if (utils::conversion::from_string<int>(iseq, this->chat_sequence_num_, std::dec)) {
+				// Increment and convert it back to string
+				iseq++;
+				std::string newSeq = utils::conversion::to_string(&iseq, UTILS_CONV_SIGNED_NUMBER);
+
+				// Check if we have different seq than the one from Facebook
+				if (newSeq != seq) {
+					parent->debugLogA("! ! ! Use self incremented sequence number: %s (instead of: %s)", newSeq.c_str(), seq.c_str());
+					seq = newSeq;
+				}
+			}
+		}
+
+		this->chat_sequence_num_ = seq;
 	}
 	else {
 		// No type? This shouldn't happen unless there is a big API change.
diff --git a/protocols/FacebookRM/src/process.cpp b/protocols/FacebookRM/src/process.cpp
index e6c2e6a1ac..cb2e5ab9de 100644
--- a/protocols/FacebookRM/src/process.cpp
+++ b/protocols/FacebookRM/src/process.cpp
@@ -891,7 +891,8 @@ void FacebookProto::ShowNotifications() {
 		if (notification != NULL && !notification->seen) {
 			debugLogA("      Showing popup for notification: %s", notification->text.c_str());
 			ptrT szText(mir_utf8decodeT(notification->text.c_str()));
-			notification->hWndPopup = NotifyEvent(m_tszUserName, szText, ContactIDToHContact(notification->user_id), FACEBOOK_EVENT_NOTIFICATION, &notification->link, &notification->id);
+			MCONTACT hContact = (notification->user_id.empty() ? NULL : ContactIDToHContact(notification->user_id));
+			notification->hWndPopup = NotifyEvent(m_tszUserName, szText, hContact, FACEBOOK_EVENT_NOTIFICATION, &notification->link, &notification->id);
 			notification->seen = true;
 		}
 	}
diff --git a/protocols/FacebookRM/src/utils.cpp b/protocols/FacebookRM/src/utils.cpp
index 67f96f060b..8c388f738c 100644
--- a/protocols/FacebookRM/src/utils.cpp
+++ b/protocols/FacebookRM/src/utils.cpp
@@ -56,7 +56,7 @@ std::string utils::time::mili_timestamp()
 
 time_t utils::time::from_string(const std::string &data)
 {
-	long long timestamp = _atoi64(data.c_str());
+	long long timestamp = _strtoi64(data.c_str(), NULL, 10);
 
 	// If it is milli timestamp
 	if (timestamp > 100000000000)
-- 
cgit v1.2.3