changeset 21441:da75dd6c41fb

Leak fix, cleanup and code reuse.
author Daniel Atallah <daniel.atallah@gmail.com>
date Mon, 12 Nov 2007 05:09:17 +0000
parents d8106b63b0a4
children 31fc6a0769ce
files libpurple/protocols/bonjour/bonjour_ft.c libpurple/protocols/bonjour/jabber.c
diffstat 2 files changed, 54 insertions(+), 86 deletions(-) [+]
line wrap: on
line diff
--- a/libpurple/protocols/bonjour/bonjour_ft.c	Mon Nov 12 04:17:10 2007 +0000
+++ b/libpurple/protocols/bonjour/bonjour_ft.c	Mon Nov 12 05:09:17 2007 +0000
@@ -303,13 +303,13 @@
 		purple_debug_info("Bonjour", "bonjour-free-xfer-null.\n");
 		return;
 	}
-	purple_debug_info("Bonjour", "bonjour-free-xfer-%x.\n", (int)xfer);
+	purple_debug_info("Bonjour", "bonjour-free-xfer-%p.\n", xfer);
 	xf = (XepXfer*)xfer->data;
 	if(xf != NULL){
 		bd = (BonjourData*)xf->data;
 		if(bd != NULL){
 			bd->xfer_lists = g_list_remove(bd->xfer_lists, xfer);
-			purple_debug_info("Bonjour", "B free xfer from lists(0x%x).\n", (int)bd->xfer_lists);
+			purple_debug_info("Bonjour", "B free xfer from lists(%p).\n", bd->xfer_lists);
 		}
 		if (xf->proxy_connection != NULL)
 			purple_proxy_connect_cancel(xf->proxy_connection);
@@ -318,10 +318,11 @@
 		g_free(xf->jid);
 		g_free(xf->proxy_host);
 		g_free(xf->buddy_ip);
+		g_free(xf->sid);
 		g_free(xf);
 		xfer->data = NULL;
 	}
-	purple_debug_info("Bonjour", "Need close socket=0x%x.\n", xfer->fd);
+	purple_debug_info("Bonjour", "Need close socket=%d.\n", xfer->fd);
 }
 
 PurpleXfer *
@@ -347,7 +348,7 @@
 			return NULL;
 		}
 		xep_xfer->data = bd;
-		purple_debug_info("Bonjour", "Bonjour-new-xfer bd=0x%x data=0x%x.\n",(int)bd, (int)xep_xfer->data);
+		purple_debug_info("Bonjour", "Bonjour-new-xfer bd=%p data=%p.\n", bd, xep_xfer->data);
 		xep_xfer->mode = XEP_BYTESTREAMS | XEP_IBB;
 		xfer->data = xep_xfer;
 		xep_xfer->sid = NULL;
@@ -461,7 +462,7 @@
 				purple_xfer_cancel_remote(xfer);
 			}
 		} else {
-			purple_debug_info("Bonjour", "si offer Message type - Unknown-%d.\n",type);
+			purple_debug_info("Bonjour", "si offer Message type - Unknown-%d.\n", type);
 		}
 	}
 }
@@ -504,7 +505,7 @@
 							xf->proxy_host = g_strdup(host);
 							xf->proxy_port = portnum;
 							purple_debug_info("Bonjour", "bytestream offer parse"
-							                  "jid=%s host=%s port=0x%x.\n",jid, host, portnum);
+							                  "jid=%s host=%s port=%d.\n", jid, host, portnum);
 							bonjour_bytestreams_connect(xfer);
 						}
 					} else {
@@ -516,7 +517,7 @@
 			}
 
 		} else {
-			purple_debug_info("Bonjour", "bytestream offer Message type - Unknown-%d.\n",type);
+			purple_debug_info("Bonjour", "bytestream offer Message type - Unknown-%d.\n", type);
 		}
 	}
 }
@@ -631,7 +632,7 @@
 		} else if(acceptfd == -1) {
 
 		} else {
-			purple_debug_info("Bonjour", "Conjour-sock5-request-cb. state= %d, accept=%d\n", xf->sock5_req_state,acceptfd);
+			purple_debug_info("Bonjour", "Conjour-sock5-request-cb. state= %d, accept=%d\n", xf->sock5_req_state, acceptfd);
 			purple_input_remove(xfer->watcher);
 			close(source);
 			xfer->watcher = purple_input_add(acceptfd, PURPLE_INPUT_READ,
--- a/libpurple/protocols/bonjour/jabber.c	Mon Nov 12 04:17:10 2007 +0000
+++ b/libpurple/protocols/bonjour/jabber.c	Mon Nov 12 05:09:17 2007 +0000
@@ -711,23 +711,21 @@
 	}
 }
 
-int
-bonjour_jabber_send_message(BonjourJabber *data, const gchar *to, const gchar *body)
+static PurpleBuddy *
+_find_or_start_conversation(BonjourJabber *data, const gchar *to)
 {
-	xmlnode *message_node, *node, *node2;
-	gchar *message;
-	PurpleBuddy *pb;
-	BonjourBuddy *bb;
-	int ret;
+	PurpleBuddy *pb = NULL;
+	BonjourBuddy *bb = NULL;
+
+	g_return_val_if_fail(data != NULL, NULL);
+	g_return_val_if_fail(to != NULL, NULL);
 
 	pb = purple_find_buddy(data->account, to);
-	if (pb == NULL) {
-		purple_debug_info("bonjour", "Can't send a message to an offline buddy (%s).\n", to);
+	if (pb == NULL || pb->proto_data == NULL)
 		/* You can not send a message to an offline buddy */
-		return -10000;
-	}
+		return NULL;
 
-	bb = pb->proto_data;
+	bb = (BonjourBuddy *) pb->proto_data;
 
 	/* Check if there is a previously open conversation */
 	if (bb->conversation == NULL)
@@ -735,6 +733,8 @@
 		PurpleProxyConnectData *connect_data;
 		PurpleProxyInfo *proxy_info;
 
+		purple_debug_info("Bonjour", "Starting conversation with %s\n", to);
+
 		/* Make sure that the account always has a proxy of "none".
 		 * This is kind of dirty, but proxy_connect_none() isn't exposed. */
 		proxy_info = purple_account_get_proxy_info(data->account);
@@ -744,13 +744,12 @@
 		}
 		purple_proxy_info_set_type(proxy_info, PURPLE_PROXY_NONE);
 
