static int pidf_generate_body_content(void *body, void *data) { pjpidf_tuple *tuple; pj_str_t note, id, contact, priority; char *statestring = NULL, *pidfstate = NULL, *pidfnote = NULL; enum ast_sip_pidf_state local_state; char sanitized[PJSIP_MAX_URL_SIZE]; pjpidf_pres *pres = body; struct ast_sip_exten_state_data *state_data = data; ast_sip_presence_exten_state_to_str(state_data->exten_state, &statestring, &pidfstate, &pidfnote, &local_state); if (!pjpidf_pres_add_note(state_data->pool, pres, pj_cstr(¬e, pidfnote))) { ast_log(LOG_WARNING, "Unable to add note to PIDF presence\n"); return -1; } if (!(tuple = pjpidf_pres_add_tuple(state_data->pool, pres, pj_cstr(&id, state_data->exten)))) { ast_log(LOG_WARNING, "Unable to create PIDF tuple\n"); return -1; } ast_sip_sanitize_xml(state_data->remote, sanitized, sizeof(sanitized)); pjpidf_tuple_set_contact(state_data->pool, tuple, pj_cstr(&contact, sanitized)); pjpidf_tuple_set_contact_prio(state_data->pool, tuple, pj_cstr(&priority, "1")); pjpidf_status_set_basic_open(pjpidf_tuple_get_status(tuple), local_state == NOTIFY_OPEN); return 0; }
/* * This is a utility function to create PIDF message body from PJSIP * presence status (pjsip_pres_status). */ PJ_DEF(pj_status_t) pjsip_pres_create_pidf( pj_pool_t *pool, const pjsip_pres_status *status, const pj_str_t *entity, pjsip_msg_body **p_body ) { pjpidf_pres *pidf; pjsip_msg_body *body; unsigned i; /* Create <presence>. */ pidf = pjpidf_create(pool, entity); /* Create <tuple> */ for (i=0; i<status->info_cnt; ++i) { pjpidf_tuple *pidf_tuple; pjpidf_status *pidf_status; pj_str_t id; /* Add tuple id. */ if (status->info[i].id.slen == 0) { pj_create_unique_string(pool, &id); } else { id = status->info[i].id; } pidf_tuple = pjpidf_pres_add_tuple(pool, pidf, &id); /* Set <contact> */ if (status->info[i].contact.slen) pjpidf_tuple_set_contact(pool, pidf_tuple, &status->info[i].contact); /* Set basic status */ pidf_status = pjpidf_tuple_get_status(pidf_tuple); pjpidf_status_set_basic_open(pidf_status, status->info[i].basic_open); } /* Create <person> (RPID) */ if (status->info_cnt) { pjrpid_add_element(pidf, pool, 0, &status->info[0].rpid); } body = PJ_POOL_ZALLOC_T(pool, pjsip_msg_body); body->data = pidf; body->content_type.type = STR_APPLICATION; body->content_type.subtype = STR_PIDF_XML; body->print_body = &pres_print_body; body->clone_data = &xml_clone_data; *p_body = body; return PJ_SUCCESS; }
/* * This is a utility function to parse PIDF body into PJSIP presence status. */ PJ_DEF(pj_status_t) pjsip_pres_parse_pidf( pjsip_rx_data *rdata, pj_pool_t *pool, pjsip_pres_status *pres_status) { pjpidf_pres *pidf; pjpidf_tuple *pidf_tuple; pidf = pjpidf_parse(rdata->tp_info.pool, (char*)rdata->msg_info.msg->body->data, rdata->msg_info.msg->body->len); if (pidf == NULL) return PJSIP_SIMPLE_EBADPIDF; pres_status->info_cnt = 0; pidf_tuple = pjpidf_pres_get_first_tuple(pidf); while (pidf_tuple && pres_status->info_cnt < PJSIP_PRES_STATUS_MAX_INFO) { pjpidf_status *pidf_status; pres_status->info[pres_status->info_cnt].tuple_node = pj_xml_clone(pool, pidf_tuple); pj_strdup(pool, &pres_status->info[pres_status->info_cnt].id, pjpidf_tuple_get_id(pidf_tuple)); pj_strdup(pool, &pres_status->info[pres_status->info_cnt].contact, pjpidf_tuple_get_contact(pidf_tuple)); pidf_status = pjpidf_tuple_get_status(pidf_tuple); if (pidf_status) { pres_status->info[pres_status->info_cnt].basic_open = pjpidf_status_is_basic_open(pidf_status); } else { pres_status->info[pres_status->info_cnt].basic_open = PJ_FALSE; } pidf_tuple = pjpidf_pres_get_next_tuple( pidf, pidf_tuple ); pres_status->info_cnt++; } /* Parse <person> (RPID) */ pjrpid_get_element(pidf, pool, &pres_status->info[0].rpid); return PJ_SUCCESS; }
/* * This is a utility function to create PIDF message body from PJSIP * presence status (pjsip_pres_status). */ PJ_DEF(pj_status_t) pjsip_pres_create_pidf( pj_pool_t *pool, const pjsip_pres_status *status, const pj_str_t *entity, pjsip_msg_body **p_body ) { pjpidf_pres *pidf; pjsip_msg_body *body; unsigned i; /* Create <presence>. */ pidf = pjpidf_create(pool, entity); /* Create <tuple> */ for (i=0; i<status->info_cnt; ++i) { pjpidf_tuple *pidf_tuple; pjpidf_status *pidf_status; pj_str_t id; /* Add tuple id. */ if (status->info[i].id.slen == 0) { /* xs:ID must start with letter */ //pj_create_unique_string(pool, &id); id.ptr = (char*)pj_pool_alloc(pool, PJ_GUID_STRING_LENGTH+2); id.ptr += 2; pj_generate_unique_string(&id); id.ptr -= 2; id.ptr[0] = 'p'; id.ptr[1] = 'j'; id.slen += 2; } else { id = status->info[i].id; } pidf_tuple = pjpidf_pres_add_tuple(pool, pidf, &id); /* Set <contact> */ if (status->info[i].contact.slen) pjpidf_tuple_set_contact(pool, pidf_tuple, &status->info[i].contact); /* Set basic status */ pidf_status = pjpidf_tuple_get_status(pidf_tuple); pjpidf_status_set_basic_open(pidf_status, status->info[i].basic_open); /* Add <timestamp> if configured */ #if defined(PJSIP_PRES_PIDF_ADD_TIMESTAMP) && PJSIP_PRES_PIDF_ADD_TIMESTAMP if (PJSIP_PRES_PIDF_ADD_TIMESTAMP) { char buf[50]; int tslen = 0; pj_time_val tv; pj_parsed_time pt; pj_gettimeofday(&tv); /* TODO: convert time to GMT! (unsupported by pjlib) */ pj_time_decode( &tv, &pt); tslen = pj_ansi_snprintf(buf, sizeof(buf), "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", pt.year, pt.mon+1, pt.day, pt.hour, pt.min, pt.sec, pt.msec); if (tslen > 0 && tslen < sizeof(buf)) { pj_str_t time = pj_str(buf); pjpidf_tuple_set_timestamp(pool, pidf_tuple, &time); } } #endif } /* Create <person> (RPID) */ if (status->info_cnt) { pjrpid_add_element(pidf, pool, 0, &status->info[0].rpid); } body = PJ_POOL_ZALLOC_T(pool, pjsip_msg_body); body->data = pidf; body->content_type.type = STR_APPLICATION; body->content_type.subtype = STR_PIDF_XML; body->print_body = &pres_print_body; body->clone_data = &xml_clone_data; *p_body = body; return PJ_SUCCESS; }