From a646cde336fc272198874838def96e541735bcb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com> Date: Thu, 15 Oct 2015 17:47:44 -0400 Subject: [PATCH] ring: parse ring:// uri for calls and text messages Tuleap: #3 Change-Id: If43ac48729f4f55ba7fa02faa548b69d7f41dc78 --- src/ringdht/ringaccount.cpp | 42 +++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/src/ringdht/ringaccount.cpp b/src/ringdht/ringaccount.cpp index c031f24d5c..ccd12c9887 100644 --- a/src/ringdht/ringaccount.cpp +++ b/src/ringdht/ringaccount.cpp @@ -83,6 +83,28 @@ constexpr const char * const RingAccount::ACCOUNT_TYPE; static std::uniform_int_distribution<dht::Value::Id> udist; +static const std::string +parseRingUri(const std::string& toUrl) +{ + auto dhtf = toUrl.find("ring:"); + if (dhtf != std::string::npos) { + dhtf = dhtf+5; + } else { + dhtf = toUrl.find("sips:"); + dhtf = (dhtf == std::string::npos) ? 0 : dhtf+5; + } + while (dhtf < toUrl.length() && toUrl[dhtf] == '/') + dhtf++; + + if (toUrl.length() - dhtf < 40) + throw std::invalid_argument("id must be a ring infohash"); + + const std::string toUri = toUrl.substr(dhtf, 40); + if (std::find_if_not(toUri.cbegin(), toUri.cend(), ::isxdigit) != toUri.cend()) + throw std::invalid_argument("id must be a ring infohash"); + return toUri; +} + /** * Local ICE Transport factory helper * @@ -152,20 +174,7 @@ template <> std::shared_ptr<SIPCall> RingAccount::newOutgoingCall(const std::string& toUrl) { - auto dhtf = toUrl.find(RING_URI_PREFIX); - if (dhtf != std::string::npos) { - dhtf = dhtf+5; - } else { - dhtf = toUrl.find("sips:"); - dhtf = (dhtf == std::string::npos) ? 0 : dhtf+5; - } - if (toUrl.length() - dhtf < 40) - throw std::invalid_argument("id must be a ring infohash"); - - const std::string toUri = toUrl.substr(dhtf, 40); - if (std::find_if_not(toUri.cbegin(), toUri.cend(), ::isxdigit) != toUri.cend()) - throw std::invalid_argument("id must be a ring infohash"); - + const std::string toUri = parseRingUri(toUrl); RING_DBG("Calling DHT peer %s", toUri.c_str()); auto& manager = Manager::instance(); @@ -1273,8 +1282,9 @@ RingAccount::connectivityChanged() void RingAccount::sendTextMessage(const std::string& to, const std::string& message) { - dht_.putEncrypted(dht::InfoHash::get("inbox:"+to), - dht::InfoHash(to), + const std::string& toUri = parseRingUri(to); + dht_.putEncrypted(dht::InfoHash::get("inbox:"+toUri), + dht::InfoHash(toUri), dht::ImMessage(std::string(message))); } -- GitLab