diff --git a/src/contactmodel.cpp b/src/contactmodel.cpp index 3116b525b1cb6483106feae3855efd5683fbc284..df1c0ee7dc22bd05c2dc696970b4dbc69a3cde1a 100644 --- a/src/contactmodel.cpp +++ b/src/contactmodel.cpp @@ -95,6 +95,11 @@ public: */ void updateTemporaryMessage(const std::string& mes, const std::string& uri); + /** + * Check if equivalent uri exist in contact + */ + std::string sipUriReceivedFilter(const std::string& uri); + // Helpers const BehaviorController& behaviorController; const ContactModel& linked; @@ -834,11 +839,19 @@ ContactModelPimpl::slotNewAccountMessage(std::string& accountId, if (contacts.find(from) == contacts.end()) { // Contact not found, load profile from database. // The conversation model will create an entry and link the incomingCall. - auto type = (linked.owner.profileInfo.type == profile::Type::RING) - ? profile::Type::PENDING - : profile::Type::SIP; - addToContacts(from, type, false); - emitNewTrust = (linked.owner.profileInfo.type == profile::Type::RING); + + if (linked.owner.profileInfo.type == profile::Type::SIP) { + std::string potentialContact = sipUriReceivedFilter(from); + if (potentialContact.empty()) { + addToContacts(from, profile::Type::SIP, false); + } else { + // equivalent uri exist, use that uri + from = potentialContact; + } + } else { + addToContacts(from, profile::Type::PENDING, false); + emitNewTrust = true; + } } } if (emitNewTrust) { @@ -847,6 +860,61 @@ ContactModelPimpl::slotNewAccountMessage(std::string& accountId, emit linked.newAccountMessage(accountId, from, payloads); } +std::string +ContactModelPimpl::sipUriReceivedFilter(const std::string& uri) +{ + // this function serves when the uri is not found in the contact list + // return "" means need to add new contact, else means equivalent uri exist + std::string uriCopy = uri; + + auto pos = uriCopy.find("@"); + auto ownerHostName = linked.owner.confProperties.hostname; + + if (pos != std::string::npos) { + // "@" is found, separate username and hostname + std::string hostName = uriCopy.substr(pos + 1); + uriCopy.erase(uriCopy.begin() + pos, uriCopy.end()); + std::string remoteUser = std::move(uriCopy); + + if (hostName.compare(ownerHostName) == 0) { + + if (contacts.find(remoteUser) != contacts.end()) { + return remoteUser; + } + if (remoteUser.at(0) == '+') { + // "+" - country dial-in codes + // maximum 3 digits + for (int i = 2; i <= 4; i++) { + std::string tempUserName = remoteUser.substr(i); + if (contacts.find(tempUserName) != contacts.end()) { + return tempUserName; + } + } + return ""; + } else { + // if not "+" from incoming + // sub "+" char from contacts to see if user exit + for (auto& i : contacts) { + if (!i.first.empty()) { + for (int j = 2; j <= 4; j++) { + std::string tempUserName = i.first.substr(j); + if (tempUserName == remoteUser) { + return i.first; + } + } + } + } + return ""; + } + } + // different hostname means not a phone number + // no need to check country dial-in codes + return ""; + } + // "@" is not found -> not possible since all response uri has one + return ""; +} + void ContactModelPimpl::slotNewAccountTransfer(long long dringId, datatransfer::Info info) {