about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2012-02-20 01:42:29 +0000
committerJames Booth <boothj5@gmail.com>2012-02-20 01:42:29 +0000
commit7dfea94c88a876c51b86b46578c4cf409605a6c3 (patch)
tree6d89f0cc73533e1af28dd08bfa0fa7b4ce3766ec
parentb3f42cd300e703e4d411ed03cef138462ff34fbf (diff)
downloadprofani-tty-7dfea94c88a876c51b86b46578c4cf409605a6c3.tar.gz
Merged input processing loops
-rw-r--r--command.c197
-rw-r--r--command.h7
-rw-r--r--jabber.c19
-rw-r--r--jabber.h7
-rw-r--r--main.c8
-rw-r--r--profanity.c44
-rw-r--r--profanity.h5
-rw-r--r--title_bar.c1
-rw-r--r--windows.c23
-rw-r--r--windows.h2
10 files changed, 111 insertions, 202 deletions
diff --git a/command.c b/command.c
index ce1919cf..32baf051 100644
--- a/command.c
+++ b/command.c
@@ -6,96 +6,27 @@
 #include "windows.h"
 #include "util.h"
 
-static int _cmd_start_quit(void);
-static int _cmd_start_help(void);
-static int _cmd_start_connect(char *inp);
-static int _cmd_start_default(char *inp);
-static int _valid_command(char *cmd);
-
+static int _handle_command(char *command, char *inp);
 static int _cmd_quit(void);
 static int _cmd_help(void);
 static int _cmd_who(void);
+static int _cmd_connect(char *inp);
 static int _cmd_msg(char *inp);
 static int _cmd_close(char *inp);
 static int _cmd_default(char *inp);
-static int _valid_start_command(char *cmd);
-
-int handle_start_command(char *inp)
-{
-    int result;
-
-    // handle nothing
-    if (strlen(inp) == 0) {
-        gui_refresh();
-        return AWAIT_COMMAND;
-    }
-    
-    // trim input and take a copy
-    inp = trim(inp);
-    char inp_cpy[strlen(inp) + 1];
-    strcpy(inp_cpy, inp);
-    
-    // get the command "/command"
-    char *command = strtok(inp_cpy, " ");
-
-    if (!_valid_start_command(command)) {
-        cons_bad_command(command);
-        gui_refresh();
-        result = AWAIT_COMMAND;
-    } else if (strcmp(command, "/quit") == 0) {
-        result = _cmd_start_quit();
-    } else if (strcmp(command, "/help") == 0) {
-        result = _cmd_start_help();
-    } else if (strcmp(command, "/connect") == 0) {
-        result = _cmd_start_connect(inp);
-    } else {
-        result = _cmd_start_default(inp);
-    }
-
-    inp_clear();
 
-    return result;
-}
-
-int handle_command(char *inp)
+int process_input(char *inp)
 {
     int result = FALSE;
 
-    // handle nothing
     if (strlen(inp) == 0) {
-        gui_refresh();
-        return TRUE;
-    }
-
-    // if it was a command, dispatch it
-    if (inp[0] == '/') {
-
-        // trim input and take a copy
+        result = TRUE;
+    } else if (inp[0] == '/') {
         inp = trim(inp);
         char inp_cpy[strlen(inp) + 1];
         strcpy(inp_cpy, inp);
-
-        // get the command "/command"
         char *command = strtok(inp_cpy, " ");
-
-        // if we don't know it, treat it as a message
-        if (!_valid_command(command)) {
-            result = _cmd_default(inp);
-        
-        // otherwise handle it
-        } else if (strcmp(inp, "/quit") == 0) {
-            result = _cmd_quit();
-        } else if (strncmp(inp, "/help", 5) == 0) {
-            result = _cmd_help();
-        } else if (strncmp(inp, "/who", 4) == 0) {
-            result = _cmd_who();
-        } else if (strncmp(inp, "/msg", 4) == 0) {
-            result = _cmd_msg(inp);
-        } else if (strncmp(inp, "/close", 6) == 0) {
-            result = _cmd_close(inp);
-        }
-
-    // was just text, send it
+        result = _handle_command(command, inp);
     } else {
         result = _cmd_default(inp);
     }
@@ -103,55 +34,58 @@ int handle_command(char *inp)
     inp_clear();
 
     return result;
-
 }
 
