diff options
| -rw-r--r-- | src/config.hpp | 5 | ||||
| -rw-r--r-- | src/main.cpp | 5 | ||||
| -rw-r--r-- | src/timer.cpp | 70 | ||||
| -rw-r--r-- | src/timer.hpp | 9 |
4 files changed, 89 insertions, 0 deletions
diff --git a/src/config.hpp b/src/config.hpp index 25519c3..323e18f 100644 --- a/src/config.hpp +++ b/src/config.hpp @@ -8,6 +8,11 @@ " password = " + c.database.password + " host = " + c.database.host + \ " port = " + c.database.port +#define GET_DATABASE_CONNECTION_URL_POINTER(c) \ + "dbname = " + c->database.name + " user = " + c->database.user + \ + " password = " + c->database.password + " host = " + c->database.host + \ + " port = " + c->database.port + namespace bot { struct DatabaseConfiguration { std::string name; diff --git a/src/main.cpp b/src/main.cpp index 700c01d..a582df4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -13,6 +13,7 @@ #include "irc/message.hpp" #include "localization/localization.hpp" #include "stream.hpp" +#include "timer.hpp" int main(int argc, char *argv[]) { std::cout << "hi world\n"; @@ -110,6 +111,10 @@ int main(int argc, char *argv[]) { }); client.run(); + + std::thread timer_thread(bot::create_timer_thread, &client, &cfg); + timer_thread.join(); + stream_listener_client.run_thread(); return 0; diff --git a/src/timer.cpp b/src/timer.cpp new file mode 100644 index 0000000..ccdf176 --- /dev/null +++ b/src/timer.cpp @@ -0,0 +1,70 @@ +#include "timer.hpp" + +#include <chrono> +#include <pqxx/pqxx> +#include <string> +#include <thread> + +#include "config.hpp" +#include "irc/client.hpp" +#include "utils/chrono.hpp" + +namespace bot { + void create_timer_thread(irc::Client *irc_client, + Configuration *configuration) { + while (true) { + pqxx::connection conn(GET_DATABASE_CONNECTION_URL_POINTER(configuration)); + pqxx::work *work = new pqxx::work(conn); + + pqxx::result timers = work->exec( + "SELECT id, interval_sec, message, channel_id, last_executed_at FROM " + "timers"); + + for (const auto &timer : timers) { + int id = timer[0].as<int>(); + int interval_sec = timer[1].as<int>(); + std::string message = timer[2].as<std::string>(); + int channel_id = timer[3].as<int>(); + + // it could be done in sql query + std::chrono::system_clock::time_point last_executed_at = + utils::chrono::string_to_time_point(timer[4].as<std::string>()); + auto now = std::chrono::system_clock::now(); + auto now_time_it = std::chrono::system_clock::to_time_t(now); + auto now_tm = std::gmtime(&now_time_it); + now = std::chrono::system_clock::from_time_t(std::mktime(now_tm)); + + auto difference = std::chrono::duration_cast<std::chrono::seconds>( + now - last_executed_at); + + if (difference.count() > interval_sec) { + pqxx::result channels = + work->exec("SELECT alias_name FROM channels WHERE id = " + + std::to_string(channel_id)); + + if (!channels.empty()) { + std::string alias_name = channels[0][0].as<std::string>(); + + irc_client->say(alias_name, message); + } + + work->exec( + "UPDATE timers SET last_executed_at = timezone('utc', now()) " + "WHERE " + "id = " + + std::to_string(id)); + + work->commit(); + + delete work; + work = new pqxx::work(conn); + } + } + + delete work; + conn.close(); + + std::this_thread::sleep_for(std::chrono::seconds(1)); + } + } +} diff --git a/src/timer.hpp b/src/timer.hpp new file mode 100644 index 0000000..40a52ee --- /dev/null +++ b/src/timer.hpp @@ -0,0 +1,9 @@ +#pragma once + +#include "config.hpp" +#include "irc/client.hpp" + +namespace bot { + void create_timer_thread(irc::Client *irc_client, + Configuration *configuration); +} |
