/soc/2012/tomkiewicz/gg: 50f7a92eeb90: Gadu-Gadu: code refactori...

Tomasz Wasilczyk tomkiewicz at cpw.pidgin.im
Thu Jul 5 13:31:51 EDT 2012


Changeset: 50f7a92eeb90a05b80473bd395da0f74a58b53ec
Author:	 Tomasz Wasilczyk <tomkiewicz at cpw.pidgin.im>
Date:	 2012-06-29 13:06 +0200
Branch:	 soc.2012.gg
URL: http://hg.pidgin.im/soc/2012/tomkiewicz/gg/rev/50f7a92eeb90

Description:

Gadu-Gadu: code refactoring - token request, proxy setup (deprecated)

diffstat:

 libpurple/protocols/gg/Makefile.am  |    6 +-
 libpurple/protocols/gg/account.c    |  108 +++++++++++++++++++
 libpurple/protocols/gg/account.h    |   21 +++
 libpurple/protocols/gg/deprecated.c |   33 +++++
 libpurple/protocols/gg/deprecated.h |    8 +
 libpurple/protocols/gg/gg.c         |  201 +++++------------------------------
 libpurple/protocols/gg/gg.h         |   18 +--
 7 files changed, 210 insertions(+), 185 deletions(-)

diffs (truncated from 551 to 300 lines):

diff --git a/libpurple/protocols/gg/Makefile.am b/libpurple/protocols/gg/Makefile.am
--- a/libpurple/protocols/gg/Makefile.am
+++ b/libpurple/protocols/gg/Makefile.am
@@ -59,7 +59,11 @@
 	resolver-purple.h \
 	resolver-purple.c \
 	image.h \
-	image.c
+	image.c \
+	account.h \
+	account.c \
+	deprecated.h \
+	deprecated.c
 
 AM_CFLAGS = $(st)
 
