From a7df149ee901310ad8ac89fa2ddbd52f9c8d9e70 Mon Sep 17 00:00:00 2001 From: James Booth Date: Thu, 9 Feb 2012 23:15:53 +0000 Subject: [PATCH] Pulled out command module --- Makefile | 5 ++- command.c | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++ command.h | 6 +++ profanity.c | 73 ++---------------------------------- 4 files changed, 117 insertions(+), 71 deletions(-) create mode 100644 command.c create mode 100644 command.h diff --git a/Makefile b/Makefile index 91e90388..dfcef035 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ WARNS = -Werror -Wall -Wextra -Wno-unused-parameter -Wno-unused-but-set-variable LIBS = -lxml2 -lssl -lresolv -lncurses -lstrophe CFLAGS = -O3 $(WARNS) $(LIBS) OBJS = log.o windows.o title_bar.o input_bar.o input_win.o jabber.o \ - profanity.o util.o main.o + profanity.o util.o command.o main.o profanity: $(OBJS) $(CC) -o profanity $(OBJS) $(LIBS) @@ -14,8 +14,9 @@ title_bar.o: windows.h input_bar.o: windows.h input_win.o: windows.h jabber.o: jabber.h log.h windows.h -profanity.o: log.h windows.h jabber.h +profanity.o: log.h windows.h jabber.h command.h util.o: util.h +command.o: command.h main.o: log.h windows.h profanity.h .PHONY: clean diff --git a/command.c b/command.c new file mode 100644 index 00000000..7b578cd8 --- /dev/null +++ b/command.c @@ -0,0 +1,104 @@ +#include +#include +#include "command.h" +#include "jabber.h" +#include "windows.h" + +static int cmd_quit(void); +static int cmd_help(void); +static int cmd_who(void); +static int cmd_msg(char *cmd); +static int cmd_close(char *cmd); +static int cmd_default(char *cmd); + +int handle_command(char *cmd) +{ + int result = FALSE; + if (strcmp(cmd, "/quit") == 0) { + result = cmd_quit(); + } else if (strncmp(cmd, "/help", 5) == 0) { + result = cmd_help(); + } else if (strncmp(cmd, "/who", 4) == 0) { + result = cmd_who(); + } else if (strncmp(cmd, "/msg ", 5) == 0) { + result = cmd_msg(cmd); + } else if (strncmp(cmd, "/close", 6) == 0) { + result = cmd_close(cmd); + } else { + result = cmd_default(cmd); + } + + return result; + +} + +static int cmd_quit(void) +{ + return FALSE; +} + +static int cmd_help(void) +{ + cons_help(); + inp_clear(); + + return TRUE; +} + +static int cmd_who(void) +{ + jabber_roster_request(); + inp_clear(); + + return TRUE; +} + +static int cmd_msg(char *cmd) +{ + char *usr_msg = NULL; + char *usr = NULL; + char *msg = NULL; + + usr_msg = strndup(cmd+5, strlen(cmd)-5); + usr = strtok(usr_msg, " "); + if (usr != NULL) { + msg = strndup(cmd+5+strlen(usr)+1, strlen(cmd)-(5+strlen(usr)+1)); + if (msg != NULL) { + jabber_send(msg, usr); + show_outgoing_msg("me", usr, msg); + } + } + + inp_clear(); + + return TRUE; +} + +static int cmd_close(char *cmd) +{ + if (in_chat()) { + close_win(); + } else { + cons_bad_command(cmd); + } + + inp_clear(); + + return TRUE; +} + +static int cmd_default(char *cmd) +{ + if (in_chat()) { + char recipient[100] = ""; + get_recipient(recipient); + jabber_send(cmd, recipient); + show_outgoing_msg("me", recipient, cmd); + } else { + cons_bad_command(cmd); + } + + inp_clear(); + + return TRUE; +} diff --git a/command.h b/command.h new file mode 100644 index 00000000..b06663cf --- /dev/null +++ b/command.h @@ -0,0 +1,6 @@ +#ifndef COMMAND_H +#define COMMAND_H + +int handle_command(char *cmd); + +#endif diff --git a/profanity.c b/profanity.c index e21bf504..2002810d 100644 --- a/profanity.c +++ b/profanity.c @@ -7,14 +7,13 @@ #include "log.h" #include "windows.h" #include "jabber.h" +#include "command.h" #define AWAIT_COMMAND 1 #define START_MAIN 2 #define QUIT_PROF 3 -#define CONTINUE_EVENT_LOOP 4 static int handle_start_command(char *cmd); -static int handle_main_command(char *cmd); static void profanity_main(void); static void profanity_event_loop(int *ch, char *cmd, int *size); @@ -36,10 +35,10 @@ void profanity_start(void) static void profanity_main(void) { - int cmd_result = CONTINUE_EVENT_LOOP; + int cmd_result = TRUE; inp_non_block(); - while(cmd_result == CONTINUE_EVENT_LOOP) { + while(cmd_result == TRUE) { int ch = ERR; char cmd[100]; int size = 0; @@ -48,7 +47,7 @@ static void profanity_main(void) profanity_event_loop(&ch, cmd, &size); cmd[size++] = '\0'; - cmd_result = handle_main_command(cmd); + cmd_result = handle_command(cmd); } jabber_disconnect(); @@ -118,67 +117,3 @@ static int handle_start_command(char *cmd) return result; } -static int handle_main_command(char *cmd) -{ - int result; - - // /quit command -> exit - if (strcmp(cmd, "/quit") == 0) { - result = QUIT_PROF; - - // /help -> print help to console - } else if (strncmp(cmd, "/help", 5) == 0) { - cons_help(); - inp_clear(); - result = CONTINUE_EVENT_LOOP; - - // /who -> request roster - } else if (strncmp(cmd, "/who", 4) == 0) { - jabber_roster_request(); - inp_clear(); - result = CONTINUE_EVENT_LOOP; - - // /msg -> send message to a user - } else if (strncmp(cmd, "/msg ", 5) == 0) { - char *usr_msg = NULL; - char *usr = NULL; - char *msg = NULL; - - usr_msg = strndup(cmd+5, strlen(cmd)-5); - usr = strtok(usr_msg, " "); - if (usr != NULL) { - msg = strndup(cmd+5+strlen(usr)+1, strlen(cmd)-(5+strlen(usr)+1)); - if (msg != NULL) { - jabber_send(msg, usr); - show_outgoing_msg("me", usr, msg); - } - } - inp_clear(); - result = CONTINUE_EVENT_LOOP; - - // /close -> close the current chat window, if in chat - } else if (strncmp(cmd, "/close", 6) == 0) { - if (in_chat()) { - close_win(); - } else { - cons_bad_command(cmd); - } - inp_clear(); - result = CONTINUE_EVENT_LOOP; - - // send message to recipient, if in chat - } else { - if (in_chat()) { - char recipient[100] = ""; - get_recipient(recipient); - jabber_send(cmd, recipient); - show_outgoing_msg("me", recipient, cmd); - } else { - cons_bad_command(cmd); - } - inp_clear(); - result = CONTINUE_EVENT_LOOP; - } - - return result; -}