pidgin/pidgin

Re-arrange GG oauth-purple code to avoid prototypes.

2019-10-11, Elliott Sales de Andrade
d76cbf47c896
Re-arrange GG oauth-purple code to avoid prototypes.
--- a/libpurple/protocols/gg/oauth/oauth-purple.c Fri Oct 11 20:23:42 2019 -0400
+++ b/libpurple/protocols/gg/oauth/oauth-purple.c Fri Oct 11 21:47:21 2019 -0400
@@ -49,17 +49,6 @@
gchar *sign_method, *sign_url;
} ggp_oauth_data;
-static void ggp_oauth_data_free(ggp_oauth_data *data);
-
-static void ggp_oauth_request_token_got(PurpleHttpConnection *http_conn,
- PurpleHttpResponse *response, gpointer user_data);
-
-static void ggp_oauth_authorization_done(PurpleHttpConnection *http_conn,
- PurpleHttpResponse *response, gpointer user_data);
-
-static void ggp_oauth_access_token_got(PurpleHttpConnection *http_conn,
- PurpleHttpResponse *response, gpointer user_data);
-
static void ggp_oauth_data_free(ggp_oauth_data *data)
{
g_free(data->token);
@@ -69,34 +58,97 @@
g_free(data);
}
-void ggp_oauth_request(PurpleConnection *gc, ggp_oauth_request_cb callback,
- gpointer user_data, const gchar *sign_method, const gchar *sign_url)
+static void
+ggp_oauth_access_token_got(PurpleHttpConnection *http_conn,
+ PurpleHttpResponse *response, gpointer user_data)
{
- PurpleAccount *account = purple_connection_get_account(gc);
+ ggp_oauth_data *data = user_data;
+ gchar *token, *token_secret;
+ PurpleXmlNode *xml;
+ const gchar *xml_raw;
+ gboolean succ = TRUE;
+
+ xml_raw = purple_http_response_get_data(response, NULL);
+ xml = purple_xmlnode_from_str(xml_raw, -1);
+ if (xml == NULL) {
+ purple_debug_error("gg", "ggp_oauth_access_token_got: invalid xml");
+ ggp_oauth_data_free(data);
+ return;
+ }
+
+ succ &= ggp_xml_get_string(xml, "oauth_token", &token);
+ succ &= ggp_xml_get_string(xml, "oauth_token_secret", &token_secret);
+ purple_xmlnode_free(xml);
+ if (!succ || strlen(token) < 10) {
+ purple_debug_error("gg", "ggp_oauth_access_token_got: invalid xml - "
+ "token is not present");
+ ggp_oauth_data_free(data);
+ return;
+ }
+
+ if (data->sign_url) {
+ PurpleAccount *account;
+ gchar *auth;
+
+ purple_debug_misc("gg", "ggp_oauth_access_token_got: got access token, "
+ "returning signed url");
+
+ account = purple_connection_get_account(data->gc);
+ auth = gg_oauth_generate_header(
+ data->sign_method, data->sign_url,
+ purple_account_get_username(account),
+ purple_connection_get_password(data->gc), token, token_secret);
+ data->callback(data->gc, auth, data->user_data);
+ } else {
+ purple_debug_misc(
+ "gg",
+ "ggp_oauth_access_token_got: got access token, returning it");
+ data->callback(data->gc, token, data->user_data);
+ }
+
+ g_free(token);
+ g_free(token_secret);
+ ggp_oauth_data_free(data);
+}
+
+static void
+ggp_oauth_authorization_done(PurpleHttpConnection *http_conn,
+ PurpleHttpResponse *response, gpointer user_data)
+{
+ ggp_oauth_data *data = user_data;
+ PurpleAccount *account;
PurpleHttpRequest *req;
char *auth;
const char *method = "POST";
- const char *url = "http://api.gadu-gadu.pl/request_token";
- ggp_oauth_data *data;
+ const char *url = "http://api.gadu-gadu.pl/access_token";
+ int response_code;
+
+ PURPLE_ASSERT_CONNECTION_IS_VALID(data->gc);
+
+ account = purple_connection_get_account(data->gc);
- purple_debug_misc("gg", "ggp_oauth_request: requesting token...\n");
+ response_code = purple_http_response_get_code(response);
+ if (response_code != 302) {
+ purple_debug_error("gg",
+ "ggp_oauth_authorization_done: failed (code = %d)",
+ response_code);
+ ggp_oauth_data_free(data);
+ return;
+ }
+
+ purple_debug_misc("gg", "ggp_oauth_authorization_done: authorization done, "
+ "requesting access token...");
auth = gg_oauth_generate_header(method, url,
- purple_account_get_username(account),
- purple_connection_get_password(gc), NULL, NULL);
-
- data = g_new0(ggp_oauth_data, 1);
- data->gc = gc;
- data->callback = callback;
- data->user_data = user_data;
- data->sign_method = g_strdup(sign_method);
- data->sign_url = g_strdup(sign_url);
+ purple_account_get_username(account),
+ purple_connection_get_password(data->gc),
+ data->token, data->token_secret);
req = purple_http_request_new(url);
purple_http_request_set_max_len(req, GGP_OAUTH_RESPONSE_MAX);
purple_http_request_set_method(req, method);
purple_http_request_header_set(req, "Authorization", auth);
- purple_http_request(gc, req, ggp_oauth_request_token_got, data);
+ purple_http_request(data->gc, req, ggp_oauth_access_token_got, data);
purple_http_request_unref(req);
free(auth);
@@ -166,97 +218,37 @@
g_free(request_data);
}
-static void ggp_oauth_authorization_done(PurpleHttpConnection *http_conn,
- PurpleHttpResponse *response, gpointer user_data)
+void
+ggp_oauth_request(PurpleConnection *gc, ggp_oauth_request_cb callback,
+ gpointer user_data, const gchar *sign_method,
+ const gchar *sign_url)
{
- ggp_oauth_data *data = user_data;
- PurpleAccount *account;
+ PurpleAccount *account = purple_connection_get_account(gc);
PurpleHttpRequest *req;
char *auth;
const char *method = "POST";
- const char *url = "http://api.gadu-gadu.pl/access_token";
- int response_code;
+ const char *url = "http://api.gadu-gadu.pl/request_token";
+ ggp_oauth_data *data;
- PURPLE_ASSERT_CONNECTION_IS_VALID(data->gc);
-
- account = purple_connection_get_account(data->gc);
+ purple_debug_misc("gg", "ggp_oauth_request: requesting token...\n");
- response_code = purple_http_response_get_code(response);
- if (response_code != 302) {
- purple_debug_error("gg", "ggp_oauth_authorization_done: "
- "failed (code = %d)", response_code);
- ggp_oauth_data_free(data);
- return;
- }
+ auth = gg_oauth_generate_header(
+ method, url, purple_account_get_username(account),
+ purple_connection_get_password(gc), NULL, NULL);
- purple_debug_misc("gg", "ggp_oauth_authorization_done: "
- "authorization done, requesting access token...\n");
-
- auth = gg_oauth_generate_header(method, url,
- purple_account_get_username(account),
- purple_connection_get_password(data->gc),
- data->token, data->token_secret);
+ data = g_new0(ggp_oauth_data, 1);
+ data->gc = gc;
+ data->callback = callback;
+ data->user_data = user_data;
+ data->sign_method = g_strdup(sign_method);
+ data->sign_url = g_strdup(sign_url);
req = purple_http_request_new(url);
purple_http_request_set_max_len(req, GGP_OAUTH_RESPONSE_MAX);
purple_http_request_set_method(req, method);
purple_http_request_header_set(req, "Authorization", auth);
- purple_http_request(data->gc, req, ggp_oauth_access_token_got, data);
+ purple_http_request(gc, req, ggp_oauth_request_token_got, data);
purple_http_request_unref(req);
free(auth);
}
-
-static void ggp_oauth_access_token_got(PurpleHttpConnection *http_conn,
- PurpleHttpResponse *response, gpointer user_data)
-{
- ggp_oauth_data *data = user_data;
- gchar *token, *token_secret;
- PurpleXmlNode *xml;
- const gchar *xml_raw;
- gboolean succ = TRUE;
-
- xml_raw = purple_http_response_get_data(response, NULL);
- xml = purple_xmlnode_from_str(xml_raw, -1);
- if (xml == NULL) {
- purple_debug_error("gg", "ggp_oauth_access_token_got: "
- "invalid xml\n");
- ggp_oauth_data_free(data);
- return;
- }
-
- succ &= ggp_xml_get_string(xml, "oauth_token", &token);
- succ &= ggp_xml_get_string(xml, "oauth_token_secret",
- &token_secret);
- purple_xmlnode_free(xml);
- if (!succ || strlen(token) < 10) {
- purple_debug_error("gg", "ggp_oauth_access_token_got: "
- "invalid xml - token is not present\n");
- ggp_oauth_data_free(data);
- return;
- }
-
- if (data->sign_url) {
- PurpleAccount *account;
- gchar *auth;
-
- purple_debug_misc("gg", "ggp_oauth_access_token_got: "
- "got access token, returning signed url\n");
-
- account = purple_connection_get_account(data->gc);
- auth = gg_oauth_generate_header(
- data->sign_method, data->sign_url,
- purple_account_get_username(account),
- purple_connection_get_password(data->gc),
- token, token_secret);
- data->callback(data->gc, auth, data->user_data);
- } else {
- purple_debug_misc("gg", "ggp_oauth_access_token_got: "
- "got access token, returning it\n");
- data->callback(data->gc, token, data->user_data);
- }
-
- g_free(token);
- g_free(token_secret);
- ggp_oauth_data_free(data);
-}