Improve is_acceptable_message.

GitOrigin-RevId: 494c88118bcb8da8e2e8cb956efd28ced3b424b5
This commit is contained in:
levlam 2019-06-17 04:25:33 +03:00
parent 4a8411329f
commit 11e9923e0a
4 changed files with 80 additions and 76 deletions

View File

@ -17880,19 +17880,20 @@ unique_ptr<MessagesManager::MessageForwardInfo> MessagesManager::get_message_for
DialogId from_dialog_id; DialogId from_dialog_id;
MessageId from_message_id; MessageId from_message_id;
string sender_name; string sender_name;
if ((flags & MESSAGE_FORWARD_HEADER_FLAG_HAS_AUTHOR_ID) != 0) { if ((flags & telegram_api::messageFwdHeader::FROM_ID_MASK) != 0) {
sender_user_id = UserId(forward_header->from_id_); sender_user_id = UserId(forward_header->from_id_);
if (!sender_user_id.is_valid()) { if (!sender_user_id.is_valid()) {
LOG(ERROR) << "Receive invalid sender id in message forward header: " << oneline(to_string(forward_header)); LOG(ERROR) << "Receive invalid sender id in message forward header: " << oneline(to_string(forward_header));
sender_user_id = UserId(); sender_user_id = UserId();
} }
} }
if ((flags & MESSAGE_FORWARD_HEADER_FLAG_HAS_CHANNEL_ID) != 0) { if ((flags & telegram_api::messageFwdHeader::CHANNEL_ID_MASK) != 0) {
channel_id = ChannelId(forward_header->channel_id_); channel_id = ChannelId(forward_header->channel_id_);
if (!channel_id.is_valid()) { if (!channel_id.is_valid()) {
LOG(ERROR) << "Receive invalid channel id in message forward header: " << oneline(to_string(forward_header)); LOG(ERROR) << "Receive invalid channel id in message forward header: " << oneline(to_string(forward_header));
} }
} }
constexpr int32 MESSAGE_FORWARD_HEADER_FLAG_HAS_MESSAGE_ID = telegram_api::messageFwdHeader::CHANNEL_POST_MASK;
if ((flags & MESSAGE_FORWARD_HEADER_FLAG_HAS_MESSAGE_ID) != 0) { if ((flags & MESSAGE_FORWARD_HEADER_FLAG_HAS_MESSAGE_ID) != 0) {
message_id = MessageId(ServerMessageId(forward_header->channel_post_)); message_id = MessageId(ServerMessageId(forward_header->channel_post_));
if (!message_id.is_valid()) { if (!message_id.is_valid()) {
@ -17900,10 +17901,11 @@ unique_ptr<MessagesManager::MessageForwardInfo> MessagesManager::get_message_for
message_id = MessageId(); message_id = MessageId();
} }
} }
constexpr int32 MESSAGE_FORWARD_HEADER_FLAG_HAS_AUTHOR_SIGNATURE = telegram_api::messageFwdHeader::POST_AUTHOR_MASK;
if ((flags & MESSAGE_FORWARD_HEADER_FLAG_HAS_AUTHOR_SIGNATURE) != 0) { if ((flags & MESSAGE_FORWARD_HEADER_FLAG_HAS_AUTHOR_SIGNATURE) != 0) {
author_signature = std::move(forward_header->post_author_); author_signature = std::move(forward_header->post_author_);
} }
if ((flags & MESSAGE_FORWARD_HEADER_FLAG_HAS_SAVED_FROM) != 0) { if ((flags & telegram_api::messageFwdHeader::SAVED_FROM_PEER_MASK) != 0) {
from_dialog_id = DialogId(forward_header->saved_from_peer_); from_dialog_id = DialogId(forward_header->saved_from_peer_);
from_message_id = MessageId(ServerMessageId(forward_header->saved_from_msg_id_)); from_message_id = MessageId(ServerMessageId(forward_header->saved_from_msg_id_));
if (!from_dialog_id.is_valid() || !from_message_id.is_valid()) { if (!from_dialog_id.is_valid() || !from_message_id.is_valid()) {
@ -17913,7 +17915,7 @@ unique_ptr<MessagesManager::MessageForwardInfo> MessagesManager::get_message_for
from_message_id = MessageId(); from_message_id = MessageId();
} }
} }
if ((flags & MESSAGE_FORWARD_HEADER_FLAG_HAS_SENDER_NAME) != 0) { if ((flags & telegram_api::messageFwdHeader::FROM_NAME_MASK) != 0) {
sender_name = std::move(forward_header->from_name_); sender_name = std::move(forward_header->from_name_);
} }

View File

