Validation on msg command

This commit is contained in:
James Booth
2012-02-19 00:17:36 +00:00
parent 94c97e08ab
commit 9f6356a8f7
3 changed files with 72 additions and 25 deletions

View File

@@ -9,13 +9,14 @@ static int _cmd_start_quit(void);
static int _cmd_start_help(void); static int _cmd_start_help(void);
static int _cmd_start_connect(char *inp); static int _cmd_start_connect(char *inp);
static int _cmd_start_default(char *inp); static int _cmd_start_default(char *inp);
static int _valid_command(char *cmd);
static int _cmd_quit(void); static int _cmd_quit(void);
static int _cmd_help(void); static int _cmd_help(void);
static int _cmd_who(void); static int _cmd_who(void);
static int _cmd_msg(char *cmd); static int _cmd_msg(char *inp);
static int _cmd_close(char *cmd); static int _cmd_close(char *inp);
static int _cmd_default(char *cmd); static int _cmd_default(char *inp);
static int _valid_start_command(char *cmd); static int _valid_start_command(char *cmd);
int handle_start_command(char *inp) int handle_start_command(char *inp)
@@ -65,16 +66,35 @@ int handle_command(char *inp)
return TRUE; return TRUE;
} }
if (strcmp(inp, "/quit") == 0) { // if it was a command, dispatch it
result = _cmd_quit(); if (inp[0] == '/') {
} else if (strncmp(inp, "/help", 5) == 0) {
result = _cmd_help(); // trim input and take a copy
} else if (strncmp(inp, "/who", 4) == 0) { char inp_cpy[100];
result = _cmd_who(); inp = trim(inp);
} else if (strncmp(inp, "/msg ", 5) == 0) { strcpy(inp_cpy, inp);
result = _cmd_msg(inp);
} else if (strncmp(inp, "/close", 6) == 0) { // get the command "/command"
result = _cmd_close(inp); 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
} else { } else {
result = _cmd_default(inp); result = _cmd_default(inp);
} }
@@ -150,45 +170,53 @@ static int _cmd_who(void)
return TRUE; return TRUE;
} }
static int _cmd_msg(char *cmd) static int _cmd_msg(char *inp)
{ {
char *usr_msg = NULL;
char *usr = NULL; char *usr = NULL;
char *msg = NULL; char *msg = NULL;
usr_msg = strndup(cmd+5, strlen(cmd)-5); // copy input
usr = strtok(usr_msg, " "); char inp_cpy[100];
if (usr != NULL) { strcpy(inp_cpy, inp);
msg = strndup(cmd+5+strlen(usr)+1, strlen(cmd)-(5+strlen(usr)+1));
// 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) { if (msg != NULL) {
jabber_send(msg, usr); jabber_send(msg, usr);
win_show_outgoing_msg("me", usr, msg); win_show_outgoing_msg("me", usr, msg);
} else {
cons_bad_message();
} }
} else {
cons_bad_message();
} }
return TRUE; return TRUE;
} }
static int _cmd_close(char *cmd) static int _cmd_close(char *inp)
{ {
if (win_in_chat()) { if (win_in_chat()) {
win_close_win(); win_close_win();
} else { } else {
cons_bad_command(cmd); cons_bad_command(inp);
} }
return TRUE; return TRUE;
} }
static int _cmd_default(char *cmd) static int _cmd_default(char *inp)
{ {
if (win_in_chat()) { if (win_in_chat()) {
char recipient[100] = ""; char recipient[100] = "";
win_get_recipient(recipient); win_get_recipient(recipient);
jabber_send(cmd, recipient); jabber_send(inp, recipient);
win_show_outgoing_msg("me", recipient, cmd); win_show_outgoing_msg("me", recipient, inp);
} else { } else {
cons_bad_command(cmd); cons_bad_command(inp);
} }
return TRUE; return TRUE;
@@ -200,3 +228,12 @@ static int _valid_start_command(char *cmd)
(strcmp(cmd, "/help") == 0) || (strcmp(cmd, "/help") == 0) ||
(strcmp(cmd, "/connect") == 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));
}

View File

@@ -229,6 +229,15 @@ void cons_bad_connect(void)
" [%s] Usage: /connect user@host\n", tstmp); " [%s] Usage: /connect user@host\n", tstmp);
} }
void cons_bad_message(void)
{
char tstmp[80];
get_time(tstmp);
wprintw(_wins[0].win,
" [%s] Usage: /msg user@host message\n", tstmp);
}
static void _create_windows(void) static void _create_windows(void)
{ {
int rows, cols; int rows, cols;

View File

@@ -37,6 +37,7 @@ void win_show_outgoing_msg(char *from, char *to, char *message);
void cons_help(void); void cons_help(void);
void cons_bad_command(char *cmd); void cons_bad_command(char *cmd);
void cons_bad_connect(void); void cons_bad_connect(void);
void cons_bad_message(void);
void cons_show(char *cmd); void cons_show(char *cmd);
// status bar actions // status bar actions