From 1c47b57e195609511c3acd52a6d5e46e8288edca Mon Sep 17 00:00:00 2001 From: James Booth Date: Tue, 21 Apr 2015 23:45:03 +0100 Subject: Tidy execute alias code --- src/command/commands.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'src/command') diff --git a/src/command/commands.c b/src/command/commands.c index 713cd0ef..529a1019 100644 --- a/src/command/commands.c +++ b/src/command/commands.c @@ -137,18 +137,18 @@ cmd_execute_alias(const char * const inp, gboolean *ran) if (inp[0] != '/') { ran = FALSE; return TRUE; - } else { - char *alias = strdup(inp+1); - char *value = prefs_get_alias(alias); - free(alias); - if (value != NULL) { - *ran = TRUE; - return cmd_process_input(value); - } else { - *ran = FALSE; - return TRUE; - } } + + char *alias = strdup(inp+1); + char *value = prefs_get_alias(alias); + free(alias); + if (value) { + *ran = TRUE; + return cmd_process_input(value); + } + + *ran = FALSE; + return TRUE; } gboolean -- cgit 1.4.1-2-gfad0 From ba286c54ef69f9adde8bd8aa4d54857dee5414cb Mon Sep 17 00:00:00 2001 From: James Booth Date: Wed, 22 Apr 2015 00:29:37 +0100 Subject: Moved login events to client events module --- src/command/commands.c | 154 +++++++++++++++++----------------------------- src/event/client_events.c | 62 +++++++++++++++++++ src/event/client_events.h | 3 + 3 files changed, 121 insertions(+), 98 deletions(-) (limited to 'src/command') diff --git a/src/command/commands.c b/src/command/commands.c index 529a1019..9ca70ef0 100644 --- a/src/command/commands.c +++ b/src/command/commands.c @@ -154,120 +154,78 @@ cmd_execute_alias(const char * const inp, gboolean *ran) gboolean cmd_connect(gchar **args, struct cmd_help_t help) { - gboolean result = FALSE; - jabber_conn_status_t conn_status = jabber_get_connection_status(); - if ((conn_status != JABBER_DISCONNECTED) && (conn_status != JABBER_STARTED)) { cons_show("You are either connected already, or a login is in process."); - result = TRUE; - } else { - gchar *opt_keys[] = { "server", "port", NULL }; - gboolean parsed; + return TRUE; + } - GHashTable *options = parse_options(&args[args[0] ? 1 : 0], opt_keys, &parsed); - if (!parsed) { - cons_show("Usage: %s", help.usage); - cons_show(""); - return TRUE; - } + gchar *opt_keys[] = { "server", "port", NULL }; + gboolean parsed; - char *altdomain = g_hash_table_lookup(options, "server"); + GHashTable *options = parse_options(&args[args[0] ? 1 : 0], opt_keys, &parsed); + if (!parsed) { + cons_show("Usage: %s", help.usage); + cons_show(""); + return TRUE; + } - int port = 0; - if (g_hash_table_contains(options, "port")) { - char *port_str = g_hash_table_lookup(options, "port"); - char *err_msg = NULL; - gboolean res = strtoi_range(port_str, &port, 1, 65535, &err_msg); - if (!res) { - cons_show(err_msg); - cons_show(""); - free(err_msg); - port = 0; - return TRUE; - } - } + char *altdomain = g_hash_table_lookup(options, "server"); - char *user = args[0]; - char *def = prefs_get_string(PREF_DEFAULT_ACCOUNT); - if(!user){ - if(def){ - user = def; - cons_show("Using default account %s.", user); - } else { - cons_show("No default account."); - g_free(def); - return TRUE; - } + int port = 0; + if (g_hash_table_contains(options, "port")) { + char *port_str = g_hash_table_lookup(options, "port"); + char *err_msg = NULL; + gboolean res = strtoi_range(port_str, &port, 1, 65535, &err_msg); + if (!res) { + cons_show(err_msg); + cons_show(""); + free(err_msg); + port = 0; + return TRUE; } + } - char *lower = g_utf8_strdown(user, -1); - char *jid; - g_free(def); - def = NULL; - - ProfAccount *account = accounts_get_account(lower); - if (account != NULL) { - jid = account_create_full_jid(account); - if(account->eval_password){ - // Evaluate as shell command to retrieve password - GString *cmd = g_string_append(g_string_new(account->eval_password), " 2>/dev/null"); - 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 TRUE; - } - 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 TRUE; - } - // 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 TRUE; - } - } else if (!account->password) { - account->password = ui_ask_password(); - } - cons_show("Connecting with account %s as %s", account->name, jid); - if(g_hash_table_contains(options, "port") || g_hash_table_contains(options, "server")) - cons_show("Ignoring extra connect options. Please set them with /account set"); - conn_status = jabber_connect_with_account(account); - account_free(account); + char *user = args[0]; + char *def = prefs_get_string(PREF_DEFAULT_ACCOUNT); + if (!user) { + if (def) { + user = def; + cons_show("Using default account %s.", user); } else { - char *passwd = ui_ask_password(); - jid = strdup(lower); - cons_show("Connecting as %s", jid); - conn_status = jabber_connect_with_details(jid, passwd, altdomain, port); - free(passwd); - } - g_free(lower); - - if (conn_status == JABBER_DISCONNECTED) { - cons_show_error("Connection attempt for %s failed.", jid); - log_info("Connection attempt for %s failed", jid); + cons_show("No default account."); + g_free(def); + return TRUE; } + } - options_destroy(options); + char *lower = g_utf8_strdown(user, -1); + char *jid; + g_free(def); - free(jid); + 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; + } + } else { + jid = strdup(lower); + conn_status = client_connect_jid(jid, altdomain, port); + } - result = TRUE; + if (conn_status == JABBER_DISCONNECTED) { + cons_show_error("Connection attempt for %s failed.", jid); + log_info("Connection attempt for %s failed", jid); } - return result; + options_destroy(options); + g_free(lower); + free(jid); + + return TRUE; } gboolean diff --git a/src/event/client_events.c b/src/event/client_events.c index c580f700..08c422c4 100644 --- a/src/event/client_events.c +++ b/src/event/client_events.c @@ -42,6 +42,68 @@ #include "otr/otr.h" #endif +jabber_conn_status_t +client_connect_jid(const char * const jid, 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; +} + +gboolean +client_connect_account(ProfAccount *account, jabber_conn_status_t *conn_status) +{ + 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; +} + void client_send_msg(const char * const barejid, const char * const msg) { diff --git a/src/event/client_events.h b/src/event/client_events.h index 41764e73..156c66a3 100644 --- a/src/event/client_events.h +++ b/src/event/client_events.h @@ -35,6 +35,9 @@ #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); + 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); void client_send_priv_msg(const char * const fulljid, const char * const msg); -- cgit 1.4.1-2-gfad0