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 fatal_handler( "message_init(): Out of Memory" );
36 void message_set_router_info( transport_message* msg, char* router_from,
37 char* router_to, char* router_class, char* router_command, int broadcast_enabled ) {
39 msg->router_from = strdup(router_from);
40 msg->router_to = strdup(router_to);
41 msg->router_class = strdup(router_class);
42 msg->router_command = strdup(router_command);
43 msg->broadcast = broadcast_enabled;
45 if( msg->router_from == NULL || msg->router_to == NULL ||
46 msg->router_class == NULL || msg->router_command == NULL )
47 fatal_handler( "message_set_router_info(): Out of Memory" );
54 /* encodes the message for traversal */
55 int message_prepare_xml( transport_message* msg ) {
56 if( msg->msg_xml != NULL ) { return 1; }
57 msg->msg_xml = message_to_xml( msg );
62 // ---------------------------------------------------------------------------------
64 // ---------------------------------------------------------------------------------
65 int message_free( transport_message* msg ){
66 if( msg == NULL ) { return 0; }
73 free(msg->router_from);
75 free(msg->router_class);
76 free(msg->router_command);
77 if( msg->error_type != NULL ) free(msg->error_type);
78 if( msg->msg_xml != NULL ) free(msg->msg_xml);
83 // ---------------------------------------------------------------------------------
84 // Allocates a char* holding the XML representation of this jabber message
85 // ---------------------------------------------------------------------------------
86 char* message_to_xml( const transport_message* msg ) {
92 xmlNodePtr message_node;
94 xmlNodePtr thread_node;
95 xmlNodePtr subject_node;
96 xmlNodePtr error_node;
100 xmlKeepBlanksDefault(0);
103 warning_handler( "Passing NULL message to message_to_xml()");
107 doc = xmlReadDoc( BAD_CAST "<message/>", NULL, NULL, XML_PARSE_NSCLEAN );
108 message_node = xmlDocGetRootElement(doc);
110 if( msg->is_error ) {
111 error_node = xmlNewChild(message_node, NULL, BAD_CAST "error" , NULL );
112 xmlAddChild( message_node, error_node );
113 xmlNewProp( error_node, BAD_CAST "type", BAD_CAST msg->error_type );
115 memset( code_buf, 0, 16);
116 sprintf(code_buf, "%d", msg->error_code );
117 xmlNewProp( error_node, BAD_CAST "code", BAD_CAST code_buf );
121 /* set from and to */
122 xmlNewProp( message_node, BAD_CAST "to", BAD_CAST msg->recipient );
123 xmlNewProp( message_node, BAD_CAST "from", BAD_CAST msg->sender );
124 xmlNewProp( message_node, BAD_CAST "router_from", BAD_CAST msg->router_from );
125 xmlNewProp( message_node, BAD_CAST "router_to", BAD_CAST msg->router_to );
126 xmlNewProp( message_node, BAD_CAST "router_class", BAD_CAST msg->router_class );
127 xmlNewProp( message_node, BAD_CAST "router_command", BAD_CAST msg->router_command );
130 xmlNewProp( message_node, BAD_CAST "broadcast", BAD_CAST "1" );
132 /* Now add nodes where appropriate */
133 char* body = msg->body;
134 char* subject = msg->subject;
135 char* thread = msg->thread;
137 if( thread && strlen(thread) > 0 ) {
138 thread_node = xmlNewChild(message_node, NULL, (xmlChar*) "thread", (xmlChar*) thread );
139 xmlAddChild( message_node, thread_node );
142 if( subject && strlen(subject) > 0 ) {
143 subject_node = xmlNewChild(message_node, NULL, (xmlChar*) "subject", (xmlChar*) subject );
144 xmlAddChild( message_node, subject_node );
147 if( body && strlen(body) > 0 ) {
148 body_node = xmlNewChild(message_node, NULL, (xmlChar*) "body", (xmlChar*) body );
149 xmlAddChild( message_node, body_node );
153 xmlDocDumpFormatMemory( doc, &xmlbuf, &bufsize, 0 );
154 encoded_body = strdup( (char*) xmlbuf );
156 if( encoded_body == NULL )
157 fatal_handler("message_to_xml(): Out of Memory");
164 /*** remove the XML declaration */
166 int len = strlen(encoded_body);
168 memset( tmp, 0, len );
172 /* when we reach the first >, take everything after it */
173 for( i = 0; i!= len; i++ ) {
174 if( encoded_body[i] == 62) { /* ascii > */
176 /* found_at holds the starting index of the rest of the doc*/
183 /* move the shortened doc into the tmp buffer */
184 strncpy( tmp, encoded_body + found_at, len - found_at );
185 /* move the tmp buffer back into the allocated space */
186 memset( encoded_body, 0, len );
187 strcpy( encoded_body, tmp );
195 void jid_get_username( const char* jid, char buf[] ) {
197 if( jid == NULL ) { return; }
199 /* find the @ and return whatever is in front of it */
200 int len = strlen( jid );
202 for( i = 0; i != len; i++ ) {
203 if( jid[i] == 64 ) { /*ascii @*/
204 strncpy( buf, jid, i );
211 void jid_get_resource( const char* jid, char buf[]) {
212 if( jid == NULL ) { return; }
213 int len = strlen( jid );
215 for( i = 0; i!= len; i++ ) {
216 if( jid[i] == 47 ) { /* ascii / */
217 strncpy( buf, jid + i + 1, len - (i+1) );
222 void set_msg_error( transport_message* msg, char* type, int err_code ) {
224 if( type != NULL && strlen( type ) > 0 ) {
225 msg->error_type = safe_malloc( strlen(type)+1);
226 strcpy( msg->error_type, type );
227 msg->error_code = err_code;