From 18c97a431afb5421db21759e7ea8496ce0377abf Mon Sep 17 00:00:00 2001 From: James Booth Date: Sat, 18 Feb 2012 22:51:08 +0000 Subject: [PATCH] Start command validation --- Makefile | 2 +- command.c | 64 ++++++++++++++++++++++++++++++++++++++++++------------- command.h | 2 +- util.c | 21 ++++++++++++++++++ util.h | 1 + windows.c | 9 ++++++++ windows.h | 1 + 7 files changed, 83 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index b53e0a35..6f4b231c 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,7 @@ input_win.o: windows.h jabber.o: jabber.h log.h windows.h profanity.o: log.h windows.h jabber.h command.h util.o: util.h -command.o: command.h +command.o: command.h util.h main.o: log.h windows.h profanity.h .PHONY: clean diff --git a/command.c b/command.c index c6d4313b..0424f705 100644 --- a/command.c +++ b/command.c @@ -3,6 +3,7 @@ #include "command.h" #include "jabber.h" #include "windows.h" +#include "util.h" static int _cmd_quit(void); static int _cmd_help(void); @@ -10,32 +11,58 @@ static int _cmd_who(void); static int _cmd_msg(char *cmd); static int _cmd_close(char *cmd); static int _cmd_default(char *cmd); +static int _valid_start_command(char *cmd); -int handle_start_command(char *cmd) +int handle_start_command(char *inp) { int result; + + // trim input and take a copy + char inp_cpy[100]; + inp = trim(inp); + strcpy(inp_cpy, inp); + + // get the command "/command" + char *command = strtok(inp_cpy, " "); - if (strcmp(cmd, "/quit") == 0) { + // handle invalid commands + if (!_valid_start_command(command)) { + cons_bad_command(command); + gui_refresh(); + result = AWAIT_COMMAND; + + // quit + } else if (strcmp(command, "/quit") == 0) { result = QUIT_PROF; - } else if (strncmp(cmd, "/help", 5) == 0) { + + // help + } else if (strcmp(command, "/help") == 0) { cons_help(); gui_refresh(); result = AWAIT_COMMAND; - } else if (strncmp(cmd, "/connect ", 9) == 0) { - char *user; - user = strndup(cmd+9, strlen(cmd)-9); - status_bar_get_password(); - status_bar_refresh(); - char passwd[20]; - inp_get_password(passwd); - int connect_status = jabber_connect(user, passwd); - if (connect_status == CONNECTING) - result = START_MAIN; - else + // connect + } else if (strcmp(command, "/connect") == 0) { + if (strlen(inp) < 10) { + cons_bad_connect(); + gui_refresh(); result = AWAIT_COMMAND; + } else { + char *user; + user = strndup(inp+9, strlen(inp)-9); + + status_bar_get_password(); + status_bar_refresh(); + char passwd[20]; + inp_get_password(passwd); + int connect_status = jabber_connect(user, passwd); + if (connect_status == CONNECTING) + result = START_MAIN; + else + result = AWAIT_COMMAND; + } } else { - cons_bad_command(cmd); + cons_bad_command(inp); gui_refresh(); result = AWAIT_COMMAND; } @@ -130,3 +157,10 @@ static int _cmd_default(char *cmd) return TRUE; } + +static int _valid_start_command(char *cmd) +{ + return ((strcmp(cmd, "/quit") == 0) || + (strcmp(cmd, "/help") == 0) || + (strcmp(cmd, "/connect") == 0)); +} diff --git a/command.h b/command.h index 219b4302..6180e313 100644 --- a/command.h +++ b/command.h @@ -5,7 +5,7 @@ #define START_MAIN 2 #define QUIT_PROF 3 -int handle_start_command(char *cmd); +int handle_start_command(char *inp); int handle_command(char *cmd); #endif diff --git a/util.c b/util.c index 08737ee9..2d50cfbc 100644 --- a/util.c +++ b/util.c @@ -1,4 +1,6 @@ #include +#include +#include void get_time(char *thetime) { @@ -11,3 +13,22 @@ void get_time(char *thetime) strftime(thetime, 80, "%H:%M", timeinfo); } +char *trim(char *str) +{ + char *end; + + while (isspace(*str)) + str++; + + if (*str == 0) + return str; + + end = str + strlen(str) - 1; + while (end > str && isspace(*end)) + end--; + + *(end+1) = 0; + + return str; +} + diff --git a/util.h b/util.h index d37d981b..76555263 100644 --- a/util.h +++ b/util.h @@ -2,5 +2,6 @@ #define UTIL_H void get_time(char *thetime); +char *trim(char *str); #endif diff --git a/windows.c b/windows.c index af5f5f88..21ccb780 100644 --- a/windows.c +++ b/windows.c @@ -220,6 +220,15 @@ void cons_bad_command(char *cmd) wprintw(_wins[0].win, " [%s] Unknown command: %s\n", tstmp, cmd); } +void cons_bad_connect(void) +{ + char tstmp[80]; + get_time(tstmp); + + wprintw(_wins[0].win, + " [%s] Usage: /connect user@host\n", tstmp); +} + static void _create_windows(void) { int rows, cols; diff --git a/windows.h b/windows.h index 2b7fa2e9..11c50f8f 100644 --- a/windows.h +++ b/windows.h @@ -36,6 +36,7 @@ void win_show_outgoing_msg(char *from, char *to, char *message); // console window actions void cons_help(void); void cons_bad_command(char *cmd); +void cons_bad_connect(void); void cons_show(char *cmd); // status bar actions