1 #include "opensrf/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 fatal_handler( "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*)sender);
63 new_msg->recipient = strdup((char*)recipient);
67 new_msg->subject = strdup((char*)subject);
71 new_msg->thread = strdup((char*)thread);
75 new_msg->router_from = strdup((char*)router_from);
79 new_msg->router_to = strdup((char*)router_to);
83 new_msg->router_class = strdup((char*)router_class);
84 xmlFree(router_class);
87 if(strcmp(broadcast,"0") )
88 new_msg->broadcast = 1;
92 xmlNodePtr search_node = root->children;
93 while( search_node != NULL ) {
95 if( ! strcmp( (char*) search_node->name, "thread" ) ) {
96 if( search_node->children && search_node->children->content )
97 new_msg->thread = strdup( (char*) search_node->children->content );
100 if( ! strcmp( (char*) search_node->name, "subject" ) ) {
101 if( search_node->children && search_node->children->content )
102 new_msg->subject = strdup( (char*) search_node->children->content );
105 if( ! strcmp( (char*) search_node->name, "body" ) ) {
106 if( search_node->children && search_node->children->content )
107 new_msg->body = strdup((char*) search_node->children->content );
110 search_node = search_node->next;
113 if( new_msg->thread == NULL )
114 new_msg->thread = strdup("");
115 if( new_msg->subject == NULL )
116 new_msg->subject = strdup("");
117 if( new_msg->body == NULL )
118 new_msg->body = strdup("");
124 xmlDocDumpFormatMemory( msg_doc, &xmlbuf, &bufsize, 0 );
125 encoded_body = strdup( (char*) xmlbuf );
127 if( encoded_body == NULL )
128 fatal_handler("message_to_xml(): Out of Memory");
134 /*** remove the XML declaration */
135 int len = strlen(encoded_body);
137 memset( tmp, 0, len );
141 /* when we reach the first >, take everything after it */
142 for( i = 0; i!= len; i++ ) {
143 if( encoded_body[i] == 62) { /* ascii > */
145 /* found_at holds the starting index of the rest of the doc*/
152 /* move the shortened doc into the tmp buffer */
153 strncpy( tmp, encoded_body + found_at, len - found_at );
154 /* move the tmp buffer back into the allocated space */
155 memset( encoded_body, 0, len );
156 strcpy( encoded_body, tmp );
159 new_msg->msg_xml = encoded_body;
165 void message_set_router_info( transport_message* msg, char* router_from,
166 char* router_to, char* router_class, char* router_command, int broadcast_enabled ) {
169 msg->router_from = strdup(router_from);
171 msg->router_from = strdup("");
174 msg->router_to = strdup(router_to);
176 msg->router_to = strdup("");
179 msg->router_class = strdup(router_class);
181 msg->router_class = strdup("");
184 msg->router_command = strdup(router_command);
186 msg->router_command = strdup("");
188 msg->broadcast = broadcast_enabled;
190 if( msg->router_from == NULL || msg->router_to == NULL ||
191 msg->router_class == NULL || msg->router_command == NULL )
192 fatal_handler( "message_set_router_info(): Out of Memory" );
199 /* encodes the message for traversal */
200 int message_prepare_xml( transport_message* msg ) {
201 if( msg->msg_xml != NULL ) { return 1; }
202 msg->msg_xml = message_to_xml( msg );
207 // ---------------------------------------------------------------------------------
209 // ---------------------------------------------------------------------------------
210 int message_free( transport_message* msg ){
211 if( msg == NULL ) { return 0; }
216 free(msg->recipient);
218 free(msg->router_from);
219 free(msg->router_to);
220 free(msg->router_class);
221 free(msg->router_command);
222 if( msg->error_type != NULL ) free(msg->error_type);
223 if( msg->msg_xml != NULL ) free(msg->msg_xml);
228 // ---------------------------------------------------------------------------------
229 // Allocates a char* holding the XML representation of this jabber message
230 // ---------------------------------------------------------------------------------
231 char* message_to_xml( const transport_message* msg ) {
237 xmlNodePtr message_node;
238 xmlNodePtr body_node;
239 xmlNodePtr thread_node;
240 xmlNodePtr subject_node;
241 xmlNodePtr error_node;
245 xmlKeepBlanksDefault(0);
248 warning_handler( "Passing NULL message to message_to_xml()");
252 doc = xmlReadDoc( BAD_CAST "<message/>", NULL, NULL, XML_PARSE_NSCLEAN );
253 message_node = xmlDocGetRootElement(doc);
255 if( msg->is_error ) {
256 error_node = xmlNewChild(message_node, NULL, BAD_CAST "error" , NULL );
257 xmlAddChild( message_node, error_node );
258 xmlNewProp( error_node, BAD_CAST "type", BAD_CAST msg->error_type );
260 memset( code_buf, 0, 16);
261 sprintf(code_buf, "%d", msg->error_code );
262 xmlNewProp( error_node, BAD_CAST "code", BAD_CAST code_buf );
266 /* set from and to */
267 xmlNewProp( message_node, BAD_CAST "to", BAD_CAST msg->recipient );
268 xmlNewProp( message_node, BAD_CAST "from", BAD_CAST msg->sender );
269 xmlNewProp( message_node, BAD_CAST "router_from", BAD_CAST msg->router_from );
270 xmlNewProp( message_node, BAD_CAST "router_to", BAD_CAST msg->router_to );
271 xmlNewProp( message_node, BAD_CAST "router_class", BAD_CAST msg->router_class );
272 xmlNewProp( message_node, BAD_CAST "router_command", BAD_CAST msg->router_command );
275 xmlNewProp( message_node, BAD_CAST "broadcast", BAD_CAST "1" );
277 /* Now add nodes where appropriate */
278 char* body = msg->body;
279 char* subject = msg->subject;
280 char* thread = msg->thread;
282 if( thread && strlen(thread) > 0 ) {
283 thread_node = xmlNewChild(message_node, NULL, (xmlChar*) "thread", (xmlChar*) thread );
284 xmlAddChild( message_node, thread_node );
287 if( subject && strlen(subject) > 0 ) {
288 subject_node = xmlNewChild(message_node, NULL, (xmlChar*) "subject", (xmlChar*) subject );
289 xmlAddChild( message_node, subject_node );
292 if( body && strlen(body) > 0 ) {
293 body_node = xmlNewChild(message_node, NULL, (xmlChar*) "body", (xmlChar*) body );
294 xmlAddChild( message_node, body_node );
298 xmlDocDumpFormatMemory( doc, &xmlbuf, &bufsize, 0 );
299 encoded_body = strdup( (char*) xmlbuf );
301 if( encoded_body == NULL )
302 fatal_handler("message_to_xml(): Out of Memory");
309 /*** remove the XML declaration */
310 int len = strlen(encoded_body);
312 memset( tmp, 0, len );
316 /* when we reach the first >, take everything after it */
317 for( i = 0; i!= len; i++ ) {
318 if( encoded_body[i] == 62) { /* ascii > */
320 /* found_at holds the starting index of the rest of the doc*/
327 /* move the shortened doc into the tmp buffer */
328 strncpy( tmp, encoded_body + found_at, len - found_at );
329 /* move the tmp buffer back into the allocated space */
330 memset( encoded_body, 0, len );
331 strcpy( encoded_body, tmp );
339 void jid_get_username( const char* jid, char buf[] ) {
341 if( jid == NULL ) { return; }
343 /* find the @ and return whatever is in front of it */
344 int len = strlen( jid );
346 for( i = 0; i != len; i++ ) {
347 if( jid[i] == 64 ) { /*ascii @*/
348 strncpy( buf, jid, i );
355 void jid_get_resource( const char* jid, char buf[]) {
356 if( jid == NULL ) { return; }
357 int len = strlen( jid );
359 for( i = 0; i!= len; i++ ) {
360 if( jid[i] == 47 ) { /* ascii / */
361 strncpy( buf, jid + i + 1, len - (i+1) );
366 void set_msg_error( transport_message* msg, char* type, int err_code ) {
368 if( type != NULL && strlen( type ) > 0 ) {
369 msg->error_type = safe_malloc( strlen(type)+1);
370 strcpy( msg->error_type, type );
371 msg->error_code = err_code;