From 03b90cf5abfcd802fd901ba2879388be209d7b5e Mon Sep 17 00:00:00 2001 From: James Booth Date: Fri, 17 Feb 2012 00:42:41 +0000 Subject: [PATCH] Handles failed logins correctly --- command.c | 9 +++++++-- input_win.c | 5 +++++ jabber.c | 26 ++++++++++++++++++++++---- jabber.h | 7 ++++++- main.c | 5 ++++- profanity.c | 24 +++++++++++++++++++----- profanity.h | 5 ++++- title_bar.c | 16 ++++++++++++++++ windows.h | 3 +++ 9 files changed, 86 insertions(+), 14 deletions(-) diff --git a/command.c b/command.c index b3ce1e93..c6d4313b 100644 --- a/command.c +++ b/command.c @@ -19,6 +19,7 @@ int handle_start_command(char *cmd) result = QUIT_PROF; } else if (strncmp(cmd, "/help", 5) == 0) { cons_help(); + gui_refresh(); result = AWAIT_COMMAND; } else if (strncmp(cmd, "/connect ", 9) == 0) { char *user; @@ -28,10 +29,14 @@ int handle_start_command(char *cmd) status_bar_refresh(); char passwd[20]; inp_get_password(passwd); - jabber_connect(user, passwd); - result = START_MAIN; + int connect_status = jabber_connect(user, passwd); + if (connect_status == CONNECTING) + result = START_MAIN; + else + result = AWAIT_COMMAND; } else { cons_bad_command(cmd); + gui_refresh(); result = AWAIT_COMMAND; } diff --git a/input_win.c b/input_win.c index 0e3e8b16..8adce620 100644 --- a/input_win.c +++ b/input_win.c @@ -32,6 +32,11 @@ void inp_non_block(void) wtimeout(inp_win, 0); } +void inp_block(void) +{ + wtimeout(inp_win, -1); +} + void inp_poll_char(int *ch, char command[], int *size) { int inp_y = 0; diff --git a/jabber.c b/jabber.c index dbb97f79..91d50530 100644 --- a/jabber.c +++ b/jabber.c @@ -12,6 +12,9 @@ static xmpp_log_t *_log; static xmpp_ctx_t *_ctx; static xmpp_conn_t *_conn; +// connection status +static int _conn_status = CONNECTING; + void xmpp_file_logger(void * const userdata, const xmpp_log_level_t level, const char * const area, const char * const msg); @@ -39,7 +42,12 @@ static int _jabber_message_handler(xmpp_conn_t * const conn, static int _roster_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata); -void jabber_connect(char *user, char *passwd) +int jabber_connection_status(void) +{ + return (_conn_status); +} + +int jabber_connect(char *user, char *passwd) { xmpp_initialize(); @@ -52,10 +60,16 @@ void jabber_connect(char *user, char *passwd) int connect_status = xmpp_connect_client(_conn, NULL, 0, _jabber_conn_handler, _ctx); - if (connect_status == -1) - cons_show("XMPP connection failure"); - else + if (connect_status == 0) { cons_show("Connecting..."); + _conn_status = CONNECTING; + } + else { + cons_show("XMPP connection failure"); + _conn_status = DISCONNECTED; + } + + return _conn_status; } void jabber_disconnect(void) @@ -63,6 +77,7 @@ void jabber_disconnect(void) xmpp_conn_release(_conn); xmpp_ctx_free(_ctx); xmpp_shutdown(); + _conn_status = DISCONNECTED; } void jabber_process_events(void) @@ -137,6 +152,7 @@ static void _jabber_conn_handler(xmpp_conn_t * const conn, if (status == XMPP_CONN_CONNECT) { char line[100]; sprintf(line, "%s logged in successfully.", xmpp_conn_get_jid(conn)); + title_bar_connected(); cons_show(line); status_bar_print_message(xmpp_conn_get_jid(conn)); @@ -151,11 +167,13 @@ static void _jabber_conn_handler(xmpp_conn_t * const conn, xmpp_send(conn, pres); xmpp_stanza_release(pres); jabber_roster_request(); + _conn_status = CONNECTED; } else { cons_show("Login failed."); log_msg(CONN, "disconnected"); xmpp_stop(ctx); + _conn_status = DISCONNECTED; } } diff --git a/jabber.h b/jabber.h index 03feacbf..a3d3847f 100644 --- a/jabber.h +++ b/jabber.h @@ -1,7 +1,12 @@ #ifndef JABBER_H #define JABBER_H -void jabber_connect(char *user, char *passwd); +#define CONNECTING 0 +#define CONNECTED 1 +#define DISCONNECTED 2 + +int jabber_connection_status(void); +int jabber_connect(char *user, char *passwd); void jabber_disconnect(void); void jabber_roster_request(void); void jabber_process_events(void); diff --git a/main.c b/main.c index 8cac8539..2d9c44b7 100644 --- a/main.c +++ b/main.c @@ -4,10 +4,13 @@ int main(void) { + int exit_status = LOGIN_FAIL; log_init(); gui_init(); - profanity_start(); + while (exit_status == LOGIN_FAIL) { + exit_status = profanity_start(); + } gui_close(); log_close(); diff --git a/profanity.c b/profanity.c index 6d1ce688..7e0dbe30 100644 --- a/profanity.c +++ b/profanity.c @@ -9,15 +9,19 @@ #include "jabber.h" #include "command.h" -static void _profanity_main(void); +static int _profanity_main(void); static void _profanity_event_loop(int *ch, char *cmd, int *size); static void _process_special_keys(int *ch); -void profanity_start(void) +int profanity_start(void) { + int exit_status = QUIT; int cmd_result = AWAIT_COMMAND; char cmd[50]; + title_bar_disconnected(); + gui_refresh(); + while (cmd_result == AWAIT_COMMAND) { title_bar_refresh(); status_bar_refresh(); @@ -26,11 +30,13 @@ void profanity_start(void) } if (cmd_result == START_MAIN) { - _profanity_main(); + exit_status = _profanity_main(); } + + return exit_status; } -static void _profanity_main(void) +static int _profanity_main(void) { int cmd_result = TRUE; @@ -40,14 +46,22 @@ static void _profanity_main(void) char cmd[100]; int size = 0; - while(ch != '\n') + while(ch != '\n') { _profanity_event_loop(&ch, cmd, &size); + int conn_status = jabber_connection_status(); + if (conn_status == DISCONNECTED) { + inp_block(); + return LOGIN_FAIL; + } + } + cmd[size++] = '\0'; cmd_result = handle_command(cmd); } jabber_disconnect(); + return QUIT; } static void _profanity_event_loop(int *ch, char *cmd, int *size) diff --git a/profanity.h b/profanity.h index 785317a2..6a8256e1 100644 --- a/profanity.h +++ b/profanity.h @@ -1,6 +1,9 @@ #ifndef PROFANITY_H #define PROFANITY_H -void profanity_start(void); +#define QUIT 0 +#define LOGIN_FAIL 1 + +int profanity_start(void); #endif diff --git a/title_bar.c b/title_bar.c index 17727f77..0eb7a640 100644 --- a/title_bar.c +++ b/title_bar.c @@ -15,6 +15,22 @@ void create_title_bar(void) title_bar_show(title); } +void title_bar_connected(void) +{ + int rows, cols; + getmaxyx(stdscr, rows, cols); + + mvwprintw(title_bar, 0, cols - 12, " connected"); +} + +void title_bar_disconnected(void) +{ + int rows, cols; + getmaxyx(stdscr, rows, cols); + + mvwprintw(title_bar, 0, cols - 12, "disconnected"); +} + void title_bar_refresh(void) { touchwin(title_bar); diff --git a/windows.h b/windows.h index ac1ba8e8..2b7fa2e9 100644 --- a/windows.h +++ b/windows.h @@ -21,6 +21,8 @@ void create_input_window(void); // title bar actions void title_bar_refresh(void); void title_bar_show(char *title); +void title_bar_connected(void); +void title_bar_disconnected(void); // main window actions int win_is_active(int i); @@ -51,6 +53,7 @@ void inp_poll_char(int *ch, char command[], int *size); void inp_clear(void); void inp_put_back(void); void inp_non_block(void); +void inp_block(void); void inp_get_password(char *passwd); #endif