| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include <pjsua-lib/pjsua.h> |
| 20 |
#include <pjsua-lib/pjsua_internal.h> |
| 21 |
|
| 22 |
|
| 23 |
#define THIS_FILE "pjsua_im.h" |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
enum |
| 32 |
{ |
| 33 |
PJSIP_MESSAGE_METHOD = PJSIP_OTHER_METHOD |
| 34 |
}; |
| 35 |
|
| 36 |
const pjsip_method pjsip_message_method = |
| 37 |
{ |
| 38 |
(pjsip_method_e) PJSIP_MESSAGE_METHOD, |
| 39 |
{ "MESSAGE", 7 } |
| 40 |
}; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
static pj_bool_t im_on_rx_request(pjsip_rx_data *rdata); |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
typedef struct im_auth_ctx { |
| 50 |
pjsip_auth_clt_sess auth; |
| 51 |
pjsip_auth_clt_async_impl_token token; |
| 52 |
pjsua_im_data *im_data; |
| 53 |
pjsip_endpt_send_callback send_cb; |
| 54 |
} im_auth_ctx; |
| 55 |
|
| 56 |
static pj_status_t im_async_auth_send_impl( |
| 57 |
pjsip_auth_clt_sess *auth_sess, |
| 58 |
void *user_data, |
| 59 |
pjsip_tx_data *tdata); |
| 60 |
static void im_async_auth_abandon_impl( |
| 61 |
pjsip_auth_clt_sess *auth_sess, |
| 62 |
void *user_data); |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
static pjsip_module mod_pjsua_im = |
| 67 |
{ |
| 68 |
NULL, NULL, |
| 69 |
{ "mod-pjsua-im", 12 }, |
| 70 |
-1, |
| 71 |
PJSIP_MOD_PRIORITY_APPLICATION, |
| 72 |
NULL, |
| 73 |
NULL, |
| 74 |
NULL, |
| 75 |
NULL, |
| 76 |
&im_on_rx_request, |
| 77 |
NULL, |
| 78 |
NULL, |
| 79 |
NULL, |
| 80 |
NULL, |
| 81 |
|
| 82 |
}; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
static const pj_str_t STR_MIME_APP = { "application", 11 }; |
| 87 |
static const pj_str_t STR_MIME_ISCOMPOSING = { "im-iscomposing+xml", 18 }; |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
#if 0 |
| 92 |
static pj_bool_t acceptable_message(const pjsip_media_type *mime) |
| 93 |
{ |
| 94 |
const pj_str_t STR_MIME_TEXT = { "text", 4 }; |
| 95 |
const pj_str_t STR_MIME_PLAIN = { "plain", 5 }; |
| 96 |
return (pj_stricmp(&mime->type, &STR_MIME_TEXT)==0 && |
| 97 |
pj_stricmp(&mime->subtype, &STR_MIME_PLAIN)==0) |
| 98 |
|| |
| 99 |
(pj_stricmp(&mime->type, &STR_MIME_APP)==0 && |
| 100 |
pj_stricmp(&mime->subtype, &STR_MIME_ISCOMPOSING)==0); |
| 101 |
} |
| 102 |
#endif |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
pjsip_accept_hdr* pjsua_im_create_accept(pj_pool_t *pool) |
| 108 |
{ |
| 109 |
|
| 110 |
pjsip_accept_hdr *accept; |
| 111 |
|
| 112 |
accept = pjsip_accept_hdr_create(pool); |
| 113 |
accept->values[0] = pj_str("text/plain"); |
| 114 |
accept->values[1] = pj_str("application/im-iscomposing+xml"); |
| 115 |
accept->count = 2; |
| 116 |
|
| 117 |
return accept; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
pj_bool_t pjsua_im_accept_pager(pjsip_rx_data *rdata, |
| 124 |
pjsip_accept_hdr **p_accept_hdr) |
| 125 |
{ |
| 126 |
|
| 127 |
#if 0 |
| 128 |
pjsip_ctype_hdr *ctype; |
| 129 |
pjsip_msg *msg; |
| 130 |
|
| 131 |
msg = rdata->msg_info.msg; |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
ctype = (pjsip_ctype_hdr*) |
| 137 |
pjsip_msg_find_hdr(msg, PJSIP_H_CONTENT_TYPE, NULL); |
| 138 |
if (msg->body == NULL || ctype == NULL || |
| 139 |
!acceptable_message(&ctype->media)) |
| 140 |
{ |
| 141 |
|
| 142 |
if (p_accept_hdr) |
| 143 |
*p_accept_hdr = pjsua_im_create_accept(rdata->tp_info.pool); |
| 144 |
|
| 145 |
return PJ_FALSE; |
| 146 |
} |
| 147 |
#elif 0 |
| 148 |
pjsip_msg *msg; |
| 149 |
|
| 150 |
msg = rdata->msg_info.msg; |
| 151 |
if (msg->body == NULL) { |
| 152 |
|
| 153 |
if (p_accept_hdr) |
| 154 |
*p_accept_hdr = pjsua_im_create_accept(rdata->tp_info.pool); |
| 155 |
|
| 156 |
return PJ_FALSE; |
| 157 |
} |
| 158 |
#else |
| 159 |
|
| 160 |
PJ_UNUSED_ARG(rdata); |
| 161 |
PJ_UNUSED_ARG(p_accept_hdr); |
| 162 |
#endif |
| 163 |
|
| 164 |
return PJ_TRUE; |
| 165 |
} |
| 166 |
|
| 167 |
static pj_bool_t is_typing_indication(pjsip_rx_data *rdata) |
| 168 |
{ |
| 169 |
pjsip_msg_body *body; |
| 170 |
PJ_ASSERT_RETURN(rdata, PJ_FALSE); |
| 171 |
PJ_ASSERT_RETURN(rdata->msg_info.msg, PJ_FALSE); |
| 172 |
|
| 173 |
body = rdata->msg_info.msg->body; |
| 174 |
|
| 175 |
if (body && pj_stricmp(&body->content_type.type, &STR_MIME_APP)==0 && |
| 176 |
pj_stricmp(&body->content_type.subtype, &STR_MIME_ISCOMPOSING)==0) { |
| 177 |
return PJ_TRUE; |
| 178 |
} |
| 179 |
return PJ_FALSE; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
void pjsua_im_process_pager(int call_id, const pj_str_t *from, |
| 187 |
const pj_str_t *to, pjsip_rx_data *rdata) |
| 188 |
{ |
| 189 |
pjsip_contact_hdr *contact_hdr; |
| 190 |
pj_str_t contact; |
| 191 |
pjsip_msg_body *body = rdata->msg_info.msg->body; |
| 192 |
|
| 193 |
#if 0 |
| 194 |
|
| 195 |
|
| 196 |
pj_assert(body != NULL); |
| 197 |
#endif |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
contact_hdr = (pjsip_contact_hdr*) |
| 202 |
pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT, |
| 203 |
NULL); |
| 204 |
if (contact_hdr && contact_hdr->uri) { |
| 205 |
contact.ptr = (char*) pj_pool_alloc(rdata->tp_info.pool, |
| 206 |
PJSIP_MAX_URL_SIZE); |
| 207 |
contact.slen = pjsip_uri_print(PJSIP_URI_IN_CONTACT_HDR, |
| 208 |
contact_hdr->uri, contact.ptr, |
| 209 |
PJSIP_MAX_URL_SIZE); |
| 210 |
} else { |
| 211 |
contact.slen = 0; |
| 212 |
} |
| 213 |
|
| 214 |
if (is_typing_indication(rdata) == PJ_TRUE) { |
| 215 |
|
| 216 |
pj_status_t status; |
| 217 |
pj_bool_t is_typing; |
| 218 |
|
| 219 |
status = pjsip_iscomposing_parse(rdata->tp_info.pool, (char*)body->data, |
| 220 |
body->len, &is_typing, NULL, NULL, |
| 221 |
NULL ); |
| 222 |
if (status != PJ_SUCCESS) { |
| 223 |
pjsua_perror(THIS_FILE, "Invalid MESSAGE body", status); |
| 224 |
return; |
| 225 |
} |
| 226 |
|
| 227 |
if (pjsua_var.ua_cfg.cb.on_typing) { |
| 228 |
(*pjsua_var.ua_cfg.cb.on_typing)(call_id, from, to, &contact, |
| 229 |
is_typing); |
| 230 |
} |
| 231 |
|
| 232 |
if (pjsua_var.ua_cfg.cb.on_typing2) { |
| 233 |
pjsua_acc_id acc_id; |
| 234 |
|
| 235 |
if (call_id == PJSUA_INVALID_ID) { |
| 236 |
acc_id = pjsua_acc_find_for_incoming(rdata); |
| 237 |
} else { |
| 238 |
pjsua_call *call = &pjsua_var.calls[call_id]; |
| 239 |
acc_id = call->acc_id; |
| 240 |
} |
| 241 |
|
| 242 |
if (acc_id != PJSUA_INVALID_ID) { |
| 243 |
(*pjsua_var.ua_cfg.cb.on_typing2)(call_id, from, to, &contact, |
| 244 |
is_typing, rdata, acc_id); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
} else { |
| 249 |
pj_str_t mime_type; |
| 250 |
char buf[256]; |
| 251 |
pjsip_media_type *m; |
| 252 |
pj_str_t text_body; |
| 253 |
|
| 254 |
|
| 255 |
if (body) { |
| 256 |
text_body.ptr = (char*)rdata->msg_info.msg->body->data; |
| 257 |
text_body.slen = rdata->msg_info.msg->body->len; |
| 258 |
|
| 259 |
|
| 260 |
m = &rdata->msg_info.msg->body->content_type; |
| 261 |
mime_type.ptr = buf; |
| 262 |
mime_type.slen = pj_ansi_snprintf(buf, sizeof(buf), |
| 263 |
"%.*s/%.*s", |
| 264 |
(int)m->type.slen, |
| 265 |
m->type.ptr, |
| 266 |
(int)m->subtype.slen, |
| 267 |
m->subtype.ptr); |
| 268 |
if (mime_type.slen < 1) |
| 269 |
mime_type.slen = 0; |
| 270 |
|
| 271 |
|
| 272 |
} else { |
| 273 |
text_body.ptr = mime_type.ptr = ""; |
| 274 |
text_body.slen = mime_type.slen = 0; |
| 275 |
} |
| 276 |
|
| 277 |
if (pjsua_var.ua_cfg.cb.on_pager) { |
| 278 |
(*pjsua_var.ua_cfg.cb.on_pager)(call_id, from, to, &contact, |
| 279 |
&mime_type, &text_body); |
| 280 |
} |
| 281 |
|
| 282 |
if (pjsua_var.ua_cfg.cb.on_pager2) { |
| 283 |
pjsua_acc_id acc_id; |
| 284 |
|
| 285 |
if (call_id == PJSUA_INVALID_ID) { |
| 286 |
acc_id = pjsua_acc_find_for_incoming(rdata); |
| 287 |
} else { |
| 288 |
pjsua_call *call = &pjsua_var.calls[call_id]; |
| 289 |
acc_id = call->acc_id; |
| 290 |
} |
| 291 |
|
| 292 |
if (acc_id != PJSUA_INVALID_ID) { |
| 293 |
(*pjsua_var.ua_cfg.cb.on_pager2)(call_id, from, to, &contact, |
| 294 |
&mime_type, &text_body, rdata, |
| 295 |
acc_id); |
| 296 |
} |
| 297 |
} |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
static pj_bool_t im_on_rx_request(pjsip_rx_data *rdata) |
| 306 |
{ |
| 307 |
pjsua_acc_id acc_id = PJSUA_INVALID_ID; |
| 308 |
pj_str_t from, to; |
| 309 |
pjsip_accept_hdr *accept_hdr; |
| 310 |
pjsip_msg *msg; |
| 311 |
|
| 312 |
msg = rdata->msg_info.msg; |
| 313 |
|
| 314 |
|
| 315 |
if (pjsip_method_cmp(&msg->line.req.method, &pjsip_message_method) != 0) { |
| 316 |
return PJ_FALSE; |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
PJ_ASSERT_RETURN(pjsip_rdata_get_tsx(rdata)==NULL, PJ_FALSE); |
| 322 |
|
| 323 |
|
| 324 |
PJ_ASSERT_RETURN(pjsip_rdata_get_dlg(rdata)==NULL, PJ_FALSE); |
| 325 |
|
| 326 |
|
| 327 |
if (!pjsua_im_accept_pager(rdata, &accept_hdr)) { |
| 328 |
pjsip_hdr hdr_list; |
| 329 |
|
| 330 |
pj_list_init(&hdr_list); |
| 331 |
pj_list_push_back(&hdr_list, accept_hdr); |
| 332 |
|
| 333 |
pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, |
| 334 |
PJSIP_SC_NOT_ACCEPTABLE_HERE, NULL, |
| 335 |
&hdr_list, NULL); |
| 336 |
return PJ_TRUE; |
| 337 |
} |
| 338 |
|
| 339 |
acc_id = pjsua_acc_find_for_incoming(rdata); |
| 340 |
|
| 341 |
if (acc_id != PJSUA_INVALID_ID && |
| 342 |
pjsua_var.acc[acc_id].cfg.auto_repond_sip_message == PJ_FALSE && |
| 343 |
is_typing_indication(rdata) == PJ_FALSE) { |
| 344 |
|
| 345 |
pjsip_transaction *tsx; |
| 346 |
pj_status_t status; |
| 347 |
|
| 348 |
status = pjsip_tsx_create_uas(&mod_pjsua_im, rdata, &tsx); |
| 349 |
if (status != PJ_SUCCESS) { |
| 350 |
|
| 351 |
pjsip_endpt_respond(pjsua_var.endpt, NULL, rdata, 500, NULL, |
| 352 |
NULL, NULL, NULL); |
| 353 |
return PJ_TRUE; |
| 354 |
} |
| 355 |
|
| 356 |
pjsip_tsx_recv_msg(tsx, rdata); |
| 357 |
} |
| 358 |
else |
| 359 |
{ |
| 360 |
|
| 361 |
pjsip_endpt_respond( pjsua_var.endpt, NULL, rdata, 200, NULL, |
| 362 |
NULL, NULL, NULL); |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
from.ptr = (char*)pj_pool_alloc(rdata->tp_info.pool, PJSIP_MAX_URL_SIZE); |
| 369 |
from.slen = pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, |
| 370 |
rdata->msg_info.from->uri, |
| 371 |
from.ptr, PJSIP_MAX_URL_SIZE); |
| 372 |
|
| 373 |
if (from.slen < 1) |
| 374 |
from = pj_str("<--URI is too long-->"); |
| 375 |
|
| 376 |
|
| 377 |
to.ptr = (char*) pj_pool_alloc(rdata->tp_info.pool, PJSIP_MAX_URL_SIZE); |
| 378 |
to.slen = pjsip_uri_print( PJSIP_URI_IN_FROMTO_HDR, |
| 379 |
rdata->msg_info.to->uri, |
| 380 |
to.ptr, PJSIP_MAX_URL_SIZE); |
| 381 |
if (to.slen < 1) |
| 382 |
to = pj_str("<--URI is too long-->"); |
| 383 |
|
| 384 |
|
| 385 |
pjsua_im_process_pager(PJSUA_INVALID_ID, &from, &to, rdata); |
| 386 |
|
| 387 |
|
| 388 |
return PJ_TRUE; |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
static pj_status_t im_async_auth_send_impl( |
| 393 |
pjsip_auth_clt_sess *auth_sess, |
| 394 |
void *user_data, |
| 395 |
pjsip_tx_data *tdata) |
| 396 |
{ |
| 397 |
im_auth_ctx *ctx = (im_auth_ctx *)user_data; |
| 398 |
pjsip_endpoint *endpt = pjsua_var.endpt; |
| 399 |
pjsip_endpt_send_callback send_cb; |
| 400 |
pjsua_im_data *im_data; |
| 401 |
pjsua_im_data *im_data2; |
| 402 |
|
| 403 |
PJ_UNUSED_ARG(auth_sess); |
| 404 |
|
| 405 |
send_cb = ctx->send_cb; |
| 406 |
im_data = ctx->im_data; |
| 407 |
|
| 408 |
im_data2 = pjsua_im_data_dup(tdata->pool, im_data); |
| 409 |
PJSIP_MSG_CSEQ_HDR(tdata->msg)->cseq++; |
| 410 |
|
| 411 |
|
| 412 |
pjsip_auth_clt_deinit(&ctx->auth); |
| 413 |
|
| 414 |
return pjsip_endpt_send_request(endpt, tdata, -1, im_data2, send_cb); |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
static void im_async_auth_abandon_impl(pjsip_auth_clt_sess *auth_sess, |
| 419 |
void *user_data) |
| 420 |
{ |
| 421 |
im_auth_ctx *ctx = (im_auth_ctx *)user_data; |
| 422 |
|
| 423 |
PJ_UNUSED_ARG(auth_sess); |
| 424 |
|
| 425 |
|
| 426 |
pjsip_auth_clt_deinit(&ctx->auth); |
| 427 |
} |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
static void im_callback(void *token, pjsip_event *e) |
| 432 |
{ |
| 433 |
pjsua_im_data *im_data = (pjsua_im_data*) token; |
| 434 |
|
| 435 |
if (e->type == PJSIP_EVENT_TSX_STATE) { |
| 436 |
|
| 437 |
pjsip_transaction *tsx = e->body.tsx_state.tsx; |
| 438 |
|
| 439 |
|
| 440 |
if (tsx->status_code < 200) |
| 441 |
return; |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG && |
| 446 |
(tsx->status_code == 401 || tsx->status_code == 407)) |
| 447 |
{ |
| 448 |
pjsip_rx_data *rdata = e->body.tsx_state.src.rdata; |
| 449 |
pjsip_tx_data *tdata; |
| 450 |
pjsip_auth_clt_async_on_chal_param chal_param = { 0 }; |
| 451 |
im_auth_ctx *ctx; |
| 452 |
pjsua_im_data *im_data2; |
| 453 |
pj_status_t status; |
| 454 |
|
| 455 |
PJ_LOG(4,(THIS_FILE, "Resending IM with authentication")); |
| 456 |
|
| 457 |
ctx = PJ_POOL_ZALLOC_T(tsx->pool, im_auth_ctx); |
| 458 |
ctx->im_data = im_data; |
| 459 |
ctx->send_cb = &im_callback; |
| 460 |
|
| 461 |
pjsip_auth_clt_init(&ctx->auth, pjsua_var.endpt, tsx->pool, 0); |
| 462 |
|
| 463 |
|
| 464 |
if (pjsua_var.acc[im_data->acc_id].cfg.use_shared_auth) { |
| 465 |
pjsip_auth_clt_set_parent(&ctx->auth, |
| 466 |
&pjsua_var.acc[im_data->acc_id].shared_auth_sess); |
| 467 |
} else if (pjsua_var.ua_cfg.cb.on_auth_challenge) { |
| 468 |
pjsip_auth_clt_async_setting async_opt; |
| 469 |
pj_bzero(&async_opt, sizeof(async_opt)); |
| 470 |
async_opt.cb = &pjsua_auth_on_challenge; |
| 471 |
async_opt.user_data = (void*)(pj_ssize_t)im_data->acc_id; |
| 472 |
pjsip_auth_clt_async_configure(&ctx->auth, &async_opt); |
| 473 |
} |
| 474 |
|
| 475 |
pjsip_auth_clt_set_credentials(&ctx->auth, |
| 476 |
pjsua_var.acc[im_data->acc_id].cred_cnt, |
| 477 |
pjsua_var.acc[im_data->acc_id].cred); |
| 478 |
pjsip_auth_clt_set_prefs(&ctx->auth, |
| 479 |
&pjsua_var.acc[im_data->acc_id].cfg.auth_pref); |
| 480 |
|
| 481 |
ctx->token.user_data = ctx; |
| 482 |
ctx->token.send_impl = &im_async_auth_send_impl; |
| 483 |
ctx->token.abandon_impl = &im_async_auth_abandon_impl; |
| 484 |
ctx->token.grp_lock = tsx->grp_lock; |
| 485 |
pj_grp_lock_add_ref(tsx->grp_lock); |
| 486 |
|
| 487 |
pj_bzero(&chal_param, sizeof(chal_param)); |
| 488 |
chal_param.rdata = rdata; |
| 489 |
chal_param.tdata = tsx->last_tx; |
| 490 |
status = pjsip_auth_clt_async_impl_on_challenge( |
| 491 |
&ctx->auth, &ctx->token, |
| 492 |
&chal_param); |
| 493 |
if (status == PJ_SUCCESS) { |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
return; |
| 498 |
} |
| 499 |
|
| 500 |
ll |
| 501 |
|
| 502 |
|
| 503 |
pj_grp_lock_dec_ref(tsx->grp_lock); |
| 504 |
|
| 505 |
|
| 506 |
status = pjsip_auth_clt_reinit_req(&ctx->auth, rdata, |
| 507 |
tsx->last_tx, &tdata); |
| 508 |
if (status == PJ_SUCCESS) { |
| 509 |
|
| 510 |
im_data2 = pjsua_im_data_dup(tdata->pool, im_data); |
| 511 |
|
| 512 |
|
| 513 |
PJSIP_MSG_CSEQ_HDR(tdata->msg)->cseq++; |
| 514 |
|
| 515 |
|
| 516 |
status = pjsip_endpt_send_request(pjsua_var.endpt, tdata, |
| 517 |
-1, im_data2, |
| 518 |
&im_callback); |
| 519 |
pjsip_auth_clt_deinit(&ctx->auth); |
| 520 |
if (status == PJ_SUCCESS) |
| 521 |
return; |
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
if (im_data2->acc_id == PJSUA_INVALID_ID) |
| 527 |
return; |
| 528 |
} else { |
| 529 |
pjsip_auth_clt_deinit(&ctx->auth); |
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
if (tsx->status_code/100 == 2) { |
| 534 |
PJ_LOG(4,(THIS_FILE, |
| 535 |
"Message \'%s\' delivered successfully", |
| 536 |
im_data->body.ptr)); |
| 537 |
} else { |
| 538 |
PJ_LOG(3,(THIS_FILE, |
| 539 |
"Failed to deliver message \'%s\': %d/%.*s", |
| 540 |
im_data->body.ptr, |
| 541 |
tsx->status_code, |
| 542 |
(int)tsx->status_text.slen, |
| 543 |
tsx->status_text.ptr)); |
| 544 |
} |
| 545 |
|
| 546 |
if (pjsua_var.ua_cfg.cb.on_pager_status) { |
| 547 |
pj_str_t im_body = im_data->body; |
| 548 |
if (im_body.slen==0) { |
| 549 |
pjsip_msg_body *body = tsx->last_tx->msg->body; |
| 550 |
pj_strset(&im_body, body->data, body->len); |
| 551 |
} |
| 552 |
|
| 553 |
pjsua_var.ua_cfg.cb.on_pager_status(im_data->call_id, |
| 554 |
&im_data->to, |
| 555 |
&im_body, |
| 556 |
im_data->user_data, |
| 557 |
(pjsip_status_code) |
| 558 |
tsx->status_code, |
| 559 |
&tsx->status_text); |
| 560 |
} |
| 561 |
|
| 562 |
if (pjsua_var.ua_cfg.cb.on_pager_status2) { |
| 563 |
pjsip_rx_data *rdata; |
| 564 |
pj_str_t im_body = im_data->body; |
| 565 |
|
| 566 |
if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) |
| 567 |
rdata = e->body.tsx_state.src.rdata; |
| 568 |
else |
| 569 |
rdata = NULL; |
| 570 |
|
| 571 |
if (im_body.slen==0) { |
| 572 |
pjsip_msg_body *body = tsx->last_tx->msg->body; |
| 573 |
pj_strset(&im_body, body->data, body->len); |
| 574 |
} |
| 575 |
|
| 576 |
pjsua_var.ua_cfg.cb.on_pager_status2(im_data->call_id, |
| 577 |
&im_data->to, |
| 578 |
&im_body, |
| 579 |
im_data->user_data, |
| 580 |
(pjsip_status_code) |
| 581 |
tsx->status_code, |
| 582 |
&tsx->status_text, |
| 583 |
tsx->last_tx, |
| 584 |
rdata, im_data->acc_id); |
| 585 |
} |
| 586 |
|
| 587 |
|
| 588 |
im_data->acc_id = PJSUA_INVALID_ID; |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
static void typing_callback(void *token, pjsip_event *e) |
| 597 |
{ |
| 598 |
pjsua_im_data *im_data = (pjsua_im_data*) token; |
| 599 |
|
| 600 |
if (e->type == PJSIP_EVENT_TSX_STATE) { |
| 601 |
|
| 602 |
pjsip_transaction *tsx = e->body.tsx_state.tsx; |
| 603 |
|
| 604 |
|
| 605 |
if (tsx->status_code < 200) |
| 606 |
return; |
| 607 |
|
| 608 |
|
| 609 |
if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG && |
| 610 |
(tsx->status_code == 401 || tsx->status_code == 407)) |
| 611 |
{ |
| 612 |
pjsip_rx_data *rdata = e->body.tsx_state.src.rdata; |
| 613 |
pjsip_tx_data *tdata; |
| 614 |
pjsip_auth_clt_async_on_chal_param chal_param = { 0 }; |
| 615 |
im_auth_ctx *ctx; |
| 616 |
pj_status_t status; |
| 617 |
|
| 618 |
PJ_LOG(4,(THIS_FILE, "Resending IM with authentication")); |
| 619 |
|
| 620 |
ctx = PJ_POOL_ZALLOC_T(tsx->pool, im_auth_ctx); |
| 621 |
ctx->im_data = im_data; |
| 622 |
ctx->send_cb = &typing_callback; |
| 623 |
|
| 624 |
pjsip_auth_clt_init(&ctx->auth, pjsua_var.endpt, tsx->pool, 0); |
| 625 |
|
| 626 |
|
| 627 |
if (pjsua_var.acc[im_data->acc_id].cfg.use_shared_auth) { |
| 628 |
pjsip_auth_clt_set_parent(&ctx->auth, |
| 629 |
&pjsua_var.acc[im_data->acc_id].shared_auth_sess); |
| 630 |
} else if (pjsua_var.ua_cfg.cb.on_auth_challenge) { |
| 631 |
pjsip_auth_clt_async_setting async_opt; |
| 632 |
pj_bzero(&async_opt, sizeof(async_opt)); |
| 633 |
async_opt.cb = &pjsua_auth_on_challenge; |
| 634 |
async_opt.user_data = (void*)(pj_ssize_t)im_data->acc_id; |
| 635 |
pjsip_auth_clt_async_configure(&ctx->auth, &async_opt); |
| 636 |
} |
| 637 |
|
| 638 |
pjsip_auth_clt_set_credentials(&ctx->auth, |
| 639 |
pjsua_var.acc[im_data->acc_id].cred_cnt, |
| 640 |
pjsua_var.acc[im_data->acc_id].cred); |
| 641 |
pjsip_auth_clt_set_prefs(&ctx->auth, |
| 642 |
&pjsua_var.acc[im_data->acc_id].cfg.auth_pref); |
| 643 |
|
| 644 |
ctx->token.user_data = ctx; |
| 645 |
ctx->token.send_impl = &im_async_auth_send_impl; |
| 646 |
ctx->token.abandon_impl = &im_async_auth_abandon_impl; |
| 647 |
ctx->token.grp_lock = tsx->grp_lock; |
| 648 |
pj_grp_lock_add_ref(tsx->grp_lock); |
| 649 |
|
| 650 |
pj_bzero(&chal_param, sizeof(chal_param)); |
| 651 |
chal_param.rdata = rdata; |
| 652 |
chal_param.tdata = tsx->last_tx; |
| 653 |
status = pjsip_auth_clt_async_impl_on_challenge( |
| 654 |
&ctx->auth, &ctx->token, |
| 655 |
&chal_param); |
| 656 |
if (status == PJ_SUCCESS) { |
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
return; |
| 661 |
} |
| 662 |
|
| 663 |
ll |
| 664 |
|
| 665 |
|
| 666 |
pj_grp_lock_dec_ref(tsx->grp_lock); |
| 667 |
|
| 668 |
|
| 669 |
status = pjsip_auth_clt_reinit_req(&ctx->auth, rdata, |
| 670 |
tsx->last_tx, &tdata); |
| 671 |
if (status == PJ_SUCCESS) { |
| 672 |
pjsua_im_data *im_data2; |
| 673 |
|
| 674 |
|
| 675 |
im_data2 = pjsua_im_data_dup(tdata->pool, im_data); |
| 676 |
|
| 677 |
|
| 678 |
PJSIP_MSG_CSEQ_HDR(tdata->msg)->cseq++; |
| 679 |
|
| 680 |
|
| 681 |
status = pjsip_endpt_send_request(pjsua_var.endpt, tdata, |
| 682 |
-1, im_data2, |
| 683 |
&typing_callback); |
| 684 |
pjsip_auth_clt_deinit(&ctx->auth); |
| 685 |
if (status == PJ_SUCCESS) |
| 686 |
return; |
| 687 |
} else { |
| 688 |
pjsip_auth_clt_deinit(&ctx->auth); |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
} |
| 693 |
} |
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
PJ_DEF(pj_status_t) pjsua_im_send( pjsua_acc_id acc_id, |
| 701 |
const pj_str_t *to, |
| 702 |
const pj_str_t *mime_type, |
| 703 |
const pj_str_t *content, |
| 704 |
const pjsua_msg_data *msg_data, |
| 705 |
void *user_data) |
| 706 |
{ |
| 707 |
pjsip_tx_data *tdata; |
| 708 |
const pj_str_t mime_text_plain = pj_str("text/plain"); |
| 709 |
pjsip_media_type media_type; |
| 710 |
pjsua_im_data *im_data; |
| 711 |
pjsua_acc *acc; |
| 712 |
pj_bool_t content_in_msg_data; |
| 713 |
pjsip_tpselector tp_sel; |
| 714 |
pj_status_t status; |
| 715 |
|
| 716 |
PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc), |
| 717 |
PJ_EINVAL); |
| 718 |
|
| 719 |
content_in_msg_data = msg_data && (msg_data->msg_body.slen || |
| 720 |
msg_data->multipart_ctype.type.slen); |
| 721 |
|
| 722 |
|
| 723 |
PJ_ASSERT_RETURN(to && (content || content_in_msg_data), PJ_EINVAL); |
| 724 |
|
| 725 |
acc = &pjsua_var.acc[acc_id]; |
| 726 |
|
| 727 |
|
| 728 |
status = pjsip_endpt_create_request(pjsua_var.endpt, |
| 729 |
&pjsip_message_method, |
| 730 |
(msg_data && msg_data->target_uri.slen? |
| 731 |
&msg_data->target_uri: to), |
| 732 |
(msg_data && msg_data->local_uri.slen? |
| 733 |
&msg_data->local_uri: &acc->cfg.id), |
| 734 |
to, NULL, NULL, -1, NULL, &tdata); |
| 735 |
if (status != PJ_SUCCESS) { |
| 736 |
pjsua_perror(THIS_FILE, "Unable to create request", status); |
| 737 |
return status; |
| 738 |
} |
| 739 |
|
| 740 |
|
| 741 |
pjsua_init_tpselector(acc_id, &tp_sel); |
| 742 |
pjsip_tx_data_set_transport(tdata, &tp_sel); |
| 743 |
|
| 744 |
|
| 745 |
pjsip_msg_add_hdr( tdata->msg, |
| 746 |
(pjsip_hdr*)pjsua_im_create_accept(tdata->pool)); |
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
im_data = PJ_POOL_ZALLOC_T(tdata->pool, pjsua_im_data); |
| 776 |
im_data->acc_id = acc_id; |
| 777 |
im_data->call_id = PJSUA_INVALID_ID; |
| 778 |
pj_strdup_with_null(tdata->pool, &im_data->to, to); |
| 779 |
im_data->user_data = user_data; |
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
if (content) { |
| 784 |
pj_strdup_with_null(tdata->pool, &im_data->body, content); |
| 785 |
|
| 786 |
|
| 787 |
if (mime_type == NULL) { |
| 788 |
mime_type = &mime_text_plain; |
| 789 |
} |
| 790 |
|
| 791 |
|
| 792 |
pjsua_parse_media_type(tdata->pool, mime_type, &media_type); |
| 793 |
|
| 794 |
|
| 795 |
tdata->msg->body = pjsip_msg_body_create( tdata->pool, &media_type.type, |
| 796 |
&media_type.subtype, |
| 797 |
&im_data->body); |
| 798 |
if (tdata->msg->body == NULL) { |
| 799 |
pjsua_perror(THIS_FILE, "Unable to create msg body", PJ_ENOMEM); |
| 800 |
pjsip_tx_data_dec_ref(tdata); |
| 801 |
return PJ_ENOMEM; |
| 802 |
} |
| 803 |
} |
| 804 |
|
| 805 |
|
| 806 |
pjsua_process_msg_data(tdata, msg_data); |
| 807 |
|
| 808 |
|
| 809 |
pjsua_set_msg_route_set(tdata, &acc->route_set); |
| 810 |
|
| 811 |
|
| 812 |
if (acc->cfg.allow_via_rewrite && acc->via_addr.host.slen > 0) { |
| 813 |
tdata->via_addr = acc->via_addr; |
| 814 |
tdata->via_tp = acc->via_tp; |
| 815 |
} |
| 816 |
|
| 817 |
|
| 818 |
status = pjsip_endpt_send_request( pjsua_var.endpt, tdata, -1, |
| 819 |
im_data, &im_callback); |
| 820 |
if (status != PJ_SUCCESS) { |
| 821 |
pjsua_perror(THIS_FILE, "Unable to send request", status); |
| 822 |
return status; |
| 823 |
} |
| 824 |
|
| 825 |
return PJ_SUCCESS; |
| 826 |
} |
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
PJ_DEF(pj_status_t) pjsua_im_typing( pjsua_acc_id acc_id, |
| 833 |
const pj_str_t *to, |
| 834 |
pj_bool_t is_typing, |
| 835 |
const pjsua_msg_data *msg_data) |
| 836 |
{ |
| 837 |
pjsua_im_data *im_data; |
| 838 |
pjsip_tx_data *tdata; |
| 839 |
pjsua_acc *acc; |
| 840 |
pjsip_tpselector tp_sel; |
| 841 |
pj_status_t status; |
| 842 |
|
| 843 |
PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc), |
| 844 |
PJ_EINVAL); |
| 845 |
|
| 846 |
acc = &pjsua_var.acc[acc_id]; |
| 847 |
|
| 848 |
|
| 849 |
status = pjsip_endpt_create_request( pjsua_var.endpt, &pjsip_message_method, |
| 850 |
to, &acc->cfg.id, |
| 851 |
to, NULL, NULL, -1, NULL, &tdata); |
| 852 |
if (status != PJ_SUCCESS) { |
| 853 |
pjsua_perror(THIS_FILE, "Unable to create request", status); |
| 854 |
return status; |
| 855 |
} |
| 856 |
|
| 857 |
|
| 858 |
pjsua_init_tpselector(acc_id, &tp_sel); |
| 859 |
pjsip_tx_data_set_transport(tdata, &tp_sel); |
| 860 |
|
| 861 |
|
| 862 |
pjsip_msg_add_hdr( tdata->msg, |
| 863 |
(pjsip_hdr*)pjsua_im_create_accept(tdata->pool)); |
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
tdata->msg->body = pjsip_iscomposing_create_body( tdata->pool, is_typing, |
| 892 |
NULL, NULL, -1); |
| 893 |
|
| 894 |
|
| 895 |
pjsua_process_msg_data(tdata, msg_data); |
| 896 |
|
| 897 |
|
| 898 |
pjsua_set_msg_route_set(tdata, &acc->route_set); |
| 899 |
|
| 900 |
|
| 901 |
if (acc->cfg.allow_via_rewrite && acc->via_addr.host.slen > 0) { |
| 902 |
tdata->via_addr = acc->via_addr; |
| 903 |
tdata->via_tp = acc->via_tp; |
| 904 |
} |
| 905 |
|
| 906 |
|
| 907 |
im_data = PJ_POOL_ZALLOC_T(tdata->pool, pjsua_im_data); |
| 908 |
im_data->acc_id = acc_id; |
| 909 |
|
| 910 |
|
| 911 |
status = pjsip_endpt_send_request( pjsua_var.endpt, tdata, -1, |
| 912 |
im_data, &typing_callback); |
| 913 |
if (status != PJ_SUCCESS) { |
| 914 |
pjsua_perror(THIS_FILE, "Unable to send request", status); |
| 915 |
return status; |
| 916 |
} |
| 917 |
|
| 918 |
return PJ_SUCCESS; |
| 919 |
} |
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
pj_status_t pjsua_im_init(void) |
| 926 |
{ |
| 927 |
const pj_str_t msg_tag = { "MESSAGE", 7 }; |
| 928 |
const pj_str_t STR_MIME_TEXT_PLAIN = { "text/plain", 10 }; |
| 929 |
const pj_str_t STR_MIME_APP_ISCOMPOSING = |
| 930 |
{ "application/im-iscomposing+xml", 30 }; |
| 931 |
pj_status_t status; |
| 932 |
|
| 933 |
|
| 934 |
status = pjsip_endpt_register_module(pjsua_var.endpt, &mod_pjsua_im); |
| 935 |
if (status != PJ_SUCCESS) |
| 936 |
return status; |
| 937 |
|
| 938 |
|
| 939 |
pjsip_endpt_add_capability( pjsua_var.endpt, &mod_pjsua_im, PJSIP_H_ALLOW, |
| 940 |
NULL, 1, &msg_tag); |
| 941 |
|
| 942 |
|
| 943 |
pjsip_endpt_add_capability( pjsua_var.endpt, &mod_pjsua_im, PJSIP_H_ACCEPT, |
| 944 |
NULL, 1, &STR_MIME_APP_ISCOMPOSING); |
| 945 |
|
| 946 |
|
| 947 |
pjsip_endpt_add_capability( pjsua_var.endpt, &mod_pjsua_im, PJSIP_H_ACCEPT, |
| 948 |
NULL, 1, &STR_MIME_TEXT_PLAIN); |
| 949 |
|
| 950 |
return PJ_SUCCESS; |
| 951 |
} |
| 952 |
|