From ba879a882e031d7b8503f78fe41d1210000c96ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Fri, 16 Mar 2018 00:53:47 +0100 Subject: Use std::optional instead of OptionalBool --- src/utils/optional_bool.cpp | 4 ++-- src/utils/optional_bool.hpp | 48 +++++++++++++++------------------------------ 2 files changed, 18 insertions(+), 34 deletions(-) (limited to 'src/utils') diff --git a/src/utils/optional_bool.cpp b/src/utils/optional_bool.cpp index 56fdca2..1d1c375 100644 --- a/src/utils/optional_bool.cpp +++ b/src/utils/optional_bool.cpp @@ -1,8 +1,8 @@ #include -std::ostream& operator<<(std::ostream& os, const OptionalBool& o) +std::ostream& operator<<(std::ostream& os, const std::optional& o) { - os << o.to_string(); + os << std::to_string(o); return os; } diff --git a/src/utils/optional_bool.hpp b/src/utils/optional_bool.hpp index 867aca2..c652ed3 100644 --- a/src/utils/optional_bool.hpp +++ b/src/utils/optional_bool.hpp @@ -1,37 +1,21 @@ #pragma once +#include + #include -struct OptionalBool +namespace std { - OptionalBool() = default; - - OptionalBool(bool value): - is_set(true), value(value) {} - - void set_value(bool value) - { - this->is_set = true; - this->value = value; - } - - void unset() - { - this->is_set = false; - } - - std::string to_string() const - { - if (this->is_set == false) - return "unset"; - else if (this->value) - return "true"; - else - return "false"; - } - - bool is_set{false}; - bool value{false}; -}; - -std::ostream& operator<<(std::ostream& os, const OptionalBool& o); +inline +std::string to_string(const std::optional b) +{ + if (!b) + return "unset"; + else if (*b) + return "true"; + else + return "false"; +} +} + +std::ostream& operator<<(std::ostream& os, const std::optional& o); -- cgit v1.2.3 From 03714c6cebf90dc7db8e3997a18cdd19e039c667 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Sat, 17 Mar 2018 17:28:47 +0100 Subject: Revert "Use std::optional instead of OptionalBool" This reverts commit ba879a882e031d7b8503f78fe41d1210000c96ca. --- src/utils/optional_bool.cpp | 4 ++-- src/utils/optional_bool.hpp | 48 ++++++++++++++++++++++++++++++--------------- 2 files changed, 34 insertions(+), 18 deletions(-) (limited to 'src/utils') diff --git a/src/utils/optional_bool.cpp b/src/utils/optional_bool.cpp index 1d1c375..56fdca2 100644 --- a/src/utils/optional_bool.cpp +++ b/src/utils/optional_bool.cpp @@ -1,8 +1,8 @@ #include -std::ostream& operator<<(std::ostream& os, const std::optional& o) +std::ostream& operator<<(std::ostream& os, const OptionalBool& o) { - os << std::to_string(o); + os << o.to_string(); return os; } diff --git a/src/utils/optional_bool.hpp b/src/utils/optional_bool.hpp index c652ed3..867aca2 100644 --- a/src/utils/optional_bool.hpp +++ b/src/utils/optional_bool.hpp @@ -1,21 +1,37 @@ #pragma once -#include - #include -namespace std -{ -inline -std::string to_string(const std::optional b) +struct OptionalBool { - if (!b) - return "unset"; - else if (*b) - return "true"; - else - return "false"; -} -} - -std::ostream& operator<<(std::ostream& os, const std::optional& o); + OptionalBool() = default; + + OptionalBool(bool value): + is_set(true), value(value) {} + + void set_value(bool value) + { + this->is_set = true; + this->value = value; + } + + void unset() + { + this->is_set = false; + } + + std::string to_string() const + { + if (this->is_set == false) + return "unset"; + else if (this->value) + return "true"; + else + return "false"; + } + + bool is_set{false}; + bool value{false}; +}; + +std::ostream& operator<<(std::ostream& os, const OptionalBool& o); -- cgit v1.2.3 From 9500bfd4ccb21b261fd8204180d78553704f7acc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Sun, 18 Mar 2018 19:33:07 +0100 Subject: Reflect message IDs in channel MUCs fix #3283 --- src/utils/uuid.cpp | 14 ++++++++++++++ src/utils/uuid.hpp | 8 ++++++++ 2 files changed, 22 insertions(+) create mode 100644 src/utils/uuid.cpp create mode 100644 src/utils/uuid.hpp (limited to 'src/utils') diff --git a/src/utils/uuid.cpp b/src/utils/uuid.cpp new file mode 100644 index 0000000..23b71fe --- /dev/null +++ b/src/utils/uuid.cpp @@ -0,0 +1,14 @@ +#include +#include + +namespace utils +{ +std::string gen_uuid() +{ + char uuid_str[37]; + uuid_t uuid; + uuid_generate(uuid); + uuid_unparse(uuid, uuid_str); + return uuid_str; +} +} diff --git a/src/utils/uuid.hpp b/src/utils/uuid.hpp new file mode 100644 index 0000000..d550475 --- /dev/null +++ b/src/utils/uuid.hpp @@ -0,0 +1,8 @@ +#pragma once + +#include + +namespace utils +{ +std::string gen_uuid(); +} -- cgit v1.2.3 From 857c7d3972a03cbeebf730d99b924d3710dee6a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Tue, 10 Apr 2018 23:33:59 +0200 Subject: Use a different Date data type MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PLEASE backup your database before testing this commit, and report any migration issue. In postgresql, we use timestamp with timezone. In sqlite3 we use REAL (the date is expressed as julianday) This requires a migration of the muclogline_ table: In postgresql it’s pretty simple, we convert all the integer into timestamps With sqlite3, we actually rename the table, create the new one with the correct type, then copy everything to the new table, with a conversion function for the Date_ column, and then we delete the old table. fix #3343 --- src/utils/datetime.hpp | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/utils/datetime.hpp (limited to 'src/utils') diff --git a/src/utils/datetime.hpp b/src/utils/datetime.hpp new file mode 100644 index 0000000..27511f7 --- /dev/null +++ b/src/utils/datetime.hpp @@ -0,0 +1,68 @@ +#pragma once + +#include +#include + +#include + +class DateTime +{ +public: + enum class Engine { + Postgresql, + Sqlite3, + } engine{Engine::Sqlite3}; + + using time_point = std::chrono::system_clock::time_point; + + DateTime(): + s{}, + t{} + { } + + DateTime(std::time_t t): + t(std::chrono::seconds(t)) + {} + + DateTime(std::string s): + s(std::move(s)) + {} + + DateTime& operator=(const std::string& s) + { + this->s = s; + return *this; + } + + DateTime& operator=(const time_point t) + { + this->t = t; + return *this; + } + + const std::string& to_string() const + { + return this->s; + } + + time_point::duration epoch() const + { + return this->t.time_since_epoch(); + } + + long double julianday() const + { + log_debug("ici?"); + auto res = ((static_cast(this->epoch().count()) / std::chrono::system_clock::period::den) / 86400) + 2440587.5; + return res; + } + +private: + std::string s; + time_point t; +}; + +inline long double to_julianday(std::time_t t) +{ + return static_cast(t) / 86400.0 + 2440587.5; +} -- cgit v1.2.3 From e4f111accb8395dc8caf1dda92b8d903d3d78151 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Fri, 13 Apr 2018 23:37:29 +0200 Subject: Remove two unused (julianday) functions --- src/utils/datetime.hpp | 12 ------------ 1 file changed, 12 deletions(-) (limited to 'src/utils') diff --git a/src/utils/datetime.hpp b/src/utils/datetime.hpp index 27511f7..656b318 100644 --- a/src/utils/datetime.hpp +++ b/src/utils/datetime.hpp @@ -50,19 +50,7 @@ public: return this->t.time_since_epoch(); } - long double julianday() const - { - log_debug("ici?"); - auto res = ((static_cast(this->epoch().count()) / std::chrono::system_clock::period::den) / 86400) + 2440587.5; - return res; - } - private: std::string s; time_point t; }; - -inline long double to_julianday(std::time_t t) -{ - return static_cast(t) / 86400.0 + 2440587.5; -} -- cgit v1.2.3 From 61de6b1dac4ef29627f3bdb9ce11b6c0d06f4a24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Tue, 24 Apr 2018 19:19:06 +0200 Subject: Revert "Use a different Date data type" This reverts commit 857c7d3972a03cbeebf730d99b924d3710dee6a0. --- src/utils/datetime.hpp | 56 -------------------------------------------------- 1 file changed, 56 deletions(-) delete mode 100644 src/utils/datetime.hpp (limited to 'src/utils') diff --git a/src/utils/datetime.hpp b/src/utils/datetime.hpp deleted file mode 100644 index 656b318..0000000 --- a/src/utils/datetime.hpp +++ /dev/null @@ -1,56 +0,0 @@ -#pragma once - -#include -#include - -#include - -class DateTime -{ -public: - enum class Engine { - Postgresql, - Sqlite3, - } engine{Engine::Sqlite3}; - - using time_point = std::chrono::system_clock::time_point; - - DateTime(): - s{}, - t{} - { } - - DateTime(std::time_t t): - t(std::chrono::seconds(t)) - {} - - DateTime(std::string s): - s(std::move(s)) - {} - - DateTime& operator=(const std::string& s) - { - this->s = s; - return *this; - } - - DateTime& operator=(const time_point t) - { - this->t = t; - return *this; - } - - const std::string& to_string() const - { - return this->s; - } - - time_point::duration epoch() const - { - return this->t.time_since_epoch(); - } - -private: - std::string s; - time_point t; -}; -- cgit v1.2.3 From 09b10cc80146c1ac2a0d5c53c6c8469b934189f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Mon, 25 Jun 2018 22:54:32 +0200 Subject: Throttle all commands sent to IRC servers fix #3354 --- src/utils/tokens_bucket.hpp | 58 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/utils/tokens_bucket.hpp (limited to 'src/utils') diff --git a/src/utils/tokens_bucket.hpp b/src/utils/tokens_bucket.hpp new file mode 100644 index 0000000..d44eb06 --- /dev/null +++ b/src/utils/tokens_bucket.hpp @@ -0,0 +1,58 @@ +/** + * Implementation of the token bucket algorithm. + * + * It uses a repetitive TimedEvent, started at construction, to fill the + * bucket. + * + * Every n seconds, it executes the given callback. If the callback + * returns true, we add a token (if the limit is not yet reached). + * + */ + +#pragma once + +#include +#include + +class TokensBucket +{ +public: + TokensBucket(std::size_t max_size, std::chrono::milliseconds fill_duration, std::function callback, std::string name): + limit(max_size), + tokens(limit), + fill_duration(fill_duration), + callback(std::move(callback)) + { + log_debug("creating TokensBucket with max size: ", max_size); + TimedEvent event(std::move(fill_duration), [this]() { this->add_token(); }, std::move(name)); + TimedEventsManager::instance().add_event(std::move(event)); + } + + bool use_token() + { + if (this->tokens > 0) + { + this->tokens--; + return true; + } + else + return false; + } + + void set_limit(std::size_t limit) + { + this->limit = limit; + } + +private: + std::size_t limit; + std::size_t tokens; + std::chrono::milliseconds fill_duration; + std::function callback; + + void add_token() + { + if (this->callback() && this->tokens != limit) + this->tokens++; + } +}; -- cgit v1.2.3 From 89ad479ef40a6a2363ea6aa80861f91cc2eddcd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Sat, 4 Aug 2018 16:04:33 +0200 Subject: Remove a useless fill_duration member --- src/utils/tokens_bucket.hpp | 2 -- 1 file changed, 2 deletions(-) (limited to 'src/utils') diff --git a/src/utils/tokens_bucket.hpp b/src/utils/tokens_bucket.hpp index d44eb06..03af015 100644 --- a/src/utils/tokens_bucket.hpp +++ b/src/utils/tokens_bucket.hpp @@ -20,7 +20,6 @@ public: TokensBucket(std::size_t max_size, std::chrono::milliseconds fill_duration, std::function callback, std::string name): limit(max_size), tokens(limit), - fill_duration(fill_duration), callback(std::move(callback)) { log_debug("creating TokensBucket with max size: ", max_size); @@ -47,7 +46,6 @@ public: private: std::size_t limit; std::size_t tokens; - std::chrono::milliseconds fill_duration; std::function callback; void add_token() -- cgit v1.2.3 From d7bc737751aae7bdc77c68e4d2953aa0d6670724 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Tue, 21 Aug 2018 19:54:47 +0200 Subject: Add two missing ref --- src/utils/dirname.cpp | 2 +- src/utils/dirname.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src/utils') diff --git a/src/utils/dirname.cpp b/src/utils/dirname.cpp index 71c9c38..a304117 100644 --- a/src/utils/dirname.cpp +++ b/src/utils/dirname.cpp @@ -2,7 +2,7 @@ namespace utils { - std::string dirname(const std::string filename) + std::string dirname(const std::string& filename) { if (filename.empty()) return "./"; diff --git a/src/utils/dirname.hpp b/src/utils/dirname.hpp index c1df81b..73e1b57 100644 --- a/src/utils/dirname.hpp +++ b/src/utils/dirname.hpp @@ -2,5 +2,5 @@ namespace utils { -std::string dirname(const std::string filename); +std::string dirname(const std::string& filename); } -- cgit v1.2.3 From 0b51e3828116f6847865fae93893eb97a10d1cc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Thu, 23 Aug 2018 20:30:11 +0200 Subject: MaxHistoryLength now has some sensible default value if the user set a negative one --- src/utils/optional_bool.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/utils') diff --git a/src/utils/optional_bool.hpp b/src/utils/optional_bool.hpp index 867aca2..3d00d23 100644 --- a/src/utils/optional_bool.hpp +++ b/src/utils/optional_bool.hpp @@ -6,7 +6,7 @@ struct OptionalBool { OptionalBool() = default; - OptionalBool(bool value): + explicit OptionalBool(bool value): is_set(true), value(value) {} void set_value(bool value) -- cgit v1.2.3 From b1564e4ddc3e54ad78788a6f5643056d03a41678 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Thu, 23 Aug 2018 20:31:31 +0200 Subject: Fix a bunch of int to unsigned int conversion warnings --- src/utils/encoding.cpp | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'src/utils') diff --git a/src/utils/encoding.cpp b/src/utils/encoding.cpp index cff0039..8532292 100644 --- a/src/utils/encoding.cpp +++ b/src/utils/encoding.cpp @@ -48,16 +48,16 @@ namespace utils if (codepoint_size == 4) { if (!str[1] || !str[2] || !str[3] - || ((str[1] & 0b11000000) != 0b10000000) - || ((str[2] & 0b11000000) != 0b10000000) - || ((str[3] & 0b11000000) != 0b10000000)) + || ((str[1] & 0b11000000u) != 0b10000000u) + || ((str[2] & 0b11000000u) != 0b10000000u) + || ((str[3] & 0b11000000u) != 0b10000000u)) return false; } else if (codepoint_size == 3) { if (!str[1] || !str[2] - || ((str[1] & 0b11000000) != 0b10000000) - || ((str[2] & 0b11000000) != 0b10000000)) + || ((str[1] & 0b11000000u) != 0b10000000u) + || ((str[2] & 0b11000000u) != 0b10000000u)) return false; } else if (codepoint_size == 2) @@ -81,7 +81,7 @@ namespace utils // pointer where we write valid chars char* r = res.data(); - const char* str = original.c_str(); + const unsigned char* str = reinterpret_cast(original.c_str()); std::bitset<20> codepoint; while (*str) @@ -89,10 +89,10 @@ namespace utils // 4 bytes: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx if ((str[0] & 0b11111000) == 0b11110000) { - codepoint = ((str[0] & 0b00000111) << 18); - codepoint |= ((str[1] & 0b00111111) << 12); - codepoint |= ((str[2] & 0b00111111) << 6 ); - codepoint |= ((str[3] & 0b00111111) << 0 ); + codepoint = ((str[0] & 0b00000111u) << 18u); + codepoint |= ((str[1] & 0b00111111u) << 12u); + codepoint |= ((str[2] & 0b00111111u) << 6u ); + codepoint |= ((str[3] & 0b00111111u) << 0u ); if (codepoint.to_ulong() <= 0x10FFFF) { ::memcpy(r, str, 4); @@ -103,9 +103,9 @@ namespace utils // 3 bytes: 1110xxx 10xxxxxx 10xxxxxx else if ((str[0] & 0b11110000) == 0b11100000) { - codepoint = ((str[0] & 0b00001111) << 12); - codepoint |= ((str[1] & 0b00111111) << 6); - codepoint |= ((str[2] & 0b00111111) << 0 ); + codepoint = ((str[0] & 0b00001111u) << 12u); + codepoint |= ((str[1] & 0b00111111u) << 6u); + codepoint |= ((str[2] & 0b00111111u) << 0u ); if (codepoint.to_ulong() <= 0xD7FF || (codepoint.to_ulong() >= 0xE000 && codepoint.to_ulong() <= 0xFFFD)) { -- cgit v1.2.3 From 8997021b2e43f61b6120ecce80b8097c3c451a48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Thu, 23 Aug 2018 21:28:06 +0200 Subject: Fix two more warnings --- src/utils/string.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/utils') diff --git a/src/utils/string.cpp b/src/utils/string.cpp index 635e71a..366ec1f 100644 --- a/src/utils/string.cpp +++ b/src/utils/string.cpp @@ -15,11 +15,11 @@ std::vector cut(const std::string& val, const std::size_t size) // Get the number of chars, <= size, that contain only whole // UTF-8 codepoints. std::size_t s = 0; - auto codepoint_size = utils::get_next_codepoint_size(val[pos + s]); + auto codepoint_size = utils::get_next_codepoint_size(static_cast(val[pos + s])); while (s + codepoint_size <= size && pos + s < val.size()) { s += codepoint_size; - codepoint_size = utils::get_next_codepoint_size(val[pos + s]); + codepoint_size = utils::get_next_codepoint_size(static_cast(val[pos + s])); } res.emplace_back(val.substr(pos, s)); pos += s; -- cgit v1.2.3 From 7d0df9b6ddee8db69ea0a511f031f32a4537a749 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Thu, 23 Aug 2018 15:21:12 +0200 Subject: Disable the throttle limit if negative Also, invalid values result in -1 being set --- src/utils/tokens_bucket.hpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'src/utils') diff --git a/src/utils/tokens_bucket.hpp b/src/utils/tokens_bucket.hpp index 03af015..2992e21 100644 --- a/src/utils/tokens_bucket.hpp +++ b/src/utils/tokens_bucket.hpp @@ -17,7 +17,7 @@ class TokensBucket { public: - TokensBucket(std::size_t max_size, std::chrono::milliseconds fill_duration, std::function callback, std::string name): + TokensBucket(long int max_size, std::chrono::milliseconds fill_duration, std::function callback, std::string name): limit(max_size), tokens(limit), callback(std::move(callback)) @@ -29,6 +29,8 @@ public: bool use_token() { + if (this->limit < 0) + return true; if (this->tokens > 0) { this->tokens--; @@ -38,19 +40,21 @@ public: return false; } - void set_limit(std::size_t limit) + void set_limit(long int limit) { this->limit = limit; } private: - std::size_t limit; + long int limit; std::size_t tokens; std::function callback; void add_token() { - if (this->callback() && this->tokens != limit) + if (this->limit < 0) + return; + if (this->callback() && this->tokens != static_casttokens)>(this->limit)) this->tokens++; } }; -- cgit v1.2.3 From a3e865ad63a1c0d634001d9d2e86c425bc5094e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Thu, 23 Aug 2018 23:58:32 +0200 Subject: Fix a signed/unsigned mismatch --- src/utils/tokens_bucket.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/utils') diff --git a/src/utils/tokens_bucket.hpp b/src/utils/tokens_bucket.hpp index 2992e21..263359a 100644 --- a/src/utils/tokens_bucket.hpp +++ b/src/utils/tokens_bucket.hpp @@ -19,7 +19,7 @@ class TokensBucket public: TokensBucket(long int max_size, std::chrono::milliseconds fill_duration, std::function callback, std::string name): limit(max_size), - tokens(limit), + tokens(static_cast(limit)), callback(std::move(callback)) { log_debug("creating TokensBucket with max size: ", max_size); -- cgit v1.2.3