changeset 30494:c3b32b027d9d

propagate from branch 'im.pidgin.pidgin' (head 29f6b9d81574bfa9235dbd438a8934f06552e368) to branch 'im.pidgin.cpw.malu.xmpp.google_relay' (head 7e11dae4b92b8ef950ff2a1ab3ed673b56d4f772)
author Marcus Lundblad <ml@update.uu.se>
date Sun, 20 Sep 2009 19:56:50 +0000
parents 5f97830906bc (current diff) 196e4c5b2043 (diff)
children 6fc4df1d12a8
files libpurple/media.c libpurple/protocols/jabber/jabber.c
diffstat 4 files changed, 115 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/libpurple/media.c	Sun Sep 20 10:57:21 2009 +0000
+++ b/libpurple/media.c	Sun Sep 20 19:56:50 2009 +0000
@@ -2413,7 +2413,7 @@
 	FsParticipant *participant;
 	PurpleMediaStream *stream;
 	GList *iter;
-
+	
 	g_return_if_fail(FS_IS_STREAM(fsstream));
 	g_return_if_fail(session != NULL);
 
--- a/libpurple/protocols/jabber/google.c	Sun Sep 20 10:57:21 2009 +0000
+++ b/libpurple/protocols/jabber/google.c	Sun Sep 20 19:56:50 2009 +0000
@@ -341,47 +341,27 @@
 	return new_params;
 }
 
-
-gboolean
-jabber_google_session_initiate(JabberStream *js, const gchar *who, PurpleMediaSessionType type)
+static void
+jabber_google_relay_response_cb(PurpleUtilFetchUrlData *url_data, 
+	gpointer user_data, const gchar *url_text, gsize len, 
+	const gchar *error_message)
 {
-	GoogleSession *session;
-	JabberBuddy *jb;
-	JabberBuddyResource *jbr;
-	gchar *jid;
+	GoogleSession *session = (GoogleSession *) user_data;
 	GParameter *params;
 	guint num_params;
+	JabberStream *js = session->js;
 
-	/* construct JID to send to */
-	jb = jabber_buddy_find(js, who, FALSE);
-	if (!jb) {
-		purple_debug_error("jingle-rtp",
-				"Could not find Jabber buddy\n");
-		return FALSE;
-	}
-	jbr = jabber_buddy_find_resource(jb, NULL);
-	if (!jbr) {
-		purple_debug_error("jingle-rtp",
-				"Could not find buddy's resource\n");
-	}
+	js->google_relay_request = NULL;
+
+	purple_debug_info("jabber", "got response on HTTP request to relay server\n");
 
-	if ((strchr(who, '/') == NULL) && jbr && (jbr->name != NULL)) {
-		jid = g_strdup_printf("%s/%s", who, jbr->name);
+	if (url_text && len > 0) {
+		purple_debug_info("jabber", "got Google relay request response:\n%s\n",
+			url_text);
 	} else {
-		jid = g_strdup(who);
+	
 	}
 
-	session = g_new0(GoogleSession, 1);
-	session->id.id = jabber_get_next_id(js);
-	session->id.initiator = g_strdup_printf("%s@%s/%s", js->user->node,
-			js->user->domain, js->user->resource);
-	session->state = SENT_INITIATE;
-	session->js = js;
-	session->remote_jid = jid;
-
-	if (type & PURPLE_MEDIA_VIDEO)
-		session->video = TRUE;
-
 	session->media = purple_media_manager_create_media(
 			purple_media_manager_get(),
 			purple_connection_get_account(js->gc),
@@ -414,9 +394,69 @@
 		return FALSE;
 	}
 
-	g_free(params);
+	g_free(params);	
+}
+
+gboolean
+jabber_google_session_initiate(JabberStream *js, const gchar *who, PurpleMediaSessionType type)
+{
+	GoogleSession *session;
+	JabberBuddy *jb;
+	JabberBuddyResource *jbr;
+	gchar *jid;
+
+	/* construct JID to send to */
+	jb = jabber_buddy_find(js, who, FALSE);
+	if (!jb) {
+		purple_debug_error("jingle-rtp",
+				"Could not find Jabber buddy\n");
+		return FALSE;
+	}
+	jbr = jabber_buddy_find_resource(jb, NULL);
+	if (!jbr) {
+		purple_debug_error("jingle-rtp",
+				"Could not find buddy's resource\n");
+	}
+
+	if ((strchr(who, '/') == NULL) && jbr && (jbr->name != NULL)) {
+		jid = g_strdup_printf("%s/%s", who, jbr->name);
+	} else {
+		jid = g_strdup(who);
+	}
 
-	return (session->media != NULL) ? TRUE : FALSE;
+	session = g_new0(GoogleSession, 1);
+	session->id.id = jabber_get_next_id(js);
+	session->id.initiator = g_strdup_printf("%s@%s/%s", js->user->node,
+			js->user->domain, js->user->resource);
+	session->state = SENT_INITIATE;
+	session->js = js;
+	session->remote_jid = jid;
+
+	if (type & PURPLE_MEDIA_VIDEO)
+		session->video = TRUE;
+
+	/* if we got a relay token and relay host in google:jingleinfo, issue an
+	 HTTP request to get that data */
+	if (js->google_relay_host && js->google_relay_token) {
+		const gchar *url = 
+			g_strdup_printf("http://%s/create_session", js->google_relay_host);
+		const gchar *request =
+			g_strdup_printf("GET /create_session HTTP 1.1\r\n"
+							"X-Talk-Google-Relay-Auth: %s\r\n"
+							"X-Google-Relay-Auth: %s\r\n", 
+				js->google_relay_token, js->google_relay_token);
+		js->google_relay_request =
+			purple_util_fetch_url_request(url, TRUE, NULL, TRUE, request, FALSE,
+				jabber_google_relay_response_cb, session);
+		g_free(url);
+		g_free(request);
+	} else {
+		jabber_google_relay_response_cb(NULL, session, NULL, 0, NULL);
+	}
+	
+	/* we don't actually know yet wether it succeeded... maybe this is very
+	 wrong... */
+	return TRUE;
 }
 
 static gboolean
@@ -1345,6 +1385,7 @@
                                  JabberIqType type, xmlnode *query)
 {
 	const xmlnode *stun = xmlnode_get_child(query, "stun");
+	const xmlnode *relay = xmlnode_get_child(query, "relay");
 	gchar *my_bare_jid;
 
 	/*
@@ -1388,8 +1429,23 @@
 			}
 		}
 	}
-	/* should perhaps handle relays later on, or maybe wait until
-	 Google supports a common standard... */
+
+	if (relay) {
+		const xmlnode *token = xmlnode_get_child(relay, "token");
+		const xmlnode *server = xmlnode_get_child(relay, "server");
+		
+		if (token) {
+			gchar *relay_token = xmlnode_get_data(token);
+
+			/* we let js own the string returned from xmlnode_get_data */
+			js->google_relay_token = relay_token;
+		}
+
+		if (server) {
+			js->google_relay_host = 
+				g_strdup(xmlnode_get_attrib(server, "host"));
+		}
+	}
 }
 
 static void
