diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 52ea356..6cfb8b7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -67,7 +67,7 @@ jobs: with: cond: ${{ matrix.mindeps == true }} if_true: | - cmake -GNinja -Bbuild -DCMAKE_BUILD_TYPE=${{ matrix.buildtype }} -DUSE_LIBHANDY=OFF -DENABLE_VOICE=OFF -DENABLE_NOTIFICATION_SOUNDS=OFF + cmake -GNinja -Bbuild -DCMAKE_BUILD_TYPE=${{ matrix.buildtype }} -DUSE_LIBHANDY=OFF -DENABLE_VOICE=OFF -DENABLE_NOTIFICATION_SOUNDS=OFF -DENABLE_QRCODE_LOGIN=OFF cmake --build build if_false: | cmake -GNinja -Bbuild -DCMAKE_BUILD_TYPE=${{ matrix.buildtype }} -DCMAKE_CXX_FLAGS="-Wl,--default-image-base-low" @@ -95,7 +95,6 @@ jobs: mkdir -p 16x16/devices 24x24/devices 32x32/devices 48x48/devices 64x64/devices 96x96/devices scalable/devices mkdir -p 16x16/status 24x24/status 32x32/status 48x48/status 64x64/status 96x96/status scalable/status cd ${GITHUB_WORKSPACE} - cp ci/gtk-for-windows/gtk-nsis-pack/share/icons/Adwaita/icon-theme.cache ${artifact_dir}/share/icons/Adwaita/icon-theme.cache cp ci/gtk-for-windows/gtk-nsis-pack/share/icons/Adwaita/index.theme ${artifact_dir}/share/icons/Adwaita/index.theme cat "ci/used-icons.txt" | sed 's/\r$//' | xargs -I % cp ci/gtk-for-windows/gtk-nsis-pack/share/icons/Adwaita/16x16/%.symbolic.png ${artifact_dir}/share/icons/Adwaita/16x16/%.symbolic.png || : cat "ci/used-icons.txt" | sed 's/\r$//' | xargs -I % cp ci/gtk-for-windows/gtk-nsis-pack/share/icons/Adwaita/24x24/%.symbolic.png ${artifact_dir}/share/icons/Adwaita/24x24/%.symbolic.png || : @@ -104,6 +103,8 @@ jobs: cat "ci/used-icons.txt" | sed 's/\r$//' | xargs -I % cp ci/gtk-for-windows/gtk-nsis-pack/share/icons/Adwaita/64x64/%.symbolic.png ${artifact_dir}/share/icons/Adwaita/64x64/%.symbolic.png || : cat "ci/used-icons.txt" | sed 's/\r$//' | xargs -I % cp ci/gtk-for-windows/gtk-nsis-pack/share/icons/Adwaita/96x96/%.symbolic.png ${artifact_dir}/share/icons/Adwaita/96x96/%.symbolic.png || : cat "ci/used-icons.txt" | sed 's/\r$//' | xargs -I % cp ci/gtk-for-windows/gtk-nsis-pack/share/icons/Adwaita/scalable/%.svg ${artifact_dir}/share/icons/Adwaita/scalable/%.svg || : + cd ${artifact_dir}/share/icons/Adwaita + gtk-update-icon-cache . - name: Upload build (1) uses: haya14busa/action-cond@v1 diff --git a/.gitmodules b/.gitmodules index 42df2be..e173bb4 100644 --- a/.gitmodules +++ b/.gitmodules @@ -16,3 +16,6 @@ [submodule "subprojects/rnnoise"] path = subprojects/rnnoise url = https://github.com/xiph/rnnoise +[submodule "subprojects/qrcodegen"] + path = subprojects/qrcodegen + url = https://github.com/nayuki/QR-Code-generator diff --git a/CMakeLists.txt b/CMakeLists.txt index 6bdacc8..ab51f67 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,6 +12,7 @@ option(ENABLE_VOICE "Enable voice suppport" ON) option(USE_KEYCHAIN "Store the token in the keychain (default)" ON) option(ENABLE_NOTIFICATION_SOUNDS "Enable notification sounds (default)" ON) option(ENABLE_RNNOISE "Enable RNNoise for voice activity detection (default)" ON) +option(ENABLE_QRCODE_LOGIN "Enable QR code login (default)" ON) find_package(nlohmann_json REQUIRED) find_package(CURL) @@ -62,6 +63,15 @@ target_include_directories(abaddon PUBLIC ${ZLIB_INCLUDE_DIRS}) target_include_directories(abaddon PUBLIC ${SQLite3_INCLUDE_DIRS}) target_include_directories(abaddon PUBLIC ${NLOHMANN_JSON_INCLUDE_DIRS}) +if (ENABLE_QRCODE_LOGIN) + add_library(qrcodegen subprojects/qrcodegen/cpp/qrcodegen.hpp subprojects/qrcodegen/cpp/qrcodegen.cpp) + target_include_directories(qrcodegen PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/subprojects/qrcodegen/cpp") + target_link_libraries(abaddon qrcodegen) + + target_include_directories(abaddon PUBLIC "subprojects/qrcodegen/cpp") + target_compile_definitions(abaddon PRIVATE WITH_QRLOGIN) +endif () + target_precompile_headers(abaddon PRIVATE src/abaddon.hpp src/util.hpp) if ((CMAKE_CXX_COMPILER_ID STREQUAL "GNU") OR @@ -180,6 +190,12 @@ if (ENABLE_VOICE) target_link_libraries(abaddon rnnoise) endif () endif () + + if (APPLE) + target_link_libraries(abaddon "-framework CoreFoundation") + target_link_libraries(abaddon "-framework CoreAudio") + target_link_libraries(abaddon "-framework AudioToolbox") + endif () endif () if (${ENABLE_NOTIFICATION_SOUNDS}) @@ -188,6 +204,11 @@ if (${ENABLE_NOTIFICATION_SOUNDS}) endif () if (USE_MINIAUDIO) - target_include_directories(abaddon PUBLIC subprojects/miniaudio) + find_path(MINIAUDIO_INCLUDE_DIR + NAMES miniaudio.h + HINTS subprojects + PATH_SUFFIXES miniaudio + REQUIRED) + target_include_directories(abaddon PUBLIC ${MINIAUDIO_INCLUDE_DIR}) target_compile_definitions(abaddon PRIVATE WITH_MINIAUDIO) endif () diff --git a/README.md b/README.md index c66af60..70996b2 100644 --- a/README.md +++ b/README.md @@ -226,32 +226,33 @@ Used in guild settings popup: Used in profile popup: -| Selector | Description | -|------------------------------|---------------------------------------------------------| -| `.mutual-friend-item` | Applied to every item in the mutual friends list | -| `.mutual-friend-item-name` | Name in mutual friend item | -| `.mutual-friend-item-avatar` | Avatar in mutual friend item | -| `.mutual-guild-item` | Applied to every item in the mutual guilds list | -| `.mutual-guild-item-name` | Name in mutual guild item | -| `.mutual-guild-item-icon` | Icon in mutual guild item | -| `.mutual-guild-item-nick` | User nickname in mutual guild item | -| `.profile-connection` | Applied to every item in the user connections list | -| `.profile-connection-label` | Label in profile connection item | -| `.profile-connection-check` | Checkmark in verified profile connection items | -| `.profile-connections` | Container for profile connections | -| `.profile-notes` | Container for notes in profile window | -| `.profile-notes-label` | Label that says "NOTE" | -| `.profile-notes-text` | Actual note text | -| `.profile-info-pane` | Applied to container for info section of profile popup | -| `.profile-info-created` | Label for creation date of profile | -| `.user-profile-window` | | -| `.profile-main-container` | Inner container for profile | -| `.profile-avatar` | | -| `.profile-username` | | -| `.profile-switcher` | Buttons used to switch viewed section of profile | -| `.profile-stack` | Container for profile info that can be switched between | -| `.profile-badges` | Container for badges | -| `.profile-badge` | | +| Selector | Description | +|--------------------------------|------------------------------------------------------------| +| `.mutual-friend-item` | Applied to every item in the mutual friends list | +| `.mutual-friend-item-name` | Name in mutual friend item | +| `.mutual-friend-item-avatar` | Avatar in mutual friend item | +| `.mutual-guild-item` | Applied to every item in the mutual guilds list | +| `.mutual-guild-item-name` | Name in mutual guild item | +| `.mutual-guild-item-icon` | Icon in mutual guild item | +| `.mutual-guild-item-nick` | User nickname in mutual guild item | +| `.profile-connection` | Applied to every item in the user connections list | +| `.profile-connection-label` | Label in profile connection item | +| `.profile-connection-check` | Checkmark in verified profile connection items | +| `.profile-connections` | Container for profile connections | +| `.profile-notes` | Container for notes in profile window | +| `.profile-notes-label` | Label that says "NOTE" | +| `.profile-notes-text` | Actual note text | +| `.profile-info-pane` | Applied to container for info section of profile popup | +| `.profile-info-created` | Label for creation date of profile | +| `.user-profile-window` | | +| `.profile-main-container` | Inner container for profile | +| `.profile-avatar` | | +| `.profile-username` | User's display name (username for backwards compatibility) | +| `.profile-username-nondisplay` | User's actual username | +| `.profile-switcher` | Buttons used to switch viewed section of profile | +| `.profile-stack` | Container for profile info that can be switched between | +| `.profile-badges` | Container for badges | +| `.profile-badge` | | ### Settings diff --git a/res/css/main.css b/res/css/main.css index dcbdf4f..b84b502 100644 --- a/res/css/main.css +++ b/res/css/main.css @@ -114,6 +114,10 @@ border: 1px solid #026FB9; } +.message-input.editing { + border: 1px solid #b9026f; +} + .message-input.bad-input { border: 1px solid #dd3300; } @@ -237,6 +241,10 @@ font-size: 20px; } +.profile-username-nondisplay { + margin-left: 10px; +} + .profile-badge { margin-right: 10px; } @@ -382,3 +390,8 @@ .voice-state-server { color: red; } + +spinbutton { + color: @text_color; + margin-top: 10px; +} diff --git a/src/abaddon.cpp b/src/abaddon.cpp index a1ed343..2be54ba 100644 --- a/src/abaddon.cpp +++ b/src/abaddon.cpp @@ -8,7 +8,6 @@ #include "audio/manager.hpp" #include "discord/discord.hpp" #include "dialogs/token.hpp" -#include "dialogs/editmessage.hpp" #include "dialogs/confirm.hpp" #include "dialogs/setstatus.hpp" #include "dialogs/friendpicker.hpp" @@ -21,6 +20,7 @@ #include "windows/voicewindow.hpp" #include "startup.hpp" #include "notifications/notifications.hpp" +#include "remoteauth/remoteauthdialog.hpp" #ifdef WITH_LIBHANDY #include @@ -252,6 +252,12 @@ int Abaddon::StartGTK() { } #endif +#ifdef _WIN32 + if (m_settings.GetSettings().HideConsole) { + ShowWindow(GetConsoleWindow(), SW_HIDE); + } +#endif + // store must be checked before this can be called m_main_window->UpdateComponents(); @@ -261,6 +267,7 @@ int Abaddon::StartGTK() { m_main_window->signal_action_connect().connect(sigc::mem_fun(*this, &Abaddon::ActionConnect)); m_main_window->signal_action_disconnect().connect(sigc::mem_fun(*this, &Abaddon::ActionDisconnect)); m_main_window->signal_action_set_token().connect(sigc::mem_fun(*this, &Abaddon::ActionSetToken)); + m_main_window->signal_action_login_qr().connect(sigc::mem_fun(*this, &Abaddon::ActionLoginQR)); m_main_window->signal_action_reload_css().connect(sigc::mem_fun(*this, &Abaddon::ActionReloadCSS)); m_main_window->signal_action_set_status().connect(sigc::mem_fun(*this, &Abaddon::ActionSetStatus)); m_main_window->signal_action_add_recipient().connect(sigc::mem_fun(*this, &Abaddon::ActionAddRecipient)); @@ -828,6 +835,21 @@ void Abaddon::ActionSetToken() { m_main_window->UpdateMenus(); } +void Abaddon::ActionLoginQR() { +#ifdef WITH_QRLOGIN + RemoteAuthDialog dlg(*m_main_window); + auto response = dlg.run(); + if (response == Gtk::RESPONSE_OK) { + m_discord_token = dlg.GetToken(); + m_discord.UpdateToken(m_discord_token); + m_main_window->UpdateComponents(); + GetSettings().DiscordToken = m_discord_token; + ActionConnect(); + } + m_main_window->UpdateMenus(); +#endif +} + void Abaddon::ActionChannelOpened(Snowflake id, bool expand_to) { if (!id.IsValid()) { m_discord.SetReferringChannel(Snowflake::Invalid); @@ -914,24 +936,26 @@ void Abaddon::ActionChatLoadHistory(Snowflake id) { } void Abaddon::ActionChatInputSubmit(ChatSubmitParams data) { - if (data.Message.substr(0, 7) == "/shrug " || data.Message == "/shrug") + if (data.Message.substr(0, 7) == "/shrug " || data.Message == "/shrug") { data.Message = data.Message.substr(6) + "\xC2\xAF\x5C\x5F\x28\xE3\x83\x84\x29\x5F\x2F\xC2\xAF"; // this is important + } + + if (data.Message.substr(0, 8) == "@silent " || (data.Message.substr(0, 7) == "@silent" && !data.Attachments.empty())) { + data.Silent = true; + data.Message = data.Message.substr(7); + } if (!m_discord.HasChannelPermission(m_discord.GetUserData().ID, data.ChannelID, Permission::VIEW_CHANNEL)) return; - m_discord.SendChatMessage(data, NOOP_CALLBACK); + if (data.EditingID.IsValid()) { + m_discord.EditMessage(data.ChannelID, data.EditingID, data.Message); + } else { + m_discord.SendChatMessage(data, NOOP_CALLBACK); + } } void Abaddon::ActionChatEditMessage(Snowflake channel_id, Snowflake id) { - const auto msg = m_discord.GetMessage(id); - if (!msg.has_value()) return; - EditMessageDialog dlg(*m_main_window); - dlg.SetContent(msg->Content); - auto response = dlg.run(); - if (response == Gtk::RESPONSE_OK) { - auto new_content = dlg.GetContent(); - m_discord.EditMessage(channel_id, id, new_content); - } + m_main_window->EditMessage(id); } void Abaddon::ActionInsertMention(Snowflake id) { @@ -952,7 +976,7 @@ void Abaddon::ActionKickMember(Snowflake user_id, Snowflake guild_id) { ConfirmDialog dlg(*m_main_window); const auto user = m_discord.GetUser(user_id); if (user.has_value()) - dlg.SetConfirmText("Are you sure you want to kick " + user->Username + "#" + user->Discriminator + "?"); + dlg.SetConfirmText("Are you sure you want to kick " + user->GetUsername() + "?"); auto response = dlg.run(); if (response == Gtk::RESPONSE_OK) m_discord.KickUser(user_id, guild_id); @@ -962,7 +986,7 @@ void Abaddon::ActionBanMember(Snowflake user_id, Snowflake guild_id) { ConfirmDialog dlg(*m_main_window); const auto user = m_discord.GetUser(user_id); if (user.has_value()) - dlg.SetConfirmText("Are you sure you want to ban " + user->Username + "#" + user->Discriminator + "?"); + dlg.SetConfirmText("Are you sure you want to ban " + user->GetUsername() + "?"); auto response = dlg.run(); if (response == Gtk::RESPONSE_OK) m_discord.BanUser(user_id, guild_id); @@ -1127,9 +1151,11 @@ int main(int argc, char **argv) { #endif spdlog::cfg::load_env_levels(); + auto log_ui = spdlog::stdout_color_mt("ui"); auto log_audio = spdlog::stdout_color_mt("audio"); auto log_voice = spdlog::stdout_color_mt("voice"); auto log_discord = spdlog::stdout_color_mt("discord"); + auto log_ra = spdlog::stdout_color_mt("remote-auth"); Gtk::Main::init_gtkmm_internals(); // why??? return Abaddon::Get().StartGTK(); diff --git a/src/abaddon.hpp b/src/abaddon.hpp index 08070ae..85b2fa0 100644 --- a/src/abaddon.hpp +++ b/src/abaddon.hpp @@ -41,6 +41,7 @@ public: void ActionConnect(); void ActionDisconnect(); void ActionSetToken(); + void ActionLoginQR(); void ActionJoinGuildDialog(); void ActionChannelOpened(Snowflake id, bool expand_to = true); void ActionChatInputSubmit(ChatSubmitParams data); diff --git a/src/audio/ma_impl.cpp b/src/audio/ma_impl.cpp index a83ddaf..531b24f 100644 --- a/src/audio/ma_impl.cpp +++ b/src/audio/ma_impl.cpp @@ -1,4 +1,7 @@ #ifdef WITH_MINIAUDIO #define MINIAUDIO_IMPLEMENTATION + #ifdef __APPLE__ + #define MA_NO_RUNTIME_LINKING + #endif #include #endif diff --git a/src/audio/manager.cpp b/src/audio/manager.cpp index 3a24079..f86c484 100644 --- a/src/audio/manager.cpp +++ b/src/audio/manager.cpp @@ -96,19 +96,23 @@ AudioManager::AudioManager() { if (const auto playback_id = m_devices.GetDefaultPlayback(); playback_id.has_value()) { m_playback_id = *playback_id; m_playback_config.playback.pDeviceID = &m_playback_id; - } - if (ma_device_init(&m_context, &m_playback_config, &m_playback_device) != MA_SUCCESS) { - spdlog::get("audio")->error("failed to initialize playback device"); - m_ok = false; - return; - } + if (ma_device_init(&m_context, &m_playback_config, &m_playback_device) != MA_SUCCESS) { + spdlog::get("audio")->error("failed to initialize playback device"); + m_ok = false; + return; + } - if (ma_device_start(&m_playback_device) != MA_SUCCESS) { - spdlog::get("audio")->error("failed to start playback"); - ma_device_uninit(&m_playback_device); - m_ok = false; - return; + if (ma_device_start(&m_playback_device) != MA_SUCCESS) { + spdlog::get("audio")->error("failed to start playback"); + ma_device_uninit(&m_playback_device); + m_ok = false; + return; + } + + char playback_device_name[MA_MAX_DEVICE_NAME_LENGTH + 1]; + ma_device_get_name(&m_playback_device, ma_device_type_playback, playback_device_name, sizeof(playback_device_name), nullptr); + spdlog::get("audio")->info("using {} as playback device", playback_device_name); } m_capture_config = ma_device_config_init(ma_device_type_capture); @@ -122,22 +126,18 @@ AudioManager::AudioManager() { if (const auto capture_id = m_devices.GetDefaultCapture(); capture_id.has_value()) { m_capture_id = *capture_id; m_capture_config.capture.pDeviceID = &m_capture_id; + + if (ma_device_init(&m_context, &m_capture_config, &m_capture_device) != MA_SUCCESS) { + spdlog::get("audio")->error("failed to initialize capture device"); + m_ok = false; + return; + } + + char capture_device_name[MA_MAX_DEVICE_NAME_LENGTH + 1]; + ma_device_get_name(&m_capture_device, ma_device_type_capture, capture_device_name, sizeof(capture_device_name), nullptr); + spdlog::get("audio")->info("using {} as capture device", capture_device_name); } - if (ma_device_init(&m_context, &m_capture_config, &m_capture_device) != MA_SUCCESS) { - spdlog::get("audio")->error("failed to initialize capture device"); - m_ok = false; - return; - } - - char playback_device_name[MA_MAX_DEVICE_NAME_LENGTH + 1]; - ma_device_get_name(&m_playback_device, ma_device_type_playback, playback_device_name, sizeof(playback_device_name), nullptr); - spdlog::get("audio")->info("using {} as playback device", playback_device_name); - - char capture_device_name[MA_MAX_DEVICE_NAME_LENGTH + 1]; - ma_device_get_name(&m_capture_device, ma_device_type_capture, capture_device_name, sizeof(capture_device_name), nullptr); - spdlog::get("audio")->info("using {} as capture device", capture_device_name); - Glib::signal_timeout().connect(sigc::mem_fun(*this, &AudioManager::DecayVolumeMeters), 40); } diff --git a/src/components/channels.cpp b/src/components/channels.cpp index 0e97837..9fd4abd 100644 --- a/src/components/channels.cpp +++ b/src/components/channels.cpp @@ -42,7 +42,7 @@ ChannelList::ChannelList() const auto type = row[m_columns.m_type]; // text channels should not be allowed to be collapsed // maybe they should be but it seems a little difficult to handle expansion to permit this - if (type != RenderType::TextChannel) { + if (type != RenderType::TextChannel && type != RenderType::DM) { if (row[m_columns.m_expanded]) { m_view.collapse_row(path); row[m_columns.m_expanded] = false; @@ -527,6 +527,7 @@ void ChannelList::OnThreadListSync(const ThreadListSyncData &data) { #ifdef WITH_VOICE void ChannelList::OnVoiceUserConnect(Snowflake user_id, Snowflake channel_id) { auto parent_iter = GetIteratorForRowFromIDOfType(channel_id, RenderType::VoiceChannel); + if (!parent_iter) parent_iter = GetIteratorForRowFromIDOfType(channel_id, RenderType::DM); if (!parent_iter) return; const auto user = Abaddon::Get().GetDiscordClient().GetUser(user_id); if (!user.has_value()) return; @@ -863,7 +864,7 @@ Gtk::TreeModel::iterator ChannelList::CreateVoiceParticipantRow(const UserData & auto row = *m_model->append(parent); row[m_columns.m_type] = RenderType::VoiceParticipant; row[m_columns.m_id] = user.ID; - row[m_columns.m_name] = user.GetEscapedName(); + row[m_columns.m_name] = user.GetDisplayNameEscaped(); const auto voice_state = Abaddon::Get().GetDiscordClient().GetVoiceState(user.ID); if (voice_state.has_value()) { @@ -1015,20 +1016,17 @@ void ChannelList::AddPrivateChannels() { row[m_columns.m_name] = Glib::Markup::escape_text(dm->GetDisplayName()); row[m_columns.m_sort] = static_cast(-(dm->LastMessageID.has_value() ? *dm->LastMessageID : dm_id)); row[m_columns.m_icon] = img.GetPlaceholder(DMIconSize); + row[m_columns.m_expanded] = true; - if (dm->HasIcon()) { - const auto cb = [this, iter](const Glib::RefPtr &pb) { - if (iter) - (*iter)[m_columns.m_icon] = pb->scale_simple(DMIconSize, DMIconSize, Gdk::INTERP_BILINEAR); - }; - img.LoadFromURL(dm->GetIconURL(), sigc::track_obj(cb, *this)); - } else if (top_recipient.has_value()) { - const auto cb = [this, iter](const Glib::RefPtr &pb) { - if (iter) - (*iter)[m_columns.m_icon] = pb->scale_simple(DMIconSize, DMIconSize, Gdk::INTERP_BILINEAR); - }; - img.LoadFromURL(top_recipient->GetAvatarURL("png", "32"), sigc::track_obj(cb, *this)); +#ifdef WITH_VOICE + for (auto user_id : discord.GetUsersInVoiceChannel(dm_id)) { + if (const auto user = discord.GetUser(user_id); user.has_value()) { + CreateVoiceParticipantRow(*user, row->children()); + } } +#endif + + SetDMChannelIcon(iter, *dm); } } @@ -1036,11 +1034,6 @@ void ChannelList::UpdateCreateDMChannel(const ChannelData &dm) { auto header_row = m_model->get_iter(m_dm_header); auto &img = Abaddon::Get().GetImageManager(); - std::optional top_recipient; - const auto recipients = dm.GetDMRecipients(); - if (!recipients.empty()) - top_recipient = recipients[0]; - auto iter = m_model->append(header_row->children()); auto row = *iter; row[m_columns.m_type] = RenderType::DM; @@ -1049,12 +1042,74 @@ void ChannelList::UpdateCreateDMChannel(const ChannelData &dm) { row[m_columns.m_sort] = static_cast(-(dm.LastMessageID.has_value() ? *dm.LastMessageID : dm.ID)); row[m_columns.m_icon] = img.GetPlaceholder(DMIconSize); - if (top_recipient.has_value()) { + SetDMChannelIcon(iter, dm); +} + +void ChannelList::SetDMChannelIcon(Gtk::TreeIter iter, const ChannelData &dm) { + auto &img = Abaddon::Get().GetImageManager(); + + std::optional top_recipient; + const auto recipients = dm.GetDMRecipients(); + if (!recipients.empty()) + top_recipient = recipients[0]; + + if (dm.HasIcon()) { + const auto cb = [this, iter](const Glib::RefPtr &pb) { + if (iter) + (*iter)[m_columns.m_icon] = pb->scale_simple(DMIconSize, DMIconSize, Gdk::INTERP_BILINEAR); + }; + img.LoadFromURL(dm.GetIconURL(), sigc::track_obj(cb, *this)); + } else if (dm.Type == ChannelType::DM && top_recipient.has_value()) { const auto cb = [this, iter](const Glib::RefPtr &pb) { if (iter) (*iter)[m_columns.m_icon] = pb->scale_simple(DMIconSize, DMIconSize, Gdk::INTERP_BILINEAR); }; img.LoadFromURL(top_recipient->GetAvatarURL("png", "32"), sigc::track_obj(cb, *this)); + } else { // GROUP_DM + std::string hash; + switch (dm.ID.GetUnixMilliseconds() % 8) { + case 0: + hash = "ee9275c5a437f7dc7f9430ba95f12ebd"; + break; + case 1: + hash = "9baf45aac2a0ec2e2dab288333acb9d9"; + break; + case 2: + hash = "7ba11ffb1900fa2b088cb31324242047"; + break; + case 3: + hash = "f90fca70610c4898bc57b58bce92f587"; + break; + case 4: + hash = "e2779af34b8d9126b77420e5f09213ce"; + break; + case 5: + hash = "c6851bd0b03f1cca5a8c1e720ea6ea17"; + break; + case 6: + hash = "f7e38ac976a2a696161c923502a8345b"; + break; + case 7: + default: + hash = "3cb840d03313467838d658bbec801fcd"; + break; + } + const auto cb = [this, iter](const Glib::RefPtr &pb) { + if (iter) + (*iter)[m_columns.m_icon] = pb->scale_simple(DMIconSize, DMIconSize, Gdk::INTERP_BILINEAR); + }; + img.LoadFromURL("https://discord.com/assets/" + hash + ".png", sigc::track_obj(cb, *this)); + } +} + +void ChannelList::RedrawUnreadIndicatorsForChannel(const ChannelData &channel) { + if (channel.GuildID.has_value()) { + auto iter = GetIteratorForGuildFromID(*channel.GuildID); + if (iter) m_model->row_changed(m_model->get_path(iter), iter); + } + if (channel.ParentID.has_value()) { + auto iter = GetIteratorForRowFromIDOfType(*channel.ParentID, RenderType::Category); + if (iter) m_model->row_changed(m_model->get_path(iter), iter); } } @@ -1064,9 +1119,8 @@ void ChannelList::OnMessageAck(const MessageAckData &data) { auto iter = GetIteratorForRowFromID(data.ChannelID); if (iter) m_model->row_changed(m_model->get_path(iter), iter); auto channel = Abaddon::Get().GetDiscordClient().GetChannel(data.ChannelID); - if (channel.has_value() && channel->GuildID.has_value()) { - iter = GetIteratorForGuildFromID(*channel->GuildID); - if (iter) m_model->row_changed(m_model->get_path(iter), iter); + if (channel.has_value()) { + RedrawUnreadIndicatorsForChannel(*channel); } } @@ -1079,9 +1133,7 @@ void ChannelList::OnMessageCreate(const Message &msg) { if (iter) (*iter)[m_columns.m_sort] = static_cast(-msg.ID); } - if (channel->GuildID.has_value()) - if ((iter = GetIteratorForGuildFromID(*channel->GuildID))) - m_model->row_changed(m_model->get_path(iter), iter); + RedrawUnreadIndicatorsForChannel(*channel); } bool ChannelList::OnButtonPressEvent(GdkEventButton *ev) { diff --git a/src/components/channels.hpp b/src/components/channels.hpp index 7a23b3d..9d449e4 100644 --- a/src/components/channels.hpp +++ b/src/components/channels.hpp @@ -114,10 +114,12 @@ protected: void AddPrivateChannels(); void UpdateCreateDMChannel(const ChannelData &channel); + void SetDMChannelIcon(Gtk::TreeIter iter, const ChannelData &dm); + void RedrawUnreadIndicatorsForChannel(const ChannelData& channel); void OnMessageAck(const MessageAckData &data); - void OnMessageCreate(const Message &msg); + Gtk::TreeModel::Path m_path_for_menu; // cant be recovered through selection diff --git a/src/components/channelscellrenderer.cpp b/src/components/channelscellrenderer.cpp index 6de7a00..ac98512 100644 --- a/src/components/channelscellrenderer.cpp +++ b/src/components/channelscellrenderer.cpp @@ -410,6 +410,17 @@ void CellRendererChannels::get_preferred_height_for_width_vfunc_category(Gtk::Wi m_renderer_text.get_preferred_height_for_width(widget, width, minimum_height, natural_height); } +void AddUnreadIndicator(const Cairo::RefPtr &cr, const Gdk::Rectangle &background_area) { + static const auto color = Gdk::RGBA(Abaddon::Get().GetSettings().UnreadIndicatorColor); + cr->set_source_rgb(color.get_red(), color.get_green(), color.get_blue()); + const auto x = background_area.get_x(); + const auto y = background_area.get_y(); + const auto w = background_area.get_width(); + const auto h = background_area.get_height(); + cr->rectangle(x, y, 3, h); + cr->fill(); +} + void CellRendererChannels::render_vfunc_category(const Cairo::RefPtr &cr, Gtk::Widget &widget, const Gdk::Rectangle &background_area, const Gdk::Rectangle &cell_area, Gtk::CellRendererState flags) { // todo: figure out how Gtk::Arrow is rendered because i like it better :^) constexpr static int len = 5; @@ -447,13 +458,18 @@ void CellRendererChannels::render_vfunc_category(const Cairo::RefPtr 0) { + AddUnreadIndicator(cr, background_area); + } m_renderer_text.property_foreground_rgba() = color; } m_renderer_text.render(cr, widget, background_area, text_cell_area, flags); @@ -519,14 +535,7 @@ void CellRendererChannels::render_vfunc_channel(const Cairo::RefPtrset_source_rgb(color.get_red(), color.get_green(), color.get_blue()); - const auto x = background_area.get_x(); - const auto y = background_area.get_y(); - const auto w = background_area.get_width(); - const auto h = background_area.get_height(); - cr->rectangle(x, y, 3, h); - cr->fill(); + AddUnreadIndicator(cr, background_area); } if (unread_state < 1) return; diff --git a/src/components/chatinput.cpp b/src/components/chatinput.cpp index d19ac4b..1133302 100644 --- a/src/components/chatinput.cpp +++ b/src/components/chatinput.cpp @@ -41,9 +41,15 @@ bool ChatInputText::ProcessKeyPress(GdkEventKey *event) { return true; } +#ifdef __APPLE__ + if ((event->state & GDK_MOD2_MASK) && event->keyval == GDK_KEY_v) { + return CheckHandleClipboardPaste(); + } +#else if ((event->state & GDK_CONTROL_MASK) && event->keyval == GDK_KEY_v) { return CheckHandleClipboardPaste(); } +#endif if (event->keyval == GDK_KEY_Return) { if (event->state & GDK_SHIFT_MASK) @@ -491,6 +497,10 @@ void ChatInput::InsertText(const Glib::ustring &text) { m_input.Get().InsertText(text); } +void ChatInput::Clear() { + GetBuffer()->set_text(""); +} + Glib::RefPtr ChatInput::GetBuffer() { return m_input.Get().GetBuffer(); } @@ -559,6 +569,24 @@ void ChatInput::StopReplying() { m_input.Get().get_style_context()->remove_class("replying"); } +void ChatInput::StartEditing(const Message &message) { + m_is_editing = true; + m_input.Get().grab_focus(); + m_input.Get().get_style_context()->add_class("editing"); + GetBuffer()->set_text(message.Content); + m_attachments.Clear(); + m_attachments_revealer.set_reveal_child(false); +} + +void ChatInput::StopEditing() { + m_is_editing = false; + m_input.Get().get_style_context()->remove_class("editing"); +} + +bool ChatInput::IsEmpty() { + return GetBuffer()->get_char_count() == 0; +} + bool ChatInput::AddFileAsImageAttachment(const Glib::RefPtr &file) { try { const auto read_stream = file->read(); @@ -571,7 +599,7 @@ bool ChatInput::AddFileAsImageAttachment(const Glib::RefPtr &file) { } bool ChatInput::CanAttachFiles() { - return Abaddon::Get().GetDiscordClient().HasSelfChannelPermission(m_active_channel, Permission::ATTACH_FILES | Permission::SEND_MESSAGES); + return !m_is_editing && Abaddon::Get().GetDiscordClient().HasSelfChannelPermission(m_active_channel, Permission::ATTACH_FILES | Permission::SEND_MESSAGES); } ChatInput::type_signal_submit ChatInput::signal_submit() { diff --git a/src/components/chatinput.hpp b/src/components/chatinput.hpp index bc6a45d..a3c9742 100644 --- a/src/components/chatinput.hpp +++ b/src/components/chatinput.hpp @@ -129,6 +129,7 @@ public: ChatInput(); void InsertText(const Glib::ustring &text); + void Clear(); Glib::RefPtr GetBuffer(); bool ProcessKeyPress(GdkEventKey *event); void AddAttachment(const Glib::RefPtr &file); @@ -139,6 +140,11 @@ public: void StartReplying(); void StopReplying(); + void StartEditing(const Message &message); + void StopEditing(); + + bool IsEmpty(); + private: bool AddFileAsImageAttachment(const Glib::RefPtr &file); bool CanAttachFiles(); @@ -149,6 +155,8 @@ private: Snowflake m_active_channel; + bool m_is_editing = false; + public: using type_signal_submit = sigc::signal; using type_signal_escape = sigc::signal; diff --git a/src/components/chatinputindicator.cpp b/src/components/chatinputindicator.cpp index 13315c6..0611e71 100644 --- a/src/components/chatinputindicator.cpp +++ b/src/components/chatinputindicator.cpp @@ -54,6 +54,12 @@ void ChatInputIndicator::AddUser(Snowflake channel_id, const UserData &user, int void ChatInputIndicator::SetActiveChannel(Snowflake id) { m_active_channel = id; + const auto channel = Abaddon::Get().GetDiscordClient().GetChannel(id); + if (channel.has_value()) { + m_active_guild = channel->GuildID; + } else { + m_active_guild = std::nullopt; + } ComputeTypingString(); } @@ -105,14 +111,14 @@ void ChatInputIndicator::ComputeTypingString() { if (typers.empty()) { SetTypingString(""); } else if (typers.size() == 1) { - SetTypingString(typers[0].Username + " is typing..."); + SetTypingString(typers[0].GetDisplayName(m_active_guild) + " is typing..."); } else if (typers.size() == 2) { - SetTypingString(typers[0].Username + " and " + typers[1].Username + " are typing..."); + SetTypingString(typers[0].GetDisplayName(m_active_guild) + " and " + typers[1].GetDisplayName(m_active_guild) + " are typing..."); } else if (typers.size() > 2 && typers.size() <= MaxUsersInIndicator) { Glib::ustring str; for (size_t i = 0; i < typers.size() - 1; i++) - str += typers[i].Username + ", "; - SetTypingString(str + "and " + typers[typers.size() - 1].Username + " are typing..."); + str += typers[i].GetDisplayName(m_active_guild) + ", "; + SetTypingString(str + "and " + typers[typers.size() - 1].GetDisplayName(m_active_guild) + " are typing..."); } else { // size() > MaxUsersInIndicator SetTypingString("Several people are typing..."); } diff --git a/src/components/chatinputindicator.hpp b/src/components/chatinputindicator.hpp index 40c966e..5688393 100644 --- a/src/components/chatinputindicator.hpp +++ b/src/components/chatinputindicator.hpp @@ -23,5 +23,6 @@ private: Glib::ustring m_custom_markup; Snowflake m_active_channel; + std::optional m_active_guild; std::unordered_map> m_typers; // channel id -> [user id -> connection] }; diff --git a/src/components/chatlist.cpp b/src/components/chatlist.cpp index 93fb46f..ba18c0a 100644 --- a/src/components/chatlist.cpp +++ b/src/components/chatlist.cpp @@ -133,10 +133,9 @@ void ChatList::ProcessNewMessage(const Message &data, bool prepend) { m_menu_delete_message->set_sensitive(false); m_menu_edit_message->set_sensitive(false); } else { - const bool can_edit = client.GetUserData().ID == data->Author.ID; - const bool can_delete = can_edit || has_manage; + const bool can_delete = (client.GetUserData().ID == data->Author.ID) || has_manage; m_menu_delete_message->set_sensitive(can_delete); - m_menu_edit_message->set_sensitive(can_edit); + m_menu_edit_message->set_sensitive(data->IsEditable()); } m_menu.popup_at_pointer(reinterpret_cast(ev)); @@ -253,6 +252,25 @@ void ChatList::ActuallyRemoveMessage(Snowflake id) { RemoveMessageAndHeader(it->second); } +std::optional ChatList::GetLastSentEditableMessage() { + const auto &discord = Abaddon::Get().GetDiscordClient(); + const auto self_id = discord.GetUserData().ID; + + std::map ordered(m_id_to_widget.begin(), m_id_to_widget.end()); + + for (auto it = ordered.crbegin(); it != ordered.crend(); it++) { + const auto *widget = dynamic_cast(it->second); + if (widget == nullptr) continue; + const auto msg = discord.GetMessage(widget->ID); + if (!msg.has_value()) continue; + if (msg->Author.ID != self_id) continue; + if (!msg->IsEditable()) continue; + return msg->ID; + } + + return std::nullopt; +} + void ChatList::SetupMenu() { m_menu_copy_id = Gtk::manage(new Gtk::MenuItem("Copy ID")); m_menu_copy_id->signal_activate().connect([this] { diff --git a/src/components/chatlist.hpp b/src/components/chatlist.hpp index 0248d7f..9f12df9 100644 --- a/src/components/chatlist.hpp +++ b/src/components/chatlist.hpp @@ -23,6 +23,7 @@ public: void SetSeparateAll(bool separate); void SetUsePinnedMenu(); // i think i need a better way to do menus void ActuallyRemoveMessage(Snowflake id); // perhaps not the best method name + std::optional GetLastSentEditableMessage(); private: void SetupMenu(); diff --git a/src/components/chatmessage.cpp b/src/components/chatmessage.cpp index 5f4c9a5..23ee36f 100644 --- a/src/components/chatmessage.cpp +++ b/src/components/chatmessage.cpp @@ -221,36 +221,36 @@ void ChatMessageItemContainer::UpdateTextComponent(Gtk::TextView *tv) { if (data->Mentions.empty()) break; const auto &adder = Abaddon::Get().GetDiscordClient().GetUser(data->Author.ID); const auto &added = data->Mentions[0]; - b->insert_markup(s, "" + adder->Username + " added " + added.Username + ""); + b->insert_markup(s, "" + adder->GetUsername() + " added " + added.GetUsername() + ""); } break; case MessageType::RECIPIENT_REMOVE: { if (data->Mentions.empty()) break; const auto &adder = Abaddon::Get().GetDiscordClient().GetUser(data->Author.ID); const auto &added = data->Mentions[0]; if (adder->ID == added.ID) - b->insert_markup(s, "" + adder->Username + " left"); + b->insert_markup(s, "" + adder->GetUsername() + " left"); else - b->insert_markup(s, "" + adder->Username + " removed " + added.Username + ""); + b->insert_markup(s, "" + adder->GetUsername() + " removed " + added.GetUsername() + ""); } break; case MessageType::CHANNEL_NAME_CHANGE: { const auto author = Abaddon::Get().GetDiscordClient().GetUser(data->Author.ID); - b->insert_markup(s, "" + author->GetEscapedBoldName() + " changed the name to " + Glib::Markup::escape_text(data->Content) + ""); + b->insert_markup(s, "" + author->GetDisplayNameEscapedBold() + " changed the name to " + Glib::Markup::escape_text(data->Content) + ""); } break; case MessageType::CHANNEL_ICON_CHANGE: { const auto author = Abaddon::Get().GetDiscordClient().GetUser(data->Author.ID); - b->insert_markup(s, "" + author->GetEscapedBoldName() + " changed the channel icon"); + b->insert_markup(s, "" + author->GetDisplayNameEscapedBold() + " changed the channel icon"); } break; case MessageType::USER_PREMIUM_GUILD_SUBSCRIPTION_TIER_1: case MessageType::USER_PREMIUM_GUILD_SUBSCRIPTION_TIER_2: case MessageType::USER_PREMIUM_GUILD_SUBSCRIPTION_TIER_3: { const auto author = Abaddon::Get().GetDiscordClient().GetUser(data->Author.ID); const auto guild = Abaddon::Get().GetDiscordClient().GetGuild(*data->GuildID); - b->insert_markup(s, "" + author->GetEscapedBoldName() + " just boosted the server " + Glib::Markup::escape_text(data->Content) + " times! " + + b->insert_markup(s, "" + author->GetDisplayNameEscapedBold() + " just boosted the server " + Glib::Markup::escape_text(data->Content) + " times! " + Glib::Markup::escape_text(guild->Name) + " has achieved Level " + std::to_string(static_cast(data->Type) - 8) + "!"); // oo cheeky me !!! } break; case MessageType::CHANNEL_FOLLOW_ADD: { const auto author = Abaddon::Get().GetDiscordClient().GetUser(data->Author.ID); - b->insert_markup(s, "" + author->GetEscapedBoldName() + " has added " + Glib::Markup::escape_text(data->Content) + " to this channel. Its most important updates will show up here."); + b->insert_markup(s, "" + author->GetDisplayNameEscapedBold() + " has added " + Glib::Markup::escape_text(data->Content) + " to this channel. Its most important updates will show up here."); } break; case MessageType::CALL: { b->insert_markup(s, "[started a call]"); @@ -270,13 +270,13 @@ void ChatMessageItemContainer::UpdateTextComponent(Gtk::TextView *tv) { case MessageType::THREAD_CREATED: { const auto author = Abaddon::Get().GetDiscordClient().GetUser(data->Author.ID); if (data->MessageReference.has_value() && data->MessageReference->ChannelID.has_value()) { - auto iter = b->insert_markup(s, "" + author->GetEscapedBoldName() + " started a thread: "); + auto iter = b->insert_markup(s, "" + author->GetDisplayNameEscapedBold() + " started a thread: "); auto tag = b->create_tag(); tag->property_weight() = Pango::WEIGHT_BOLD; m_channel_tagmap[tag] = *data->MessageReference->ChannelID; b->insert_with_tag(iter, data->Content, tag); } else { - b->insert_markup(s, "" + author->GetEscapedBoldName() + " started a thread: " + Glib::Markup::escape_text(data->Content) + ""); + b->insert_markup(s, "" + author->GetDisplayNameEscapedBold() + " started a thread: " + Glib::Markup::escape_text(data->Content) + ""); } } break; default: break; @@ -656,7 +656,7 @@ Gtk::Widget *ChatMessageItemContainer::CreateReplyComponent(const Message &data) if (role.has_value()) { const auto author = discord.GetUser(author_id); if (author.has_value()) { - return "Color) + "\">" + author->GetEscapedString() + ""; + return "Color) + "\">" + author->GetDisplayNameEscaped(guild_id) + ""; } } } @@ -664,7 +664,7 @@ Gtk::Widget *ChatMessageItemContainer::CreateReplyComponent(const Message &data) const auto author = discord.GetUser(author_id); if (author.has_value()) { - return author->GetEscapedBoldString(); + return author->GetDisplayNameEscapedBold(guild_id); } return "Unknown User"; @@ -674,8 +674,9 @@ Gtk::Widget *ChatMessageItemContainer::CreateReplyComponent(const Message &data) std::optional> referenced_message = data.ReferencedMessage; if (data.MessageReference.has_value() && data.MessageReference->MessageID.has_value() && !referenced_message.has_value()) { auto refd = discord.GetMessage(*data.MessageReference->MessageID); - if (refd.has_value()) + if (refd.has_value()) { referenced_message = std::make_shared(std::move(*refd)); + } } if (data.Interaction.has_value()) { @@ -685,7 +686,7 @@ Gtk::Widget *ChatMessageItemContainer::CreateReplyComponent(const Message &data) Glib::Markup::escape_text(data.Interaction->Name) + ""); } else if (const auto user = discord.GetUser(data.Interaction->User.ID); user.has_value()) { - lbl->set_markup(user->GetEscapedBoldString()); + lbl->set_markup(user->GetDisplayNameEscapedBold()); } else { lbl->set_markup("Unknown User"); } @@ -1011,6 +1012,7 @@ ChatMessageHeader::ChatMessageHeader(const Message &data) add(m_main_box); set_margin_bottom(8); + set_focus_on_click(false); show_all(); @@ -1042,7 +1044,7 @@ void ChatMessageHeader::UpdateName() { else m_author.set_markup("" + name + ""); } else - m_author.set_markup("" + user->GetEscapedName() + ""); + m_author.set_markup("" + user->GetDisplayNameEscaped() + ""); } std::vector ChatMessageHeader::GetChildContent() { @@ -1068,7 +1070,7 @@ Glib::ustring ChatMessageHeader::GetEscapedDisplayName(const UserData &user, con if (member.has_value() && !member->Nickname.empty()) return Glib::Markup::escape_text(member->Nickname); else - return Glib::Markup::escape_text(user.GetEscapedName()); + return Glib::Markup::escape_text(user.GetDisplayNameEscaped()); } bool ChatMessageHeader::on_author_button_press(GdkEventButton *ev) { diff --git a/src/components/chatwindow.cpp b/src/components/chatwindow.cpp index 1565d0c..aeed4ed 100644 --- a/src/components/chatwindow.cpp +++ b/src/components/chatwindow.cpp @@ -50,8 +50,8 @@ ChatWindow::ChatWindow() { m_input->signal_submit().connect(sigc::mem_fun(*this, &ChatWindow::OnInputSubmit)); m_input->signal_escape().connect([this]() { - if (m_is_replying) - StopReplying(); + if (m_is_replying) StopReplying(); + if (m_is_editing) StopEditing(); }); m_input->signal_key_press_event().connect(sigc::mem_fun(*this, &ChatWindow::OnKeyPressEvent), false); m_input->show(); @@ -132,8 +132,8 @@ void ChatWindow::SetActiveChannel(Snowflake id) { m_input->SetActiveChannel(id); m_input_indicator->SetActiveChannel(id); m_rate_limit_indicator->SetActiveChannel(id); - if (m_is_replying) - StopReplying(); + if (m_is_replying) StopReplying(); + if (m_is_editing) StopEditing(); #ifdef WITH_LIBHANDY m_tab_switcher->ReplaceActiveTab(id); @@ -274,15 +274,31 @@ bool ChatWindow::OnInputSubmit(ChatSubmitParams data) { data.ChannelID = m_active_channel; data.InReplyToID = m_replying_to; + data.EditingID = m_editing_id; if (m_active_channel.IsValid()) m_signal_action_chat_submit.emit(data); // m_replying_to is checked for invalid in the handler - if (m_is_replying) - StopReplying(); + + if (m_is_replying) StopReplying(); + if (m_is_editing) StopEditing(); return true; } +bool ChatWindow::ProcessKeyEvent(GdkEventKey *e) { + if (e->type != GDK_KEY_PRESS) return false; + if (e->keyval == GDK_KEY_Up && !(e->state & GDK_SHIFT_MASK) && m_input->IsEmpty()) { + const auto edit_id = m_chat->GetLastSentEditableMessage(); + if (edit_id.has_value()) { + StartEditing(*edit_id); + } + + return true; + } + + return false; +} + bool ChatWindow::OnKeyPressEvent(GdkEventKey *e) { if (m_completer.ProcessKeyPress(e)) return true; @@ -290,6 +306,9 @@ bool ChatWindow::OnKeyPressEvent(GdkEventKey *e) { if (m_input->ProcessKeyPress(e)) return true; + if (ProcessKeyEvent(e)) + return true; + return false; } @@ -300,10 +319,11 @@ void ChatWindow::StartReplying(Snowflake message_id) { m_replying_to = message_id; m_is_replying = true; m_input->StartReplying(); - if (author.has_value()) - m_input_indicator->SetCustomMarkup("Replying to " + author->GetEscapedBoldString()); - else + if (author.has_value()) { + m_input_indicator->SetCustomMarkup("Replying to " + author->GetUsernameEscapedBold()); + } else { m_input_indicator->SetCustomMarkup("Replying..."); + } } void ChatWindow::StopReplying() { @@ -313,6 +333,26 @@ void ChatWindow::StopReplying() { m_input_indicator->ClearCustom(); } +void ChatWindow::StartEditing(Snowflake message_id) { + const auto message = Abaddon::Get().GetDiscordClient().GetMessage(message_id); + if (!message.has_value()) { + spdlog::get("ui")->warn("ChatWindow::StartEditing message is nullopt"); + return; + } + m_is_editing = true; + m_editing_id = message_id; + m_input->StartEditing(*message); + m_input_indicator->SetCustomMarkup("Editing..."); +} + +void ChatWindow::StopEditing() { + m_is_editing = false; + m_editing_id = Snowflake::Invalid; + m_input->StopEditing(); + m_input->Clear(); + m_input_indicator->ClearCustom(); +} + void ChatWindow::OnScrollEdgeOvershot(Gtk::PositionType pos) { if (pos == Gtk::POS_TOP) m_signal_action_chat_load_history.emit(m_active_channel); diff --git a/src/components/chatwindow.hpp b/src/components/chatwindow.hpp index e1bb57a..b3c9d41 100644 --- a/src/components/chatwindow.hpp +++ b/src/components/chatwindow.hpp @@ -37,6 +37,9 @@ public: void SetTopic(const std::string &text); void AddAttachment(const Glib::RefPtr &file); + void StartEditing(Snowflake message_id); + void StopEditing(); + #ifdef WITH_LIBHANDY void OpenNewTab(Snowflake id); TabsState GetTabsState(); @@ -55,10 +58,14 @@ protected: void StartReplying(Snowflake message_id); void StopReplying(); + bool m_is_editing = false; + Snowflake m_editing_id; + Snowflake m_active_channel; bool OnInputSubmit(ChatSubmitParams data); + bool ProcessKeyEvent(GdkEventKey *e); bool OnKeyPressEvent(GdkEventKey *e); void OnScrollEdgeOvershot(Gtk::PositionType pos); diff --git a/src/components/completer.cpp b/src/components/completer.cpp index c6a5a08..f6f0906 100644 --- a/src/components/completer.cpp +++ b/src/components/completer.cpp @@ -117,12 +117,13 @@ void Completer::CompleteMentions(const Glib::ustring &term) { if (id == me) continue; const auto author = discord.GetUser(id); if (!author.has_value()) continue; + // todo improve the predicate here if (!StringContainsCaseless(author->Username, term)) continue; if (i++ > 15) break; auto entry = CreateEntry(author->GetMention()); - entry->SetText(author->Username + "#" + author->Discriminator); + entry->SetText(author->GetUsername()); if (channel_id.IsValid()) { const auto chan = discord.GetChannel(channel_id); diff --git a/src/components/friendslist.cpp b/src/components/friendslist.cpp index d8a566f..99fbafa 100644 --- a/src/components/friendslist.cpp +++ b/src/components/friendslist.cpp @@ -117,16 +117,16 @@ void FriendsList::OnActionRemove(Snowflake id) { Glib::ustring str; switch (*discord.GetRelationship(id)) { case RelationshipType::Blocked: - str = "Are you sure you want to unblock " + user->Username + "#" + user->Discriminator + "?"; + str = "Are you sure you want to unblock " + user->GetUsername() + "?"; break; case RelationshipType::Friend: - str = "Are you sure you want to remove " + user->Username + "#" + user->Discriminator + "?"; + str = "Are you sure you want to remove " + user->GetUsername() + "?"; break; case RelationshipType::PendingIncoming: - str = "Are you sure you want to ignore " + user->Username + "#" + user->Discriminator + "?"; + str = "Are you sure you want to ignore " + user->GetUsername() + "?"; break; case RelationshipType::PendingOutgoing: - str = "Are you sure you want to cancel your request to " + user->Username + "#" + user->Discriminator + "?"; + str = "Are you sure you want to cancel your request to " + user->GetUsername() + "?"; break; default: break; @@ -244,7 +244,7 @@ bool FriendsListAddComponent::OnKeyPress(GdkEventKey *e) { FriendsListFriendRow::FriendsListFriendRow(RelationshipType type, const UserData &data) : ID(data.ID) , Type(type) - , Name(data.Username + "#" + data.Discriminator) + , Name(data.GetUsername()) , Status(Abaddon::Get().GetDiscordClient().GetUserStatus(data.ID)) , m_accept("Accept") { auto *ev = Gtk::manage(new Gtk::EventBox); @@ -265,7 +265,7 @@ FriendsListFriendRow::FriendsListFriendRow(RelationshipType type, const UserData img->SetURL(data.GetAvatarURL("png", "32")); } - namelbl->set_markup(data.GetEscapedBoldName()); + namelbl->set_markup(data.GetDisplayNameEscapedBold()); UpdatePresenceLabel(); diff --git a/src/components/memberlist.cpp b/src/components/memberlist.cpp index 19b4fb8..975b527 100644 --- a/src/components/memberlist.cpp +++ b/src/components/memberlist.cpp @@ -37,9 +37,17 @@ MemberListUserRow::MemberListUserRow(const std::optional &guild, cons m_label->set_single_line_mode(true); m_label->set_ellipsize(Pango::ELLIPSIZE_END); - std::string display = data.Username; - if (Abaddon::Get().GetSettings().ShowMemberListDiscriminators) - display += "#" + data.Discriminator; + // todo remove after migration complete + std::string display; + if (data.IsPomelo()) { + display = data.GetDisplayName(guild.has_value() ? guild->ID : Snowflake::Invalid); + } else { + display = data.Username; + if (Abaddon::Get().GetSettings().ShowMemberListDiscriminators) { + display += "#" + data.Discriminator; + } + } + if (guild.has_value()) { if (const auto col_id = data.GetHoistedRole(guild->ID, true); col_id.IsValid()) { auto color = Abaddon::Get().GetDiscordClient().GetRole(col_id)->Color; diff --git a/src/constants.hpp b/src/constants.hpp index 256f85e..5ed123c 100644 --- a/src/constants.hpp +++ b/src/constants.hpp @@ -4,7 +4,7 @@ constexpr static uint64_t SnowflakeSplitDifference = 600; constexpr static int MaxMessagesForChatCull = 50; // this has to be 50 (for now) cuz that magic number is used in a couple other places and i dont feel like replacing them constexpr static int AttachmentItemSize = 120; -constexpr static int BaseAttachmentSizeLimit = 8 * 1024 * 1024; +constexpr static int BaseAttachmentSizeLimit = 25 * 1024 * 1024; constexpr static int NitroClassicAttachmentSizeLimit = 50 * 1024 * 1024; constexpr static int NitroAttachmentSizeLimit = 100 * 1024 * 1024; constexpr static int BoostLevel2AttachmentSizeLimit = 50 * 1024 * 1024; diff --git a/src/dialogs/editmessage.cpp b/src/dialogs/editmessage.cpp deleted file mode 100644 index b4308a0..0000000 --- a/src/dialogs/editmessage.cpp +++ /dev/null @@ -1,45 +0,0 @@ -#include "editmessage.hpp" - -EditMessageDialog::EditMessageDialog(Gtk::Window &parent) - : Gtk::Dialog("Edit Message", parent, true) - , m_layout(Gtk::ORIENTATION_VERTICAL) - , m_ok("OK") - , m_cancel("Cancel") - , m_bbox(Gtk::ORIENTATION_HORIZONTAL) { - set_default_size(300, 50); - get_style_context()->add_class("app-window"); - get_style_context()->add_class("app-popup"); - - m_ok.signal_clicked().connect([&]() { - m_content = m_text.get_buffer()->get_text(); - response(Gtk::RESPONSE_OK); - }); - - m_cancel.signal_clicked().connect([&]() { - response(Gtk::RESPONSE_CANCEL); - }); - - m_bbox.pack_start(m_ok, Gtk::PACK_SHRINK); - m_bbox.pack_start(m_cancel, Gtk::PACK_SHRINK); - m_bbox.set_layout(Gtk::BUTTONBOX_END); - - m_text.set_hexpand(true); - - m_scroll.set_hexpand(true); - m_scroll.set_vexpand(true); - m_scroll.add(m_text); - - m_layout.add(m_scroll); - m_layout.add(m_bbox); - get_content_area()->add(m_layout); - - show_all_children(); -} - -Glib::ustring EditMessageDialog::GetContent() { - return m_content; -} - -void EditMessageDialog::SetContent(const Glib::ustring &str) { - m_text.get_buffer()->set_text(str); -} diff --git a/src/dialogs/editmessage.hpp b/src/dialogs/editmessage.hpp deleted file mode 100644 index 38ebedb..0000000 --- a/src/dialogs/editmessage.hpp +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once -#include - -class EditMessageDialog : public Gtk::Dialog { -public: - EditMessageDialog(Gtk::Window &parent); - Glib::ustring GetContent(); - void SetContent(const Glib::ustring &str); - -protected: - Gtk::Box m_layout; - Gtk::Button m_ok; - Gtk::Button m_cancel; - Gtk::ButtonBox m_bbox; - Gtk::ScrolledWindow m_scroll; - Gtk::TextView m_text; - -private: - Glib::ustring m_content; -}; diff --git a/src/dialogs/friendpicker.cpp b/src/dialogs/friendpicker.cpp index c70a9e9..fe52b77 100644 --- a/src/dialogs/friendpicker.cpp +++ b/src/dialogs/friendpicker.cpp @@ -62,7 +62,7 @@ FriendPickerDialogItem::FriendPickerDialogItem(Snowflake user_id) , m_layout(Gtk::ORIENTATION_HORIZONTAL) { auto user = *Abaddon::Get().GetDiscordClient().GetUser(user_id); - m_name.set_markup(user.GetEscapedBoldString()); + m_name.set_markup(user.GetUsernameEscapedBold()); m_name.set_single_line_mode(true); m_avatar.property_pixbuf() = Abaddon::Get().GetImageManager().GetPlaceholder(32); diff --git a/src/discord/channel.cpp b/src/discord/channel.cpp index 4b1d909..a7102ad 100644 --- a/src/discord/channel.cpp +++ b/src/discord/channel.cpp @@ -147,13 +147,13 @@ std::string ChannelData::GetRecipientsDisplay() const { const auto recipients = GetDMRecipients(); if (Type == ChannelType::DM && !recipients.empty()) { - return recipients[0].Username; + return recipients[0].GetDisplayName(); } Glib::ustring r; for (size_t i = 0; i < recipients.size(); i++) { const auto &recipient = recipients[i]; - r += recipient.Username; + r += recipient.GetDisplayName(); if (i < recipients.size() - 1) { r += ", "; } diff --git a/src/discord/chatsubmitparams.hpp b/src/discord/chatsubmitparams.hpp index 6199634..e195189 100644 --- a/src/discord/chatsubmitparams.hpp +++ b/src/discord/chatsubmitparams.hpp @@ -17,8 +17,10 @@ struct ChatSubmitParams { std::string Filename; }; + bool Silent = false; Snowflake ChannelID; Snowflake InReplyToID; + Snowflake EditingID; Glib::ustring Message; std::vector Attachments; }; diff --git a/src/discord/discord.cpp b/src/discord/discord.cpp index 2a25a26..084e19f 100644 --- a/src/discord/discord.cpp +++ b/src/discord/discord.cpp @@ -461,8 +461,13 @@ void DiscordClient::SendChatMessageNoAttachments(const ChatSubmitParams ¶ms, CreateMessageObject obj; obj.Content = params.Message; obj.Nonce = nonce; - if (params.InReplyToID.IsValid()) + if (params.Silent) { + obj.Flags |= MessageFlags::SUPPRESS_NOTIFICATIONS; + } + + if (params.InReplyToID.IsValid()) { obj.MessageReference.emplace().MessageID = params.InReplyToID; + } m_http.MakePOST("/channels/" + std::to_string(params.ChannelID) + "/messages", nlohmann::json(obj).dump(), @@ -494,8 +499,13 @@ void DiscordClient::SendChatMessageAttachments(const ChatSubmitParams ¶ms, c CreateMessageObject obj; obj.Content = params.Message; obj.Nonce = nonce; - if (params.InReplyToID.IsValid()) + if (params.Silent) { + obj.Flags |= MessageFlags::SUPPRESS_NOTIFICATIONS; + } + + if (params.InReplyToID.IsValid()) { obj.MessageReference.emplace().MessageID = params.InReplyToID; + } auto req = m_http.CreateRequest(http::REQUEST_POST, "/channels/" + std::to_string(params.ChannelID) + "/messages"); m_progress_cb_timer.start(); @@ -545,10 +555,11 @@ void DiscordClient::SendChatMessageAttachments(const ChatSubmitParams ¶ms, c } void DiscordClient::SendChatMessage(const ChatSubmitParams ¶ms, const sigc::slot &callback) { - if (params.Attachments.empty()) + if (params.Attachments.empty()) { SendChatMessageNoAttachments(params, callback); - else + } else { SendChatMessageAttachments(params, callback); + } } void DiscordClient::DeleteMessage(Snowflake channel_id, Snowflake id) { @@ -1186,6 +1197,27 @@ void DiscordClient::AcceptVerificationGate(Snowflake guild_id, VerificationGateI }); } +void DiscordClient::RemoteAuthLogin(const std::string &ticket, const sigc::slot, DiscordError code)> &callback) { + http::request req(http::REQUEST_POST, "https://discord.com/api/v9/users/@me/remote-auth/login"); + req.set_header("Content-Type", "application/json"); + req.set_user_agent(Abaddon::Get().GetSettings().UserAgent); + req.set_body("{\"ticket\":\"" + ticket + "\"}"); + m_http.Execute(std::move(req), [this, callback](const http::response_type &r) { + if (CheckCode(r)) { + callback(nlohmann::json::parse(r.text).at("encrypted_token").get(), DiscordError::NONE); + } else { + try { + const auto j = nlohmann::json::parse(r.text); + if (j.contains("captcha_service")) { + callback(std::nullopt, DiscordError::CAPTCHA_REQUIRED); + return; + } + } catch (...) {} + callback(std::nullopt, GetCodeFromResponse(r)); + } + }); +} + #ifdef WITH_VOICE void DiscordClient::ConnectToVoice(Snowflake channel_id) { auto channel = GetChannel(channel_id); @@ -1306,6 +1338,17 @@ int DiscordClient::GetUnreadStateForChannel(Snowflake id) const noexcept { return iter->second; } +int DiscordClient::GetUnreadChannelsCountForCategory(Snowflake id) const noexcept { + int result = 0; + for (Snowflake channel_id : m_store.GetChannelIDsWithParentID(id)) { + if (IsChannelMuted(channel_id)) continue; + const auto iter = m_unread.find(channel_id); + if (iter == m_unread.end()) continue; + result += 1; + } + return result; +} + bool DiscordClient::GetUnreadStateForGuild(Snowflake id, int &total_mentions) const noexcept { total_mentions = 0; bool has_any_unread = false; @@ -1579,6 +1622,9 @@ void DiscordClient::HandleGatewayMessage(std::string str) { case GatewayEvent::VOICE_SERVER_UPDATE: { HandleGatewayVoiceServerUpdate(m); } break; + case GatewayEvent::CALL_CREATE: { + HandleGatewayCallCreate(m); + } break; #endif } } break; @@ -1801,9 +1847,12 @@ void DiscordClient::HandleGatewayPresenceUpdate(const GatewayMessage &msg) { void DiscordClient::HandleGatewayChannelDelete(const GatewayMessage &msg) { const auto id = msg.Data.at("id").get(); const auto channel = GetChannel(id); - auto it = m_guild_to_channels.find(*channel->GuildID); - if (it != m_guild_to_channels.end()) - it->second.erase(id); + if (channel.has_value() && channel->GuildID.has_value()) { + auto it = m_guild_to_channels.find(*channel->GuildID); + if (it != m_guild_to_channels.end()) { + it->second.erase(id); + } + } m_store.ClearChannel(id); m_signal_channel_delete.emit(id); m_signal_channel_accessibility_changed.emit(id, false); @@ -2253,8 +2302,39 @@ void DiscordClient::HandleGatewayGuildMembersChunk(const GatewayMessage &msg) { void DiscordClient::HandleGatewayVoiceStateUpdate(const GatewayMessage &msg) { spdlog::get("discord")->trace("VOICE_STATE_UPDATE"); - VoiceState data = msg.Data; + CheckVoiceState(msg.Data); +} +void DiscordClient::HandleGatewayVoiceServerUpdate(const GatewayMessage &msg) { + spdlog::get("discord")->trace("VOICE_SERVER_UPDATE"); + + VoiceServerUpdateData data = msg.Data; + spdlog::get("discord")->debug("Voice server endpoint: {}", data.Endpoint); + spdlog::get("discord")->debug("Voice token: {}", data.Token); + m_voice.SetEndpoint(data.Endpoint); + m_voice.SetToken(data.Token); + if (data.GuildID.has_value()) { + m_voice.SetServerID(*data.GuildID); + } else if (data.ChannelID.has_value()) { + m_voice.SetServerID(*data.ChannelID); + } else { + spdlog::get("discord")->error("No guild or channel ID in voice server?"); + } + m_voice.SetUserID(m_user_data.ID); + m_voice.Start(); +} + +void DiscordClient::HandleGatewayCallCreate(const GatewayMessage &msg) { + CallCreateData data = msg.Data; + + spdlog::get("discord")->debug("CALL_CREATE: {}", data.ChannelID); + + for (const auto &state : data.VoiceStates) { + CheckVoiceState(state); + } +} + +void DiscordClient::CheckVoiceState(const VoiceState &data) { if (data.UserID == m_user_data.ID) { spdlog::get("discord")->debug("Voice session ID: {}", data.SessionID); m_voice.SetSessionID(data.SessionID); @@ -2292,25 +2372,6 @@ void DiscordClient::HandleGatewayVoiceStateUpdate(const GatewayMessage &msg) { } } } - -void DiscordClient::HandleGatewayVoiceServerUpdate(const GatewayMessage &msg) { - spdlog::get("discord")->trace("VOICE_SERVER_UPDATE"); - - VoiceServerUpdateData data = msg.Data; - spdlog::get("discord")->debug("Voice server endpoint: {}", data.Endpoint); - spdlog::get("discord")->debug("Voice token: {}", data.Token); - m_voice.SetEndpoint(data.Endpoint); - m_voice.SetToken(data.Token); - if (data.GuildID.has_value()) { - m_voice.SetServerID(*data.GuildID); - } else if (data.ChannelID.has_value()) { - m_voice.SetServerID(*data.ChannelID); - } else { - spdlog::get("discord")->error("No guild or channel ID in voice server?"); - } - m_voice.SetUserID(m_user_data.ID); - m_voice.Start(); -} #endif void DiscordClient::HandleGatewayReadySupplemental(const GatewayMessage &msg) { @@ -2556,7 +2617,7 @@ void DiscordClient::HeartbeatThread() { void DiscordClient::SendIdentify() { IdentifyMessage msg; msg.Token = m_token; - msg.Capabilities = 509; // no idea what this is + msg.Capabilities = 4605; // bit 12 is necessary for CALL_CREATE... apparently? need to get this in sync with official client msg.Properties.OS = "Windows"; msg.Properties.Browser = "Chrome"; msg.Properties.Device = ""; @@ -2575,9 +2636,6 @@ void DiscordClient::SendIdentify() { msg.Presence.Since = 0; msg.Presence.IsAFK = false; msg.DoesSupportCompression = false; - msg.ClientState.HighestLastMessageID = "0"; - msg.ClientState.ReadStateVersion = 0; - msg.ClientState.UserGuildSettingsVersion = -1; SetSuperPropertiesFromIdentity(msg); const bool b = m_websocket.GetPrintMessages(); m_websocket.SetPrintMessages(false); @@ -2893,6 +2951,7 @@ void DiscordClient::LoadEventMap() { m_event_map["GUILD_MEMBERS_CHUNK"] = GatewayEvent::GUILD_MEMBERS_CHUNK; m_event_map["VOICE_STATE_UPDATE"] = GatewayEvent::VOICE_STATE_UPDATE; m_event_map["VOICE_SERVER_UPDATE"] = GatewayEvent::VOICE_SERVER_UPDATE; + m_event_map["CALL_CREATE"] = GatewayEvent::CALL_CREATE; } DiscordClient::type_signal_gateway_ready DiscordClient::signal_gateway_ready() { diff --git a/src/discord/discord.hpp b/src/discord/discord.hpp index 7f7518c..eba190a 100644 --- a/src/discord/discord.hpp +++ b/src/discord/discord.hpp @@ -184,6 +184,8 @@ public: void GetVerificationGateInfo(Snowflake guild_id, const sigc::slot)> &callback); void AcceptVerificationGate(Snowflake guild_id, VerificationGateInfoObject info, const sigc::slot &callback); + void RemoteAuthLogin(const std::string &ticket, const sigc::slot, DiscordError code)> &callback); + #ifdef WITH_VOICE void ConnectToVoice(Snowflake channel_id); void DisconnectFromVoice(); @@ -214,6 +216,7 @@ public: bool IsChannelMuted(Snowflake id) const noexcept; bool IsGuildMuted(Snowflake id) const noexcept; int GetUnreadStateForChannel(Snowflake id) const noexcept; + int GetUnreadChannelsCountForCategory(Snowflake id) const noexcept; bool GetUnreadStateForGuild(Snowflake id, int &total_mentions) const noexcept; int GetUnreadDMsCount() const; @@ -291,6 +294,9 @@ private: #ifdef WITH_VOICE void HandleGatewayVoiceStateUpdate(const GatewayMessage &msg); void HandleGatewayVoiceServerUpdate(const GatewayMessage &msg); + void HandleGatewayCallCreate(const GatewayMessage &msg); + + void CheckVoiceState(const VoiceState &data); #endif void HeartbeatThread(); diff --git a/src/discord/errors.hpp b/src/discord/errors.hpp index 4579563..8ead4f2 100644 --- a/src/discord/errors.hpp +++ b/src/discord/errors.hpp @@ -11,6 +11,7 @@ enum class DiscordError { RELATIONSHIP_ALREADY_FRIENDS = 80007, NONE = -1, + CAPTCHA_REQUIRED = -2, }; constexpr const char *GetDiscordErrorDisplayString(DiscordError error) { diff --git a/src/discord/message.cpp b/src/discord/message.cpp index f5d8ad8..9adc7e6 100644 --- a/src/discord/message.cpp +++ b/src/discord/message.cpp @@ -265,6 +265,10 @@ bool Message::IsEdited() const { return m_edited; } +bool Message::IsEditable() const noexcept { + return (Abaddon::Get().GetDiscordClient().GetUserData().ID == Author.ID) && !IsDeleted() && !IsPending && (Type == MessageType::DEFAULT || Type == MessageType::INLINE_REPLY); +} + bool Message::DoesMentionEveryoneOrUser(Snowflake id) const noexcept { if (DoesMentionEveryone) return true; return std::any_of(Mentions.begin(), Mentions.end(), [id](const UserData &user) { diff --git a/src/discord/message.hpp b/src/discord/message.hpp index df2cb38..fb11604 100644 --- a/src/discord/message.hpp +++ b/src/discord/message.hpp @@ -8,6 +8,7 @@ #include "emoji.hpp" #include "member.hpp" #include "interactions.hpp" +#include "misc/bitwise.hpp" enum class MessageType { DEFAULT = 0, // yep @@ -35,14 +36,23 @@ enum class MessageType { enum class MessageFlags { NONE = 0, - CROSSPOSTED = 1 << 0, // this message has been published to subscribed channels (via Channel Following) - IS_CROSSPOST = 1 << 1, // this message originated from a message in another channel (via Channel Following) - SUPPRESS_EMBEDS = 1 << 2, // do not include any embeds when serializing this message - SOURCE_MESSAGE_DELETE = 1 << 3, // the source message for this crosspost has been deleted (via Channel Following) - URGENT = 1 << 4, // this message came from the urgent message system - HAS_THREAD = 1 << 5, // this message has an associated thread, with the same id as the message - EPHEMERAL = 1 << 6, // this message is only visible to the user who invoked the Interaction - LOADING = 1 << 7, // this message is an Interaction Response and the bot is "thinking" + CROSSPOSTED = 1 << 0, // this message has been published to subscribed channels (via Channel Following) + IS_CROSSPOST = 1 << 1, // this message originated from a message in another channel (via Channel Following) + SUPPRESS_EMBEDS = 1 << 2, // do not include any embeds when serializing this message + SOURCE_MESSAGE_DELETE = 1 << 3, // the source message for this crosspost has been deleted (via Channel Following) + URGENT = 1 << 4, // this message came from the urgent message system + HAS_THREAD = 1 << 5, // this message has an associated thread, with the same id as the message + EPHEMERAL = 1 << 6, // this message is only visible to the user who invoked the Interaction + LOADING = 1 << 7, // this message is an Interaction Response and the bot is "thinking" + FAILED_TO_MENTION_SOME_ROLES_IN_THREAD = 1 << 8, // this message failed to mention some roles and add their members to the thread + SHOULD_SHOW_LINK_NOT_DISCORD_WARNING = 1 << 10, // + SUPPRESS_NOTIFICATIONS = 1 << 12, // this message will not trigger push and desktop notifications + IS_VOICE_MESSAGE = 1 << 13, // this message is a voice message +}; + +template<> +struct Bitwise { + static const bool enable = true; }; struct EmbedFooterData { @@ -209,11 +219,13 @@ struct Message { void SetDeleted(); void SetEdited(); - [[nodiscard]] bool IsDeleted() const; - [[nodiscard]] bool IsEdited() const; + bool IsDeleted() const; + bool IsEdited() const; - [[nodiscard]] bool DoesMentionEveryoneOrUser(Snowflake id) const noexcept; - [[nodiscard]] bool DoesMention(Snowflake id) const noexcept; + bool IsEditable() const noexcept; + + bool DoesMentionEveryoneOrUser(Snowflake id) const noexcept; + bool DoesMention(Snowflake id) const noexcept; private: bool m_deleted = false; diff --git a/src/discord/objects.cpp b/src/discord/objects.cpp index 86264cc..cb0a685 100644 --- a/src/discord/objects.cpp +++ b/src/discord/objects.cpp @@ -251,7 +251,7 @@ void from_json(const nlohmann::json &j, SupplementalMergedPresencesData &m) { void from_json(const nlohmann::json &j, SupplementalGuildEntry &m) { JS_D("id", m.ID); - JS_D("voice_states", m.VoiceStates); + JS_ON("voice_states", m.VoiceStates); } void from_json(const nlohmann::json &j, ReadySupplementalData &m) { @@ -308,6 +308,7 @@ void to_json(nlohmann::json &j, const HeartbeatMessage &m) { void to_json(nlohmann::json &j, const CreateMessageObject &m) { j["content"] = m.Content; + j["flags"] = m.Flags; JS_IF("message_reference", m.MessageReference); JS_IF("nonce", m.Nonce); } @@ -463,6 +464,7 @@ void from_json(const nlohmann::json &j, UserProfileData &m) { JS_D("mutual_guilds", m.MutualGuilds); JS_ON("premium_guild_since", m.PremiumGuildSince); JS_ON("premium_since", m.PremiumSince); + JS_ON("legacy_username", m.LegacyUsername); JS_D("user", m.User); } @@ -686,6 +688,11 @@ void from_json(const nlohmann::json &j, VoiceServerUpdateData &m) { JS_ON("guild_id", m.GuildID); JS_ON("channel_id", m.ChannelID); } + +void from_json(const nlohmann::json &j, CallCreateData &m) { + JS_D("channel_id", m.ChannelID); + JS_ON("voice_states", m.VoiceStates); +} #endif void from_json(const nlohmann::json &j, VoiceState &m) { diff --git a/src/discord/objects.hpp b/src/discord/objects.hpp index 3ad4037..305ac65 100644 --- a/src/discord/objects.hpp +++ b/src/discord/objects.hpp @@ -102,6 +102,7 @@ enum class GatewayEvent : int { GUILD_MEMBERS_CHUNK, VOICE_STATE_UPDATE, VOICE_SERVER_UPDATE, + CALL_CREATE, }; enum class GatewayCloseCode : uint16_t { @@ -427,6 +428,7 @@ struct HeartbeatMessage : GatewayMessage { struct CreateMessageObject { std::string Content; + MessageFlags Flags = MessageFlags::NONE; std::optional MessageReference; std::optional Nonce; @@ -617,6 +619,7 @@ struct UserProfileData { std::vector MutualGuilds; std::optional PremiumGuildSince; // null std::optional PremiumSince; // null + std::optional LegacyUsername; // null UserData User; friend void from_json(const nlohmann::json &j, UserProfileData &m); @@ -886,6 +889,23 @@ struct GuildMembersChunkData { friend void from_json(const nlohmann::json &j, GuildMembersChunkData &m); }; +struct VoiceState { + std::optional ChannelID; + bool IsDeafened; + bool IsMuted; + std::optional GuildID; + std::optional Member; + bool IsSelfDeafened; + bool IsSelfMuted; + bool IsSelfVideo; + bool IsSelfStream = false; + std::string SessionID; + bool IsSuppressed; + Snowflake UserID; + + friend void from_json(const nlohmann::json &j, VoiceState &m); +}; + #ifdef WITH_VOICE struct VoiceStateUpdateMessage { std::optional GuildID; @@ -906,21 +926,15 @@ struct VoiceServerUpdateData { friend void from_json(const nlohmann::json &j, VoiceServerUpdateData &m); }; -#endif -struct VoiceState { - std::optional ChannelID; - bool IsDeafened; - bool IsMuted; - std::optional GuildID; - std::optional Member; - bool IsSelfDeafened; - bool IsSelfMuted; - bool IsSelfVideo; - bool IsSelfStream = false; - std::string SessionID; - bool IsSuppressed; - Snowflake UserID; +struct CallCreateData { + Snowflake ChannelID; + std::vector VoiceStates; + // Snowflake MessageID; + // std::string Region; + // std::vector Ringing; + // std::vector EmbeddedActivities; - friend void from_json(const nlohmann::json &j, VoiceState &m); + friend void from_json(const nlohmann::json &j, CallCreateData &m); }; +#endif diff --git a/src/discord/snowflake.cpp b/src/discord/snowflake.cpp index 15dacae..680d4da 100644 --- a/src/discord/snowflake.cpp +++ b/src/discord/snowflake.cpp @@ -60,6 +60,10 @@ Glib::ustring Snowflake::GetLocalTimestamp() const { return tmp.data(); } +uint64_t Snowflake::GetUnixMilliseconds() const noexcept { + return (m_num >> 22) + DiscordEpochSeconds * 1000; +} + void from_json(const nlohmann::json &j, Snowflake &s) { if (j.is_string()) { std::string tmp; diff --git a/src/discord/snowflake.hpp b/src/discord/snowflake.hpp index 2ced46b..68cb5ea 100644 --- a/src/discord/snowflake.hpp +++ b/src/discord/snowflake.hpp @@ -16,6 +16,7 @@ struct Snowflake { [[nodiscard]] bool IsValid() const; [[nodiscard]] Glib::ustring GetLocalTimestamp() const; + [[nodiscard]] uint64_t GetUnixMilliseconds() const noexcept; bool operator==(const Snowflake &s) const noexcept { return m_num == s.m_num; diff --git a/src/discord/store.cpp b/src/discord/store.cpp index 41b2069..7ee4d87 100644 --- a/src/discord/store.cpp +++ b/src/discord/store.cpp @@ -438,6 +438,7 @@ void Store::SetUser(Snowflake id, const UserData &user) { s->Bind(7, user.IsMFAEnabled); s->Bind(8, user.PremiumType); s->Bind(9, user.PublicFlags); + s->Bind(10, user.GlobalName); if (!s->Insert()) fprintf(stderr, "user insert failed for %" PRIu64 ": %s\n", static_cast(id), m_db.ErrStr()); @@ -558,8 +559,9 @@ std::vector Store::GetMessagesBefore(Snowflake channel_id, Snowflake me for (auto &msg : msgs) { if (msg.MessageReference.has_value() && msg.MessageReference->MessageID.has_value()) { auto ref = GetMessage(*msg.MessageReference->MessageID); - if (ref.has_value()) + if (ref.has_value()) { msg.ReferencedMessage = std::make_shared(std::move(*ref)); + } } } @@ -1109,6 +1111,7 @@ std::optional Store::GetUser(Snowflake id) const { s->Get(6, r.IsMFAEnabled); s->Get(7, r.PremiumType); s->Get(8, r.PublicFlags); + s->Get(9, r.GlobalName); s->Reset(); @@ -1233,7 +1236,8 @@ bool Store::CreateTables() { system BOOL, mfa BOOL, premium INTEGER, - pubflags INTEGER + pubflags INTEGER, + global_name TEXT ) )"; @@ -1797,7 +1801,7 @@ bool Store::CreateStatements() { m_stmt_set_user = std::make_unique(m_db, R"( REPLACE INTO users VALUES ( - ?, ?, ?, ?, ?, ?, ?, ?, ? + ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) )"); if (!m_stmt_set_user->OK()) { @@ -2057,26 +2061,22 @@ bool Store::CreateStatements() { message_interactions.name, message_interactions.type, message_interactions.user_id, - attachments.id, - attachments.filename, - attachments.size, - attachments.url, - attachments.proxy, - attachments.height, - attachments.width, - message_references.message + message_references.message, + message_references.channel, + message_references.guild, + COUNT(attachments.id) FROM messages LEFT OUTER JOIN message_interactions ON messages.id = message_interactions.message_id - LEFT OUTER JOIN - attachments - ON messages.id = attachments.message LEFT OUTER JOIN message_references ON messages.id = message_references.id + LEFT OUTER JOIN + attachments + ON messages.id = attachments.message WHERE channel_id = ? AND pending = 0 AND messages.id < ? ORDER BY id DESC LIMIT ? - ) ORDER BY id ASC + ) WHERE id IS NOT NULL ORDER BY id ASC )"); if (!m_stmt_get_messages_before->OK()) { fprintf(stderr, "failed to prepare get messages before statement: %s\n", m_db.ErrStr()); @@ -2084,32 +2084,28 @@ bool Store::CreateStatements() { } m_stmt_get_pins = std::make_unique(m_db, R"( - SELECT messages.*, - message_interactions.interaction_id, - message_interactions.name, - message_interactions.type, - message_interactions.user_id, - attachments.id, - attachments.filename, - attachments.size, - attachments.url, - attachments.proxy, - attachments.height, - attachments.width, - message_references.message, - message_references.channel, - message_references.guild - FROM messages - LEFT OUTER JOIN - message_interactions - ON messages.id = message_interactions.message_id - LEFT OUTER JOIN - attachments - ON messages.id = attachments.message - LEFT OUTER JOIN - message_references - ON messages.id = message_references.id - WHERE channel_id = ? AND pinned = 1 ORDER BY id ASC + SELECT * FROM ( + SELECT messages.*, + message_interactions.interaction_id, + message_interactions.name, + message_interactions.type, + message_interactions.user_id, + message_references.message, + message_references.channel, + message_references.guild, + COUNT(attachments.id) + FROM messages + LEFT OUTER JOIN + message_interactions + ON messages.id = message_interactions.message_id + LEFT OUTER JOIN + message_references + ON messages.id = message_references.id + LEFT OUTER JOIN + attachments + ON messages.id = attachments.message + WHERE channel_id = ? AND pinned = 1 ORDER BY id ASC + ) WHERE id IS NOT NULL )"); if (!m_stmt_get_pins->OK()) { fprintf(stderr, "failed to prepare get pins statement: %s\n", m_db.ErrStr()); diff --git a/src/discord/user.cpp b/src/discord/user.cpp index 2ee7361..45d70e8 100644 --- a/src/discord/user.cpp +++ b/src/discord/user.cpp @@ -1,5 +1,9 @@ #include "user.hpp" +bool UserData::IsPomelo() const noexcept { + return Discriminator.size() == 1 && Discriminator[0] == '0'; +} + bool UserData::IsABot() const noexcept { return IsBot.has_value() && *IsBot; } @@ -18,49 +22,54 @@ bool UserData::HasAnimatedAvatar() const noexcept { bool UserData::HasAnimatedAvatar(Snowflake guild_id) const { const auto member = Abaddon::Get().GetDiscordClient().GetMember(ID, guild_id); - if (member.has_value() && member->Avatar.has_value() && member->Avatar.value()[0] == 'a' && member->Avatar.value()[1] == '_') + if (member.has_value() && member->Avatar.has_value() && member->Avatar.value()[0] == 'a' && member->Avatar.value()[1] == '_') { return true; - else if (member.has_value() && !member->Avatar.has_value()) + } else if (member.has_value() && !member->Avatar.has_value()) { return HasAnimatedAvatar(); + } return false; } bool UserData::HasAnimatedAvatar(const std::optional &guild_id) const { - if (guild_id.has_value()) + if (guild_id.has_value()) { return HasAnimatedAvatar(*guild_id); - else - return HasAnimatedAvatar(); + } + + return HasAnimatedAvatar(); } std::string UserData::GetAvatarURL(Snowflake guild_id, const std::string &ext, std::string size) const { const auto member = Abaddon::Get().GetDiscordClient().GetMember(ID, guild_id); if (member.has_value() && member->Avatar.has_value()) { - if (ext == "gif" && !(member->Avatar.value()[0] == 'a' && member->Avatar.value()[1] == '_')) + if (ext == "gif" && !(member->Avatar.value()[0] == 'a' && member->Avatar.value()[1] == '_')) { return GetAvatarURL(ext, size); + } return "https://cdn.discordapp.com/guilds/" + std::to_string(guild_id) + "/users/" + std::to_string(ID) + "/avatars/" + *member->Avatar + "." + ext + "?" + "size=" + size; - } else { - return GetAvatarURL(ext, size); } + return GetAvatarURL(ext, size); } std::string UserData::GetAvatarURL(const std::optional &guild_id, const std::string &ext, std::string size) const { - if (guild_id.has_value()) + if (guild_id.has_value()) { return GetAvatarURL(*guild_id, ext, size); - else - return GetAvatarURL(ext, size); + } + return GetAvatarURL(ext, size); } std::string UserData::GetAvatarURL(const std::string &ext, std::string size) const { - if (HasAvatar()) + if (HasAvatar()) { return "https://cdn.discordapp.com/avatars/" + std::to_string(ID) + "/" + Avatar + "." + ext + "?size=" + size; - else - return GetDefaultAvatarURL(); + } + return GetDefaultAvatarURL(); } std::string UserData::GetDefaultAvatarURL() const { + if (IsPomelo()) { + return "https://cdn.discordapp.com/embed/avatars/" + std::to_string((static_cast(ID) >> 22) % 6) + ".png"; + } return "https://cdn.discordapp.com/embed/avatars/" + std::to_string(std::stoul(Discriminator) % 5) + ".png"; // size isn't respected by the cdn } @@ -72,18 +81,75 @@ std::string UserData::GetMention() const { return "<@" + std::to_string(ID) + ">"; } -std::string UserData::GetEscapedName() const { - return Glib::Markup::escape_text(Username); +std::string UserData::GetDisplayName() const { + if (IsPomelo() && GlobalName.has_value()) { + return *GlobalName; + } + + return Username; } -std::string UserData::GetEscapedBoldName() const { - return "" + Glib::Markup::escape_text(Username) + ""; +std::string UserData::GetDisplayName(Snowflake guild_id) const { + const auto member = Abaddon::Get().GetDiscordClient().GetMember(ID, guild_id); + if (member.has_value() && !member->Nickname.empty()) { + return member->Nickname; + } + return GetDisplayName(); } -std::string UserData::GetEscapedString() const { +std::string UserData::GetDisplayName(const std::optional &guild_id) const { + if (guild_id.has_value()) { + return GetDisplayName(*guild_id); + } + return GetDisplayName(); +} + +std::string UserData::GetDisplayNameEscaped() const { + return Glib::Markup::escape_text(GetDisplayName()); +} + +std::string UserData::GetDisplayNameEscaped(Snowflake guild_id) const { + return Glib::Markup::escape_text(GetDisplayName(guild_id)); +} + +std::string UserData::GetDisplayNameEscapedBold() const { + return "" + Glib::Markup::escape_text(GetDisplayName()) + ""; +} + +std::string UserData::GetDisplayNameEscapedBold(Snowflake guild_id) const { + return "" + Glib::Markup::escape_text(GetDisplayName(guild_id)) + ""; +} + +std::string UserData::GetUsername() const { + if (IsPomelo()) { + return Username; + } + + return Username + "#" + Discriminator; +} + +std::string UserData::GetUsernameEscaped() const { + if (IsPomelo()) { + return Glib::Markup::escape_text(Username); + } + return Glib::Markup::escape_text(Username) + "#" + Discriminator; } +std::string UserData::GetUsernameEscapedBold() const { + if (IsPomelo()) { + return "" + Glib::Markup::escape_text(Username) + ""; + } + return "" + Glib::Markup::escape_text(Username) + "#" + Discriminator; +} + +std::string UserData::GetUsernameEscapedBoldAt() const { + if (IsPomelo()) { + return "@" + Glib::Markup::escape_text(Username) + ""; + } + return "@" + Glib::Markup::escape_text(Username) + "#" + Discriminator; +} + void from_json(const nlohmann::json &j, UserData &m) { JS_D("id", m.ID); JS_D("username", m.Username); @@ -104,6 +170,7 @@ void from_json(const nlohmann::json &j, UserData &m) { JS_ON("phone", m.Phone); JS_ON("bio", m.Bio); JS_ON("banner", m.BannerHash); + JS_ON("global_name", m.GlobalName); } void to_json(nlohmann::json &j, const UserData &m) { @@ -127,6 +194,7 @@ void to_json(nlohmann::json &j, const UserData &m) { JS_IF("mobile", m.IsMobile); JS_IF("nsfw_allowed", m.IsNSFWAllowed); JS_IF("phone", m.Phone); + JS_IF("global_name", m.GlobalName); } void UserData::update_from_json(const nlohmann::json &j) { @@ -146,6 +214,7 @@ void UserData::update_from_json(const nlohmann::json &j) { JS_RD("mobile", IsMobile); JS_RD("nsfw_allowed", IsNSFWAllowed); JS_RD("phone", Phone); + JS_RD("global_name", GlobalName); } const char *UserData::GetFlagName(uint64_t flag) { diff --git a/src/discord/user.hpp b/src/discord/user.hpp index 1b9d517..8b2a2c4 100644 --- a/src/discord/user.hpp +++ b/src/discord/user.hpp @@ -25,6 +25,11 @@ struct UserData { VerifiedBot = 1 << 16, EarlyVerifiedBotDeveloper = 1 << 17, CertifiedModerator = 1 << 18, + BotHTTPInteractions = 1 << 19, + Spammer = 1 << 20, + DisablePremium = 1 << 21, + ActiveDeveloper = 1 << 22, + Quarantined = 1ULL << 44, MaxFlag_PlusOne, MaxFlag = MaxFlag_PlusOne - 1, @@ -37,6 +42,7 @@ struct UserData { std::string Username; std::string Discriminator; std::string Avatar; // null + std::optional GlobalName; std::optional IsBot; std::optional IsSystem; std::optional IsMFAEnabled; @@ -60,6 +66,7 @@ struct UserData { friend void to_json(nlohmann::json &j, const UserData &m); void update_from_json(const nlohmann::json &j); + [[nodiscard]] bool IsPomelo() const noexcept; [[nodiscard]] bool IsABot() const noexcept; [[nodiscard]] bool IsDeleted() const; [[nodiscard]] bool HasAvatar() const; @@ -72,14 +79,15 @@ struct UserData { [[nodiscard]] std::string GetDefaultAvatarURL() const; [[nodiscard]] Snowflake GetHoistedRole(Snowflake guild_id, bool with_color = false) const; [[nodiscard]] std::string GetMention() const; - [[nodiscard]] std::string GetEscapedName() const; - [[nodiscard]] std::string GetEscapedBoldName() const; - [[nodiscard]] std::string GetEscapedString() const; - template - [[nodiscard]] inline std::string GetEscapedBoldString() const { - if constexpr (with_at) - return "@" + Glib::Markup::escape_text(Username) + "#" + Discriminator; - else - return "" + Glib::Markup::escape_text(Username) + "#" + Discriminator; - } + [[nodiscard]] std::string GetDisplayName() const; + [[nodiscard]] std::string GetDisplayName(Snowflake guild_id) const; + [[nodiscard]] std::string GetDisplayName(const std::optional &guild_id) const; + [[nodiscard]] std::string GetDisplayNameEscaped() const; + [[nodiscard]] std::string GetDisplayNameEscaped(Snowflake guild_id) const; + [[nodiscard]] std::string GetDisplayNameEscapedBold() const; + [[nodiscard]] std::string GetDisplayNameEscapedBold(Snowflake guild_id) const; + [[nodiscard]] std::string GetUsername() const; + [[nodiscard]] std::string GetUsernameEscaped() const; + [[nodiscard]] std::string GetUsernameEscapedBold() const; + [[nodiscard]] std::string GetUsernameEscapedBoldAt() const; }; diff --git a/src/discord/voiceclient.cpp b/src/discord/voiceclient.cpp index 524d930..e9814b6 100644 --- a/src/discord/voiceclient.cpp +++ b/src/discord/voiceclient.cpp @@ -112,8 +112,8 @@ void UDPSocket::ReadThread() { sockaddr_in from; socklen_t addrlen = sizeof(from); - tv.tv_sec = 0; - tv.tv_usec = 1000000; + tv.tv_sec = 1; + tv.tv_usec = 0; fd_set read_fds; FD_ZERO(&read_fds); diff --git a/src/discord/websocket.cpp b/src/discord/websocket.cpp index f886e69..cdc4db1 100644 --- a/src/discord/websocket.cpp +++ b/src/discord/websocket.cpp @@ -26,7 +26,7 @@ void Websocket::StartConnection(const std::string &url) { m_websocket->disableAutomaticReconnection(); m_websocket->setUrl(url); m_websocket->setOnMessageCallback([this](auto &&msg) { OnMessage(std::forward(msg)); }); - m_websocket->setExtraHeaders(ix::WebSocketHttpHeaders { { "User-Agent", m_agent } }); // idk if this actually works + m_websocket->setExtraHeaders(ix::WebSocketHttpHeaders { { "User-Agent", m_agent }, { "Origin", "https://discord.com" } }); // idk if this actually works m_websocket->start(); } @@ -81,6 +81,9 @@ void Websocket::OnMessage(const ix::WebSocketMessagePtr &msg) { case ix::WebSocketMessageType::Message: { m_signal_message.emit(msg->str); } break; + case ix::WebSocketMessageType::Error: { + m_log->error("Websocket error: Status: {} Reason: {}", msg->errorInfo.http_status, msg->errorInfo.reason); + } break; default: break; } diff --git a/src/misc/chatutil.cpp b/src/misc/chatutil.cpp index a87df00..e61d32b 100644 --- a/src/misc/chatutil.cpp +++ b/src/misc/chatutil.cpp @@ -74,17 +74,17 @@ void HandleUserMentions(const Glib::RefPtr &buf, Snowflake chan if (channel->Type == ChannelType::DM || channel->Type == ChannelType::GROUP_DM || !channel->GuildID.has_value() || plain) { if (plain) { - replacement = "@" + user->Username + "#" + user->Discriminator; + replacement = "@" + user->GetUsername(); } else { - replacement = user->GetEscapedBoldString(); + replacement = user->GetUsernameEscapedBoldAt(); } } else { const auto role_id = user->GetHoistedRole(*channel->GuildID, true); const auto role = discord.GetRole(role_id); if (!role.has_value()) - replacement = user->GetEscapedBoldString(); + replacement = user->GetUsernameEscapedBoldAt(); else - replacement = "Color) + "\">" + user->GetEscapedBoldString() + ""; + replacement = "Color) + "\">" + user->GetUsernameEscapedBoldAt() + ""; } // regex returns byte positions and theres no straightforward way in the c++ bindings to deal with that :( diff --git a/src/notifications/notifications.cpp b/src/notifications/notifications.cpp index 3c88c44..e95c1c0 100644 --- a/src/notifications/notifications.cpp +++ b/src/notifications/notifications.cpp @@ -88,6 +88,8 @@ bool CheckGuildMessage(const Message &message) { void Notifications::CheckMessage(const Message &message) { if (!Abaddon::Get().GetSettings().NotificationsEnabled) return; + // ignore if silent message + if (message.Flags.has_value() && ((*message.Flags & MessageFlags::SUPPRESS_NOTIFICATIONS) == MessageFlags::SUPPRESS_NOTIFICATIONS)) return; // ignore if our status is do not disturb if (IsDND()) return; auto &discord = Abaddon::Get().GetDiscordClient(); diff --git a/src/remoteauth/remoteauthclient.cpp b/src/remoteauth/remoteauthclient.cpp new file mode 100644 index 0000000..7653b78 --- /dev/null +++ b/src/remoteauth/remoteauthclient.cpp @@ -0,0 +1,351 @@ +#ifdef WITH_QRLOGIN + +// clang-format off + +#include "remoteauthclient.hpp" +#include "http.hpp" +#include +#include + +// clang-format on + +RemoteAuthClient::RemoteAuthClient() + : m_ws("remote-auth-ws") + , m_log(spdlog::get("remote-auth")) { + m_ws.signal_open().connect(sigc::mem_fun(*this, &RemoteAuthClient::OnWebsocketOpen)); + m_ws.signal_close().connect(sigc::mem_fun(*this, &RemoteAuthClient::OnWebsocketClose)); + m_ws.signal_message().connect(sigc::mem_fun(*this, &RemoteAuthClient::OnWebsocketMessage)); + + m_dispatcher.connect(sigc::mem_fun(*this, &RemoteAuthClient::OnDispatch)); +} + +RemoteAuthClient::~RemoteAuthClient() { + Stop(); +} + +void RemoteAuthClient::Start() { + if (IsConnected()) { + Stop(); + } + + m_connected = true; + m_heartbeat_waiter.revive(); + m_ws.StartConnection("wss://remote-auth-gateway.discord.gg/?v=2"); +} + +void RemoteAuthClient::Stop() { + if (!IsConnected()) { + m_log->warn("Requested stop while not connected"); + return; + } + + m_connected = false; + if (m_timeout_conn) m_timeout_conn.disconnect(); + m_ws.Stop(1000); + m_heartbeat_waiter.kill(); + if (m_heartbeat_thread.joinable()) m_heartbeat_thread.join(); +} + +bool RemoteAuthClient::IsConnected() const noexcept { + return m_connected; +} + +void RemoteAuthClient::OnGatewayMessage(const std::string &str) { + m_log->trace(str); + auto j = nlohmann::json::parse(str); + const auto opcode = j.at("op").get(); + if (opcode == "hello") { + HandleGatewayHello(j); + } else if (opcode == "nonce_proof") { + HandleGatewayNonceProof(j); + } else if (opcode == "pending_remote_init") { + HandleGatewayPendingRemoteInit(j); + } else if (opcode == "pending_ticket") { + HandleGatewayPendingTicket(j); + } else if (opcode == "pending_login") { + HandleGatewayPendingLogin(j); + } else if (opcode == "cancel") { + HandleGatewayCancel(j); + } +} + +void RemoteAuthClient::HandleGatewayHello(const nlohmann::json &j) { + const auto timeout_ms = j.at("timeout_ms").get(); + const auto heartbeat_interval = j.at("heartbeat_interval").get(); + m_log->debug("Timeout: {}, Heartbeat: {}", timeout_ms, heartbeat_interval); + + m_heartbeat_msec = heartbeat_interval; + m_heartbeat_thread = std::thread(&RemoteAuthClient::HeartbeatThread, this); + + m_timeout_conn = Glib::signal_timeout().connect(sigc::mem_fun(*this, &RemoteAuthClient::OnTimeout), timeout_ms); + + Init(); + + m_signal_hello.emit(); +} + +void RemoteAuthClient::HandleGatewayNonceProof(const nlohmann::json &j) { + m_log->debug("Received encrypted nonce"); + + const auto encrypted_nonce = Glib::Base64::decode(j.at("encrypted_nonce").get()); + const auto proof = Decrypt(reinterpret_cast(encrypted_nonce.data()), encrypted_nonce.size()); + auto proof_encoded = Glib::Base64::encode(std::string(proof.begin(), proof.end())); + + std::replace(proof_encoded.begin(), proof_encoded.end(), '/', '_'); + std::replace(proof_encoded.begin(), proof_encoded.end(), '+', '-'); + proof_encoded.erase(std::remove(proof_encoded.begin(), proof_encoded.end(), '='), proof_encoded.end()); + + nlohmann::json reply; + reply["op"] = "nonce_proof"; + reply["nonce"] = proof_encoded; + m_ws.Send(reply); +} + +void RemoteAuthClient::HandleGatewayPendingRemoteInit(const nlohmann::json &j) { + m_log->debug("Received fingerprint"); + + m_signal_fingerprint.emit(j.at("fingerprint").get()); +} + +void RemoteAuthClient::HandleGatewayPendingTicket(const nlohmann::json &j) { + const auto encrypted_payload = Glib::Base64::decode(j.at("encrypted_user_payload").get()); + const auto payload = Decrypt(reinterpret_cast(encrypted_payload.data()), encrypted_payload.size()); + + m_log->trace("User payload: {}", std::string(payload.begin(), payload.end())); + + const std::vector user_info = Glib::Regex::split_simple(":", std::string(payload.begin(), payload.end())); + Snowflake user_id; + std::string discriminator; + std::string avatar_hash; + std::string username; + if (user_info.size() >= 4) { + user_id = Snowflake(user_info[0]); + discriminator = user_info[1]; + avatar_hash = user_info[2]; + username = user_info[3]; + } + + m_signal_pending_ticket.emit(user_id, discriminator, avatar_hash, username); +} + +void RemoteAuthClient::HandleGatewayPendingLogin(const nlohmann::json &j) { + Abaddon::Get().GetDiscordClient().RemoteAuthLogin(j.at("ticket").get(), sigc::mem_fun(*this, &RemoteAuthClient::OnRemoteAuthLoginResponse)); + m_signal_pending_login.emit(); +} + +void RemoteAuthClient::HandleGatewayCancel(const nlohmann::json &j) { + Stop(); + Start(); +} + +void RemoteAuthClient::OnRemoteAuthLoginResponse(const std::optional &encrypted_token, DiscordError err) { + if (!encrypted_token.has_value()) { + m_log->error("Remote auth login failed: {}", static_cast(err)); + if (err == DiscordError::CAPTCHA_REQUIRED) { + m_signal_error.emit("Discord is requiring a captcha. You must use a web browser to log in."); + } else { + m_signal_error.emit("An error occurred. Try again."); + } + return; + } + + const auto encrypted = Glib::Base64::decode(*encrypted_token); + const auto token = Decrypt(reinterpret_cast(encrypted.data()), encrypted.size()); + m_signal_token.emit(std::string(token.begin(), token.end())); +} + +void RemoteAuthClient::Init() { + GenerateKey(); + const auto key = GetEncodedPublicKey(); + if (key.empty()) { + m_log->error("Something went wrong"); + // todo disconnect + return; + } + + nlohmann::json msg; + msg["op"] = "init"; + msg["encoded_public_key"] = key; + m_ws.Send(msg); +} + +void RemoteAuthClient::GenerateKey() { + // you javascript people have it so easy + // check out this documentation https://www.openssl.org/docs/man1.1.1/man3/PEM_write_bio_PUBKEY.html + + m_pkey_ctx.reset(EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, nullptr)); + if (!m_pkey_ctx) { + m_log->error("Failed to create RSA context"); + return; + } + + if (EVP_PKEY_keygen_init(m_pkey_ctx.get()) <= 0) { + m_log->error("Failed to initialize RSA context"); + return; + } + + if (EVP_PKEY_CTX_set_rsa_keygen_bits(m_pkey_ctx.get(), 2048) <= 0) { + m_log->error("Failed to set keygen bits"); + return; + } + + EVP_PKEY *pkey_tmp = nullptr; + if (EVP_PKEY_keygen(m_pkey_ctx.get(), &pkey_tmp) <= 0) { + m_log->error("Failed to generate keypair"); + return; + } + m_pkey.reset(pkey_tmp); + + m_dec_ctx.reset(EVP_PKEY_CTX_new(m_pkey.get(), nullptr)); + if (EVP_PKEY_decrypt_init(m_dec_ctx.get()) <= 0) { + m_log->error("Failed to initialize RSA decrypt context"); + return; + } + + if (EVP_PKEY_CTX_set_rsa_padding(m_dec_ctx.get(), RSA_PKCS1_OAEP_PADDING) <= 0) { + m_log->error("EVP_PKEY_CTX_set_rsa_padding failed"); + return; + } + + if (EVP_PKEY_CTX_set_rsa_oaep_md(m_dec_ctx.get(), EVP_sha256()) <= 0) { + m_log->error("EVP_PKEY_CTX_set_rsa_oaep_md failed"); + return; + } + + if (EVP_PKEY_CTX_set_rsa_mgf1_md(m_dec_ctx.get(), EVP_sha256()) <= 0) { + m_log->error("EVP_PKEY_CTX_set_rsa_mgf1_md"); + return; + } +} + +std::string RemoteAuthClient::GetEncodedPublicKey() const { + auto bio = BIO_ptr(BIO_new(BIO_s_mem()), BIO_free); + if (!bio) { + m_log->error("Failed to create BIO"); + return {}; + } + + if (PEM_write_bio_PUBKEY(bio.get(), m_pkey.get()) <= 0) { + m_log->error("Failed to write public key to BIO"); + return {}; + } + + // i think this is freed when the bio is too + BUF_MEM *mem = nullptr; + if (BIO_get_mem_ptr(bio.get(), &mem) <= 0) { + m_log->error("Failed to get BIO mem buf"); + return {}; + } + + if (mem->data == nullptr || mem->length == 0) { + m_log->error("BIO mem buf is null or of zero length"); + return {}; + } + + std::string pem_pubkey(mem->data, mem->length); + // isolate key + pem_pubkey.erase(0, pem_pubkey.find("\n") + 1); + pem_pubkey.erase(pem_pubkey.rfind("\n-")); + size_t pos; + while ((pos = pem_pubkey.find("\n")) != std::string::npos) { + pem_pubkey.erase(pos, 1); + } + return pem_pubkey; +} + +std::vector RemoteAuthClient::Decrypt(const unsigned char *in, size_t inlen) const { + // get length + size_t outlen; + if (EVP_PKEY_decrypt(m_dec_ctx.get(), nullptr, &outlen, in, inlen) <= 0) { + m_log->error("Failed to get length when decrypting"); + return {}; + } + + std::vector ret(outlen); + if (EVP_PKEY_decrypt(m_dec_ctx.get(), ret.data(), &outlen, in, inlen) <= 0) { + m_log->error("Failed to decrypt"); + return {}; + } + ret.resize(outlen); + return ret; +} + +void RemoteAuthClient::OnWebsocketOpen() { + m_log->info("Websocket opened"); +} + +void RemoteAuthClient::OnWebsocketClose(const ix::WebSocketCloseInfo &info) { + if (info.remote) { + m_log->debug("Websocket closed (remote): {} ({})", info.code, info.reason); + if (m_connected) { + m_signal_error.emit("Error. Websocket closed (remote): " + std::to_string(info.code) + " (" + info.reason + ")"); + } + } else { + m_log->debug("Websocket closed (local): {} ({})", info.code, info.reason); + if (m_connected) { + m_signal_error.emit("Error. Websocket closed (local): " + std::to_string(info.code) + " (" + info.reason + ")"); + } + } +} + +void RemoteAuthClient::OnWebsocketMessage(const std::string &data) { + m_dispatch_mutex.lock(); + m_dispatch_queue.push(data); + m_dispatcher.emit(); + m_dispatch_mutex.unlock(); +} + +void RemoteAuthClient::HeartbeatThread() { + while (true) { + if (!m_heartbeat_waiter.wait_for(std::chrono::milliseconds(m_heartbeat_msec))) break; + + nlohmann::json hb; + hb["op"] = "heartbeat"; + m_ws.Send(hb); + } +} + +void RemoteAuthClient::OnDispatch() { + m_dispatch_mutex.lock(); + if (m_dispatch_queue.empty()) { + m_dispatch_mutex.unlock(); + return; + } + auto msg = std::move(m_dispatch_queue.front()); + m_dispatch_queue.pop(); + m_dispatch_mutex.unlock(); + OnGatewayMessage(msg); +} + +bool RemoteAuthClient::OnTimeout() { + m_log->trace("Socket timeout"); + Stop(); + Start(); + return false; // disconnect +} + +RemoteAuthClient::type_signal_hello RemoteAuthClient::signal_hello() { + return m_signal_hello; +} + +RemoteAuthClient::type_signal_fingerprint RemoteAuthClient::signal_fingerprint() { + return m_signal_fingerprint; +} + +RemoteAuthClient::type_signal_pending_ticket RemoteAuthClient::signal_pending_ticket() { + return m_signal_pending_ticket; +} + +RemoteAuthClient::type_signal_pending_login RemoteAuthClient::signal_pending_login() { + return m_signal_pending_login; +} + +RemoteAuthClient::type_signal_token RemoteAuthClient::signal_token() { + return m_signal_token; +} + +RemoteAuthClient::type_signal_error RemoteAuthClient::signal_error() { + return m_signal_error; +} + +#endif diff --git a/src/remoteauth/remoteauthclient.hpp b/src/remoteauth/remoteauthclient.hpp new file mode 100644 index 0000000..6ab6dbb --- /dev/null +++ b/src/remoteauth/remoteauthclient.hpp @@ -0,0 +1,95 @@ +#pragma once + +#ifdef WITH_QRLOGIN + +// clang-format off + +#include +#include +#include +#include "ssl.hpp" +#include "discord/waiter.hpp" +#include "discord/websocket.hpp" + +// clang-format on + +class RemoteAuthClient { +public: + RemoteAuthClient(); + ~RemoteAuthClient(); + + void Start(); + void Stop(); + + [[nodiscard]] bool IsConnected() const noexcept; + +private: + void OnGatewayMessage(const std::string &str); + void HandleGatewayHello(const nlohmann::json &j); + void HandleGatewayNonceProof(const nlohmann::json &j); + void HandleGatewayPendingRemoteInit(const nlohmann::json &j); + void HandleGatewayPendingTicket(const nlohmann::json &j); + void HandleGatewayPendingLogin(const nlohmann::json &j); + void HandleGatewayCancel(const nlohmann::json &j); + + void OnRemoteAuthLoginResponse(const std::optional &encrypted_token, DiscordError err); + + void Init(); + + void GenerateKey(); + std::string GetEncodedPublicKey() const; + + std::vector Decrypt(const unsigned char *in, size_t inlen) const; + + void OnWebsocketOpen(); + void OnWebsocketClose(const ix::WebSocketCloseInfo &info); + void OnWebsocketMessage(const std::string &str); + + void HeartbeatThread(); + + int m_heartbeat_msec; + Waiter m_heartbeat_waiter; + std::thread m_heartbeat_thread; + + Glib::Dispatcher m_dispatcher; + std::queue m_dispatch_queue; + std::mutex m_dispatch_mutex; + + void OnDispatch(); + + bool OnTimeout(); + sigc::connection m_timeout_conn; + + Websocket m_ws; + bool m_connected = false; + + std::shared_ptr m_log; + + EVP_PKEY_CTX_ptr m_pkey_ctx; + EVP_PKEY_CTX_ptr m_dec_ctx; + EVP_PKEY_ptr m_pkey; + +public: + using type_signal_hello = sigc::signal; + using type_signal_fingerprint = sigc::signal; + using type_signal_pending_ticket = sigc::signal; + using type_signal_pending_login = sigc::signal; + using type_signal_token = sigc::signal; + using type_signal_error = sigc::signal; + type_signal_hello signal_hello(); + type_signal_fingerprint signal_fingerprint(); + type_signal_pending_ticket signal_pending_ticket(); + type_signal_pending_login signal_pending_login(); + type_signal_token signal_token(); + type_signal_error signal_error(); + +private: + type_signal_hello m_signal_hello; + type_signal_fingerprint m_signal_fingerprint; + type_signal_pending_ticket m_signal_pending_ticket; + type_signal_pending_login m_signal_pending_login; + type_signal_token m_signal_token; + type_signal_error m_signal_error; +}; + +#endif diff --git a/src/remoteauth/remoteauthdialog.cpp b/src/remoteauth/remoteauthdialog.cpp new file mode 100644 index 0000000..7975b4e --- /dev/null +++ b/src/remoteauth/remoteauthdialog.cpp @@ -0,0 +1,132 @@ +#ifdef WITH_QRLOGIN + +// clang-format off + +#include "remoteauthdialog.hpp" +#include + +// clang-format on + +RemoteAuthDialog::RemoteAuthDialog(Gtk::Window &parent) + : Gtk::Dialog("Login with QR Code", parent, true) + , m_layout(Gtk::ORIENTATION_VERTICAL) + , m_ok("OK") + , m_cancel("Cancel") + , m_bbox(Gtk::ORIENTATION_HORIZONTAL) { + set_default_size(300, 50); + get_style_context()->add_class("app-window"); + get_style_context()->add_class("app-popup"); + + m_ok.signal_clicked().connect([&]() { + response(Gtk::RESPONSE_OK); + }); + + m_cancel.signal_clicked().connect([&]() { + response(Gtk::RESPONSE_CANCEL); + }); + + m_bbox.pack_start(m_ok, Gtk::PACK_SHRINK); + m_bbox.pack_start(m_cancel, Gtk::PACK_SHRINK); + m_bbox.set_layout(Gtk::BUTTONBOX_END); + + m_ra.signal_hello().connect(sigc::mem_fun(*this, &RemoteAuthDialog::OnHello)); + m_ra.signal_fingerprint().connect(sigc::mem_fun(*this, &RemoteAuthDialog::OnFingerprint)); + m_ra.signal_pending_ticket().connect(sigc::mem_fun(*this, &RemoteAuthDialog::OnPendingTicket)); + m_ra.signal_pending_login().connect(sigc::mem_fun(*this, &RemoteAuthDialog::OnPendingLogin)); + m_ra.signal_token().connect(sigc::mem_fun(*this, &RemoteAuthDialog::OnToken)); + m_ra.signal_error().connect(sigc::mem_fun(*this, &RemoteAuthDialog::OnError)); + + m_ra.Start(); + + m_image.set_size_request(256, 256); + + m_status.set_text("Connecting..."); + m_status.set_hexpand(true); + m_status.set_halign(Gtk::ALIGN_CENTER); + + m_layout.add(m_image); + m_layout.add(m_status); + m_layout.add(m_bbox); + get_content_area()->add(m_layout); + + show_all_children(); +} + +std::string RemoteAuthDialog::GetToken() { + return m_token; +} + +void RemoteAuthDialog::OnHello() { + m_status.set_text("Handshaking..."); +} + +void RemoteAuthDialog::OnFingerprint(const std::string &fingerprint) { + m_status.set_text("Waiting for mobile device..."); + + const auto url = "https://discord.com/ra/" + fingerprint; + + const auto level = qrcodegen::QrCode::Ecc::QUARTILE; + const auto qr = qrcodegen::QrCode::encodeText(url.c_str(), level); + + int size = qr.getSize(); + const int border = 4; + + const auto module_set = "192 0 255"; + const auto module_clr = "255 255 255"; + + std::ostringstream sb; + sb << "P3\n"; + sb << size + border * 2 << " " << size + border * 2 << " 255\n"; + for (int y = -border; y < size + border; y++) { + for (int x = -border; x < size + border; x++) { + if (qr.getModule(x, y)) { + sb << module_set << "\n"; + } else { + sb << module_clr << "\n"; + } + } + } + + const auto img = sb.str(); + + auto loader = Gdk::PixbufLoader::create(); + loader->write(reinterpret_cast(img.data()), img.size()); + loader->close(); + const auto pb = loader->get_pixbuf()->scale_simple(256, 256, Gdk::INTERP_NEAREST); + + m_image.property_pixbuf() = pb; +} + +void RemoteAuthDialog::OnPendingTicket(Snowflake user_id, const std::string &discriminator, const std::string &avatar_hash, const std::string &username) { + Glib::ustring name = username; + if (discriminator != "0") { + name += "#" + discriminator; + } + m_status.set_text("Waiting for confirmation... (" + name + ")"); + + if (!avatar_hash.empty()) { + const auto url = "https://cdn.discordapp.com/avatars/" + std::to_string(user_id) + "/" + avatar_hash + ".png?size=256"; + const auto cb = [this](const Glib::RefPtr &pb) { + m_image.property_pixbuf() = pb->scale_simple(256, 256, Gdk::INTERP_BILINEAR); + }; + Abaddon::Get().GetImageManager().LoadFromURL(url, sigc::track_obj(cb, *this)); + } +} + +void RemoteAuthDialog::OnPendingLogin() { + m_status.set_text("Logging in!"); +} + +void RemoteAuthDialog::OnToken(const std::string &token) { + m_token = token; + m_ra.Stop(); + response(Gtk::RESPONSE_OK); +} + +void RemoteAuthDialog::OnError(const std::string &error) { + m_ra.Stop(); + Abaddon::Get().ShowConfirm(error, dynamic_cast(get_toplevel())); + response(Gtk::RESPONSE_CANCEL); +} + +#endif diff --git a/src/remoteauth/remoteauthdialog.hpp b/src/remoteauth/remoteauthdialog.hpp new file mode 100644 index 0000000..465a188 --- /dev/null +++ b/src/remoteauth/remoteauthdialog.hpp @@ -0,0 +1,38 @@ +#pragma once + +#ifdef WITH_QRLOGIN + +// clang-format off + +#include +#include "remoteauthclient.hpp" + +// clang-format on + +class RemoteAuthDialog : public Gtk::Dialog { +public: + RemoteAuthDialog(Gtk::Window &parent); + std::string GetToken(); + +protected: + Gtk::Image m_image; + Gtk::Label m_status; + Gtk::Box m_layout; + Gtk::Button m_ok; + Gtk::Button m_cancel; + Gtk::ButtonBox m_bbox; + +private: + RemoteAuthClient m_ra; + + void OnHello(); + void OnFingerprint(const std::string &fingerprint); + void OnPendingTicket(Snowflake user_id, const std::string &discriminator, const std::string &avatar_hash, const std::string &username); + void OnPendingLogin(); + void OnToken(const std::string &token); + void OnError(const std::string &error); + + std::string m_token; +}; + +#endif diff --git a/src/remoteauth/ssl.hpp b/src/remoteauth/ssl.hpp new file mode 100644 index 0000000..1753bd3 --- /dev/null +++ b/src/remoteauth/ssl.hpp @@ -0,0 +1,31 @@ +#pragma once +#include +#include +#include +#include +#include +#include + +struct EVP_PKEY_CTX_deleter { + void operator()(EVP_PKEY_CTX *ptr) const { + EVP_PKEY_CTX_free(ptr); + } +}; + +struct EVP_PKEY_deleter { + void operator()(EVP_PKEY *ptr) const { + EVP_PKEY_free(ptr); + } +}; + +struct EVP_MD_CTX_deleter { + void operator()(EVP_MD_CTX *ptr) const { + EVP_MD_CTX_free(ptr); + } +}; + +using EVP_PKEY_CTX_ptr = std::unique_ptr; +using EVP_PKEY_ptr = std::unique_ptr; +using EVP_MD_CTX_ptr = std::unique_ptr; +using BIO_ptr = std::unique_ptr; +using BUF_MEM_ptr = std::unique_ptr; diff --git a/src/settings.cpp b/src/settings.cpp index 82401f5..0b868da 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -70,6 +70,7 @@ void SettingsManager::ReadSettings() { SMSTR("style", "unreadcolor", UnreadIndicatorColor); SMBOOL("notifications", "enabled", NotificationsEnabled); SMBOOL("notifications", "playsound", NotificationsPlaySound); + SMBOOL("windows", "hideconsole", HideConsole); #ifdef WITH_KEYCHAIN keychain::Error error {}; @@ -153,6 +154,7 @@ void SettingsManager::Close() { SMSTR("style", "unreadcolor", UnreadIndicatorColor); SMBOOL("notifications", "enabled", NotificationsEnabled); SMBOOL("notifications", "playsound", NotificationsPlaySound); + SMBOOL("windows", "hideconsole", HideConsole); #ifdef WITH_KEYCHAIN keychain::Error error {}; diff --git a/src/settings.hpp b/src/settings.hpp index 53f3423..40cb1d3 100644 --- a/src/settings.hpp +++ b/src/settings.hpp @@ -52,6 +52,9 @@ public: bool NotificationsEnabled { true }; #endif bool NotificationsPlaySound { true }; + + // [windows] + bool HideConsole { false }; }; SettingsManager(const std::string &filename); diff --git a/src/windows/guildsettings/auditlogpane.cpp b/src/windows/guildsettings/auditlogpane.cpp index 53bfd15..f2f4a17 100644 --- a/src/windows/guildsettings/auditlogpane.cpp +++ b/src/windows/guildsettings/auditlogpane.cpp @@ -38,7 +38,7 @@ void GuildSettingsAuditLogPane::OnAuditLogFetch(const AuditLogData &data) { Glib::ustring user_markup = "Unknown User"; if (entry.UserID.has_value()) { if (auto user = discord.GetUser(*entry.UserID); user.has_value()) - user_markup = discord.GetUser(*entry.UserID)->GetEscapedBoldString(); + user_markup = discord.GetUser(*entry.UserID)->GetUsernameEscapedBold(); } // spaghetti moment @@ -177,7 +177,7 @@ void GuildSettingsAuditLogPane::OnAuditLogFetch(const AuditLogData &data) { const auto target_user = discord.GetUser(entry.TargetID); markup = user_markup + " kicked " + - target_user->GetEscapedString() + + target_user->GetUsernameEscaped() + ""; } break; case AuditLogActionType::MEMBER_PRUNE: { @@ -193,21 +193,21 @@ void GuildSettingsAuditLogPane::OnAuditLogFetch(const AuditLogData &data) { const auto target_user = discord.GetUser(entry.TargetID); markup = user_markup + " banned " + - target_user->GetEscapedString() + + target_user->GetUsernameEscaped() + ""; } break; case AuditLogActionType::MEMBER_BAN_REMOVE: { const auto target_user = discord.GetUser(entry.TargetID); markup = user_markup + " removed the ban for " + - target_user->GetEscapedString() + + target_user->GetUsernameEscaped() + ""; } break; case AuditLogActionType::MEMBER_UPDATE: { const auto target_user = discord.GetUser(entry.TargetID); markup = user_markup + " updated " + - target_user->GetEscapedString() + + target_user->GetUsernameEscaped() + ""; if (entry.Changes.has_value()) for (const auto &change : *entry.Changes) { @@ -227,7 +227,7 @@ void GuildSettingsAuditLogPane::OnAuditLogFetch(const AuditLogData &data) { const auto target_user = discord.GetUser(entry.TargetID); markup = user_markup + " updated roles for " + - target_user->GetEscapedString() + ""; + target_user->GetUsernameEscaped() + ""; if (entry.Changes.has_value()) for (const auto &change : *entry.Changes) { if (change.Key == "$remove" && change.NewValue.has_value()) { @@ -262,7 +262,7 @@ void GuildSettingsAuditLogPane::OnAuditLogFetch(const AuditLogData &data) { const auto target_user = discord.GetUser(entry.TargetID); markup = user_markup + " added " + - target_user->GetEscapedString() + + target_user->GetUsernameEscaped() + " to the server"; } break; case AuditLogActionType::ROLE_CREATE: { @@ -450,14 +450,14 @@ void GuildSettingsAuditLogPane::OnAuditLogFetch(const AuditLogData &data) { const auto target_user = discord.GetUser(entry.TargetID); markup = user_markup + " pinned a message by " + - target_user->GetEscapedString() + + target_user->GetUsernameEscaped() + ""; } break; case AuditLogActionType::MESSAGE_UNPIN: { const auto target_user = discord.GetUser(entry.TargetID); markup = user_markup + " unpinned a message by " + - target_user->GetEscapedString() + + target_user->GetUsernameEscaped() + ""; } break; case AuditLogActionType::STAGE_INSTANCE_CREATE: { diff --git a/src/windows/guildsettings/banspane.cpp b/src/windows/guildsettings/banspane.cpp index bfc7331..68a232d 100644 --- a/src/windows/guildsettings/banspane.cpp +++ b/src/windows/guildsettings/banspane.cpp @@ -72,7 +72,7 @@ void GuildSettingsBansPane::OnGuildBanFetch(const BanData &ban) { auto row = *m_model->append(); row[m_columns.m_col_id] = ban.User.ID; if (user.has_value()) - row[m_columns.m_col_user] = user->Username + "#" + user->Discriminator; + row[m_columns.m_col_user] = user->GetUsername(); else row[m_columns.m_col_user] = "<@" + std::to_string(ban.User.ID) + ">"; @@ -84,7 +84,7 @@ void GuildSettingsBansPane::OnGuildBansFetch(const std::vector &bans) { const auto user = Abaddon::Get().GetDiscordClient().GetUser(ban.User.ID); auto row = *m_model->append(); row[m_columns.m_col_id] = user->ID; - row[m_columns.m_col_user] = user->Username + "#" + user->Discriminator; + row[m_columns.m_col_user] = user->GetUsername(); row[m_columns.m_col_reason] = ban.Reason; } } @@ -148,7 +148,7 @@ void GuildSettingsBansPane::OnBanAdd(Snowflake guild_id, Snowflake user_id) { auto user = *discord.GetUser(user_id); auto row = *m_model->append(); row[m_columns.m_col_id] = user_id; - row[m_columns.m_col_user] = user.Username + "#" + user.Discriminator; + row[m_columns.m_col_user] = user.GetUsername(); row[m_columns.m_col_reason] = ""; } } diff --git a/src/windows/guildsettings/emojispane.cpp b/src/windows/guildsettings/emojispane.cpp index c86ea64..702432f 100644 --- a/src/windows/guildsettings/emojispane.cpp +++ b/src/windows/guildsettings/emojispane.cpp @@ -119,7 +119,7 @@ void GuildSettingsEmojisPane::AddEmojiRow(const EmojiData &emoji) { row[m_columns.m_col_pixbuf] = img.GetPlaceholder(32); row[m_columns.m_col_name] = emoji.Name; if (emoji.Creator.has_value()) - row[m_columns.m_col_creator] = emoji.Creator->Username + "#" + emoji.Creator->Discriminator; + row[m_columns.m_col_creator] = emoji.Creator->GetUsername(); if (emoji.IsAnimated.has_value()) row[m_columns.m_col_animated] = *emoji.IsAnimated ? "Yes" : "No"; else diff --git a/src/windows/guildsettings/invitespane.cpp b/src/windows/guildsettings/invitespane.cpp index 4426f6a..a76b3f2 100644 --- a/src/windows/guildsettings/invitespane.cpp +++ b/src/windows/guildsettings/invitespane.cpp @@ -51,7 +51,7 @@ void GuildSettingsInvitesPane::AppendInvite(const InviteData &invite) { auto row = *m_model->append(); row[m_columns.m_col_code] = invite.Code; if (invite.Inviter.has_value()) - row[m_columns.m_col_inviter] = invite.Inviter->Username + "#" + invite.Inviter->Discriminator; + row[m_columns.m_col_inviter] = invite.Inviter->GetUsername(); if (invite.MaxAge.has_value()) { if (*invite.MaxAge == 0) diff --git a/src/windows/guildsettings/memberspane.cpp b/src/windows/guildsettings/memberspane.cpp index 64c28cb..8c613d0 100644 --- a/src/windows/guildsettings/memberspane.cpp +++ b/src/windows/guildsettings/memberspane.cpp @@ -103,7 +103,7 @@ GuildSettingsMembersListItem::GuildSettingsMembersListItem(const GuildData &guil else m_avatar.SetURL(member.User->GetAvatarURL("png", "32")); - DisplayTerm = member.User->Username + "#" + member.User->Discriminator; + DisplayTerm = member.User->GetUsername(); const auto member_update_cb = [this](Snowflake guild_id, Snowflake user_id) { if (user_id == UserID) @@ -150,9 +150,9 @@ void GuildSettingsMembersListItem::UpdateColor() { const auto user = *discord.GetUser(UserID); if (auto color_id = discord.GetMemberHoistedRole(GuildID, UserID, true); color_id.IsValid()) { auto role = *discord.GetRole(color_id); - m_name.set_markup("" + user.GetEscapedBoldString() + ""); + m_name.set_markup("" + user.GetUsernameEscapedBold() + ""); } else - m_name.set_markup(user.GetEscapedBoldString()); + m_name.set_markup(user.GetUsernameEscapedBold()); } GuildSettingsMembersPaneInfo::GuildSettingsMembersPaneInfo(Snowflake guild_id) diff --git a/src/windows/mainwindow.cpp b/src/windows/mainwindow.cpp index bb09735..8e030ed 100644 --- a/src/windows/mainwindow.cpp +++ b/src/windows/mainwindow.cpp @@ -168,6 +168,10 @@ void MainWindow::ToggleMenuVisibility() { m_menu_bar.set_visible(!m_menu_bar.get_visible()); } +void MainWindow::EditMessage(Snowflake message_id) { + m_chat.StartEditing(message_id); +} + #ifdef WITH_LIBHANDY void MainWindow::GoBack() { m_chat.GoBack(); @@ -206,6 +210,9 @@ void MainWindow::OnDiscordSubmenuPopup() { m_menu_discord_connect.set_sensitive(!token.empty() && !discord_active); m_menu_discord_disconnect.set_sensitive(discord_active); m_menu_discord_set_token.set_sensitive(!discord_active); +#ifdef WITH_QRLOGIN + m_menu_discord_login_qr.set_sensitive(!discord_active); +#endif m_menu_discord_set_status.set_sensitive(discord_active); } @@ -247,12 +254,18 @@ void MainWindow::SetupMenu() { m_menu_discord_disconnect.set_label("Disconnect"); m_menu_discord_disconnect.set_sensitive(false); m_menu_discord_set_token.set_label("Set Token"); + m_menu_discord_login_qr.set_label("Login with QR Code"); +#ifndef WITH_QRLOGIN + m_menu_discord_login_qr.set_sensitive(false); + m_menu_discord_login_qr.set_tooltip_text("Not compiled with support"); +#endif m_menu_discord_set_status.set_label("Set Status"); m_menu_discord_set_status.set_sensitive(false); m_menu_discord_add_recipient.set_label("Add user to DM"); m_menu_discord_sub.append(m_menu_discord_connect); m_menu_discord_sub.append(m_menu_discord_disconnect); m_menu_discord_sub.append(m_menu_discord_set_token); + m_menu_discord_sub.append(m_menu_discord_login_qr); m_menu_discord_sub.append(m_menu_discord_set_status); m_menu_discord_sub.append(m_menu_discord_add_recipient); m_menu_discord.set_submenu(m_menu_discord_sub); @@ -331,6 +344,10 @@ void MainWindow::SetupMenu() { m_signal_action_set_token.emit(); }); + m_menu_discord_login_qr.signal_activate().connect([this] { + m_signal_action_login_qr.emit(); + }); + m_menu_file_reload_css.signal_activate().connect([this] { m_signal_action_reload_css.emit(); }); @@ -421,6 +438,10 @@ MainWindow::type_signal_action_set_token MainWindow::signal_action_set_token() { return m_signal_action_set_token; } +MainWindow::type_signal_action_login_qr MainWindow::signal_action_login_qr() { + return m_signal_action_login_qr; +} + MainWindow::type_signal_action_reload_css MainWindow::signal_action_reload_css() { return m_signal_action_reload_css; } diff --git a/src/windows/mainwindow.hpp b/src/windows/mainwindow.hpp index e075c10..ce2e636 100644 --- a/src/windows/mainwindow.hpp +++ b/src/windows/mainwindow.hpp @@ -29,6 +29,7 @@ public: void UpdateChatReactionRemove(Snowflake id, const Glib::ustring ¶m); void UpdateMenus(); void ToggleMenuVisibility(); + void EditMessage(Snowflake message_id); #ifdef WITH_LIBHANDY void GoBack(); @@ -74,6 +75,7 @@ private: Gtk::MenuItem m_menu_discord_connect; Gtk::MenuItem m_menu_discord_disconnect; Gtk::MenuItem m_menu_discord_set_token; + Gtk::MenuItem m_menu_discord_login_qr; Gtk::MenuItem m_menu_discord_set_status; Gtk::MenuItem m_menu_discord_add_recipient; // move me somewhere else some day void OnDiscordSubmenuPopup(); @@ -103,6 +105,7 @@ public: typedef sigc::signal type_signal_action_connect; typedef sigc::signal type_signal_action_disconnect; typedef sigc::signal type_signal_action_set_token; + typedef sigc::signal type_signal_action_login_qr; typedef sigc::signal type_signal_action_reload_css; typedef sigc::signal type_signal_action_set_status; // this should probably be removed @@ -113,6 +116,7 @@ public: type_signal_action_connect signal_action_connect(); type_signal_action_disconnect signal_action_disconnect(); type_signal_action_set_token signal_action_set_token(); + type_signal_action_login_qr signal_action_login_qr(); type_signal_action_reload_css signal_action_reload_css(); type_signal_action_set_status signal_action_set_status(); type_signal_action_add_recipient signal_action_add_recipient(); @@ -123,6 +127,7 @@ private: type_signal_action_connect m_signal_action_connect; type_signal_action_disconnect m_signal_action_disconnect; type_signal_action_set_token m_signal_action_set_token; + type_signal_action_login_qr m_signal_action_login_qr; type_signal_action_reload_css m_signal_action_reload_css; type_signal_action_set_status m_signal_action_set_status; type_signal_action_add_recipient m_signal_action_add_recipient; diff --git a/src/windows/profile/mutualfriendspane.cpp b/src/windows/profile/mutualfriendspane.cpp index e9f465b..75fb4a6 100644 --- a/src/windows/profile/mutualfriendspane.cpp +++ b/src/windows/profile/mutualfriendspane.cpp @@ -22,7 +22,7 @@ MutualFriendItem::MutualFriendItem(const UserData &user) img.LoadFromURL(user.GetAvatarURL("png", "32"), sigc::track_obj(cb, *this)); } - m_name.set_markup(user.GetEscapedBoldString()); + m_name.set_markup(user.GetUsernameEscapedBold()); m_name.set_valign(Gtk::ALIGN_CENTER); add(m_avatar); diff --git a/src/windows/profilewindow.cpp b/src/windows/profilewindow.cpp index 3d5f140..72996aa 100644 --- a/src/windows/profilewindow.cpp +++ b/src/windows/profilewindow.cpp @@ -5,6 +5,7 @@ ProfileWindow::ProfileWindow(Snowflake user_id) , m_main(Gtk::ORIENTATION_VERTICAL) , m_upper(Gtk::ORIENTATION_HORIZONTAL) , m_badges(Gtk::ORIENTATION_HORIZONTAL) + , m_name_box(Gtk::ORIENTATION_VERTICAL) , m_pane_info(user_id) , m_pane_guilds(user_id) , m_pane_friends(user_id) { @@ -15,14 +16,15 @@ ProfileWindow::ProfileWindow(Snowflake user_id) set_name("user-profile"); set_default_size(450, 375); - set_title(user.Username + "#" + user.Discriminator); + set_title(user.GetUsername()); set_position(Gtk::WIN_POS_CENTER); get_style_context()->add_class("app-window"); get_style_context()->add_class("app-popup"); get_style_context()->add_class("user-profile-window"); m_main.get_style_context()->add_class("profile-main-container"); m_avatar.get_style_context()->add_class("profile-avatar"); - m_username.get_style_context()->add_class("profile-username"); + m_displayname.get_style_context()->add_class("profile-username"); + m_username.get_style_context()->add_class("profile-username-nondisplay"); m_switcher.get_style_context()->add_class("profile-switcher"); m_stack.get_style_context()->add_class("profile-stack"); m_badges.get_style_context()->add_class("profile-badges"); @@ -31,8 +33,8 @@ ProfileWindow::ProfileWindow(Snowflake user_id) m_scroll.set_vexpand(true); m_scroll.set_propagate_natural_height(true); - if (user.HasAvatar()) - AddPointerCursor(m_avatar_ev); + if (user.HasAvatar()) AddPointerCursor(m_avatar_ev); + m_avatar_ev.signal_button_release_event().connect([user](GdkEventButton *event) -> bool { if (event->type == GDK_BUTTON_RELEASE && event->button == GDK_BUTTON_PRIMARY) { if (user.HasAnimatedAvatar()) @@ -62,7 +64,8 @@ ProfileWindow::ProfileWindow(Snowflake user_id) img.LoadFromURL(user.GetAvatarURL("png", "64"), sigc::track_obj(cb, *this)); } - m_username.set_markup(user.GetEscapedString()); + m_displayname.set_markup(user.GetDisplayNameEscaped()); + m_username.set_label(user.GetUsername()); m_switcher.set_stack(m_stack); m_switcher.set_halign(Gtk::ALIGN_START); @@ -79,10 +82,13 @@ ProfileWindow::ProfileWindow(Snowflake user_id) m_upper.set_halign(Gtk::ALIGN_START); m_avatar.set_halign(Gtk::ALIGN_START); + m_displayname.set_halign(Gtk::ALIGN_START); m_username.set_halign(Gtk::ALIGN_START); m_avatar_ev.add(m_avatar); m_upper.add(m_avatar_ev); - m_upper.add(m_username); + m_upper.add(m_name_box); + m_name_box.add(m_displayname); + m_name_box.add(m_username); m_badges_scroll.add(m_badges); m_upper.add(m_badges_scroll); m_main.add(m_upper); @@ -97,8 +103,13 @@ void ProfileWindow::OnFetchProfile(const UserProfileData &data) { m_pane_info.SetProfile(data); m_pane_guilds.SetMutualGuilds(data.MutualGuilds); - for (auto child : m_badges.get_children()) + if (data.LegacyUsername.has_value()) { + m_username.set_tooltip_text("Originally known as " + *data.LegacyUsername); + } + + for (auto child : m_badges.get_children()) { delete child; + } if (!data.User.PublicFlags.has_value()) return; const auto x = *data.User.PublicFlags; diff --git a/src/windows/profilewindow.hpp b/src/windows/profilewindow.hpp index 2052e7b..fbc30e0 100644 --- a/src/windows/profilewindow.hpp +++ b/src/windows/profilewindow.hpp @@ -16,9 +16,11 @@ private: Gtk::Box m_main; Gtk::Box m_upper; Gtk::Box m_badges; + Gtk::Box m_name_box; Gtk::ScrolledWindow m_badges_scroll; Gtk::EventBox m_avatar_ev; Gtk::Image m_avatar; + Gtk::Label m_displayname; Gtk::Label m_username; Gtk::ScrolledWindow m_scroll; Gtk::Stack m_stack; diff --git a/src/windows/voicesettingswindow.cpp b/src/windows/voicesettingswindow.cpp index c009cbf..3749986 100644 --- a/src/windows/voicesettingswindow.cpp +++ b/src/windows/voicesettingswindow.cpp @@ -110,9 +110,17 @@ VoiceSettingsWindow::VoiceSettingsWindow() } }); + m_gain.set_increments(1.0, 5.0); + m_gain.set_range(0.0, 6969696969.0); + m_gain.set_value(Abaddon::Get().GetAudio().GetCaptureGain() * 100.0); + m_gain.signal_value_changed().connect([this]() { + m_signal_gain.emit(m_gain.get_value() / 100.0); + }); + m_main.add(m_encoding_mode); m_main.add(m_signal); m_main.add(m_bitrate); + m_main.add(m_gain); add(m_main); show_all_children(); @@ -122,4 +130,8 @@ VoiceSettingsWindow::VoiceSettingsWindow() }); } +VoiceSettingsWindow::type_signal_gain VoiceSettingsWindow::signal_gain() { + return m_signal_gain; +} + #endif diff --git a/src/windows/voicesettingswindow.hpp b/src/windows/voicesettingswindow.hpp index cf6b477..9b3498e 100644 --- a/src/windows/voicesettingswindow.hpp +++ b/src/windows/voicesettingswindow.hpp @@ -18,8 +18,14 @@ public: Gtk::ComboBoxText m_encoding_mode; Gtk::ComboBoxText m_signal; Gtk::Scale m_bitrate; + Gtk::SpinButton m_gain; private: + using type_signal_gain = sigc::signal; + type_signal_gain m_signal_gain; + +public: + type_signal_gain signal_gain(); }; #endif diff --git a/src/windows/voicewindow.cpp b/src/windows/voicewindow.cpp index 9e2efee..829a1b8 100644 --- a/src/windows/voicewindow.cpp +++ b/src/windows/voicewindow.cpp @@ -39,7 +39,7 @@ public: auto &discord = Abaddon::Get().GetDiscordClient(); const auto user = discord.GetUser(id); if (user.has_value()) { - m_name.set_text(user->Username); + m_name.set_text(user->GetUsername()); m_avatar.SetURL(user->GetAvatarURL("png", "32")); } else { m_name.set_text("Unknown user"); @@ -139,7 +139,9 @@ VoiceWindow::VoiceWindow(Snowflake channel_id) m_playback_combo.set_hexpand(true); m_playback_combo.set_halign(Gtk::ALIGN_FILL); m_playback_combo.set_model(Abaddon::Get().GetAudio().GetDevices().GetPlaybackDeviceModel()); - m_playback_combo.set_active(Abaddon::Get().GetAudio().GetDevices().GetActivePlaybackDevice()); + if (const auto iter = Abaddon::Get().GetAudio().GetDevices().GetActivePlaybackDevice()) { + m_playback_combo.set_active(iter); + } m_playback_combo.pack_start(*playback_renderer); m_playback_combo.add_attribute(*playback_renderer, "text", 0); m_playback_combo.signal_changed().connect([this]() { @@ -151,7 +153,9 @@ VoiceWindow::VoiceWindow(Snowflake channel_id) m_capture_combo.set_hexpand(true); m_capture_combo.set_halign(Gtk::ALIGN_FILL); m_capture_combo.set_model(Abaddon::Get().GetAudio().GetDevices().GetCaptureDeviceModel()); - m_capture_combo.set_active(Abaddon::Get().GetAudio().GetDevices().GetActiveCaptureDevice()); + if (const auto iter = Abaddon::Get().GetAudio().GetDevices().GetActiveCaptureDevice()) { + m_capture_combo.set_active(iter); + } m_capture_combo.pack_start(*capture_renderer); m_capture_combo.add_attribute(*capture_renderer, "text", 0); m_capture_combo.signal_changed().connect([this]() { @@ -163,6 +167,11 @@ VoiceWindow::VoiceWindow(Snowflake channel_id) m_menu_view_sub.append(m_menu_view_settings); m_menu_view_settings.signal_activate().connect([this]() { auto *window = new VoiceSettingsWindow; + const auto cb = [this](double gain) { + m_capture_gain.set_value(gain * 100.0); + m_signal_gain.emit(gain); + }; + window->signal_gain().connect(sigc::track_obj(cb, *this)); window->show(); }); diff --git a/subprojects/qrcodegen b/subprojects/qrcodegen new file mode 160000 index 0000000..22fac31 --- /dev/null +++ b/subprojects/qrcodegen @@ -0,0 +1 @@ +Subproject commit 22fac31bdf81da68730c177c0e931c93234d2a30