Asterisk - The Open Source Telephony Project GIT-master-f36a736
res_pjsip_xpidf_body_generator.c
Go to the documentation of this file.
1/*
2 * asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2014, Digium, Inc.
5 *
6 * Mark Michelson <mmichelson@digium.com>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19/*** MODULEINFO
20 <depend>pjproject</depend>
21 <depend>res_pjsip</depend>
22 <depend>res_pjsip_pubsub</depend>
23 <support_level>core</support_level>
24 ***/
25
26#include "asterisk.h"
27
28#include <pjsip.h>
29#include <pjsip_simple.h>
30#include <pjlib.h>
31
32#include "asterisk/module.h"
33#include "asterisk/res_pjsip.h"
37
38static void *xpidf_allocate_body(void *data)
39{
40 struct ast_sip_exten_state_data *state_data = data;
41 char *local = ast_strdupa(state_data->local);
42 pjxpidf_pres *pres;
43 pj_str_t name;
44
45 pres = pjxpidf_create(state_data->pool, pj_cstr(&name, ast_strip_quoted(local, "<", ">")));
46 return pres;
47}
48
49static int xpidf_generate_body_content(void *body, void *data)
50{
51 pjxpidf_pres *pres = body;
52 struct ast_sip_exten_state_data *state_data = data;
53 static pj_str_t STR_ADDR_PARAM = { ";user=ip", 8 };
54 char *statestring = NULL, *pidfstate = NULL, *pidfnote = NULL;
55 pj_xml_attr *attr;
56 enum ast_sip_pidf_state local_state;
57 pj_str_t uri;
58 char sanitized[PJSIP_MAX_URL_SIZE];
59 pj_xml_node *atom;
60 pj_xml_node *address;
61 pj_xml_node *status;
62 pj_xml_node *msnsubstatus;
63
64 ast_sip_presence_exten_state_to_str(state_data->exten_state, &statestring,
65 &pidfstate, &pidfnote, &local_state, 0);
66
67 ast_sip_presence_xml_find_node_attr(state_data->pool, pres, "atom", "id",
68 &atom, &attr);
69 pj_strdup2(state_data->pool, &attr->value, state_data->exten);
70
71 ast_sip_presence_xml_find_node_attr(state_data->pool, atom, "address",
72 "uri", &address, &attr);
73
74 ast_sip_sanitize_xml(state_data->remote, sanitized, sizeof(sanitized));
75
76 uri.ptr = (char*) pj_pool_alloc(state_data->pool,
77 strlen(sanitized) + STR_ADDR_PARAM.slen);
78 pj_strcpy2( &uri, sanitized);
79 pj_strcat( &uri, &STR_ADDR_PARAM);
80 pj_strdup(state_data->pool, &attr->value, &uri);
81
82 ast_sip_presence_xml_create_attr(state_data->pool, address, "priority", "0.80000");
83
85 "status", "status", &status, &attr);
86 pj_strdup2(state_data->pool, &attr->value,
87 (local_state == NOTIFY_OPEN) ? "open" :
88 (local_state == NOTIFY_INUSE) ? "inuse" : "closed");
89
91 "msnsubstatus", "substatus", &msnsubstatus, &attr);
92 pj_strdup2(state_data->pool, &attr->value,
93 (local_state == NOTIFY_OPEN) ? "online" :
94 (local_state == NOTIFY_INUSE) ? "onthephone" : "offline");
95
96 return 0;
97}
98
99#define MAX_STRING_GROWTHS 5
100
101static void xpidf_to_string(void *body, struct ast_str **str)
102{
103 pjxpidf_pres *pres = body;
104 int growths = 0;
105 int size;
106
107 do {
108 size = pjxpidf_print(pres, ast_str_buffer(*str), ast_str_size(*str) - 1);
109 if (size <= AST_PJSIP_XML_PROLOG_LEN) {
111 ++growths;
112 }
113 } while (size <= AST_PJSIP_XML_PROLOG_LEN && growths < MAX_STRING_GROWTHS);
114 if (size <= AST_PJSIP_XML_PROLOG_LEN) {
115 ast_log(LOG_WARNING, "XPIDF body text too large\n");
116 return;
117 }
118
119 *(ast_str_buffer(*str) + size) = '\0';
121}
122
124 .type = "application",
125 .subtype = "xpidf+xml",
126 .body_type = AST_SIP_EXTEN_STATE_DATA,
127 .allocate_body = xpidf_allocate_body,
128 .generate_body_content = xpidf_generate_body_content,
129 .to_string = xpidf_to_string,
130 /* No need for a destroy_body callback since we use a pool */
131};
132
134 .type = "application",
135 .subtype = "cpim-pidf+xml",
136 .body_type = AST_SIP_EXTEN_STATE_DATA,
137 .allocate_body = xpidf_allocate_body,
138 .generate_body_content = xpidf_generate_body_content,
139 .to_string = xpidf_to_string,
140 /* No need for a destroy_body callback since we use a pool */
141};
142
143static void unregister_all(void)
144{
147}
148
149static int load_module(void)
150{
152 goto fail;
153 }
154
156 goto fail;
157 }
158
160
161fail:
164}
165
166static int unload_module(void)
167{
169 return 0;
170}
171
172AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Extension State PIDF Provider",
173 .support_level = AST_MODULE_SUPPORT_CORE,
174 .load = load_module,
175 .unload = unload_module,
176 .load_pri = AST_MODPRI_CHANNEL_DEPEND,
177 .requires = "res_pjsip,res_pjsip_pubsub",
jack_status_t status
Definition: app_jack.c:146
const char * str
Definition: app_jack.c:147
Asterisk main include file. File version handling, generic pbx functions.
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define ast_log
Definition: astobj2.c:42
char * address
Definition: f2c.h:59
static const char name[]
Definition: format_mp3.c:68
#define LOG_WARNING
Asterisk module definitions.
@ AST_MODFLAG_LOAD_ORDER
Definition: module.h:331
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:557
@ AST_MODPRI_CHANNEL_DEPEND
Definition: module.h:340
@ AST_MODULE_SUPPORT_CORE
Definition: module.h:121
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
void ast_sip_sanitize_xml(const char *input, char *output, size_t len)
Replace offensive XML characters with XML entities.
Definition: presence_xml.c:29
#define AST_PJSIP_XML_PROLOG_LEN
Length of the XML prolog when printing presence or other XML in PJSIP.
ast_sip_pidf_state
@ NOTIFY_INUSE
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)
Find an attribute within a given node.
Definition: presence_xml.c:172
void ast_sip_presence_exten_state_to_str(int state, char **statestring, char **pidfstate, char **pidfnote, enum ast_sip_pidf_state *local_state, unsigned int notify_early_inuse_ringing)
Convert extension state to relevant PIDF strings.
Definition: presence_xml.c:84
pj_xml_attr * ast_sip_presence_xml_create_attr(pj_pool_t *pool, pj_xml_node *node, const char *name, const char *value)
Create XML attribute.
Definition: presence_xml.c:140
void ast_sip_pubsub_unregister_body_generator(struct ast_sip_pubsub_body_generator *generator)
Unregister a body generator with the pubsub core.
int ast_sip_pubsub_register_body_generator(struct ast_sip_pubsub_body_generator *generator)
Register a body generator with the pubsub core.
#define AST_SIP_EXTEN_STATE_DATA
static int xpidf_generate_body_content(void *body, void *data)
static void unregister_all(void)
static void * xpidf_allocate_body(void *data)
#define MAX_STRING_GROWTHS
static struct ast_sip_pubsub_body_generator xpidf_body_generator
static int load_module(void)
static int unload_module(void)
static void xpidf_to_string(void *body, struct ast_str **str)
static struct ast_sip_pubsub_body_generator cpim_pidf_body_generator
#define NULL
Definition: resample.c:96
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:761
char * ast_strip_quoted(char *s, const char *beg_quotes, const char *end_quotes)
Strip leading/trailing whitespace and quotes from a string.
Definition: utils.c:1818
#define ast_str_make_space(buf, new_len)
Definition: strings.h:828
void ast_str_update(struct ast_str *buf)
Update the length of the buffer, after using ast_str merely as a buffer.
Definition: strings.h:703
size_t ast_str_size(const struct ast_str *buf)
Returns the current maximum length (without reallocation) of the current buffer.
Definition: strings.h:742
structure used for presence XML bodies
enum ast_extension_states exten_state
Pubsub body generator.
const char * type
Content type In "plain/text", "plain" is the type.
Support for dynamic strings.
Definition: strings.h:623