changeset 26590:acee7d2b1983

Hide PurpleTxtResponse and use a more purple API
author Paul Aurich <paul@darkrain42.org>
date Sat, 11 Apr 2009 21:20:31 +0000
parents 400f74b95c0e
children 178718b181e4
files libpurple/dnssrv.c libpurple/dnssrv.h libpurple/protocols/jabber/jabber.c
diffstat 3 files changed, 80 insertions(+), 41 deletions(-) [+]
line wrap: on
line diff
--- a/libpurple/dnssrv.c	Sat Apr 11 20:31:19 2009 +0000
+++ b/libpurple/dnssrv.c	Sat Apr 11 21:20:31 2009 +0000
@@ -65,6 +65,10 @@
 	DNS_FREE_TYPE FreeType) = NULL;
 #endif
 
+struct _PurpleTxtResponse {
+    char *content;
+};
+
 struct _PurpleSrvQueryData {
 	union {
 		PurpleSrvCallback srv;
@@ -246,29 +250,30 @@
 			}
 			cb(res, size, query_data->extradata);
 		} else if (type == T_TXT) {
+			GSList *responses = NULL;
 			PurpleTxtResponse *res;
-			PurpleTxtResponse *tmp;
 			PurpleTxtCallback cb = query_data->cb.txt;
 			if (read(source, &size, sizeof(int)) == sizeof(int)) {
 				ssize_t red;
 				purple_debug_info("dnssrv","found %d TXT entries\n", size);
-				tmp = res = g_new0(PurpleTxtResponse, size);
+				res = g_new0(PurpleTxtResponse, 1);
 				for (i = 0; i < size; i++) {
-					red = read(source, tmp++, sizeof(PurpleTxtResponse));
+					red = read(source, res, sizeof(PurpleTxtResponse));
 					if (red != sizeof(PurpleTxtResponse)) {
 						purple_debug_error("dnssrv","unable to read txt "
 								"response: %s\n", g_strerror(errno));
 						size = 0;
 						g_free(res);
-						res = NULL;
+						g_slist_foreach(responses, (GFunc)purple_txt_response_destroy, NULL);
+						g_slist_free(responses);
+						responses = NULL;
+						break;
 					}
 				}
 			} else {
 				purple_debug_info("dnssrv","found 0 TXT entries; errno is %i\n", errno);
-				size = 0;
-				res = NULL;
 			}
-			cb(res, size, query_data->extradata);			
+			cb(responses, query_data->extradata);			
 		} else {
 			purple_debug_info("dnssrv","type unknown of DNS result entry; errno is %i\n", errno);
 		}
@@ -316,22 +321,12 @@
 			PurpleTxtResponse *txtres_tmp = NULL;
 			GSList *lst = query_data->results;
 
-			size = g_slist_length(lst);
-
-			if(query_data->cb.txt && size > 0)
-				txtres_tmp = txtres = g_new0(PurpleTxtResponse, size);
-			while (lst) {
-				if(query_data->cb.txt)
-					memcpy(txtres_tmp++, lst->data, sizeof(PurpleTxtResponse));
-				g_free(lst->data);
-				lst = g_slist_remove(lst, lst->data);
+			purple_debug_info("dnssrv", "found %d TXT entries\n", g_slist_length(lst));
+			
+			if (query_data->cb.txt) {
+				query_data->results = NULL;
+				query_data->cb.txt(lst, query_data->extradata);
 			}
-
-			query_data->results = NULL;
-
-			purple_debug_info("dnssrv", "found %d TXT entries\n", size);
-			
-			if(query_data->cb.txt) query_data->cb.txt(txtres, size, query_data->extradata);
 		} else {
 			purple_debug_error("dnssrv", "unknown query type");
 		}
@@ -557,14 +552,14 @@
 	if(pipe(in) || pipe(out)) {
 		purple_debug_error("dnssrv", "Could not create pipe\n");
 		g_free(query);
-		cb(NULL, 0, extradata);
+		cb(NULL, extradata);
 		return NULL;
 	}
 
 	pid = fork();
 	if (pid == -1) {
 		purple_debug_error("dnssrv", "Could not create process!\n");
-		cb(NULL, 0, extradata);
+		cb(NULL, extradata);
 		g_free(query);
 		return NULL;
 	}
@@ -625,7 +620,7 @@
 		}
 	}
 
-	/* The query isn't going to happen, so finish the SRV lookup now.
+	/* The query isn't going to happen, so finish the TXT lookup now.
 	 * Asynchronously call the callback since stuff may not expect
 	 * the callback to be called before this returns */
 	if (query_data->error_message != NULL)
@@ -665,3 +660,19 @@
 {
 	purple_srv_cancel(query_data);
 }
