From 480589f0aec93b5b83d287a221c69d0388f2ec3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Senart?= Date: Mon, 14 Oct 2013 20:15:51 +0200 Subject: [PATCH] Use passwords from the accounts file This commit makes it so that if the password in an account in the accounts file is present, then use it. Otherwise ask for the password to the user. --- src/command/command.c | 30 ++++++++++++++++++++++-------- src/config/accounts.c | 12 ++++++++++++ src/config/accounts.h | 1 + src/ui/console.c | 1 + src/xmpp/connection.c | 9 ++++----- src/xmpp/xmpp.h | 3 +-- 6 files changed, 41 insertions(+), 15 deletions(-) diff --git a/src/command/command.c b/src/command/command.c index e8232405..d824ebc5 100644 --- a/src/command/command.c +++ b/src/command/command.c @@ -20,6 +20,7 @@ * */ +#include #include #include #include @@ -68,6 +69,7 @@ typedef struct cmd_t { typedef char*(*autocompleter)(char*, int*); +static void _ask_password(char * passwd); static void _update_presence(const resource_presence_t presence, const char * const show, gchar **args); static gboolean _cmd_set_boolean_preference(gchar *arg, struct cmd_help_t help, @@ -1347,13 +1349,6 @@ _cmd_connect(gchar **args, struct cmd_help_t help) char *lower = g_utf8_strdown(user, -1); char *jid; - status_bar_get_password(); - status_bar_refresh(); - char passwd[21]; - inp_block(); - inp_get_password(passwd); - inp_non_block(); - ProfAccount *account = accounts_get_account(lower); if (account != NULL) { if (account->resource != NULL) { @@ -1361,9 +1356,16 @@ _cmd_connect(gchar **args, struct cmd_help_t help) } else { jid = strdup(account->jid); } + + if (strlen(account->password) == 0) { + account->password = (gchar *) realloc(account->password, 21); + _ask_password(account->password); + } cons_show("Connecting with account %s as %s", account->name, jid); - conn_status = jabber_connect_with_account(account, passwd); + conn_status = jabber_connect_with_account(account); } else { + char passwd[21]; + _ask_password(passwd); jid = strdup(lower); cons_show("Connecting as %s", jid); conn_status = jabber_connect_with_details(jid, passwd, altdomain); @@ -3525,6 +3527,18 @@ _cmd_xa(gchar **args, struct cmd_help_t help) return TRUE; } +// helper function that asks the user for a password and saves it in passwd + +static void +_ask_password(char * passwd) { + assert(passwd != NULL); + status_bar_get_password(); + status_bar_refresh(); + inp_block(); + inp_get_password(passwd); + inp_non_block(); +} + // helper function for status change commands static void diff --git a/src/config/accounts.c b/src/config/accounts.c index 4eb4b8e3..e083fd38 100644 --- a/src/config/accounts.c +++ b/src/config/accounts.c @@ -133,6 +133,7 @@ accounts_add(const char *account_name, const char *altdomain) if (!g_key_file_has_group(accounts, account_name)) { g_key_file_set_boolean(accounts, account_name, "enabled", TRUE); g_key_file_set_string(accounts, account_name, "jid", barejid); + g_key_file_set_string(accounts, account_name, "password", ""); g_key_file_set_string(accounts, account_name, "resource", resource); if (altdomain != NULL) { g_key_file_set_string(accounts, account_name, "server", altdomain); @@ -191,6 +192,16 @@ accounts_get_account(const char * const name) _save_accounts(); } + gchar *password = g_key_file_get_string(accounts, name, "password", NULL); + if (password != NULL) { + account->password = strdup(password); + g_free(password); + } else { + account->password = strdup(""); + g_key_file_set_string(accounts, name, "password", account->password); + _save_accounts(); + } + account->enabled = g_key_file_get_boolean(accounts, name, "enabled", NULL); gchar *server = g_key_file_get_string(accounts, name, "server", NULL); @@ -288,6 +299,7 @@ accounts_free_account(ProfAccount *account) if (account != NULL) { free(account->name); free(account->jid); + free(account->password); free(account->resource); free(account->server); free(account->last_presence); diff --git a/src/config/accounts.h b/src/config/accounts.h index 4c74a523..ba282eac 100644 --- a/src/config/accounts.h +++ b/src/config/accounts.h @@ -28,6 +28,7 @@ typedef struct prof_account_t { gchar *name; gchar *jid; + gchar *password; gchar *resource; gchar *server; gchar *last_presence; diff --git a/src/ui/console.c b/src/ui/console.c index 732fc53b..faa8e2fe 100644 --- a/src/ui/console.c +++ b/src/ui/console.c @@ -793,6 +793,7 @@ cons_show_account(ProfAccount *account) cons_show ("enabled : FALSE"); } cons_show ("jid : %s", account->jid); + cons_show ("password : [redacted]"); if (account->resource != NULL) { cons_show ("resource : %s", account->resource); } diff --git a/src/xmpp/connection.c b/src/xmpp/connection.c index 40a1ea05..a1cb31c4 100644 --- a/src/xmpp/connection.c +++ b/src/xmpp/connection.c @@ -109,21 +109,20 @@ jabber_init(const int disable_tls) } jabber_conn_status_t -jabber_connect_with_account(const ProfAccount * const account, - const char * const passwd) +jabber_connect_with_account(const ProfAccount * const account) { assert(account != NULL); - assert(passwd != NULL); log_info("Connecting using account: %s", account->name); // save account name and password for reconnect saved_account.name = strdup(account->name); - saved_account.passwd = strdup(passwd); + saved_account.passwd = strdup(account->password); // connect with fulljid Jid *jidp = jid_create_from_bare_and_resource(account->jid, account->resource); - jabber_conn_status_t result = _jabber_connect(jidp->fulljid, passwd, account->server); + jabber_conn_status_t result = + _jabber_connect(jidp->fulljid, account->password, account->server); jid_destroy(jidp); return result; diff --git a/src/xmpp/xmpp.h b/src/xmpp/xmpp.h index 61f07e5e..407fe0e2 100644 --- a/src/xmpp/xmpp.h +++ b/src/xmpp/xmpp.h @@ -78,8 +78,7 @@ typedef struct disco_identity_t { void jabber_init(const int disable_tls); jabber_conn_status_t jabber_connect_with_details(const char * const jid, const char * const passwd, const char * const altdomain); -jabber_conn_status_t jabber_connect_with_account(const ProfAccount * const account, - const char * const passwd); +jabber_conn_status_t jabber_connect_with_account(const ProfAccount * const account); void jabber_disconnect(void); void jabber_shutdown(void); void jabber_process_events(void);