From 22f7be8c6b911693e7c871d78b151308aeeaddda Mon Sep 17 00:00:00 2001
From: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
Date: Mon, 11 Apr 2016 10:58:22 -0400
Subject: [PATCH] ip2ip: restore fallback value in guessAccount

By removing the default value returned by this method (the old and
famous IP2IP account), we lost the ability to receive IP2IP calls even
with an empty SIP account

Change-Id: Ia30c9067e44bd3d83080d201dab75e7c6d770b4f
Tuleap: #448
---
 src/manager.cpp         | 17 +++--------------
 src/manager.h           | 23 ++++++++++++++++++++---
 src/sip/sipvoiplink.cpp |  8 ++++++--
 3 files changed, 29 insertions(+), 19 deletions(-)

diff --git a/src/manager.cpp b/src/manager.cpp
index 702f7630b5..f389070faf 100644
--- a/src/manager.cpp
+++ b/src/manager.cpp
@@ -2321,22 +2321,11 @@ Manager::setAccountsOrder(const std::string& order)
 std::vector<std::string>
 Manager::getAccountList() const
 {
-    auto account_order = loadAccountOrder();
-
     // Concatenate all account pointers in a single map
-    const auto& allAccounts = accountFactory_.getAllAccounts();
     std::vector<std::string> v;
-    v.reserve(allAccounts.size());
-
-    // If no order has been set, load the default one ie according to the creation date.
-    if (account_order.empty()) {
-        for (const auto &account : allAccounts)
-            v.emplace_back(account->getAccountID());
-    } else {
-        for (const auto& id : account_order) {
-            if (accountFactory_.hasAccount(id))
-                v.push_back(id);
-        }
+    v.reserve(accountCount());
+    for (const auto &account : getAllAccounts()) {
+        v.emplace_back(account->getAccountID());
     }
 
     return v;
diff --git a/src/manager.h b/src/manager.h
index 1e2e8014aa..a792490c76 100644
--- a/src/manager.h
+++ b/src/manager.h
@@ -885,7 +885,7 @@ class Manager {
         bool hasCurrentCall() const;
 
         /**
-         * Get an account pointer, looks for both SIP and IAX
+         * Get an account pointer, looks for account of type T
          * @param accountID account ID to get
          * @return std::shared_ptr<Account> Shared pointer on an Account instance or nullptr if not found
          */
@@ -894,9 +894,26 @@ class Manager {
             return accountFactory_.getAccount<T>(accountID);
         }
 
+        /**
+         * Get a list of account pointers of type T (baseclass Account)
+         * @return a sorted vector of all accounts of type T
+         */
         template <class T=Account>
-        std::vector<std::shared_ptr<T> > getAllAccounts() const {
-            return accountFactory_.getAllAccounts<T>();
+        std::vector<std::shared_ptr<T>> getAllAccounts() const {
+            auto account_order = loadAccountOrder();
+            std::vector<std::shared_ptr<T>> accountList;
+
+            // If no order has been set, load the default one ie according to the creation date.
+            if (account_order.empty()) {
+                for (const auto &account : accountFactory_.getAllAccounts<T>())
+                    accountList.emplace_back(account);
+            } else {
+                for (const auto& id : account_order) {
+                    if (auto acc = accountFactory_.getAccount<T>(id))
+                        accountList.push_back(acc);
+                }
+            }
+            return accountList;
         }
 
         template <class T=Account>
diff --git a/src/sip/sipvoiplink.cpp b/src/sip/sipvoiplink.cpp
index 7efce94e64..c17ea35f23 100644
--- a/src/sip/sipvoiplink.cpp
+++ b/src/sip/sipvoiplink.cpp
@@ -633,7 +633,8 @@ SIPVoIPLink::guessAccount(const std::string& userName,
     RING_DBG("username = %s, server = %s, from = %s", userName.c_str(), server.c_str(), fromUri.c_str());
     // Try to find the account id from username and server name by full match
 
-    std::shared_ptr<SIPAccountBase> result {};
+    std::shared_ptr<SIPAccountBase> result;
+    std::shared_ptr<SIPAccountBase> IP2IPAccount;
     MatchRank best = MatchRank::NONE;
 
 #if HAVE_DHT
@@ -665,10 +666,13 @@ SIPVoIPLink::guessAccount(const std::string& userName,
         } else if (match > best) {
             best = match;
             result = account;
+        } else if (!IP2IPAccount && account->isIP2IP()) {
+            // Allow IP2IP calls if an account exists for this type of calls
+            IP2IPAccount = account;
         }
     }
 
-    return result;
+    return result ? result : IP2IPAccount;
 }
 
 // Called from EventThread::run (not main thread)
-- 
GitLab