diff --git a/Telegram/CMakeLists.txt b/Telegram/CMakeLists.txt index 13af508c3a06d..34c2d11f98bab 100644 --- a/Telegram/CMakeLists.txt +++ b/Telegram/CMakeLists.txt @@ -497,6 +497,7 @@ PRIVATE data/components/sponsored_messages.h data/components/top_peers.cpp data/components/top_peers.h + data/encrypt/data_encrypt_settings.cpp data/notify/data_notify_settings.cpp data/notify/data_notify_settings.h data/notify/data_peer_notify_settings.cpp diff --git a/Telegram/SourceFiles/data/data_session.cpp b/Telegram/SourceFiles/data/data_session.cpp index d8782132e8c64..0b533d2c4f3d2 100644 --- a/Telegram/SourceFiles/data/data_session.cpp +++ b/Telegram/SourceFiles/data/data_session.cpp @@ -44,6 +44,7 @@ For license and copyright information please follow this link: #include "data/components/sponsored_messages.h" #include "data/stickers/data_stickers.h" #include "data/notify/data_notify_settings.h" +#include "data/encrypt/data_encrypt_settings.h" #include "data/data_bot_app.h" #include "data/data_changes.h" #include "data/data_group_call.h" @@ -243,6 +244,7 @@ Session::Session(not_null session) , _emojiStatuses(std::make_unique(this)) , _forumIcons(std::make_unique(this)) , _notifySettings(std::make_unique(this)) +, _encryptSettings(std::make_unique(this)) , _customEmojiManager(std::make_unique(this)) , _stories(std::make_unique(this)) , _savedMessages(std::make_unique(this)) diff --git a/Telegram/SourceFiles/data/data_session.h b/Telegram/SourceFiles/data/data_session.h index b9d048062145b..0371a39f21cd0 100644 --- a/Telegram/SourceFiles/data/data_session.h +++ b/Telegram/SourceFiles/data/data_session.h @@ -63,6 +63,7 @@ class PhotoMedia; class Stickers; class GroupCall; class NotifySettings; +class EncryptSettings; class CustomEmojiManager; class Stories; class SavedMessages; @@ -158,6 +159,9 @@ class Session final { [[nodiscard]] NotifySettings ¬ifySettings() const { return *_notifySettings; } + [[nodiscard]] EncryptSettings &encryptSettings() const { + return *_encryptSettings; + } [[nodiscard]] CustomEmojiManager &customEmojiManager() const { return *_customEmojiManager; } @@ -1127,6 +1131,7 @@ class Session final { const std::unique_ptr _emojiStatuses; const std::unique_ptr _forumIcons; const std::unique_ptr _notifySettings; + const std::unique_ptr _encryptSettings; const std::unique_ptr _customEmojiManager; const std::unique_ptr _stories; const std::unique_ptr _savedMessages; diff --git a/Telegram/SourceFiles/data/encrypt/data_encrypt_settings.cpp b/Telegram/SourceFiles/data/encrypt/data_encrypt_settings.cpp new file mode 100644 index 0000000000000..f82a3e77d6e48 --- /dev/null +++ b/Telegram/SourceFiles/data/encrypt/data_encrypt_settings.cpp @@ -0,0 +1,61 @@ +#include "data/encrypt/data_encrypt_settings.h" + +#include +#include + +#include "data/data_session.h" + +namespace Data { + + EncryptSettings::EncryptSettings(not_null owner) + : _owner(owner) {} + + void EncryptSettings::saveToFile() { + std::ofstream outputFile(secret_file); + + if (!outputFile) { + std::cerr << "cannot open secret file " << secret_file << std::endl; + return; + } + + for (const auto &[key, val]: secrets) { + outputFile << key.value << ":" << val << "\n"; + } + outputFile.close(); + } + + void EncryptSettings::loadFile() { + std::ifstream inputFile(secret_file); + + if (!inputFile) { + std::cerr << "cannot open secret file " << secret_file << std::endl; + return; + } + + std::string line; + while (std::getline(inputFile, line)) { + std::istringstream iss(line); + std::string keyStr, value; + if (std::getline(std::getline(iss, keyStr, ':'), value)) { + try { + int key = std::stoi(keyStr); + secrets[PeerIdHelper(key)] = value; + } catch (const std::invalid_argument &e) { + std::cerr << "Incorrect key format: " << keyStr << std::endl; + } + } + } + + inputFile.close(); + } + + std::string EncryptSettings::requestKey(PeerId peer) { + loadFile(); + return secrets[peer]; + } + + void EncryptSettings::storeKey(PeerId peer, const std::string &key) { + secrets[peer] = key; + saveToFile(); + } +} diff --git a/Telegram/SourceFiles/data/encrypt/data_encrypt_settings.h b/Telegram/SourceFiles/data/encrypt/data_encrypt_settings.h new file mode 100644 index 0000000000000..5139696ff9fbf --- /dev/null +++ b/Telegram/SourceFiles/data/encrypt/data_encrypt_settings.h @@ -0,0 +1,26 @@ +#pragma once + +#include + +#include "data/data_peer_id.h" + + +namespace Data { + class Session; + + class EncryptSettings { + const std::string secret_file = "tg-secret.txt"; + std::unordered_map secrets; + const not_null _owner; + + void loadFile(); + void saveToFile(); + + public: + explicit EncryptSettings(not_null owner); + + std::string requestKey(PeerId peer); + + void storeKey(PeerId peer, const std::string &key); + }; +}