| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 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 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
remaining = len - strlen(output) - 1; |
| 49 |
|
| 50 |
switch (to_escape) { |
| 51 |
case '<': |
| 52 |
strncat(output, "<", remaining); |
| 53 |
break; |
| 54 |
case '>': |
| 55 |
strncat(output, ">", remaining); |
| 56 |
break; |
| 57 |
case '"': |
| 58 |
strncat(output, """, remaining); |
| 59 |
break; |
| 60 |
case '&': |
| 61 |
strncat(output, "&", remaining); |
| 62 |
break; |
| 63 |
case '\'': |
| 64 |
strncat(output, "'", remaining); |
| 65 |
break; |
| 66 |
case '\r': |
| 67 |
strncat(output, " ", remaining); |
| 68 |
break; |
| 69 |
case '\n': |
| 70 |
strncat(output, " ", remaining); |
| 71 |
break; |
| 72 |
}; |
| 73 |
|
| 74 |
copy = break_point + 1; |
| 75 |
remaining = len - strlen(output) - 1; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 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 = "on-the-phone"; |
| 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 = "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; |
| 117 |
case AST_EXTENSION_UNAVAILABLE: |
| 118 |
*statestring = "terminated"; |
| 119 |
*local_state = NOTIFY_CLOSED; |
| 120 |
*pidfstate = "--"; |
| 121 |
*pidfnote = "Unavailable"; |
| 122 |
break; |
| 123 |
case AST_EXTENSION_ONHOLD: |
| 124 |
*statestring = "confirmed"; |
| 125 |
*local_state = NOTIFY_INUSE; |
| 126 |
*pidfstate = "on-the-phone"; |
| 127 |
*pidfnote = "On hold"; |
| 128 |
break; |
| 129 |
case AST_EXTENSION_NOT_INUSE: |
| 130 |
default: |
| 131 |
|
| 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 |
} |