Check for an act on failures to update contacts during registration.
[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 /*** MODULEINFO
20         <depend>pjproject</depend>
21         <depend>res_pjsip</depend>
22         <depend>res_pjsip_pubsub</depend>
23         <depend>res_pjsip_exten_state</depend>
24         <support_level>core</support_level>
25  ***/
26
27 #include "asterisk.h"
28
29 #include <pjsip.h>
30 #include <pjsip_simple.h>
31 #include <pjlib.h>
32
33 #include "asterisk/module.h"
34 #include "asterisk/res_pjsip.h"
35 #include "asterisk/res_pjsip_pubsub.h"
36 #include "asterisk/res_pjsip_presence_xml.h"
37 #include "asterisk/res_pjsip_body_generator_types.h"
38
39 void ast_sip_sanitize_xml(const char *input, char *output, size_t len)
40 {
41         char *copy = ast_strdupa(input);
42         char *break_point;
43
44         output[0] = '\0';
45
46         while ((break_point = strpbrk(copy, "<>\"&'"))) {
47                 char to_escape = *break_point;
48
49                 *break_point = '\0';
50                 strncat(output, copy, len);
51
52                 switch (to_escape) {
53                 case '<':
54                         strncat(output, "&lt;", len);
55                         break;
56                 case '>':
57                         strncat(output, "&gt;", len);
58                         break;
59                 case '"':
60                         strncat(output, "&quot;", len);
61                         break;
62                 case '&':
63                         strncat(output, "&amp;", len);
64                         break;
65                 case '\'':
66                         strncat(output, "&apos;", len);
67                         break;
68                 };
69
70                 copy = break_point + 1;
71         }
72
73         /* Be sure to copy everything after the final bracket */
74         if (*copy) {
75                 strncat(output, copy, len);
76         }
77 }
78
79 void ast_sip_presence_exten_state_to_str(int state, char **statestring, char **pidfstate,
80                                char **pidfnote, enum ast_sip_pidf_state *local_state)
81 {
82         switch (state) {
83         case AST_EXTENSION_RINGING:
84                 *statestring = "early";
85                 *local_state = NOTIFY_INUSE;
86                 *pidfstate = "busy";
87                 *pidfnote = "Ringing";
88                 break;
89         case AST_EXTENSION_INUSE:
90                 *statestring = "confirmed";
91                 *local_state = NOTIFY_INUSE;
92                 *pidfstate = "busy";
93                 *pidfnote = "On the phone";
94                 break;
95         case AST_EXTENSION_BUSY:
96                 *statestring = "confirmed";
97                 *local_state = NOTIFY_CLOSED;
98                 *pidfstate = "busy";
99                 *pidfnote = "On the phone";
100                 break;
101         case AST_EXTENSION_UNAVAILABLE:
102                 *statestring = "terminated";
103                 *local_state = NOTIFY_CLOSED;
104                 *pidfstate = "away";
105                 *pidfnote = "Unavailable";
106                 break;
107         case AST_EXTENSION_ONHOLD:
108                 *statestring = "confirmed";
109                 *local_state = NOTIFY_CLOSED;
110                 *pidfstate = "busy";
111                 *pidfnote = "On hold";
112                 break;
113         case AST_EXTENSION_NOT_INUSE:
114         default:
115                 /* Default setting */
116                 *statestring = "terminated";
117                 *local_state = NOTIFY_OPEN;
118                 *pidfstate = "--";
119                 *pidfnote ="Ready";
120                 break;
121         }
122 }
123
124 pj_xml_attr *ast_sip_presence_xml_create_attr(pj_pool_t *pool,
125                 pj_xml_node *node, const char *name, const char *value)
126 {
127         pj_xml_attr *attr = PJ_POOL_ALLOC_T(pool, pj_xml_attr);
128
129         pj_strdup2(pool, &attr->name, name);
130         pj_strdup2(pool, &attr->value, value);
131
132         pj_xml_add_attr(node, attr);
133         return attr;
134 }
135
136 pj_xml_node *ast_sip_presence_xml_create_node(pj_pool_t *pool,
137                 pj_xml_node *parent, const char* name)
138 {
139         pj_xml_node *node = PJ_POOL_ALLOC_T(pool, pj_xml_node);
140
141         pj_list_init(&node->attr_head);
142         pj_list_init(&node->node_head);
143
144         pj_strdup2(pool, &node->name, name);
145
146         node->content.ptr = NULL;
147         node->content.slen = 0;
148
149         pj_xml_add_node(parent, node);
150         return node;
151 }
152
153 void ast_sip_presence_xml_find_node_attr(pj_pool_t* pool,
154                 pj_xml_node *parent, const char *node_name, const char *attr_name,
155                 pj_xml_node **node, pj_xml_attr **attr)
156 {
157         pj_str_t name;
158
159         if (!(*node = pj_xml_find_node(parent, pj_cstr(&name, node_name)))) {
160                 *node = ast_sip_presence_xml_create_node(pool, parent, node_name);
161         }
162
163         if (!(*attr = pj_xml_find_attr(*node, pj_cstr(&name, attr_name), NULL))) {
164                 *attr = ast_sip_presence_xml_create_attr(pool, *node, attr_name, "");
165         }
166 }