summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorlouiz’ <louiz@louiz.org>2016-08-12 16:39:19 +0200
committerlouiz’ <louiz@louiz.org>2016-08-12 16:39:19 +0200
commit8ec823be4fc587abb7282a06a12f9df9c37810d2 (patch)
tree7ce77108ac2e6da63ee89a4f5fb6dbc97fe8a67f /src
parent7ffe278e0556d222291840dc91c785ccf824fee5 (diff)
downloadbiboumi-8ec823be4fc587abb7282a06a12f9df9c37810d2.tar.gz
biboumi-8ec823be4fc587abb7282a06a12f9df9c37810d2.tar.bz2
biboumi-8ec823be4fc587abb7282a06a12f9df9c37810d2.tar.xz
biboumi-8ec823be4fc587abb7282a06a12f9df9c37810d2.zip
Save received and sent messages into the database
Diffstat (limited to 'src')
-rw-r--r--src/bridge/bridge.cpp10
-rw-r--r--src/database/database.cpp31
-rw-r--r--src/database/database.hpp8
3 files changed, 49 insertions, 0 deletions
diff --git a/src/bridge/bridge.cpp b/src/bridge/bridge.cpp
index ac61dbc..1812611 100644
--- a/src/bridge/bridge.cpp
+++ b/src/bridge/bridge.cpp
@@ -224,6 +224,11 @@ void Bridge::send_channel_message(const Iid& iid, const std::string& body)
irc->send_channel_message(iid.get_local(), action_prefix + line.substr(4) + "\01");
else
irc->send_channel_message(iid.get_local(), line);
+
+ const auto xmpp_body = this->make_xmpp_body(line);
+ Database::store_muc_message(this->get_bare_jid(), iid, std::chrono::system_clock::now(),
+ std::get<0>(xmpp_body), irc->get_own_nick());
+
for (const auto& resource: this->resources_in_chan[iid.to_tuple()])
this->xmpp.send_muc_message(std::to_string(iid), irc->get_own_nick(),
this->make_xmpp_body(line), this->user_jid + "/" + resource);
@@ -578,10 +583,15 @@ void Bridge::send_message(const Iid& iid, const std::string& nick, const std::st
const auto encoding = in_encoding_for(*this, iid);
if (muc)
{
+ const auto xmpp_body = this->make_xmpp_body(body, encoding);
+ Database::store_muc_message(this->get_bare_jid(), iid, std::chrono::system_clock::now(),
+ std::get<0>(xmpp_body), nick);
+
for (const auto& resource: this->resources_in_chan[iid.to_tuple()])
{
this->xmpp.send_muc_message(std::to_string(iid), nick,
this->make_xmpp_body(body, encoding), this->user_jid + "/" + resource);
+
}
}
else
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 <database/database.hpp>
#include <logger/logger.hpp>
+#include <irc/iid.hpp>
#include <string>
+#include <uuid.h>
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 <memory>
#include <litesql.hpp>
+#include <chrono>
+
+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::BibouDB> db;
};
#endif /* USE_DATABASE */