From a63d97262ba0bb99f590426272543a60a9362079 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Fri, 16 Oct 2015 17:23:45 -0400
Subject: [PATCH] ringdht: bump opendht, msgpack, remember fetched messages

* bump MsgPack to stable release 1.2.0
* bump OpenDHT to latest master supporting MsgPack 1.2.0
* adapt RingAccount to latest OpenDHT API

Issue: #81946
Change-Id: I331626120ca5e1c4c0d6f9a2dcf69e8d2934a636
---
 contrib/src/msgpack/rules.mak |  6 +--
 contrib/src/opendht/rules.mak |  2 +-
 src/ringdht/ringaccount.cpp   | 82 +++++++++++++++++++++++------------
 src/ringdht/ringaccount.h     |  4 ++
 4 files changed, 63 insertions(+), 31 deletions(-)

diff --git a/contrib/src/msgpack/rules.mak b/contrib/src/msgpack/rules.mak
index 2cdcae5de7..fb73f18637 100644
--- a/contrib/src/msgpack/rules.mak
+++ b/contrib/src/msgpack/rules.mak
@@ -1,5 +1,5 @@
 # MSGPACK
-MSGPACK_VERSION := 294aa52c3ad8392ea54331d0ed89299f6a32a798
+MSGPACK_VERSION := cpp-1.2.0
 MSGPACK_URL := https://github.com/msgpack/msgpack-c/archive/$(MSGPACK_VERSION).tar.gz
 
 PKGS += msgpack
@@ -8,8 +8,8 @@ PKGS_FOUND += msgpack
 endif
 
 MSGPACK_CMAKECONF := -DMSGPACK_CXX11=ON \
-                     -DMSGPACK_BUILD_EXAMPLES=OFF \
-					 -DCMAKE_INSTALL_LIBDIR=lib
+		-DMSGPACK_BUILD_EXAMPLES=OFF \
+		-DCMAKE_INSTALL_LIBDIR=lib
 
 $(TARBALLS)/msgpack-c-$(MSGPACK_VERSION).tar.gz:
 	$(call download,$(MSGPACK_URL))
diff --git a/contrib/src/opendht/rules.mak b/contrib/src/opendht/rules.mak
index 2700e19ed1..a57b88d6f9 100644
--- a/contrib/src/opendht/rules.mak
+++ b/contrib/src/opendht/rules.mak
@@ -1,5 +1,5 @@
 # OPENDHT
-OPENDHT_VERSION := fe3ded9d0f833b953d1eaa4dfdb509c51ae09ef6
+OPENDHT_VERSION := 184dc17aa8cf57a9a3e1b26ff2794b2cab4597ba
 OPENDHT_URL := https://github.com/savoirfairelinux/opendht/archive/$(OPENDHT_VERSION).tar.gz
 
 PKGS += opendht