-		connect_data =
-			purple_proxy_connect(data->account->gc, data->account, bb->ip,
-					     bb->port_p2pj, _connected_to_buddy, pb);
+		connect_data = purple_proxy_connect(data->account->gc, data->account,
+						    bb->ip, bb->port_p2pj, _connected_to_buddy, pb);
 
 		if (connect_data == NULL) {
 			purple_debug_error("bonjour", "Unable to connect to buddy (%s).\n", to);
-			return -10001;
+			return NULL;
 		}
 
 		bb->conversation = bonjour_jabber_conv_new();
@@ -759,6 +758,26 @@
 		 * that neeeds to wait until we're actually connected. */
 		bb->conversation->tx_handler = 0;
 	}
+	return pb;
+}
+
+int
+bonjour_jabber_send_message(BonjourJabber *data, const gchar *to, const gchar *body)
+{
+	xmlnode *message_node, *node, *node2;
+	gchar *message;
+	PurpleBuddy *pb;
+	BonjourBuddy *bb;
+	int ret;
+
+	pb = _find_or_start_conversation(data, to);
+	if (pb == NULL) {
+		purple_debug_info("bonjour", "Can't send a message to an offline buddy (%s).\n", to);
+		/* You can not send a message to an offline buddy */
+		return -10000;
+	}
+
+	bb = pb->proto_data;
 
 	message_node = xmlnode_new("message");
 	xmlnode_set_attrib(message_node, "to", bb->name);
@@ -853,57 +872,6 @@
 		g_slist_free(buddies);
 	}
 }
-static PurpleBuddy *
-_start_conversation(BonjourJabber *data, const gchar *to)
-{
-	PurpleBuddy *pb = NULL;
-	BonjourBuddy *bb = NULL;
-
-	if(data == NULL || to == NULL)
-		return NULL;
-
-	purple_debug_info("Bonjour", "start-conversation with  %s - \n", to);
-
-	pb = purple_find_buddy(data->account, to);
-	if (pb == NULL)
-		/* You can not send a message to an offline buddy */
-		return NULL;
-
-	bb = (BonjourBuddy *) pb->proto_data;
-	if(bb == NULL)
-		return NULL;
-
-	/* Check if there is a previously open conversation */
-	if (bb->conversation == NULL)
-	{
-		PurpleProxyConnectData *connect_data;
-		PurpleProxyInfo *proxy_info;
-
-		/* Make sure that the account always has a proxy of "none".
-		 * This is kind of dirty, but proxy_connect_none() isn't exposed. */
-		proxy_info = purple_account_get_proxy_info(data->account);
-		if (proxy_info == NULL) {
-			proxy_info = purple_proxy_info_new();
-			purple_account_set_proxy_info(data->account, proxy_info);
-		}
-		purple_proxy_info_set_type(proxy_info, PURPLE_PROXY_NONE);
-
-		connect_data = purple_proxy_connect(data->account->gc, data->account,
-						    bb->ip, bb->port_p2pj, _connected_to_buddy, pb);
-
-		if (connect_data == NULL) {
-			purple_debug_error("bonjour", "Unable to connect to buddy (%s).\n", to);
-			return NULL;
-		}
-
-		bb->conversation = bonjour_jabber_conv_new();
-		bb->conversation->connect_data = connect_data;
-		/* We don't want _send_data() to register the tx_handler;
-		 * that neeeds to wait until we're actually connected. */
-		bb->conversation->tx_handler = 0;
-	}
-	return pb;
-}
 
 XepIq *
 xep_iq_new(void *data, XepIqType type, const gchar *to, const gchar *id)
@@ -998,22 +966,21 @@
 int
 xep_iq_send(XepIq *iq)
 {
-	char *msg = NULL;
-	gint msg_len = 0;
 	int ret = -1;
 	PurpleBuddy *pb = NULL;
-	/* Convert xml node into stream */
-	msg = xmlnode_to_str(iq->node, &msg_len);
-	xmlnode_free(iq->node);
+
 	/* start the talk, reuse the message socket  */
-	pb = _start_conversation ((BonjourJabber*)iq->data, iq->to);
+	pb = _find_or_start_conversation ((BonjourJabber*)iq->data, iq->to);
 	/* Send the message */
-	if (pb != NULL)
+	if (pb != NULL) {
+		/* Convert xml node into stream */
+		gchar *msg = xmlnode_to_str(iq->node, NULL);
 		ret = _send_data(pb, msg);
-	g_free(msg);
-	if (ret == -1)
-		return -1;
-	return 0;
+		g_free(msg);
+	}
+	xmlnode_free(iq->node);
+
+	return (ret >= 0) ? 0 : -1;
 }
 
 /* This returns a ';' delimited string containing all non-localhost IPs */