sippresence.cpp 8.41 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
 *  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"
#include "sipaccount.h"
#include "sipbuddy.h"
#include "sippublish.h"
#include "sippresence.h"
#include "presence_subscription.h"
#include "sipvoiplink.h"

/**
 * Generic function.
 * Fill the header.
 */
void pres_process_msg_data(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");
        pjsip_hdr *h;
        pj_str_t ua = pj_str("SFLPhone");
        h = (pjsip_hdr*) pjsip_generic_string_hdr_create(tdata->pool, &STR_USER_AGENT, &ua);
        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;
    }
}

79
static pj_caching_pool cp;
80
81
82
83
84
85
86
87
88
89
90
91

SIPPresence::SIPPresence(SIPAccount *acc)
    : acc_(acc)
    , pres_status_data()
    , online_status()
    , rpid({PJRPID_ELEMENT_TYPE_PERSON,
            pj_str("20"),
            PJRPID_ACTIVITY_BUSY,
            pj_str("")})
    , publish_sess()
    , publish_state()
    , publish_enabled(true)
92
    , newPresenceSubscription_(NULL)
93
94
    , serverSubscriptions_ ()
    , buddies_ ()
95
96
97
98
99
    , mutex_()
    , mutex_nesting_level_()
    , mutex_owner_()
    , pool_()
    , cp_()
100
101
102
{
    // init default status
    updateStatus("open","Available");
103
104
105
106
107
108
109

    cp_ = &cp;
    pj_caching_pool_init(cp_, NULL, 0);
    pool_ = pj_pool_create(&(cp_->factory), "pres", 1000, 1000, NULL);
    /* Create mutex */
    if(pj_mutex_create_recursive(pool_, "pres",&mutex_) != PJ_SUCCESS)
	ERROR("Unable to create mutex");
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
}


SIPPresence::~SIPPresence(){}

SIPAccount * SIPPresence::getAccount(){
    return acc_;
}

pjsip_pres_status * SIPPresence::getStatus(){
    return &pres_status_data;
}

int SIPPresence::getModId(){
    return  ((SIPVoIPLink*) (acc_->getVoIPLink()))->getModId();
}

void SIPPresence::updateStatus(const std::string &status, const std::string &note){
    pjrpid_element rpid = {PJRPID_ELEMENT_TYPE_PERSON,
            pj_str("20"),
            PJRPID_ACTIVITY_UNKNOWN,
            pj_str(strdup(note.c_str()))};

    /* fill activity if user not available. */
    if(note=="away")
        rpid.activity = PJRPID_ACTIVITY_AWAY;
    else if (note=="busy")
        rpid.activity = PJRPID_ACTIVITY_BUSY;
    else
        DEBUG("Presence : no activity");

    pj_bzero(&pres_status_data, sizeof(pres_status_data));
    pres_status_data.info_cnt = 1;
    pres_status_data.info[0].basic_open = (status == "open")? true: false;
    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 */
}

void SIPPresence::sendPresence(const std::string &status, const std::string &note){
    updateStatus(status,note);
    if (acc_->isIP2IP())
        notifyServerSubscription();
    else
        pres_publish(this);
}


void SIPPresence::reportBuddy(const std::string& buddySipUri, pjsip_pres_status * status){
    /* 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);
    DEBUG(" Received presenceStateChange for %s status=%s note=%s",buddySipUri.c_str(),basic.c_str(),note.c_str());
    /* report status to client signal */
    Manager::instance().getClient()->getCallManager()->newPresenceNotification(buddySipUri, basic, note);
}

void SIPPresence::subscribeBuddy(const std::string& buddySipUri){
    std::list< SIPBuddy *>::iterator buddyIt;
    for (buddyIt = buddies_.begin(); buddyIt != buddies_.end(); buddyIt++)
        if((*buddyIt)->getURI()==buddySipUri){
            DEBUG("-Buddy:%s exist in the list. Replace it.",buddySipUri.c_str());
            delete *buddyIt;
            removeBuddy(*buddyIt);
            break;
        }
    SIPBuddy *b = new SIPBuddy(buddySipUri, acc_);
    if(!(b->subscribe())){
        WARN("Failed to add buddy.");
        delete b;
    }
}

void SIPPresence::unsubscribeBuddy(const std::string& buddySipUri){
    std::list< SIPBuddy *>::iterator buddyIt;
    for (buddyIt = buddies_.begin(); buddyIt != buddies_.end(); buddyIt++)
        if((*buddyIt)->getURI()==buddySipUri){
            DEBUG("-Found buddy:%s in the buddy list.",buddySipUri.c_str());
            delete *buddyIt;
            removeBuddy(*buddyIt);
            return;
        }
}

void SIPPresence::addBuddy(SIPBuddy *b){
    DEBUG("-New buddy subscription added in the buddy list.");
    buddies_.push_back(b);
}
void SIPPresence::removeBuddy(SIPBuddy *b){
    DEBUG("-Buddy subscription removed from the buddy list.");
    buddies_.remove(b);
}

203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223

void SIPPresence::reportNewServerSubscription(PresenceSubscription *s){
    newPresenceSubscription_ = s;
    Manager::instance().getClient()->getCallManager()->newPresenceSubscription(s->remote);
}

void SIPPresence::confirmNewServerSubscription(const bool& confirm){
    if(newPresenceSubscription_!=NULL)
        return;

    if(confirm){
        DEBUG("-Confirm new PresenceSubscription for %s",newPresenceSubscription_->remote);
        addServerSubscription(newPresenceSubscription_);
    }
    else{
        DEBUG("-Refused new PresenceSubscription for %s",newPresenceSubscription_->remote);
        newPresenceSubscription_ = NULL;
    }
}


224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
void SIPPresence::addServerSubscription(PresenceSubscription *s) {
    DEBUG("-PresenceServer subscription added.");
    serverSubscriptions_.push_back(s);
}

void SIPPresence::removeServerSubscription(PresenceSubscription *s) {
    serverSubscriptions_.remove(s);
    DEBUG("-PresenceServer removed");
}

void SIPPresence::notifyServerSubscription() {
    std::list< PresenceSubscription *>::iterator serverIt;
    DEBUG("-Iterating through PresenceServers:");
    for (serverIt = serverSubscriptions_.begin(); serverIt != serverSubscriptions_.end(); serverIt++)
        (*serverIt)->notify();
}
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270


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();
}