blob: 38fa578e80d4e14acbb84597c64abe58e0da63f4 [file] [log] [blame]
Benny Prijonob0808372006-03-02 21:18:58 +00001/* $Id$ */
2/*
3 * Copyright (C) 2003-2006 Benny Prijono <benny@prijono.org>
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/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/* MIME */
28static const pj_str_t STR_MIME_TYPE = { "application", 11 };
29static const pj_str_t STR_MIME_SUBTYPE = { "im-iscomposing+xml", 18 };
30
31
32/* XML node constants. */
33static const pj_str_t STR_ISCOMPOSING = { "isComposing", 11 };
34static const pj_str_t STR_STATE = { "state", 5 };
35static const pj_str_t STR_ACTIVE = { "active", 6 };
36static const pj_str_t STR_IDLE = { "idle", 4 };
37static const pj_str_t STR_LASTACTIVE = { "lastactive", 10 };
38static const pj_str_t STR_CONTENTTYPE = { "contenttype", 11 };
39static const pj_str_t STR_REFRESH = { "refresh", 7 };
40
41
42/* XML attributes constants */
43static const pj_str_t STR_XMLNS_NAME = { "xmlns", 5 };
44static const pj_str_t STR_XMLNS_VAL = { "urn:ietf:params:xml:ns:im-iscomposing", 37 };
45static const pj_str_t STR_XMLNS_XSI_NAME = { "xmlns:xsi", 9 };
46static const pj_str_t STR_XMLNS_XSI_VAL = { "http://www.w3.org/2001/XMLSchema-instance", 41 };
47static const pj_str_t STR_XSI_SLOC_NAME = { "xsi:schemaLocation", 18 };
48static const pj_str_t STR_XSI_SLOC_VAL = { "urn:ietf:params:xml:ns:im-composing iscomposing.xsd", 51 };
49
50
51PJ_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 /* Root document. */
61 doc = pj_xml_node_new(pool, &STR_ISCOMPOSING);
62
63 /* Add attributes */
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 /* Add state. */
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 /* Add lastactive, if any. */
Benny Prijonod4e0abd2006-03-05 11:53:36 +000083 PJ_UNUSED_ARG(lst_actv);
84 //if (!is_composing && lst_actv) {
85 // PJ_TODO(IMPLEMENT_LAST_ACTIVE_ATTRIBUTE);
86 //}
Benny Prijonob0808372006-03-02 21:18:58 +000087
88 /* Add contenttype, if any. */
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 /* Add refresh, if any. */
96 if (is_composing && refresh > 1 && refresh < 3601) {
97 node = pj_xml_node_new(pool, &STR_REFRESH);
98 node->content.ptr = 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 /* Done! */
104
105 return doc;
106}
107
108
109
110/*
111 * Function to print XML message body.
112 */
113static int xml_print_body( struct pjsip_msg_body *msg_body,
114 char *buf, pj_size_t size)
115{
116 return pj_xml_print(msg_body->data, buf, size, PJ_TRUE);
117}
118
119
120/*
121 * Function to clone XML document.
122 */
123static void* xml_clone_data(pj_pool_t *pool, const void *data, unsigned len)
124{
125 PJ_UNUSED_ARG(len);
126 return pj_xml_clone( pool, data);
127}
128
129
130
131PJ_DEF(pjsip_msg_body*) pjsip_iscomposing_create_body( pj_pool_t *pool,
132 pj_bool_t is_composing,
133 const pj_time_val *lst_actv,
134 const pj_str_t *content_tp,
135 int refresh)
136{
137 pj_xml_node *doc;
138 pjsip_msg_body *body;
139
140 doc = pjsip_iscomposing_create_xml( pool, is_composing, lst_actv,
141 content_tp, refresh);
142 if (doc == NULL)
143 return NULL;
144
145
146 body = pj_pool_zalloc(pool, sizeof(pjsip_msg_body));
147 body->content_type.type = STR_MIME_TYPE;
148 body->content_type.subtype = STR_MIME_SUBTYPE;
149
150 body->data = doc;
151 body->len = 0;
152
153 body->print_body = &xml_print_body;
154 body->clone_data = &xml_clone_data;
155
156 return body;
157}
158
159
160PJ_DEF(pj_status_t) pjsip_iscomposing_parse( pj_pool_t *pool,
161 char *msg,
162 pj_size_t len,
163 pj_bool_t *p_is_composing,
164 pj_str_t **p_last_active,
165 pj_str_t **p_content_type,
166 int *p_refresh )
167{
168 pj_xml_node *doc, *node;
169
170 /* Set defaults: */
171 if (p_is_composing) *p_is_composing = PJ_FALSE;
172 if (p_last_active) *p_last_active = NULL;
173 if (p_content_type) *p_content_type = NULL;
174
175 /* Parse XML */
176 doc = pj_xml_parse( pool, msg, len);
177 if (!doc)
178 return PJLIB_UTIL_EINXML;
179
180 /* Root document must be "isComposing" */
181 if (pj_stricmp(&doc->name, &STR_ISCOMPOSING) != 0)
182 return PJSIP_SIMPLE_EBADISCOMPOSE;
183
184 /* Get the status. */
185 if (p_is_composing) {
186 node = pj_xml_find_node(doc, &STR_STATE);
187 if (node == NULL)
188 return PJSIP_SIMPLE_EBADISCOMPOSE;
189 *p_is_composing = (pj_stricmp(&node->content, &STR_ACTIVE)==0);
190 }
191
192 /* Get last active. */
193 if (p_last_active) {
194 node = pj_xml_find_node(doc, &STR_LASTACTIVE);
195 if (node)
196 *p_last_active = &node->content;
197 }
198
199 /* Get content type */
200 if (p_content_type) {
201 node = pj_xml_find_node(doc, &STR_CONTENTTYPE);
202 if (node)
203 *p_content_type = &node->content;
204 }
205
206 /* Get refresh */
207 if (p_refresh) {
208 node = pj_xml_find_node(doc, &STR_REFRESH);
209 if (node)
210 *p_refresh = pj_strtoul(&node->content);
211 }
212
213 return PJ_SUCCESS;
214}
215
216