+
+const gchar *
+purple_txt_response_get_content(PurpleTxtResponse *resp)
+{
+	g_return_val_if_fail(resp != NULL, NULL);
+
+	return resp->content;
+}
+
+void purple_txt_response_destroy(PurpleTxtResponse *resp)
+{
+	g_return_if_fail(resp != NULL);
+
+	g_free(resp->content);
+	g_free(resp);
+}
--- a/libpurple/dnssrv.h	Sat Apr 11 20:31:19 2009 +0000
+++ b/libpurple/dnssrv.h	Sat Apr 11 21:20:31 2009 +0000
@@ -32,6 +32,8 @@
 typedef struct _PurpleSrvResponse PurpleSrvResponse;
 typedef struct _PurpleTxtResponse PurpleTxtResponse;
 
+#include <glib.h>
+
 struct _PurpleSrvResponse {
 	char hostname[256];
 	int port;
@@ -39,12 +41,15 @@
 	int pref;
 };
 
-struct _PurpleTxtResponse {
-    char *content;
-};
+typedef void (*PurpleSrvCallback)(PurpleSrvResponse *resp, int results, gpointer data);
 
-typedef void (*PurpleSrvCallback)(PurpleSrvResponse *resp, int results, gpointer data);
-typedef void (*PurpleTxtCallback)(PurpleTxtResponse *resp, int results, gpointer data);
+/**
+ * Callback that returns the data retrieved from a DNS TXT lookup.
+ *
+ * @param responses   A GSList of PurpleTxtResponse objects.
+ * @param data        The extra data passed to purple_txt_resolve.
+ */
+typedef void (*PurpleTxtCallback)(GSList *responses, gpointer data);
 
 /**
  * Queries an SRV record.
@@ -71,6 +76,8 @@
  * @param domain Domain name to query (e.g. "blubb.com")
  * @param cb A callback which will be called with the results
  * @param extradata Extra data to be passed to the callback
+ *
+ * @since 2.6.0
  */
 PurpleSrvQueryData *purple_txt_resolve(const char *owner, const char *domain, PurpleTxtCallback cb, gpointer extradata);
 
@@ -78,9 +85,27 @@
  * Cancel an TXT DNS query.
  *
  * @param query_data The request to cancel.
+ * @since 2.6.0
  */
 void purple_txt_cancel(PurpleSrvQueryData *query_data);
 
+/**
+ * Get the value of the current TXT record.
+ *
+ * @param resp  The TXT response record
+ * @returns The value of the current TXT record.
+ * @since 2.6.0
+ */
+const gchar *purple_txt_response_get_content(PurpleTxtResponse *resp);
+
+/**
+ * Destroy a TXT DNS response object.
+ *
+ * @param response The PurpleTxtResponse to destroy.
+ * @since 2.6.0
+ */
+void purple_txt_response_destroy(PurpleTxtResponse *resp);
+
 #ifdef __cplusplus
 }
 #endif
--- a/libpurple/protocols/jabber/jabber.c	Sat Apr 11 20:31:19 2009 +0000
+++ b/libpurple/protocols/jabber/jabber.c	Sat Apr 11 21:20:31 2009 +0000
@@ -584,14 +584,13 @@
 }
 
 static void 
-txt_resolved_cb(PurpleTxtResponse *resp, int results, gpointer data)
+txt_resolved_cb(GSList *responses, gpointer data)
 {
 	JabberStream *js = data;
-	int n;
-	
+
 	js->srv_query_data = NULL;
 
-	if (results == 0) {
+	if (responses == NULL) {
 		gchar *tmp;
 		tmp = g_strdup_printf(_("Could not find alternative XMPP connection methods after failing to connect directly.\n"));
 		purple_connection_error_reason (js->gc,
@@ -599,10 +598,11 @@
 		g_free(tmp);
 		return;	
 	}
-	
-	for (n = 0; n < results; n++) {
+
+	while (responses) {
+		PurpleTxtResponse *resp = responses->data;
 		gchar **token;
-		token = g_strsplit(resp[n].content, "=", 2);
+		token = g_strsplit(purple_txt_response_get_content(resp), "=", 2);
 		if (!strcmp(token[0], "_xmpp-client-xbosh")) {
 			purple_debug_info("jabber","Found alternative connection method using %s at %s.\n", token[0], token[1]);
 			js->bosh = jabber_bosh_connection_init(js, token[1]);
@@ -611,17 +611,20 @@
 			break;
 		}
 		g_strfreev(token);
+		purple_txt_response_destroy(resp);
+		responses = g_slist_delete_link(responses, responses);
 	}
+
 	if (js->bosh) {
 		jabber_bosh_connection_connect(js->bosh);
 	} else {
 		purple_debug_info("jabber","Didn't find an alternative connection method.\n");
 	}
 
-	for (n = 0; n < results; ++n)
-		g_free(resp[n].content);
-
-	g_free(resp);
+	if (responses) {
+		g_slist_foreach(responses, (GFunc)purple_txt_response_destroy, NULL);
+		g_slist_free(responses);
+	}
 }
 
 static void