1 /* $Id$ */
2 /*
3  * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4  * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
5  *
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 */
29 static const pj_str_t STR_MIME_TYPE = { "application", 11 };
30 static const pj_str_t STR_MIME_SUBTYPE = { "im-iscomposing+xml", 18 };
31 
32 
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 };
41 
42 
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 };
50 
51 
pjsip_iscomposing_create_xml(pj_pool_t * pool,pj_bool_t is_composing,const pj_time_val * lst_actv,const pj_str_t * content_tp,int refresh)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,
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. */
84     PJ_UNUSED_ARG(lst_actv);
85     //if (!is_composing && lst_actv) {
86     //	PJ_TODO(IMPLEMENT_LAST_ACTIVE_ATTRIBUTE);
87     //}
88 
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);
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);
102     }
103 
104     /* Done! */
105 
106     return doc;
107 }
108 
109 
110 
111 /*
112  * Function to print XML message body.
113  */
xml_print_body(struct pjsip_msg_body * msg_body,char * buf,pj_size_t size)114 static int xml_print_body( struct pjsip_msg_body *msg_body,
115 			   char *buf, pj_size_t size)
116 {
117     return pj_xml_print((const pj_xml_node*)msg_body->data, buf, size,
118     			PJ_TRUE);
119 }
120 
121 
122 /*
123  * Function to clone XML document.
124  */
xml_clone_data(pj_pool_t * pool,const void * data,unsigned len)125 static void* xml_clone_data(pj_pool_t *pool, const void *data, unsigned len)
126 {
127     PJ_UNUSED_ARG(len);
128     return pj_xml_clone( pool, (const pj_xml_node*)data);
129 }
130 
131 
132 
pjsip_iscomposing_create_body(pj_pool_t * pool,pj_bool_t is_composing,const pj_time_val * lst_actv,const pj_str_t * content_tp,int refresh)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,
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 
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;
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 
pjsip_iscomposing_parse(pj_pool_t * pool,char * msg,pj_size_t len,pj_bool_t * p_is_composing,pj_str_t ** p_last_active,pj_str_t ** p_content_type,int * p_refresh)162 PJ_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 
219