| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include <pjsip-simple/xpidf.h> |
| 20 |
#include <pj/assert.h> |
| 21 |
#include <pj/guid.h> |
| 22 |
#include <pj/pool.h> |
| 23 |
#include <pj/string.h> |
| 24 |
|
| 25 |
static pj_str_t STR_PRESENCE = { "presence", 8 }; |
| 26 |
static pj_str_t STR_STATUS = { "status", 6 }; |
| 27 |
static pj_str_t STR_OPEN = { "open", 4 }; |
| 28 |
static pj_str_t STR_CLOSED = { "closed", 6 }; |
| 29 |
static pj_str_t STR_URI = { "uri", 3 }; |
| 30 |
static pj_str_t STR_ATOM = { "atom", 4 }; |
| 31 |
static pj_str_t STR_ATOMID = { "atomid", 6 }; |
| 32 |
static pj_str_t STR_ID = { "id", 2 }; |
| 33 |
static pj_str_t STR_ADDRESS = { "address", 7 }; |
| 34 |
static pj_str_t STR_SUBSCRIBE_PARAM = { ";method=SUBSCRIBE", 17 }; |
| 35 |
static pj_str_t STR_PRESENTITY = { "presentity", 10 }; |
| 36 |
static pj_str_t STR_EMPTY_STRING = { NULL, 0 }; |
| 37 |
|
| 38 |
static pj_xml_node* xml_create_node(pj_pool_t *pool, |
| 39 |
pj_str_t *name, const pj_str_t *value) |
| 40 |
{ |
| 41 |
pj_xml_node *node; |
| 42 |
|
| 43 |
node = PJ_POOL_ALLOC_T(pool, pj_xml_node); |
| 44 |
pj_list_init(&node->attr_head); |
| 45 |
pj_list_init(&node->node_head); |
| 46 |
node->name = *name; |
| 47 |
if (value) pj_strdup(pool, &node->content, value); |
| 48 |
else node->content.ptr=NULL, node->content.slen=0; |
| 49 |
|
| 50 |
return node; |
| 51 |
} |
| 52 |
|
| 53 |
static pj_xml_attr* xml_create_attr(pj_pool_t *pool, pj_str_t *name, |
| 54 |
const pj_str_t *value) |
| 55 |
{ |
| 56 |
pj_xml_attr *attr = PJ_POOL_ALLOC_T(pool, pj_xml_attr); |
| 57 |
attr->name = *name; |
| 58 |
pj_strdup(pool, &attr->value, value); |
| 59 |
return attr; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
PJ_DEF(pjxpidf_pres*) pjxpidf_create(pj_pool_t *pool, const pj_str_t *uri_cstr) |
| 64 |
{ |
| 65 |
pjxpidf_pres *pres; |
| 66 |
pj_xml_node *presentity; |
| 67 |
pj_xml_node *atom; |
| 68 |
pj_xml_node *addr; |
| 69 |
pj_xml_node *status; |
| 70 |
pj_xml_attr *attr; |
| 71 |
pj_str_t uri; |
| 72 |
pj_str_t tmp; |
| 73 |
|
| 74 |
|
| 75 |
pres = xml_create_node(pool, &STR_PRESENCE, NULL); |
| 76 |
|
| 77 |
|
| 78 |
presentity = xml_create_node(pool, &STR_PRESENTITY, NULL); |
| 79 |
pj_xml_add_node(pres, presentity); |
| 80 |
|
| 81 |
|
| 82 |
uri.ptr = (char*) pj_pool_alloc(pool, uri_cstr->slen + |
| 83 |
STR_SUBSCRIBE_PARAM.slen); |
| 84 |
pj_strcpy( &uri, uri_cstr); |
| 85 |
pj_strcat( &uri, &STR_SUBSCRIBE_PARAM); |
| 86 |
attr = xml_create_attr(pool, &STR_URI, &uri); |
| 87 |
pj_xml_add_attr(presentity, attr); |
| 88 |
|
| 89 |
|
| 90 |
atom = xml_create_node(pool, &STR_ATOM, NULL); |
| 91 |
pj_xml_add_node(pres, atom); |
| 92 |
|
| 93 |
|
| 94 |
pj_create_unique_string(pool, &tmp); |
| 95 |
attr = xml_create_attr(pool, &STR_ATOMID, &tmp); |
| 96 |
pj_xml_add_attr(atom, attr); |
| 97 |
|
| 98 |
|
| 99 |
addr = xml_create_node(pool, &STR_ADDRESS, NULL); |
| 100 |
pj_xml_add_node(atom, addr); |
| 101 |
|
| 102 |
|
| 103 |
attr = xml_create_attr(pool, &STR_URI, uri_cstr); |
| 104 |
pj_xml_add_attr(addr, attr); |
| 105 |
|
| 106 |
|
| 107 |
status = xml_create_node(pool, &STR_STATUS, NULL); |
| 108 |
pj_xml_add_node(addr, status); |
| 109 |
|
| 110 |
|
| 111 |
attr = xml_create_attr(pool, &STR_STATUS, &STR_OPEN); |
| 112 |
pj_xml_add_attr(status, attr); |
| 113 |
|
| 114 |
return pres; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
PJ_DEF(pjxpidf_pres*) pjxpidf_parse(pj_pool_t *pool, char *text, pj_size_t len) |
| 120 |
{ |
| 121 |
pjxpidf_pres *pres; |
| 122 |
pj_xml_node *node; |
| 123 |
|
| 124 |
pres = pj_xml_parse(pool, text, len); |
| 125 |
if (!pres) |
| 126 |
return NULL; |
| 127 |
|
| 128 |
|
| 129 |
if (pj_stricmp(&pres->name, &STR_PRESENCE) != 0) |
| 130 |
return NULL; |
| 131 |
|
| 132 |
|
| 133 |
node = pj_xml_find_node(pres, &STR_PRESENTITY); |
| 134 |
if (node == NULL) |
| 135 |
return NULL; |
| 136 |
if (pj_xml_find_attr(node, &STR_URI, NULL) == NULL) |
| 137 |
return NULL; |
| 138 |
|
| 139 |
|
| 140 |
node = pj_xml_find_node(pres, &STR_ATOM); |
| 141 |
if (node == NULL) |
| 142 |
return NULL; |
| 143 |
if (pj_xml_find_attr(node, &STR_ATOMID, NULL) == NULL && |
| 144 |
pj_xml_find_attr(node, &STR_ID, NULL) == NULL) |
| 145 |
{ |
| 146 |
return NULL; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
node = pj_xml_find_node(node, &STR_ADDRESS); |
| 151 |
if (node == NULL) |
| 152 |
return NULL; |
| 153 |
if (pj_xml_find_attr(node, &STR_URI, NULL) == NULL) |
| 154 |
return NULL; |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
node = pj_xml_find_node(node, &STR_STATUS); |
| 159 |
if (node == NULL) |
| 160 |
return NULL; |
| 161 |
if (pj_xml_find_attr(node, &STR_STATUS, NULL) == NULL) |
| 162 |
return NULL; |
| 163 |
|
| 164 |
return pres; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
PJ_DEF(int) pjxpidf_print( pjxpidf_pres *pres, char *text, pj_size_t len) |
| 169 |
{ |
| 170 |
return pj_xml_print(pres, text, len, PJ_TRUE); |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
PJ_DEF(pj_str_t*) pjxpidf_get_uri(pjxpidf_pres *pres) |
| 175 |
{ |
| 176 |
pj_xml_node *presentity; |
| 177 |
pj_xml_attr *attr; |
| 178 |
|
| 179 |
presentity = pj_xml_find_node(pres, &STR_PRESENTITY); |
| 180 |
if (!presentity) |
| 181 |
return &STR_EMPTY_STRING; |
| 182 |
|
| 183 |
attr = pj_xml_find_attr(presentity, &STR_URI, NULL); |
| 184 |
if (!attr) |
| 185 |
return &STR_EMPTY_STRING; |
| 186 |
|
| 187 |
return &attr->value; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
PJ_DEF(pj_status_t) pjxpidf_set_uri(pj_pool_t *pool, pjxpidf_pres *pres, |
| 192 |
const pj_str_t *uri) |
| 193 |
{ |
| 194 |
pj_xml_node *presentity; |
| 195 |
pj_xml_node *atom; |
| 196 |
pj_xml_node *addr; |
| 197 |
pj_xml_attr *attr; |
| 198 |
pj_str_t dup_uri; |
| 199 |
|
| 200 |
presentity = pj_xml_find_node(pres, &STR_PRESENTITY); |
| 201 |
if (!presentity) { |
| 202 |
pj_assert(0); |
| 203 |
return -1; |
| 204 |
} |
| 205 |
atom = pj_xml_find_node(pres, &STR_ATOM); |
| 206 |
if (!atom) { |
| 207 |
pj_assert(0); |
| 208 |
return -1; |
| 209 |
} |
| 210 |
addr = pj_xml_find_node(atom, &STR_ADDRESS); |
| 211 |
if (!addr) { |
| 212 |
pj_assert(0); |
| 213 |
return -1; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
attr = pj_xml_find_attr(presentity, &STR_URI, NULL); |
| 218 |
if (!attr) { |
| 219 |
pj_assert(0); |
| 220 |
return -1; |
| 221 |
} |
| 222 |
pj_strdup(pool, &dup_uri, uri); |
| 223 |
attr->value = dup_uri; |
| 224 |
|
| 225 |
|
| 226 |
attr = pj_xml_find_attr(addr, &STR_URI, NULL); |
| 227 |
if (!attr) { |
| 228 |
pj_assert(0); |
| 229 |
return -1; |
| 230 |
} |
| 231 |
attr->value = dup_uri; |
| 232 |
|
| 233 |
return 0; |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
PJ_DEF(pj_bool_t) pjxpidf_get_status(pjxpidf_pres *pres) |
| 238 |
{ |
| 239 |
pj_xml_node *atom; |
| 240 |
pj_xml_node *addr; |
| 241 |
pj_xml_node *status; |
| 242 |
pj_xml_attr *attr; |
| 243 |
|
| 244 |
atom = pj_xml_find_node(pres, &STR_ATOM); |
| 245 |
if (!atom) { |
| 246 |
pj_assert(0); |
| 247 |
return PJ_FALSE; |
| 248 |
} |
| 249 |
addr = pj_xml_find_node(atom, &STR_ADDRESS); |
| 250 |
if (!addr) { |
| 251 |
pj_assert(0); |
| 252 |
return PJ_FALSE; |
| 253 |
} |
| 254 |
status = pj_xml_find_node(addr, &STR_STATUS); |
| 255 |
if (!status) { |
| 256 |
pj_assert(0); |
| 257 |
return PJ_FALSE; |
| 258 |
} |
| 259 |
attr = pj_xml_find_attr(status, &STR_STATUS, NULL); |
| 260 |
if (!attr) { |
| 261 |
pj_assert(0); |
| 262 |
return PJ_FALSE; |
| 263 |
} |
| 264 |
|
| 265 |
return pj_stricmp(&attr->value, &STR_OPEN)==0 ? PJ_TRUE : PJ_FALSE; |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
PJ_DEF(pj_status_t) pjxpidf_set_status(pjxpidf_pres *pres, pj_bool_t online_status) |
| 270 |
{ |
| 271 |
pj_xml_node *atom; |
| 272 |
pj_xml_node *addr; |
| 273 |
pj_xml_node *status; |
| 274 |
pj_xml_attr *attr; |
| 275 |
|
| 276 |
atom = pj_xml_find_node(pres, &STR_ATOM); |
| 277 |
if (!atom) { |
| 278 |
pj_assert(0); |
| 279 |
return -1; |
| 280 |
} |
| 281 |
addr = pj_xml_find_node(atom, &STR_ADDRESS); |
| 282 |
if (!addr) { |
| 283 |
pj_assert(0); |
| 284 |
return -1; |
| 285 |
} |
| 286 |
status = pj_xml_find_node(addr, &STR_STATUS); |
| 287 |
if (!status) { |
| 288 |
pj_assert(0); |
| 289 |
return -1; |
| 290 |
} |
| 291 |
attr = pj_xml_find_attr(status, &STR_STATUS, NULL); |
| 292 |
if (!attr) { |
| 293 |
pj_assert(0); |
| 294 |
return -1; |
| 295 |
} |
| 296 |
|
| 297 |
attr->value = ( online_status ? STR_OPEN : STR_CLOSED ); |
| 298 |
return 0; |
| 299 |
} |
| 300 |
|