diff --git a/src/client/presencemanager.cpp b/src/client/presencemanager.cpp index 2c59a213ad51319fc6f1f1c8615303b0b32c8462..722ed1cedb1dd7485cf3d8f40e31f224fa7a787b 100644 --- a/src/client/presencemanager.cpp +++ b/src/client/presencemanager.cpp @@ -128,10 +128,9 @@ getSubscriptions(const std::string& accountID) ret.reserve(subs.size()); for (const auto& s : subs) { ret.push_back( - {{DRing::Presence::BUDDY_KEY, s->getURI()}, - {DRing::Presence::STATUS_KEY, - s->isPresent() ? DRing::Presence::ONLINE_KEY : DRing::Presence::OFFLINE_KEY}, - {DRing::Presence::LINESTATUS_KEY, s->getLineStatus()}}); + {{DRing::Presence::BUDDY_KEY, std::string(s->getURI())}, + {DRing::Presence::STATUS_KEY, s->isPresent() ? DRing::Presence::ONLINE_KEY : DRing::Presence::OFFLINE_KEY}, + {DRing::Presence::LINESTATUS_KEY, std::string(s->getLineStatus())}}); } } else JAMI_ERR("Presence not initialized"); diff --git a/src/sip/pres_sub_client.cpp b/src/sip/pres_sub_client.cpp index 29ecd4a83158eec178e30f7bd5e636d104809b8f..4fa7dde5aa797f6e356921f23fdc436fe339761f 100644 --- a/src/sip/pres_sub_client.cpp +++ b/src/sip/pres_sub_client.cpp @@ -56,7 +56,7 @@ void PresSubClient::pres_client_timer_cb(pj_timer_heap_t* /*th*/, pj_timer_entry* entry) { PresSubClient* c = (PresSubClient*) entry->user_data; - JAMI_DBG("timeout for %s", c->getURI().c_str()); + JAMI_DBG("timeout for %.*s", (int)c->getURI().size(), c->getURI().data()); } /* Callback called when *client* subscription state has changed. */ @@ -73,8 +73,8 @@ PresSubClient::pres_client_evsub_on_state(pjsip_evsub* sub, pjsip_event* event) return; } - JAMI_DBG("Subscription for pres_client '%s' is '%s'", - pres_client->getURI().c_str(), + JAMI_DBG("Subscription for pres_client '%.*s' is '%s'", + (int)pres_client->getURI().size(), pres_client->getURI().data(), pjsip_evsub_get_state_name(sub) ? pjsip_evsub_get_state_name(sub) : "null"); pjsip_evsub_state state = pjsip_evsub_get_state(sub); @@ -85,7 +85,7 @@ PresSubClient::pres_client_evsub_on_state(pjsip_evsub* sub, pjsip_event* event) pres_client->enable(true); emitSignal<DRing::PresenceSignal::SubscriptionStateChanged>(pres->getAccount() ->getAccountID(), - pres_client->getURI().c_str(), + std::string(pres_client->getURI()), PJ_TRUE); pres->getAccount()->supportPresence(PRESENCE_FUNCTION_SUBSCRIBE, true); @@ -98,7 +98,7 @@ PresSubClient::pres_client_evsub_on_state(pjsip_evsub* sub, pjsip_event* event) emitSignal<DRing::PresenceSignal::SubscriptionStateChanged>(pres->getAccount() ->getAccountID(), - pres_client->getURI().c_str(), + std::string(pres_client->getURI()), PJ_FALSE); pres_client->term_code_ = 200; @@ -112,8 +112,7 @@ PresSubClient::pres_client_evsub_on_state(pjsip_evsub* sub, pjsip_event* event) std::ostringstream os; os << pres_client->term_code_; const std::string error = os.str() + "/" - + std::string(pres_client->term_reason_.ptr, - pres_client->term_reason_.slen); + + sip_utils::as_view(pres_client->term_reason_); std::string msg; bool subscribe_allowed = PJ_FALSE; @@ -158,9 +157,8 @@ PresSubClient::pres_client_evsub_on_state(pjsip_evsub* sub, pjsip_event* event) emitSignal<DRing::PresenceSignal::ServerError>( pres_client->getPresence()->getAccount()->getAccountID(), error, msg); - std::string account_host = std::string(pj_gethostname()->ptr, - pj_gethostname()->slen); - std::string sub_host = sip_utils::getHostFromUri(pres_client->getURI()); + auto account_host = sip_utils::as_view(*pj_gethostname()); + auto sub_host = sip_utils::getHostFromUri(pres_client->getURI()); if (not subscribe_allowed and account_host == sub_host) pres_client->getPresence() @@ -363,11 +361,10 @@ PresSubClient::isSubscribed() return monitored_; } -std::string +std::string_view PresSubClient::getURI() { - std::string res(uri_.ptr, uri_.slen); - return res; + return {uri_.ptr, (size_t)uri_.slen}; } SIPPresence* @@ -382,16 +379,16 @@ PresSubClient::isPresent() return status_.info[0].basic_open; } -std::string +std::string_view PresSubClient::getLineStatus() { - return std::string(status_.info[0].rpid.note.ptr, status_.info[0].rpid.note.slen); + return {status_.info[0].rpid.note.ptr, (size_t)status_.info[0].rpid.note.slen}; } bool PresSubClient::isTermReason(const std::string& reason) { - const std::string myReason(term_reason_.ptr, term_reason_.slen); + const std::string_view myReason(term_reason_.ptr, (size_t)term_reason_.slen); return not myReason.compare(reason); } @@ -429,7 +426,7 @@ PresSubClient::rescheduleTimer(bool reschedule, unsigned msec) void PresSubClient::enable(bool flag) { - JAMI_DBG("pres_client %s is %s monitored.", getURI().c_str(), flag ? "" : "NOT"); + JAMI_DBG("pres_client %.*s is %s monitored.", (int)getURI().size(), getURI().data(), flag ? "" : "NOT"); if (flag and not monitored_) pres_->addPresSubClient(this); monitored_ = flag; diff --git a/src/sip/pres_sub_client.h b/src/sip/pres_sub_client.h index 801e230b2bce14ed6f9519f8ca979b8b1dad4e6a..6d261fa5488f9adc610c13e781bda25aa1f584cd 100644 --- a/src/sip/pres_sub_client.h +++ b/src/sip/pres_sub_client.h @@ -89,7 +89,7 @@ public: /** * Return the pres_client URI */ - std::string getURI(); + std::string_view getURI(); /** * Is the buddy present @@ -99,7 +99,7 @@ public: /** * A message from the URIs */ - std::string getLineStatus(); + std::string_view getLineStatus(); /** * TODO: explain this: diff --git a/src/sip/sip_utils.cpp b/src/sip/sip_utils.cpp index 8fc22e8889155ae87f6927fcff2f965b087fae8c..0fe34f77b71bfab247ee1aa9afce7cd72fb140f3 100644 --- a/src/sip/sip_utils.cpp +++ b/src/sip/sip_utils.cpp @@ -159,19 +159,18 @@ stripSipUriPrefix(std::string_view sipUri) return sipUri; } -std::string -getHostFromUri(const std::string& sipUri) +std::string_view +getHostFromUri(std::string_view uri) { - std::string hostname(sipUri); - size_t found = hostname.find('@'); - if (found != std::string::npos) - hostname.erase(0, found + 1); + auto found = uri.find('@'); + if (found != std::string_view::npos) + uri = uri.substr(found + 1); - found = hostname.find('>'); - if (found != std::string::npos) - hostname.erase(found, 1); + found = uri.find('>'); + if (found != std::string_view::npos) + uri = uri.substr(0, found); - return hostname; + return uri; } void diff --git a/src/sip/sip_utils.h b/src/sip/sip_utils.h index 0df0f97201a47fb9993c9fa7031d375c5dd1af0f..66e524b7718084ba9d9d7ec78571f166edeee0b0 100644 --- a/src/sip/sip_utils.h +++ b/src/sip/sip_utils.h @@ -96,7 +96,7 @@ std::string parseDisplayName(const pjsip_name_addr* sip_name_addr); std::string parseDisplayName(const pjsip_from_hdr* header); std::string parseDisplayName(const pjsip_contact_hdr* header); -std::string getHostFromUri(const std::string& sipUri); +std::string_view getHostFromUri(std::string_view sipUri); void addContactHeader(const pj_str_t* contactStr, pjsip_tx_data* tdata); void addUserAgenttHeader(const std::string& userAgent, pjsip_tx_data* tdata); diff --git a/src/sip/sippresence.cpp b/src/sip/sippresence.cpp index 6507fedcfb24a9be9b1e6ff2ae348137166d0d93..19c28c757f29b3636341253f35da132450f60f76 100644 --- a/src/sip/sippresence.cpp +++ b/src/sip/sippresence.cpp @@ -178,16 +178,15 @@ SIPPresence::sendPresence(bool status, const std::string& note) } void -SIPPresence::reportPresSubClientNotification(const std::string& uri, pjsip_pres_status* status) +SIPPresence::reportPresSubClientNotification(std::string_view uri, pjsip_pres_status* status) { /* Update our info. See pjsua_buddy_get_info() for additionnal ideas*/ - const std::string acc_ID = acc_->getAccountID(); - const std::string basic(status->info[0].basic_open ? "open" : "closed"); + const std::string& acc_ID = acc_->getAccountID(); const std::string note(status->info[0].rpid.note.ptr, status->info[0].rpid.note.slen); - JAMI_DBG(" Received status of PresSubClient %s(acc:%s): status=%s note=%s", - uri.c_str(), + JAMI_DBG(" Received status of PresSubClient %.*s(acc:%s): status=%s note=%s", + (int)uri.size(), uri.data(), acc_ID.c_str(), - basic.c_str(), + status->info[0].basic_open ? "open" : "closed", note.c_str()); if (uri == acc_->getFromUri()) { @@ -197,7 +196,7 @@ SIPPresence::reportPresSubClientNotification(const std::string& uri, pjsip_pres_ } // report status to client signal emitSignal<DRing::PresenceSignal::NewBuddyNotification>(acc_ID, - uri, + std::string(uri), status->info[0].basic_open, note); } diff --git a/src/sip/sippresence.h b/src/sip/sippresence.h index 140faab30e138228e5afe02f3742b57efb3221aa..1dfd355ea084b37fe4a9bb623828c34391ec36a6 100644 --- a/src/sip/sippresence.h +++ b/src/sip/sippresence.h @@ -146,7 +146,7 @@ public: * Send a signal to the client on DBus. The signal contain the status * of a remote user. */ - void reportPresSubClientNotification(const std::string& uri, pjsip_pres_status* status); + void reportPresSubClientNotification(std::string_view uri, pjsip_pres_status* status); /** * Send a SUBSCRIBE request to PBX/IP2IP * @param buddyUri Remote user that we want to subscribe