Asterisk - The Open Source Telephony Project  18.5.0
presence_xml.c
Go to the documentation of this file.
1 /*
2  * asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, 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 #include "asterisk.h"
20 
21 #include <pjsip.h>
22 #include <pjsip_simple.h>
23 #include <pjlib.h>
24 
25 #include "asterisk/module.h"
26 #include "asterisk/pbx.h"
28 
29 void ast_sip_sanitize_xml(const char *input, char *output, size_t len)
30 {
31  char *copy = ast_strdupa(input);
32  char *break_point;
33  size_t remaining = len - 1;
34 
35  output[0] = '\0';
36 
37  while ((break_point = strpbrk(copy, "<>\"&'\n\r")) && remaining) {
38  char to_escape = *break_point;
39 
40  *break_point = '\0';
41  strncat(output, copy, remaining);
42 
43  /* The strncat function will write remaining+1 if the string length is
44  * equal to or greater than the size provided to it. We take this into
45  * account by subtracting 1, which ensures that the NULL byte is written
46  * inside of the provided buffer.
47  */
48  remaining = len - strlen(output) - 1;
49 
50  switch (to_escape) {
51  case '<':
52  strncat(output, "&lt;", remaining);
53  break;
54  case '>':
55  strncat(output, "&gt;", remaining);
56  break;
57  case '"':
58  strncat(output, "&quot;", remaining);
59  break;
60  case '&':
61  strncat(output, "&amp;", remaining);
62  break;
63  case '\'':
64  strncat(output, "&apos;", remaining);
65  break;
66  case '\r':
67  strncat(output, "&#13;", remaining);
68  break;
69  case '\n':
70  strncat(output, "&#10;", remaining);
71  break;
72  };
73 
74  copy = break_point + 1;
75  remaining = len - strlen(output) - 1;
76  }
77 
78  /* Be sure to copy everything after the final bracket */
79  if (*copy && remaining) {
80  strncat(output, copy, remaining);
81  }
82 }
83 
84 void ast_sip_presence_exten_state_to_str(int state, char **statestring, char **pidfstate,
85  char **pidfnote, enum ast_sip_pidf_state *local_state,
86  unsigned int notify_early_inuse_ringing)
87 {
88  switch (state) {
90  *statestring = "early";
91  *local_state = NOTIFY_INUSE;
92  *pidfstate = "on-the-phone";
93  *pidfnote = "Ringing";
94  break;
96  if (notify_early_inuse_ringing) {
97  *statestring = "early";
98  } else {
99  *statestring = "confirmed";
100  }
101  *local_state = NOTIFY_INUSE;
102  *pidfstate = "on-the-phone";
103  *pidfnote = "Ringing";
104  break;
105  case AST_EXTENSION_INUSE:
106  *statestring = "confirmed";
107  *local_state = NOTIFY_INUSE;
108  *pidfstate = "on-the-phone";
109  *pidfnote = "On the phone";
110  break;
111  case AST_EXTENSION_BUSY:
112  *statestring = "confirmed";
113  *local_state = NOTIFY_INUSE;
114  *pidfstate = "on-the-phone";
115  *pidfnote = "On the phone";
116  break;
118  *statestring = "terminated";
119  *local_state = NOTIFY_CLOSED;
120  *pidfstate = "--";
121  *pidfnote = "Unavailable";
122  break;
124  *statestring = "confirmed";
125  *local_state = NOTIFY_INUSE;
126  *pidfstate = "on-the-phone";
127  *pidfnote = "On hold";
128  break;
130  default:
131  /* Default setting */
132  *statestring = "terminated";
133  *local_state = NOTIFY_OPEN;
134  *pidfstate = "--";
135  *pidfnote = "Ready";
136  break;
137  }
138 }
139 
140 pj_xml_attr *ast_sip_presence_xml_create_attr(pj_pool_t *pool,
141  pj_xml_node *node, const char *name, const char *value)
142 {
143  pj_xml_attr *attr = PJ_POOL_ALLOC_T(pool, pj_xml_attr);
144 
145  pj_strdup2(pool, &attr->name, name);
146  pj_strdup2(pool, &attr->value, value);
147 
148  pj_xml_add_attr(node, attr);
149  return attr;
150 }
151 
152 pj_xml_node *ast_sip_presence_xml_create_node(pj_pool_t *pool,
153  pj_xml_node *parent, const char* name)
154 {
155  pj_xml_node *node = PJ_POOL_ALLOC_T(pool, pj_xml_node);
156 
157  pj_list_init(&node->attr_head);
158  pj_list_init(&node->node_head);
159 
160  pj_strdup2(pool, &node->name, name);
161 
162  node->content.ptr = NULL;
163  node->content.slen = 0;
164 
165  if (parent) {
166  pj_xml_add_node(parent, node);
167  }
168 
169  return node;
170 }
171 
173  pj_xml_node *parent, const char *node_name, const char *attr_name,
174  pj_xml_node **node, pj_xml_attr **attr)
175 {
176  pj_str_t name;
177 
178  if (!(*node = pj_xml_find_node(parent, pj_cstr(&name, node_name)))) {
179  *node = ast_sip_presence_xml_create_node(pool, parent, node_name);
180  }
181 
182  if (!(*attr = pj_xml_find_attr(*node, pj_cstr(&name, attr_name), NULL))) {
183  *attr = ast_sip_presence_xml_create_attr(pool, *node, attr_name, "");
184  }
185 }
Definition: test_heap.c:38
Asterisk main include file. File version handling, generic pbx functions.
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
static pj_pool_t * pool
Global memory pool for configuration and timers.
static int copy(char *infile, char *outfile)
Utility function to copy a file.
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
#define NULL
Definition: resample.c:96
int value
Definition: syslog.c:37
static int input(yyscan_t yyscanner)
Definition: ast_expr2f.c:1584
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_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
Core PBX routines and definitions.
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
static const char name[]
Definition: cdr_mysql.c:74
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
ast_sip_pidf_state
Asterisk module definitions.
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