| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include <pjsip-simple/rpid.h> |
| 20 |
#include <pjsip-simple/errno.h> |
| 21 |
#include <pj/assert.h> |
| 22 |
#include <pj/guid.h> |
| 23 |
#include <pj/pool.h> |
| 24 |
#include <pj/string.h> |
| 25 |
|
| 26 |
|
| 27 |
static const pj_str_t DM_NAME = {"xmlns:dm", 8}; |
| 28 |
static const pj_str_t DM_VAL = {"urn:ietf:params:xml:ns:pidf:data-model", 38}; |
| 29 |
static const pj_str_t RPID_NAME = {"xmlns:rpid", 10}; |
| 30 |
static const pj_str_t RPID_VAL = {"urn:ietf:params:xml:ns:pidf:rpid", 32}; |
| 31 |
|
| 32 |
static const pj_str_t DM_NOTE = {"dm:note", 7}; |
| 33 |
static const pj_str_t DM_PERSON = {"dm:person", 9}; |
| 34 |
static const pj_str_t ID = {"id", 2}; |
| 35 |
static const pj_str_t NOTE = {"note", 4}; |
| 36 |
static const pj_str_t RPID_ACTIVITIES = {"rpid:activities", 15}; |
| 37 |
static const pj_str_t RPID_AWAY = {"rpid:away", 9}; |
| 38 |
static const pj_str_t RPID_BUSY = {"rpid:busy", 9}; |
| 39 |
static const pj_str_t RPID_UNKNOWN = {"rpid:unknown", 12}; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
PJ_DEF(void) pjrpid_element_dup(pj_pool_t *pool, pjrpid_element *dst, |
| 44 |
const pjrpid_element *src) |
| 45 |
{ |
| 46 |
pj_memcpy(dst, src, sizeof(pjrpid_element)); |
| 47 |
pj_strdup(pool, &dst->id, &src->id); |
| 48 |
pj_strdup(pool, &dst->note, &src->note); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
static void update_namespaces(pjpidf_pres *pres, |
| 54 |
pj_pool_t *pool) |
| 55 |
{ |
| 56 |
|
| 57 |
if (pj_xml_find_attr(pres, &DM_NAME, NULL) != NULL) |
| 58 |
return; |
| 59 |
|
| 60 |
pj_xml_add_attr(pres, pj_xml_attr_new(pool, &DM_NAME, &DM_VAL)); |
| 61 |
pj_xml_add_attr(pres, pj_xml_attr_new(pool, &RPID_NAME, &RPID_VAL)); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
static pj_bool_t substring_match(const pj_xml_node *node, |
| 67 |
const char *part_name, |
| 68 |
pj_ssize_t part_len) |
| 69 |
{ |
| 70 |
pj_str_t end_name; |
| 71 |
|
| 72 |
if (part_len < 1) |
| 73 |
part_len = pj_ansi_strlen(part_name); |
| 74 |
|
| 75 |
if (node->name.slen < part_len) |
| 76 |
return PJ_FALSE; |
| 77 |
|
| 78 |
end_name.ptr = node->name.ptr + (node->name.slen - part_len); |
| 79 |
end_name.slen = part_len; |
| 80 |
|
| 81 |
return pj_strnicmp2(&end_name, part_name, part_len)==0; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
static pj_xml_node *find_node(const pj_xml_node *parent, |
| 86 |
const char *part_name) |
| 87 |
{ |
| 88 |
const pj_xml_node *node = parent->node_head.next, |
| 89 |
*head = (pj_xml_node*) &parent->node_head; |
| 90 |
pj_ssize_t part_len = pj_ansi_strlen(part_name); |
| 91 |
|
| 92 |
while (node != head) { |
| 93 |
if (substring_match(node, part_name, part_len)) |
| 94 |
return (pj_xml_node*) node; |
| 95 |
|
| 96 |
node = node->next; |
| 97 |
} |
| 98 |
|
| 99 |
return NULL; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
PJ_DEF(pj_status_t) pjrpid_add_element(pjpidf_pres *pres, |
| 106 |
pj_pool_t *pool, |
| 107 |
unsigned options, |
| 108 |
const pjrpid_element *elem) |
| 109 |
{ |
| 110 |
pj_xml_node *nd_person, *nd_activities, *nd_activity, *nd_note; |
| 111 |
pj_xml_attr *attr; |
| 112 |
|
| 113 |
PJ_ASSERT_RETURN(pres && pool && options==0 && elem, PJ_EINVAL); |
| 114 |
|
| 115 |
PJ_UNUSED_ARG(options); |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
if (elem->note.slen != 0) { |
| 121 |
pj_xml_node *nd_tuple; |
| 122 |
|
| 123 |
nd_tuple = find_node(pres, "tuple"); |
| 124 |
nd_note = nd_tuple? find_node(nd_tuple, "note"): |
| 125 |
find_node(pres, "note"); |
| 126 |
if (!nd_note) { |
| 127 |
nd_note = pj_xml_node_new(pool, &NOTE); |
| 128 |
pj_strdup(pool, &nd_note->content, &elem->note); |
| 129 |
pj_xml_add_node(nd_tuple? nd_tuple: pres, nd_note); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
if (elem->id.slen==0 && |
| 135 |
elem->activity==PJRPID_ACTIVITY_UNKNOWN) |
| 136 |
{ |
| 137 |
|
| 138 |
return PJ_SUCCESS; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
update_namespaces(pres, pool); |
| 143 |
|
| 144 |
|
| 145 |
nd_person = pj_xml_node_new(pool, &DM_PERSON); |
| 146 |
if (elem->id.slen != 0) { |
| 147 |
attr = pj_xml_attr_new(pool, &ID, &elem->id); |
| 148 |
} else { |
| 149 |
pj_str_t person_id; |
| 150 |
|
| 151 |
|
| 152 |
person_id.ptr = (char*)pj_pool_alloc(pool, pj_GUID_STRING_LENGTH()+2); |
| 153 |
person_id.ptr += 2; |
| 154 |
pj_generate_unique_string(&person_id); |
| 155 |
person_id.ptr -= 2; |
| 156 |
person_id.ptr[0] = 'p'; |
| 157 |
person_id.ptr[1] = 'j'; |
| 158 |
person_id.slen += 2; |
| 159 |
|
| 160 |
attr = pj_xml_attr_new(pool, &ID, &person_id); |
| 161 |
} |
| 162 |
pj_xml_add_attr(nd_person, attr); |
| 163 |
pj_xml_add_node(pres, nd_person); |
| 164 |
|
| 165 |
|
| 166 |
nd_activities = pj_xml_node_new(pool, &RPID_ACTIVITIES); |
| 167 |
pj_xml_add_node(nd_person, nd_activities); |
| 168 |
|
| 169 |
|
| 170 |
switch (elem->activity) { |
| 171 |
case PJRPID_ACTIVITY_AWAY: |
| 172 |
nd_activity = pj_xml_node_new(pool, &RPID_AWAY); |
| 173 |
break; |
| 174 |
case PJRPID_ACTIVITY_BUSY: |
| 175 |
nd_activity = pj_xml_node_new(pool, &RPID_BUSY); |
| 176 |
break; |
| 177 |
case PJRPID_ACTIVITY_UNKNOWN: |
| 178 |
default: |
| 179 |
nd_activity = pj_xml_node_new(pool, &RPID_UNKNOWN); |
| 180 |
break; |
| 181 |
} |
| 182 |
pj_xml_add_node(nd_activities, nd_activity); |
| 183 |
|
| 184 |
|
| 185 |
if (elem->note.slen != 0) { |
| 186 |
nd_note = pj_xml_node_new(pool, &DM_NOTE); |
| 187 |
pj_strdup(pool, &nd_note->content, &elem->note); |
| 188 |
pj_xml_add_node(nd_person, nd_note); |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
return PJ_SUCCESS; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
static pj_status_t get_tuple_note(const pjpidf_pres *pres, |
| 198 |
pj_pool_t *pool, |
| 199 |
pjrpid_element *elem) |
| 200 |
{ |
| 201 |
const pj_xml_node *nd_tuple, *nd_note; |
| 202 |
|
| 203 |
nd_tuple = find_node(pres, "tuple"); |
| 204 |
if (!nd_tuple) |
| 205 |
return PJSIP_SIMPLE_EBADRPID; |
| 206 |
|
| 207 |
nd_note = find_node(nd_tuple, "note"); |
| 208 |
|
| 209 |
if (!nd_note) |
| 210 |
nd_note = find_node(pres, "note"); |
| 211 |
|
| 212 |
if (nd_note) { |
| 213 |
pj_strdup(pool, &elem->note, &nd_note->content); |
| 214 |
return PJ_SUCCESS; |
| 215 |
} |
| 216 |
|
| 217 |
return PJSIP_SIMPLE_EBADRPID; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
PJ_DEF(pj_status_t) pjrpid_get_element(const pjpidf_pres *pres, |
| 224 |
pj_pool_t *pool, |
| 225 |
pjrpid_element *elem) |
| 226 |
{ |
| 227 |
const pj_xml_node *nd_person, *nd_activities, *nd_note = NULL; |
| 228 |
const pj_xml_attr *attr; |
| 229 |
|
| 230 |
|
| 231 |
pj_bzero(elem, sizeof(*elem)); |
| 232 |
elem->activity = PJRPID_ACTIVITY_UNKNOWN; |
| 233 |
|
| 234 |
|
| 235 |
nd_person = find_node(pres, "person"); |
| 236 |
if (!nd_person) { |
| 237 |
|
| 238 |
return get_tuple_note(pres, pool, elem); |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
attr = pj_xml_find_attr((pj_xml_node*)nd_person, &ID, NULL); |
| 243 |
if (attr) |
| 244 |
pj_strdup(pool, &elem->id, &attr->value); |
| 245 |
|
| 246 |
|
| 247 |
nd_activities = find_node(nd_person, "activities"); |
| 248 |
if (nd_activities) { |
| 249 |
const pj_xml_node *nd_activity; |
| 250 |
|
| 251 |
|
| 252 |
nd_note = find_node(nd_activities, "note"); |
| 253 |
|
| 254 |
|
| 255 |
nd_activity = nd_activities->node_head.next; |
| 256 |
PJ_ASSERT_RETURN(nd_activity && nd_activity->next, PJ_EBUG); |
| 257 |
|
| 258 |
if (nd_activity == nd_note) |
| 259 |
nd_activity = nd_activity->next; |
| 260 |
|
| 261 |
if (nd_activity != (pj_xml_node*) &nd_activities->node_head) { |
| 262 |
if (substring_match(nd_activity, "busy", -1)) |
| 263 |
elem->activity = PJRPID_ACTIVITY_BUSY; |
| 264 |
else if (substring_match(nd_activity, "away", -1)) |
| 265 |
elem->activity = PJRPID_ACTIVITY_AWAY; |
| 266 |
else |
| 267 |
elem->activity = PJRPID_ACTIVITY_UNKNOWN; |
| 268 |
|
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
if (nd_note == NULL) |
| 274 |
nd_note = find_node(nd_person, "note"); |
| 275 |
|
| 276 |
if (nd_note) { |
| 277 |
pj_strdup(pool, &elem->note, &nd_note->content); |
| 278 |
} else { |
| 279 |
get_tuple_note(pres, pool, elem); |
| 280 |
} |
| 281 |
|
| 282 |
return PJ_SUCCESS; |
| 283 |
} |
| 284 |
|
| 285 |
|