diff --git a/libpurple/protocols/gg/account.c b/libpurple/protocols/gg/account.c
new file mode 100644
--- /dev/null
+++ b/libpurple/protocols/gg/account.c
@@ -0,0 +1,108 @@
+#include "account.h"
+
+#include <libgadu.h>
+#include <debug.h>
+
+#include "deprecated.h"
+
+typedef struct
+{
+	ggp_account_token_cb callback;
+	void *user_data;
+	
+	struct gg_http *h;
+	guint inpa;
+} ggp_account_token_reqdata;
+
+static void ggp_account_token_handler(gpointer _reqdata, gint fd, PurpleInputCondition cond);
+
+static void ggp_account_token_cb_call(ggp_account_token_reqdata *reqdata, ggp_account_token *token);
+
+/***********************/
+
+void ggp_account_token_free(ggp_account_token *token)
+{
+	g_free(token->id);
+	g_free(token->data);
+	g_free(token);
+}
+
+static void ggp_account_token_cb_call(ggp_account_token_reqdata *reqdata, ggp_account_token *token)
+{
+	reqdata->callback(token, reqdata->user_data);
+	purple_input_remove(reqdata->inpa);
+	gg_token_free(reqdata->h);
+	g_free(reqdata);
+}
+
+void ggp_account_token_request(PurpleConnection *gc, ggp_account_token_cb callback, void *user_data)
+{
+	struct gg_http *h;
+	ggp_account_token_reqdata *reqdata;
+	
+	if (!ggp_deprecated_setup_proxy(gc))
+	{
+		callback(NULL, user_data);
+		return;
+	}
+	
+	h = gg_token(1);
+	
+	if (!h)
+	{
+		callback(NULL, user_data);
+		return;
+	}
+	
+	reqdata = g_new(ggp_account_token_reqdata, 1);
+	reqdata->callback = callback;
+	reqdata->user_data = user_data;
+	reqdata->h = h;
+	
+	reqdata->inpa = purple_input_add(h->fd, PURPLE_INPUT_READ, ggp_account_token_handler, reqdata);
+}
+
+static void ggp_account_token_handler(gpointer _reqdata, gint fd, PurpleInputCondition cond)
+{
+	ggp_account_token_reqdata *reqdata = _reqdata;
+	struct gg_token *token_info;
+	ggp_account_token *token;
+	
+	purple_debug_info("gg", "ggp_account_token_handler: got fd update [check=%d, state=%d]\n",
+		reqdata->h->check, reqdata->h->state);
+	
+	if (reqdata->h->state == GG_STATE_ERROR || gg_token_watch_fd(reqdata->h) == -1)
+	{
+		purple_debug_error("gg", "ggp_account_token_handler: failed to request token: %d\n",
+			reqdata->h->error);
+		ggp_account_token_cb_call(reqdata, NULL);
+		return;
+	}
+	
+	if (reqdata->h->state != GG_STATE_DONE)
+	{
+		purple_input_remove(reqdata->inpa);
+		reqdata->inpa = purple_input_add(reqdata->h->fd,
+			(reqdata->h->check == 1) ? PURPLE_INPUT_WRITE : PURPLE_INPUT_READ,
+			ggp_account_token_handler, reqdata);
+		return;
+	}
+	
+	if (!reqdata->h->data || !reqdata->h->body)
+	{
+		purple_debug_error("gg", "ggp_account_token_handler: failed to fetch token: %d\n",
+			reqdata->h->error); // TODO: will anything be here in that case?
+		ggp_account_token_cb_call(reqdata, NULL);
+		return;
+	}
+	
+	
+	token = g_new(ggp_account_token, 1);
+	
+	token_info = reqdata->h->data;
+	token->id = g_strdup(token_info->tokenid);
+	token->size = reqdata->h->body_size;
+	token->data = g_memdup(reqdata->h->body, token->size);
+	
+	ggp_account_token_cb_call(reqdata, token);
+}
diff --git a/libpurple/protocols/gg/account.h b/libpurple/protocols/gg/account.h
new file mode 100644
--- /dev/null
+++ b/libpurple/protocols/gg/account.h
@@ -0,0 +1,21 @@
+#ifndef _GGP_ACCOUNT_H
+#define _GGP_ACCOUNT_H
+
+#include <internal.h>
+
+typedef struct
+{
+	gchar *id;
+	gpointer data;
+	size_t size;
+} ggp_account_token;
+
+/**
+ * token must be free'd with ggp_account_token_free
+ */
+typedef void (*ggp_account_token_cb)(ggp_account_token *token, gpointer user_data);
+
+void ggp_account_token_request(PurpleConnection *gc, ggp_account_token_cb callback, void *user_data);
+void ggp_account_token_free(ggp_account_token *token);
+
+#endif /* _GGP_ACCOUNT_H */
diff --git a/libpurple/protocols/gg/deprecated.c b/libpurple/protocols/gg/deprecated.c
new file mode 100644
--- /dev/null
+++ b/libpurple/protocols/gg/deprecated.c
@@ -0,0 +1,33 @@
+#include "deprecated.h"
+
+#include <libgadu.h>
+
+gboolean ggp_deprecated_setup_proxy(PurpleConnection *gc)
+{
+	PurpleProxyInfo *gpi = purple_proxy_get_setup(purple_connection_get_account(gc));
+	
+	if ((purple_proxy_info_get_type(gpi) != PURPLE_PROXY_NONE) &&
+		(purple_proxy_info_get_host(gpi) == NULL ||
+		purple_proxy_info_get_port(gpi) <= 0))
+	{
+		gg_proxy_enabled = 0;
+		purple_notify_error(NULL, NULL, _("Invalid proxy settings"),
+			_("Either the host name or port number specified for your given proxy type is invalid."));
+		return FALSE;
+	}
+	
+	if (purple_proxy_info_get_type(gpi) == PURPLE_PROXY_NONE)
+	{
+		gg_proxy_enabled = 0;
+		return TRUE;
+	}
+
+	gg_proxy_enabled = 1;
+	//TODO: memleak
+	gg_proxy_host = g_strdup(purple_proxy_info_get_host(gpi));
+	gg_proxy_port = purple_proxy_info_get_port(gpi);
+	gg_proxy_username = g_strdup(purple_proxy_info_get_username(gpi));
+	gg_proxy_password = g_strdup(purple_proxy_info_get_password(gpi));
+	
+	return TRUE;
+}
diff --git a/libpurple/protocols/gg/deprecated.h b/libpurple/protocols/gg/deprecated.h
new file mode 100644
--- /dev/null
+++ b/libpurple/protocols/gg/deprecated.h
@@ -0,0 +1,8 @@
+#ifndef _GGP_DEPRECATED_H
+#define _GGP_DEPRECATED_H
+
+#include <internal.h>
+
+gboolean ggp_deprecated_setup_proxy(PurpleConnection *gc);
+
+#endif /* _GGP_DEPRECATED_H */
diff --git a/libpurple/protocols/gg/gg.c b/libpurple/protocols/gg/gg.c
--- a/libpurple/protocols/gg/gg.c
+++ b/libpurple/protocols/gg/gg.c
@@ -45,143 +45,14 @@
 #include "buddylist.h"
 #include "utils.h"
 #include "resolver-purple.h"
