grim/purple-spasm

So many requests...

2017-05-09, Gary Kramlich
9fbc77f4a80c
So many requests...
/*
* PurpleSpasm - A Twitch Protocol Plugin
* Copyright (C) 2017 Gary Kramlich <grim@reaperworld.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <libsoup/soup.h>
#include "request.h"
#include "spasm-account.h"
#include "spasm-auth.h"
#include "spasm-const.h"
#include "spasm-user.h"
/******************************************************************************
* Helpers
*****************************************************************************/
static void
purple_spasm_access_token_input_cb(gpointer data, const gchar *access_token) {
PurpleAccount *account = NULL;
PurpleConnection *connection = NULL;
PurpleSpasmAccount *sa = PURPLE_SPASM_ACCOUNT(data);
account = purple_spasm_account_get_account(sa);
purple_account_set_remember_password(account, TRUE);
purple_spasm_account_set_access_token(sa, access_token);
purple_account_set_password(account, access_token);
connection = purple_spasm_account_get_connection(sa);
purple_connection_set_state(connection, PURPLE_CONNECTED);
purple_spasm_refresh_user(sa);
}
static void
purple_spasm_access_token_cancel_cb(gpointer data) {
PurpleSpasmAccount *sa = PURPLE_SPASM_ACCOUNT(data);
purple_connection_error(
purple_spasm_account_get_connection(sa),
"User cancelled authorization"
);
purple_spasm_account_free(sa);
}
/******************************************************************************
* API
*****************************************************************************/
void
purple_spasm_login(PurpleAccount *account) {
PurpleConnection *pc = NULL;
PurpleSpasmAccount *sa = NULL;
gchar *state = NULL, *uri = NULL;
const gchar *username = NULL;
pc = purple_account_get_connection(account);
username = purple_account_get_username(account);
sa = purple_spasm_account_new(account, pc);
purple_connection_set_protocol_data(pc, sa);
purple_connection_set_state(pc, PURPLE_CONNECTING);
state = g_strdup_printf("%s,%s", PURPLE_SPASM_PLUGIN_ID, username);
uri = g_strdup_printf(
PURPLE_SPASM_OAUTH2_URI,
PURPLE_SPASM_OAUTH2_CLIENT_ID,
PURPLE_SPASM_OAUTH2_REDIRECT_URI,
PURPLE_SPASM_OAUTH2_SCOPES,
state
);
g_free(state);
purple_connection_update_progress(pc, "Authenticating", 1, 2);
/* send off the oauth implicit request */
purple_notify_uri(pc, uri);
g_free(uri);
purple_request_input(
pc,
"Access Token",
"Enter the access token from https://pidgin.im/oauth.html which should have opened in your web browser",
NULL,
"access token",
FALSE,
TRUE,
NULL,
"OK", G_CALLBACK(purple_spasm_access_token_input_cb),
"Cancel", G_CALLBACK(purple_spasm_access_token_cancel_cb),
account,
NULL,
NULL,
sa
);
}