Asterisk - The Open Source Telephony Project  18.5.0
res_pjsip_pidf_digium_body_supplement.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  * Kevin Harwell <[email protected]>
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/presencestate.h"
34 #include "asterisk/res_pjsip.h"
38 
39 static int pidf_supplement_body(void *body, void *data)
40 {
41  struct ast_sip_exten_state_data *state_data = data;
42  pj_xml_node *node;
43  char sanitized[1024];
44 
45  /* The res_pjsip_exten_state module converts the user agent to lower case */
46  if (ast_strlen_zero(state_data->user_agent) ||
47  (!strstr(state_data->user_agent, "digium") &&
48  !strstr(state_data->user_agent, "sangoma"))) {
49  /* not a Sangoma phone */
50  return 0;
51  }
52 
54  state_data->pool, body, "tuple"))) {
55  ast_log(LOG_WARNING, "Unable to create PIDF tuple\n");
56  return -1;
57  }
58 
60  state_data->pool, node, "id", "digium-presence");
61 
63  state_data->pool, node, "status"))) {
64  ast_log(LOG_WARNING, "Unable to create PIDF tuple status\n");
65  return -1;
66  }
67 
69  state_data->pool, node, "digium_presence"))) {
70  ast_log(LOG_WARNING, "Unable to create digium presence\n");
71  return -1;
72  }
73 
74  if (!ast_strlen_zero(state_data->presence_message)) {
75  ast_sip_sanitize_xml(state_data->presence_message, sanitized, sizeof(sanitized));
76  pj_strdup2(state_data->pool, &node->content, sanitized);
77  }
78 
80  state_data->pool, node, "type", ast_presence_state2str(
81  state_data->presence_state));
82 
83  if (!ast_strlen_zero(state_data->presence_subtype)) {
84  ast_sip_sanitize_xml(state_data->presence_subtype, sanitized, sizeof(sanitized));
86  state_data->pool, node, "subtype", sanitized);
87  }
88 
89  return 0;
90 }
91 
93  .type = "application",
94  .subtype = "pidf+xml",
95  .supplement_body = pidf_supplement_body,
96 };
97 
98 static int load_module(void)
99 {
100  if (ast_sip_pubsub_register_body_supplement(&pidf_supplement)) {
102  }
104 }
105 
106 static int unload_module(void)
107 {
109  return 0;
110 }
111 
112 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP PIDF Sangoma presence supplement",
113  .support_level = AST_MODULE_SUPPORT_CORE,
114  .load = load_module,
115  .unload = unload_module,
116  .load_pri = AST_MODPRI_CHANNEL_DEPEND,
117  .requires = "res_pjsip,res_pjsip_pubsub",
118 );
Definition: test_heap.c:38
static int pidf_supplement_body(void *body, void *data)
Asterisk main include file. File version handling, generic pbx functions.
static struct ast_sip_pubsub_body_supplement pidf_supplement
#define LOG_WARNING
Definition: logger.h:274
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
const char * type
Content type In "plain/text", "plain" is the type.
#define ast_strlen_zero(foo)
Definition: strings.h:52
static int load_module(void)
#define ast_log
Definition: astobj2.c:42
enum ast_presence_state presence_state
Presence state management.
pj_xml_node * ast_sip_presence_xml_create_node(pj_pool_t *pool, pj_xml_node *parent, const char *name)
Create XML node.
Definition: presence_xml.c:152
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
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS|AST_MODFLAG_LOAD_ORDER, "HTTP Phone Provisioning",.support_level=AST_MODULE_SUPPORT_EXTENDED,.load=load_module,.unload=unload_module,.reload=reload,.load_pri=AST_MODPRI_CHANNEL_DEPEND,.requires="http",)
structure used for presence XML bodies
int ast_sip_pubsub_register_body_supplement(struct ast_sip_pubsub_body_supplement *supplement)
Register a body generator with the pubsub core.
static int unload_module(void)
void ast_sip_pubsub_unregister_body_supplement(struct ast_sip_pubsub_body_supplement *supplement)
Unregister a body generator with the pubsub core.
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.
const char * ast_presence_state2str(enum ast_presence_state state)
Convert presence state to text string for output.