1
1
mirror of https://github.com/profanity-im/profanity.git synced 2025-01-03 14:57:42 -05:00

Merged input processing loops

This commit is contained in:
James Booth 2012-02-20 01:42:29 +00:00
parent b3f42cd300
commit 7dfea94c88
10 changed files with 118 additions and 209 deletions

211
command.c
View File

@ -6,96 +6,27 @@
#include "windows.h" #include "windows.h"
#include "util.h" #include "util.h"
static int _cmd_start_quit(void); static int _handle_command(char *command, char *inp);
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 _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_connect(char *inp);
static int _cmd_msg(char *inp); static int _cmd_msg(char *inp);
static int _cmd_close(char *inp); static int _cmd_close(char *inp);
static int _cmd_default(char *inp); static int _cmd_default(char *inp);
static int _valid_start_command(char *cmd);
int handle_start_command(char *inp) int process_input(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 result = FALSE; int result = FALSE;
// handle nothing
if (strlen(inp) == 0) { if (strlen(inp) == 0) {
gui_refresh(); result = TRUE;
return TRUE; } else if (inp[0] == '/') {
}
// if it was a command, dispatch it
if (inp[0] == '/') {
// trim input and take a copy
inp = trim(inp); inp = trim(inp);
char inp_cpy[strlen(inp) + 1]; char inp_cpy[strlen(inp) + 1];
strcpy(inp_cpy, inp); strcpy(inp_cpy, inp);
// get the command "/command"
char *command = strtok(inp_cpy, " "); char *command = strtok(inp_cpy, " ");
result = _handle_command(command, inp);
// 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);
} }
@ -103,53 +34,56 @@ int handle_command(char *inp)
inp_clear(); inp_clear();
return result; 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) if (strcmp(command, "/quit") == 0) {
{ result = _cmd_quit();
cons_help(); } else if (strcmp(command, "/help") == 0) {
gui_refresh(); result = _cmd_help();
return AWAIT_COMMAND; } else if (strcmp(command, "/who") == 0) {
} result = _cmd_who();
} else if (strcmp(command, "/msg") == 0) {
static int _cmd_start_connect(char *inp) result = _cmd_msg(inp);
{ } else if (strcmp(command, "/close") == 0) {
int result = AWAIT_COMMAND; result = _cmd_close(inp);
} else if (strcmp(command, "/connect") == 0) {
if (strlen(inp) < 10) { result = _cmd_connect(inp);
cons_bad_connect();
gui_refresh();
result = AWAIT_COMMAND;
} else { } else {
char *user; result = _cmd_default(inp);
user = strndup(inp+9, strlen(inp)-9);
status_bar_get_password();
status_bar_refresh();
char passwd[21];
inp_get_password(passwd);
int connect_status = jabber_connect(user, passwd);
if (connect_status == CONNECTING)
result = START_MAIN;
else
result = AWAIT_COMMAND;
} }
return result; return result;
} }
static int _cmd_start_default(char *inp) static int _cmd_connect(char *inp)
{ {
cons_bad_command(inp); int result = FALSE;
gui_refresh(); int conn_status = jabber_connection_status();
return AWAIT_COMMAND; if ((conn_status != DISCONNECTED) && (conn_status != STARTED)) {
cons_not_disconnected();
result = TRUE;
} else if (strlen(inp) < 10) {
cons_bad_connect();
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);
inp_non_block();
jabber_connect(user, passwd);
result = TRUE;
}
return result;
} }
static int _cmd_quit(void) static int _cmd_quit(void)
@ -166,7 +100,12 @@ static int _cmd_help(void)
static int _cmd_who(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; return TRUE;
} }
@ -176,23 +115,29 @@ static int _cmd_msg(char *inp)
char *usr = NULL; char *usr = NULL;
char *msg = NULL; char *msg = NULL;
// copy input int conn_status = jabber_connection_status();
char inp_cpy[strlen(inp) + 1];
strcpy(inp_cpy, inp);
// get user if (conn_status != CONNECTED) {
strtok(inp_cpy, " "); cons_not_connected();
usr = strtok(NULL, " "); } else {
if ((usr != NULL) && (strlen(inp) > (5 + strlen(usr) + 1))) { // copy input
msg = strndup(inp+5+strlen(usr)+1, strlen(inp)-(5+strlen(usr)+1)); char inp_cpy[strlen(inp) + 1];
if (msg != NULL) { strcpy(inp_cpy, inp);
jabber_send(msg, usr);
win_show_outgoing_msg("me", usr, msg); // 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 { } else {
cons_bad_message(); cons_bad_message();
} }
} else {
cons_bad_message();
} }
return TRUE; return TRUE;
@ -222,19 +167,3 @@ static int _cmd_default(char *inp)
return TRUE; 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));
}

View File

@ -1,11 +1,6 @@
#ifndef COMMAND_H #ifndef COMMAND_H
#define COMMAND_H #define COMMAND_H
#define AWAIT_COMMAND 1 int process_input(char *inp);
#define START_MAIN 2
#define QUIT_PROF 3
int handle_start_command(char *inp);
int handle_command(char *inp);
#endif #endif

View File

