diff --git a/src/command/commands.c b/src/command/commands.c index 9ca70ef0..2d9e1dfc 100644 --- a/src/command/commands.c +++ b/src/command/commands.c @@ -203,17 +203,42 @@ cmd_connect(gchar **args, struct cmd_help_t help) char *jid; g_free(def); + // connect with account ProfAccount *account = accounts_get_account(lower); if (account) { - jid = account_create_full_jid(account); - gboolean res = client_connect_account(account, &conn_status); - if (!res) { - g_free(lower); - return TRUE; + // use password if set + if (account->password) { + conn_status = client_connect_account(account); + + // use eval_password if set + } else if (account->eval_password) { + gboolean res = account_eval_password(account); + if (res) { + conn_status = client_connect_account(account); + free(account->password); + account->password = NULL; + } else { + cons_show("Error evaluating password, see logs for details."); + g_free(lower); + return TRUE; + } + + // no account password setting, prompt + } else { + account->password = ui_ask_password(); + conn_status = client_connect_account(account); + free(account->password); + account->password = NULL; } + + jid = account_create_full_jid(account); + + // connect with JID } else { jid = strdup(lower); - conn_status = client_connect_jid(jid, altdomain, port); + char *passwd = ui_ask_password(); + conn_status = client_connect_jid(jid, passwd, altdomain, port); + free(passwd); } if (conn_status == JABBER_DISCONNECTED) { diff --git a/src/config/account.c b/src/config/account.c index 3896286b..749eb885 100644 --- a/src/config/account.c +++ b/src/config/account.c @@ -34,12 +34,14 @@ #include #include +#include #include #include "jid.h" #include "config/account.h" #include "common.h" +#include "log.h" ProfAccount* account_new(const gchar * const name, const gchar * const jid, @@ -155,6 +157,44 @@ account_create_full_jid(ProfAccount *account) } } +gboolean +account_eval_password(ProfAccount *account) +{ + assert(account != NULL); + assert(account->eval_password != NULL); + + // Evaluate as shell command to retrieve password + GString *cmd = g_string_new(""); + g_string_append_printf(cmd, "%s 2>/dev/null", account->eval_password); + + FILE *stream = popen(cmd->str, "r"); + g_string_free(cmd, TRUE); + if (stream) { + // Limit to READ_BUF_SIZE bytes to prevent overflows in the case of a poorly chosen command + account->password = g_malloc(READ_BUF_SIZE); + if (!account->password) { + log_error("Failed to allocate enough memory to read eval_password output"); + return FALSE; + } + account->password = fgets(account->password, READ_BUF_SIZE, stream); + pclose(stream); + if (!account->password) { + log_error("No result from eval_password."); + return FALSE; + } + + // strip trailing newline + if (g_str_has_suffix(account->password, "\n")) { + account->password[strlen(account->password)-1] = '\0'; + } + } else { + log_error("popen failed when running eval_password."); + return FALSE; + } + + return TRUE; +} + void account_free(ProfAccount *account) { diff --git a/src/config/account.h b/src/config/account.h index c237a19e..218f8ce7 100644 --- a/src/config/account.h +++ b/src/config/account.h @@ -69,9 +69,8 @@ ProfAccount* account_new(const gchar * const name, const gchar * const jid, const gchar * const muc_service, const gchar * const muc_nick, const gchar * const otr_policy, GList *otr_manual, GList *otr_opportunistic, GList *otr_always); - char* account_create_full_jid(ProfAccount *account); - +gboolean account_eval_password(ProfAccount *account); void account_free(ProfAccount *account); #endif diff --git a/src/event/client_events.c b/src/event/client_events.c index 08c422c4..2a1d349a 100644 --- a/src/event/client_events.c +++ b/src/event/client_events.c @@ -43,65 +43,20 @@ #endif jabber_conn_status_t -client_connect_jid(const char * const jid, const char * const altdomain, const int port) +client_connect_jid(const char * const jid, const char * const passwd, const char * const altdomain, const int port) { cons_show("Connecting as %s", jid); - char *passwd = ui_ask_password(); - jabber_conn_status_t conn_status = jabber_connect_with_details(jid, passwd, altdomain, port); - free(passwd); - - return conn_status; + return jabber_connect_with_details(jid, passwd, altdomain, port); } -gboolean -client_connect_account(ProfAccount *account, jabber_conn_status_t *conn_status) +jabber_conn_status_t +client_connect_account(ProfAccount *account) { - if (account->eval_password) { - - // Evaluate as shell command to retrieve password - GString *cmd = g_string_new(""); - g_string_append_printf(cmd, "%s 2>/dev/null", account->eval_password); - - FILE *stream = popen(cmd->str, "r"); - g_string_free(cmd, TRUE); - if (stream) { - // Limit to READ_BUF_SIZE bytes to prevent overflows in the case of a poorly chosen command - account->password = g_malloc(READ_BUF_SIZE); - if (!account->password) { - log_error("Failed to allocate enough memory to read eval_password output"); - cons_show("Error evaluating password, see logs for details."); - return FALSE; - } - account->password = fgets(account->password, READ_BUF_SIZE, stream); - pclose(stream); - if (!account->password) { - log_error("No result from eval_password."); - cons_show("Error evaluating password, see logs for details."); - return FALSE; - } - - // strip trailing newline - if (g_str_has_suffix(account->password, "\n")) { - account->password[strlen(account->password)-1] = '\0'; - } - } else { - log_error("popen failed when running eval_password."); - cons_show("Error evaluating password, see logs for details."); - return FALSE; - } - - } else if (!account->password) { - account->password = ui_ask_password(); - } - char *jid = account_create_full_jid(account); cons_show("Connecting with account %s as %s", account->name, jid); free(jid); - *conn_status = jabber_connect_with_account(account); - account_free(account); - - return TRUE; + return jabber_connect_with_account(account); } void diff --git a/src/event/client_events.h b/src/event/client_events.h index 156c66a3..efa535c9 100644 --- a/src/event/client_events.h +++ b/src/event/client_events.h @@ -35,8 +35,8 @@ #ifndef CLIENT_EVENTS_H #define CLIENT_EVENTS_H -jabber_conn_status_t client_connect_jid(const char * const jid, const char * const altdomain, const int port); -gboolean client_connect_account(ProfAccount *account, jabber_conn_status_t *conn_status); +jabber_conn_status_t client_connect_jid(const char * const jid, const char * const passwd, const char * const altdomain, const int port); +jabber_conn_status_t client_connect_account(ProfAccount *account); void client_send_msg(const char * const barejid, const char * const msg); void client_send_muc_msg(const char * const roomjid, const char * const msg);