summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Ruprecht <cmaiku@gmail.com>2016-01-09 21:41:51 -0600
committerMike Ruprecht <cmaiku@gmail.com>2016-01-09 21:41:51 -0600
commit890ae406587670e8dc5401edf3a8a061bfaf930d (patch)
tree418897b3eb49fa57f4fbfb941ead47c5bfa5168e
parent11270366a8208472a5a03b9d03241ed3effd4767 (diff)
downloadpidgin-890ae406587670e8dc5401edf3a8a061bfaf930d.tar.gz
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.
-rw-r--r--libpurple/proxy.c82
-rw-r--r--libpurple/proxy.h16
2 files changed, 98 insertions, 0 deletions
diff --git a/libpurple/proxy.c b/libpurple/proxy.c
index 1ffc3e5b4c..d12f4cdbb8 100644
--- a/libpurple/proxy.c
+++ b/libpurple/proxy.c
@@ -2603,6 +2603,88 @@ purple_proxy_connect_cancel_with_handle(void *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
index 13c95f388a..8ccefbe216 100644
--- 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(PurpleProxyConnectData *connect_data);
*/
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_ */