Ring Daemon 16.0.0
Loading...
Searching...
No Matches
presencemanager.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2004-2025 Savoir-faire Linux Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18#ifdef HAVE_CONFIG_H
19#include "config.h"
20#endif
21
23
24#include <cerrno>
25#include <sstream>
26#include <cstring>
27
28#include "logger.h"
29#include "manager.h"
30#include "sip/sipaccount.h"
31#include "sip/sippresence.h"
32#include "sip/pres_sub_client.h"
33#include "client/ring_signal.h"
34#include "compiler_intrinsics.h"
35
36#include "jamidht/jamiaccount.h"
37
38namespace libjami {
39
41
42void
43registerPresHandlers(const std::map<std::string, std::shared_ptr<CallbackWrapperBase>>& handlers)
44{
45 registerSignalHandlers(handlers);
46}
47
51void
52subscribeBuddy(const std::string& accountId, const std::string& uri, bool flag)
53{
54 if (auto sipaccount = jami::Manager::instance().getAccount<SIPAccount>(accountId)) {
55 auto pres = sipaccount->getPresence();
56 if (pres and pres->isEnabled() and pres->isSupported(PRESENCE_FUNCTION_SUBSCRIBE)) {
57 JAMI_DEBUG("{}ubscribePresence (acc:{}, buddy:{})",
58 flag ? "S" : "Uns",
59 accountId,
60 uri);
61 pres->subscribeClient(uri, flag);
62 }
63 } else if (auto ringaccount = jami::Manager::instance().getAccount<jami::JamiAccount>(
64 accountId)) {
65 ringaccount->trackBuddyPresence(uri, flag);
66 } else
67 JAMI_ERROR("Unable to find account {}", accountId);
68}
69
75void
76publish(const std::string& accountId, bool status, const std::string& note)
77{
78 if (auto sipaccount = jami::Manager::instance().getAccount<SIPAccount>(accountId)) {
79 auto pres = sipaccount->getPresence();
80 if (pres and pres->isEnabled() and pres->isSupported(PRESENCE_FUNCTION_PUBLISH)) {
81 JAMI_DEBUG("Send Presence (acc:{}, status {}).",
82 accountId,
83 status ? "online" : "offline");
84 pres->sendPresence(status, note);
85 }
87 accountId)) {
88 acc->sendPresenceNote(note);
89 } else
90 JAMI_ERROR("Unable to find account {}", accountId);
91}
92
96void
97answerServerRequest(UNUSED const std::string& uri, UNUSED bool flag)
98{
99#if 0 // DISABLED: removed IP2IP support, tuleap: #448
100 auto account = jami::Manager::instance().getIP2IPAccount();
101 if (auto sipaccount = static_cast<SIPAccount *>(account.get())) {
102 JAMI_DEBUG("Approve presence (acc:IP2IP, serv:{}, flag:{})", uri, flag);
103
104 if (auto pres = sipaccount->getPresence())
105 pres->approvePresSubServer(uri, flag);
106 else
107 JAMI_ERROR("Presence not initialized");
108 } else
109 JAMI_ERROR("Unable to find account IP2IP");
110#else
111 JAMI_ERROR("answerServerRequest() is deprecated and does nothing");
112#endif
113}
114
118std::vector<std::map<std::string, std::string>>
119getSubscriptions(const std::string& accountId)
120{
121 std::vector<std::map<std::string, std::string>> ret;
122
123 if (auto sipaccount = jami::Manager::instance().getAccount<SIPAccount>(accountId)) {
124 if (auto pres = sipaccount->getPresence()) {
125 const auto& subs = pres->getClientSubscriptions();
126 ret.reserve(subs.size());
127 for (const auto& s : subs) {
128 ret.push_back(
129 {{libjami::Presence::BUDDY_KEY, std::string(s->getURI())},
131 {libjami::Presence::LINESTATUS_KEY, std::string(s->getLineStatus())}});
132 }
133 } else
134 JAMI_ERROR("Presence not initialized");
135 } else if (auto ringaccount = jami::Manager::instance().getAccount<jami::JamiAccount>(
136 accountId)) {
137 const auto& trackedBuddies = ringaccount->getTrackedBuddyPresence();
138 ret.reserve(trackedBuddies.size());
139 for (const auto& tracked_id : trackedBuddies) {
140 ret.push_back(
141 {{libjami::Presence::BUDDY_KEY, tracked_id.first},
144 }
145 } else
146 JAMI_ERROR("Unable to find account {}", accountId);
147
148 return ret;
149}
150
154void
155setSubscriptions(const std::string& accountId, const std::vector<std::string>& uris)
156{
157 if (auto sipaccount = jami::Manager::instance().getAccount<SIPAccount>(accountId)) {
158 if (auto pres = sipaccount->getPresence()) {
159 for (const auto& u : uris)
160 pres->subscribeClient(u, true);
161 } else
162 JAMI_ERROR("Presence not initialized");
163 } else if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(
164 accountId)) {
165 for (const auto& u : uris)
166 acc->trackBuddyPresence(u, true);
167 } else
168 JAMI_ERROR("Unable to find account {}", accountId);
169}
170
171} // namespace libjami
Ring Account is build on top of SIPAccountBase and uses DHT to handle call connectivity.
Definition jamiaccount.h:96
static LIBJAMI_TEST_EXPORT Manager & instance()
Definition manager.cpp:676
std::shared_ptr< T > getAccount(std::string_view accountId) const
Get an account pointer, looks for account of type T.
Definition manager.h:721
#define UNUSED
#define JAMI_ERROR(formatstr,...)
Definition logger.h:228
#define JAMI_DEBUG(formatstr,...)
Definition logger.h:226
static constexpr const char * ONLINE_KEY
static constexpr const char * LINESTATUS_KEY
static constexpr const char * OFFLINE_KEY
static constexpr const char * BUDDY_KEY
static constexpr const char * STATUS_KEY
std::vector< std::map< std::string, std::string > > getSubscriptions(const std::string &accountId)
Get all active subscriptions for "accountId".
void subscribeBuddy(const std::string &accountId, const std::string &uri, bool flag)
Un/subscribe to buddySipUri for an accountId.
void registerPresHandlers(const std::map< std::string, std::shared_ptr< CallbackWrapperBase > > &handlers)
void answerServerRequest(UNUSED const std::string &uri, UNUSED bool flag)
Accept or not a PresSubServer request for IP2IP account.
void registerSignalHandlers(const std::map< std::string, std::shared_ptr< CallbackWrapperBase > > &handlers)
void setSubscriptions(const std::string &accountId, const std::vector< std::string > &uris)
Batch subscribing of URIs.
void publish(const std::string &accountId, bool status, const std::string &note)
push a presence for a account Notify for IP2IP account For JamiAccount status is ignored but note is ...
A SIP Account specify SIP specific functions and object = SIPCall/SIPVoIPLink)
A SIP Presence manages buddy subscription in both PBX and IP2IP contexts.
#define PRESENCE_FUNCTION_SUBSCRIBE
Definition sippresence.h:35
#define PRESENCE_FUNCTION_PUBLISH
Definition sippresence.h:34