1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

Moved login events to client events module

This commit is contained in:
James Booth 2015-04-22 00:29:37 +01:00
parent 1c47b57e19
commit ba286c54ef
3 changed files with 132 additions and 109 deletions

View File

@ -154,14 +154,12 @@ cmd_execute_alias(const char * const inp, gboolean *ran)
gboolean gboolean
cmd_connect(gchar **args, struct cmd_help_t help) cmd_connect(gchar **args, struct cmd_help_t help)
{ {
gboolean result = FALSE;
jabber_conn_status_t conn_status = jabber_get_connection_status(); jabber_conn_status_t conn_status = jabber_get_connection_status();
if ((conn_status != JABBER_DISCONNECTED) && (conn_status != JABBER_STARTED)) { if ((conn_status != JABBER_DISCONNECTED) && (conn_status != JABBER_STARTED)) {
cons_show("You are either connected already, or a login is in process."); cons_show("You are either connected already, or a login is in process.");
result = TRUE; return TRUE;
} else { }
gchar *opt_keys[] = { "server", "port", NULL }; gchar *opt_keys[] = { "server", "port", NULL };
gboolean parsed; gboolean parsed;
@ -190,8 +188,8 @@ cmd_connect(gchar **args, struct cmd_help_t help)
char *user = args[0]; char *user = args[0];
char *def = prefs_get_string(PREF_DEFAULT_ACCOUNT); char *def = prefs_get_string(PREF_DEFAULT_ACCOUNT);
if(!user){ if (!user) {
if(def){ if (def) {
user = def; user = def;
cons_show("Using default account %s.", user); cons_show("Using default account %s.", user);
} else { } else {
@ -204,56 +202,19 @@ cmd_connect(gchar **args, struct cmd_help_t help)
char *lower = g_utf8_strdown(user, -1); char *lower = g_utf8_strdown(user, -1);
char *jid; char *jid;
g_free(def); g_free(def);
def = NULL;
ProfAccount *account = accounts_get_account(lower); ProfAccount *account = accounts_get_account(lower);
if (account != NULL) { if (account) {
jid = account_create_full_jid(account); jid = account_create_full_jid(account);
if(account->eval_password){ gboolean res = client_connect_account(account, &conn_status);
// Evaluate as shell command to retrieve password if (!res) {
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);
} 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); g_free(lower);
return TRUE;
}
} else {
jid = strdup(lower);
conn_status = client_connect_jid(jid, altdomain, port);
}
if (conn_status == JABBER_DISCONNECTED) { if (conn_status == JABBER_DISCONNECTED) {
cons_show_error("Connection attempt for %s failed.", jid); cons_show_error("Connection attempt for %s failed.", jid);
@ -261,13 +222,10 @@ cmd_connect(gchar **args, struct cmd_help_t help)
} }
options_destroy(options); options_destroy(options);
g_free(lower);
free(jid); free(jid);
result = TRUE; return TRUE;
}
return result;
} }
gboolean gboolean

View File

@ -42,6 +42,68 @@
#include "otr/otr.h" #include "otr/otr.h"
#endif #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 void
client_send_msg(const char * const barejid, const char * const msg) client_send_msg(const char * const barejid, const char * const msg)
{ {

View File

@ -35,6 +35,9 @@
#ifndef CLIENT_EVENTS_H #ifndef CLIENT_EVENTS_H
#define 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_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_muc_msg(const char * const roomjid, const char * const msg);
void client_send_priv_msg(const char * const fulljid, const char * const msg); void client_send_priv_msg(const char * const fulljid, const char * const msg);