diff --git a/abaddon.cpp b/abaddon.cpp index df63cf4..f0d94e4 100644 --- a/abaddon.cpp +++ b/abaddon.cpp @@ -20,6 +20,11 @@ Abaddon::Abaddon() , m_discord(m_settings.GetSettingBool("discord", "memory_db", false)) { // stupid but easy LoadFromSettings(); + // todo: set user agent for non-client(?) + std::string ua = m_settings.GetSettingString("http", "user_agent", ""); + if (ua != "") + m_discord.SetUserAgent(ua); + m_discord.signal_gateway_ready().connect(sigc::mem_fun(*this, &Abaddon::DiscordOnReady)); m_discord.signal_message_create().connect(sigc::mem_fun(*this, &Abaddon::DiscordOnMessageCreate)); m_discord.signal_message_delete().connect(sigc::mem_fun(*this, &Abaddon::DiscordOnMessageDelete)); diff --git a/discord/discord.cpp b/discord/discord.cpp index a787c70..bad6cc4 100644 --- a/discord/discord.cpp +++ b/discord/discord.cpp @@ -424,6 +424,11 @@ void DiscordClient::UpdateToken(std::string token) { } } +void DiscordClient::SetUserAgent(std::string agent) { + m_http.SetUserAgent(agent); + m_websocket.SetUserAgent(agent); +} + void DiscordClient::HandleGatewayMessageRaw(std::string str) { // handles multiple zlib compressed messages, calling HandleGatewayMessage when a full message is received std::vector buf(str.begin(), str.end()); diff --git a/discord/discord.hpp b/discord/discord.hpp index df7675a..50886bd 100644 --- a/discord/discord.hpp +++ b/discord/discord.hpp @@ -112,6 +112,7 @@ public: std::optional FindDM(Snowflake user_id); // wont find group dms void UpdateToken(std::string token); + void SetUserAgent(std::string agent); private: static const constexpr int InflateChunkSize = 0x10000; diff --git a/discord/http.cpp b/discord/http.cpp index 41f6c17..a435aef 100644 --- a/discord/http.cpp +++ b/discord/http.cpp @@ -6,6 +6,10 @@ HTTPClient::HTTPClient(std::string api_base) m_dispatcher.connect(sigc::mem_fun(*this, &HTTPClient::RunCallbacks)); } +void HTTPClient::SetUserAgent(std::string agent) { + m_agent = agent; +} + void HTTPClient::SetAuth(std::string auth) { m_authorization = auth; } @@ -16,16 +20,18 @@ void HTTPClient::MakeDELETE(std::string path, std::function { "Authorization", m_authorization }, { "Content-Type", "application/json" }, }; + auto ua = cpr::UserAgent { m_agent != "" ? m_agent : "Abaddon" }; + + auto x = cpr::UserAgent {}; #ifdef USE_LOCAL_PROXY m_futures.push_back(cpr::GetCallback( std::bind(&HTTPClient::OnResponse, this, std::placeholders::_1, cb), - url, headers, + url, headers, ua, cpr::Proxies { { "http", "127.0.0.1:8888" }, { "https", "127.0.0.1:8888" } }, cpr::VerifySsl { false })); #else m_futures.push_back(cpr::GetCallback( std::bind(&HTTPClient::OnResponse, this, std::placeholders::_1, cb), - url, headers)); + url, headers, ua)); #endif } diff --git a/discord/http.hpp b/discord/http.hpp index db60498..b664c86 100644 --- a/discord/http.hpp +++ b/discord/http.hpp @@ -13,6 +13,7 @@ class HTTPClient { public: HTTPClient(std::string api_base); + void SetUserAgent(std::string agent); void SetAuth(std::string auth); void MakeDELETE(std::string path, std::function cb); void MakeGET(std::string path, std::function cb); @@ -32,4 +33,5 @@ private: std::vector> m_futures; std::string m_api_base; std::string m_authorization; + std::string m_agent; }; diff --git a/discord/websocket.cpp b/discord/websocket.cpp index 2dc4ad0..5cd82b1 100644 --- a/discord/websocket.cpp +++ b/discord/websocket.cpp @@ -7,9 +7,14 @@ void Websocket::StartConnection(std::string url) { m_websocket.disableAutomaticReconnection(); m_websocket.setUrl(url); m_websocket.setOnMessageCallback(std::bind(&Websocket::OnMessage, this, std::placeholders::_1)); + m_websocket.setExtraHeaders(ix::WebSocketHttpHeaders { { "User-Agent", m_agent } }); // idk if this actually works m_websocket.start(); } +void Websocket::SetUserAgent(std::string agent) { + m_agent = agent; +} + void Websocket::Stop() { m_websocket.stop(); } diff --git a/discord/websocket.hpp b/discord/websocket.hpp index ed6317a..e6a6489 100644 --- a/discord/websocket.hpp +++ b/discord/websocket.hpp @@ -11,6 +11,8 @@ public: Websocket(); void StartConnection(std::string url); + void SetUserAgent(std::string agent); + void Send(const std::string &str); void Send(const nlohmann::json &j); void Stop(); @@ -21,6 +23,7 @@ private: void OnMessage(const ix::WebSocketMessagePtr &msg); ix::WebSocket m_websocket; + std::string m_agent; public: typedef sigc::signal type_signal_open; diff --git a/filecache.cpp b/filecache.cpp index 1e15d24..9a7c062 100644 --- a/filecache.cpp +++ b/filecache.cpp @@ -85,15 +85,15 @@ void Cache::OnResponse(const cpr::Response &r) { if (r.error || r.status_code > 300) return; std::vector data(r.text.begin(), r.text.end()); - auto path = m_tmp_path / SanitizeString(r.url); + auto path = m_tmp_path / SanitizeString(static_cast(r.url)); FILE *fp = std::fopen(path.string().c_str(), "wb"); if (fp == nullptr) return; std::fwrite(data.data(), 1, data.size(), fp); std::fclose(fp); - for (const auto &cb : m_callbacks[r.url]) { + for (const auto &cb : m_callbacks[static_cast(r.url)]) { cb(path.string()); } - m_callbacks.erase(r.url); + m_callbacks.erase(static_cast(r.url)); } diff --git a/windows/mainwindow.cpp b/windows/mainwindow.cpp index dfb6c2b..0ff95e6 100644 --- a/windows/mainwindow.cpp +++ b/windows/mainwindow.cpp @@ -86,8 +86,8 @@ MainWindow::MainWindow() m_chan_chat_paned.pack1(*channel_list); m_chan_chat_paned.pack2(m_chat_members_paned); - m_chan_chat_paned.child_property_shrink(*channel_list) = true; - m_chan_chat_paned.child_property_resize(*channel_list) = true; + m_chan_chat_paned.child_property_shrink(*channel_list) = false; + m_chan_chat_paned.child_property_resize(*channel_list) = false; m_chan_chat_paned.set_position(200); m_content_box.add(m_chan_chat_paned);