1 #include "transport_message.h"
4 // ---------------------------------------------------------------------------------
5 // Allocates and initializes a new transport_message
6 // ---------------------------------------------------------------------------------
7 transport_message* message_init( char* body,
8 char* subject, char* thread, char* recipient, char* sender ) {
10 transport_message* msg =
11 (transport_message*) safe_malloc( sizeof(transport_message) );
13 if( body == NULL ) { body = ""; }
14 if( thread == NULL ) { thread = ""; }
15 if( subject == NULL ) { subject = ""; }
16 if( sender == NULL ) { sender = ""; }
17 if( recipient == NULL ) { recipient = ""; }
19 msg->body = strdup(body);
20 msg->thread = strdup(thread);
21 msg->subject = strdup(subject);
22 msg->recipient = strdup(recipient);
23 msg->sender = strdup(sender);
25 if( msg->body == NULL || msg->thread == NULL ||
26 msg->subject == NULL || msg->recipient == NULL ||
27 msg->sender == NULL ) {
29 osrfLogError(OSRF_LOG_MARK, "message_init(): Out of Memory" );
37 transport_message* new_message_from_xml( const char* msg_xml ) {
39 if( msg_xml == NULL || strlen(msg_xml) < 1 )
42 transport_message* new_msg =
43 (transport_message*) safe_malloc( sizeof(transport_message) );
45 xmlKeepBlanksDefault(0);
46 xmlDocPtr msg_doc = xmlReadDoc( BAD_CAST msg_xml, NULL, NULL, 0 );
47 xmlNodePtr root = xmlDocGetRootElement(msg_doc);
49 xmlChar* sender = xmlGetProp(root, BAD_CAST "from");
50 xmlChar* recipient = xmlGetProp(root, BAD_CAST "to");
51 xmlChar* subject = xmlGetProp(root, BAD_CAST "subject");
52 xmlChar* thread = xmlGetProp( root, BAD_CAST "thread" );
53 xmlChar* router_from = xmlGetProp( root, BAD_CAST "router_from" );
54 xmlChar* router_to = xmlGetProp( root, BAD_CAST "router_to" );
55 xmlChar* router_class= xmlGetProp( root, BAD_CAST "router_class" );
56 xmlChar* broadcast = xmlGetProp( root, BAD_CAST "broadcast" );
59 new_msg->sender = strdup((char*)router_from);
62 new_msg->sender = strdup((char*)sender);
68 new_msg->recipient = strdup((char*)recipient);
72 new_msg->subject = strdup((char*)subject);
76 new_msg->thread = strdup((char*)thread);
80 new_msg->router_from = strdup((char*)router_from);
84 new_msg->router_to = strdup((char*)router_to);
88 new_msg->router_class = strdup((char*)router_class);
89 xmlFree(router_class);
92 if(strcmp(broadcast,"0") )
93 new_msg->broadcast = 1;
97 xmlNodePtr search_node = root->children;
98 while( search_node != NULL ) {
100 if( ! strcmp( (char*) search_node->name, "thread" ) ) {
101 if( search_node->children && search_node->children->content )
102 new_msg->thread = strdup( (char*) search_node->children->content );
105 if( ! strcmp( (char*) search_node->name, "subject" ) ) {
106 if( search_node->children && search_node->children->content )
107 new_msg->subject = strdup( (char*) search_node->children->content );
110 if( ! strcmp( (char*) search_node->name, "body" ) ) {
111 if( search_node->children && search_node->children->content )
112 new_msg->body = strdup((char*) search_node->children->content );
115 search_node = search_node->next;
118 if( new_msg->thread == NULL )
119 new_msg->thread = strdup("");
120 if( new_msg->subject == NULL )
121 new_msg->subject = strdup("");
122 if( new_msg->body == NULL )
123 new_msg->body = strdup("");
129 xmlDocDumpFormatMemory( msg_doc, &xmlbuf, &bufsize, 0 );
130 encoded_body = strdup( (char*) xmlbuf );
132 if( encoded_body == NULL )
133 osrfLogError(OSRF_LOG_MARK, "message_to_xml(): Out of Memory");
139 /*** remove the XML declaration */
140 int len = strlen(encoded_body);
142 memset( tmp, 0, len );
146 /* when we reach the first >, take everything after it */
147 for( i = 0; i!= len; i++ ) {
148 if( encoded_body[i] == 62) { /* ascii > */
150 /* found_at holds the starting index of the rest of the doc*/
157 /* move the shortened doc into the tmp buffer */
158 strncpy( tmp, encoded_body + found_at, len - found_at );
159 /* move the tmp buffer back into the allocated space */
160 memset( encoded_body, 0, len );
161 strcpy( encoded_body, tmp );
164 new_msg->msg_xml = encoded_body;
170 void message_set_router_info( transport_message* msg, char* router_from,
171 char* router_to, char* router_class, char* router_command, int broadcast_enabled ) {
174 msg->router_from = strdup(router_from);
176 msg->router_from = strdup("");
179 msg->router_to = strdup(router_to);
181 msg->router_to = strdup("");
184 msg->router_class = strdup(router_class);
186 msg->router_class = strdup("");
189 msg->router_command = strdup(router_command);
191 msg->router_command = strdup("");
193 msg->broadcast = broadcast_enabled;
195 if( msg->router_from == NULL || msg->router_to == NULL ||
196 msg->router_class == NULL || msg->router_command == NULL )
197 osrfLogError(OSRF_LOG_MARK, "message_set_router_info(): Out of Memory" );
204 /* encodes the message for traversal */
205 int message_prepare_xml( transport_message* msg ) {
206 if( msg->msg_xml != NULL ) { return 1; }
207 msg->msg_xml = message_to_xml( msg );
212 // ---------------------------------------------------------------------------------
214 // ---------------------------------------------------------------------------------
215 int message_free( transport_message* msg ){
216 if( msg == NULL ) { return 0; }
221 free(msg->recipient);
223 free(msg->router_from);
224 free(msg->router_to);
225 free(msg->router_class);
226 free(msg->router_command);
227 if( msg->error_type != NULL ) free(msg->error_type);
228 if( msg->msg_xml != NULL ) free(msg->msg_xml);
233 // ---------------------------------------------------------------------------------
234 // Allocates a char* holding the XML representation of this jabber message
235 // ---------------------------------------------------------------------------------
236 char* message_to_xml( const transport_message* msg ) {
242 xmlNodePtr message_node;
243 xmlNodePtr body_node;
244 xmlNodePtr thread_node;
245 xmlNodePtr subject_node;
246 xmlNodePtr error_node;
250 xmlKeepBlanksDefault(0);
253 osrfLogWarning(OSRF_LOG_MARK, "Passing NULL message to message_to_xml()");
257 doc = xmlReadDoc( BAD_CAST "<message/>", NULL, NULL, XML_PARSE_NSCLEAN );
258 message_node = xmlDocGetRootElement(doc);
260 if( msg->is_error ) {
261 error_node = xmlNewChild(message_node, NULL, BAD_CAST "error" , NULL );
262 xmlAddChild( message_node, error_node );
263 xmlNewProp( error_node, BAD_CAST "type", BAD_CAST msg->error_type );
265 memset( code_buf, 0, 16);
266 sprintf(code_buf, "%d", msg->error_code );
267 xmlNewProp( error_node, BAD_CAST "code", BAD_CAST code_buf );
271 /* set from and to */
272 xmlNewProp( message_node, BAD_CAST "to", BAD_CAST msg->recipient );
273 xmlNewProp( message_node, BAD_CAST "from", BAD_CAST msg->sender );
274 xmlNewProp( message_node, BAD_CAST "router_from", BAD_CAST msg->router_from );
275 xmlNewProp( message_node, BAD_CAST "router_to", BAD_CAST msg->router_to );
276 xmlNewProp( message_node, BAD_CAST "router_class", BAD_CAST msg->router_class );
277 xmlNewProp( message_node, BAD_CAST "router_command", BAD_CAST msg->router_command );
280 xmlNewProp( message_node, BAD_CAST "broadcast", BAD_CAST "1" );
282 /* Now add nodes where appropriate */
283 char* body = msg->body;
284 char* subject = msg->subject;
285 char* thread = msg->thread;
287 if( thread && strlen(thread) > 0 ) {
288 thread_node = xmlNewChild(message_node, NULL, (xmlChar*) "thread", NULL );
289 // xmlNewTextChild(thread_node, NULL, NULL, );
290 xmlNodePtr txt = xmlNewText((xmlChar*) thread);
291 xmlAddChild(thread_node, txt);
292 xmlAddChild(message_node, thread_node);
295 if( subject && strlen(subject) > 0 ) {
296 subject_node = xmlNewChild(message_node, NULL, (xmlChar*) "subject", NULL );
297 //xmlNewTextChild(subject_node, NULL, NULL, (xmlChar*) subject );
298 xmlNodePtr txt = xmlNewText((xmlChar*) subject);
299 xmlAddChild(subject_node, txt);
300 xmlAddChild( message_node, subject_node );
303 if( body && strlen(body) > 0 ) {
304 body_node = xmlNewChild(message_node, NULL, (xmlChar*) "body", NULL);
305 //xmlNewTextChild(body_node, NULL, NULL, (xmlChar*) body );
306 xmlNodePtr txt = xmlNewText((xmlChar*) body);
307 xmlAddChild(body_node, txt);
308 xmlAddChild( message_node, body_node );
311 xmlDocDumpMemoryEnc( doc, &xmlbuf, &bufsize, "UTF-8" );
313 encoded_body = strdup( (char*) xmlbuf );
315 if( encoded_body == NULL ) {
316 osrfLogError(OSRF_LOG_MARK, "message_to_xml(): Out of Memory");
325 /*** remove the XML declaration */
326 int len = strlen(encoded_body);
328 memset( tmp, 0, len );
332 /* when we reach the first >, take everything after it */
333 for( i = 0; i!= len; i++ ) {
334 if( encoded_body[i] == 62) { /* ascii > */
336 /* found_at holds the starting index of the rest of the doc*/
343 /* move the shortened doc into the tmp buffer */
344 strncpy( tmp, encoded_body + found_at, len - found_at );
345 /* move the tmp buffer back into the allocated space */
346 memset( encoded_body, 0, len );
347 strcpy( encoded_body, tmp );
355 void jid_get_username( const char* jid, char buf[], int size ) {
357 if( jid == NULL ) { return; }
359 /* find the @ and return whatever is in front of it */
360 int len = strlen( jid );
362 for( i = 0; i != len; i++ ) {
363 if( jid[i] == 64 ) { /*ascii @*/
364 if(i > size) i = size;
365 strncpy( buf, jid, i );
372 void jid_get_resource( const char* jid, char buf[], int size) {
373 if( jid == NULL ) { return; }
374 int len = strlen( jid );
376 for( i = 0; i!= len; i++ ) {
377 if( jid[i] == 47 ) { /* ascii / */
378 const char* start = jid + i + 1; /* right after the '/' */
379 int rlen = len - (i+1);
380 if(rlen > size) rlen = size;
381 strncpy( buf, start, rlen );
386 void jid_get_domain( const char* jid, char buf[], int size ) {
388 if(jid == NULL) return;
390 int len = strlen(jid);
395 for( i = 0; i!= len; i++ ) {
396 if(jid[i] == 64) /* ascii @ */
398 else if(jid[i] == 47 && index1 != 0) /* ascii / */
402 if( index1 > 0 && index2 > 0 && index2 > index1 ) {
403 int dlen = index2 - index1;
404 if(dlen > size) dlen = size;
405 memcpy( buf, jid + index1, dlen );
409 void set_msg_error( transport_message* msg, char* type, int err_code ) {
411 if( type != NULL && strlen( type ) > 0 ) {
412 msg->error_type = safe_malloc( strlen(type)+1);
413 strcpy( msg->error_type, type );
414 msg->error_code = err_code;