-static int _cmd_start_quit(void)
+static int _handle_command(char *command, char *inp)
 {
-    return QUIT_PROF;
-}
+    int result = FALSE;
 
-static int _cmd_start_help(void)
-{
-    cons_help();
-    gui_refresh();
-    return AWAIT_COMMAND;
+    if (strcmp(command, "/quit") == 0) {
+        result = _cmd_quit();
+    } else if (strcmp(command, "/help") == 0) {
+        result = _cmd_help();
+    } else if (strcmp(command, "/who") == 0) {
+        result = _cmd_who();
+    } else if (strcmp(command, "/msg") == 0) {
+        result = _cmd_msg(inp);
+    } else if (strcmp(command, "/close") == 0) {
+        result = _cmd_close(inp);
+    } else if (strcmp(command, "/connect") == 0) {
+        result = _cmd_connect(inp);
+    } else {
+        result = _cmd_default(inp);
+    }
+
+    return result;
 }
 
-static int _cmd_start_connect(char *inp)
+static int _cmd_connect(char *inp)
 {
-    int result = AWAIT_COMMAND;
+    int result = FALSE;
+    int conn_status = jabber_connection_status();
 
-    if (strlen(inp) < 10) {
+    if ((conn_status != DISCONNECTED) && (conn_status != STARTED)) {
+        cons_not_disconnected();
+        result = TRUE;
+    } else if (strlen(inp) < 10) {
         cons_bad_connect();
-        gui_refresh();
-        result = AWAIT_COMMAND;
+        result = TRUE;
     } else {
         char *user;
         user = strndup(inp+9, strlen(inp)-9);
-
         status_bar_get_password();
         status_bar_refresh();
         char passwd[21];
+        inp_block();
         inp_get_password(passwd);
-        int connect_status = jabber_connect(user, passwd);
-        if (connect_status == CONNECTING)
-            result = START_MAIN;
-        else
-            result = AWAIT_COMMAND;
+        inp_non_block();
+        jabber_connect(user, passwd);
+        result = TRUE;
     }
     
     return result;
 }
 
-static int _cmd_start_default(char *inp)
-{
-    cons_bad_command(inp);
-    gui_refresh();
-
-    return AWAIT_COMMAND;
-}
-
 static int _cmd_quit(void)
 {
     return FALSE;
@@ -166,7 +100,12 @@ static int _cmd_help(void)
 
 static int _cmd_who(void)
 {
-    jabber_roster_request();
+    int conn_status = jabber_connection_status();
+
+    if (conn_status != CONNECTED)
+        cons_not_connected();
+    else
+        jabber_roster_request();
 
     return TRUE;
 }
@@ -176,23 +115,29 @@ static int _cmd_msg(char *inp)
     char *usr = NULL;
     char *msg = NULL;
 
-    // copy input    
-    char inp_cpy[strlen(inp) + 1];
-    strcpy(inp_cpy, inp);
-
-    // get user
-    strtok(inp_cpy, " ");
-    usr = strtok(NULL, " ");
-    if ((usr != NULL) && (strlen(inp) > (5 + strlen(usr) + 1))) {
-        msg = strndup(inp+5+strlen(usr)+1, strlen(inp)-(5+strlen(usr)+1));
-        if (msg != NULL) {
-            jabber_send(msg, usr);
-            win_show_outgoing_msg("me", usr, msg);
+    int conn_status = jabber_connection_status();
+
+    if (conn_status != CONNECTED) {
+        cons_not_connected();
+    } else {
+        // copy input    
+        char inp_cpy[strlen(inp) + 1];
+        strcpy(inp_cpy, inp);
+
+        // get user
+        strtok(inp_cpy, " ");
+        usr = strtok(NULL, " ");
+        if ((usr != NULL) && (strlen(inp) > (5 + strlen(usr) + 1))) {
+            msg = strndup(inp+5+strlen(usr)+1, strlen(inp)-(5+strlen(usr)+1));
+            if (msg != NULL) {
+                jabber_send(msg, usr);
+                win_show_outgoing_msg("me", usr, msg);
+            } else {
+                cons_bad_message();
+            }
         } else {
             cons_bad_message();
         }
-    } else {
-        cons_bad_message();
     }
 
     return TRUE;
@@ -222,19 +167,3 @@ static int _cmd_default(char *inp)
 
     return TRUE;
 }
-
-static int _valid_start_command(char *cmd)
-{
-    return ((strcmp(cmd, "/quit") == 0) || 
-            (strcmp(cmd, "/help") == 0) ||
-            (strcmp(cmd, "/connect") == 0));
-}
-
-static int _valid_command(char *cmd)
-{
-    return ((strcmp(cmd, "/quit") == 0) || 
-            (strcmp(cmd, "/help") == 0) ||
-            (strcmp(cmd, "/who") == 0) ||
-            (strcmp(cmd, "/msg") == 0) ||
-            (strcmp(cmd, "/close") == 0));
-}
diff --git a/command.h b/command.h
index b0b2cbea..c0e09b92 100644
--- a/command.h
+++ b/command.h
@@ -1,11 +1,6 @@
 #ifndef COMMAND_H
 #define COMMAND_H
 
-#define AWAIT_COMMAND 1
-#define START_MAIN 2
-#define QUIT_PROF 3
-
-int handle_start_command(char *inp);
-int handle_command(char *inp);
+int process_input(char *inp);
 
 #endif
diff --git a/jabber.c b/jabber.c
index 77c4906c..16196a62 100644
--- a/jabber.c
+++ b/jabber.c
@@ -13,7 +13,7 @@ static xmpp_ctx_t *_ctx;
 static xmpp_conn_t *_conn;
 
 // connection status
-static int _conn_status = CONNECTING;
+static int _conn_status = STARTED;
 
 void xmpp_file_logger(void * const userdata, const xmpp_log_level_t level,
     const char * const area, const char * const msg);
@@ -74,15 +74,18 @@ int jabber_connect(char *user, char *passwd)
 
 void jabber_disconnect(void)
 {
-    xmpp_conn_release(_conn);
-    xmpp_ctx_free(_ctx);
-    xmpp_shutdown();
-    _conn_status = DISCONNECTED;
+    if (_conn_status == CONNECTED) {
+        xmpp_conn_release(_conn);
+        xmpp_ctx_free(_ctx);
+        xmpp_shutdown();
+        _conn_status = DISCONNECTED;
+    }
 }
 
 void jabber_process_events(void)
 {
-    xmpp_run_once(_ctx, 10);
+    if (_conn_status == CONNECTED || _conn_status == CONNECTING)
+        xmpp_run_once(_ctx, 10);
 }
 
 void jabber_send(char *msg, char *recipient)
@@ -153,11 +156,11 @@ static void _jabber_conn_handler(xmpp_conn_t * const conn,
         const char *jid = xmpp_conn_get_jid(conn);
         const char *msg = " logged in successfully.";
         char line[strlen(jid) + 1 + strlen(msg) + 1];
-        sprintf(line, "%s %s", xmpp_conn_get_jid(conn), msg);
+        sprintf(line, "%s %s", jid, msg);
         title_bar_connected();
 
         cons_good_show(line);
-        status_bar_print_message(xmpp_conn_get_jid(conn));
+        status_bar_print_message(jid);
         status_bar_refresh();
 
         xmpp_stanza_t* pres;
diff --git a/jabber.h b/jabber.h
index a3d3847f..036dc1ee 100644
--- a/jabber.h
+++ b/jabber.h
@@ -1,9 +1,10 @@
 #ifndef JABBER_H
 #define JABBER_H
 
-#define CONNECTING 0
-#define CONNECTED 1
-#define DISCONNECTED 2
+#define STARTED 0
+#define CONNECTING 1
+#define CONNECTED 2
+#define DISCONNECTED 3
 
 int jabber_connection_status(void);
 int jabber_connect(char *user, char *passwd);
diff --git a/main.c b/main.c
index 50a9f1a5..103b4190 100644
--- a/main.c
+++ b/main.c
@@ -4,15 +4,11 @@
 
 int main(void)
 {   
-    int exit_status;
-    
     log_init();
     gui_init();
 
-    do { 
-        exit_status = profanity_start();
-    } while (exit_status == LOGIN_FAIL);
-    
+    profanity_main();
+        
     gui_close();
     log_close();
 
diff --git a/profanity.c b/profanity.c
index 7e0dbe30..a4352c74 100644
--- a/profanity.c
+++ b/profanity.c
@@ -9,59 +9,27 @@
 #include "jabber.h"
 #include "command.h"
 
-static int _profanity_main(void);
 static void _profanity_event_loop(int *ch, char *cmd, int *size);
 static void _process_special_keys(int *ch);
 
-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();
-        inp_get_command_str(cmd);
-        cmd_result = handle_start_command(cmd);
-    }
-
-    if (cmd_result == START_MAIN) {
-        exit_status = _profanity_main();
-    }
-
-    return exit_status;
-}
-
-static int _profanity_main(void)
+void profanity_main(void)
 {
     int cmd_result = TRUE;
 
     inp_non_block();
     while(cmd_result == TRUE) {
         int ch = ERR;
-        char cmd[100];
+        char inp[100];
         int size = 0;
 
-        while(ch != '\n') {
-            _profanity_event_loop(&ch, cmd, &size);
-
-            int conn_status = jabber_connection_status();
-            if (conn_status == DISCONNECTED) {
-                inp_block();
-                return LOGIN_FAIL;
-            }
-        }
+        while(ch != '\n')
+            _profanity_event_loop(&ch, inp, &size);
 
-        cmd[size++] = '\0';
-        cmd_result = handle_command(cmd);
+        inp[size++] = '\0';
+        cmd_result = process_input(inp);
     }
 
     jabber_disconnect();
-    return QUIT;
 }
 
 static void _profanity_event_loop(int *ch, char *cmd, int *size)
diff --git a/profanity.h b/profanity.h
index 6a8256e1..966cfbaa 100644
--- a/profanity.h
+++ b/profanity.h
@@ -1,9 +1,6 @@
 #ifndef PROFANITY_H
 #define PROFANITY_H
 
-#define QUIT 0
-#define LOGIN_FAIL 1
-
-int profanity_start(void);
+void profanity_main(void);
 
 #endif
diff --git a/title_bar.c b/title_bar.c
index aaa053f1..67b68566 100644
--- a/title_bar.c
+++ b/title_bar.c
@@ -11,6 +11,7 @@ void create_title_bar(void)
     title_bar = newwin(1, cols, 0, 0);
     wbkgd(title_bar, COLOR_PAIR(3));
     title_bar_title();
+    title_bar_disconnected();
 }
 
 void title_bar_title()
diff --git a/windows.c b/windows.c
index a1e7e8fd..8db277be 100644
--- a/windows.c
+++ b/windows.c
@@ -61,11 +61,10 @@ void win_switch_to(int i)
 {    
     _curr_win = i;
 
-    if (i == 0) {
+    if (i == 0)
         title_bar_title();
-    } else {
+    else
         title_bar_show(_wins[i].from);
-    }
 }
 
 void win_close_win(void)
@@ -273,6 +272,24 @@ void cons_bad_connect(void)
         " [%s] Usage: /connect user@host\n", tstmp);
 }
 
+void cons_not_disconnected(void)
+{
+    char tstmp[80];
+    get_time(tstmp);
+
+    wprintw(_wins[0].win, 
+        " [%s] You are either connected already, or a login is in process.\n", tstmp);
+}
+
+void cons_not_connected(void)
+{
+    char tstmp[80];
+    get_time(tstmp);
+
+    wprintw(_wins[0].win, 
+        " [%s] You are not currently connected.\n", tstmp);
+}
+
 void cons_bad_message(void)
 {
     char tstmp[80];
diff --git a/windows.h b/windows.h
index eccccf56..4519484c 100644
--- a/windows.h
+++ b/windows.h
@@ -38,6 +38,8 @@ void win_show_outgoing_msg(char *from, char *to, char *message);
 void cons_help(void);
 void cons_bad_command(char *cmd);
 void cons_bad_connect(void);
+void cons_not_disconnected(void);
+void cons_not_connected(void);
 void cons_bad_message(void);
 void cons_show(char *cmd);
 void cons_good_show(char *cmd);