sippresence.cpp 8.96 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
 *  Copyright (C) 2004-2013 Savoir-Faire Linux Inc.
 *
 *  Author: Patrick Keroulas  <patrick.keroulas@savoirfairelinux.com>
 *
 *  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.
 *
 *  Additional permission under GNU GPL version 3 section 7:
 *
 *  If you modify this program, or any covered work, by linking or
 *  combining it with the OpenSSL project's OpenSSL library (or a
 *  modified version of that library), containing parts covered by the
 *  terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
 *  grants you additional permission to convey the resulting work.
 *  Corresponding Source for a non-source form of such a combination
 *  shall include the source code for the parts of OpenSSL used as well
 *  as that of the covered work.
 */


#include "logger.h"
#include "manager.h"
#include "client/client.h"
#include "client/callmanager.h"
37
#include "client/presencemanager.h"
38
39
40
#include "sipaccount.h"
#include "sippublish.h"
#include "sippresence.h"
41
42
#include "pres_sub_server.h"
#include "pres_sub_client.h"
43
44
#include "sipvoiplink.h"

45
46
#define MAX_N_PRES_SUB_SERVER 20
#define MAX_N_PRES_SUB_CLIENT 20
47
48

SIPPresence::SIPPresence(SIPAccount *acc)
49
    : pres_status_data()
50
    , publish_sess()
51
    , enabled(true)
52
    , acc_(acc)
53
54
    , pres_sub_server_list_ () //IP2IP context
    , pres_sub_client_list_ ()
55
56
57
58
    , mutex_()
    , mutex_nesting_level_()
    , mutex_owner_()
    , cp_()
59
    , pool_()
60
{
61
    /* init default status */
62
    updateStatus(true,"Available");
63

64
65
66
67
    /* init pool */
    pj_caching_pool_init(&cp_, &pj_pool_factory_default_policy, 0);
    pool_ = pj_pool_create(&cp_.factory, "pres", 1000, 1000, NULL);

68
69
70
    /* Create mutex */
    if(pj_mutex_create_recursive(pool_, "pres",&mutex_) != PJ_SUCCESS)
	ERROR("Unable to create mutex");
71
72
73
}


74
SIPPresence::~SIPPresence(){
75
    /* Flush the lists */
76
77
78
79
    for (auto c : pres_sub_client_list_)
        removePresSubClient(c) ;
    for (auto s : pres_sub_server_list_)
        removePresSubServer(s);
80
}
81

82
SIPAccount * SIPPresence::getAccount() const {
83
84
85
    return acc_;
}

86
pjsip_pres_status * SIPPresence::getStatus() {
87
88
89
    return &pres_status_data;
}

90
int SIPPresence::getModId() const {
91
92
93
    return  ((SIPVoIPLink*) (acc_->getVoIPLink()))->getModId();
}

94
pj_pool_t*  SIPPresence::getPool() const {
95
96
97
    return pool_;
}

98
99
100
101
void SIPPresence::enable(const bool& flag){
    enabled = flag;
}

102
void SIPPresence::updateStatus(const bool& status, const std::string &note){
103
104
    //char* pj_note  = (char*) pj_pool_alloc(pool_, "50");

105
106
    pjrpid_element rpid = {
            PJRPID_ELEMENT_TYPE_PERSON,
107
108
            pj_str("20"),
            PJRPID_ACTIVITY_UNKNOWN,
109
            pj_str((char *) note.c_str())};
110
111
112
113
114
115

    /* fill activity if user not available. */
    if(note=="away")
        rpid.activity = PJRPID_ACTIVITY_AWAY;
    else if (note=="busy")
        rpid.activity = PJRPID_ACTIVITY_BUSY;
116
    else // TODO: is there any other possibilities
117
118
119
120
        DEBUG("Presence : no activity");

    pj_bzero(&pres_status_data, sizeof(pres_status_data));
    pres_status_data.info_cnt = 1;
121
    pres_status_data.info[0].basic_open = status;
122
123
124
125
126
    pres_status_data.info[0].id = pj_str("0"); /* todo: tuplie_id*/
    pj_memcpy(&pres_status_data.info[0].rpid, &rpid,sizeof(pjrpid_element));
    /* "contact" field is optionnal */
}

127
void SIPPresence::sendPresence(const bool& status, const std::string &note){
128
    updateStatus(status,note);
129
130
131
132
133
134
    if(enabled){
        if (acc_->isIP2IP())
            notifyPresSubServer(); // to each subscribers
        else
            pres_publish(this); // to the PBX server
    }
135
136
137
}


138
void SIPPresence::reportPresSubClientNotification(const std::string& uri, pjsip_pres_status * status){
139
140
141
    /* Update our info. See pjsua_buddy_get_info() for additionnal ideas*/
    const std::string basic(status->info[0].basic_open ? "open" : "closed");
    const std::string note(status->info[0].rpid.note.ptr,status->info[0].rpid.note.slen);
142
    DEBUG(" Received status of PresSubClient  %s: status=%s note=%s",uri.c_str(),basic.c_str(),note.c_str());
143
    /* report status to client signal */
144
    Manager::instance().getClient()->getPresenceManager()->newPresSubClientNotification(uri, basic, note);
145
146
}

