diff --git a/external/toxcore/CMakeLists.txt b/external/toxcore/CMakeLists.txt index 45fade7..95f88a4 100644 --- a/external/toxcore/CMakeLists.txt +++ b/external/toxcore/CMakeLists.txt @@ -134,6 +134,10 @@ add_library(toxcore STATIC ${TOX_DIR}toxcore/tox_unpack.h ${TOX_DIR}toxcore/util.c ${TOX_DIR}toxcore/util.h + + ${TOX_DIR}toxencryptsave/defines.h + ${TOX_DIR}toxencryptsave/toxencryptsave.c + ${TOX_DIR}toxencryptsave/toxencryptsave.h ) # HACK: "install" api headers into self diff --git a/src/main_screen.cpp b/src/main_screen.cpp index 20cb1c1..740310b 100644 --- a/src/main_screen.cpp +++ b/src/main_screen.cpp @@ -6,11 +6,11 @@ #include -MainScreen::MainScreen(SDL_Renderer* renderer_, std::string save_path, std::vector plugins) : +MainScreen::MainScreen(SDL_Renderer* renderer_, std::string save_path, std::string save_password, std::vector plugins) : renderer(renderer_), rmm(cr), mts(rmm), - tc(save_path), + tc(save_path, save_password), ad(tc), tcm(cr, tc, tc), tmm(rmm, cr, tcm, tc, tc), diff --git a/src/main_screen.hpp b/src/main_screen.hpp index 7fc3b9f..2830b5f 100644 --- a/src/main_screen.hpp +++ b/src/main_screen.hpp @@ -56,7 +56,7 @@ struct MainScreen final : public Screen { ChatGui4 cg; - MainScreen(SDL_Renderer* renderer_, std::string save_path, std::vector plugins); + MainScreen(SDL_Renderer* renderer_, std::string save_path, std::string save_password, std::vector plugins); ~MainScreen(void); bool handleEvent(SDL_Event& e) override; diff --git a/src/start_screen.cpp b/src/start_screen.cpp index 4969887..1faa911 100644 --- a/src/start_screen.cpp +++ b/src/start_screen.cpp @@ -3,6 +3,7 @@ #include "./main_screen.hpp" #include +#include #include #include @@ -24,6 +25,10 @@ Screen* StartScreen::poll(bool&) { if (ImGui::BeginTabBar("view")) { if (ImGui::BeginTabItem("load profile")) { + _new_save = false; + + ImGui::TextUnformatted("profile :"); + ImGui::SameLine(); if (ImGui::Button("select")) { _fss.requestFile( [](const auto& path) -> bool { return std::filesystem::is_regular_file(path); }, @@ -34,16 +39,36 @@ Screen* StartScreen::poll(bool&) { ); } ImGui::SameLine(); - ImGui::Text("profile: %s", tox_profile_path.c_str()); + ImGui::TextUnformatted(tox_profile_path.c_str()); - ImGui::Text("TODO: profile password"); + ImGui::TextUnformatted("password:"); + ImGui::SameLine(); + if (_show_password) { + ImGui::InputText("##password", &_password); + } else { + ImGui::InputText("##password", &_password, ImGuiInputTextFlags_Password); + } + ImGui::SameLine(); + ImGui::Checkbox("show password", &_show_password); ImGui::EndTabItem(); } if (ImGui::BeginTabItem("create profile")) { - ImGui::Text("TODO: profile path"); - ImGui::Text("TODO: profile name"); - ImGui::Text("TODO: profile password"); + _new_save = true; + + ImGui::TextUnformatted("TODO: profile path"); + ImGui::TextUnformatted("TODO: profile name"); + + ImGui::TextUnformatted("password:"); + ImGui::SameLine(); + if (_show_password) { + ImGui::InputText("##password", &_password); + } else { + ImGui::InputText("##password", &_password, ImGuiInputTextFlags_Password); + } + ImGui::SameLine(); + ImGui::Checkbox("show password", &_show_password); + ImGui::EndTabItem(); } if (ImGui::BeginTabItem("plugins")) { @@ -80,9 +105,31 @@ Screen* StartScreen::poll(bool&) { ImGui::Separator(); - if (ImGui::Button("load", {60, 25})) { - auto new_screen = std::make_unique(_renderer, tox_profile_path, queued_plugin_paths); - return new_screen.release(); + if (!_new_save && !std::filesystem::is_regular_file(tox_profile_path)) { + // load but file missing + + ImGui::BeginDisabled(); + ImGui::Button("load", {60, 25}); + ImGui::EndDisabled(); + + if (ImGui::IsItemHovered(ImGuiHoveredFlags_ForTooltip | ImGuiHoveredFlags_AllowWhenDisabled)) { + ImGui::SetTooltip("file does not exist"); + } + } else if (_new_save && std::filesystem::exists(tox_profile_path)) { + // new but file exists + + ImGui::BeginDisabled(); + ImGui::Button("load", {60, 25}); + ImGui::EndDisabled(); + + if (ImGui::IsItemHovered(ImGuiHoveredFlags_ForTooltip | ImGuiHoveredFlags_AllowWhenDisabled)) { + ImGui::SetTooltip("file already exists"); + } + } else { + if (ImGui::Button("load", {60, 25})) { + auto new_screen = std::make_unique(_renderer, tox_profile_path, _password, queued_plugin_paths); + return new_screen.release(); + } } _fss.render(); diff --git a/src/start_screen.hpp b/src/start_screen.hpp index e198678..5175f97 100644 --- a/src/start_screen.hpp +++ b/src/start_screen.hpp @@ -16,6 +16,11 @@ struct StartScreen final : public Screen { SDL_Renderer* _renderer; FileSelector _fss; + bool _new_save {false}; + + bool _show_password {false}; + std::string _password; + std::string tox_profile_path {"tomato.tox"}; std::vector queued_plugin_paths; diff --git a/src/tox_client.cpp b/src/tox_client.cpp index c0172d9..b299b1b 100644 --- a/src/tox_client.cpp +++ b/src/tox_client.cpp @@ -1,5 +1,10 @@ #include "./tox_client.hpp" +// meh, change this +#include +#include +#include + #include #include @@ -7,8 +12,8 @@ #include #include -ToxClient::ToxClient(std::string_view save_path) : - _tox_profile_path(save_path) +ToxClient::ToxClient(std::string_view save_path, std::string_view save_password) : + _tox_profile_path(save_path), _tox_profile_password(save_password) { TOX_ERR_OPTIONS_NEW err_opt_new; Tox_Options* options = tox_options_new(&err_opt_new); @@ -34,6 +39,19 @@ ToxClient::ToxClient(std::string_view save_path) : std::cerr << "empty tox save\n"; } else { // set options + if (!save_password.empty()) { + std::vector encrypted_copy(profile_data.begin(), profile_data.end()); + //profile_data.clear(); + profile_data.resize(encrypted_copy.size() - TOX_PASS_ENCRYPTION_EXTRA_LENGTH); + if (!tox_pass_decrypt( + encrypted_copy.data(), encrypted_copy.size(), + reinterpret_cast(save_password.data()), save_password.size(), + profile_data.data(), + nullptr // TODO: error checking + )) { + throw std::runtime_error("FAILED to decrypt save file!!!!"); + } + } tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE); tox_options_set_savedata_data(options, profile_data.data(), profile_data.size()); } @@ -122,6 +140,19 @@ void ToxClient::saveToxProfile(void) { data.resize(tox_get_savedata_size(_tox)); tox_get_savedata(_tox, data.data()); + if (!_tox_profile_password.empty()) { + std::vector unencrypted_copy(data.begin(), data.end()); + //profile_data.clear(); + data.resize(unencrypted_copy.size() + TOX_PASS_ENCRYPTION_EXTRA_LENGTH); + if (!tox_pass_encrypt( + unencrypted_copy.data(), unencrypted_copy.size(), + reinterpret_cast(_tox_profile_password.data()), _tox_profile_password.size(), + data.data(), + nullptr // TODO: error checking + )) { + throw std::runtime_error("FAILED to encrypt save file!!!!"); + } + } std::ofstream ofile{_tox_profile_path, std::ios::binary}; // TODO: improve for (const auto& ch : data) { diff --git a/src/tox_client.hpp b/src/tox_client.hpp index 739d1de..208c95b 100644 --- a/src/tox_client.hpp +++ b/src/tox_client.hpp @@ -20,11 +20,12 @@ class ToxClient : public ToxDefaultImpl, public ToxEventProviderBase { std::string _self_name; std::string _tox_profile_path; + std::string _tox_profile_password; bool _tox_profile_dirty {true}; // set in callbacks public: //ToxClient(/*const CommandLine& cl*/); - ToxClient(std::string_view save_path); + ToxClient(std::string_view save_path, std::string_view save_password); ~ToxClient(void); public: // tox stuff