changeset 25600:1cbe5a9f6efb

* adding bosh files to Makefile.am & .mingw * establishing HTTP connection for BOSH
author Tobias Markmann <tfar@soc.pidgin.im>
date Tue, 05 Aug 2008 18:44:42 +0000
parents 3176d5a0b9dd
children 15d9825a5621
files libpurple/protocols/jabber/Makefile.am libpurple/protocols/jabber/Makefile.mingw libpurple/protocols/jabber/bosh.c libpurple/protocols/jabber/bosh.h libpurple/protocols/jabber/jabber.c
diffstat 5 files changed, 99 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/libpurple/protocols/jabber/Makefile.am	Sun Aug 03 22:58:10 2008 +0000
+++ b/libpurple/protocols/jabber/Makefile.am	Tue Aug 05 18:44:42 2008 +0000
@@ -9,6 +9,8 @@
 			  auth.h \
 			  buddy.c \
 			  buddy.h \
+			  bosh.c \
+			  bosh.h \
 			  chat.c \
 			  chat.h \
 			  disco.c \
--- a/libpurple/protocols/jabber/Makefile.mingw	Sun Aug 03 22:58:10 2008 +0000
+++ b/libpurple/protocols/jabber/Makefile.mingw	Tue Aug 05 18:44:42 2008 +0000
@@ -46,6 +46,7 @@
 			adhoccommands.c \
 			auth.c \
 			buddy.c \
+			bosh.c
 			caps.c \
 			chat.c \
 			disco.c \
--- a/libpurple/protocols/jabber/bosh.c	Sun Aug 03 22:58:10 2008 +0000
+++ b/libpurple/protocols/jabber/bosh.c	Tue Aug 05 18:44:42 2008 +0000
@@ -37,13 +37,51 @@
 #include "pep.h"
 #include "adhoccommands.h"
 