--- a/libpurple/protocols/jabber/jabber.c	Sun Sep 20 10:57:21 2009 +0000
+++ b/libpurple/protocols/jabber/jabber.c	Sun Sep 20 19:56:50 2009 +0000
@@ -842,6 +842,9 @@
 	js->stun_ip = NULL;
 	js->stun_port = 0;
 	js->stun_query = NULL;
+	js->google_relay_token = NULL;
+	js->google_relay_host = NULL;
+	js->google_relay_request = NULL;
 
 	/* if we are idle, set idle-ness on the stream (this could happen if we get
 		disconnected and the reconnects while being idle. I don't think it makes
@@ -1563,6 +1566,15 @@
 		js->stun_query = NULL;
 	}
 
+	/* remove Google relay-related stuff */
+	g_free(js->google_relay_token);
+	g_free(js->google_relay_host);
+
+	if (js->google_relay_request != NULL) {
+		purple_util_fetch_url_cancel(js->google_relay_request);
+		js->google_relay_request = NULL;
+	}
+
 	g_free(js);
 
 	gc->proto_data = NULL;
@@ -3021,6 +3033,9 @@
 		jbr = jabber_buddy_find_resource(jb, resource);
 		g_free(resource);
 
+		/*
+		return jabber_google_session_initiate(js, who, type);
+		*/
 		if (type & PURPLE_MEDIA_AUDIO &&
 				!jabber_resource_has_capability(jbr,
 				JINGLE_APP_RTP_SUPPORT_AUDIO) &&
--- a/libpurple/protocols/jabber/jabber.h	Sun Sep 20 10:57:21 2009 +0000
+++ b/libpurple/protocols/jabber/jabber.h	Sun Sep 20 19:56:50 2009 +0000
@@ -272,7 +272,12 @@
 	gchar *stun_ip;
 	int stun_port;
 	PurpleDnsQueryData *stun_query;
-	/* later add stuff to handle TURN relays... */
+
+	/* stuff for Google's relay handling */
+	gchar *google_relay_token;
+	gchar *google_relay_host;
+	PurpleUtilFetchUrlData *google_relay_request; /* the HTTP request to get */
+												/* relay info */
 };
 
 typedef gboolean (JabberFeatureEnabled)(JabberStream *js, const gchar *namespace);