blob: 18fd1f5c0c3c18afc171343a9439eedfede00bf9 [file] [log] [blame]
Benny Prijonob0808372006-03-02 21:18:58 +00001/* $Id$ */
2/*
Nanang Izzuddina62ffc92011-05-05 06:14:19 +00003 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
Benny Prijono32177c02008-06-20 22:44:47 +00004 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijonob0808372006-03-02 21:18:58 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20#include <pjsip-simple/iscomposing.h>
21#include <pjsip-simple/errno.h>
22#include <pjsip/sip_msg.h>
23#include <pjlib-util/errno.h>
24#include <pj/pool.h>
25#include <pj/string.h>
26
27
28/* MIME */
29static const pj_str_t STR_MIME_TYPE = { "application", 11 };
30static const pj_str_t STR_MIME_SUBTYPE = { "im-iscomposing+xml", 18 };
31
32
33/* XML node constants. */
34static const pj_str_t STR_ISCOMPOSING = { "isComposing", 11 };
35static const pj_str_t STR_STATE = { "state", 5 };
36static const pj_str_t STR_ACTIVE = { "active", 6 };
37static const pj_str_t STR_IDLE = { "idle", 4 };
38static const pj_str_t STR_LASTACTIVE = { "lastactive", 10 };
39static const pj_str_t STR_CONTENTTYPE = { "contenttype", 11 };
40static const pj_str_t STR_REFRESH = { "refresh", 7 };
41
42
43/* XML attributes constants */
44static const pj_str_t STR_XMLNS_NAME = { "xmlns", 5 };
45static const pj_str_t STR_XMLNS_VAL = { "urn:ietf:params:xml:ns:im-iscomposing", 37 };
46static const pj_str_t STR_XMLNS_XSI_NAME = { "xmlns:xsi", 9 };
47static const pj_str_t STR_XMLNS_XSI_VAL = { "http://www.w3.org/2001/XMLSchema-instance", 41 };
48static const pj_str_t STR_XSI_SLOC_NAME = { "xsi:schemaLocation", 18 };
49static const pj_str_t STR_XSI_SLOC_VAL = { "urn:ietf:params:xml:ns:im-composing iscomposing.xsd", 51 };
50
51
52PJ_DEF(pj_xml_node*) pjsip_iscomposing_create_xml( pj_pool_t *pool,
53 pj_bool_t is_composing,
54 const pj_time_val *lst_actv,
55 const pj_str_t *content_tp,
56 int refresh)
57{
58 pj_xml_node *doc, *node;
59 pj_xml_attr *attr;
60
61 /* Root document. */
62 doc = pj_xml_node_new(pool, &STR_ISCOMPOSING);
63
64 /* Add attributes */
65 attr = pj_xml_attr_new(pool, &STR_XMLNS_NAME, &STR_XMLNS_VAL);
66 pj_xml_add_attr(doc, attr);
67
68 attr = pj_xml_attr_new(pool, &STR_XMLNS_XSI_NAME, &STR_XMLNS_XSI_VAL);
69 pj_xml_add_attr(doc, attr);
70
71 attr = pj_xml_attr_new(pool, &STR_XSI_SLOC_NAME, &STR_XSI_SLOC_VAL);
72 pj_xml_add_attr(doc, attr);
73
74
75 /* Add state. */
76 node = pj_xml_node_new(pool, &STR_STATE);
77 if (is_composing)
78 node->content = STR_ACTIVE;
79 else
80 node->content = STR_IDLE;
81 pj_xml_add_node(doc, node);
82
83 /* Add lastactive, if any. */
Benny Prijonod4e0abd2006-03-05 11:53:36 +000084 PJ_UNUSED_ARG(lst_actv);
85 //if (!is_composing && lst_actv) {
86 // PJ_TODO(IMPLEMENT_LAST_ACTIVE_ATTRIBUTE);
87 //}
Benny Prijonob0808372006-03-02 21:18:58 +000088
89 /* Add contenttype, if any. */
90 if (content_tp) {
91 node = pj_xml_node_new(pool, &STR_CONTENTTYPE);
92 pj_strdup(pool, &node->content, content_tp);
93 pj_xml_add_node(doc, node);
94 }
95
96 /* Add refresh, if any. */
97 if (is_composing && refresh > 1 && refresh < 3601) {
98 node = pj_xml_node_new(pool, &STR_REFRESH);
Benny Prijono9d4469d2007-05-02 05:14:29 +000099 node->content.ptr = (char*) pj_pool_alloc(pool, 10);
Benny Prijonob0808372006-03-02 21:18:58 +0000100 node->content.slen = pj_utoa(refresh, node->content.ptr);
101 pj_xml_add_node(doc, node);
102 }
103
104 /* Done! */
105
106 return doc;
107}
108
109
110
111/*
112 * Function to print XML message body.
113 */
114static int xml_print_body( struct pjsip_msg_body *msg_body,
115 char *buf, pj_size_t size)
116{
Benny Prijono9d4469d2007-05-02 05:14:29 +0000117 return pj_xml_print((const pj_xml_node*)msg_body->data, buf, size,
118 PJ_TRUE);
Benny Prijonob0808372006-03-02 21:18:58 +0000119}
120
121
122/*
123 * Function to clone XML document.
124 */
125static void* xml_clone_data(pj_pool_t *pool, const void *data, unsigned len)
126{
127 PJ_UNUSED_ARG(len);
Benny Prijono9d4469d2007-05-02 05:14:29 +0000128 return pj_xml_clone( pool, (const pj_xml_node*)data);
Benny Prijonob0808372006-03-02 21:18:58 +0000129}
130
131
132
133PJ_DEF(pjsip_msg_body*) pjsip_iscomposing_create_body( pj_pool_t *pool,
134 pj_bool_t is_composing,
135 const pj_time_val *lst_actv,
136 const pj_str_t *content_tp,
137 int refresh)
138{
139 pj_xml_node *doc;
140 pjsip_msg_body *body;
141
142 doc = pjsip_iscomposing_create_xml( pool, is_composing, lst_actv,
143 content_tp, refresh);
144 if (doc == NULL)
145 return NULL;
146
147
Benny Prijono9d4469d2007-05-02 05:14:29 +0000148 body = PJ_POOL_ZALLOC_T(pool, pjsip_msg_body);
Benny Prijonob0808372006-03-02 21:18:58 +0000149 body->content_type.type = STR_MIME_TYPE;
150 body->content_type.subtype = STR_MIME_SUBTYPE;
151
152 body->data = doc;
153 body->len = 0;
154
155 body->print_body = &xml_print_body;
156 body->clone_data = &xml_clone_data;
157
158 return body;
159}
160
161
162PJ_DEF(pj_status_t) pjsip_iscomposing_parse( pj_pool_t *pool,
163 char *msg,
164 pj_size_t len,
165 pj_bool_t *p_is_composing,
166 pj_str_t **p_last_active,
167 pj_str_t **p_content_type,
168 int *p_refresh )
169{
170 pj_xml_node *doc, *node;
171
172 /* Set defaults: */
173 if (p_is_composing) *p_is_composing = PJ_FALSE;
174 if (p_last_active) *p_last_active = NULL;
175 if (p_content_type) *p_content_type = NULL;
176
177 /* Parse XML */
178 doc = pj_xml_parse( pool, msg, len);
179 if (!doc)
180 return PJLIB_UTIL_EINXML;
181
182 /* Root document must be "isComposing" */
183 if (pj_stricmp(&doc->name, &STR_ISCOMPOSING) != 0)
184 return PJSIP_SIMPLE_EBADISCOMPOSE;
185
186 /* Get the status. */
187 if (p_is_composing) {
188 node = pj_xml_find_node(doc, &STR_STATE);
189 if (node == NULL)
190 return PJSIP_SIMPLE_EBADISCOMPOSE;
191 *p_is_composing = (pj_stricmp(&node->content, &STR_ACTIVE)==0);
192 }
193
194 /* Get last active. */
195 if (p_last_active) {
196 node = pj_xml_find_node(doc, &STR_LASTACTIVE);
197 if (node)
198 *p_last_active = &node->content;
199 }
200
201 /* Get content type */
202 if (p_content_type) {
203 node = pj_xml_find_node(doc, &STR_CONTENTTYPE);
204 if (node)
205 *p_content_type = &node->content;
206 }
207
208 /* Get refresh */
209 if (p_refresh) {
210 node = pj_xml_find_node(doc, &STR_REFRESH);
211 if (node)
212 *p_refresh = pj_strtoul(&node->content);
213 }
214
215 return PJ_SUCCESS;
216}
217
218