-PurpleHTTPHeaderField* jabber_bosh_http_header_field(const char *name, *const char *value) {
-	PurpleHTTPHeaderField *tmp = g_new0(PurpleHTTPHeaderField, 0);
-	tmp->name = g_strdup(name);
-	tmp->value = g_strdup(value);
-	return tmp;
+void jabber_bosh_connection_init(PurpleBOSHConnection *conn, PurpleAccount *account, char *url) {
+	conn->pipelining = TRUE;
+	conn->account = account;
+	if (!purple_url_parse(url, &(conn->host), &(conn->port), &(conn->path), &(conn->user), &(conn->passwd))) {
+		purple_debug_info("jabber", "Unable to parse given URL.\n");
+		return;
+	}
+	if (conn->user || conn->passwd) {
+		purple_debug_info("jabber", "Sorry, HTTP Authentication isn't supported yet. Username and password in the BOSH URL will be ignored.\n");
+	}
+	conn->conn_a = g_new0(PurpleHTTPConnection, 1);
+	jabber_bosh_http_connection_init(conn->conn_a, conn->account, conn->host, conn->port);
+	conn->conn_a->userdata = conn;
+}
+
+static void jabber_bosh_connection_connected(PurpleHTTPConnection *conn) {
+	PurpleBOSHConnection *bosh_conn = conn->userdata;
+	if (bosh_conn->connect_cb) bosh_conn->connect_cb(bosh_conn);
+}
+
+void jabber_bosh_connection_connect(PurpleBOSHConnection *conn) {
+	conn->conn_a->connect_cb = jabber_bosh_connection_connected;
+	jabber_bosh_http_connection_connect(conn->conn_a);	
+}
+
+
+void jabber_bosh_http_connection_init(PurpleHTTPConnection *conn, PurpleAccount *account, char *host, int port) {
+	conn->account = account;
+	conn->host = host;
+	conn->port = port;
+	conn->connect_cb = NULL;
+}
+
+static void jabber_bosh_http_connection_callback(gpointer data, gint source, const gchar *error) {
+	PurpleHTTPConnection *conn = data;
+	if (source < 0) {
+		purple_debug_info("jabber", "Couldn't connect becasue of: %s\n", error);
+		return;
+	}
+	conn->fd = source;
+	if (conn->connect_cb) conn->connect_cb(conn);
 }
 
 void jabber_bosh_http_connection_connect(PurpleHTTPConnection *conn) {
-	
+	if((purple_proxy_connect(&(conn->handle), conn->account, conn->host, conn->port, jabber_bosh_http_connection_callback, conn)) == NULL) {
+		purple_debug_info("jabber", "Unable to connect to %s.\n", conn->host);
+	}
 }
\ No newline at end of file
--- a/libpurple/protocols/jabber/bosh.h	Sun Aug 03 22:58:10 2008 +0000
+++ b/libpurple/protocols/jabber/bosh.h	Tue Aug 05 18:44:42 2008 +0000
@@ -26,23 +26,44 @@
 
 typedef struct _PurpleHTTPRequest PurpleHTTPRequest;
 typedef struct _PurpleHTTPResponse PurpleHTTPResponse;
-typedef struct _PurpleHTTPHeaderField PurpleHTTPHeaderField;
+typedef struct _PurpleHTTPConnection PurpleHTTPConnection;
+typedef struct _PurpleBOSHConnection PurpleBOSHConnection;
 
+typedef void (*PurpleHTTPConnectionConnectFunction)(PurpleHTTPConnection *conn);
 typedef void (*PurpleHTTPRequestCallback)(PurpleHTTPRequest *req, PurpleHTTPResponse *res, void *userdata);
+typedef void (*PurpleBOSHConnectionConnectFunction)(PurpleBOSHConnection *conn);
+typedef void (*PurpleBOSHConnectionReciveFunction)(PurpleBOSHConnection *conn, xmlnode *node);
 
-typedef struct {
-    int fd;
-    PurpleConnection *conn;
-    GQueue *requests;
+struct _PurpleBOSHConnection {
+    /* decoded URL */
+    char *host;
+    int port;
+    char *path; 
+    char *user;
+    char *passwd;
+    
     void *userdata;
-} PurpleHTTPConnection;
-
-typedef struct {
-    char *url;
+    PurpleAccount *account;
     gboolean pipelining;
     PurpleHTTPConnection *conn_a;
     PurpleHTTPConnection *conn_b;
-} PurpleBOSHConnection;
+    
+    PurpleBOSHConnectionConnectFunction connect_cb;
+    PurpleBOSHConnectionReciveFunction receive_cb;
+};
+
+struct _PurpleHTTPConnection {
+    int fd;
+    char *host;
+    int port;
+    int handle;
+    PurpleConnection *conn;
+    PurpleAccount *account;
+    GQueue *requests;
+    
+    PurpleHTTPConnectionConnectFunction connect_cb;
+    void *userdata;
+};
 
 struct _PurpleHTTPRequest {
     PurpleHTTPRequestCallback cb;
@@ -55,21 +76,18 @@
 
 struct _PurpleHTTPResponse {
     int status;
-    GList *header;
+    GHashTable *header;
     char *data;
 };
 
-struct _PurpleHTTPHeaderField {
-    char *name;
-    char *value;
-};
+void jabber_bosh_connection_init(PurpleBOSHConnection *conn, PurpleAccount *account, char *url);
+void jabber_bosh_connection_connect(PurpleBOSHConnection *conn);
 
-PurpleHTTPHeaderField *jabber_bosh_http_header_field(const char *name, const char *value);
-
+void jabber_bosh_http_connection_init(PurpleHTTPConnection *conn, PurpleAccount *account, char *host, int port);
 void jabber_bosh_http_connection_connect(PurpleHTTPConnection *conn);
 void jabber_bosh_http_send_request(PurpleHTTPConnection *conn, PurpleHTTPRequest *req);
 void jabber_bosh_http_connection_clean(PurpleHTTPConnection *conn);
 
-void jabber_bosh_http_request_init(PurpleHTTPRequest *req, const char *method, const char *url, PurpleHTTPRequestCallback cb, void *userdata);
+void jabber_bosh_http_request_init(PurpleHTTPRequest *req, const char *method, const char *path, PurpleHTTPRequestCallback cb, void *userdata);
 void jabber_bosh_http_request_clean(PurpleHTTPRequest *req);
 #endif /* _PURPLE_JABBER_BOSH_H_ */
--- a/libpurple/protocols/jabber/jabber.c	Sun Aug 03 22:58:10 2008 +0000
+++ b/libpurple/protocols/jabber/jabber.c	Tue Aug 05 18:44:42 2008 +0000
@@ -521,6 +521,12 @@
 	jabber_stream_set_state(js, JABBER_STREAM_INITIALIZING_ENCRYPTION);
 }
 
+static void
+jabber_bosh_login_callback(PurpleBOSHConnection *conn) 
+{
+	purple_debug_info("jabber","YAY...BOSH connection established.\n");
+}
+
 static void 
 txt_resolved_cb(PurpleTxtResponse *resp, int results, gpointer data)
 {
@@ -533,7 +539,8 @@
 		tmp = g_strdup_printf(_("Could not find alternative XMPP connection methods after failing to connect directly.\n"));
 		purple_connection_error_reason (gc,
 				PURPLE_CONNECTION_ERROR_NETWORK_ERROR, tmp);
-		g_free(tmp);	
+		g_free(tmp);
+		return;	
 	}
 	
 	for (n = 0; n < results; n++) {
@@ -541,12 +548,19 @@
 		token = g_strsplit(resp[n].content, "=", 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.url = g_strdup(token[1]);
+			jabber_bosh_connection_init(&(js->bosh), gc->account, token[1]);
 			g_strfreev(token);
 			break;
 		}
 		g_strfreev(token);
 	}
+	if (js->bosh.host) {
+		js->bosh.userdata = gc;
+		js->bosh.connect_cb = jabber_bosh_login_callback;
+		jabber_bosh_connection_connect(&(js->bosh));
+	} else {
+		purple_debug_info("jabber","Didn't find an alternative connection method.\n");
+	}
 }
 
 static void