summaryrefslogtreecommitdiff
path: root/bot/src/utils
diff options
context:
space:
mode:
authorilotterytea <iltsu@alright.party>2024-05-18 14:48:12 +0500
committerilotterytea <iltsu@alright.party>2024-05-18 14:48:12 +0500
commitd1793df1eda463b10107d41785ad1d7f055ed476 (patch)
treefd3e41c3b4a05924748ae4b762e1ae55a0bc815c /bot/src/utils
parentd7a2de17e9b7931f68b5b4079b1c36866a19d343 (diff)
upd: moved the bot part to a relative subfolder
Diffstat (limited to 'bot/src/utils')
-rw-r--r--bot/src/utils/chrono.cpp48
-rw-r--r--bot/src/utils/chrono.hpp11
-rw-r--r--bot/src/utils/string.cpp66
-rw-r--r--bot/src/utils/string.hpp32
4 files changed, 157 insertions, 0 deletions
diff --git a/bot/src/utils/chrono.cpp b/bot/src/utils/chrono.cpp
new file mode 100644
index 0000000..7a7f2c9
--- /dev/null
+++ b/bot/src/utils/chrono.cpp
@@ -0,0 +1,48 @@
+#include "chrono.hpp"
+
+#include <chrono>
+#include <cmath>
+#include <ctime>
+#include <iomanip>
+#include <sstream>
+#include <string>
+
+namespace bot::utils::chrono {
+ std::string format_timestamp(int seconds) {
+ int d = round(seconds / (60 * 60 * 24));
+ int h = round(seconds / (60 * 60) % 24);
+ int m = round(seconds % (60 * 60) / 60);
+ int s = round(seconds % 60);
+
+ // Only seconds:
+ if (d == 0 && h == 0 && m == 0) {
+ return std::to_string(s) + "s";
+ }
+ // Minutes and seconds:
+ else if (d == 0 && h == 0) {
+ return std::to_string(m) + "m" + std::to_string(s) + "s";
+ }
+ // Hours and minutes:
+ else if (d == 0) {
+ return std::to_string(h) + "h" + std::to_string(m) + "m";
+ }
+ // Days and hours:
+ else {
+ return std::to_string(d) + "d" + std::to_string(h) + "h";
+ }
+ }
+
+ std::chrono::system_clock::time_point string_to_time_point(
+ const std::string &value, const std::string &format) {
+ std::tm tm = {};
+ std::stringstream ss(value);
+
+ ss >> std::get_time(&tm, format.c_str());
+
+ if (ss.fail()) {
+ throw std::invalid_argument("Invalid time format");
+ }
+
+ return std::chrono::system_clock::from_time_t(std::mktime(&tm));
+ }
+}
diff --git a/bot/src/utils/chrono.hpp b/bot/src/utils/chrono.hpp
new file mode 100644
index 0000000..7e85e70
--- /dev/null
+++ b/bot/src/utils/chrono.hpp
@@ -0,0 +1,11 @@
+#pragma once
+
+#include <chrono>
+#include <string>
+
+namespace bot::utils::chrono {
+ std::string format_timestamp(int seconds);
+ std::chrono::system_clock::time_point string_to_time_point(
+ const std::string &value,
+ const std::string &format = "%Y-%m-%d %H:%M:%S");
+}
diff --git a/bot/src/utils/string.cpp b/bot/src/utils/string.cpp
new file mode 100644
index 0000000..71c06bf
--- /dev/null
+++ b/bot/src/utils/string.cpp
@@ -0,0 +1,66 @@
+#include "string.hpp"
+
+#include <sstream>
+#include <string>
+#include <vector>
+
+namespace bot {
+ namespace utils {
+ namespace string {
+ std::vector<std::string> split_text(const std::string &text,
+ char delimiter) {
+ std::vector<std::string> parts;
+
+ std::istringstream iss(text);
+ std::string part;
+
+ while (std::getline(iss, part, delimiter)) {
+ parts.push_back(part);
+ }
+
+ return parts;
+ }
+
+ std::string join_vector(const std::vector<std::string> &vec,
+ char delimiter) {
+ if (vec.empty()) {
+ return "";
+ }
+
+ std::string str;
+
+ for (auto i = vec.begin(); i != vec.end() - 1; i++) {
+ str += *i + delimiter;
+ }
+
+ str += vec[vec.size() - 1];
+
+ return str;
+ }
+
+ std::string join_vector(const std::vector<std::string> &vec) {
+ std::string str;
+
+ for (const auto &e : vec) {
+ str += e;
+ }
+
+ return str;
+ }
+
+ bool string_contains_sql_injection(const std::string &input) {
+ std::string forbidden_strings[] = {";", "--", "'", "\"",
+ "/*", "*/", "xp_", "exec",
+ "sp_", "insert", "select", "delete"};
+
+ for (const auto &str : forbidden_strings) {
+ if (input.find(str) != std::string::npos) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+ }
+ }
+}
diff --git a/bot/src/utils/string.hpp b/bot/src/utils/string.hpp
new file mode 100644
index 0000000..c8385ad
--- /dev/null
+++ b/bot/src/utils/string.hpp
@@ -0,0 +1,32 @@
+#pragma once
+
+#include <sstream>
+#include <string>
+#include <vector>
+
+namespace bot {
+ namespace utils {
+ namespace string {
+ std::vector<std::string> split_text(const std::string &text,
+ char delimiter);
+ std::string join_vector(const std::vector<std::string> &vec,
+ char delimiter);
+ std::string join_vector(const std::vector<std::string> &vec);
+
+ template <typename T>
+ std::string str(T begin, T end, char delimiter) {
+ std::stringstream ss;
+ bool first = true;
+
+ for (; begin != end; begin++) {
+ if (!first) ss << delimiter;
+ ss << *begin;
+ first = false;
+ }
+ return ss.str();
+ }
+
+ bool string_contains_sql_injection(const std::string &input);
+ }
+ }
+}