From daae77abcd36743d89c4b81d92a6e78c0e9c0fe3 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Mon, 1 Jan 2018 17:01:56 +0100 Subject: [PATCH] First commit --- .gitmodules | 3 + CMakeLists.txt | 10 ++ main.cpp | 306 +++++++++++++++++++++++++++++++++++++++++++++++++ td | 1 + 4 files changed, 320 insertions(+) create mode 100644 .gitmodules create mode 100644 CMakeLists.txt create mode 100644 main.cpp create mode 160000 td diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..b641dcc --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "td"] + path = td + url = https://github.com/tdlib/td diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..51850fa --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,10 @@ + +cmake_minimum_required(VERSION 3.1 FATAL_ERROR) + +project(TdExample VERSION 1.0 LANGUAGES CXX) + +add_subdirectory(td) + +add_executable(main main.cpp) +target_link_libraries(main PRIVATE Td::TdStatic) +set_property(TARGET main PROPERTY CXX_STANDARD 14) diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..c1480ab --- /dev/null +++ b/main.cpp @@ -0,0 +1,306 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2017 +// +// 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 +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +// Simple single-threaded example of TDLib usage. +// Real world programs should use separate thread for the user input. + +// overloaded +namespace detail { +template +struct overload; + +template +struct overload : public F { + explicit overload(F f) : F(f) { + } +}; +template +struct overload + : public overload + , overload { + overload(F f, Fs... fs) : overload(f), overload(fs...) { + } + using overload::operator(); + using overload::operator(); +}; +} // namespace detail + +template +auto overloaded(F... f) { + return detail::overload(f...); +} + +namespace td_api = td::td_api; + +class TdExample { + public: + TdExample() { + td::Log::set_verbosity_level(1); + client_ = std::make_unique(); + } + + void loop() { + while (true) { + if (need_restart_) { + restart(); + } else if (!are_authorized_) { + process_response(client_->receive(10)); + } else { + std::cerr + << "Enter action [q] quit [u] check for updates and request results [c] show chats [m ] send message [l] logout: " + << std::endl; + std::string line; + std::getline(std::cin, line); + std::istringstream ss(line); + std::string action; + if (!(ss >> action)) { + continue; + } + if (action == "q") { + return; + } + if (action == "u") { + std::cerr << "Checking for updates..." << std::endl; + while (true) { + auto response = client_->receive(0); + if (response.object) { + process_response(std::move(response)); + } else { + break; + } + } + } else if (action == "l") { + std::cerr << "Logging out..." << std::endl; + send_query(td_api::make_object(), {}); + } else if (action == "m") { + std::int64_t chat_id; + ss >> chat_id; + ss.get(); + std::string text; + std::getline(ss, text); + + std::cerr << "Sending message to chat " << chat_id << "..." << std::endl; + auto send_message = td_api::make_object(); + send_message->chat_id_ = chat_id; + auto message_content = td_api::make_object(); + message_content->text_ = std::move(text); + send_message->input_message_content_ = std::move(message_content); + + send_query(std::move(send_message), {}); + } else if (action == "c") { + std::cerr << "Loading chat list..." << std::endl; + send_query(td_api::make_object(std::numeric_limits::max(), 0, 20), + [this](Object object) { + if (object->get_id() == td_api::error::ID) { + return; + } + auto chats = td::move_tl_object_as(object); + for (auto chat_id : chats->chat_ids_) { + std::cerr << "[id:" << chat_id << "] [title:" << chat_title_[chat_id] << "]" << std::endl; + } + }); + } + } + } + } + + private: + using Object = td_api::object_ptr; + std::unique_ptr client_; + + td_api::object_ptr authorization_state_; + bool are_authorized_{false}; + bool need_restart_{false}; + std::uint64_t current_query_id_{0}; + std::uint64_t authentication_query_id_{0}; + + std::map> handlers_; + + std::map> users_; + + std::map chat_title_; + + void restart() { + client_.reset(); + *this = TdExample(); + } + + void send_query(td_api::object_ptr f, std::function handler) { + auto query_id = next_query_id(); + if (handler) { + handlers_.emplace(query_id, std::move(handler)); + } + client_->send({query_id, std::move(f)}); + } + + void process_response(td::Client::Response response) { + if (!response.object) { + return; + } + //std::cerr << response.id << " " << to_string(response.object) << std::endl; + if (response.id == 0) { + return process_update(std::move(response.object)); + } + auto it = handlers_.find(response.id); + if (it != handlers_.end()) { + it->second(std::move(response.object)); + } + } + + std::string get_user_name(std::int32_t user_id) { + auto it = users_.find(user_id); + if (it == users_.end()) { + return "unknown user"; + } + return it->second->first_name_ + " " + it->second->last_name_; + } + + void process_update(td_api::object_ptr update) { + td_api::downcast_call( + *update, overloaded( + [this](td_api::updateAuthorizationState &update_authorization_state) { + authorization_state_ = std::move(update_authorization_state.authorization_state_); + on_authorization_state_update(); + }, + [this](td_api::updateNewChat &update_new_chat) { + chat_title_[update_new_chat.chat_->id_] = update_new_chat.chat_->title_; + }, + [this](td_api::updateChatTitle &update_chat_title) { + chat_title_[update_chat_title.chat_id_] = update_chat_title.title_; + }, + [this](td_api::updateUser &update_user) { + auto user_id = update_user.user_->id_; + users_[user_id] = std::move(update_user.user_); + }, + [this](td_api::updateNewMessage &update_new_message) { + auto chat_id = update_new_message.message_->chat_id_; + auto sender_user_name = get_user_name(update_new_message.message_->sender_user_id_); + std::string text; + if (update_new_message.message_->content_->get_id() == td_api::messageText::ID) { + text = static_cast(*update_new_message.message_->content_).text_; + } + std::cerr << "Got message: [chat_id:" << chat_id << "] [from:" << sender_user_name << "] [" + << text << "]" << std::endl; + }, + [](auto &update) {})); + } + + auto create_authentication_query_handler() { + return [this, id = authentication_query_id_](Object object) { + if (id == authentication_query_id_) { + check_authentication_error(std::move(object)); + } + }; + } + + void on_authorization_state_update() { + authentication_query_id_++; + td_api::downcast_call( + *authorization_state_, + overloaded( + [this](td_api::authorizationStateReady &) { + are_authorized_ = true; + std::cerr << "Got authorization" << std::endl; + }, + [this](td_api::authorizationStateLoggingOut &) { + are_authorized_ = false; + std::cerr << "Logging out" << std::endl; + }, + [this](td_api::authorizationStateClosing &) { std::cerr << "Closing" << std::endl; }, + [this](td_api::authorizationStateClosed &) { + are_authorized_ = false; + need_restart_ = true; + std::cerr << "Terminated" << std::endl; + }, + [this](td_api::authorizationStateWaitCode &wait_code) { + std::string first_name; + std::string last_name; + if (!wait_code.is_registered_) { + std::cerr << "Enter your first name: "; + std::cin >> first_name; + std::cerr << "Enter your last name: "; + std::cin >> last_name; + } + std::cerr << "Enter authentication code: "; + std::string code; + std::cin >> code; + send_query(td_api::make_object(code, first_name, last_name), + create_authentication_query_handler()); + }, + [this](td_api::authorizationStateWaitPassword &) { + std::cerr << "Enter authentication password: "; + std::string password; + std::cin >> password; + send_query(td_api::make_object(password), + create_authentication_query_handler()); + }, + [this](td_api::authorizationStateWaitPhoneNumber &) { + std::cerr << "Enter phone number: "; + std::string phone_number; + std::cin >> phone_number; + send_query(td_api::make_object( + phone_number, false /*allow_flash_calls*/, false /*is_current_phone_number*/), + create_authentication_query_handler()); + }, + [this](td_api::authorizationStateWaitEncryptionKey &) { + std::cerr << "Enter encryption key or DESTROY: "; + std::string key; + std::getline(std::cin, key); + if (key == "DESTROY") { + send_query(td_api::make_object(), create_authentication_query_handler()); + } else { + send_query(td_api::make_object(std::move(key)), + create_authentication_query_handler()); + } + }, + [this](td_api::authorizationStateWaitTdlibParameters &) { + auto parameters = td_api::make_object(); + parameters->use_message_database_ = true; + parameters->use_secret_chats_ = true; + parameters->api_id_ = 94575; + parameters->api_hash_ = "a3406de8d171bb422bb6ddf3bbd800e2"; + parameters->system_language_code_ = "en"; + parameters->device_model_ = "Desktop"; + parameters->system_version_ = "Unknown"; + parameters->application_version_ = "1.0"; + parameters->enable_storage_optimizer_ = true; + send_query(td_api::make_object(std::move(parameters)), + create_authentication_query_handler()); + })); + } + + void check_authentication_error(Object object) { + if (object->get_id() == td_api::error::ID) { + auto error = td::move_tl_object_as(object); + std::cerr << "Error: " << to_string(error); + on_authorization_state_update(); + } + } + + std::uint64_t next_query_id() { + return ++current_query_id_; + } +}; + +int main() { + TdExample example; + example.loop(); +} + diff --git a/td b/td new file mode 160000 index 0000000..71d03f3 --- /dev/null +++ b/td @@ -0,0 +1 @@ +Subproject commit 71d03f39c364367a8a7c51f783a41099297de826