@ -13,7 +13,7 @@ static xmpp_ctx_t *_ctx;
static xmpp_conn_t *_conn; static xmpp_conn_t *_conn;
// connection status // 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, void xmpp_file_logger(void * const userdata, const xmpp_log_level_t level,
const char * const area, const char * const msg); const char * const area, const char * const msg);
@ -74,15 +74,18 @@ int jabber_connect(char *user, char *passwd)
void jabber_disconnect(void) void jabber_disconnect(void)
{ {
xmpp_conn_release(_conn); if (_conn_status == CONNECTED) {
xmpp_ctx_free(_ctx); xmpp_conn_release(_conn);
xmpp_shutdown(); xmpp_ctx_free(_ctx);
_conn_status = DISCONNECTED; xmpp_shutdown();
_conn_status = DISCONNECTED;
}
} }
void jabber_process_events(void) 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) 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 *jid = xmpp_conn_get_jid(conn);
const char *msg = " logged in successfully."; const char *msg = " logged in successfully.";
char line[strlen(jid) + 1 + strlen(msg) + 1]; 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(); title_bar_connected();
cons_good_show(line); cons_good_show(line);
status_bar_print_message(xmpp_conn_get_jid(conn)); status_bar_print_message(jid);
status_bar_refresh(); status_bar_refresh();
xmpp_stanza_t* pres; xmpp_stanza_t* pres;

View File

@ -1,9 +1,10 @@
#ifndef JABBER_H #ifndef JABBER_H
#define JABBER_H #define JABBER_H
#define CONNECTING 0 #define STARTED 0
#define CONNECTED 1 #define CONNECTING 1
#define DISCONNECTED 2 #define CONNECTED 2
#define DISCONNECTED 3
int jabber_connection_status(void); int jabber_connection_status(void);
int jabber_connect(char *user, char *passwd); int jabber_connect(char *user, char *passwd);

8
main.c
View File

@ -4,15 +4,11 @@
int main(void) int main(void)
{ {
int exit_status;
log_init(); log_init();
gui_init(); gui_init();
do { profanity_main();
exit_status = profanity_start();
} while (exit_status == LOGIN_FAIL);
gui_close(); gui_close();
log_close(); log_close();

View File

@ -9,59 +9,27 @@
#include "jabber.h" #include "jabber.h"
#include "command.h" #include "command.h"
static int _profanity_main(void);
static void _profanity_event_loop(int *ch, char *cmd, int *size); static void _profanity_event_loop(int *ch, char *cmd, int *size);
static void _process_special_keys(int *ch); static void _process_special_keys(int *ch);
int profanity_start(void) void profanity_main(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)
{ {
int cmd_result = TRUE; int cmd_result = TRUE;
inp_non_block(); inp_non_block();
while(cmd_result == TRUE) { while(cmd_result == TRUE) {
int ch = ERR; int ch = ERR;
char cmd[100]; char inp[100];
int size = 0; int size = 0;
while(ch != '\n') { while(ch != '\n')
_profanity_event_loop(&ch, cmd, &size); _profanity_event_loop(&ch, inp, &size);
int conn_status = jabber_connection_status(); inp[size++] = '\0';
if (conn_status == DISCONNECTED) { cmd_result = process_input(inp);
inp_block();
return LOGIN_FAIL;
}
}
cmd[size++] = '\0';
cmd_result = handle_command(cmd);
} }
jabber_disconnect(); jabber_disconnect();
return QUIT;
} }
static void _profanity_event_loop(int *ch, char *cmd, int *size) static void _profanity_event_loop(int *ch, char *cmd, int *size)

View File

@ -1,9 +1,6 @@
#ifndef PROFANITY_H #ifndef PROFANITY_H
#define PROFANITY_H #define PROFANITY_H
#define QUIT 0 void profanity_main(void);
#define LOGIN_FAIL 1
int profanity_start(void);
#endif #endif

View File

@ -11,6 +11,7 @@ void create_title_bar(void)
title_bar = newwin(1, cols, 0, 0); title_bar = newwin(1, cols, 0, 0);
wbkgd(title_bar, COLOR_PAIR(3)); wbkgd(title_bar, COLOR_PAIR(3));
title_bar_title(); title_bar_title();
title_bar_disconnected();
} }
void title_bar_title() void title_bar_title()

View File

@ -61,11 +61,10 @@ void win_switch_to(int i)
{ {
_curr_win = i; _curr_win = i;
if (i == 0) { if (i == 0)
title_bar_title(); title_bar_title();
} else { else
title_bar_show(_wins[i].from); title_bar_show(_wins[i].from);
}
} }
void win_close_win(void) void win_close_win(void)
@ -273,6 +272,24 @@ void cons_bad_connect(void)
" [%s] Usage: /connect user@host\n", tstmp); " [%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) void cons_bad_message(void)
{ {
char tstmp[80]; char tstmp[80];

View File

@ -38,6 +38,8 @@ 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_not_disconnected(void);
void cons_not_connected(void);
void cons_bad_message(void); void cons_bad_message(void);
void cons_show(char *cmd); void cons_show(char *cmd);
void cons_good_show(char *cmd); void cons_good_show(char *cmd);