| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include <pjsip-simple/iscomposing.h> |
| 20 |
#include <pjsip-simple/errno.h> |
| 21 |
#include <pjsip/sip_msg.h> |
| 22 |
#include <pjlib-util/errno.h> |
| 23 |
#include <pj/pool.h> |
| 24 |
#include <pj/string.h> |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
static const pj_str_t STR_MIME_TYPE = { "application", 11 }; |
| 29 |
static const pj_str_t STR_MIME_SUBTYPE = { "im-iscomposing+xml", 18 }; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
static const pj_str_t STR_ISCOMPOSING = { "isComposing", 11 }; |
| 34 |
static const pj_str_t STR_STATE = { "state", 5 }; |
| 35 |
static const pj_str_t STR_ACTIVE = { "active", 6 }; |
| 36 |
static const pj_str_t STR_IDLE = { "idle", 4 }; |
| 37 |
static const pj_str_t STR_LASTACTIVE = { "lastactive", 10 }; |
| 38 |
static const pj_str_t STR_CONTENTTYPE = { "contenttype", 11 }; |
| 39 |
static const pj_str_t STR_REFRESH = { "refresh", 7 }; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
static const pj_str_t STR_XMLNS_NAME = { "xmlns", 5 }; |
| 44 |
static const pj_str_t STR_XMLNS_VAL = { "urn:ietf:params:xml:ns:im-iscomposing", 37 }; |
| 45 |
static const pj_str_t STR_XMLNS_XSI_NAME = { "xmlns:xsi", 9 }; |
| 46 |
static const pj_str_t STR_XMLNS_XSI_VAL = { "http://www.w3.org/2001/XMLSchema-instance", 41 }; |
| 47 |
static const pj_str_t STR_XSI_SLOC_NAME = { "xsi:schemaLocation", 18 }; |
| 48 |
static const pj_str_t STR_XSI_SLOC_VAL = { "urn:ietf:params:xml:ns:im-composing iscomposing.xsd", 51 }; |
| 49 |
|
| 50 |
|
| 51 |
PJ_DEF(pj_xml_node*) pjsip_iscomposing_create_xml( pj_pool_t *pool, |
| 52 |
pj_bool_t is_composing, |
| 53 |
const pj_time_val *lst_actv, |
| 54 |
const pj_str_t *content_tp, |
| 55 |
int refresh) |
| 56 |
{ |
| 57 |
pj_xml_node *doc, *node; |
| 58 |
pj_xml_attr *attr; |
| 59 |
|
| 60 |
|
| 61 |
doc = pj_xml_node_new(pool, &STR_ISCOMPOSING); |
| 62 |
|
| 63 |
|
| 64 |
attr = pj_xml_attr_new(pool, &STR_XMLNS_NAME, &STR_XMLNS_VAL); |
| 65 |
pj_xml_add_attr(doc, attr); |
| 66 |
|
| 67 |
attr = pj_xml_attr_new(pool, &STR_XMLNS_XSI_NAME, &STR_XMLNS_XSI_VAL); |
| 68 |
pj_xml_add_attr(doc, attr); |
| 69 |
|
| 70 |
attr = pj_xml_attr_new(pool, &STR_XSI_SLOC_NAME, &STR_XSI_SLOC_VAL); |
| 71 |
pj_xml_add_attr(doc, attr); |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
node = pj_xml_node_new(pool, &STR_STATE); |
| 76 |
if (is_composing) |
| 77 |
node->content = STR_ACTIVE; |
| 78 |
else |
| 79 |
node->content = STR_IDLE; |
| 80 |
pj_xml_add_node(doc, node); |
| 81 |
|
| 82 |
|
| 83 |
PJ_UNUSED_ARG(lst_actv); |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
if (content_tp) { |
| 90 |
node = pj_xml_node_new(pool, &STR_CONTENTTYPE); |
| 91 |
pj_strdup(pool, &node->content, content_tp); |
| 92 |
pj_xml_add_node(doc, node); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
if (is_composing && refresh > 1 && refresh < 3601) { |
| 97 |
node = pj_xml_node_new(pool, &STR_REFRESH); |
| 98 |
node->content.ptr = (char*) pj_pool_alloc(pool, 10); |
| 99 |
node->content.slen = pj_utoa(refresh, node->content.ptr); |
| 100 |
pj_xml_add_node(doc, node); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
return doc; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
static int xml_print_body( struct pjsip_msg_body *msg_body, |
| 114 |
char *buf, pj_size_t size) |
| 115 |
{ |
| 116 |
return pj_xml_print((const pj_xml_node*)msg_body->data, buf, size, |
| 117 |
PJ_TRUE); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
static void* xml_clone_data(pj_pool_t *pool, const void *data, unsigned len) |
| 125 |
{ |
| 126 |
PJ_UNUSED_ARG(len); |
| 127 |
return pj_xml_clone( pool, (const pj_xml_node*)data); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
PJ_DEF(pjsip_msg_body*) pjsip_iscomposing_create_body( pj_pool_t *pool, |
| 133 |
pj_bool_t is_composing, |
| 134 |
const pj_time_val *lst_actv, |
| 135 |
const pj_str_t *content_tp, |
| 136 |
int refresh) |
| 137 |
{ |
| 138 |
pj_xml_node *doc; |
| 139 |
pjsip_msg_body *body; |
| 140 |
|
| 141 |
doc = pjsip_iscomposing_create_xml( pool, is_composing, lst_actv, |
| 142 |
content_tp, refresh); |
| 143 |
if (doc == NULL) |
| 144 |
return NULL; |
| 145 |
|
| 146 |
|
| 147 |
body = PJ_POOL_ZALLOC_T(pool, pjsip_msg_body); |
| 148 |
body->content_type.type = STR_MIME_TYPE; |
| 149 |
body->content_type.subtype = STR_MIME_SUBTYPE; |
| 150 |
|
| 151 |
body->data = doc; |
| 152 |
body->len = 0; |
| 153 |
|
| 154 |
body->print_body = &xml_print_body; |
| 155 |
body->clone_data = &xml_clone_data; |
| 156 |
|
| 157 |
return body; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
PJ_DEF(pj_status_t) pjsip_iscomposing_parse( pj_pool_t *pool, |
| 162 |
char *msg, |
| 163 |
pj_size_t len, |
| 164 |
pj_bool_t *p_is_composing, |
| 165 |
pj_str_t **p_last_active, |
| 166 |
pj_str_t **p_content_type, |
| 167 |
int *p_refresh ) |
| 168 |
{ |
| 169 |
pj_xml_node *doc, *node; |
| 170 |
|
| 171 |
|
| 172 |
if (p_is_composing) *p_is_composing = PJ_FALSE; |
| 173 |
if (p_last_active) *p_last_active = NULL; |
| 174 |
if (p_content_type) *p_content_type = NULL; |
| 175 |
|
| 176 |
|
| 177 |
doc = pj_xml_parse( pool, msg, len); |
| 178 |
if (!doc) |
| 179 |
return PJLIB_UTIL_EINXML; |
| 180 |
|
| 181 |
|
| 182 |
if (pj_stricmp(&doc->name, &STR_ISCOMPOSING) != 0) |
| 183 |
return PJSIP_SIMPLE_EBADISCOMPOSE; |
| 184 |
|
| 185 |
|
| 186 |
if (p_is_composing) { |
| 187 |
node = pj_xml_find_node(doc, &STR_STATE); |
| 188 |
if (node == NULL) |
| 189 |
return PJSIP_SIMPLE_EBADISCOMPOSE; |
| 190 |
*p_is_composing = (pj_stricmp(&node->content, &STR_ACTIVE)==0); |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
if (p_last_active) { |
| 195 |
node = pj_xml_find_node(doc, &STR_LASTACTIVE); |
| 196 |
if (node) |
| 197 |
*p_last_active = &node->content; |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
if (p_content_type) { |
| 202 |
node = pj_xml_find_node(doc, &STR_CONTENTTYPE); |
| 203 |
if (node) |
| 204 |
*p_content_type = &node->content; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
if (p_refresh) { |
| 209 |
node = pj_xml_find_node(doc, &STR_REFRESH); |
| 210 |
if (node) |
| 211 |
*p_refresh = pj_strtoul(&node->content); |
| 212 |
} |
| 213 |
|
| 214 |
return PJ_SUCCESS; |
| 215 |
} |
| 216 |
|
| 217 |
|