1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-10-06 20:03:46 -04:00

Start command validation

This commit is contained in:
James Booth 2012-02-18 22:51:08 +00:00
parent 809daa9395
commit 18c97a431a
7 changed files with 83 additions and 17 deletions

View File

@ -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

View File

@ -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));
}

View File

@ -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

21
util.c
View File

@ -1,4 +1,6 @@
#include <time.h>
#include <string.h>
#include <ctype.h>
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;
}

1
util.h
View File

@ -2,5 +2,6 @@
#define UTIL_H
void get_time(char *thetime);
char *trim(char *str);
#endif

View File

@ -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;

View File

@ -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