diff --git a/src/ringdht/ringaccount.cpp b/src/ringdht/ringaccount.cpp
index ccd12c9887..cfa403e921 100644
--- a/src/ringdht/ringaccount.cpp
+++ b/src/ringdht/ringaccount.cpp
@@ -716,6 +716,7 @@ RingAccount::doRegister_()
 {
     try {
         loadTreatedCalls();
+        loadTreatedMessages();
         if (dht_.isRunning()) {
             RING_ERR("DHT already running (stopping it first).");
             dht_.join();
@@ -887,8 +888,13 @@ RingAccount::doRegister_()
             inboxKey,
             [shared](dht::ImMessage&& v) {
                 auto& this_ = *shared.get();
+                auto res = this_.treatedMessages_.insert(v.id);
+                this_.saveTreatedMessages();
+                if (!res.second)
+                    return true;
+
                 auto from = v.from.toString();
-                auto msg = v.im_message;
+                auto msg = v.msg;
                 RING_DBG("Text message received from DHT ! %s -> %s",  from.c_str(), msg.c_str());
                 emitSignal<DRing::ConfigurationSignal::IncomingAccountMessage>(this_.getAccountID(), from, msg);
                 return true;
@@ -999,40 +1005,61 @@ RingAccount::getCertificatesByStatus(tls::TrustStore::Status status)
     return trust_.getCertificatesByStatus(status);
 }
 
+std::set<dht::Value::Id>
+loadIdList(const std::string& path)
+{
+    std::set<dht::Value::Id> ids;
+    std::ifstream file(path);
+    if (!file.is_open()) {
+        RING_WARN("Could not load %s", path.c_str());
+        return ids;
+    }
+    std::string line;
+    while (std::getline(file, line)) {
+        std::istringstream iss(line);
+        dht::Value::Id vid;
+        if (!(iss >> std::hex >> vid)) { break; }
+        ids.insert(vid);
+    }
+    return ids;
+}
+
 void
-RingAccount::loadTreatedCalls()
+saveIdList(const std::string& path, const std::set<dht::Value::Id>& ids)
 {
-    std::string treatedcallPath = cachePath_+DIR_SEPARATOR_STR "treatedCalls";
-    {
-        std::ifstream file(treatedcallPath);
-        if (!file.is_open()) {
-            RING_WARN("Could not load treated calls from %s", treatedcallPath.c_str());
-            return;
-        }
-        std::string line;
-        while (std::getline(file, line)) {
-            std::istringstream iss(line);
-            dht::Value::Id vid;
-            if (!(iss >> std::hex >> vid)) { break; }
-            treatedCalls_.insert(vid);
-        }
+    std::ofstream file(path, std::ios::trunc);
+    if (!file.is_open()) {
+        RING_ERR("Could not save to %s", path.c_str());
+        return;
     }
+    for (auto& c : ids)
+        file << std::hex << c << "\n";
+}
+
+void
+RingAccount::loadTreatedCalls()
+{
+    treatedCalls_ = loadIdList(cachePath_+DIR_SEPARATOR_STR "treatedCalls");
 }
 
 void
 RingAccount::saveTreatedCalls() const
 {
     fileutils::check_dir(cachePath_.c_str());
-    std::string treatedcallPath = cachePath_+DIR_SEPARATOR_STR "treatedCalls";
-    {
-        std::ofstream file(treatedcallPath, std::ios::trunc);
-        if (!file.is_open()) {
-            RING_ERR("Could not save treated calls to %s", treatedcallPath.c_str());
-            return;
-        }
-        for (auto& c : treatedCalls_)
-            file << std::hex << c << "\n";
-    }
+    saveIdList(cachePath_+DIR_SEPARATOR_STR "treatedCalls", treatedCalls_);
+}
+
+void
+RingAccount::loadTreatedMessages()
+{
+    treatedMessages_ = loadIdList(cachePath_+DIR_SEPARATOR_STR "treatedMessages");
+}
+
+void
+RingAccount::saveTreatedMessages() const
+{
+    fileutils::check_dir(cachePath_.c_str());
+    saveIdList(cachePath_+DIR_SEPARATOR_STR "treatedMessages", treatedMessages_);
 }
 
 void RingAccount::saveNodes(const std::vector<dht::Dht::NodeExport>& nodes) const
@@ -1283,9 +1310,10 @@ void
 RingAccount::sendTextMessage(const std::string& to, const std::string& message)
 {
     const std::string& toUri = parseRingUri(to);
+    auto now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
     dht_.putEncrypted(dht::InfoHash::get("inbox:"+toUri),
                       dht::InfoHash(toUri),
-                      dht::ImMessage(std::string(message)));
+                      dht::ImMessage(udist(rand_), std::string(message), now));
 }
 
 } // namespace ring
diff --git a/src/ringdht/ringaccount.h b/src/ringdht/ringaccount.h
index 8a9e9efd59..b7764593ac 100644
--- a/src/ringdht/ringaccount.h
+++ b/src/ringdht/ringaccount.h
@@ -315,6 +315,7 @@ class RingAccount : public SIPAccountBase {
          */
         std::list<PendingCall> pendingSipCalls_ {};
         std::set<dht::Value::Id> treatedCalls_ {};
+        std::set<dht::Value::Id> treatedMessages_ {};
         mutable std::mutex callsMutex_ {};
 
         std::string idPath_ {};
@@ -344,6 +345,9 @@ class RingAccount : public SIPAccountBase {
         void loadTreatedCalls();
         void saveTreatedCalls() const;
 
+        void loadTreatedMessages();
+        void saveTreatedMessages() const;
+
         /**
          * If privkeyPath_ is a valid private key file (PEM or DER),
          * and certPath_ a valid certificate file, load and returns them.
-- 
GitLab