changeset 30967:52803883f33f

refactor out the relay credential fetching code to its own module
author Marcus Lundblad <ml@update.uu.se>
date Sun, 05 Sep 2010 08:35:37 +0000
parents 75045bed7704
children c954ee775713
files libpurple/protocols/jabber/Makefile.am libpurple/protocols/jabber/Makefile.mingw libpurple/protocols/jabber/google/google_session.c
diffstat 3 files changed, 15 insertions(+), 127 deletions(-) [+]
line wrap: on
line diff
--- a/libpurple/protocols/jabber/Makefile.am	Sat Sep 04 17:47:54 2010 +0000
+++ b/libpurple/protocols/jabber/Makefile.am	Sun Sep 05 08:35:37 2010 +0000
@@ -38,6 +38,8 @@
 			  google/google_session.h \
 			  google/jingleinfo.c \
 			  google/jingleinfo.h \
+			  google/relay.c \
+			  google/relay.h \
 			  ibb.c \
 			  ibb.h \
 			  iq.c \
--- a/libpurple/protocols/jabber/Makefile.mingw	Sat Sep 04 17:47:54 2010 +0000
+++ b/libpurple/protocols/jabber/Makefile.mingw	Sun Sep 05 08:35:37 2010 +0000
@@ -61,6 +61,7 @@
 			google/google_roster.c \
 			google/google_session.c \
 			google/jingleinfo.c \
+			google/relay.c \
 			ibb.c \
 			iq.c \
 			jabber.c \
--- a/libpurple/protocols/jabber/google/google_session.c	Sat Sep 04 17:47:54 2010 +0000
+++ b/libpurple/protocols/jabber/google/google_session.c	Sun Sep 05 08:35:37 2010 +0000
@@ -21,6 +21,7 @@
 #include "internal.h"
 #include "debug.h"
 #include "google_session.h"
+#include "relay.h"
 
 #include "jingle/jingle.h"
 
@@ -336,83 +337,17 @@
 	return new_params;
 }
 
-static void
-jabber_google_relay_parse_response(const gchar *response, gchar **ip,
-	guint *udp, guint *tcp, guint *ssltcp, gchar **username, gchar **password)
-{
-	gchar **lines = g_strsplit(response, "\n", -1);
-	int i = 0;
-
-	for (; lines[i] ; i++) {
-		gchar *line = lines[i];
-		gchar **parts = g_strsplit(line, "=", 2);
-		
-		if (parts[0] && parts[1]) {
-			if (purple_strequal(parts[0], "relay.ip")) {
-				*ip = g_strdup(parts[1]);
-			} else if (purple_strequal(parts[0], "relay.udp_port")) {
-				*udp = atoi(parts[1]);
-			} else if (purple_strequal(parts[0], "relay.tcp_port")) {
-				*tcp = atoi(parts[1]);
-			} else if (purple_strequal(parts[0], "relay.ssltcp_port")) {
-				*ssltcp = atoi(parts[1]);
-			} else if (purple_strequal(parts[0], "username")) {
-				*username = g_strdup(parts[1]);
-			} else if (purple_strequal(parts[0], "password")) {
-				*password = g_strdup(parts[1]);
-			}
-		}
-		g_strfreev(parts);
-	}
-
-	g_strfreev(lines);
-}
 
 static void
