diff --git a/CMakeLists.txt b/CMakeLists.txt index 12767bb6d..666cf19e4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -296,6 +296,7 @@ set(TDLIB_SOURCE td/telegram/BackgroundInfo.cpp td/telegram/BackgroundManager.cpp td/telegram/BackgroundType.cpp + td/telegram/Birthdate.cpp td/telegram/BoostManager.cpp td/telegram/BotCommand.cpp td/telegram/BotCommandScope.cpp @@ -585,6 +586,7 @@ set(TDLIB_SOURCE td/telegram/BackgroundInfo.h td/telegram/BackgroundManager.h td/telegram/BackgroundType.h + td/telegram/Birthdate.h td/telegram/BlockListId.h td/telegram/BoostManager.h td/telegram/BotCommand.h @@ -903,6 +905,7 @@ set(TDLIB_SOURCE td/telegram/AuthManager.hpp td/telegram/BackgroundInfo.hpp td/telegram/BackgroundType.hpp + td/telegram/Birthdate.hpp td/telegram/BusinessAwayMessage.hpp td/telegram/BusinessAwayMessageSchedule.hpp td/telegram/BusinessConnectedBot.hpp diff --git a/SplitSource.php b/SplitSource.php index e3acffc0e..adb4b0bb7 100644 --- a/SplitSource.php +++ b/SplitSource.php @@ -306,6 +306,7 @@ function split_file($file, $chunks, $undo) { 'BackgroundId' => 'BackgroundId', 'background_manager[_(-](?![.]get[(][)])|BackgroundManager' => 'BackgroundManager', 'BackgroundType' => 'BackgroundType', + 'Birthdate' => 'Birthdate', 'BotMenuButton|[a-z_]*_menu_button' => 'BotMenuButton', 'boost_manager[_(-](?![.]get[(][)])|BoostManager' => 'BoostManager', 'bot_info_manager[_(-](?![.]get[(][)])|BotInfoManager' => 'BotInfoManager', diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 14489c8d8..3c0ce1bbb 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -576,6 +576,10 @@ botMenuButton text:string url:string = BotMenuButton; chatLocation location:location address:string = ChatLocation; +//@description Represents a birthdate of a user @day Day of the month; 1-31 @month Month of the year; 1-12 @year Birth year; 0 if unknown +birthdate day:int32 month:int32 year:int32 = Birthdate; + + //@class BusinessAwayMessageSchedule @description Describes conditions for sending of away messages by a Telegram Business account //@description Send away messages always diff --git a/td/telegram/Birthdate.cpp b/td/telegram/Birthdate.cpp new file mode 100644 index 000000000..d0b0c8c08 --- /dev/null +++ b/td/telegram/Birthdate.cpp @@ -0,0 +1,71 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024 +// +// 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) +// +#include "td/telegram/Birthdate.h" + +#include "td/telegram/ConfigManager.h" + +#include "td/utils/misc.h" + +namespace td { + +void Birthdate::init(int32 day, int32 month, int32 year) { + if (year < 1800 || year > 3000) { + year = 0; + } + if (month <= 0 || month > 12 || day <= 0 || day > HttpDate::days_in_month(year, month)) { + return; + } + birthdate_ = day | (month << 5) | (year << 9); +} + +Birthdate::Birthdate(telegram_api::object_ptr birthday) { + if (birthday == nullptr) { + return; + } + init(birthday->day_, birthday->month_, birthday->year_); +} + +Birthdate::Birthdate(td_api::object_ptr birthdate) { + if (birthdate == nullptr) { + return; + } + init(birthdate->day_, birthdate->month_, birthdate->year_); +} + +td_api::object_ptr Birthdate::get_birthdate_object() const { + if (is_empty()) { + return nullptr; + } + return td_api::make_object(get_day(), get_month(), get_year()); +} + +telegram_api::object_ptr Birthdate::get_input_birthday() const { + int32 flags = 0; + auto year = get_year(); + if (year != 0) { + flags |= telegram_api::birthday::YEAR_MASK; + } + return telegram_api::make_object(flags, get_day(), get_month(), year); +} + +bool operator==(const Birthdate &lhs, const Birthdate &rhs) { + return lhs.birthdate_ == rhs.birthdate_; +} + +StringBuilder &operator<<(StringBuilder &string_builder, const Birthdate &birthdate) { + if (birthdate.is_empty()) { + return string_builder << "unknown birthdate"; + } + string_builder << "birtdate " << birthdate.get_day() << '.' << birthdate.get_month(); + auto year = birthdate.get_year(); + if (year != 0) { + string_builder << '.' << year; + } + return string_builder; +} + +} // namespace td diff --git a/td/telegram/Birthdate.h b/td/telegram/Birthdate.h new file mode 100644 index 000000000..da48b1ac9 --- /dev/null +++ b/td/telegram/Birthdate.h @@ -0,0 +1,69 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024 +// +// 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/td_api.h" +#include "td/telegram/telegram_api.h" + +#include "td/utils/common.h" +#include "td/utils/StringBuilder.h" + +namespace td { + +class Birthdate { + public: + Birthdate() = default; + + explicit Birthdate(telegram_api::object_ptr birthday); + + explicit Birthdate(td_api::object_ptr birthdate); + + td_api::object_ptr get_birthdate_object() const; + + telegram_api::object_ptr get_input_birthday() const; + + bool is_empty() const { + return birthdate_ == 0; + } + + template + void store(StorerT &storer) const; + + template + void parse(ParserT &parser); + + private: + int32 birthdate_ = 0; + + int32 get_day() const { + return birthdate_ & 31; + } + + int32 get_month() const { + return (birthdate_ >> 5) & 15; + } + + int32 get_year() const { + return birthdate_ >> 9; + } + + void init(int32 day, int32 month, int32 year); + + friend bool operator==(const Birthdate &lhs, const Birthdate &rhs); + + friend StringBuilder &operator<<(StringBuilder &string_builder, const Birthdate &birthdate); +}; + +bool operator==(const Birthdate &lhs, const Birthdate &rhs); + +inline bool operator!=(const Birthdate &lhs, const Birthdate &rhs) { + return !(lhs == rhs); +} + +StringBuilder &operator<<(StringBuilder &string_builder, const Birthdate &birthdate); + +} // namespace td diff --git a/td/telegram/Birthdate.hpp b/td/telegram/Birthdate.hpp new file mode 100644 index 000000000..b797a7ed2 --- /dev/null +++ b/td/telegram/Birthdate.hpp @@ -0,0 +1,25 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024 +// +// 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/Birthdate.h" + +#include "td/utils/tl_helpers.h" + +namespace td { + +template +void Birthdate::store(StorerT &storer) const { + td::store(birthdate_, storer); +} + +template +void Birthdate::parse(ParserT &parser) { + td::parse(birthdate_, parser); +} + +} // namespace td diff --git a/td/telegram/ConfigManager.h b/td/telegram/ConfigManager.h index bf05cc9c3..a1ee66b28 100644 --- a/td/telegram/ConfigManager.h +++ b/td/telegram/ConfigManager.h @@ -60,15 +60,16 @@ class HttpDate { static bool is_leap(int32 year) { return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0); } - static int32 days_in_month(int32 year, int32 month) { - static int cnt[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; - return cnt[month - 1] + (month == 2 && is_leap(year)); - } static int32 seconds_in_day() { return 24 * 60 * 60; } public: + static int32 days_in_month(int32 year, int32 month) { + static int cnt[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + return cnt[month - 1] + (month == 2 && is_leap(year)); + } + static Result to_unix_time(int32 year, int32 month, int32 day, int32 hour, int32 minute, int32 second); static Result parse_http_date(std::string slice); };