1
/* $Id: iscomposing.c 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
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.
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.
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
20
#include <pjsip-simple/iscomposing.h>
21
#include <pjsip-simple/errno.h>
22
#include <pjsip/sip_msg.h>
23
#include <pjlib-util/errno.h>
25
#include <pj/string.h>
29
static const pj_str_t STR_MIME_TYPE = { "application", 11 };
30
static const pj_str_t STR_MIME_SUBTYPE = { "im-iscomposing+xml", 18 };
33
/* XML node constants. */
34
static const pj_str_t STR_ISCOMPOSING = { "isComposing", 11 };
35
static const pj_str_t STR_STATE = { "state", 5 };
36
static const pj_str_t STR_ACTIVE = { "active", 6 };
37
static const pj_str_t STR_IDLE = { "idle", 4 };
38
static const pj_str_t STR_LASTACTIVE = { "lastactive", 10 };
39
static const pj_str_t STR_CONTENTTYPE = { "contenttype", 11 };
40
static const pj_str_t STR_REFRESH = { "refresh", 7 };
43
/* XML attributes constants */
44
static const pj_str_t STR_XMLNS_NAME = { "xmlns", 5 };
45
static const pj_str_t STR_XMLNS_VAL = { "urn:ietf:params:xml:ns:im-iscomposing", 37 };
46
static const pj_str_t STR_XMLNS_XSI_NAME = { "xmlns:xsi", 9 };
47
static const pj_str_t STR_XMLNS_XSI_VAL = { "http://www.w3.org/2001/XMLSchema-instance", 41 };
48
static const pj_str_t STR_XSI_SLOC_NAME = { "xsi:schemaLocation", 18 };
49
static const pj_str_t STR_XSI_SLOC_VAL = { "urn:ietf:params:xml:ns:im-composing iscomposing.xsd", 51 };
52
PJ_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,
58
pj_xml_node *doc, *node;
62
doc = pj_xml_node_new(pool, &STR_ISCOMPOSING);
65
attr = pj_xml_attr_new(pool, &STR_XMLNS_NAME, &STR_XMLNS_VAL);
66
pj_xml_add_attr(doc, attr);
68
attr = pj_xml_attr_new(pool, &STR_XMLNS_XSI_NAME, &STR_XMLNS_XSI_VAL);
69
pj_xml_add_attr(doc, attr);
71
attr = pj_xml_attr_new(pool, &STR_XSI_SLOC_NAME, &STR_XSI_SLOC_VAL);
72
pj_xml_add_attr(doc, attr);
76
node = pj_xml_node_new(pool, &STR_STATE);
78
node->content = STR_ACTIVE;
80
node->content = STR_IDLE;
81
pj_xml_add_node(doc, node);
83
/* Add lastactive, if any. */
84
PJ_UNUSED_ARG(lst_actv);
85
//if (!is_composing && lst_actv) {
86
// PJ_TODO(IMPLEMENT_LAST_ACTIVE_ATTRIBUTE);
89
/* Add contenttype, if any. */
91
node = pj_xml_node_new(pool, &STR_CONTENTTYPE);
92
pj_strdup(pool, &node->content, content_tp);
93
pj_xml_add_node(doc, node);
96
/* Add refresh, if any. */
97
if (is_composing && refresh > 1 && refresh < 3601) {
98
node = pj_xml_node_new(pool, &STR_REFRESH);
99
node->content.ptr = (char*) pj_pool_alloc(pool, 10);
100
node->content.slen = pj_utoa(refresh, node->content.ptr);
101
pj_xml_add_node(doc, node);
112
* Function to print XML message body.
114
static int xml_print_body( struct pjsip_msg_body *msg_body,
115
char *buf, pj_size_t size)
117
return pj_xml_print((const pj_xml_node*)msg_body->data, buf, size,
123
* Function to clone XML document.
125
static void* xml_clone_data(pj_pool_t *pool, const void *data, unsigned len)
128
return pj_xml_clone( pool, (const pj_xml_node*)data);
133
PJ_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,
140
pjsip_msg_body *body;
142
doc = pjsip_iscomposing_create_xml( pool, is_composing, lst_actv,
143
content_tp, refresh);
148
body = PJ_POOL_ZALLOC_T(pool, pjsip_msg_body);
149
body->content_type.type = STR_MIME_TYPE;
150
body->content_type.subtype = STR_MIME_SUBTYPE;
155
body->print_body = &xml_print_body;
156
body->clone_data = &xml_clone_data;
162
PJ_DEF(pj_status_t) pjsip_iscomposing_parse( pj_pool_t *pool,
165
pj_bool_t *p_is_composing,
166
pj_str_t **p_last_active,
167
pj_str_t **p_content_type,
170
pj_xml_node *doc, *node;
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;
178
doc = pj_xml_parse( pool, msg, len);
180
return PJLIB_UTIL_EINXML;
182
/* Root document must be "isComposing" */
183
if (pj_stricmp(&doc->name, &STR_ISCOMPOSING) != 0)
184
return PJSIP_SIMPLE_EBADISCOMPOSE;
186
/* Get the status. */
187
if (p_is_composing) {
188
node = pj_xml_find_node(doc, &STR_STATE);
190
return PJSIP_SIMPLE_EBADISCOMPOSE;
191
*p_is_composing = (pj_stricmp(&node->content, &STR_ACTIVE)==0);
194
/* Get last active. */
196
node = pj_xml_find_node(doc, &STR_LASTACTIVE);
198
*p_last_active = &node->content;
201
/* Get content type */
202
if (p_content_type) {
203
node = pj_xml_find_node(doc, &STR_CONTENTTYPE);
205
*p_content_type = &node->content;
210
node = pj_xml_find_node(doc, &STR_REFRESH);
212
*p_refresh = pj_strtoul(&node->content);