From 8ec823be4fc587abb7282a06a12f9df9c37810d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Fri, 12 Aug 2016 16:39:19 +0200 Subject: Save received and sent messages into the database --- src/database/database.cpp | 31 +++++++++++++++++++++++++++++++ src/database/database.hpp | 8 ++++++++ 2 files changed, 39 insertions(+) (limited to 'src/database') diff --git a/src/database/database.cpp b/src/database/database.cpp index 61e1b47..5513946 100644 --- a/src/database/database.cpp +++ b/src/database/database.cpp @@ -3,7 +3,9 @@ #include #include +#include #include +#include using namespace std::string_literals; @@ -79,9 +81,38 @@ db::IrcChannelOptions Database::get_irc_channel_options_with_server_default(cons return coptions; } +void Database::store_muc_message(const std::string& owner, const Iid& iid, + Database::time_point date, + const std::string& body, + const std::string& nick) +{ + db::MucLogLine line(*Database::db); + + line.uuid = Database::gen_uuid(); + line.owner = owner; + line.ircChanName = iid.get_local(); + line.ircServerName = iid.get_server(); + line.date = date.time_since_epoch().count(); + line.body = body; + line.nick = nick; + + line.update(); +} + void Database::close() { Database::db.reset(nullptr); } + +std::string Database::gen_uuid() +{ + char uuid_str[37]; + uuid_t uuid; + uuid_generate(uuid); + uuid_unparse(uuid, uuid_str); + return uuid_str; +} + + #endif diff --git a/src/database/database.hpp b/src/database/database.hpp index 7173bcd..b11332e 100644 --- a/src/database/database.hpp +++ b/src/database/database.hpp @@ -9,10 +9,14 @@ #include #include +#include + +class Iid; class Database { public: + using time_point = std::chrono::system_clock::time_point; Database() = default; ~Database() = default; @@ -41,11 +45,15 @@ public: const std::string& server, const std::string& channel); + static void store_muc_message(const std::string& owner, const Iid& iid, + time_point date, const std::string& body, const std::string& nick); + static void close(); static void open(const std::string& filename, const std::string& db_type="sqlite3"); private: + static std::string gen_uuid(); static std::unique_ptr db; }; #endif /* USE_DATABASE */ -- cgit v1.2.3