grim/purple-spasm

99c8b92f6aaf
Basic chat auth working, need to implement ping's yet
/*
* 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 "debug.h"
#include "request.h"
#include "spasm-account.h"
#include "spasm-auth.h"
#include "spasm-chat.h"
#include "spasm-const.h"
#include "spasm-user.h"
/******************************************************************************
* Prototypes
*****************************************************************************/
static void _purple_spasm_oauth_login(PurpleSpasmAccount *sa);
/******************************************************************************
* Helpers
*****************************************************************************/
static void
_purple_spasm_login_test_cb(PurpleSpasmAccount *sa, JsonParser *parser,
GError *error, gpointer data)
{
PurpleAccount *account = NULL;
PurpleConnection *connection = NULL;
if(error != NULL) {
purple_debug_info("spasm", "oauth token invalid, re-authenticating : %s\n", error->message);
_purple_spasm_oauth_login(sa);
return;
}
account = purple_spasm_account_get_account(sa);
connection = purple_account_get_connection(account);
error = purple_spasm_account_update_profile(sa, parser);
if(error != NULL) {
gchar *err_msg = NULL;
err_msg = g_strdup_printf(
"failed to update profile : %s",
error->message
);
g_error_free(error);
purple_connection_error(connection, err_msg);
g_free(err_msg);
return;
}
purple_spasm_chat_login(sa);
}
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_update_progress(connection, "Verifying", 2, 3);
purple_spasm_get_user(sa, _purple_spasm_login_test_cb, NULL);
}
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);
}
static void
_purple_spasm_oauth_login(PurpleSpasmAccount *sa) {
PurpleAccount *account = NULL;
PurpleConnection *connection = NULL;
gchar *state = NULL, *uri = NULL;
const gchar *username = NULL;
account = purple_spasm_account_get_account(sa);
connection = purple_account_get_connection(account);
purple_connection_update_progress(
connection,
"Authenticating",
1,
3
);
username = purple_account_get_username(account);
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);
/* send off the oauth implicit request */
purple_notify_uri(connection, uri);
g_free(uri);
purple_request_input(
connection,
"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
);
}
/******************************************************************************
* API
*****************************************************************************/
void
purple_spasm_login(PurpleAccount *account) {
PurpleConnection *pc = NULL;
PurpleSpasmAccount *sa = NULL;
const gchar *password = NULL;
pc = purple_account_get_connection(account);
sa = purple_spasm_account_new(account, pc);
purple_connection_set_protocol_data(pc, sa);
purple_connection_set_state(pc, PURPLE_CONNECTING);
/* try to load the password */
password = purple_account_get_password(account);
if(password != NULL) {
purple_spasm_account_set_access_token(sa, password);
purple_spasm_get_user(sa, _purple_spasm_login_test_cb, NULL);
} else {
_purple_spasm_oauth_login(sa);
}
}