147
148
149
150
151
152
153
void SIPPresence::subscribePresSubClient(const std::string& uri, const bool& flag){
    /* Check if the buddy was already subscribed */
    for(auto  c : pres_sub_client_list_)
        if(c->getURI()==uri){
            DEBUG("-PresSubClient:%s exists in the list. Replace it.",uri.c_str());
            delete c;
            removePresSubClient(c);
154
155
156
            break;
        }

157
158
159
160
161
    if(pres_sub_client_list_.size() >= MAX_N_PRES_SUB_CLIENT){
        WARN("Can't add PresSubClient, max number reached.");
        return;
    }

162
    if(flag){
163
        PresSubClient *c = new PresSubClient(uri,this);
164
        if(!(c->subscribe())){
165
            WARN("Failed send subscribe.");
166
            delete c;
167
        }
168
169
        // the buddy has to be accepted before being added in the list
    }
170
171
}

172
void SIPPresence::addPresSubClient(PresSubClient *c){
173
    if(pres_sub_client_list_.size() < MAX_N_PRES_SUB_CLIENT){
174
        pres_sub_client_list_.push_back(c);
175
        DEBUG("New Presence_subscription_client client added in the list[l=%i].",pres_sub_client_list_.size());
176
177
    }
    else{
178
        WARN("Max Presence_subscription_client is reach.");
179
        // let the client alive //delete c;
180
    }
181
}
182

183
void SIPPresence::removePresSubClient(PresSubClient *c){
184
    DEBUG("Presence_subscription_client removed from the buddy list.");
185
    pres_sub_client_list_.remove(c);
186
187
}

188

189
void SIPPresence::reportNewPresSubServerRequest(PresSubServer *s){
190
    Manager::instance().getClient()->getPresenceManager()->newPresSubServerRequest(s->remote);
191
192
}

193
void SIPPresence::approvePresSubServer(const std::string& uri, const bool& flag){
194
195
    for (auto s : pres_sub_server_list_)
         if(s->matches((char *) uri.c_str())){
196
             DEBUG("Approve Presence_subscription_server for %s: %s.",s->remote,flag? "true":"false");
197
             s->approve(flag);
198
199
             // return; // 'return' would prevent multiple-time subscribers from spam
         }
200
201
202
}


203
204
void SIPPresence::addPresSubServer(PresSubServer *s) {
    if(pres_sub_server_list_.size() < MAX_N_PRES_SUB_SERVER){
205
        DEBUG("Presence_subscription_server added: %s.",s->remote);
206
207
208
        pres_sub_server_list_.push_back(s);
    }
    else{
209
        WARN("Max Presence_subscription_server is reach.");
210
        // let de server alive // delete s;
211
    }
212
213
}

214
215
void SIPPresence::removePresSubServer(PresSubServer *s) {
    pres_sub_server_list_.remove(s);
216
    DEBUG("Presence_subscription_server removed");
217
218
}

219
void SIPPresence::notifyPresSubServer() {
220
    DEBUG("Iterating through Presence_subscription_server:");
221
222
    for (auto s : pres_sub_server_list_)
        s->notify();
223
}
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254


void SIPPresence::lock()
{
    pj_mutex_lock(mutex_);
    mutex_owner_ = pj_thread_this();
    ++mutex_nesting_level_;
}

void SIPPresence::unlock()
{
    if (--mutex_nesting_level_ == 0)
	mutex_owner_ = NULL;
    pj_mutex_unlock(mutex_);
}

bool SIPPresence::tryLock()
{
    pj_status_t status;
    status = pj_mutex_trylock(mutex_);
    if (status == PJ_SUCCESS) {
	mutex_owner_ = pj_thread_this();
	++mutex_nesting_level_;
    }
    return status;
}

bool SIPPresence::isLocked()
{
    return mutex_owner_ == pj_thread_this();
}
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287

void SIPPresence::fillDoc(pjsip_tx_data *tdata, const pres_msg_data *msg_data)
{

    if (tdata->msg->type == PJSIP_REQUEST_MSG) {
        const pj_str_t STR_USER_AGENT = pj_str("User-Agent");
        std::string useragent(acc_->getUserAgentName());
        pj_str_t pJuseragent = pj_str((char*) useragent.c_str());
        pjsip_hdr *h = (pjsip_hdr*) pjsip_generic_string_hdr_create(tdata->pool, &STR_USER_AGENT, &pJuseragent);
        pjsip_msg_add_hdr(tdata->msg, h);
    }

    if(msg_data == NULL)
        return;

    const pjsip_hdr *hdr;
    hdr = msg_data->hdr_list.next;
    while (hdr && hdr != &msg_data->hdr_list) {
        pjsip_hdr *new_hdr;
        new_hdr = (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, hdr);
        DEBUG("adding header", new_hdr->name.ptr);
        pjsip_msg_add_hdr(tdata->msg, new_hdr);
        hdr = hdr->next;
    }

    if (msg_data->content_type.slen && msg_data->msg_body.slen) {
        pjsip_msg_body *body;
        pj_str_t type = pj_str("application");
        pj_str_t subtype = pj_str("pidf+xml");
        body = pjsip_msg_body_create(tdata->pool, &type, &subtype, &msg_data->msg_body);
        tdata->msg->body = body;
    }
}