From 823e3839b108658811f689f959f51058effd82ac Mon Sep 17 00:00:00 2001 From: George Hazan Date: Thu, 4 Jan 2018 15:09:01 +0300 Subject: =?UTF-8?q?=D0=A1++'11=20scope=20for's=20are=20suddenly=20more=20e?= =?UTF-8?q?ffective...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- protocols/FacebookRM/src/chat.cpp | 10 +- protocols/FacebookRM/src/client.h | 11 +- protocols/FacebookRM/src/communication.cpp | 31 ++- protocols/FacebookRM/src/contacts.cpp | 21 +- protocols/FacebookRM/src/entities.h | 2 +- protocols/FacebookRM/src/json.cpp | 319 +++++++++++++---------------- protocols/FacebookRM/src/list.hpp | 185 ----------------- protocols/FacebookRM/src/messages.cpp | 4 +- protocols/FacebookRM/src/process.cpp | 19 +- protocols/FacebookRM/src/proto.cpp | 16 +- protocols/FacebookRM/src/stdafx.h | 1 - 11 files changed, 185 insertions(+), 434 deletions(-) delete mode 100644 protocols/FacebookRM/src/list.hpp diff --git a/protocols/FacebookRM/src/chat.cpp b/protocols/FacebookRM/src/chat.cpp index 335b9e281a..7b5a0d7935 100644 --- a/protocols/FacebookRM/src/chat.cpp +++ b/protocols/FacebookRM/src/chat.cpp @@ -260,8 +260,8 @@ INT_PTR FacebookProto::OnJoinChat(WPARAM hContact, LPARAM) AddChat(fbc->thread_id.c_str(), fbc->chat_name.c_str()); // Add chat contacts - for (std::map::iterator jt = fbc->participants.begin(); jt != fbc->participants.end(); ++jt) - AddChatContact(fbc->thread_id.c_str(), jt->second, false); + for (auto &jt : fbc->participants) + AddChatContact(fbc->thread_id.c_str(), jt.second, false); // Load last messages delSetting(hContact, FACEBOOK_KEY_MESSAGE_ID); // We're creating new chatroom so we want load all recent messages @@ -393,11 +393,11 @@ std::string FacebookProto::GenerateChatName(facebook_chatroom *fbc) std::string name = ""; unsigned int namesUsed = 0; - for (auto it = fbc->participants.begin(); it != fbc->participants.end(); ++it) { - std::string participant = it->second.nick; + for (auto &it : fbc->participants) { + std::string participant = it.second.nick; // Ignore self contact, empty and numeric only participant names - if (it->second.role == ROLE_ME || participant.empty() || participant.find_first_not_of("0123456789") == std::string::npos) + if (it.second.role == ROLE_ME || participant.empty() || participant.find_first_not_of("0123456789") == std::string::npos) continue; if (namesUsed > 0) diff --git a/protocols/FacebookRM/src/client.h b/protocols/FacebookRM/src/client.h index c97a43be55..129a2ae8c1 100644 --- a/protocols/FacebookRM/src/client.h +++ b/protocols/FacebookRM/src/client.h @@ -92,14 +92,12 @@ public: bool mbasicWorks; //////////////////////////////////////////////////////////// - // Client vs protocol communication void client_notify(wchar_t* message); void info_notify(wchar_t* message); //////////////////////////////////////////////////////////// - // Cookies, Data storage HANDLE cookies_lock_; @@ -165,7 +163,6 @@ public: } //////////////////////////////////////////////////////////// - // Login handling bool login(const char *username, const char *password); @@ -175,7 +172,6 @@ public: const std::string & get_username() const; //////////////////////////////////////////////////////////// - // Session handling bool home(); @@ -183,15 +179,12 @@ public: bool chat_state(bool online = true); //////////////////////////////////////////////////////////// - // Updates handling - List::List buddies; HANDLE send_message_lock_; HANDLE notifications_lock_; //////////////////////////////////////////////////////////// - // Messages handling std::map messages_ignore; @@ -205,13 +198,11 @@ public: int send_message(int seqid, MCONTACT, const std::string &message_text, std::string *error_text, const std::string &captchaPersistData = "", const std::string &captcha = ""); //////////////////////////////////////////////////////////// - // Status handling - bool post_status(status_data *data); + bool post_status(status_data *data); //////////////////////////////////////////////////////////// - // HTTP communication http::response sendRequest(HttpRequest *request); diff --git a/protocols/FacebookRM/src/communication.cpp b/protocols/FacebookRM/src/communication.cpp index 562f46b3a8..f07674b258 100644 --- a/protocols/FacebookRM/src/communication.cpp +++ b/protocols/FacebookRM/src/communication.cpp @@ -220,10 +220,10 @@ char* facebook_client::load_cookies() std::string cookieString; if (!cookies.empty()) - for (std::map< std::string, std::string >::iterator iter = cookies.begin(); iter != cookies.end(); ++iter) { - cookieString.append(iter->first); + for (auto &iter : cookies) { + cookieString.append(iter.first); cookieString.append(1, '='); - cookieString.append(iter->second); + cookieString.append(iter.second); cookieString.append(1, ';'); } @@ -264,12 +264,11 @@ void facebook_client::clear_notifications() { ScopedLock s(notifications_lock_); - for (auto it = notifications.begin(); it != notifications.end();) { - if (it->second->hWndPopup != nullptr) - PUDeletePopup(it->second->hWndPopup); // close popup + for (auto &it : notifications) { + if (it.second->hWndPopup != nullptr) + PUDeletePopup(it.second->hWndPopup); // close popup - delete it->second; - it = notifications.erase(it); + delete it.second; } notifications.clear(); @@ -277,10 +276,9 @@ void facebook_client::clear_notifications() void facebook_client::clear_chatrooms() { - for (auto it = chat_rooms.begin(); it != chat_rooms.end();) { - delete it->second; - it = chat_rooms.erase(it); - } + for (auto &it : chat_rooms) + delete it.second; + chat_rooms.clear(); } @@ -289,12 +287,11 @@ void facebook_client::clear_chatrooms() */ void facebook_client::clear_readers() { - for (std::map::iterator it = readers.begin(); it != readers.end();) { - if (parent->isChatRoom(it->first)) - parent->delSetting(it->first, FACEBOOK_KEY_MESSAGE_READERS); + for (auto &it : readers) { + if (parent->isChatRoom(it.first)) + parent->delSetting(it.first, FACEBOOK_KEY_MESSAGE_READERS); - parent->delSetting(it->first, FACEBOOK_KEY_MESSAGE_READ); - it = readers.erase(it); + parent->delSetting(it.first, FACEBOOK_KEY_MESSAGE_READ); } readers.clear(); } diff --git a/protocols/FacebookRM/src/contacts.cpp b/protocols/FacebookRM/src/contacts.cpp index b66628b733..6352300348 100644 --- a/protocols/FacebookRM/src/contacts.cpp +++ b/protocols/FacebookRM/src/contacts.cpp @@ -222,9 +222,9 @@ void FacebookProto::LoadParticipantsNames(facebook_chatroom *fbc) std::vector namelessIds; // TODO: We could load all names from server at once by skipping this for cycle and using namelessIds as all in participants list, but we would lost our local names of our contacts. But maybe that's not a problem? - for (auto it = fbc->participants.begin(); it != fbc->participants.end(); ++it) { - const char *id = it->first.c_str(); - chatroom_participant &user = it->second; + for (auto &it : fbc->participants) { + const char *id = it.first.c_str(); + chatroom_participant &user = it.second; if (!user.loaded) { if (!mir_strcmp(id, facy.self_.user_id.c_str())) { @@ -260,9 +260,8 @@ void FacebookProto::LoadParticipantsNames(facebook_chatroom *fbc) // we have some contacts without name, let's load them all from the server LIST userIds(1); - for (std::string::size_type i = 0; i < namelessIds.size(); i++) { + for (std::string::size_type i = 0; i < namelessIds.size(); i++) userIds.insert(mir_strdup(namelessIds.at(i).c_str())); - } HttpRequest *request = new UserInfoRequest(&facy, userIds); http::response resp = facy.sendRequest(request); @@ -411,14 +410,8 @@ void FacebookProto::DeleteContactFromServer(void *data) http::response resp = facy.sendRequest(request); if (resp.data.find("\"payload\":null", 0) != std::string::npos) { - // FIXME: Remember that we deleted this contact, so we won't accidentally add him at status change - /* facebook_user* fbu = facy.buddies.find(id); - if (fbu != nullptr) - fbu->deleted = true; */ - - MCONTACT hContact = ContactIDToHContact(id); - // If contact wasn't deleted from database + MCONTACT hContact = ContactIDToHContact(id); if (hContact != 0) { setWord(hContact, "Status", ID_STATUS_OFFLINE); setByte(hContact, FACEBOOK_KEY_CONTACT_TYPE, CONTACT_NONE); @@ -427,9 +420,7 @@ void FacebookProto::DeleteContactFromServer(void *data) NotifyEvent(m_tszUserName, TranslateT("Contact was removed from your server list."), 0, EVENT_FRIENDSHIP); } - else { - facy.client_notify(TranslateT("Error occurred when removing contact from server.")); - } + else facy.client_notify(TranslateT("Error occurred when removing contact from server.")); if (resp.code != HTTP_CODE_OK) facy.handle_error("DeleteContactFromServer"); diff --git a/protocols/FacebookRM/src/entities.h b/protocols/FacebookRM/src/entities.h index 3a88dc8acc..5b10bd53ee 100644 --- a/protocols/FacebookRM/src/entities.h +++ b/protocols/FacebookRM/src/entities.h @@ -147,7 +147,7 @@ struct facebook_notification this->icon = nullptr; } - void setIcon(const std::string &iconUrl) + void setIcon(const std::string& /*iconUrl*/) { icon = nullptr; } diff --git a/protocols/FacebookRM/src/json.cpp b/protocols/FacebookRM/src/json.cpp index 586189784f..5ce92f5441 100644 --- a/protocols/FacebookRM/src/json.cpp +++ b/protocols/FacebookRM/src/json.cpp @@ -93,8 +93,8 @@ void FacebookProto::ParseMessageType(facebook_message &message, const JSONNode & message.type = SUBSCRIBE; const JSONNode &fbids_ = log_data_["added_participants"]; - for (auto it2 = fbids_.begin(); it2 != fbids_.end(); ++it2) { - std::string id = (*it2).as_string().substr(5); // strip "fbid:" prefix + for (auto &it2 : fbids_) { + std::string id = it2.as_string().substr(5); // strip "fbid:" prefix if (!message.data.empty()) message.data += ";"; message.data += id; @@ -104,8 +104,8 @@ void FacebookProto::ParseMessageType(facebook_message &message, const JSONNode & message.type = UNSUBSCRIBE; const JSONNode &fbids_ = log_data_["removed_participants"]; - for (auto it2 = fbids_.begin(); it2 != fbids_.end(); ++it2) { - std::string id = (*it2).as_string().substr(5); // strip "fbid:" prefix + for (auto &it2 : fbids_) { + std::string id = it2.as_string().substr(5); // strip "fbid:" prefix if (!message.data.empty()) message.data += ";"; message.data += id; @@ -135,10 +135,10 @@ int FacebookProto::ParseChatParticipants(std::string *data, std::maptype != CONTACT_FRIEND) { @@ -193,7 +193,6 @@ int FacebookProto::ParseFriends(std::string *data, std::map< std::string, facebo return EXIT_SUCCESS; } - int FacebookProto::ParseNotifications(std::string *data, std::map< std::string, facebook_notification* > *notifications) { std::string jsonData = data->substr(9); @@ -209,13 +208,13 @@ int FacebookProto::ParseNotifications(std::string *data, std::map< std::string, // Create notifications chatroom (if it doesn't exists), because we will be writing to it new notifications here PrepareNotificationsChatRoom(); - for (auto it = list.begin(); it != list.end(); ++it) { - const JSONNode &id_ = (*it)["alert_id"]; - const JSONNode &state_ = (*it)["seen_state"]; - const JSONNode &time_ = (*it)["timestamp"]["time"]; - const JSONNode &text_ = (*it)["title"]["text"]; - const JSONNode &url_ = (*it)["url"]; - const JSONNode &icon_ = (*it)["icon"]["uri"]; + for (auto &it : list) { + const JSONNode &id_ = it["alert_id"]; + const JSONNode &state_ = it["seen_state"]; + const JSONNode &time_ = it["timestamp"]["time"]; + const JSONNode &text_ = it["title"]["text"]; + const JSONNode &url_ = it["url"]; + const JSONNode &icon_ = it["icon"]["uri"]; // Ignore empty and old notifications if (!text_ || !state_ || state_.as_string() == "SEEN_AND_READ" || !time_) @@ -281,9 +280,8 @@ void FacebookProto::ParseAttachments(std::string &message_text, const JSONNode & if (!attachments_ || attachments_.empty()) return; - for (auto itAttachment = attachments_.begin(); itAttachment != attachments_.end(); ++itAttachment) { - const JSONNode &attach_ = legacy ? (*itAttachment) : (*itAttachment)["mercury"]; - + for (auto &itAttachment : attachments_) { + const JSONNode &attach_ = legacy ? itAttachment : itAttachment["mercury"]; if (const JSONNode sticker_ = attach_["sticker_attachment"]) { newText = TranslateT("a sticker"); attachments_text += "\n"; @@ -433,10 +431,9 @@ bool FacebookProto::ProcessSpecialMessage(std::vector* message int FacebookProto::ParseMessages(std::string *pData, std::vector* messages, std::map< std::string, facebook_notification* >* notifications) { // remove old received messages from map - for (std::map::iterator it = facy.messages_ignore.begin(); it != facy.messages_ignore.end();) { - if (it->second > FACEBOOK_IGNORE_COUNTER_LIMIT) { + for (auto it = facy.messages_ignore.begin(); it != facy.messages_ignore.end();) { + if (it->second > FACEBOOK_IGNORE_COUNTER_LIMIT) it = facy.messages_ignore.erase(it); - } else { it->second++; // increase counter on each request ++it; @@ -451,8 +448,8 @@ int FacebookProto::ParseMessages(std::string *pData, std::vectorhandle, "MirVer", fbu->getMirVer()); */ - const JSONNode &a_ = (*itNodes)["a"]; // possible values: 0, 2 (something more?) - const JSONNode &la_ = (*itNodes)["la"]; // timestamp of last activity (could be 0) - const JSONNode &s_ = (*itNodes)["s"]; // possible values: push (something more?) - const JSONNode &vc_ = (*itNodes)["vc"]; // possible values: 0, 8, 10 (something more?) + const JSONNode &a_ = itNodes["a"]; // possible values: 0, 2 (something more?) + const JSONNode &la_ = itNodes["la"]; // timestamp of last activity (could be 0) + const JSONNode &s_ = itNodes["s"]; // possible values: push (something more?) + const JSONNode &vc_ = itNodes["vc"]; // possible values: 0, 8, 10 (something more?) // Friller account has also these: - // const JSONNode &ol_ = (*itNodes)["ol"]; // possible values: -1 (when goes to offline), 0 (when goes back online) (something more?) - // const JSONNode &p_ = (*itNodes)["p"]; // class with fbAppStatus, messengerStatus, otherStatus, status, webStatus + // const JSONNode &ol_ = itNodes["ol"]; // possible values: -1 (when goes to offline), 0 (when goes back online) (something more?) + // const JSONNode &p_ = itNodes["p"]; // class with fbAppStatus, messengerStatus, otherStatus, status, webStatus int status = ID_STATUS_FREECHAT; // FREECHAT to easily spot some problem, as we expect it will always be p==0 or p==2 below @@ -946,27 +936,11 @@ int FacebookProto::ParseMessages(std::string *pData, std::vectoridle || status == ID_STATUS_OFFLINE) &&*/ last_active > 0) - // setDword(hContact, "IdleTS", last_active); - // else - // delSetting(hContact, "IdleTS"); - //} - - /*if (last_active > 0) - setDword(hContact, "LastActiveTS", last_active); - else - delSetting(hContact, "LastActiveTS"); - */ - // Set users inactive for too long as offline if (last_active > 0 && last_active < offlineThreshold) status = ID_STATUS_OFFLINE; } - else { - delSetting(hContact, "IdleTS"); - } + else delSetting(hContact, "IdleTS"); setWord(hContact, "Status", status); @@ -1000,8 +974,8 @@ int FacebookProto::ParseMessages(std::string *pData, std::vectorfind(id); + const JSONNode &alerts = it["alert_ids"]; + for (auto &itAlerts : alerts) { + auto itAlert = notifications->find(itAlerts.as_string()); if (itAlert != notifications->end()) { if (itAlert->second->hWndPopup != nullptr) PUDeletePopup(itAlert->second->hWndPopup); // close popup @@ -1041,35 +1012,35 @@ int FacebookProto::ParseMessages(std::string *pData, std::vectorpush_back((*jt).as_string()); + const JSONNode &thread_fbids = it["thread_fbids"]; + for (auto &jt : thread_fbids) + threads->push_back(jt.as_string()); // For classic conversations - const JSONNode &other_user_fbids = (*it)["other_user_fbids"]; - for (auto jt = other_user_fbids.begin(); jt != other_user_fbids.end(); ++jt) - threads->push_back((*jt).as_string()); + const JSONNode &other_user_fbids = it["other_user_fbids"]; + for (auto &jt : other_user_fbids) + threads->push_back(jt.as_string()); } return EXIT_SUCCESS; @@ -1122,21 +1093,21 @@ int FacebookProto::ParseThreadMessages(std::string *data, std::vector< facebook_ if (!actions || !threads) return EXIT_FAILURE; - for (auto it = actions.begin(); it != actions.end(); ++it) { - const JSONNode &author_ = (*it)["author"]; - const JSONNode &other_user_fbid_ = (*it)["other_user_fbid"]; - const JSONNode &body_ = (*it)["body"]; - const JSONNode &thread_id_ = (*it)["thread_id"]; - const JSONNode &thread_fbid_ = (*it)["thread_fbid"]; - const JSONNode &mid_ = (*it)["message_id"]; - const JSONNode ×tamp_ = (*it)["timestamp"]; - const JSONNode &filtered_ = (*it)["is_filtered_content"]; - const JSONNode &is_unread_ = (*it)["is_unread"]; + for (auto &it : actions) { + const JSONNode &author_ = it["author"]; + const JSONNode &other_user_fbid_ = it["other_user_fbid"]; + const JSONNode &body_ = it["body"]; + const JSONNode &thread_id_ = it["thread_id"]; + const JSONNode &thread_fbid_ = it["thread_fbid"]; + const JSONNode &mid_ = it["message_id"]; + const JSONNode ×tamp_ = it["timestamp"]; + const JSONNode &filtered_ = it["is_filtered_content"]; + const JSONNode &is_unread_ = it["is_unread"]; // Either there is "body" (for classic messages), or "log_message_type" and "log_message_body" (for log messages) - const JSONNode &log_type_ = (*it)["log_message_type"]; - const JSONNode &log_body_ = (*it)["log_message_body"]; - const JSONNode &log_data_ = (*it)["log_message_data"]; // additional data for this log message + const JSONNode &log_type_ = it["log_message_type"]; + const JSONNode &log_body_ = it["log_message_body"]; + const JSONNode &log_data_ = it["log_message_data"]; // additional data for this log message if (!author_ || (!body_ && !log_body_) || !mid_ || (!thread_fbid_ && !thread_id_) || !timestamp_) { debugLogA("ParseThreadMessages: ignoring message (%s) - missing attribute", mid_.as_string().c_str()); @@ -1154,7 +1125,7 @@ int FacebookProto::ParseThreadMessages(std::string *data, std::vector< facebook_ author_id = author_id.substr(pos + 1); // Process attachements and stickers - ParseAttachments(message_text, *it, other_user_fbid, true); + ParseAttachments(message_text, it, other_user_fbid, true); if (filtered_.as_bool() && message_text.empty()) message_text = Translate("This message is no longer available, because it was marked as abusive or spam."); @@ -1214,20 +1185,20 @@ int FacebookProto::ParseHistory(std::string *data, std::vector< facebook_message bool first = true; - for (auto it = actions.begin(); it != actions.end(); ++it) { - const JSONNode &author = (*it)["author"]; - const JSONNode &other_user_fbid = (*it)["other_user_fbid"]; - const JSONNode &body = (*it)["body"]; - const JSONNode &tid = (*it)["thread_id"]; - const JSONNode &mid = (*it)["message_id"]; - const JSONNode ×tamp = (*it)["timestamp"]; - const JSONNode &filtered = (*it)["is_filtered_content"]; - const JSONNode &is_unread = (*it)["is_unread"]; + for (auto &it : actions) { + const JSONNode &author = it["author"]; + const JSONNode &other_user_fbid = it["other_user_fbid"]; + const JSONNode &body = it["body"]; + const JSONNode &tid = it["thread_id"]; + const JSONNode &mid = it["message_id"]; + const JSONNode ×tamp = it["timestamp"]; + const JSONNode &filtered = it["is_filtered_content"]; + const JSONNode &is_unread = it["is_unread"]; // Either there is "body" (for classic messages), or "log_message_type" and "log_message_body" (for log messages) - const JSONNode &log_type_ = (*it)["log_message_type"]; - const JSONNode &log_body_ = (*it)["log_message_body"]; - const JSONNode &log_data_ = (*it)["log_message_data"]; + const JSONNode &log_type_ = it["log_message_type"]; + const JSONNode &log_body_ = it["log_message_body"]; + const JSONNode &log_data_ = it["log_message_data"]; if (!author || (!body && !log_body_) || !mid || !tid || !timestamp) { debugLogA("ParseHistory: ignoring message (%s) - missing attribute", mid.as_string().c_str()); @@ -1249,7 +1220,7 @@ int FacebookProto::ParseHistory(std::string *data, std::vector< facebook_message author_id = author_id.substr(pos + 1); // Process attachements and stickers - ParseAttachments(message_text, *it, other_user_id, true); + ParseAttachments(message_text, it, other_user_id, true); if (filtered.as_bool() && message_text.empty()) message_text = Translate("This message is no longer available, because it was marked as abusive or spam."); @@ -1291,10 +1262,10 @@ int FacebookProto::ParseThreadInfo(std::string *data, std::string* user_id) return EXIT_FAILURE; //std::map thread_ids; - for (auto it = threads.begin(); it != threads.end(); ++it) { - const JSONNode &canonical = (*it)["canonical_fbid"]; - const JSONNode &thread_id = (*it)["thread_id"]; - //const JSONNode &message_count = (*it)["message_count"); // TODO: this could be useful for loading history from server + for (auto &it : threads) { + const JSONNode &canonical = it["canonical_fbid"]; + const JSONNode &thread_id = it["thread_id"]; + //const JSONNode &message_count = it["message_count"); // TODO: this could be useful for loading history from server if (!canonical || !thread_id) continue; @@ -1322,12 +1293,12 @@ int FacebookProto::ParseUserInfo(std::string *data, facebook_user* fbu) return EXIT_FAILURE; //std::map user_ids; - for (auto it = profiles.begin(); it != profiles.end(); ++it) { + for (auto &it : profiles) { // TODO: allow more users to parse at once - std::string id = (*it).name(); + std::string id = it.name(); if (fbu->user_id == id) { - parseUser(*it, fbu); + parseUser(it, fbu); break; } } @@ -1347,12 +1318,12 @@ int FacebookProto::ParseChatInfo(std::string *data, facebook_chatroom* fbc) if (!threads) return EXIT_FAILURE; - for (auto it = threads.begin(); it != threads.end(); ++it) { - const JSONNode &is_canonical_user_ = (*it)["is_canonical_user"]; - const JSONNode &thread_fbid_ = (*it)["thread_fbid"]; - const JSONNode &name_ = (*it)["name"]; - //const JSONNode &message_count_ = (*it)["message_count"); - //const JSONNode &unread_count_ = (*it)["unread_count"); // TODO: use it to check against number of loaded messages... but how? + for (auto &it : threads) { + const JSONNode &is_canonical_user_ = it["is_canonical_user"]; + const JSONNode &thread_fbid_ = it["thread_fbid"]; + const JSONNode &name_ = it["name"]; + //const JSONNode &message_count_ = it["message_count"); + //const JSONNode &unread_count_ = it["unread_count"); // TODO: use it to check against number of loaded messages... but how? if (!thread_fbid_ || !is_canonical_user_ || is_canonical_user_.as_bool()) continue; @@ -1366,27 +1337,27 @@ int FacebookProto::ParseChatInfo(std::string *data, facebook_chatroom* fbc) chatroom_participant user; user.is_former = true; - const JSONNode &former_participants = (*it)["former_participants"]; - for (auto jt = former_participants.begin(); jt != former_participants.end(); ++jt) { - user.role = (*jt)["is_friend"].as_bool() ? ROLE_FRIEND : ROLE_NONE; - user.user_id = (*jt)["id"].as_string().substr(5); // strip "fbid:" prefix + const JSONNode &former_participants = it["former_participants"]; + for (auto &jt : former_participants) { + user.role = jt["is_friend"].as_bool() ? ROLE_FRIEND : ROLE_NONE; + user.user_id = jt["id"].as_string().substr(5); // strip "fbid:" prefix fbc->participants.insert(std::make_pair(user.user_id, user)); } user.is_former = false; user.role = ROLE_NONE; - const JSONNode &participants = (*it)["participants"]; - for (auto jt = participants.begin(); jt != participants.end(); ++jt) { - user.user_id = (*jt).as_string().substr(5); // strip "fbid:" prefix + const JSONNode &participants = it["participants"]; + for (auto &jt : participants) { + user.user_id = jt.as_string().substr(5); // strip "fbid:" prefix fbc->participants.insert(std::make_pair(user.user_id, user)); } // TODO: don't automatically join unsubscribed or archived chatrooms - fbc->can_reply = (*it)["can_reply"].as_bool(); - fbc->is_archived = (*it)["is_archived"].as_bool(); - fbc->is_subscribed = (*it)["is_subscribed"].as_bool(); - fbc->read_only = (*it)["read_only"].as_bool(); + fbc->can_reply = it["can_reply"].as_bool(); + fbc->is_archived = it["is_archived"].as_bool(); + fbc->is_subscribed = it["is_subscribed"].as_bool(); + fbc->read_only = it["read_only"].as_bool(); fbc->chat_name = std::wstring(ptrW(mir_utf8decodeW(name_.as_string().c_str()))); } @@ -1405,9 +1376,9 @@ int FacebookProto::ParseMessagesCount(std::string *data, int *messagesCount, int if (!threads) return EXIT_FAILURE; - for (auto it = threads.begin(); it != threads.end(); ++it) { - const JSONNode &message_count_ = (*it)["message_count"]; - const JSONNode &unread_count_ = (*it)["unread_count"]; + for (auto &it : threads) { + const JSONNode &message_count_ = it["message_count"]; + const JSONNode &unread_count_ = it["unread_count"]; if (!message_count_ || !unread_count_) return EXIT_FAILURE; diff --git a/protocols/FacebookRM/src/list.hpp b/protocols/FacebookRM/src/list.hpp deleted file mode 100644 index 7e3038fd20..0000000000 --- a/protocols/FacebookRM/src/list.hpp +++ /dev/null @@ -1,185 +0,0 @@ -/* - -Facebook plugin for Miranda Instant Messenger -_____________________________________________ - -Copyright © 2009-11 Michal Zelinka, 2011-17 Robert Pösel, 2017-18 Miranda NG team - -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 . - -*/ - -#pragma once - -namespace List -{ - template< typename T > class Item - { - public: - std::string key; - T* data; - Item< T >* prev; - Item< T >* next; - - Item() - { - this->data = nullptr; - this->prev = nullptr; - this->next = nullptr; - } - - ~Item() - { - delete this->data; - } - }; - - template< typename T > class List - { - private: - Item< T >* first; - Item< T >* last; - unsigned int count; - - public: - List() - { - this->first = this->last = nullptr; - this->count = 0; - } - - ~List() - { - this->clear(); - } - - Item< T >* begin() - { - return first; - } - - Item< T >* end() - { - return last; - } - - unsigned int size() - { - return count; - } - - bool empty() - { - return (this->first == nullptr); - } - - void insert(Item< T >* item) - { - if (this->empty()) { - this->first = this->last = item; - this->count = 1; - } - else { // TODO: key sorting/comparation - item->next = this->first; - this->first->prev = item; - this->first = item; - this->count++; - } - } - - void insert(std::pair< std::string, T* > item) - { - Item* ins = new Item; - ins->key = item.first; - ins->data = item.second; - this->insert(ins); - } - void erase(std::string key) - { - Item< T >* help = this->first; - while (help != nullptr) { - if (help->key.compare(key) != 0) - help = help->next; - else { - if (help == this->first) { - this->first = help->next; - if (this->first != nullptr) - this->first->prev = nullptr; - else - this->last = nullptr; - } - else if (help == this->last) { - this->last = help->prev; - if (this->last != nullptr) - this->last->next = nullptr; - else - this->first = nullptr; - } - else { - help->prev->next = help->next; - help->next->prev = help->prev; - } - this->count--; - delete help; - break; - } - } - } - - void erase(Item< T >* item) - { - if (item != nullptr) - erase(item->key); - } - - T* find(std::string key) - { - Item< T >* help = this->begin(); - while (help != nullptr) { - if (help->key.compare(key) != 0) - help = help->next; - else - return help->data; - } - return nullptr; - } - - T* at(const unsigned int item) - { - if (item >= this->count) - return nullptr; - Item< T >* help = this->begin(); - for (unsigned int i = 0; i < item; i++) - help = help->next; - return help->item; - } - - T* operator[](const unsigned int item) - { - return at(item); - } - - void clear() - { - Item< T >* help; - while (this->first != nullptr) { - help = this->first; - this->first = this->first->next; - delete help; - } - this->last = nullptr; - this->count = 0; - } - }; -}; diff --git a/protocols/FacebookRM/src/messages.cpp b/protocols/FacebookRM/src/messages.cpp index 4bf30fc915..e634473c6d 100644 --- a/protocols/FacebookRM/src/messages.cpp +++ b/protocols/FacebookRM/src/messages.cpp @@ -168,9 +168,7 @@ void FacebookProto::ReadMessageWorker(void *p) } LIST ids(1); - for (std::set::iterator it = hContacts->begin(); it != hContacts->end(); ++it) { - MCONTACT hContact = *it; - + for (auto &hContact : *hContacts) { if (getBool(hContact, FACEBOOK_KEY_KEEP_UNREAD, 0)) continue; diff --git a/protocols/FacebookRM/src/process.cpp b/protocols/FacebookRM/src/process.cpp index fc8f1ea67e..cfbcdeec99 100644 --- a/protocols/FacebookRM/src/process.cpp +++ b/protocols/FacebookRM/src/process.cpp @@ -157,12 +157,11 @@ void FacebookProto::ProcessFriendList(void*) } // 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 + for (auto &it : friends) { + 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); + delete it.second; } friends.clear(); @@ -714,7 +713,7 @@ void FacebookProto::ReceiveMessages(std::vector &messages, boo } // 2. remove all marked messages from list - for (std::vector::iterator it = messages.begin(); it != messages.end();) { + for (auto it = messages.begin(); it != messages.end();) { if ((*it).flag_ == 1) it = messages.erase(it); else @@ -760,8 +759,8 @@ void FacebookProto::ReceiveMessages(std::vector &messages, boo // Set thread id (TID) for later setString(hChatContact, FACEBOOK_KEY_TID, fbc->thread_id.c_str()); - for (auto jt = fbc->participants.begin(); jt != fbc->participants.end(); ++jt) - AddChatContact(fbc->thread_id.c_str(), jt->second, false); + for (auto &jt : fbc->participants) + AddChatContact(fbc->thread_id.c_str(), jt.second, false); } if (!hChatContact) @@ -955,8 +954,8 @@ void FacebookProto::ShowNotifications() ScopedLock s(facy.notifications_lock_); // Show popups for unseen notifications and/or write them to chatroom - for (std::map::iterator it = facy.notifications.begin(); it != facy.notifications.end(); ++it) { - facebook_notification *notification = it->second; + for (auto &it : facy.notifications) { + facebook_notification *notification = it.second; if (notification != nullptr && !notification->seen) { debugLogA(" Showing popup for notification ID: %s", notification->id.c_str()); ptrW szText(mir_utf8decodeW(notification->text.c_str())); diff --git a/protocols/FacebookRM/src/proto.cpp b/protocols/FacebookRM/src/proto.cpp index 6533f754ee..f46b558c63 100644 --- a/protocols/FacebookRM/src/proto.cpp +++ b/protocols/FacebookRM/src/proto.cpp @@ -534,11 +534,9 @@ INT_PTR FacebookProto::OnMind(WPARAM wParam, LPARAM) post_status_data *data = new post_status_data(this, wall); - if (wall->user_id == facy.self_.user_id) { - for (std::map::iterator iter = facy.pages.begin(); iter != facy.pages.end(); ++iter) { - data->walls.push_back(new wall_data(iter->first, mir_utf8decodeW(iter->second.c_str()), true)); - } - } + if (wall->user_id == facy.self_.user_id) + for (auto &iter : facy.pages) + data->walls.push_back(new wall_data(iter.first, mir_utf8decodeW(iter.second.c_str()), true)); HWND hDlg = CreateDialogParam(g_hInstance, MAKEINTRESOURCE(IDD_MIND), (HWND)nullptr, FBMindProc, reinterpret_cast(data)); ShowWindow(hDlg, SW_SHOW); @@ -789,14 +787,6 @@ INT_PTR FacebookProto::CancelFriendship(WPARAM wParam, LPARAM lParam) return 1; std::string *val = new std::string(id); - - // FIXME: Remember that we deleted this contact, so we won't accidentally add him at status change - /*if (deleting) { - facebook_user *fbu = facy.buddies.find(*val); - if (fbu != nullptr) - fbu->handle = nullptr; - }*/ - ForkThread(&FacebookProto::DeleteContactFromServer, val); } diff --git a/protocols/FacebookRM/src/stdafx.h b/protocols/FacebookRM/src/stdafx.h index fe8d50eb58..6bf092d283 100644 --- a/protocols/FacebookRM/src/stdafx.h +++ b/protocols/FacebookRM/src/stdafx.h @@ -71,7 +71,6 @@ class FacebookProto; #include "entities.h" #include "http.h" #include "http_request.h" -#include "list.hpp" #include "client.h" #include "proto.h" #include "db.h" -- cgit v1.2.3