/pidgin/main: 5aaf2d93e7e3: Implement function to get a GProxyRe...

Mike Ruprecht cmaiku at gmail.com
Thu Jan 14 20:42:57 EST 2016


Changeset: 5aaf2d93e7e3071d748737bb9f32f152331ab3f5
Author:	 Mike Ruprecht <cmaiku at gmail.com>
Date:	 2016-01-09 21:41 -0600
Branch:	 purple-proxy-to-gio
URL: https://hg.pidgin.im/pidgin/main/rev/5aaf2d93e7e3

Description:

Implement function to get a GProxyResolver with an account's settings

Add purple_proxy_get_proxy_resolver() to return a GProxyResolver
which has been setup with proxy settings according to a particular
account's proxy settings, falling back to libpurple's global
settings, and then the system settings.

diffstat:

 libpurple/proxy.c |  82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 libpurple/proxy.h |  16 ++++++++++
 2 files changed, 98 insertions(+), 0 deletions(-)

diffs (125 lines):

diff --git a/libpurple/proxy.c b/libpurple/proxy.c
--- a/libpurple/proxy.c
+++ b/libpurple/proxy.c
@@ -2603,6 +2603,88 @@ purple_proxy_connect_cancel_with_handle(
 	}
 }
 
+GProxyResolver *
+purple_proxy_get_proxy_resolver(PurpleAccount *account)
+{
+	PurpleProxyInfo *info = purple_proxy_get_setup(account);
+	const gchar *protocol;
+	const gchar *username;
+	const gchar *password;
+	gchar *auth;
+	gchar *proxy;
+	GProxyResolver *resolver;
+
+	if (purple_proxy_info_get_proxy_type(info) == PURPLE_PROXY_NONE) {
+		/* Return the default proxy which, if it doesn't support any
+		 * further system proxy settings than purple_proxy_get_setup()
+		 * detects, will end up as direct connections as intended.
+		 */
+		return g_object_ref(g_proxy_resolver_get_default());
+	}
+
+	switch (purple_proxy_info_get_proxy_type(info))
+	{
+		/* PURPLE_PROXY_NONE already handled above */
+
+		case PURPLE_PROXY_USE_ENVVAR:
+			/* Intentional passthrough */
+		case PURPLE_PROXY_HTTP:
+			protocol = "http";
+			break;
+		case PURPLE_PROXY_SOCKS4:
+			protocol = "socks4";
+			break;
+		case PURPLE_PROXY_SOCKS5:
+			/* Intentional passthrough */
+		case PURPLE_PROXY_TOR:
+			protocol = "socks5";
+			break;
+
+		default:
+			purple_debug_error("proxy",
+					"Invalid Proxy type (%d) specified.\n",
+					purple_proxy_info_get_proxy_type(info));
+			return NULL;
+	}
+
+
+	if (purple_proxy_info_get_host(info) == NULL ||
+			purple_proxy_info_get_port(info) <= 0) {
+		purple_notify_error(NULL, NULL,
+				_("Invalid proxy settings"),
+				_("Either the host name or port number "
+				"specified for your given proxy type is "
+				"invalid."),
+				purple_request_cpar_from_account( account));
+		return NULL;
+	}
+
+	/* Everything checks out. Create and return the GProxyResolver */
+
+	username = purple_proxy_info_get_username(info);
+	password = purple_proxy_info_get_password(info);
+
+	/* Username and password are optional */
+	if (username != NULL && password != NULL) {
+		auth = g_strdup_printf("%s:%s@", username, password);
+	} else if (username != NULL) {
+		auth = g_strdup_printf("%s@", username);
+	} else {
+		auth = NULL;
+	}
+
+	proxy = g_strdup_printf("%s://%s%s:%i", protocol,
+			auth != NULL ? auth : "",
+			purple_proxy_info_get_host(info),
+			purple_proxy_info_get_port(info));
+	g_free(auth);
+
+	resolver = g_simple_proxy_resolver_new(proxy, NULL);
+	g_free(proxy);
+
+	return resolver;
+}
+
 static void
 proxy_pref_cb(const char *name, PurplePrefType type,
 			  gconstpointer value, gpointer data)
diff --git a/libpurple/proxy.h b/libpurple/proxy.h
--- a/libpurple/proxy.h
+++ b/libpurple/proxy.h
@@ -29,6 +29,7 @@
  */
 
 #include <glib.h>
+#include <gio/gio.h>
 #include "eventloop.h"
 
 /**
@@ -357,6 +358,21 @@ void purple_proxy_connect_cancel(PurpleP
  */
 void purple_proxy_connect_cancel_with_handle(void *handle);
 
+/**
+ * purple_proxy_get_proxy_resolver:
+ * @account: The account for which to get the proxy resolver.
+ *
+ * Returns a #GProxyResolver capable of resolving which proxy
+ * to use for this account, if any. This object can be given to a
+ * #GSocketClient for automatic proxy handling or can be used
+ * directly if desired.
+ *
+ * Returns: (transfer full): NULL if there was an error with the
+ *         account's (or system) proxy settings, or a reference to
+ *         a #GProxyResolver on success.
+ */
+GProxyResolver *purple_proxy_get_proxy_resolver(PurpleAccount *account);
+
 G_END_DECLS
 
 #endif /* _PURPLE_PROXY_H_ */



More information about the Commits mailing list