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/json.cpp | 319 +++++++++++++++++--------------------- 1 file changed, 145 insertions(+), 174 deletions(-) (limited to 'protocols/FacebookRM/src/json.cpp') 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; -- cgit v1.2.3