res_pjsip: New endpoint option "notify_early_inuse_ringing"
[asterisk/asterisk.git] / res / res_pjsip / presence_xml.c
1 /*
2  * asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Kevin Harwell <kharwell@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 #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"
27 #include "asterisk/res_pjsip_presence_xml.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) {
89         case AST_EXTENSION_RINGING:
90                 *statestring = "early";
91                 *local_state = NOTIFY_INUSE;
92                 *pidfstate = "busy";
93                 *pidfnote = "Ringing";
94                 break;
95         case (AST_EXTENSION_INUSE | AST_EXTENSION_RINGING):
96                 if (notify_early_inuse_ringing) {
97                         *statestring = "early";
98                 } else {
99                         *statestring = "confirmed";
100                 }
101                 *local_state = NOTIFY_INUSE;
102                 *pidfstate = "busy";
103                 *pidfnote = "Ringing";
104                 break;
105         case AST_EXTENSION_INUSE:
106                 *statestring = "confirmed";
107                 *local_state = NOTIFY_INUSE;
108                 *pidfstate = "busy";
109                 *pidfnote = "On the phone";
110                 break;
111         case AST_EXTENSION_BUSY:
112                 *statestring = "confirmed";
113                 *local_state = NOTIFY_CLOSED;
114                 *pidfstate = "busy";
115                 *pidfnote = "On the phone";
116                 break;
117         case AST_EXTENSION_UNAVAILABLE:
118                 *statestring = "terminated";
119                 *local_state = NOTIFY_CLOSED;
120                 *pidfstate = "away";
121                 *pidfnote = "Unavailable";
122                 break;
123         case AST_EXTENSION_ONHOLD:
124                 *statestring = "confirmed";
125                 *local_state = NOTIFY_CLOSED;
126                 *pidfstate = "busy";
127                 *pidfnote = "On hold";
128                 break;
129         case AST_EXTENSION_NOT_INUSE:
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
172 void ast_sip_presence_xml_find_node_attr(pj_pool_t* pool,
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 }