Debian Codegraph @

Xpjproject/pjsip/src/pjsip-simple/rpid.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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 /* Duplicate RPID element */
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 /* Update RPID namespaces. */
53 static void update_namespaces(pjpidf_pres *pres,
54 pj_pool_t *pool)
55 {
56 /* Check if namespace is already present. */
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 /* Comparison function to find node name substring */
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 /* Util to find child node with the specified substring */
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 * Add RPID element into existing PIDF document.
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 /* Add <note>, if none. We add it to the first <tuple>, if any,
118 * otherwise to the root <presence>.
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 /* Check if we need to add RPID information into the PIDF document. */
134 if (elem->id.slen==0 &&
135 elem->activity==PJRPID_ACTIVITY_UNKNOWN)
136 {
137 /* No RPID information to be added. */
138 return PJ_SUCCESS;
139 }
140
141 /* Update namespace */
142 update_namespaces(pres, pool);
143
144 /* Add <person> */
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 /* xs:ID must start with letter */
151 //pj_create_unique_string(pool, &person_id);
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 /* Add <activities> */
166 nd_activities = pj_xml_node_new(pool, &RPID_ACTIVITIES);
167 pj_xml_add_node(nd_person, nd_activities);
168
169 /* Add the activity */
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 /* Add custom text if required. */
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 /* Done */
192 return PJ_SUCCESS;
193 }
194
195
196 /* Get <note> element from PIDF <tuple> element */
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 /* If we cannot find <note> inside <tuple>, try to get it from root. */
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 * Get RPID element from PIDF document, if any.
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 /* Reset */
231 pj_bzero(elem, sizeof(*elem));
232 elem->activity = PJRPID_ACTIVITY_UNKNOWN;
233
234 /* Find <person> */
235 nd_person = find_node(pres, "person");
236 if (!nd_person) {
237 /* <person> not found, try to get <note> from <tuple> */
238 return get_tuple_note(pres, pool, elem);
239 }
240
241 /* Get element id attribute */
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 /* Get <activities> */
247 nd_activities = find_node(nd_person, "activities");
248 if (nd_activities) {
249 const pj_xml_node *nd_activity;
250
251 /* Try to get <note> from <activities> */
252 nd_note = find_node(nd_activities, "note");
253
254 /* Get the activity */
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 /* If <note> is not found, get <note> from <person> */
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