2018-12-31 20:04:05 +01:00
|
|
|
//
|
2021-01-01 13:57:46 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/telegram/Global.h"
|
|
|
|
#include "td/telegram/Version.h"
|
|
|
|
|
|
|
|
#include "td/utils/buffer.h"
|
2019-02-12 22:26:36 +01:00
|
|
|
#include "td/utils/common.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/format.h"
|
|
|
|
#include "td/utils/logging.h"
|
2019-05-03 15:53:34 +02:00
|
|
|
#include "td/utils/misc.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
#include "td/utils/Status.h"
|
2019-02-12 17:48:52 +01:00
|
|
|
#include "td/utils/StorerBase.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/StringBuilder.h"
|
|
|
|
#include "td/utils/tl_helpers.h"
|
|
|
|
#include "td/utils/tl_parsers.h"
|
|
|
|
#include "td/utils/tl_storers.h"
|
|
|
|
|
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
namespace td {
|
2020-09-22 01:15:09 +02:00
|
|
|
namespace log_event {
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
template <class ParentT>
|
|
|
|
class WithVersion : public ParentT {
|
|
|
|
public:
|
|
|
|
using ParentT::ParentT;
|
|
|
|
void set_version(int32 version) {
|
|
|
|
version_ = version;
|
|
|
|
}
|
|
|
|
int32 version() const {
|
|
|
|
return version_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-10-26 16:11:20 +02:00
|
|
|
int32 version_{};
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class ParentT, class ContextT>
|
|
|
|
class WithContext : public ParentT {
|
|
|
|
public:
|
|
|
|
using ParentT::ParentT;
|
|
|
|
void set_context(ContextT context) {
|
|
|
|
context_ = context;
|
|
|
|
}
|
|
|
|
ContextT context() const {
|
|
|
|
return context_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-10-26 16:11:20 +02:00
|
|
|
ContextT context_{};
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class LogEvent {
|
|
|
|
public:
|
|
|
|
LogEvent() = default;
|
|
|
|
LogEvent(const LogEvent &) = delete;
|
|
|
|
LogEvent &operator=(const LogEvent &) = delete;
|
|
|
|
virtual ~LogEvent() = default;
|
|
|
|
enum HandlerType : uint32 {
|
|
|
|
SecretChats = 1,
|
|
|
|
Users = 2,
|
|
|
|
Chats = 3,
|
|
|
|
Channels = 4,
|
|
|
|
SecretChatInfos = 5,
|
|
|
|
WebPages = 0x10,
|
2019-02-21 15:40:37 +01:00
|
|
|
SetPollAnswer = 0x20,
|
2019-02-22 16:09:55 +01:00
|
|
|
StopPoll = 0x21,
|
2018-12-31 20:04:05 +01:00
|
|
|
SendMessage = 0x100,
|
|
|
|
DeleteMessage = 0x101,
|
|
|
|
DeleteMessagesFromServer = 0x102,
|
|
|
|
ReadHistoryOnServer = 0x103,
|
|
|
|
ForwardMessages = 0x104,
|
|
|
|
ReadMessageContentsOnServer = 0x105,
|
|
|
|
SendBotStartMessage = 0x106,
|
|
|
|
SendScreenshotTakenNotificationMessage = 0x107,
|
|
|
|
SendInlineQueryResultMessage = 0x108,
|
|
|
|
DeleteDialogHistoryFromServer = 0x109,
|
|
|
|
ReadAllDialogMentionsOnServer = 0x10a,
|
|
|
|
DeleteAllChannelMessagesFromUserOnServer = 0x10b,
|
|
|
|
ToggleDialogIsPinnedOnServer = 0x10c,
|
|
|
|
ReorderPinnedDialogsOnServer = 0x10d,
|
|
|
|
SaveDialogDraftMessageOnServer = 0x10e,
|
2018-04-28 21:31:42 +02:00
|
|
|
UpdateDialogNotificationSettingsOnServer = 0x10f,
|
|
|
|
UpdateScopeNotificationSettingsOnServer = 0x110,
|
2018-04-28 21:50:12 +02:00
|
|
|
ResetAllNotificationSettingsOnServer = 0x111,
|
2018-04-29 07:58:45 +02:00
|
|
|
ChangeDialogReportSpamStateOnServer = 0x112,
|
2018-06-01 18:58:23 +02:00
|
|
|
GetDialogFromServer = 0x113,
|
2018-06-20 03:02:02 +02:00
|
|
|
ReadHistoryInSecretChat = 0x114,
|
2018-06-27 23:08:44 +02:00
|
|
|
ToggleDialogIsMarkedAsUnreadOnServer = 0x115,
|
2019-08-27 16:23:01 +02:00
|
|
|
SetDialogFolderIdOnServer = 0x116,
|
2019-11-29 13:03:03 +01:00
|
|
|
DeleteScheduledMessagesFromServer = 0x117,
|
2020-09-20 02:00:01 +02:00
|
|
|
ToggleDialogIsBlockedOnServer = 0x118,
|
2020-09-22 00:13:06 +02:00
|
|
|
ReadMessageThreadHistoryOnServer = 0x119,
|
2020-10-18 00:26:36 +02:00
|
|
|
BlockMessageSenderFromRepliesOnServer = 0x120,
|
2020-10-23 00:56:06 +02:00
|
|
|
UnpinAllDialogMessagesOnServer = 0x121,
|
2021-01-19 11:21:50 +01:00
|
|
|
DeleteAllCallMessagesFromServer = 0x122,
|
2018-12-31 20:04:05 +01:00
|
|
|
GetChannelDifference = 0x140,
|
2019-03-31 03:30:25 +02:00
|
|
|
AddMessagePushNotification = 0x200,
|
2019-04-07 22:57:54 +02:00
|
|
|
EditMessagePushNotification = 0x201,
|
2018-12-31 20:04:05 +01:00
|
|
|
ConfigPmcMagic = 0x1f18,
|
|
|
|
BinlogPmcMagic = 0x4327
|
|
|
|
};
|
|
|
|
|
|
|
|
using Id = uint64;
|
|
|
|
|
2020-09-22 01:15:09 +02:00
|
|
|
Id log_event_id() const {
|
|
|
|
return log_event_id_;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-09-22 01:15:09 +02:00
|
|
|
void set_log_event_id(Id log_event_id) {
|
|
|
|
log_event_id_ = log_event_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual StringBuilder &print(StringBuilder &sb) const {
|
2020-09-22 01:15:09 +02:00
|
|
|
return sb << "[Logevent " << tag("id", log_event_id()) << "]";
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-09-22 01:15:09 +02:00
|
|
|
Id log_event_id_{};
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
inline StringBuilder &operator<<(StringBuilder &sb, const LogEvent &log_event) {
|
|
|
|
return log_event.print(sb);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
template <class EventT>
|
|
|
|
int32 magic(EventT &event) {
|
|
|
|
return static_cast<int32>(event.get_type());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class EventT, class StorerT>
|
|
|
|
void store(const EventT &event, StorerT &storer) {
|
|
|
|
EventT::downcast_call(event.get_type(),
|
|
|
|
[&](auto *ptr) { static_cast<const std::decay_t<decltype(*ptr)> &>(event).store(storer); });
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class DestT, class T>
|
2018-09-27 03:19:03 +02:00
|
|
|
Result<unique_ptr<DestT>> from_parser(T &&parser) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto version = parser.fetch_int();
|
|
|
|
parser.set_version(version);
|
|
|
|
parser.set_context(G());
|
|
|
|
auto magic = static_cast<typename DestT::Type>(parser.fetch_int());
|
|
|
|
|
2018-09-27 03:19:03 +02:00
|
|
|
unique_ptr<DestT> event;
|
2018-12-31 20:04:05 +01:00
|
|
|
DestT::downcast_call(magic, [&](auto *ptr) {
|
|
|
|
auto tmp = make_unique<std::decay_t<decltype(*ptr)>>();
|
|
|
|
tmp->parse(parser);
|
|
|
|
event = std::move(tmp);
|
|
|
|
});
|
|
|
|
parser.fetch_end();
|
|
|
|
TRY_STATUS(parser.get_status());
|
|
|
|
if (event) {
|
|
|
|
return std::move(event);
|
|
|
|
}
|
|
|
|
return Status::Error(PSLICE() << "Unknown SecretChatEvent type: " << format::as_hex(magic));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class DestT>
|
2018-09-27 03:19:03 +02:00
|
|
|
Result<unique_ptr<DestT>> from_buffer_slice(BufferSlice slice) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return from_parser<DestT>(WithVersion<WithContext<TlBufferParser, Global *>>{&slice});
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
class StorerImpl : public Storer {
|
|
|
|
public:
|
|
|
|
explicit StorerImpl(const T &event) : event_(event) {
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t size() const override {
|
|
|
|
WithContext<TlStorerCalcLength, Global *> storer;
|
|
|
|
storer.set_context(G());
|
|
|
|
|
|
|
|
storer.store_int(T::version());
|
|
|
|
td::store(magic(event_), storer);
|
|
|
|
td::store(event_, storer);
|
|
|
|
return storer.get_length();
|
|
|
|
}
|
2018-06-25 01:58:43 +02:00
|
|
|
size_t store(uint8 *ptr) const override {
|
2018-12-31 20:04:05 +01:00
|
|
|
WithContext<TlStorerUnsafe, Global *> storer(ptr);
|
|
|
|
storer.set_context(G());
|
|
|
|
|
|
|
|
storer.store_int(T::version());
|
|
|
|
td::store(magic(event_), storer);
|
|
|
|
td::store(event_, storer);
|
2018-06-25 01:58:43 +02:00
|
|
|
return static_cast<size_t>(storer.get_buf() - ptr);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const T &event_;
|
|
|
|
};
|
2019-02-22 16:33:14 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
template <class ChildT>
|
|
|
|
class LogEventBase : public LogEvent {
|
|
|
|
public:
|
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &storer) const {
|
|
|
|
detail::store(static_cast<const ChildT &>(*this), storer);
|
|
|
|
}
|
2018-09-27 03:19:03 +02:00
|
|
|
static Result<unique_ptr<ChildT>> from_buffer_slice(BufferSlice slice) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return detail::from_buffer_slice<ChildT>(std::move(slice));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class LogEventParser : public WithVersion<WithContext<TlParser, Global *>> {
|
|
|
|
public:
|
|
|
|
explicit LogEventParser(Slice data) : WithVersion<WithContext<TlParser, Global *>>(data) {
|
|
|
|
set_version(fetch_int());
|
2019-02-12 17:17:20 +01:00
|
|
|
LOG_CHECK(version() < static_cast<int32>(Version::Next)) << "Wrong version " << version();
|
2018-12-31 20:04:05 +01:00
|
|
|
set_context(G());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class LogEventStorerCalcLength : public WithContext<TlStorerCalcLength, Global *> {
|
|
|
|
public:
|
|
|
|
LogEventStorerCalcLength() : WithContext<TlStorerCalcLength, Global *>() {
|
|
|
|
store_int(static_cast<int32>(Version::Next) - 1);
|
|
|
|
set_context(G());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class LogEventStorerUnsafe : public WithContext<TlStorerUnsafe, Global *> {
|
|
|
|
public:
|
2018-06-25 01:58:43 +02:00
|
|
|
explicit LogEventStorerUnsafe(unsigned char *buf) : WithContext<TlStorerUnsafe, Global *>(buf) {
|
2018-12-31 20:04:05 +01:00
|
|
|
store_int(static_cast<int32>(Version::Next) - 1);
|
|
|
|
set_context(G());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
class LogEventStorerImpl : public Storer {
|
|
|
|
public:
|
|
|
|
explicit LogEventStorerImpl(const T &event) : event_(event) {
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t size() const override {
|
|
|
|
LogEventStorerCalcLength storer;
|
|
|
|
td::store(event_, storer);
|
|
|
|
return storer.get_length();
|
|
|
|
}
|
2018-06-25 01:58:43 +02:00
|
|
|
size_t store(uint8 *ptr) const override {
|
2018-12-31 20:04:05 +01:00
|
|
|
LogEventStorerUnsafe storer(ptr);
|
|
|
|
td::store(event_, storer);
|
|
|
|
#ifdef TD_DEBUG
|
|
|
|
T check_result;
|
|
|
|
log_event_parse(check_result, Slice(ptr, storer.get_buf())).ensure();
|
|
|
|
#endif
|
2018-06-25 01:58:43 +02:00
|
|
|
return static_cast<size_t>(storer.get_buf() - ptr);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const T &event_;
|
|
|
|
};
|
|
|
|
|
2020-09-22 01:15:09 +02:00
|
|
|
} // namespace log_event
|
2020-09-22 00:39:45 +02:00
|
|
|
|
2020-09-22 01:15:09 +02:00
|
|
|
using LogEvent = log_event::LogEvent;
|
|
|
|
using LogEventParser = log_event::LogEventParser;
|
|
|
|
using LogEventStorerCalcLength = log_event::LogEventStorerCalcLength;
|
|
|
|
using LogEventStorerUnsafe = log_event::LogEventStorerUnsafe;
|
2020-09-22 00:39:45 +02:00
|
|
|
|
|
|
|
template <class T>
|
|
|
|
Status log_event_parse(T &data, Slice slice) TD_WARN_UNUSED_RESULT;
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
Status log_event_parse(T &data, Slice slice) {
|
|
|
|
LogEventParser parser(slice);
|
|
|
|
parse(data, parser);
|
|
|
|
parser.fetch_end();
|
|
|
|
return parser.get_status();
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class T>
|
|
|
|
BufferSlice log_event_store(const T &data) {
|
|
|
|
LogEventStorerCalcLength storer_calc_length;
|
|
|
|
store(data, storer_calc_length);
|
|
|
|
|
|
|
|
BufferSlice value_buffer{storer_calc_length.get_length()};
|
2019-05-03 15:53:34 +02:00
|
|
|
auto ptr = value_buffer.as_slice().ubegin();
|
|
|
|
LOG_CHECK(is_aligned_pointer<4>(ptr)) << ptr;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-05-03 15:53:34 +02:00
|
|
|
LogEventStorerUnsafe storer_unsafe(ptr);
|
2018-12-31 20:04:05 +01:00
|
|
|
store(data, storer_unsafe);
|
|
|
|
|
|
|
|
#ifdef TD_DEBUG
|
|
|
|
T check_result;
|
|
|
|
log_event_parse(check_result, value_buffer.as_slice()).ensure();
|
|
|
|
#endif
|
|
|
|
return value_buffer;
|
|
|
|
}
|
|
|
|
|
2020-09-22 00:39:45 +02:00
|
|
|
template <class T>
|
2020-09-22 01:15:09 +02:00
|
|
|
log_event::LogEventStorerImpl<T> get_log_event_storer(const T &event) {
|
|
|
|
return log_event::LogEventStorerImpl<T>(event);
|
2020-09-22 00:39:45 +02:00
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|