blob: d49fae4e397eddaae97b26666baf5cc1a238066e [file] [log] [blame]
Benny Prijonob0808372006-03-02 21:18:58 +00001/* $Id$ */
2/*
Benny Prijonoa771a512007-02-19 01:13:53 +00003 * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org>
Benny Prijonob0808372006-03-02 21:18:58 +00004 *
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);
Benny Prijono9d4469d2007-05-02 05:14:29 +000098 node->content.ptr = (char*) pj_pool_alloc(pool, 10);
Benny Prijonob0808372006-03-02 21:18:58 +000099 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{
Benny Prijono9d4469d2007-05-02 05:14:29 +0000116 return pj_xml_print((const pj_xml_node*)msg_body->data, buf, size,
117 PJ_TRUE);
Benny Prijonob0808372006-03-02 21:18:58 +0000118}
119
120
121/*
122 * Function to clone XML document.
123 */
124static void* xml_clone_data(pj_pool_t *pool, const void *data, unsigned len)
125{
126 PJ_UNUSED_ARG(len);
Benny Prijono9d4469d2007-05-02 05:14:29 +0000127 return pj_xml_clone( pool, (const pj_xml_node*)data);
Benny Prijonob0808372006-03-02 21:18:58 +0000128}
129
130
131
132PJ_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
Benny Prijono9d4469d2007-05-02 05:14:29 +0000147 body = PJ_POOL_ZALLOC_T(pool, pjsip_msg_body);
Benny Prijonob0808372006-03-02 21:18:58 +0000148 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
161PJ_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 /* Set defaults: */
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 /* Parse XML */
177 doc = pj_xml_parse( pool, msg, len);
178 if (!doc)
179 return PJLIB_UTIL_EINXML;
180
181 /* Root document must be "isComposing" */
182 if (pj_stricmp(&doc->name, &STR_ISCOMPOSING) != 0)
183 return PJSIP_SIMPLE_EBADISCOMPOSE;
184
185 /* Get the status. */
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 /* Get last active. */
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 /* Get content type */
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 /* Get refresh */
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