From 9c3d08bd457fcd596f74a726a67efeac4daf4c47 Mon Sep 17 00:00:00 2001 From: harrrson Date: Wed, 17 Jan 2024 21:34:52 +0100 Subject: [PATCH 1/3] feat: added expire and issued timestamp in attachment --- include/dpp/message.h | 14 ++++++++++++++ src/dpp/message.cpp | 24 ++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/include/dpp/message.h b/include/dpp/message.h index 4a593ecf7a..1c274b3ae8 100644 --- a/include/dpp/message.h +++ b/include/dpp/message.h @@ -1172,6 +1172,20 @@ struct DPP_EXPORT attachment { * @return true if remixed */ bool is_remix() const; + + /** + * @brief Returns expiration timestamp for attachment's URL + * + * @return timestamp of URL expiration + */ + time_t get_expire_time() const; + + /** + * @brief Returns creation timestamp for attachment's URL + * + * @return timestamp of URL creation + */ + time_t get_issued_time() const; }; /** diff --git a/src/dpp/message.cpp b/src/dpp/message.cpp index 9b304cf094..7054920ce6 100644 --- a/src/dpp/message.cpp +++ b/src/dpp/message.cpp @@ -882,6 +882,30 @@ bool attachment::is_remix() const { return flags & a_is_remix; } +time_t attachment::get_expire_time() const { + std::string attributes = url.substr(url.find('?')+1); + std::vector attr_list = utility::tokenize(attributes,"&"); + auto ex_attr = std::find_if(attr_list.begin(), attr_list.end(),[](const std::string& s){return s.substr(0,3) == "ex=";}); + /*Check if discord sent us the 'ex' attribute*/ + if(attr_list.end() == ex_attr){ + return 0; + } + /*Erase 'ex=' prefix before parsing*/ + return std::stol(ex_attr->substr(3),nullptr,16); +} + +time_t attachment::get_issued_time() const { + std::string attributes = url.substr(url.find('?')+1); + std::vector attr_list = utility::tokenize(attributes,"&"); + auto is_attr = std::find_if(attr_list.begin(), attr_list.end(),[](const std::string& s){return s.substr(0,3) == "is=";}); + /*Check if discord sent us the 'is' attribute*/ + if(attr_list.end() == is_attr){ + return 0; + } + /*Erase 'is=' prefix before parsing*/ + return std::stol(is_attr->substr(3),nullptr,16); +} + json message::to_json(bool with_id, bool is_interaction_response) const { /* This is the basics. once it works, expand on it. */ json j({ From 96b22cdcd64ad74600b02c3f58d428563c168ce0 Mon Sep 17 00:00:00 2001 From: harrrson Date: Thu, 18 Jan 2024 16:51:05 +0100 Subject: [PATCH 2/3] fix: check for eventual absence of attributes --- src/dpp/message.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/dpp/message.cpp b/src/dpp/message.cpp index 7054920ce6..9e70752acf 100644 --- a/src/dpp/message.cpp +++ b/src/dpp/message.cpp @@ -883,10 +883,14 @@ bool attachment::is_remix() const { } time_t attachment::get_expire_time() const { - std::string attributes = url.substr(url.find('?')+1); + size_t attr_position = url.find('?'); + /*If no attributes were sent in url, we do not need to parse more*/ + if(url.npos == attr_position){ + return 0; + } + std::string attributes = url.substr(attr_position+1); std::vector attr_list = utility::tokenize(attributes,"&"); auto ex_attr = std::find_if(attr_list.begin(), attr_list.end(),[](const std::string& s){return s.substr(0,3) == "ex=";}); - /*Check if discord sent us the 'ex' attribute*/ if(attr_list.end() == ex_attr){ return 0; } @@ -895,10 +899,14 @@ time_t attachment::get_expire_time() const { } time_t attachment::get_issued_time() const { - std::string attributes = url.substr(url.find('?')+1); + size_t attr_position = url.find('?'); + /*No attributes were sent in url, so we do not need to parse more*/ + if(url.npos == attr_position){ + return 0; + } + std::string attributes = url.substr(attr_position+1); std::vector attr_list = utility::tokenize(attributes,"&"); auto is_attr = std::find_if(attr_list.begin(), attr_list.end(),[](const std::string& s){return s.substr(0,3) == "is=";}); - /*Check if discord sent us the 'is' attribute*/ if(attr_list.end() == is_attr){ return 0; } From e9522ab3828376b92d64a569dd45cdb01c8bdab2 Mon Sep 17 00:00:00 2001 From: Piotr Chorzelewski <45640997+harrrson@users.noreply.github.com> Date: Thu, 25 Jan 2024 23:59:29 +0100 Subject: [PATCH 3/3] fix: spacing in functions and comments --- src/dpp/message.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/dpp/message.cpp b/src/dpp/message.cpp index 9e70752acf..b7da4fc5af 100644 --- a/src/dpp/message.cpp +++ b/src/dpp/message.cpp @@ -884,34 +884,34 @@ bool attachment::is_remix() const { time_t attachment::get_expire_time() const { size_t attr_position = url.find('?'); - /*If no attributes were sent in url, we do not need to parse more*/ + /* If no attributes were sent in url, we do not need to parse more */ if(url.npos == attr_position){ return 0; } - std::string attributes = url.substr(attr_position+1); - std::vector attr_list = utility::tokenize(attributes,"&"); - auto ex_attr = std::find_if(attr_list.begin(), attr_list.end(),[](const std::string& s){return s.substr(0,3) == "ex=";}); + std::string attributes = url.substr(attr_position + 1); + std::vector attr_list = utility::tokenize(attributes, "&"); + auto ex_attr = std::find_if(attr_list.begin(), attr_list.end(), [](const std::string& s){return s.substr(0, 3) == "ex=";}); if(attr_list.end() == ex_attr){ return 0; } - /*Erase 'ex=' prefix before parsing*/ - return std::stol(ex_attr->substr(3),nullptr,16); + /* Erase 'ex=' prefix before parsing */ + return std::stol(ex_attr->substr(3), nullptr, 16); } time_t attachment::get_issued_time() const { size_t attr_position = url.find('?'); - /*No attributes were sent in url, so we do not need to parse more*/ + /* No attributes were sent in url, so we do not need to parse more */ if(url.npos == attr_position){ return 0; } - std::string attributes = url.substr(attr_position+1); - std::vector attr_list = utility::tokenize(attributes,"&"); - auto is_attr = std::find_if(attr_list.begin(), attr_list.end(),[](const std::string& s){return s.substr(0,3) == "is=";}); + std::string attributes = url.substr(attr_position + 1); + std::vector attr_list = utility::tokenize(attributes, "&"); + auto is_attr = std::find_if(attr_list.begin(), attr_list.end(), [](const std::string& s){return s.substr(0, 3) == "is=";}); if(attr_list.end() == is_attr){ return 0; } - /*Erase 'is=' prefix before parsing*/ - return std::stol(is_attr->substr(3),nullptr,16); + /* Erase 'is=' prefix before parsing */ + return std::stol(is_attr->substr(3), nullptr, 16); } json message::to_json(bool with_id, bool is_interaction_response) const {