mirror of https://github.com/asterisk/asterisk
When the PJSIP pubsub framework was created, subscription handlers were required to state what event they handled along with what body types they knew how to generate. While this serves well when implementing a base RFC, it has problems when trying to extend the body to support non-standard or proprietary body elements. The code also was NOTIFY-specific, meaning that when the time comes that we start writing code to send out PUBLISH requests with MWI or presence bodies, we would likely find ourselves duplicating code that had previously been written. This changeset introduces the concept of body generators and body supplements. A body generator is responsible for allocating a native structure for a given body type, providing the primary body content, converting the native structure to a string, and deallocating resources. A body supplement takes the primary body content (the native structure, not a string) generated by the body generator and adds nonstandard elements to the body. With these elements living in their own module, it becomes easy to extend our support for body types and to re-use resources when sending a PUBLISH request. Body generators and body supplements register themselves with the pubsub core, similar to how subscription and publish handlers had done. Now, subscription handlers do not need to know what type of body content they generate, but they still need to inform the pubsub core about what the default body type for a given event package is. The pubsub core keeps track of what body generators and body supplements have been registered. When a SUBSCRIBE arrives, the pubsub core will check that there is a subscription handler for the event in the SUBSCRIBE, then it will check that there is a body generator that can provide the content specified in the Accept header(s). Because of the nature of body generators and supplements, it means res_pjsip_exten_state and res_pjsip_mwi have been completely gutted. They no longer worry about body types, instead calling ast_sip_pubsub_generate_body_content() when they need to generate a NOTIFY body. Review: https://reviewboard.asterisk.org/r/3150 ........ Merged revisions 407016 from http://svn.asterisk.org/svn/asterisk/branches/12 git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@407030 65c4cc65-6c06-0410-ace0-fbb531ad65f3changes/97/197/1
parent
e29c5e0c5c
commit
f55abe9cf1
@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Asterisk -- An open source telephony toolkit.
|
||||
*
|
||||
* Copyright (C) 2014, Digium, Inc.
|
||||
*
|
||||
* Mark Michelson <mmichelson@digium.com>
|
||||
*
|
||||
* See http://www.asterisk.org for more information about
|
||||
* the Asterisk project. Please do not directly contact
|
||||
* any of the maintainers of this project for assistance;
|
||||
* the project provides a web site, mailing lists and IRC
|
||||
* channels for your use.
|
||||
*
|
||||
* This program is free software, distributed under the terms of
|
||||
* the GNU General Public License Version 2. See the LICENSE file
|
||||
* at the top of the source tree.
|
||||
*/
|
||||
|
||||
#ifndef _RES_PJSIP_BODY_GENERATOR_TYPES_H
|
||||
#define _RES_PJSIP_BODY_GENERATOR_TYPES_H
|
||||
|
||||
#include "asterisk/pbx.h"
|
||||
|
||||
/*!
|
||||
* \brief structure used for presence XML bodies
|
||||
*
|
||||
* This is used for the following body types:
|
||||
* \li application/pidf+xml
|
||||
* \li application/xpidf+xml
|
||||
* \li application/cpim-pidf+xml
|
||||
*/
|
||||
struct ast_sip_exten_state_data {
|
||||
/*! The extension of the current state change */
|
||||
const char *exten;
|
||||
/*! The extension state of the change */
|
||||
enum ast_extension_states exten_state;
|
||||
/*! The presence state of the change */
|
||||
enum ast_presence_state presence_state;
|
||||
/*! Current device state information */
|
||||
struct ao2_container *device_state_info;
|
||||
/*! Local dialog URI */
|
||||
char local[PJSIP_MAX_URL_SIZE];
|
||||
/*! Remote dialog URI */
|
||||
char remote[PJSIP_MAX_URL_SIZE];
|
||||
/*! Allocation pool */
|
||||
pj_pool_t *pool;
|
||||
};
|
||||
|
||||
/*!
|
||||
* \brief Message counter used for message-summary XML bodies
|
||||
*
|
||||
* This is used for application/simple-message-summary bodies.
|
||||
*/
|
||||
struct ast_sip_message_accumulator {
|
||||
/*! Number of old messages */
|
||||
int old_msgs;
|
||||
/*! Number of new messages */
|
||||
int new_msgs;
|
||||
};
|
||||
|
||||
#endif /* _RES_PJSIP_BODY_GENERATOR_TYPES_H */
|
@ -1,94 +0,0 @@
|
||||
/*
|
||||
* Asterisk -- An open source telephony toolkit.
|
||||
*
|
||||
* Copyright (C) 2013, Digium, Inc.
|
||||
*
|
||||
* Kevin Harwell <kharwell@digium.com>
|
||||
*
|
||||
* See http://www.asterisk.org for more information about
|
||||
* the Asterisk project. Please do not directly contact
|
||||
* any of the maintainers of this project for assistance;
|
||||
* the project provides a web site, mailing lists and IRC
|
||||
* channels for your use.
|
||||
*
|
||||
* This program is free software, distributed under the terms of
|
||||
* the GNU General Public License Version 2. See the LICENSE file
|
||||
* at the top of the source tree.
|
||||
*/
|
||||
|
||||
#ifndef _RES_PJSIP_EXTEN_STATE_H
|
||||
#define _RES_PJSIP_EXTEN_STATE_H
|
||||
|
||||
#include "asterisk/stringfields.h"
|
||||
#include "asterisk/linkedlists.h"
|
||||
|
||||
#include "asterisk/pbx.h"
|
||||
#include "asterisk/presencestate.h"
|
||||
|
||||
|
||||
/*!
|
||||
* \brief Contains information pertaining to extension/device state changes.
|
||||
*/
|
||||
struct ast_sip_exten_state_data {
|
||||
/*! The extension of the current state change */
|
||||
const char *exten;
|
||||
/*! The extension state of the change */
|
||||
enum ast_extension_states exten_state;
|
||||
/*! The presence state of the change */
|
||||
enum ast_presence_state presence_state;
|
||||
/*! Current device state information */
|
||||
struct ao2_container *device_state_info;
|
||||
};
|
||||
|
||||
/*!
|
||||
* \brief Extension state provider.
|
||||
*/
|
||||
struct ast_sip_exten_state_provider {
|
||||
/*! The name of the event this provider registers for */
|
||||
const char *event_name;
|
||||
/*! Type of the body, ex: "application" */
|
||||
const char *type;
|
||||
/*! Subtype of the body, ex: "pidf+xml" */
|
||||
const char *subtype;
|
||||
/*! Type/Subtype together - ex: application/pidf+xml */
|
||||
const char *body_type;
|
||||
/*! Subscription handler to be used and associated with provider */
|
||||
struct ast_sip_subscription_handler *handler;
|
||||
|
||||
/*!
|
||||
* \brief Create the body text of a NOTIFY request.
|
||||
*
|
||||
* Implementors use this to create body information within the given
|
||||
* ast_str. That information is then added to the NOTIFY request.
|
||||
*
|
||||
* \param data Current extension state changes
|
||||
* \param local URI of the dialog's local party, e.g. 'from'
|
||||
* \param remote URI of the dialog's remote party, e.g. 'to'
|
||||
* \param body_text Out parameter used to populate the NOTIFY msg body
|
||||
* \retval 0 Successfully created the body's text
|
||||
* \retval -1 Failed to create the body's text
|
||||
*/
|
||||
int (*create_body)(struct ast_sip_exten_state_data *data, const char *local,
|
||||
const char *remote, struct ast_str **body_text);
|
||||
|
||||
/*! Next item in the list */
|
||||
AST_LIST_ENTRY(ast_sip_exten_state_provider) next;
|
||||
};
|
||||
|
||||
/*!
|
||||
* \brief Registers an extension state provider.
|
||||
*
|
||||
* \param obj An extension state provider
|
||||
* \retval 0 Successfully registered the extension state provider
|
||||
* \retval -1 Failed to register the extension state provider
|
||||
*/
|
||||
int ast_sip_register_exten_state_provider(struct ast_sip_exten_state_provider *obj);
|
||||
|
||||
/*!
|
||||
* \brief Unregisters an extension state provider.
|
||||
*
|
||||
* \param obj An extension state provider
|
||||
*/
|
||||
void ast_sip_unregister_exten_state_provider(struct ast_sip_exten_state_provider *obj);
|
||||
|
||||
#endif /* _RES_PJSIP_EXTEN_STATE_H */
|
@ -0,0 +1,166 @@
|
||||
/*
|
||||
* asterisk -- An open source telephony toolkit.
|
||||
*
|
||||
* Copyright (C) 2013, Digium, Inc.
|
||||
*
|
||||
* Kevin Harwell <kharwell@digium.com>
|
||||
*
|
||||
* See http://www.asterisk.org for more information about
|
||||
* the Asterisk project. Please do not directly contact
|
||||
* any of the maintainers of this project for assistance;
|
||||
* the project provides a web site, mailing lists and IRC
|
||||
* channels for your use.
|
||||
*
|
||||
* This program is free software, distributed under the terms of
|
||||
* the GNU General Public License Version 2. See the LICENSE file
|
||||
* at the top of the source tree.
|
||||
*/
|
||||
|
||||
/*** MODULEINFO
|
||||
<depend>pjproject</depend>
|
||||
<depend>res_pjsip</depend>
|
||||
<depend>res_pjsip_pubsub</depend>
|
||||
<depend>res_pjsip_exten_state</depend>
|
||||
<support_level>core</support_level>
|
||||
***/
|
||||
|
||||
#include "asterisk.h"
|
||||
|
||||
#include <pjsip.h>
|
||||
#include <pjsip_simple.h>
|
||||
#include <pjlib.h>
|
||||
|
||||
#include "asterisk/module.h"
|
||||
#include "asterisk/res_pjsip.h"
|
||||
#include "asterisk/res_pjsip_pubsub.h"
|
||||
#include "asterisk/res_pjsip_presence_xml.h"
|
||||
#include "asterisk/res_pjsip_body_generator_types.h"
|
||||
|
||||
void ast_sip_sanitize_xml(const char *input, char *output, size_t len)
|
||||
{
|
||||
char *copy = ast_strdupa(input);
|
||||
char *break_point;
|
||||
|
||||
output[0] = '\0';
|
||||
|
||||
while ((break_point = strpbrk(copy, "<>\"&'"))) {
|
||||
char to_escape = *break_point;
|
||||
|
||||
*break_point = '\0';
|
||||
strncat(output, copy, len);
|
||||
|
||||
switch (to_escape) {
|
||||
case '<':
|
||||
strncat(output, "<", len);
|
||||
break;
|
||||
case '>':
|
||||
strncat(output, ">", len);
|
||||
break;
|
||||
case '"':
|
||||
strncat(output, """, len);
|
||||
break;
|
||||
case '&':
|
||||
strncat(output, "&", len);
|
||||
break;
|
||||
case '\'':
|
||||
strncat(output, "'", len);
|
||||
break;
|
||||
};
|
||||
|
||||
copy = break_point + 1;
|
||||
}
|
||||
|
||||
/* Be sure to copy everything after the final bracket */
|
||||
if (*copy) {
|
||||
strncat(output, copy, len);
|
||||
}
|
||||
}
|
||||
|
||||
void ast_sip_presence_exten_state_to_str(int state, char **statestring, char **pidfstate,
|
||||
char **pidfnote, enum ast_sip_pidf_state *local_state)
|
||||
{
|
||||
switch (state) {
|
||||
case AST_EXTENSION_RINGING:
|
||||
*statestring = "early";
|
||||
*local_state = NOTIFY_INUSE;
|
||||
*pidfstate = "busy";
|
||||
*pidfnote = "Ringing";
|
||||
break;
|
||||
case AST_EXTENSION_INUSE:
|
||||
*statestring = "confirmed";
|
||||
*local_state = NOTIFY_INUSE;
|
||||
*pidfstate = "busy";
|
||||
*pidfnote = "On the phone";
|
||||
break;
|
||||
case AST_EXTENSION_BUSY:
|
||||
*statestring = "confirmed";
|
||||
*local_state = NOTIFY_CLOSED;
|
||||
*pidfstate = "busy";
|
||||
*pidfnote = "On the phone";
|
||||
break;
|
||||
case AST_EXTENSION_UNAVAILABLE:
|
||||
*statestring = "terminated";
|
||||
*local_state = NOTIFY_CLOSED;
|
||||
*pidfstate = "away";
|
||||
*pidfnote = "Unavailable";
|
||||
break;
|
||||
case AST_EXTENSION_ONHOLD:
|
||||
*statestring = "confirmed";
|
||||
*local_state = NOTIFY_CLOSED;
|
||||
*pidfstate = "busy";
|
||||
*pidfnote = "On hold";
|
||||
break;
|
||||
case AST_EXTENSION_NOT_INUSE:
|
||||
default:
|
||||
/* Default setting */
|
||||
*statestring = "terminated";
|
||||
*local_state = NOTIFY_OPEN;
|
||||
*pidfstate = "--";
|
||||
*pidfnote ="Ready";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
pj_xml_attr *ast_sip_presence_xml_create_attr(pj_pool_t *pool,
|
||||
pj_xml_node *node, const char *name, const char *value)
|
||||
{
|
||||
pj_xml_attr *attr = PJ_POOL_ALLOC_T(pool, pj_xml_attr);
|
||||
|
||||
pj_strdup2(pool, &attr->name, name);
|
||||
pj_strdup2(pool, &attr->value, value);
|
||||
|
||||
pj_xml_add_attr(node, attr);
|
||||
return attr;
|
||||
}
|
||||
|
||||
pj_xml_node *ast_sip_presence_xml_create_node(pj_pool_t *pool,
|
||||
pj_xml_node *parent, const char* name)
|
||||
{
|
||||
pj_xml_node *node = PJ_POOL_ALLOC_T(pool, pj_xml_node);
|
||||
|
||||
pj_list_init(&node->attr_head);
|
||||
pj_list_init(&node->node_head);
|
||||
|
||||
pj_strdup2(pool, &node->name, name);
|
||||
|
||||
node->content.ptr = NULL;
|
||||
node->content.slen = 0;
|
||||
|
||||
pj_xml_add_node(parent, node);
|
||||
return node;
|
||||
}
|
||||
|
||||
void ast_sip_presence_xml_find_node_attr(pj_pool_t* pool,
|
||||
pj_xml_node *parent, const char *node_name, const char *attr_name,
|
||||
pj_xml_node **node, pj_xml_attr **attr)
|
||||
{
|
||||
pj_str_t name;
|
||||
|
||||
if (!(*node = pj_xml_find_node(parent, pj_cstr(&name, node_name)))) {
|
||||
*node = ast_sip_presence_xml_create_node(pool, parent, node_name);
|
||||
}
|
||||
|
||||
if (!(*attr = pj_xml_find_attr(*node, pj_cstr(&name, attr_name), NULL))) {
|
||||
*attr = ast_sip_presence_xml_create_attr(pool, *node, attr_name, "");
|
||||
}
|
||||
}
|
@ -0,0 +1,112 @@
|
||||
/*
|
||||
* Asterisk -- An open source telephony toolkit.
|
||||
*
|
||||
* Copyright (C) 2014, Digium, Inc.
|
||||
*
|
||||
* Mark Michelson <mmichelson@digium.com>
|
||||
*
|
||||
* See http://www.asterisk.org for more information about
|
||||
* the Asterisk project. Please do not directly contact
|
||||
* any of the maintainers of this project for assistance;
|
||||
* the project provides a web site, mailing lists and IRC
|
||||
* channels for your use.
|
||||
*
|
||||
* This program is free software, distributed under the terms of
|
||||
* the GNU General Public License Version 2. See the LICENSE file
|
||||
* at the top of the source tree.
|
||||
*/
|
||||
|
||||
/*** MODULEINFO
|
||||
<depend>pjproject</depend>
|
||||
<depend>res_pjsip</depend>
|
||||
<depend>res_pjsip_pubsub</depend>
|
||||
<support_level>core</support_level>
|
||||
***/
|
||||
|
||||
#include "asterisk.h"
|
||||
|
||||
#include <pjsip.h>
|
||||
#include <pjsip_simple.h>
|
||||
#include <pjlib.h>
|
||||
|
||||
#include "asterisk/res_pjsip.h"
|
||||
#include "asterisk/res_pjsip_pubsub.h"
|
||||
#include "asterisk/res_pjsip_body_generator_types.h"
|
||||
#include "asterisk/module.h"
|
||||
#include "asterisk/strings.h"
|
||||
|
||||
#define MWI_TYPE "application"
|
||||
#define MWI_SUBTYPE "simple-message-summary"
|
||||
|
||||
static void *mwi_allocate_body(void *data)
|
||||
{
|
||||
struct ast_str **mwi_str;
|
||||
|
||||
mwi_str = ast_malloc(sizeof(*mwi_str));
|
||||
if (!mwi_str) {
|
||||
return NULL;
|
||||
}
|
||||
*mwi_str = ast_str_create(64);
|
||||
if (!*mwi_str) {
|
||||
ast_free(mwi_str);
|
||||
return NULL;
|
||||
}
|
||||
return mwi_str;
|
||||
}
|
||||
|
||||
static int mwi_generate_body_content(void *body, void *data)
|
||||
{
|
||||
struct ast_str **mwi = body;
|
||||
struct ast_sip_message_accumulator *counter = data;
|
||||
|
||||
ast_str_append(mwi, 0, "Messages-Waiting: %s\r\n",
|
||||
counter->new_msgs ? "yes" : "no");
|
||||
ast_str_append(mwi, 0, "Voice-Message: %d/%d (0/0)\r\n",
|
||||
counter->new_msgs, counter->old_msgs);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void mwi_to_string(void *body, struct ast_str **str)
|
||||
{
|
||||
struct ast_str **mwi = body;
|
||||
|
||||
ast_str_set(str, 0, "%s", ast_str_buffer(*mwi));
|
||||
}
|
||||
|
||||
static void mwi_destroy_body(void *body)
|
||||
{
|
||||
struct ast_str **mwi = body;
|
||||
|
||||
ast_free(*mwi);
|
||||
ast_free(mwi);
|
||||
}
|
||||
|
||||
static struct ast_sip_pubsub_body_generator mwi_generator = {
|
||||
.type = MWI_TYPE,
|
||||
.subtype = MWI_SUBTYPE,
|
||||
.allocate_body = mwi_allocate_body,
|
||||
.generate_body_content = mwi_generate_body_content,
|
||||
.to_string = mwi_to_string,
|
||||
.destroy_body = mwi_destroy_body,
|
||||
};
|
||||
|
||||
static int load_module(void)
|
||||
{
|
||||
if (ast_sip_pubsub_register_body_generator(&mwi_generator)) {
|
||||
return AST_MODULE_LOAD_DECLINE;
|
||||
}
|
||||
return AST_MODULE_LOAD_SUCCESS;
|
||||
}
|
||||
|
||||
static int unload_module(void)
|
||||
{
|
||||
ast_sip_pubsub_unregister_body_generator(&mwi_generator);
|
||||
return 0;
|
||||
}
|
||||
|
||||
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP MWI resource",
|
||||
.load = load_module,
|
||||
.unload = unload_module,
|
||||
.load_pri = AST_MODPRI_CHANNEL_DEPEND,
|
||||
);
|
@ -1,398 +0,0 @@
|
||||
/*
|
||||
* asterisk -- An open source telephony toolkit.
|
||||
*
|
||||
* Copyright (C) 2013, Digium, Inc.
|
||||
*
|
||||
* Kevin Harwell <kharwell@digium.com>
|
||||
*
|
||||
* See http://www.asterisk.org for more information about
|
||||
* the Asterisk project. Please do not directly contact
|
||||
* any of the maintainers of this project for assistance;
|
||||
* the project provides a web site, mailing lists and IRC
|
||||
* channels for your use.
|
||||
*
|
||||
* This program is free software, distributed under the terms of
|
||||
* the GNU General Public License Version 2. See the LICENSE file
|
||||
* at the top of the source tree.
|
||||
*/
|
||||
|
||||
/*** MODULEINFO
|
||||
<depend>pjproject</depend>
|
||||
<depend>res_pjsip</depend>
|
||||
<depend>res_pjsip_pubsub</depend>
|
||||
<depend>res_pjsip_exten_state</depend>
|
||||
<support_level>core</support_level>
|
||||
***/
|
||||
|
||||
#include "asterisk.h"
|
||||
|
||||
#include <pjsip.h>
|
||||
#include <pjsip_simple.h>
|
||||
#include <pjlib.h>
|
||||
|
||||
#include "asterisk/module.h"
|
||||
#include "asterisk/res_pjsip.h"
|
||||
#include "asterisk/res_pjsip_exten_state.h"
|
||||
|
||||
enum state {
|
||||
NOTIFY_OPEN,
|
||||
NOTIFY_INUSE,
|
||||
NOTIFY_CLOSED
|
||||
};
|
||||
|
||||
static void exten_state_to_str(int state, char **statestring, char **pidfstate,
|
||||
char **pidfnote, int *local_state)
|
||||
{
|
||||
switch (state) {
|
||||
case AST_EXTENSION_RINGING:
|
||||
*statestring = "early";
|
||||
*local_state = NOTIFY_INUSE;
|
||||
*pidfstate = "busy";
|
||||
*pidfnote = "Ringing";
|
||||
break;
|
||||
case AST_EXTENSION_INUSE:
|
||||
*statestring = "confirmed";
|
||||
*local_state = NOTIFY_INUSE;
|
||||
*pidfstate = "busy";
|
||||
*pidfnote = "On the phone";
|
||||
break;
|
||||
case AST_EXTENSION_BUSY:
|
||||
*statestring = "confirmed";
|
||||
*local_state = NOTIFY_CLOSED;
|
||||
*pidfstate = "busy";
|
||||
*pidfnote = "On the phone";
|
||||
break;
|
||||
case AST_EXTENSION_UNAVAILABLE:
|
||||
*statestring = "terminated";
|
||||
*local_state = NOTIFY_CLOSED;
|
||||
*pidfstate = "away";
|
||||
*pidfnote = "Unavailable";
|
||||
break;
|
||||
case AST_EXTENSION_ONHOLD:
|
||||
*statestring = "confirmed";
|
||||
*local_state = NOTIFY_CLOSED;
|
||||
*pidfstate = "busy";
|
||||
*pidfnote = "On hold";
|
||||
break;
|
||||
case AST_EXTENSION_NOT_INUSE:
|
||||
default:
|
||||
/* Default setting */
|
||||
*statestring = "terminated";
|
||||
*local_state = NOTIFY_OPEN;
|
||||
*pidfstate = "--";
|
||||
*pidfnote ="Ready";
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static pj_xml_attr *create_attr(pj_pool_t *pool, pj_xml_node *node,
|
||||
const char *name, const char *value)
|
||||
{
|
||||
pj_xml_attr *attr = PJ_POOL_ALLOC_T(pool, pj_xml_attr);
|
||||
|
||||
pj_strdup2(pool, &attr->name, name);
|
||||
pj_strdup2(pool, &attr->value, value);
|
||||
|
||||
pj_xml_add_attr(node, attr);
|
||||
return attr;
|
||||
}
|
||||
|
||||
static pj_xml_node *create_node(pj_pool_t *pool, pj_xml_node *parent,
|
||||
const char* name)
|
||||
{
|
||||
pj_xml_node *node = PJ_POOL_ALLOC_T(pool, pj_xml_node);
|
||||
|
||||
pj_list_init(&node->attr_head);
|
||||
pj_list_init(&node->node_head);
|
||||
|
||||
pj_strdup2(pool, &node->name, name);
|
||||
|
||||
node->content.ptr = NULL;
|
||||
node->content.slen = 0;
|
||||
|
||||
pj_xml_add_node(parent, node);
|
||||
return node;
|
||||
}
|
||||
|
||||
static void find_node_attr(pj_pool_t* pool, pj_xml_node *parent,
|
||||
const char *node_name, const char *attr_name,
|
||||
pj_xml_node **node, pj_xml_attr **attr)
|
||||
{
|
||||
pj_str_t name;
|
||||
|
||||
if (!(*node = pj_xml_find_node(parent, pj_cstr(&name, node_name)))) {
|
||||
*node = create_node(pool, parent, node_name);
|
||||
}
|
||||
|
||||
if (!(*attr = pj_xml_find_attr(*node, pj_cstr(&name, attr_name), NULL))) {
|
||||
*attr = create_attr(pool, *node, attr_name, "");
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \internal
|
||||
* \brief Adds non standard elements to the xml body
|
||||
*
|
||||
* This is some code that was part of the original chan_sip implementation
|
||||
* that is not part of the RFC 3863 definition, but we are keeping available
|
||||
* for backward compatability. The original comment stated that Eyebeam
|
||||
* supports this format.
|
||||
|
||||
*/
|
||||
static void add_non_standard(pj_pool_t *pool, pj_xml_node *node, const char *pidfstate)
|
||||
{
|
||||
static const char *XMLNS_PP = "xmlns:pp";
|
||||
static const char *XMLNS_PERSON = "urn:ietf:params:xml:ns:pidf:person";
|
||||
|
||||
static const char *XMLNS_ES = "xmlns:es";
|
||||
static const char *XMLNS_RPID_STATUS = "urn:ietf:params:xml:ns:pidf:rpid:status:rpid-status";
|
||||
|
||||
static const char *XMLNS_EP = "xmlns:ep";
|
||||
static const char *XMLNS_RPID_PERSON = "urn:ietf:params:xml:ns:pidf:rpid:rpid-person";
|
||||
|
||||
pj_xml_node *person = create_node(pool, node, "pp:person");
|
||||
pj_xml_node *status = create_node(pool, person, "status");
|
||||
|
||||
if (pidfstate[0] != '-') {
|
||||
pj_xml_node *activities = create_node(pool, status, "ep:activities");
|
||||
size_t str_size = sizeof("ep:") + strlen(pidfstate);
|
||||
|
||||
activities->content.ptr = pj_pool_alloc(pool, str_size);
|
||||
activities->content.slen = pj_ansi_snprintf(activities->content.ptr, str_size,
|
||||
"ep:%s", pidfstate);
|
||||
}
|
||||
|
||||
create_attr(pool, node, XMLNS_PP, XMLNS_PERSON);
|
||||
create_attr(pool, node, XMLNS_ES, XMLNS_RPID_STATUS);
|
||||
create_attr(pool, node, XMLNS_EP, XMLNS_RPID_PERSON);
|
||||
}
|
||||
|
||||
static void release_pool(void *obj)
|
||||
{
|
||||
pj_pool_t *pool = obj;
|
||||
|
||||
pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \internal
|
||||
* \brief Convert angle brackets in input into escaped forms suitable for XML
|
||||
*
|
||||
* \param input Raw input string
|
||||
* \param output Sanitized string
|
||||
* \param len Size of output buffer
|
||||
*/
|
||||
static void sanitize_xml(const char *input, char *output, size_t len)
|
||||
{
|
||||
char *copy = ast_strdupa(input);
|
||||
char *break_point;
|
||||
|
||||
output[0] = '\0';
|
||||
|
||||
while ((break_point = strpbrk(copy, "<>\"&'"))) {
|
||||
char to_escape = *break_point;
|
||||
|
||||
*break_point = '\0';
|
||||
strncat(output, copy, len);
|
||||
|
||||
switch (to_escape) {
|
||||
case '<':
|
||||
strncat(output, "<", len);
|
||||
break;
|
||||
case '>':
|
||||
strncat(output, ">", len);
|
||||
break;
|
||||
case '"':
|
||||
strncat(output, """, len);
|
||||
break;
|
||||
case '&':
|
||||
strncat(output, "&", len);
|
||||
break;
|
||||
case '\'':
|
||||
strncat(output, "'", len);
|
||||
break;
|
||||
};
|
||||
|
||||
copy = break_point + 1;
|
||||
}
|
||||
|
||||
/* Be sure to copy everything after the final bracket */
|
||||
if (*copy) {
|
||||
strncat(output, copy, len);
|
||||
}
|
||||
}
|
||||
|
||||
static int pidf_xml_create_body(struct ast_sip_exten_state_data *data, const char *local,
|
||||
const char *remote, struct ast_str **body_text)
|
||||
{
|
||||
pjpidf_pres *pres;
|
||||
pjpidf_tuple *tuple;
|
||||
pj_str_t entity, note, id, contact, priority;
|
||||
char *statestring = NULL, *pidfstate = NULL, *pidfnote = NULL;
|
||||
int local_state, size;
|
||||
char sanitized[PJSIP_MAX_URL_SIZE];
|
||||
|
||||
RAII_VAR(pj_pool_t *, pool,
|
||||
pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(),
|
||||
"pidf", 1024, 1024), release_pool);
|
||||
|
||||
exten_state_to_str(data->exten_state, &statestring, &pidfstate,
|
||||
&pidfnote, &local_state);
|
||||
|
||||
if (!(pres = pjpidf_create(pool, pj_cstr(&entity, local)))) {
|
||||
ast_log(LOG_WARNING, "Unable to create PIDF presence\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
add_non_standard(pool, pres, pidfstate);
|
||||
|
||||
if (!pjpidf_pres_add_note(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(pool, pres, pj_cstr(&id, data->exten)))) {
|
||||
ast_log(LOG_WARNING, "Unable to create PIDF tuple\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
sanitize_xml(remote, sanitized, sizeof(sanitized));
|
||||
pjpidf_tuple_set_contact(pool, tuple, pj_cstr(&contact, sanitized));
|
||||
pjpidf_tuple_set_contact_prio(pool, tuple, pj_cstr(&priority, "1"));
|
||||
pjpidf_status_set_basic_open(pjpidf_tuple_get_status(tuple),
|
||||
local_state == NOTIFY_OPEN);
|
||||
|
||||
if (!(size = pjpidf_print(pres, ast_str_buffer(*body_text),
|
||||
ast_str_size(*body_text)))) {
|
||||
ast_log(LOG_WARNING, "PIDF body text too large\n");
|
||||
return -1;
|
||||
}
|
||||
*(ast_str_buffer(*body_text) + size) = '\0';
|
||||
ast_str_update(*body_text);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct ast_sip_exten_state_provider pidf_xml_provider = {
|
||||
.event_name = "presence",
|
||||
.type = "application",
|
||||
.subtype = "pidf+xml",
|
||||
.body_type = "application/pidf+xml",
|
||||
.create_body = pidf_xml_create_body
|
||||
};
|
||||
|
||||
static int xpidf_xml_create_body(struct ast_sip_exten_state_data *data, const char *local,
|
||||
const char *remote, struct ast_str **body_text)
|
||||
{
|
||||
static pj_str_t STR_ADDR_PARAM = { ";user=ip", 8 };
|
||||
pjxpidf_pres *pres;
|
||||
pj_xml_attr *attr;
|
||||
pj_str_t name, uri;
|
||||
char *statestring = NULL, *pidfstate = NULL, *pidfnote = NULL;
|
||||
int local_state, size;
|
||||
char sanitized[PJSIP_MAX_URL_SIZE];
|
||||
pj_xml_node *atom;
|
||||
pj_xml_node *address;
|
||||
pj_xml_node *status;
|
||||
pj_xml_node *msnsubstatus;
|
||||
|
||||
RAII_VAR(pj_pool_t *, pool,
|
||||
pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(),
|
||||
"pidf", 1024, 1024), release_pool);
|
||||
|
||||
exten_state_to_str(data->exten_state, &statestring, &pidfstate,
|
||||
&pidfnote, &local_state);
|
||||
|
||||
if (!(pres = pjxpidf_create(pool, pj_cstr(&name, local)))) {
|
||||
ast_log(LOG_WARNING, "Unable to create PIDF presence\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
find_node_attr(pool, pres, "atom", "id", &atom, &attr);
|
||||
pj_strdup2(pool, &attr->value, data->exten);
|
||||
|
||||
find_node_attr(pool, atom, "address", "uri", &address, &attr);
|
||||
|
||||
sanitize_xml(remote, sanitized, sizeof(sanitized));
|
||||
|
||||
uri.ptr = (char*) pj_pool_alloc(pool, strlen(sanitized) + STR_ADDR_PARAM.slen);
|
||||
pj_strcpy2( &uri, sanitized);
|
||||
|
||||
pj_strcat( &uri, &STR_ADDR_PARAM);
|
||||
pj_strdup(pool, &attr->value, &uri);
|
||||
|
||||
create_attr(pool, address, "priority", "0.80000");
|
||||
|
||||
find_node_attr(pool, address, "status", "status", &status, &attr);
|
||||
pj_strdup2(pool, &attr->value,
|
||||
(local_state == NOTIFY_OPEN) ? "open" :
|
||||
(local_state == NOTIFY_INUSE) ? "inuse" : "closed");
|
||||
|
||||
find_node_attr(pool, address, "msnsubstatus", "substatus", &msnsubstatus, &attr);
|
||||
pj_strdup2(pool, &attr->value,
|
||||
(local_state == NOTIFY_OPEN) ? "online" :
|
||||
(local_state == NOTIFY_INUSE) ? "onthephone" : "offline");
|
||||
|
||||
if (!(size = pjxpidf_print(pres, ast_str_buffer(*body_text),
|
||||
ast_str_size(*body_text)))) {
|
||||
ast_log(LOG_WARNING, "XPIDF body text too large\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
*(ast_str_buffer(*body_text) + size) = '\0';
|
||||
ast_str_update(*body_text);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct ast_sip_exten_state_provider xpidf_xml_provider = {
|
||||
.event_name = "presence",
|
||||
.type = "application",
|
||||
.subtype = "xpidf+xml",
|
||||
.body_type = "application/xpidf+xml",
|
||||
.create_body = xpidf_xml_create_body
|
||||
};
|
||||
|
||||
static struct ast_sip_exten_state_provider cpim_pidf_xml_provider = {
|
||||
.event_name = "presence",
|
||||
.type = "application",
|
||||
.subtype = "cpim-pidf+xml",
|
||||
.body_type = "application/cpim-pidf+xml",
|
||||
.create_body = xpidf_xml_create_body,
|
||||
};
|
||||
|
||||
static int load_module(void)
|
||||
{
|
||||
if (ast_sip_register_exten_state_provider(&pidf_xml_provider)) {
|
||||
ast_log(LOG_WARNING, "Unable to load provider event_name=%s, body_type=%s",
|
||||
pidf_xml_provider.event_name, pidf_xml_provider.body_type);
|
||||
}
|
||||
|
||||
if (ast_sip_register_exten_state_provider(&xpidf_xml_provider)) {
|
||||
ast_log(LOG_WARNING, "Unable to load provider event_name=%s, body_type=%s",
|
||||
xpidf_xml_provider.event_name, xpidf_xml_provider.body_type);
|
||||
}
|
||||
|
||||
if (ast_sip_register_exten_state_provider(&cpim_pidf_xml_provider)) {
|
||||
ast_log(LOG_WARNING, "Unable to load provider event_name=%s, body_type=%s",
|
||||
cpim_pidf_xml_provider.event_name, cpim_pidf_xml_provider.body_type);
|
||||
}
|
||||
|
||||
return AST_MODULE_LOAD_SUCCESS;
|
||||
}
|
||||
|
||||
static int unload_module(void)
|
||||
{
|
||||
ast_sip_unregister_exten_state_provider(&cpim_pidf_xml_provider);
|
||||
ast_sip_unregister_exten_state_provider(&xpidf_xml_provider);
|
||||
ast_sip_unregister_exten_state_provider(&pidf_xml_provider);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Extension State PIDF Provider",
|
||||
.load = load_module,
|
||||
.unload = unload_module,
|
||||
.load_pri = AST_MODPRI_CHANNEL_DEPEND,
|
||||
);
|
@ -0,0 +1,135 @@
|
||||
/*
|
||||
* asterisk -- An open source telephony toolkit.
|
||||
*
|
||||
* Copyright (C) 2014, Digium, Inc.
|
||||
*
|
||||
* Mark Michelson <mmichelson@digium.com>
|
||||
*
|
||||
* See http://www.asterisk.org for more information about
|
||||
* the Asterisk project. Please do not directly contact
|
||||
* any of the maintainers of this project for assistance;
|
||||
* the project provides a web site, mailing lists and IRC
|
||||
* channels for your use.
|
||||
*
|
||||
* This program is free software, distributed under the terms of
|
||||
* the GNU General Public License Version 2. See the LICENSE file
|
||||
* at the top of the source tree.
|
||||
*/
|
||||
|
||||
/*** MODULEINFO
|
||||
<depend>pjproject</depend>
|
||||
<depend>res_pjsip</depend>
|
||||
<depend>res_pjsip_pubsub</depend>
|
||||
<support_level>core</support_level>
|
||||
***/
|
||||
|
||||
#include "asterisk.h"
|
||||
|
||||
#include <pjsip.h>
|
||||
#include <pjsip_simple.h>
|
||||
#include <pjlib.h>
|
||||
|
||||
#include "asterisk/module.h"
|
||||
#include "asterisk/res_pjsip.h"
|
||||
#include "asterisk/res_pjsip_pubsub.h"
|
||||
#include "asterisk/res_pjsip_presence_xml.h"
|
||||
#include "asterisk/res_pjsip_body_generator_types.h"
|
||||
|
||||
static void *pidf_allocate_body(void *data)
|
||||
{
|
||||
struct ast_sip_exten_state_data *state_data = data;
|
||||
pjpidf_pres *pres;
|
||||
pj_str_t entity;
|
||||
|
||||
pres = pjpidf_create(state_data->pool, pj_cstr(&entity, state_data->local));
|
||||
|
||||
return pres;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
#define MAX_STRING_GROWTHS 3
|
||||
|
||||
static void pidf_to_string(void *body, struct ast_str **str)
|
||||
{
|
||||
int size;
|
||||
int growths = 0;
|
||||
pjpidf_pres *pres = body;
|
||||
|
||||
do {
|
||||
size = pjpidf_print(pres, ast_str_buffer(*str), ast_str_size(*str) - 1);
|
||||
if (size < 0) {
|
||||
ast_str_make_space(str, ast_str_size(*str) * 2);
|
||||
++growths;
|
||||
return;
|
||||
}
|
||||
} while (size < 0 && growths < MAX_STRING_GROWTHS);
|
||||
|
||||
if (size < 0) {
|
||||
ast_log(LOG_WARNING, "PIDF body text too large\n");
|
||||
return;
|
||||
}
|
||||
|
||||
*(ast_str_buffer(*str) + size) = '\0';
|
||||
ast_str_update(*str);
|
||||
}
|
||||
|
||||
static struct ast_sip_pubsub_body_generator pidf_body_generator = {
|
||||
.type = "application",
|
||||
.subtype = "pidf+xml",
|
||||
.allocate_body = pidf_allocate_body,
|
||||
.generate_body_content = pidf_generate_body_content,
|
||||
.to_string = pidf_to_string,
|
||||
/* No need for a destroy_body callback since we use a pool */
|
||||
};
|
||||
|
||||
static int load_module(void)
|
||||
{
|
||||
if (ast_sip_pubsub_register_body_generator(&pidf_body_generator)) {
|
||||
return AST_MODULE_LOAD_DECLINE;
|
||||
}
|
||||
return AST_MODULE_LOAD_SUCCESS;
|
||||
}
|
||||
|
||||
static int unload_module(void)
|
||||
{
|
||||
ast_sip_pubsub_unregister_body_generator(&pidf_body_generator);
|
||||
return 0;
|
||||
}
|
||||
|
||||
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Extension State PIDF Provider",
|
||||
.load = load_module,
|
||||
.unload = unload_module,
|
||||
.load_pri = AST_MODPRI_CHANNEL_DEPEND,
|
||||
);
|
@ -0,0 +1,113 @@
|
||||
/*
|
||||
* asterisk -- An open source telephony toolkit.
|
||||
*
|
||||
* Copyright (C) 2014, Digium, Inc.
|
||||
*
|
||||
* Mark Michelson <mmichelson@digium.com>
|
||||
*
|
||||
* See http://www.asterisk.org for more information about
|
||||
* the Asterisk project. Please do not directly contact
|
||||
* any of the maintainers of this project for assistance;
|
||||
* the project provides a web site, mailing lists and IRC
|
||||
* channels for your use.
|
||||
*
|
||||
* This program is free software, distributed under the terms of
|
||||
* the GNU General Public License Version 2. See the LICENSE file
|
||||
* at the top of the source tree.
|
||||
*/
|
||||
|
||||
/*** MODULEINFO
|
||||
<depend>pjproject</depend>
|
||||
<depend>res_pjsip</depend>
|
||||
<depend>res_pjsip_pubsub</depend>
|
||||
<support_level>core</support_level>
|
||||
***/
|
||||
|
||||
#include "asterisk.h"
|
||||
|
||||
#include <pjsip.h>
|
||||
#include <pjsip_simple.h>
|
||||
#include <pjlib.h>
|
||||
|
||||
#include "asterisk/module.h"
|
||||
#include "asterisk/res_pjsip.h"
|
||||
#include "asterisk/res_pjsip_pubsub.h"
|
||||
#include "asterisk/res_pjsip_presence_xml.h"
|
||||
#include "asterisk/res_pjsip_body_generator_types.h"
|
||||
|
||||
/*!
|
||||
* \internal
|
||||
* \brief Adds non standard elements to the xml body
|
||||
*
|
||||
* This is some code that was part of the original chan_sip implementation
|
||||
* that is not part of the RFC 3863 definition, but we are keeping available
|
||||
* for backward compatability. The original comment stated that Eyebeam
|
||||
* supports this format.
|
||||
*/
|
||||
static void add_eyebeam(pj_pool_t *pool, pj_xml_node *node, const char *pidfstate)
|
||||
{
|
||||
static const char *XMLNS_PP = "xmlns:pp";
|
||||
static const char *XMLNS_PERSON = "urn:ietf:params:xml:ns:pidf:person";
|
||||
|
||||
static const char *XMLNS_ES = "xmlns:es";
|
||||
static const char *XMLNS_RPID_STATUS = "urn:ietf:params:xml:ns:pidf:rpid:status:rpid-status";
|
||||
|
||||
static const char *XMLNS_EP = "xmlns:ep";
|
||||
static const char *XMLNS_RPID_PERSON = "urn:ietf:params:xml:ns:pidf:rpid:rpid-person";
|
||||
|
||||
pj_xml_node *person = ast_sip_presence_xml_create_node(pool, node, "pp:person");
|
||||
pj_xml_node *status = ast_sip_presence_xml_create_node(pool, person, "status");
|
||||
|
||||
if (pidfstate[0] != '-') {
|
||||
pj_xml_node *activities = ast_sip_presence_xml_create_node(pool, status, "ep:activities");
|
||||
size_t str_size = sizeof("ep:") + strlen(pidfstate);
|
||||
|
||||
activities->content.ptr = pj_pool_alloc(pool, str_size);
|
||||
activities->content.slen = pj_ansi_snprintf(activities->content.ptr, str_size,
|
||||
"ep:%s", pidfstate);
|
||||
}
|
||||
|
||||
ast_sip_presence_xml_create_attr(pool, node, XMLNS_PP, XMLNS_PERSON);
|
||||
ast_sip_presence_xml_create_attr(pool, node, XMLNS_ES, XMLNS_RPID_STATUS);
|
||||
ast_sip_presence_xml_create_attr(pool, node, XMLNS_EP, XMLNS_RPID_PERSON);
|
||||
}
|
||||
|
||||
static int pidf_supplement_body(void *body, void *data)
|
||||
{
|
||||
pjpidf_pres *pres = body;
|
||||
struct ast_sip_exten_state_data *state_data = data;
|
||||
char *statestring = NULL, *pidfstate = NULL, *pidfnote = NULL;
|
||||
enum ast_sip_pidf_state local_state;
|
||||
|
||||
ast_sip_presence_exten_state_to_str(state_data->exten_state, &statestring,
|
||||
&pidfstate, &pidfnote, &local_state);
|
||||
|
||||
add_eyebeam(state_data->pool, pres, pidfstate);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct ast_sip_pubsub_body_supplement pidf_supplement = {
|
||||
.type = "application",
|
||||
.subtype = "pidf+xml",
|
||||
.supplement_body = pidf_supplement_body,
|
||||
};
|
||||
|
||||
static int load_module(void)
|
||||
{
|
||||
if (ast_sip_pubsub_register_body_supplement(&pidf_supplement)) {
|
||||
return AST_MODULE_LOAD_DECLINE;
|
||||
}
|
||||
return AST_MODULE_LOAD_SUCCESS;
|
||||
}
|
||||
|
||||
static int unload_module(void)
|
||||
{
|
||||
ast_sip_pubsub_unregister_body_supplement(&pidf_supplement);
|
||||
return 0;
|
||||
}
|
||||
|
||||
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP PIDF Eyebeam supplement",
|
||||
.load = load_module,
|
||||
.unload = unload_module,
|
||||
.load_pri = AST_MODPRI_CHANNEL_DEPEND,
|
||||
);
|
@ -0,0 +1,177 @@
|
||||
/*
|
||||
* asterisk -- An open source telephony toolkit.
|
||||
*
|
||||
* Copyright (C) 2014, Digium, Inc.
|
||||
*
|
||||
* Mark Michelson <mmichelson@digium.com>
|
||||
*
|
||||
* See http://www.asterisk.org for more information about
|
||||
* the Asterisk project. Please do not directly contact
|
||||
* any of the maintainers of this project for assistance;
|
||||
* the project provides a web site, mailing lists and IRC
|
||||
* channels for your use.
|
||||
*
|
||||
* This program is free software, distributed under the terms of
|
||||
* the GNU General Public License Version 2. See the LICENSE file
|
||||
* at the top of the source tree.
|
||||
*/
|
||||
|
||||
/*** MODULEINFO
|
||||
<depend>pjproject</depend>
|
||||
<depend>res_pjsip</depend>
|
||||
<depend>res_pjsip_pubsub</depend>
|
||||
<depend>res_pjsip_exten_state</depend>
|
||||
<support_level>core</support_level>
|
||||
***/
|
||||
|
||||
#include "asterisk.h"
|
||||
|
||||
#include <pjsip.h>
|
||||
#include <pjsip_simple.h>
|
||||
#include <pjlib.h>
|
||||
|
||||
#include "asterisk/module.h"
|
||||
#include "asterisk/res_pjsip.h"
|
||||
#include "asterisk/res_pjsip_pubsub.h"
|
||||
#include "asterisk/res_pjsip_presence_xml.h"
|
||||
#include "asterisk/res_pjsip_body_generator_types.h"
|
||||
|
||||
static void *xpidf_allocate_body(void *data)
|
||||
{
|
||||
struct ast_sip_exten_state_data *state_data = data;
|
||||
pjxpidf_pres *pres;
|
||||
pj_str_t name;
|
||||
|
||||
pres = pjxpidf_create(state_data->pool, pj_cstr(&name, state_data->local));
|
||||
return pres;
|
||||
}
|
||||
|
||||
static int xpidf_generate_body_content(void *body, void *data)
|
||||
{
|
||||
pjxpidf_pres *pres = body;
|
||||
struct ast_sip_exten_state_data *state_data = data;
|
||||
static pj_str_t STR_ADDR_PARAM = { ";user=ip", 8 };
|
||||
char *statestring = NULL, *pidfstate = NULL, *pidfnote = NULL;
|
||||
pj_xml_attr *attr;
|
||||
enum ast_sip_pidf_state local_state;
|
||||
pj_str_t uri;
|
||||
char sanitized[PJSIP_MAX_URL_SIZE];
|
||||
pj_xml_node *atom;
|
||||
pj_xml_node *address;
|
||||
pj_xml_node *status;
|
||||
pj_xml_node *msnsubstatus;
|
||||
|
||||
ast_sip_presence_exten_state_to_str(state_data->exten_state, &statestring,
|
||||
&pidfstate, &pidfnote, &local_state);
|
||||
|
||||
ast_sip_presence_xml_find_node_attr(state_data->pool, pres, "atom", "id",
|
||||
&atom, &attr);
|
||||
pj_strdup2(state_data->pool, &attr->value, state_data->exten);
|
||||
|
||||
ast_sip_presence_xml_find_node_attr(state_data->pool, atom, "address",
|
||||
"uri", &address, &attr);
|
||||
|
||||
ast_sip_sanitize_xml(state_data->remote, sanitized, sizeof(sanitized));
|
||||
|
||||
uri.ptr = (char*) pj_pool_alloc(state_data->pool,
|
||||
strlen(sanitized) + STR_ADDR_PARAM.slen);
|
||||
pj_strcpy2( &uri, sanitized);
|
||||
pj_strcat( &uri, &STR_ADDR_PARAM);
|
||||
pj_strdup(state_data->pool, &attr->value, &uri);
|
||||
|
||||
ast_sip_presence_xml_create_attr(state_data->pool, address, "priority", "0.80000");
|
||||
|
||||
ast_sip_presence_xml_find_node_attr(state_data->pool, address,
|
||||
"status", "status", &status, &attr);
|
||||
pj_strdup2(state_data->pool, &attr->value,
|
||||
(local_state == NOTIFY_OPEN) ? "open" :
|
||||
(local_state == NOTIFY_INUSE) ? "inuse" : "closed");
|
||||
|
||||
ast_sip_presence_xml_find_node_attr(state_data->pool, address,
|
||||
"msnsubstatus", "substatus", &msnsubstatus, &attr);
|
||||
pj_strdup2(state_data->pool, &attr->value,
|
||||
(local_state == NOTIFY_OPEN) ? "online" :
|
||||
(local_state == NOTIFY_INUSE) ? "onthephone" : "offline");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define MAX_STRING_GROWTHS 3
|
||||
|
||||
static void xpidf_to_string(void *body, struct ast_str **str)
|
||||
{
|
||||
pjxpidf_pres *pres = body;
|
||||
int growths = 0;
|
||||
int size;
|
||||
|
||||
do {
|
||||
|
||||
size = pjxpidf_print(pres, ast_str_buffer(*str), ast_str_size(*str));
|
||||
if (size < 0) {
|
||||
ast_str_make_space(str, ast_str_size(*str) * 2);
|
||||
++growths;
|
||||
return;
|
||||
}
|
||||
} while (size < 0 && growths < MAX_STRING_GROWTHS);
|
||||
|
||||
if (size < 0) {
|
||||
ast_log(LOG_WARNING, "XPIDF body text too large\n");
|
||||
return;
|
||||
}
|
||||
|
||||
*(ast_str_buffer(*str) + size) = '\0';
|
||||
ast_str_update(*str);
|
||||
}
|
||||
|
||||
static struct ast_sip_pubsub_body_generator xpidf_body_generator = {
|
||||
.type = "application",
|
||||
.subtype = "xpidf+xml",
|
||||
.allocate_body = xpidf_allocate_body,
|
||||
.generate_body_content = xpidf_generate_body_content,
|
||||
.to_string = xpidf_to_string,
|
||||
/* No need for a destroy_body callback since we use a pool */
|
||||
};
|
||||
|
||||
static struct ast_sip_pubsub_body_generator cpim_pidf_body_generator = {
|
||||
.type = "application",
|
||||
.subtype = "cpim-pidf+xml",
|
||||
.allocate_body = xpidf_allocate_body,
|
||||
.generate_body_content = xpidf_generate_body_content,
|
||||
.to_string = xpidf_to_string,
|
||||
/* No need for a destroy_body callback since we use a pool */
|
||||
};
|
||||
|
||||
static void unregister_all(void)
|
||||
{
|
||||
ast_sip_pubsub_unregister_body_generator(&cpim_pidf_body_generator);
|
||||
ast_sip_pubsub_unregister_body_generator(&xpidf_body_generator);
|
||||
}
|
||||
|
||||
static int load_module(void)
|
||||
{
|
||||
if (ast_sip_pubsub_register_body_generator(&xpidf_body_generator)) {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
if (ast_sip_pubsub_register_body_generator(&cpim_pidf_body_generator)) {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
return AST_MODULE_LOAD_SUCCESS;
|
||||
|
||||
fail:
|
||||
unregister_all();
|
||||
return AST_MODULE_LOAD_DECLINE;
|
||||
}
|
||||
|
||||
static int unload_module(void)
|
||||
{
|
||||
unregister_all();
|
||||
return 0;
|
||||
}
|
||||
|
||||
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Extension State PIDF Provider",
|
||||
.load = load_module,
|
||||
.unload = unload_module,
|
||||
.load_pri = AST_MODPRI_CHANNEL_DEPEND,
|
||||
);
|
Loading…
Reference in new issue