-jabber_google_relay_remove_url_data(JabberStream *js, 
-	PurpleUtilFetchUrlData *url_data)
+jabber_google_relay_response_session_initiate_cb(GoogleSession *session,
+    const gchar *relay_ip, guint relay_udp, guint relay_tcp, guint relay_ssltcp,
+    const gchar *relay_username, const gchar *relay_password)
 {
-	GList *iter = js->google_relay_requests;
-
-	while (iter) {
-		if (iter->data == url_data) {
-			js->google_relay_requests =
-				g_list_delete_link(js->google_relay_requests, iter);
-			break;
-		}
-	}
-}
-
-static void
-jabber_google_relay_response_session_initiate_cb(PurpleUtilFetchUrlData *url_data, 
-	gpointer user_data, const gchar *url_text, gsize len, 
-	const gchar *error_message)
-{
-	GoogleSession *session = (GoogleSession *) user_data;
 	GParameter *params;
 	guint num_params;
 	JabberStream *js = session->js;
-	gchar *relay_ip = NULL;
-	guint relay_udp = 0;
-	guint relay_tcp = 0;
-	guint relay_ssltcp = 0;
-	gchar *relay_username = NULL;
-	gchar *relay_password = NULL;
 	GoogleAVSessionData *session_data =
 		(GoogleAVSessionData *) session->session_data;
-	
-	if (url_data) {
-		jabber_google_relay_remove_url_data(js, url_data);
-	}
-
-	purple_debug_info("jabber", "got response on HTTP request to relay server\n");
-
-	if (url_text && len > 0) {
-		purple_debug_info("jabber", "got Google relay request response:\n%s\n",
-			url_text);
-		jabber_google_relay_parse_response(url_text, &relay_ip, &relay_udp,
-			&relay_tcp, &relay_ssltcp, &relay_username, &relay_password);
-	}
 
 	session_data->media = purple_media_manager_create_media(
 			purple_media_manager_get(),
@@ -434,10 +369,6 @@
 	params =
 		jabber_google_session_get_params(js, relay_ip, relay_udp, relay_tcp,
 			relay_ssltcp, relay_username, relay_password, &num_params);
-
-	g_free(relay_ip);
-	g_free(relay_username);
-	g_free(relay_password);
 	
 	if (purple_media_add_stream(session_data->media, "google-voice",
 			session->remote_jid, PURPLE_MEDIA_AUDIO,
@@ -456,33 +387,6 @@
 	g_free(params);	
 }
 
-static void
-jabber_google_do_relay_request(JabberStream *js, GoogleSession *session,
-	PurpleUtilFetchUrlCallback cb)
-{
-	PurpleUtilFetchUrlData *url_data = NULL;
-	gchar *url = g_strdup_printf("http://%s", js->google_relay_host);
-	gchar *request =
-		g_strdup_printf("GET /create_session HTTP/1.0\r\n"
-			            "Host: %s\r\n"
-						"X-Talk-Google-Relay-Auth: %s\r\n"
-						"X-Google-Relay-Auth: %s\r\n\r\n", 
-			js->google_relay_host, js->google_relay_token, js->google_relay_token);
-	purple_debug_info("jabber", 
-		"sending Google relay request %s to %s\n", request, url); 
-	url_data = 
-		purple_util_fetch_url_request(url, FALSE, NULL, FALSE, request, FALSE,
-			cb, session);
-	if (url_data) {
-		js->google_relay_requests =
-			g_list_prepend(js->google_relay_requests, url_data);
-	} else {
-		purple_debug_error("jabber", "unable to create Google relay request\n");
-		cb(NULL, session, NULL, 0, NULL);
-	}
-	g_free(url);
-	g_free(request);
-}
 
 gboolean
 jabber_google_session_initiate(JabberStream *js, const gchar *who, PurpleMediaSessionType type)
@@ -531,8 +435,8 @@
 		jabber_google_do_relay_request(js, session,
 			jabber_google_relay_response_session_initiate_cb);
 	} else {
-		jabber_google_relay_response_session_initiate_cb(NULL, session, NULL, 0,
-			NULL);
+		jabber_google_relay_response_session_initiate_cb(session, NULL, 0, 0, 0,
+			NULL, NULL);
 	}
 	
 	/* we don't actually know yet wether it succeeded... maybe this is very
@@ -541,21 +445,13 @@
 }
 
 static void
-jabber_google_relay_response_session_handle_initiate_cb(
-    PurpleUtilFetchUrlData *url_data, 
-	gpointer user_data, const gchar *url_text, gsize len, 
-	const gchar *error_message)
+jabber_google_relay_response_session_handle_initiate_cb(GoogleSession *session,
+    const gchar *relay_ip, guint relay_udp, guint relay_tcp, guint relay_ssltcp,
+    const gchar *relay_username, const gchar *relay_password)
 {
-	GoogleSession *session = (GoogleSession *) user_data;
 	GParameter *params;
 	guint num_params;
 	JabberStream *js = session->js;
-	gchar *relay_ip = NULL;
-	guint relay_udp = 0;
-	guint relay_tcp = 0;
-	guint relay_ssltcp = 0;
-	gchar *relay_username = NULL;
-	gchar *relay_password = NULL;
 	xmlnode *codec_element;
 	xmlnode *desc_element;
 	const gchar *xmlns;
@@ -566,18 +462,7 @@
 	GoogleAVSessionData *session_data =
 		(GoogleAVSessionData *) session->session_data;
 
-	if (url_data) {
-		jabber_google_relay_remove_url_data(js, url_data);
-	}
-
-	if (url_text && len > 0) {
-		purple_debug_info("jabber", "got Google relay request response:\n%s\n",
-			url_text);
-		jabber_google_relay_parse_response(url_text, &relay_ip, &relay_udp,
-			&relay_tcp, &relay_ssltcp, &relay_username, &relay_password);
-	}
-
-	params = 
+	params =
 		jabber_google_session_get_params(js, relay_ip, relay_udp, relay_tcp, 
 	    	relay_ssltcp, relay_username, relay_password, &num_params);
 
@@ -718,8 +603,8 @@
 		jabber_google_do_relay_request(js, session, 
 			jabber_google_relay_response_session_handle_initiate_cb);
 	} else {
-		jabber_google_relay_response_session_handle_initiate_cb(NULL, session,
-			NULL, 0, NULL);
+		jabber_google_relay_response_session_handle_initiate_cb(session, NULL,
+			0, 0, 0, NULL, NULL);
 	}
 
 	return TRUE;