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 );
270 /* set from and to */
271 xmlNewProp( message_node, BAD_CAST "to", BAD_CAST msg->recipient );
272 xmlNewProp( message_node, BAD_CAST "from", BAD_CAST msg->sender );
273 xmlNewProp( message_node, BAD_CAST "router_from", BAD_CAST msg->router_from );
274 xmlNewProp( message_node, BAD_CAST "router_to", BAD_CAST msg->router_to );
275 xmlNewProp( message_node, BAD_CAST "router_class", BAD_CAST msg->router_class );
276 xmlNewProp( message_node, BAD_CAST "router_command", BAD_CAST msg->router_command );
279 xmlNewProp( message_node, BAD_CAST "broadcast", BAD_CAST "1" );
281 /* Now add nodes where appropriate */
282 char* body = msg->body;
283 char* subject = msg->subject;
284 char* thread = msg->thread;
286 if( thread && strlen(thread) > 0 ) {
287 thread_node = xmlNewChild(message_node, NULL, (xmlChar*) "thread", NULL );
288 xmlNodePtr txt = xmlNewText((xmlChar*) thread);
289 xmlAddChild(thread_node, txt);
290 xmlAddChild(message_node, thread_node);
293 if( subject && strlen(subject) > 0 ) {
294 subject_node = xmlNewChild(message_node, NULL, (xmlChar*) "subject", NULL );
295 xmlNodePtr txt = xmlNewText((xmlChar*) subject);
296 xmlAddChild(subject_node, txt);
297 xmlAddChild( message_node, subject_node );
300 if( body && strlen(body) > 0 ) {
301 body_node = xmlNewChild(message_node, NULL, (xmlChar*) "body", NULL);
302 xmlNodePtr txt = xmlNewText((xmlChar*) body);
303 xmlAddChild(body_node, txt);
304 xmlAddChild( message_node, body_node );
307 xmlBufferPtr xmlbuf = xmlBufferCreate();
308 xmlNodeDump( xmlbuf, doc, xmlDocGetRootElement(doc), 0, 0);
309 char* xml = strdup((char*) (xmlBufferContent(xmlbuf)));
310 xmlBufferFree(xmlbuf);
316 void jid_get_username( const char* jid, char buf[], int size ) {
318 if( jid == NULL ) { return; }
320 /* find the @ and return whatever is in front of it */
321 int len = strlen( jid );
323 for( i = 0; i != len; i++ ) {
324 if( jid[i] == 64 ) { /*ascii @*/
325 if(i > size) i = size;
326 strncpy( buf, jid, i );
333 void jid_get_resource( const char* jid, char buf[], int size) {
334 if( jid == NULL ) { return; }
335 int len = strlen( jid );
337 for( i = 0; i!= len; i++ ) {
338 if( jid[i] == 47 ) { /* ascii / */
339 const char* start = jid + i + 1; /* right after the '/' */
340 int rlen = len - (i+1);
341 if(rlen > size) rlen = size;
342 strncpy( buf, start, rlen );
347 void jid_get_domain( const char* jid, char buf[], int size ) {
349 if(jid == NULL) return;
351 int len = strlen(jid);
356 for( i = 0; i!= len; i++ ) {
357 if(jid[i] == 64) /* ascii @ */
359 else if(jid[i] == 47 && index1 != 0) /* ascii / */
363 if( index1 > 0 && index2 > 0 && index2 > index1 ) {
364 int dlen = index2 - index1;
365 if(dlen > size) dlen = size;
366 memcpy( buf, jid + index1, dlen );
370 void set_msg_error( transport_message* msg, char* type, int err_code ) {
372 if( type != NULL && strlen( type ) > 0 ) {
373 msg->error_type = safe_malloc( strlen(type)+1);
374 strcpy( msg->error_type, type );
375 msg->error_code = err_code;