@ -141,13 +141,6 @@ class MessagesManager : public Actor {
static constexpr int32 MESSAGE_FLAG_HAS_AUTHOR_SIGNATURE = 1 << 16; static constexpr int32 MESSAGE_FLAG_HAS_AUTHOR_SIGNATURE = 1 << 16;
static constexpr int32 MESSAGE_FLAG_HAS_MEDIA_ALBUM_ID = 1 << 17; static constexpr int32 MESSAGE_FLAG_HAS_MEDIA_ALBUM_ID = 1 << 17;
static constexpr int32 MESSAGE_FORWARD_HEADER_FLAG_HAS_AUTHOR_ID = 1 << 0;
static constexpr int32 MESSAGE_FORWARD_HEADER_FLAG_HAS_CHANNEL_ID = 1 << 1;
static constexpr int32 MESSAGE_FORWARD_HEADER_FLAG_HAS_MESSAGE_ID = 1 << 2;
static constexpr int32 MESSAGE_FORWARD_HEADER_FLAG_HAS_AUTHOR_SIGNATURE = 1 << 3;
static constexpr int32 MESSAGE_FORWARD_HEADER_FLAG_HAS_SAVED_FROM = 1 << 4;
static constexpr int32 MESSAGE_FORWARD_HEADER_FLAG_HAS_SENDER_NAME = 1 << 5;
static constexpr int32 SEND_MESSAGE_FLAG_IS_REPLY = 1 << 0; static constexpr int32 SEND_MESSAGE_FLAG_IS_REPLY = 1 << 0;
static constexpr int32 SEND_MESSAGE_FLAG_DISABLE_WEB_PAGE_PREVIEW = 1 << 1; static constexpr int32 SEND_MESSAGE_FLAG_DISABLE_WEB_PAGE_PREVIEW = 1 << 1;
static constexpr int32 SEND_MESSAGE_FLAG_HAS_REPLY_MARKUP = 1 << 2; static constexpr int32 SEND_MESSAGE_FLAG_HAS_REPLY_MARKUP = 1 << 2;

View File

@ -326,6 +326,33 @@ void UpdatesManager::set_date(int32 date, bool from_update, string date_source)
} }
} }
bool UpdatesManager::is_acceptable_dialog(DialogId dialog_id) const {
switch (dialog_id.get_type()) {
case DialogType::User:
if (!td_->contacts_manager_->have_user(dialog_id.get_user_id())) {
return false;
}
break;
case DialogType::Chat:
if (!td_->contacts_manager_->have_chat(dialog_id.get_chat_id())) {
return false;
}
break;
case DialogType::Channel:
if (!td_->contacts_manager_->have_channel(dialog_id.get_channel_id())) {
return false;
}
break;
case DialogType::None:
return false;
case DialogType::SecretChat:
default:
UNREACHABLE();
return false;
}
return true;
}
bool UpdatesManager::is_acceptable_message_entities( bool UpdatesManager::is_acceptable_message_entities(
const vector<tl_object_ptr<telegram_api::MessageEntity>> &message_entities) const { const vector<tl_object_ptr<telegram_api::MessageEntity>> &message_entities) const {
for (auto &entity : message_entities) { for (auto &entity : message_entities) {
@ -340,45 +367,56 @@ bool UpdatesManager::is_acceptable_message_entities(
return true; return true;
} }
bool UpdatesManager::is_acceptable_message_forward_header(
const telegram_api::object_ptr<telegram_api::messageFwdHeader> &header) const {
if (header == nullptr) {
return true;
}
auto flags = header->flags_;
if (flags & telegram_api::messageFwdHeader::CHANNEL_ID_MASK) {
ChannelId channel_id(header->channel_id_);
if (!td_->contacts_manager_->have_channel(channel_id)) {
return false;
}
}
if (flags & telegram_api::messageFwdHeader::FROM_ID_MASK) {
UserId user_id(header->from_id_);
if (!td_->contacts_manager_->have_user(user_id)) {
return false;
}
}
if (flags & telegram_api::messageFwdHeader::SAVED_FROM_PEER_MASK) {
DialogId dialog_id(header->saved_from_peer_);
if (!is_acceptable_dialog(dialog_id)) {
return false;
}
}
return true;
}
bool UpdatesManager::is_acceptable_message(const telegram_api::Message *message_ptr) const { bool UpdatesManager::is_acceptable_message(const telegram_api::Message *message_ptr) const {
CHECK(message_ptr != nullptr); CHECK(message_ptr != nullptr);
int32 constructor_id = message_ptr->get_id(); int32 constructor_id = message_ptr->get_id();
bool is_channel_post = false;
DialogId dialog_id;
UserId sender_user_id;
switch (constructor_id) { switch (constructor_id) {
case telegram_api::messageEmpty::ID: case telegram_api::messageEmpty::ID:
return true; return true;
case telegram_api::message::ID: { case telegram_api::message::ID: {
auto message = static_cast<const telegram_api::message *>(message_ptr); auto message = static_cast<const telegram_api::message *>(message_ptr);
is_channel_post = (message->flags_ & MessagesManager::MESSAGE_FLAG_IS_POST) != 0; if (!is_acceptable_dialog(DialogId(message->to_id_))) {
dialog_id = DialogId(message->to_id_); return false;
}
if (message->flags_ & MessagesManager::MESSAGE_FLAG_HAS_FROM_ID) { if (message->flags_ & MessagesManager::MESSAGE_FLAG_HAS_FROM_ID) {
sender_user_id = UserId(message->from_id_); if (!td_->contacts_manager_->have_user(UserId(message->from_id_))) {
return false;
}
} }
if (message->flags_ & MessagesManager::MESSAGE_FLAG_IS_FORWARDED) { if (!is_acceptable_message_forward_header(message->fwd_from_)) {
CHECK(message->fwd_from_ != nullptr);
auto flags = message->fwd_from_->flags_;
bool from_post = (flags & MessagesManager::MESSAGE_FORWARD_HEADER_FLAG_HAS_CHANNEL_ID) != 0;
if (from_post && !td_->contacts_manager_->have_channel(ChannelId(message->fwd_from_->channel_id_))) {
return false; return false;
} }
if (flags & MessagesManager::MESSAGE_FORWARD_HEADER_FLAG_HAS_AUTHOR_ID) {
UserId user_id(message->fwd_from_->from_id_);
if (from_post && !td_->contacts_manager_->have_min_user(user_id)) {
return false;
}
if (!from_post && !td_->contacts_manager_->have_user(user_id)) {
return false;
}
}
} else {
CHECK(message->fwd_from_ == nullptr);
}
if ((message->flags_ & MessagesManager::MESSAGE_FLAG_IS_SENT_VIA_BOT) && if ((message->flags_ & MessagesManager::MESSAGE_FLAG_IS_SENT_VIA_BOT) &&
!td_->contacts_manager_->have_user(UserId(message->via_bot_id_))) { !td_->contacts_manager_->have_user(UserId(message->via_bot_id_))) {
@ -434,10 +472,13 @@ bool UpdatesManager::is_acceptable_message(const telegram_api::Message *message_
case telegram_api::messageService::ID: { case telegram_api::messageService::ID: {
auto message = static_cast<const telegram_api::messageService *>(message_ptr); auto message = static_cast<const telegram_api::messageService *>(message_ptr);
is_channel_post = (message->flags_ & MessagesManager::MESSAGE_FLAG_IS_POST) != 0; if (!is_acceptable_dialog(DialogId(message->to_id_))) {
dialog_id = DialogId(message->to_id_); return false;
}
if (message->flags_ & MessagesManager::MESSAGE_FLAG_HAS_FROM_ID) { if (message->flags_ & MessagesManager::MESSAGE_FLAG_HAS_FROM_ID) {
sender_user_id = UserId(message->from_id_); if (!td_->contacts_manager_->have_user(UserId(message->from_id_))) {
return false;
}
} }
const telegram_api::MessageAction *action = message->action_.get(); const telegram_api::MessageAction *action = message->action_.get();
@ -519,43 +560,6 @@ bool UpdatesManager::is_acceptable_message(const telegram_api::Message *message_
return false; return false;
} }
switch (dialog_id.get_type()) {
case DialogType::None:
LOG(ERROR) << "Receive message in the invalid " << dialog_id;
return false;
case DialogType::User: {
if (!td_->contacts_manager_->have_user(dialog_id.get_user_id())) {
return false;
}
break;
}
case DialogType::Chat: {
if (!td_->contacts_manager_->have_chat(dialog_id.get_chat_id())) {
return false;
}
break;
}
case DialogType::Channel: {
if (!td_->contacts_manager_->have_channel(dialog_id.get_channel_id())) {
return false;
}
break;
}
case DialogType::SecretChat:
default:
UNREACHABLE();
return false;
}
if (sender_user_id != UserId()) {
if (is_channel_post && !td_->contacts_manager_->have_min_user(sender_user_id)) {
return false;
}
if (!is_channel_post && !td_->contacts_manager_->have_user(sender_user_id)) {
return false;
}
}
return true; return true;
} }

View File

@ -162,8 +162,13 @@ class UpdatesManager : public Actor {
static const vector<tl_object_ptr<telegram_api::Update>> *get_updates(const telegram_api::Updates *updates_ptr); static const vector<tl_object_ptr<telegram_api::Update>> *get_updates(const telegram_api::Updates *updates_ptr);
bool is_acceptable_dialog(DialogId dialog_id) const;
bool is_acceptable_message_entities(const vector<tl_object_ptr<telegram_api::MessageEntity>> &message_entities) const; bool is_acceptable_message_entities(const vector<tl_object_ptr<telegram_api::MessageEntity>> &message_entities) const;
bool is_acceptable_message_forward_header(
const telegram_api::object_ptr<telegram_api::messageFwdHeader> &header) const;
bool is_acceptable_message(const telegram_api::Message *message_ptr) const; bool is_acceptable_message(const telegram_api::Message *message_ptr) const;
bool is_acceptable_update(const telegram_api::Update *update) const; bool is_acceptable_update(const telegram_api::Update *update) const;