Ring Daemon
Loading...
Searching...
No Matches
presencemanager.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2004-2026 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 <cstring>
26
27#include "logger.h"
28#include "manager.h"
29#include "presence_const.h"
30#include "sip/sipaccount.h"
31#include "sip/sippresence.h"
32#include "sip/pres_sub_client.h"
33#include "compiler_intrinsics.h"
34
35#include "jamidht/jamiaccount.h"
36
37namespace libjami {
38
40
41void
42registerPresHandlers(const std::map<std::string, std::shared_ptr<CallbackWrapperBase>>& handlers)
43{
44 registerSignalHandlers(handlers);
45}
46
50void
51subscribeBuddy(const std::string& accountId, const std::string& uri, bool flag)
52{
53 if (auto sipaccount = jami::Manager::instance().getAccount<SIPAccount>(accountId)) {
54 auto* pres = sipaccount->getPresence();
55 if (pres and pres->isEnabled() and pres->isSupported(PRESENCE_FUNCTION_SUBSCRIBE)) {
56 JAMI_DEBUG("{}ubscribePresence (acc:{}, buddy:{})", flag ? "S" : "Uns", accountId, uri);
57 pres->subscribeClient(uri, flag);
58 }
59 } else if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accountId)) {
60 acc->trackBuddyPresence(uri, flag);
61 } else
62 JAMI_ERROR("Unable to find account {}", accountId);
63}
64
70void
71publish(const std::string& accountId, bool status, const std::string& note)
72{
73 if (auto sipaccount = jami::Manager::instance().getAccount<SIPAccount>(accountId)) {
74 auto* pres = sipaccount->getPresence();
75 if (pres and pres->isEnabled() and pres->isSupported(PRESENCE_FUNCTION_PUBLISH)) {
76 JAMI_DEBUG("Send Presence (acc:{}, status {}).", accountId, status ? "online" : "offline");
77 pres->sendPresence(status, note);
78 }
79 } else if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accountId)) {
80 acc->sendPresenceNote(note);
81 } else
82 JAMI_ERROR("Unable to find account {}", accountId);
83}
84
88void
89answerServerRequest(UNUSED const std::string& uri, UNUSED bool flag)
90{
91#if 0 // DISABLED: removed IP2IP support, tuleap: #448
92 auto account = jami::Manager::instance().getIP2IPAccount();
93 if (auto sipaccount = static_cast<SIPAccount *>(account.get())) {
94 JAMI_DEBUG("Approve presence (acc:IP2IP, serv:{}, flag:{})", uri, flag);
95
96 if (auto pres = sipaccount->getPresence())
97 pres->approvePresSubServer(uri, flag);
98 else
99 JAMI_ERROR("Presence not initialized");
100 } else
101 JAMI_ERROR("Unable to find account IP2IP");
102#else
103 JAMI_ERROR("answerServerRequest() is deprecated and does nothing");
104#endif
105}
106
110std::vector<std::map<std::string, std::string>>
111getSubscriptions(const std::string& accountId)
112{
113 std::vector<std::map<std::string, std::string>> ret;
114
115 if (auto sipaccount = jami::Manager::instance().getAccount<SIPAccount>(accountId)) {
116 if (auto* pres = sipaccount->getPresence()) {
117 const auto& subs = pres->getClientSubscriptions();
118 ret.reserve(subs.size());
119 for (const auto& s : subs) {
120 ret.push_back({{libjami::Presence::BUDDY_KEY, std::string(s->getURI())},
123 {libjami::Presence::LINESTATUS_KEY, std::string(s->getLineStatus())}});
124 }
125 } else
126 JAMI_ERROR("Presence not initialized");
127 } else if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accountId)) {
128 const auto& trackedBuddies = acc->getTrackedBuddyPresence();
129 ret.reserve(trackedBuddies.size());
130 for (const auto& tracked_id : trackedBuddies) {
131 ret.push_back({{libjami::Presence::BUDDY_KEY, tracked_id.first},
134 }
135 } else
136 JAMI_ERROR("Unable to find account {}", accountId);
137
138 return ret;
139}
140
144void
145setSubscriptions(const std::string& accountId, const std::vector<std::string>& uris)
146{
147 if (auto sipaccount = jami::Manager::instance().getAccount<SIPAccount>(accountId)) {
148 if (auto* pres = sipaccount->getPresence()) {
149 for (const auto& u : uris)
150 pres->subscribeClient(u, true);
151 } else
152 JAMI_ERROR("Presence not initialized");
153 } else if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accountId)) {
154 for (const auto& u : uris)
155 acc->trackBuddyPresence(u, true);
156 } else
157 JAMI_ERROR("Unable to find account {}", accountId);
158}
159
160} // namespace libjami
Ring Account is build on top of SIPAccountBase and uses DHT to handle call connectivity.
Definition jamiaccount.h:93
static LIBJAMI_TEST_EXPORT Manager & instance()
Definition manager.cpp:694
std::shared_ptr< T > getAccount(std::string_view accountId) const
Get an account pointer, looks for account of type T.
Definition manager.h:753
#define UNUSED
#define JAMI_ERROR(formatstr,...)
Definition logger.h:243
#define JAMI_DEBUG(formatstr,...)
Definition logger.h:238
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:34
#define PRESENCE_FUNCTION_PUBLISH
Definition sippresence.h:33