/*
Facebook plugin for Miranda Instant Messenger
_____________________________________________
Copyright � 2009-11 Michal Zelinka, 2011-16 Robert P�sel
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
*/
#include "stdafx.h"
/**
* Helper function for loading name from database (or use default one specified as parameter), used for title of few notifications.
*/
std::string getContactName(FacebookProto *proto, MCONTACT hContact, const char *defaultName)
{
std::string name = defaultName;
DBVARIANT dbv;
if (!proto->getStringUtf(hContact, FACEBOOK_KEY_NICK, &dbv)) {
name = dbv.pszVal;
db_free(&dbv);
}
return name;
}
void FacebookProto::ProcessFriendList(void*)
{
if (isOffline())
return;
facy.handle_entry("load_friends");
// Get friends list
std::string data = "__user=" + facy.self_.user_id;
data += "&__dyn=" + facy.__dyn();
data += "&__req=" + facy.__req();
data += "&fb_dtsg=" + facy.dtsg_;
data += "&ttstamp=" + facy.ttstamp_;
data += "&__rev=" + facy.__rev();
data += "&__pc=PHASED:DEFAULT&__be=-1&__a=1";
http::response resp = facy.flap(REQUEST_USER_INFO_ALL, &data); // NOTE: Request revised 17.8.2016
if (resp.code != HTTP_CODE_OK) {
facy.handle_error("load_friends");
return;
}
debugLogA("*** Starting processing friend list");
CODE_BLOCK_TRY
std::map friends;
facebook_json_parser* p = new facebook_json_parser(this);
p->parse_friends(&resp.data, &friends);
delete p;
// Check and update old contacts
for (MCONTACT hContact = db_find_first(m_szModuleName); hContact; hContact = db_find_next(hContact, m_szModuleName)) {
if (isChatRoom(hContact))
continue;
// TODO RM: change name of "Deleted" key to "DeletedTS", remove this code in some next version
int deletedTS = getDword(hContact, "Deleted", 0);
if (deletedTS != 0) {
delSetting(hContact, "Deleted");
setDword(hContact, FACEBOOK_KEY_DELETED, deletedTS);
}
facebook_user *fbu;
ptrA id(getStringA(hContact, FACEBOOK_KEY_ID));
if (id != NULL) {
std::map< std::string, facebook_user* >::iterator iter;
if ((iter = friends.find(std::string(id))) != friends.end()) {
// Found contact, update it and remove from map
fbu = iter->second;
// TODO RM: remove, because contacts cant change it, so its only for "first run"
// - but what with contacts, that was added after logon?
// Update gender
if (getByte(hContact, "Gender", 0) != (int)fbu->gender)
setByte(hContact, "Gender", fbu->gender);
// TODO: remove this in some future version?
// Remove old useless "RealName" field
ptrA realname(getStringA(hContact, "RealName"));
if (realname != NULL) {
delSetting(hContact, "RealName");
}
// Update real name and nick
if (!fbu->real_name.empty()) {
SaveName(hContact, fbu);
}
// Update username
ptrA username(getStringA(hContact, FACEBOOK_KEY_USERNAME));
if (!username || mir_strcmp(username, fbu->username.c_str())) {
if (!fbu->username.empty())
setString(hContact, FACEBOOK_KEY_USERNAME, fbu->username.c_str());
else
delSetting(hContact, FACEBOOK_KEY_USERNAME);
}
// Update contact type
if (getByte(hContact, FACEBOOK_KEY_CONTACT_TYPE) != fbu->type) {
setByte(hContact, FACEBOOK_KEY_CONTACT_TYPE, fbu->type);
// TODO: remove that popup and use "Contact added you" event?
}
// Wasn't contact removed from "server-list" someday?
if (getDword(hContact, FACEBOOK_KEY_DELETED, 0)) {
delSetting(hContact, FACEBOOK_KEY_DELETED);
// Notify it, if user wants to be notified
if (getByte(FACEBOOK_KEY_EVENT_FRIENDSHIP_ENABLE, DEFAULT_EVENT_FRIENDSHIP_ENABLE)) {
std::string url = FACEBOOK_URL_PROFILE + fbu->user_id;
std::string contactname = getContactName(this, hContact, !fbu->real_name.empty() ? fbu->real_name.c_str() : fbu->user_id.c_str());
ptrW szTitle(mir_utf8decodeW(contactname.c_str()));
NotifyEvent(szTitle, TranslateT("Contact is back on server-list."), hContact, FACEBOOK_EVENT_FRIENDSHIP, &url);
}
}
// Check avatar change
CheckAvatarChange(hContact, fbu->image_url);
// Mark this contact as deleted ("processed") and delete them later (as there may be some duplicit contacts to use)
fbu->deleted = true;
}
else {
// Contact was removed from "server-list", notify it
// Wasn't we already been notified about this contact? And was this real friend?
if (!getDword(hContact, FACEBOOK_KEY_DELETED, 0) && getByte(hContact, FACEBOOK_KEY_CONTACT_TYPE, 0) == CONTACT_FRIEND) {
setDword(hContact, FACEBOOK_KEY_DELETED, ::time(NULL));
setByte(hContact, FACEBOOK_KEY_CONTACT_TYPE, CONTACT_NONE);
// Notify it, if user wants to be notified
if (getByte(FACEBOOK_KEY_EVENT_FRIENDSHIP_ENABLE, DEFAULT_EVENT_FRIENDSHIP_ENABLE)) {
std::string url = FACEBOOK_URL_PROFILE + std::string(id);
std::string contactname = getContactName(this, hContact, id);
ptrW szTitle(mir_utf8decodeW(contactname.c_str()));
NotifyEvent(szTitle, TranslateT("Contact is no longer on server-list."), hContact, FACEBOOK_EVENT_FRIENDSHIP, &url);
}
}
}
}
}
// Check remaining contacts in map and add them to contact list
for (std::map< std::string, facebook_user* >::iterator it = friends.begin(); it != friends.end();) {
if (!it->second->deleted)
AddToContactList(it->second, true); // we know this contact doesn't exists, so we force add it
delete it->second;
it = friends.erase(it);
}
friends.clear();
debugLogA("*** Friend list processed");
CODE_BLOCK_CATCH
debugLogA("*** Error processing friend list: %s", e.what());
CODE_BLOCK_END
}
void FacebookProto::ProcessUnreadMessages(void*)
{
if (isOffline())
return;
facy.handle_entry("ProcessUnreadMessages");
std::string data = "folders[0]=inbox&folders[1]=other"; // TODO: "other" is probably unused, and there is now "pending" instead
data += "&client=mercury";
data += "&__user=" + facy.self_.user_id;
data += "&fb_dtsg=" + facy.dtsg_;
data += "&__a=1&__dyn=&__req=&ttstamp=" + facy.ttstamp_;
data += "&__rev=" + facy.__rev();
data += "&__pc=PHASED:DEFAULT&__be=-1";
http::response resp = facy.flap(REQUEST_UNREAD_THREADS, &data);
if (resp.code != HTTP_CODE_OK) {
facy.handle_error("ProcessUnreadMessages");
return;
}
CODE_BLOCK_TRY
std::vector threads;
facebook_json_parser* p = new facebook_json_parser(this);
p->parse_unread_threads(&resp.data, &threads);
delete p;
ForkThread(&FacebookProto::ProcessUnreadMessage, new std::vector(threads));
debugLogA("*** Unread threads list processed");
CODE_BLOCK_CATCH
debugLogA("*** Error processing unread threads list: %s", e.what());
CODE_BLOCK_END
facy.handle_success("ProcessUnreadMessages");
}
void FacebookProto::ProcessUnreadMessage(void *pParam)
{
if (pParam == NULL)
return;
std::vector *threads = (std::vector*)pParam;
if (isOffline()) {
delete threads;
return;
}
facy.handle_entry("ProcessUnreadMessage");
int offset = 0;
int limit = 21;
http::response resp;
// TODO: First load info about amount of unread messages, then load exactly this amount for each thread
while (!threads->empty()) {
std::string data = "client=mercury";
data += "&__user=" + facy.self_.user_id;
data += "&__dyn=" + facy.__dyn();
data += "&__req=" + facy.__req();
data += "&fb_dtsg=" + facy.dtsg_;
data += "&ttstamp=" + facy.ttstamp_;
data += "&__rev=" + facy.__rev();
data += "&__pc=PHASED:DEFAULT&__be=-1&__a=1";
for (std::vector::size_type i = 0; i < threads->size(); i++) {
std::string thread_id = utils::url::encode(threads->at(i));
// request messages from thread
data += "&messages[thread_ids][" + thread_id;
data += "][offset]=" + utils::conversion::to_string(&offset, UTILS_CONV_SIGNED_NUMBER);
data += "&messages[thread_ids][" + thread_id;
data += "][limit]=" + utils::conversion::to_string(&limit, UTILS_CONV_SIGNED_NUMBER);
// request info about thread
data += "&threads[thread_ids][" + utils::conversion::to_string(&i, UTILS_CONV_UNSIGNED_NUMBER);
data += "]=" + thread_id;
}
resp = facy.flap(REQUEST_THREAD_INFO, &data); // NOTE: Request revised 17.8.2016
if (resp.code == HTTP_CODE_OK) {
CODE_BLOCK_TRY
std::vector messages;
std::map chatrooms;
facebook_json_parser* p = new facebook_json_parser(this);
p->parse_thread_messages(&resp.data, &messages, &chatrooms, false);
delete p;
for (std::map::iterator it = chatrooms.begin(); it != chatrooms.end();) {
// TODO: refactor this too!
// TODO: have all chatrooms in facy, in memory, and then handle them as needed... somehow think about it...
/* facebook_chatroom *room = it->second;
MCONTACT hChatContact = NULL;
ptrA users(GetChatUsers(room->thread_id.c_str()));
if (users == NULL) {
AddChat(room->thread_id.c_str(), room->chat_name.c_str());
hChatContact = ChatIDToHContact(room->thread_id);
// Set thread id (TID) for later
setWString(hChatContact, FACEBOOK_KEY_TID, room->thread_id.c_str());
for (std::map::iterator jt = room->participants.begin(); jt != room->participants.end(); ) {
AddChatContact(room->thread_id.c_str(), jt->first.c_str(), jt->second.c_str());
++jt;
}
}
if (!hChatContact)
hChatContact = ChatIDToHContact(room->thread_id);
ForkThread(&FacebookProto::ReadMessageWorker, (void*)hChatContact);*/
delete it->second;
it = chatrooms.erase(it);
}
chatrooms.clear();
ReceiveMessages(messages, true);
debugLogA("*** Unread messages processed");
CODE_BLOCK_CATCH
debugLogA("*** Error processing unread messages: %s", e.what());
CODE_BLOCK_END
facy.handle_success("ProcessUnreadMessage");
}
else {
facy.handle_error("ProcessUnreadMessage");
}
offset += limit;
limit = 20; // TODO: use better limits?
threads->clear(); // TODO: if we have limit messages from one user, there may be more unread messages... continue with it... otherwise remove that threadd from threads list -- or do it in json parser? hm = allow more than "limit" unread messages to be parsed
}
delete threads;
}
void FacebookProto::LoadLastMessages(void *pParam)
{
if (pParam == NULL)
return;
MCONTACT hContact = *(MCONTACT*)pParam;
delete (MCONTACT*)pParam;
if (isOffline())
return;
facy.handle_entry("LoadLastMessages");
if (!isOnline())
return;
std::string data = "client=mercury";
data += "&__user=" + facy.self_.user_id;
data += "&__dyn=" + facy.__dyn();
data += "&__req=" + facy.__req();
data += "&fb_dtsg=" + facy.dtsg_;
data += "&ttstamp=" + facy.ttstamp_;
data += "&__rev=" + facy.__rev();
data += "&__pc=PHASED:DEFAULT&__be=-1&__a=1";
bool isChat = isChatRoom(hContact);
if (isChat && (!m_enableChat || IsSpecialChatRoom(hContact))) // disabled chats or special chatroom (e.g. nofitications)
return;
ptrA item_id(getStringA(hContact, isChat ? FACEBOOK_KEY_TID : FACEBOOK_KEY_ID));
if (item_id == NULL) {
debugLogA("!!! LoadLastMessages(): Contact has no TID/ID");
return;
}
std::string id = utils::url::encode(std::string(item_id));
std::string type = isChat ? "thread_ids" : "user_ids";
int count = getByte(FACEBOOK_KEY_MESSAGES_ON_OPEN_COUNT, DEFAULT_MESSAGES_ON_OPEN_COUNT);
count = min(count, FACEBOOK_MESSAGES_ON_OPEN_LIMIT);
// request messages from thread
data += "&messages[" + type + "][" + id;
data += "][offset]=0";
data += "&messages[" + type + "][" + id;
data += "][limit]=" + utils::conversion::to_string(&count, UTILS_CONV_UNSIGNED_NUMBER);
// request info about thread
data += "&threads[" + type + "][0]=" + id;
http::response resp = facy.flap(REQUEST_THREAD_INFO, &data); // NOTE: Request revised 17.8.2016
if (resp.code != HTTP_CODE_OK || resp.data.empty()) {
facy.handle_error("LoadLastMessages");
return;
}
// Temporarily disable marking messages as read for this contact
facy.ignore_read.insert(hContact);
CODE_BLOCK_TRY
std::vector messages;
std::map chatrooms;
facebook_json_parser* p = new facebook_json_parser(this);
p->parse_thread_messages(&resp.data, &messages, &chatrooms, false);
delete p;
// TODO: do something with this, chat is loading somewhere else... (in receiveMessages method right now)
/*for (std::map::iterator it = chatrooms.begin(); it != chatrooms.end();) {
facebook_chatroom *room = it->second;
MCONTACT hChatContact = NULL;
ptrA users(GetChatUsers(room->thread_id.c_str()));
if (users == NULL) {
AddChat(room->thread_id.c_str(), room->chat_name.c_str());
hChatContact = ChatIDToHContact(room->thread_id);
// Set thread id (TID) for later
setWString(hChatContact, FACEBOOK_KEY_TID, room->thread_id.c_str());
for (std::map::iterator jt = room->participants.begin(); jt != room->participants.end();) {
AddChatContact(room->thread_id.c_str(), jt->first.c_str(), jt->second.c_str());
++jt;
}
}
if (!hChatContact)
hChatContact = ChatIDToHContact(room->thread_id);
ForkThread(&FacebookProto::ReadMessageWorker, (void*)hChatContact);
delete it->second;
it = chatrooms.erase(it);
}
chatrooms.clear();*/
ReceiveMessages(messages, true);
debugLogA("*** Thread messages processed");
CODE_BLOCK_CATCH
debugLogA("*** Error processing thread messages: %s", e.what());
CODE_BLOCK_END
facy.handle_success("LoadLastMessages");
// Enable marking messages as read for this contact
facy.ignore_read.erase(hContact);
// And force mark read
OnDbEventRead(hContact, NULL);
}
void FacebookProto::LoadHistory(void *pParam)
{
if (pParam == NULL)
return;
MCONTACT hContact = *(MCONTACT*)pParam;
delete (MCONTACT*)pParam;
if (!isOnline())
return;
facy.handle_entry("LoadHistory");
std::string data = "client=mercury";
data += "&__user=" + facy.self_.user_id;
data += "&__dyn=" + facy.__dyn();
data += "&__req=" + facy.__req();
data += "&fb_dtsg=" + facy.dtsg_;
data += "&ttstamp=" + facy.ttstamp_;
data += "&__rev=" + facy.__rev();
data += "&__pc=PHASED:DEFAULT&__be=-1&__a=1";
bool isChat = isChatRoom(hContact);
if (isChat) // TODO: Support chats?
return;
/*if (isChat && (!m_enableChat || IsSpecialChatRoom(hContact))) // disabled chats or special chatroom (e.g. nofitications)
return;*/
ptrA item_id(getStringA(hContact, isChat ? FACEBOOK_KEY_TID : FACEBOOK_KEY_ID));
if (item_id == NULL) {
debugLogA("!!! LoadHistory(): Contact has no TID/ID");
return;
}
std::string id = utils::url::encode(std::string(item_id));
std::string type = isChat ? "thread_ids" : "user_ids";
// first get info about this thread and how many messages is there
// request info about thread
data += "&threads[" + type + "][0]=" + id;
http::response resp = facy.flap(REQUEST_THREAD_INFO, &data); // NOTE: Request revised 17.8.2016
if (resp.code != HTTP_CODE_OK || resp.data.empty()) {
facy.handle_error("LoadHistory");
return;
}
int messagesCount = -1;
int unreadCount = -1;
facebook_json_parser* p = new facebook_json_parser(this);
if (p->parse_messages_count(&resp.data, &messagesCount, &unreadCount) == EXIT_FAILURE) {
delete p;
facy.handle_error("LoadHistory");
return;
}
// Temporarily disable marking messages as read for this contact
facy.ignore_read.insert(hContact);
POPUPDATAW pd = { sizeof(pd) };
pd.iSeconds = 5;
pd.lchContact = hContact;
pd.lchIcon = IcoLib_GetIconByHandle(GetIconHandle("conversation")); // TODO: Use better icon
wcsncpy(pd.lptzContactName, m_tszUserName, MAX_CONTACTNAME);
wcsncpy(pd.lptzText, TranslateT("Loading history started."), MAX_SECONDLINE);
HWND popupHwnd = NULL;
if (ServiceExists(MS_POPUP_ADDPOPUPW)) {
popupHwnd = (HWND)CallService(MS_POPUP_ADDPOPUPW, (WPARAM)&pd, (LPARAM)APF_RETURN_HWND);
}
std::vector messages;
std::string firstTimestamp = "";
std::string firstMessageId = "";
std::string lastMessageId = "";
int loadedMessages = 0;
int messagesPerBatch = messagesCount > 10000 ? 500 : 100;
for (int batch = 0; batch < messagesCount; batch += messagesPerBatch) {
if (!isOnline())
break;
// Request batch of messages from thread
std::string data = "client=mercury";
data += "&__user=" + facy.self_.user_id;
data += "&__dyn=" + facy.__dyn();
data += "&__req=" + facy.__req();
data += "&fb_dtsg=" + facy.dtsg_;
data += "&ttstamp=" + facy.ttstamp_;
data += "&__rev=" + facy.__rev();
data += "&__pc=PHASED:DEFAULT&__be=-1&__a=1";
// Grrr, offset doesn't work at all, we need to use timestamps to get back in history...
// And we don't know, what's timestamp of first message, so we need to get from latest to oldest
data += "&messages[" + type + "][" + id;
data += "][offset]=" + utils::conversion::to_string(&batch, UTILS_CONV_UNSIGNED_NUMBER);
data += "&messages[" + type + "][" + id;
data += "][timestamp]=" + firstTimestamp;
data += "&messages[" + type + "][" + id;
data += "][limit]=" + utils::conversion::to_string(&messagesPerBatch, UTILS_CONV_UNSIGNED_NUMBER);
resp = facy.flap(REQUEST_THREAD_INFO, &data); // NOTE: Request revised 17.8.2016
if (resp.code != HTTP_CODE_OK || resp.data.empty()) {
facy.handle_error("LoadHistory");
break;
}
// Parse the result
CODE_BLOCK_TRY
messages.clear();
p->parse_history(&resp.data, &messages, &firstTimestamp);
// Receive messages
std::string previousFirstMessageId = firstMessageId;
for (std::vector::size_type i = 0; i < messages.size(); i++) {
facebook_message &msg = messages[i];
// First message might overlap (as we are using it's timestamp for the next loading), so we need to check for it
if (i == 0) {
firstMessageId = msg.message_id;
}
if (previousFirstMessageId == msg.message_id) {
continue;
}
lastMessageId = msg.message_id;
if (msg.isIncoming && msg.isUnread && msg.type == MESSAGE) {
PROTORECVEVENT recv = { 0 };
recv.szMessage = const_cast(msg.message_text.c_str());
recv.timestamp = msg.time;
ProtoChainRecvMsg(hContact, &recv);
}
else {
DBEVENTINFO dbei = { 0 };
dbei.cbSize = sizeof(dbei);
if (msg.type == MESSAGE)
dbei.eventType = EVENTTYPE_MESSAGE;
else if (msg.type == VIDEO_CALL || msg.type == PHONE_CALL)
dbei.eventType = FACEBOOK_EVENTTYPE_CALL;
else
dbei.eventType = EVENTTYPE_URL; // FIXME: Use better and specific type for our other event types.
dbei.flags = DBEF_UTF;
if (!msg.isIncoming)
dbei.flags |= DBEF_SENT;
if (!msg.isUnread)
dbei.flags |= DBEF_READ;
dbei.szModule = m_szModuleName;
dbei.timestamp = msg.time;
dbei.cbBlob = (DWORD)msg.message_text.length() + 1;
dbei.pBlob = (PBYTE)msg.message_text.c_str();
db_event_add(hContact, &dbei);
}
loadedMessages++;
}
// Save last message id of first batch which is latest message completely, because we're going backwards
if (batch == 0 && !lastMessageId.empty()) {
setString(hContact, FACEBOOK_KEY_MESSAGE_ID, lastMessageId.c_str());
}
debugLogA("*** Load history messages processed");
CODE_BLOCK_CATCH
debugLogA("*** Error processing load history messages: %s", e.what());
break;
CODE_BLOCK_END
// Update progress popup
CMStringW text;
text.AppendFormat(TranslateT("Loading messages: %d/%d"), loadedMessages, messagesCount);
if (ServiceExists(MS_POPUP_CHANGETEXTW) && popupHwnd) {
PUChangeTextW(popupHwnd, text);
}
else if (ServiceExists(MS_POPUP_ADDPOPUPW)) {
wcsncpy(pd.lptzText, text, MAX_SECONDLINE);
pd.iSeconds = 1;
popupHwnd = (HWND)CallService(MS_POPUP_ADDPOPUPW, (WPARAM)&pd, (LPARAM)0);
}
// There is no more messages
if (messages.empty() || loadedMessages > messagesCount) {
break;
}
}
delete p;
facy.handle_success("LoadHistory");
// Enable marking messages as read for this contact
facy.ignore_read.erase(hContact);
if (ServiceExists(MS_POPUP_CHANGETEXTW) && popupHwnd) {
PUChangeTextW(popupHwnd, TranslateT("Loading history completed."));
} else if (ServiceExists(MS_POPUP_ADDPOPUPW)) {
pd.iSeconds = 5;
wcsncpy(pd.lptzText, TranslateT("Loading history completed."), MAX_SECONDLINE);
popupHwnd = (HWND)CallService(MS_POPUP_ADDPOPUPW, (WPARAM)&pd, (LPARAM)0);
}
// PUDeletePopup(popupHwnd);
}
std::string truncateUtf8(std::string &text, size_t maxLength) {
// To not split some unicode character we need to transform it to wchar_t first, then split it, and then convert it back, because we want std::string as result
// TODO: Probably there is much simpler and nicer way
std::wstring ttext = ptrW(mir_utf8decodeW(text.c_str()));
if (ttext.length() > maxLength) {
ttext = ttext.substr(0, maxLength) + L"\x2026"; // unicode ellipsis
return std::string(_T2A(ttext.c_str(), CP_UTF8));
}
// It's not longer, return given string
return text;
}
void parseFeeds(const std::string &text, std::vector &news, DWORD &last_post_time, bool filterAds = true) {
std::string::size_type pos = 0;
UINT limit = 0;
DWORD new_time = last_post_time;
while ((pos = text.find("