aboutsummaryrefslogtreecommitdiffstats
path: root/matrix-connection.c
diff options
context:
space:
mode:
authorDr. David Alan Gilbert <dave@treblig.org>2018-04-06 14:39:15 +0100
committerDr. David Alan Gilbert <dave@treblig.org>2019-12-28 18:41:04 +0000
commit64c1c2f5a783380938c8aade6bda5b2303bdcfab (patch)
tree36b405a39dc50c60924f17d200aa998e56434a27 /matrix-connection.c
parentad1b10efe1d07225abe5f0e56f6903c3f4144450 (diff)
downloadpurple-matrix-64c1c2f5a783380938c8aade6bda5b2303bdcfab.tar.gz
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 <dave@treblig.org>
Diffstat (limited to 'matrix-connection.c')
-rw-r--r--matrix-connection.c79
1 files changed, 75 insertions, 4 deletions
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 <debug.h>
+#include <libpurple/request.h>
+#include <libpurple/core.h>
/* 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);
}