+#include "account.h"
+#include "deprecated.h"
 
 /* Prototypes */
 static void ggp_set_status(PurpleAccount *account, PurpleStatus *status);
 static int ggp_to_gg_status(PurpleStatus *status, char **msg);
 
 /* ---------------------------------------------------------------------- */
-/* ----- EXTERNAL CALLBACKS --------------------------------------------- */
-/* ---------------------------------------------------------------------- */
-
-
-/* ----- HELPERS -------------------------------------------------------- */
-
-/**
- * Set up libgadu's proxy.
- *
- * @param account Account for which to set up the proxy.
- *
- * @return Zero if proxy setup is valid, otherwise -1.
- */
-static int ggp_setup_proxy(PurpleAccount *account)
-{
-	PurpleProxyInfo *gpi;
-
-	gpi = purple_proxy_get_setup(account);
-
-	if ((purple_proxy_info_get_type(gpi) != PURPLE_PROXY_NONE) &&
-	    (purple_proxy_info_get_host(gpi) == NULL ||
-	     purple_proxy_info_get_port(gpi) <= 0)) {
-
-		gg_proxy_enabled = 0;
-		purple_notify_error(NULL, NULL, _("Invalid proxy settings"),
-				  _("Either the host name or port number specified for your given proxy type is invalid."));
-		return -1;
-	} else if (purple_proxy_info_get_type(gpi) != PURPLE_PROXY_NONE) {
-		gg_proxy_enabled = 1;
-		gg_proxy_host = g_strdup(purple_proxy_info_get_host(gpi));
-		gg_proxy_port = purple_proxy_info_get_port(gpi);
-		gg_proxy_username = g_strdup(purple_proxy_info_get_username(gpi));
-		gg_proxy_password = g_strdup(purple_proxy_info_get_password(gpi));
-	} else {
-		gg_proxy_enabled = 0;
-	}
-
-	return 0;
-}
-
-static void ggp_async_token_handler(gpointer _gc, gint fd, PurpleInputCondition cond)
-{
-	PurpleConnection *gc = _gc;
-	GGPInfo *info = purple_connection_get_protocol_data(gc);
-	GGPToken *token = info->token;
-	GGPTokenCallback cb;
-
-	struct gg_token *t = NULL;
-
-	purple_debug_info("gg", "token_handler: token->req: check = %d; state = %d;\n",
-			token->req->check, token->req->state);
-
-	if (gg_token_watch_fd(token->req) == -1 || token->req->state == GG_STATE_ERROR) {
-		purple_debug_error("gg", "token error (1): %d\n", token->req->error);
-		purple_input_remove(token->inpa);
-		gg_token_free(token->req);
-		token->req = NULL;
-
-		purple_notify_error(purple_connection_get_account(gc),
-				  _("Token Error"),
-				  _("Unable to fetch the token.\n"), NULL);
-		return;
-	}
-
-	if (token->req->state != GG_STATE_DONE) {
-		purple_input_remove(token->inpa);
-		token->inpa = purple_input_add(token->req->fd,
-						   (token->req->check == 1)
-						   	? PURPLE_INPUT_WRITE
-							: PURPLE_INPUT_READ,
-						   ggp_async_token_handler, gc);
-		return;
-	}
-
-	if (!(t = token->req->data) || !token->req->body) {
-		purple_debug_error("gg", "token error (2): %d\n", token->req->error);
-		purple_input_remove(token->inpa);
-		gg_token_free(token->req);
-		token->req = NULL;



More information about the Commits mailing list