From 43e2239fbdaea68957e1b1a1c6d1964184767cbb Mon Sep 17 00:00:00 2001
From: Guillaume Roguez <guillaume.roguez@savoirfairelinux.com>
Date: Mon, 4 Apr 2016 15:19:11 -0400
Subject: [PATCH] manager: try to find a valid IP2IP account when IP is called

This patch fixes broken Manager::newOutgoingCall() when an IP
url is given.
Now, if the given prefered account is not a valid IP2IP like account,
ie doesn't have an empty hostname, we try to find one in all SIPAccount.
If nothing is found the method returns nullptr.

Change-Id: I60eaf99a13c8f25df8decd87286d857f75391231
Tuleap: #448
---
 src/manager.cpp | 42 ++++++++++++++++++++++--------------------
 1 file changed, 22 insertions(+), 20 deletions(-)

diff --git a/src/manager.cpp b/src/manager.cpp
index e86b664881..702f7630b5 100644
--- a/src/manager.cpp
+++ b/src/manager.cpp
@@ -51,6 +51,7 @@ using random_device = std::random_device;
 
 #include "sip/sip_utils.h"
 #include "sip/sipvoiplink.h"
+#include "sip/sipaccount.h"
 
 #include "im/instant_messaging.h"
 
@@ -2742,42 +2743,43 @@ Manager::getAudioDriver()
 
 std::shared_ptr<Call>
 Manager::newOutgoingCall(const std::string& toUrl,
-                             const std::string& preferredAccountId)
+                         const std::string& preferredAccountId)
 {
-    std::shared_ptr<Account> account {};
     auto preferred = getAccount(preferredAccountId);
-    std::string finalToUrl = toUrl;
 
 #if HAVE_DHT
     if (toUrl.find("ring:") != std::string::npos) {
         if (preferred && preferred->getAccountType() == RingAccount::ACCOUNT_TYPE)
-            return preferred->newOutgoingCall(finalToUrl);
+            return preferred->newOutgoingCall(toUrl);
         auto dhtAcc = getAllAccounts<RingAccount>();
         for (const auto& acc : dhtAcc)
             if (acc->isEnabled())
-                return acc->newOutgoingCall(finalToUrl);
+                return acc->newOutgoingCall(toUrl);
     }
 #endif
+    // If peer url is an IP, and the preferred account is not an "IP2IP like",
+    // we try to find a suitable one in all SIPAccount's.
+    auto strippedToUrl = toUrl;
+    sip_utils::stripSipUriPrefix(strippedToUrl); // FIXME: have a generic version to remove sip dependency
+    if (IpAddr::isValid(strippedToUrl) and !preferred->isIP2IP()) {
+        std::shared_ptr<Account> ip2ip_acc;
+        for (const auto& acc : getAllAccounts<SIPAccount>())
+            if (acc->isEnabled() and acc->isIP2IP())
+                ip2ip_acc = acc;
+        if (!ip2ip_acc) {
+            RING_ERR("No suitable IP2IP account found to call '%s'", toUrl.c_str());
+            return nullptr;
+        }
+        preferred = ip2ip_acc;
+    }
 
-    // FIXME: have a generic version to remove sip dependency
-    sip_utils::stripSipUriPrefix(finalToUrl);
-
-    if (!IpAddr::isValid(finalToUrl)) {
-        account = preferred;
-        if (account)
-            finalToUrl = toUrl;
-        else
-            RING_WARN("Preferred account %s doesn't exist, using IP2IP account",
-                 preferredAccountId.c_str());
-    } else
-        RING_WARN("IP Url detected, using IP2IP account");
-
-    if (!account or !account->isUsable()) {
+    // Sanity checks
+    if (!preferred or !preferred->isUsable()) {
         RING_ERR("No suitable account found to create outgoing call");
         return nullptr;
     }
 
-    return account->newOutgoingCall(finalToUrl);
+    return preferred->newOutgoingCall(toUrl);
 }
 
 #ifdef RING_VIDEO
-- 
GitLab