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 From 992fa938951558f4515145c9b82af0123c979a29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Sun, 21 Aug 2016 01:04:32 +0200 Subject: Add get_first_non_empty and use it into Database to simplify a little bit --- src/database/database.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'src/database') diff --git a/src/database/database.cpp b/src/database/database.cpp index 5513946..3891f41 100644 --- a/src/database/database.cpp +++ b/src/database/database.cpp @@ -4,8 +4,8 @@ #include #include #include -#include #include +#include using namespace std::string_literals; @@ -73,10 +73,11 @@ db::IrcChannelOptions Database::get_irc_channel_options_with_server_default(cons { auto coptions = Database::get_irc_channel_options(owner, server, channel); auto soptions = Database::get_irc_server_options(owner, server); - if (coptions.encodingIn.value().empty()) - coptions.encodingIn = soptions.encodingIn; - if (coptions.encodingOut.value().empty()) - coptions.encodingOut = soptions.encodingOut; + + coptions.encodingIn = get_first_non_empty(coptions.encodingIn.value(), + soptions.encodingIn.value()); + coptions.encodingOut = get_first_non_empty(coptions.encodingOut.value(), + soptions.encodingOut.value()); return coptions; } -- cgit v1.2.3 From 6776b827d243ec0e018eac8233c5df402030640e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Mon, 22 Aug 2016 00:38:44 +0200 Subject: Add a global configure ad-hoc command, with max history length --- src/database/database.cpp | 36 ++++++++++++++++++++++++++++++++++++ src/database/database.hpp | 5 ++++- 2 files changed, 40 insertions(+), 1 deletion(-) (limited to 'src/database') diff --git a/src/database/database.cpp b/src/database/database.cpp index 3891f41..acf57d1 100644 --- a/src/database/database.cpp +++ b/src/database/database.cpp @@ -31,6 +31,19 @@ void Database::set_verbose(const bool val) Database::db->verbose = val; } +db::GlobalOptions Database::get_global_options(const std::string& owner) +{ + try { + auto options = litesql::select(*Database::db, + db::GlobalOptions::Owner == owner).one(); + return options; + } catch (const litesql::NotFound& e) { + db::GlobalOptions options(*Database::db); + options.owner = owner; + return options; + } +} + db::IrcServerOptions Database::get_irc_server_options(const std::string& owner, const std::string& server) { @@ -79,6 +92,29 @@ db::IrcChannelOptions Database::get_irc_channel_options_with_server_default(cons coptions.encodingOut = get_first_non_empty(coptions.encodingOut.value(), soptions.encodingOut.value()); + coptions.maxHistoryLength = get_first_non_empty(coptions.maxHistoryLength.value(), + soptions.maxHistoryLength.value()); + + return coptions; +} + +db::IrcChannelOptions Database::get_irc_channel_options_with_server_and_global_default(const std::string& owner, + const std::string& server, + const std::string& channel) +{ + auto coptions = Database::get_irc_channel_options(owner, server, channel); + auto soptions = Database::get_irc_server_options(owner, server); + auto goptions = Database::get_global_options(owner); + + coptions.encodingIn = get_first_non_empty(coptions.encodingIn.value(), + soptions.encodingIn.value()); + coptions.encodingOut = get_first_non_empty(coptions.encodingOut.value(), + soptions.encodingOut.value()); + + coptions.maxHistoryLength = get_first_non_empty(coptions.maxHistoryLength.value(), + soptions.maxHistoryLength.value(), + goptions.maxHistoryLength.value()); + return coptions; } diff --git a/src/database/database.hpp b/src/database/database.hpp index b11332e..d1be2fd 100644 --- a/src/database/database.hpp +++ b/src/database/database.hpp @@ -36,6 +36,7 @@ public: * Return the object from the db. Create it beforehand (with all default * values) if it is not already present. */ + static db::GlobalOptions get_global_options(const std::string& owner); static db::IrcServerOptions get_irc_server_options(const std::string& owner, const std::string& server); static db::IrcChannelOptions get_irc_channel_options(const std::string& owner, @@ -44,7 +45,9 @@ public: static db::IrcChannelOptions get_irc_channel_options_with_server_default(const std::string& owner, const std::string& server, const std::string& channel); - + static db::IrcChannelOptions get_irc_channel_options_with_server_and_global_default(const std::string& owner, + 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); -- cgit v1.2.3 From 593b3268273cac2fa58257ee8e51ce1a6de30872 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Mon, 22 Aug 2016 00:39:11 +0200 Subject: When saving the logs, the date must be in seconds, not nanoseconds --- src/database/database.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/database') diff --git a/src/database/database.cpp b/src/database/database.cpp index acf57d1..fce0f45 100644 --- a/src/database/database.cpp +++ b/src/database/database.cpp @@ -129,7 +129,7 @@ void Database::store_muc_message(const std::string& owner, const Iid& iid, line.owner = owner; line.ircChanName = iid.get_local(); line.ircServerName = iid.get_server(); - line.date = date.time_since_epoch().count(); + line.date = date.time_since_epoch().count() / 1'000'000'000; line.body = body; line.nick = nick; -- cgit v1.2.3 From d1626c929f1d313c2f0f85b7d8b756a8d488d1dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Mon, 22 Aug 2016 00:44:17 +0200 Subject: When joining a channel, send the most recent history found in the database --- src/database/database.cpp | 9 +++++++++ src/database/database.hpp | 2 ++ 2 files changed, 11 insertions(+) (limited to 'src/database') diff --git a/src/database/database.cpp b/src/database/database.cpp index fce0f45..48cdea8 100644 --- a/src/database/database.cpp +++ b/src/database/database.cpp @@ -136,6 +136,15 @@ void Database::store_muc_message(const std::string& owner, const Iid& iid, line.update(); } +std::vector Database::get_muc_logs(const std::string& owner, const std::string& chan_name, const std::string& server, const int limit) +{ + auto res = litesql::select(*Database::db, + db::MucLogLine::Owner == owner && + db::MucLogLine::IrcChanName == chan_name && + db::MucLogLine::IrcServerName == server).orderBy(db::MucLogLine::Date, false).limit(limit).all(); + return {res.rbegin(), res.rend()}; +} + void Database::close() { Database::db.reset(nullptr); diff --git a/src/database/database.hpp b/src/database/database.hpp index d1be2fd..14012ff 100644 --- a/src/database/database.hpp +++ b/src/database/database.hpp @@ -48,6 +48,8 @@ public: static db::IrcChannelOptions get_irc_channel_options_with_server_and_global_default(const std::string& owner, const std::string& server, const std::string& channel); + static std::vector get_muc_logs(const std::string& owner, const std::string& chan_name, const std::string& server, + const int limit); static void store_muc_message(const std::string& owner, const Iid& iid, time_point date, const std::string& body, const std::string& nick); -- cgit v1.2.3 From 88499d037962b4a0bd85d812cb77ef0452c985f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Tue, 23 Aug 2016 00:28:12 +0200 Subject: If the maxHistoryLength is negative, make it 0 Instead of doing a broken SQL request that returns weird lines --- src/database/database.cpp | 4 +++- src/database/database.hpp | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) (limited to 'src/database') diff --git a/src/database/database.cpp b/src/database/database.cpp index 48cdea8..009ff0e 100644 --- a/src/database/database.cpp +++ b/src/database/database.cpp @@ -136,8 +136,10 @@ void Database::store_muc_message(const std::string& owner, const Iid& iid, line.update(); } -std::vector Database::get_muc_logs(const std::string& owner, const std::string& chan_name, const std::string& server, const int limit) +std::vector Database::get_muc_logs(const std::string& owner, const std::string& chan_name, const std::string& server, int limit) { + if (limit < 0) + limit = 0; auto res = litesql::select(*Database::db, db::MucLogLine::Owner == owner && db::MucLogLine::IrcChanName == chan_name && diff --git a/src/database/database.hpp b/src/database/database.hpp index 14012ff..e7fdd5f 100644 --- a/src/database/database.hpp +++ b/src/database/database.hpp @@ -49,7 +49,7 @@ public: const std::string& server, const std::string& channel); static std::vector get_muc_logs(const std::string& owner, const std::string& chan_name, const std::string& server, - const int limit); + int limit); static void store_muc_message(const std::string& owner, const Iid& iid, time_point date, const std::string& body, const std::string& nick); -- cgit v1.2.3 From 7536a1b3f38fbf093c1629b0db209754ada0c906 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Thu, 25 Aug 2016 19:43:51 +0200 Subject: Respond to MAM requests on a channel JID At the moment, result-set-management is not implemented, the whole history (well, at most 1024 messages) is returned. --- src/database/database.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/database') diff --git a/src/database/database.cpp b/src/database/database.cpp index 009ff0e..be0da8e 100644 --- a/src/database/database.cpp +++ b/src/database/database.cpp @@ -138,13 +138,13 @@ void Database::store_muc_message(const std::string& owner, const Iid& iid, std::vector Database::get_muc_logs(const std::string& owner, const std::string& chan_name, const std::string& server, int limit) { - if (limit < 0) - limit = 0; - auto res = litesql::select(*Database::db, + if (limit == -1) + limit = 1024; + const auto& res = litesql::select(*Database::db, db::MucLogLine::Owner == owner && db::MucLogLine::IrcChanName == chan_name && - db::MucLogLine::IrcServerName == server).orderBy(db::MucLogLine::Date, false).limit(limit).all(); - return {res.rbegin(), res.rend()}; + db::MucLogLine::IrcServerName == server).orderBy(db::MucLogLine::Id, false).limit(limit).all(); + return {res.crbegin(), res.crend()}; } void Database::close() -- cgit v1.2.3 From 3047bd41b212390da8e3a4dbcf351e79879042dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Sun, 4 Sep 2016 21:04:21 +0200 Subject: MAM results can be filtered by start and end dates --- src/database/database.cpp | 32 ++++++++++++++++++++++++-------- src/database/database.hpp | 2 +- 2 files changed, 25 insertions(+), 9 deletions(-) (limited to 'src/database') diff --git a/src/database/database.cpp b/src/database/database.cpp index be0da8e..e995d95 100644 --- a/src/database/database.cpp +++ b/src/database/database.cpp @@ -6,6 +6,7 @@ #include #include #include +#include using namespace std::string_literals; @@ -136,14 +137,30 @@ void Database::store_muc_message(const std::string& owner, const Iid& iid, line.update(); } -std::vector Database::get_muc_logs(const std::string& owner, const std::string& chan_name, const std::string& server, int limit) +std::vector Database::get_muc_logs(const std::string& owner, const std::string& chan_name, const std::string& server, + int limit, const std::string& start, const std::string& end) { - if (limit == -1) - limit = 1024; - const auto& res = litesql::select(*Database::db, - db::MucLogLine::Owner == owner && - db::MucLogLine::IrcChanName == chan_name && - db::MucLogLine::IrcServerName == server).orderBy(db::MucLogLine::Id, false).limit(limit).all(); + auto request = litesql::select(*Database::db, + db::MucLogLine::Owner == owner && + db::MucLogLine::IrcChanName == chan_name && + db::MucLogLine::IrcServerName == server); + request.orderBy(db::MucLogLine::Id, false); + + if (limit >= 0) + request.limit(limit); + if (!start.empty()) + { + const auto start_time = utils::parse_datetime(start); + if (start_time != -1) + request.where(db::MucLogLine::Date >= start_time); + } + if (!end.empty()) + { + const auto end_time = utils::parse_datetime(end); + if (end_time != -1) + request.where(db::MucLogLine::Date <= end_time); + } + const auto& res = request.all(); return {res.crbegin(), res.crend()}; } @@ -152,7 +169,6 @@ void Database::close() Database::db.reset(nullptr); } - std::string Database::gen_uuid() { char uuid_str[37]; diff --git a/src/database/database.hpp b/src/database/database.hpp index e7fdd5f..6823574 100644 --- a/src/database/database.hpp +++ b/src/database/database.hpp @@ -49,7 +49,7 @@ public: const std::string& server, const std::string& channel); static std::vector get_muc_logs(const std::string& owner, const std::string& chan_name, const std::string& server, - int limit); + int limit=-1, const std::string& before="", const std::string& after=""); static void store_muc_message(const std::string& owner, const Iid& iid, time_point date, const std::string& body, const std::string& nick); -- cgit v1.2.3 From 363a0bf02cf20592b2f9f034de1c5f54c8922b82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Thu, 29 Sep 2016 20:11:35 +0200 Subject: Look for uuid/uuid.h instead of just uuid.h Avoids a conflict between /usr/include/uuid.h and /usr/local/include/uuid/uuid.h on freebsd --- src/database/database.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/database') diff --git a/src/database/database.cpp b/src/database/database.cpp index e995d95..f7d309b 100644 --- a/src/database/database.cpp +++ b/src/database/database.cpp @@ -4,7 +4,7 @@ #include #include #include -#include +#include #include #include -- cgit v1.2.3