presencemanager.cpp 5.86 KB
Newer Older
1
/*
2
 *  Copyright (C) 2013-2018 Savoir-faire Linux Inc.
3
 *
4
 *  Author: Patrick Keroulas <patrick.keroulas@savoirfairelinux.com>
5
 *  Author: Guillaume Roguez <Guillaume.Roguez@savoirfairelinux.com>
6
 *  Author: Simon Désaulniers <simon.desaulniers@savoirfairelinux.com>
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

27
#include "presencemanager_interface.h"
28 29 30

#include <cerrno>
#include <sstream>
31
#include <cstring>
32 33 34

#include "logger.h"
#include "manager.h"
35
#include "sip/sipaccount.h"
36
#include "sip/sippresence.h"
37
#include "sip/pres_sub_client.h"
38
#include "client/ring_signal.h"
39
#include "compiler_intrinsics.h"
40

41 42
#include "ringdht/ringaccount.h"

43 44 45
namespace DRing {

using ring::SIPAccount;
46

47 48 49
void
registerPresHandlers(const std::map<std::string,
                     std::shared_ptr<CallbackWrapperBase>>& handlers)
50
{
51 52 53 54 55 56 57 58 59 60
    auto& handlers_ = ring::getSignalHandlers();
    for (auto& item : handlers) {
        auto iter = handlers_.find(item.first);
        if (iter == handlers_.end()) {
            RING_ERR("Signal %s not supported", item.first.c_str());
            continue;
        }

        iter->second = std::move(item.second);
    }
61 62
}

63 64 65 66
/**
 * Un/subscribe to buddySipUri for an accountID
 */
void
67
subscribeBuddy(const std::string& accountID, const std::string& uri, bool flag)
68
{
69 70 71 72 73 74 75
    if (auto sipaccount = ring::Manager::instance().getAccount<SIPAccount>(accountID)) {
        auto pres = sipaccount->getPresence();
        if (pres and pres->isEnabled() and pres->isSupported(PRESENCE_FUNCTION_SUBSCRIBE)) {
            RING_DBG("%subscribePresence (acc:%s, buddy:%s)",
                     flag ? "S" : "Uns", accountID.c_str(), uri.c_str());
            pres->subscribeClient(uri, flag);
        }
76 77
    } else if (auto ringaccount = ring::Manager::instance().getAccount<ring::RingAccount>(accountID)) {
        ringaccount->trackBuddyPresence(uri);
78
    } else
Adrien Béraud's avatar
Adrien Béraud committed
79
        RING_ERR("Could not find account %s", accountID.c_str());
80 81 82 83 84 85 86
}

/**
 * push a presence for a account
 * Notify for IP2IP account and publish for PBX account
 */
void
87
publish(const std::string& accountID, bool status, const std::string& note)
88
{
89 90 91 92 93 94 95 96
    if (auto sipaccount = ring::Manager::instance().getAccount<SIPAccount>(accountID)) {
        auto pres = sipaccount->getPresence();
        if (pres and pres->isEnabled() and pres->isSupported(PRESENCE_FUNCTION_PUBLISH)) {
            RING_DBG("Send Presence (acc:%s, status %s).", accountID.c_str(),
                     status ? "online" : "offline");
            pres->sendPresence(status, note);
        }
    } else
Adrien Béraud's avatar
Adrien Béraud committed
97
        RING_ERR("Could not find account %s.", accountID.c_str());
98 99 100 101 102 103
}

/**
 * Accept or not a PresSubServer request for IP2IP account
 */
void
104
answerServerRequest(UNUSED const std::string& uri, UNUSED bool flag)
105
{
106
#if 0 // DISABLED: removed IP2IP support, tuleap: #448
107 108 109 110 111 112 113 114 115 116
    auto account = ring::Manager::instance().getIP2IPAccount();
    if (auto sipaccount = static_cast<SIPAccount *>(account.get())) {
        RING_DBG("Approve presence (acc:IP2IP, serv:%s, flag:%s)", uri.c_str(),
                 flag ? "true" : "false");

        if (auto pres = sipaccount->getPresence())
            pres->approvePresSubServer(uri, flag);
        else
            RING_ERR("Presence not initialized");
    } else
Adrien Béraud's avatar
Adrien Béraud committed
117
        RING_ERR("Could not find account IP2IP");
118 119 120
#else
    RING_ERR("answerServerRequest() is deprecated and does nothing");
#endif
121
}
122 123 124 125 126

/**
 * Get all active subscriptions for "accountID"
 */
std::vector<std::map<std::string, std::string> >
127
getSubscriptions(const std::string& accountID)
128
{
129 130 131 132 133 134
    std::vector<std::map<std::string, std::string>> ret;

    if (auto sipaccount = ring::Manager::instance().getAccount<SIPAccount>(accountID)) {
        if (auto pres = sipaccount->getPresence()) {
            for (const auto& s : pres->getClientSubscriptions()) {
                ret.push_back({
135 136 137
                    {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()}
138
                });
139 140
            }
        } else
Adrien Béraud's avatar
Adrien Béraud committed
141
            RING_ERR("Presence not initialized");
142 143 144
    } else if (auto ringaccount = ring::Manager::instance().getAccount<ring::RingAccount>(accountID)) {
        for (const auto& tracked_id : ringaccount->getTrackedBuddyPresence()) {
            ret.push_back({
145 146
                    {DRing::Presence::BUDDY_KEY, tracked_id.first},
                    {DRing::Presence::STATUS_KEY, tracked_id.second ? DRing::Presence::ONLINE_KEY : DRing::Presence::OFFLINE_KEY}
147 148
                });
        }
149 150
    } else
        RING_ERR("Could not find account %s.", accountID.c_str());
151

152 153 154 155 156 157 158
    return ret;
}

/**
 * Batch subscribing of URIs
 */
void
159
setSubscriptions(const std::string& accountID, const std::vector<std::string>& uris)
160
{
161 162 163 164 165 166 167 168
    if (auto sipaccount = ring::Manager::instance().getAccount<SIPAccount>(accountID)) {
        if (auto pres = sipaccount->getPresence()) {
            for (const auto &u : uris)
                pres->subscribeClient(u, true);
        } else
            RING_ERR("Presence not initialized");
    } else
        RING_ERR("Could not find account %s.", accountID.c_str());
169
}
170

171
} // namespace DRing