From 64c1c2f5a783380938c8aade6bda5b2303bdcfab Mon Sep 17 00:00:00 2001 From: "Dr. David Alan Gilbert" Date: Fri, 6 Apr 2018 14:39:15 +0100 Subject: connection: Handle no password explicitly By default purple asks for a password if there's none stored, but when we use an access token we'll want to have no password prompt at all. To do this we flag OPT_PROTO_PASSWORD_OPTIONAL and then have to handle the case with no password manually. Mostly cribbed from jabber/auth_cyrus.c Signed-off-by: Dr. David Alan Gilbert --- matrix-connection.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 75 insertions(+), 4 deletions(-) (limited to 'matrix-connection.c') diff --git a/matrix-connection.c b/matrix-connection.c index 5c86de2..0dd543b 100644 --- a/matrix-connection.c +++ b/matrix-connection.c @@ -26,6 +26,8 @@ /* libpurple */ #include +#include +#include /* libmatrix */ #include "libmatrix.h" @@ -234,6 +236,78 @@ static void _login_completed(MatrixConnectionData *conn, _start_sync(conn); } +/* + * Callback from _password_login when the user enters a password. + */ +static void +_password_received(PurpleConnection *gc, PurpleRequestFields *fields) +{ + PurpleAccount *acct; + const char *entry; + MatrixConnectionData *conn; + gboolean remember; + + /* The password prompt dialog doesn't get disposed if the account disconnects */ + if (!PURPLE_CONNECTION_IS_VALID(gc)) + return; + + acct = purple_connection_get_account(gc); + conn = purple_connection_get_protocol_data(gc); + + entry = purple_request_fields_get_string(fields, "password"); + remember = purple_request_fields_get_bool(fields, "remember"); + + if (!entry || !*entry) + { + purple_notify_error(acct, NULL, _("Password is required to sign on."), NULL); + return; + } + + if (remember) + purple_account_set_remember_password(acct, TRUE); + + purple_account_set_password(acct, entry); + + matrix_api_password_login(conn, acct->username, + entry, + purple_account_get_string(acct, "device_id", NULL), + _login_completed, conn); +} + + +static void +_password_cancel(PurpleConnection *gc, PurpleRequestFields *fields) +{ + PurpleAccount *account; + + /* The password prompt dialog doesn't get disposed if the account disconnects */ + if (!PURPLE_CONNECTION_IS_VALID(gc)) + return; + + account = purple_connection_get_account(gc); + + /* Disable the account as the user has cancelled connecting */ + purple_account_set_enabled(account, purple_core_get_ui(), FALSE); +} + +/* + * Start a passworded login. + */ +static void _password_login(MatrixConnectionData *conn, PurpleAccount *acct) +{ + const char *password = purple_account_get_password(acct); + + if (password) { + matrix_api_password_login(conn, acct->username, + password, + purple_account_get_string(acct, "device_id", NULL), + _login_completed, conn); + } else { + purple_account_request_password(acct,G_CALLBACK( _password_received), + G_CALLBACK(_password_cancel), conn->pc); + } +} + void matrix_connection_start_login(PurpleConnection *pc) { @@ -251,10 +325,7 @@ void matrix_connection_start_login(PurpleConnection *pc) purple_connection_set_state(pc, PURPLE_CONNECTING); purple_connection_update_progress(pc, _("Logging in"), 0, 3); - matrix_api_password_login(conn, acct->username, - purple_account_get_password(acct), - purple_account_get_string(acct, "device_id", NULL), - _login_completed, conn); + _password_login(conn, acct); } -- cgit