1 #include <opensrf/transport_message.h>
3 // ---------------------------------------------------------------------------------
4 // Allocates and initializes a new transport_message
5 // ---------------------------------------------------------------------------------
6 transport_message* message_init( const char* body, const char* subject,
7 const char* thread, const char* recipient, const char* sender ) {
9 transport_message* msg = safe_malloc( sizeof(transport_message) );
11 if( body == NULL ) { body = ""; }
12 if( thread == NULL ) { thread = ""; }
13 if( subject == NULL ) { subject = ""; }
14 if( sender == NULL ) { sender = ""; }
15 if( recipient == NULL ) { recipient = ""; }
17 msg->body = strdup(body);
18 msg->thread = strdup(thread);
19 msg->subject = strdup(subject);
20 msg->recipient = strdup(recipient);
21 msg->sender = strdup(sender);
23 if( msg->body == NULL || msg->thread == NULL ||
24 msg->subject == NULL || msg->recipient == NULL ||
25 msg->sender == NULL ) {
27 osrfLogError(OSRF_LOG_MARK, "message_init(): Out of Memory" );
31 free( msg->recipient );
37 msg->router_from = NULL;
38 msg->router_to = NULL;
39 msg->router_class = NULL;
40 msg->router_command = NULL;
43 msg->error_type = NULL;
53 transport_message* new_message_from_xml( const char* msg_xml ) {
55 if( msg_xml == NULL || *msg_xml == '\0' )
58 transport_message* new_msg = safe_malloc( sizeof(transport_message) );
61 new_msg->subject = NULL;
62 new_msg->thread = NULL;
63 new_msg->recipient = NULL;
64 new_msg->sender = NULL;
65 new_msg->router_from = NULL;
66 new_msg->router_to = NULL;
67 new_msg->router_class = NULL;
68 new_msg->router_command = NULL;
69 new_msg->osrf_xid = NULL;
70 new_msg->is_error = 0;
71 new_msg->error_type = NULL;
72 new_msg->error_code = 0;
73 new_msg->broadcast = 0;
74 new_msg->msg_xml = NULL;
77 xmlKeepBlanksDefault(0);
78 xmlDocPtr msg_doc = xmlReadDoc( BAD_CAST msg_xml, NULL, NULL, 0 );
79 xmlNodePtr root = xmlDocGetRootElement(msg_doc);
81 xmlChar* sender = xmlGetProp(root, BAD_CAST "from");
82 xmlChar* recipient = xmlGetProp(root, BAD_CAST "to");
83 xmlChar* subject = xmlGetProp(root, BAD_CAST "subject");
84 xmlChar* thread = xmlGetProp( root, BAD_CAST "thread" );
85 xmlChar* router_from = xmlGetProp( root, BAD_CAST "router_from" );
86 xmlChar* router_to = xmlGetProp( root, BAD_CAST "router_to" );
87 xmlChar* router_class= xmlGetProp( root, BAD_CAST "router_class" );
88 xmlChar* broadcast = xmlGetProp( root, BAD_CAST "broadcast" );
89 xmlChar* osrf_xid = xmlGetProp( root, BAD_CAST "osrf_xid" );
92 message_set_osrf_xid( new_msg, (char*) osrf_xid);
97 new_msg->sender = strdup((const char*)router_from);
100 new_msg->sender = strdup((const char*)sender);
106 new_msg->recipient = strdup((const char*)recipient);
110 new_msg->subject = strdup((const char*)subject);
114 new_msg->thread = strdup((const char*)thread);
118 new_msg->router_from = strdup((const char*)router_from);
119 xmlFree(router_from);
122 new_msg->router_to = strdup((const char*)router_to);
126 new_msg->router_class = strdup((const char*)router_class);
127 xmlFree(router_class);
130 if(strcmp((const char*) broadcast,"0") )
131 new_msg->broadcast = 1;
135 xmlNodePtr search_node = root->children;
136 while( search_node != NULL ) {
138 if( ! strcmp( (const char*) search_node->name, "thread" ) ) {
139 if( search_node->children && search_node->children->content )
140 new_msg->thread = strdup( (const char*) search_node->children->content );
143 if( ! strcmp( (const char*) search_node->name, "subject" ) ) {
144 if( search_node->children && search_node->children->content )
145 new_msg->subject = strdup( (const char*) search_node->children->content );
148 if( ! strcmp( (const char*) search_node->name, "body" ) ) {
149 if( search_node->children && search_node->children->content )
150 new_msg->body = strdup((const char*) search_node->children->content );
153 search_node = search_node->next;
156 if( new_msg->thread == NULL )
157 new_msg->thread = strdup("");
158 if( new_msg->subject == NULL )
159 new_msg->subject = strdup("");
160 if( new_msg->body == NULL )
161 new_msg->body = strdup("");
163 new_msg->msg_xml = xmlDocToString(msg_doc, 0);
170 void message_set_osrf_xid( transport_message* msg, const char* osrf_xid ) {
172 if( msg->osrf_xid ) free( msg->osrf_xid );
173 msg->osrf_xid = strdup( osrf_xid ? osrf_xid : "" );
177 void message_set_router_info( transport_message* msg, const char* router_from,
178 const char* router_to, const char* router_class, const char* router_command,
179 int broadcast_enabled ) {
183 /* free old values, if any */
184 if( msg->router_from ) free( msg->router_from );
185 if( msg->router_to ) free( msg->router_to );
186 if( msg->router_class ) free( msg->router_class );
187 if( msg->router_command ) free( msg->router_command );
189 /* install new values */
190 msg->router_from = strdup( router_from ? router_from : "" );
191 msg->router_to = strdup( router_to ? router_to : "" );
192 msg->router_class = strdup( router_class ? router_class : "" );
193 msg->router_command = strdup( router_command ? router_command : "" );
194 msg->broadcast = broadcast_enabled;
196 if( msg->router_from == NULL || msg->router_to == NULL ||
197 msg->router_class == NULL || msg->router_command == NULL )
198 osrfLogError(OSRF_LOG_MARK, "message_set_router_info(): Out of Memory" );
203 // ---------------------------------------------------------------------------------
205 // ---------------------------------------------------------------------------------
206 int message_free( transport_message* msg ){
207 if( msg == NULL ) { return 0; }
212 free(msg->recipient);
214 free(msg->router_from);
215 free(msg->router_to);
216 free(msg->router_class);
217 free(msg->router_command);
219 if( msg->error_type != NULL ) free(msg->error_type);
220 if( msg->msg_xml != NULL ) free(msg->msg_xml);
226 // ---------------------------------------------------------------------------------
227 // Encodes the message as XML for traversal; stores in msg_xml member
228 // ---------------------------------------------------------------------------------
229 int message_prepare_xml( transport_message* msg ) {
232 if( msg->msg_xml ) return 1; /* already done */
234 xmlNodePtr message_node;
235 xmlNodePtr body_node;
236 xmlNodePtr thread_node;
237 xmlNodePtr subject_node;
238 xmlNodePtr error_node;
242 xmlKeepBlanksDefault(0);
244 doc = xmlReadDoc( BAD_CAST "<message/>", NULL, NULL, XML_PARSE_NSCLEAN );
245 message_node = xmlDocGetRootElement(doc);
247 if( msg->is_error ) {
248 error_node = xmlNewChild(message_node, NULL, BAD_CAST "error" , NULL );
249 xmlAddChild( message_node, error_node );
250 xmlNewProp( error_node, BAD_CAST "type", BAD_CAST msg->error_type );
252 osrf_clearbuf( code_buf, sizeof(code_buf));
253 sprintf(code_buf, "%d", msg->error_code );
254 xmlNewProp( error_node, BAD_CAST "code", BAD_CAST code_buf );
257 /* set from and to */
258 xmlNewProp( message_node, BAD_CAST "to", BAD_CAST msg->recipient );
259 xmlNewProp( message_node, BAD_CAST "from", BAD_CAST msg->sender );
260 xmlNewProp( message_node, BAD_CAST "router_from", BAD_CAST msg->router_from );
261 xmlNewProp( message_node, BAD_CAST "router_to", BAD_CAST msg->router_to );
262 xmlNewProp( message_node, BAD_CAST "router_class", BAD_CAST msg->router_class );
263 xmlNewProp( message_node, BAD_CAST "router_command", BAD_CAST msg->router_command );
264 xmlNewProp( message_node, BAD_CAST "osrf_xid", BAD_CAST msg->osrf_xid );
267 xmlNewProp( message_node, BAD_CAST "broadcast", BAD_CAST "1" );
269 /* Now add nodes where appropriate */
270 char* body = msg->body;
271 char* subject = msg->subject;
272 char* thread = msg->thread;
274 if( thread && *thread ) {
275 thread_node = xmlNewChild(message_node, NULL, (xmlChar*) "thread", NULL );
276 xmlNodePtr txt = xmlNewText((xmlChar*) thread);
277 xmlAddChild(thread_node, txt);
278 xmlAddChild(message_node, thread_node);
281 if( subject && *subject ) {
282 subject_node = xmlNewChild(message_node, NULL, (xmlChar*) "subject", NULL );
283 xmlNodePtr txt = xmlNewText((xmlChar*) subject);
284 xmlAddChild(subject_node, txt);
285 xmlAddChild( message_node, subject_node );
288 if( body && *body ) {
289 body_node = xmlNewChild(message_node, NULL, (xmlChar*) "body", NULL);
290 xmlNodePtr txt = xmlNewText((xmlChar*) body);
291 xmlAddChild(body_node, txt);
292 xmlAddChild( message_node, body_node );
295 xmlBufferPtr xmlbuf = xmlBufferCreate();
296 xmlNodeDump( xmlbuf, doc, xmlDocGetRootElement(doc), 0, 0);
297 msg->msg_xml = strdup((const char*) (xmlBufferContent(xmlbuf)));
299 xmlBufferFree(xmlbuf);
308 void jid_get_username( const char* jid, char buf[], int size ) {
310 if( jid == NULL || buf == NULL || size <= 0 ) { return; }
314 /* find the @ and return whatever is in front of it */
315 int len = strlen( jid );
317 for( i = 0; i != len; i++ ) {
318 if( jid[i] == '@' ) {
319 if(i > size) i = size;
320 memcpy( buf, jid, i );
328 void jid_get_resource( const char* jid, char buf[], int size) {
329 if( jid == NULL || buf == NULL || size <= 0 ) { return; }
331 // Find the last slash, if any
333 const char* start = strrchr( jid, '/' );
336 // Copy the text beyond the slash, up to a maximum size
338 size_t len = strlen( ++start );
339 if( len > size ) len = size;
340 memcpy( buf, start, len );
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++ ) {
359 else if(jid[i] == '/' && index1 != 0)
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 );
367 buf[dlen] = '\0'; // memcpy doesn't provide the nul
373 void set_msg_error( transport_message* msg, const char* type, int err_code ) {
377 if( type != NULL && *type ) {
378 msg->error_type = safe_malloc( strlen(type)+1);
379 strcpy( msg->error_type, type );
380 msg->error_code = err_code;