diff --git a/Makefile b/Makefile index 98a029d5..91e90388 100644 --- a/Makefile +++ b/Makefile @@ -2,8 +2,8 @@ CC = gcc 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 app.o \ - profanity.o util.o +OBJS = log.o windows.o title_bar.o input_bar.o input_win.o jabber.o \ + profanity.o util.o main.o profanity: $(OBJS) $(CC) -o profanity $(OBJS) $(LIBS) @@ -14,9 +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 -app.o: log.h windows.h jabber.h +profanity.o: log.h windows.h jabber.h util.o: util.h -profanity.o: log.h windows.h app.h +main.o: log.h windows.h profanity.h .PHONY: clean clean: diff --git a/app.c b/app.c deleted file mode 100644 index 374f92d4..00000000 --- a/app.c +++ /dev/null @@ -1,166 +0,0 @@ -#include -#include - -#include - -#include "log.h" -#include "windows.h" -#include "jabber.h" - -#define AWAIT_COMMAND 1 -#define START_MAIN 2 -#define QUIT_PROF 3 - -static void main_event_loop(void); -static int handle_pre_command(char *cmd); - -void start_profanity(void) -{ - int cmd_result = AWAIT_COMMAND; - char cmd[50]; - - while (cmd_result == AWAIT_COMMAND) { - inp_get_command_str(cmd); - cmd_result = handle_pre_command(cmd); - } - - if (cmd_result == START_MAIN) { - main_event_loop(); - } -} - -static int handle_pre_command(char *cmd) -{ - int result; - - if (strcmp(cmd, "/quit") == 0) { - result = QUIT_PROF; - } else if (strncmp(cmd, "/help", 5) == 0) { - cons_help(); - inp_clear(); - result = AWAIT_COMMAND; - } else if (strncmp(cmd, "/connect ", 9) == 0) { - char *user; - user = strndup(cmd+9, strlen(cmd)-9); - - inp_bar_print_message("Enter password:"); - char passwd[20]; - inp_get_password(passwd); - - inp_bar_print_message(user); - jabber_connect(user, passwd); - result = START_MAIN; - } else { - cons_bad_command(cmd); - inp_clear(); - result = AWAIT_COMMAND; - } - - return result; -} - -static void main_event_loop(void) -{ - inp_non_block(); - - while(TRUE) { - int ch = ERR; - char command[100]; - int size = 0; - - // while not enter, process all events, and try to get another char - while(ch != '\n') { - usleep(1); - - // handle incoming messages - jabber_process_events(); - - // determine if they changed windows - if (ch == KEY_F(1)) { - switch_to(0); - } else if (ch == KEY_F(2)) { - switch_to(1); - } else if (ch == KEY_F(3)) { - switch_to(2); - } else if (ch == KEY_F(4)) { - switch_to(3); - } else if (ch == KEY_F(5)) { - switch_to(4); - } else if (ch == KEY_F(6)) { - switch_to(5); - } else if (ch == KEY_F(7)) { - switch_to(6); - } else if (ch == KEY_F(8)) { - switch_to(7); - } else if (ch == KEY_F(9)) { - switch_to(8); - } else if (ch == KEY_F(10)) { - switch_to(9); - } - - // get another character from the command box - inp_poll_char(&ch, command, &size); - } - - // null terminate the input - command[size++] = '\0'; - - // deal with input - - // /quit command -> exit - if (strcmp(command, "/quit") == 0) { - break; - - // /help -> print help to console - } else if (strncmp(command, "/help", 5) == 0) { - cons_help(); - inp_clear(); - - // /who -> request roster - } else if (strncmp(command, "/who", 4) == 0) { - jabber_roster_request(); - inp_clear(); - - // /msg -> send message to a user - } else if (strncmp(command, "/msg ", 5) == 0) { - char *usr_msg = NULL; - char *usr = NULL; - char *msg = NULL; - - usr_msg = strndup(command+5, strlen(command)-5); - usr = strtok(usr_msg, " "); - if (usr != NULL) { - msg = strndup(command+5+strlen(usr)+1, strlen(command)-(5+strlen(usr)+1)); - if (msg != NULL) { - jabber_send(msg, usr); - show_outgoing_msg("me", usr, msg); - } - } - inp_clear(); - - // /close -> close the current chat window, if in chat - } else if (strncmp(command, "/close", 6) == 0) { - if (in_chat()) { - close_win(); - } else { - cons_bad_command(command); - } - inp_clear(); - - // send message to recipient, if in chat - } else { - if (in_chat()) { - char recipient[100] = ""; - get_recipient(recipient); - jabber_send(command, recipient); - show_outgoing_msg("me", recipient, command); - } else { - cons_bad_command(command); - } - inp_clear(); - } - } - - jabber_disconnect(); -} - diff --git a/app.h b/app.h deleted file mode 100644 index 6019742d..00000000 --- a/app.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef APP_H -#define APP_H - -void start_profanity(void); - -#endif diff --git a/main.c b/main.c new file mode 100644 index 00000000..8cac8539 --- /dev/null +++ b/main.c @@ -0,0 +1,16 @@ +#include "log.h" +#include "windows.h" +#include "profanity.h" + +int main(void) +{ + log_init(); + gui_init(); + + profanity_start(); + + gui_close(); + log_close(); + + return 0; +} diff --git a/profanity.c b/profanity.c index 7ca3e52c..5618a959 100644 --- a/profanity.c +++ b/profanity.c @@ -1,16 +1,167 @@ +#include +#include + +#include + +#include "profanity.h" #include "log.h" #include "windows.h" -#include "app.h" +#include "jabber.h" -int main(void) -{ - log_init(); - gui_init(); +#define AWAIT_COMMAND 1 +#define START_MAIN 2 +#define QUIT_PROF 3 - start_profanity(); - - gui_close(); - log_close(); +static void profanity_main(void); +static int handle_pre_command(char *cmd); - return 0; +void profanity_start(void) +{ + int cmd_result = AWAIT_COMMAND; + char cmd[50]; + + while (cmd_result == AWAIT_COMMAND) { + inp_get_command_str(cmd); + cmd_result = handle_pre_command(cmd); + } + + if (cmd_result == START_MAIN) { + profanity_main(); + } } + +static int handle_pre_command(char *cmd) +{ + int result; + + if (strcmp(cmd, "/quit") == 0) { + result = QUIT_PROF; + } else if (strncmp(cmd, "/help", 5) == 0) { + cons_help(); + inp_clear(); + result = AWAIT_COMMAND; + } else if (strncmp(cmd, "/connect ", 9) == 0) { + char *user; + user = strndup(cmd+9, strlen(cmd)-9); + + inp_bar_print_message("Enter password:"); + char passwd[20]; + inp_get_password(passwd); + + inp_bar_print_message(user); + jabber_connect(user, passwd); + result = START_MAIN; + } else { + cons_bad_command(cmd); + inp_clear(); + result = AWAIT_COMMAND; + } + + return result; +} + +static void profanity_main(void) +{ + inp_non_block(); + + while(TRUE) { + int ch = ERR; + char command[100]; + int size = 0; + + // while not enter, process all events, and try to get another char + while(ch != '\n') { + usleep(1); + + // handle incoming messages + jabber_process_events(); + + // determine if they changed windows + if (ch == KEY_F(1)) { + switch_to(0); + } else if (ch == KEY_F(2)) { + switch_to(1); + } else if (ch == KEY_F(3)) { + switch_to(2); + } else if (ch == KEY_F(4)) { + switch_to(3); + } else if (ch == KEY_F(5)) { + switch_to(4); + } else if (ch == KEY_F(6)) { + switch_to(5); + } else if (ch == KEY_F(7)) { + switch_to(6); + } else if (ch == KEY_F(8)) { + switch_to(7); + } else if (ch == KEY_F(9)) { + switch_to(8); + } else if (ch == KEY_F(10)) { + switch_to(9); + } + + // get another character from the command box + inp_poll_char(&ch, command, &size); + } + + // null terminate the input + command[size++] = '\0'; + + // deal with input + + // /quit command -> exit + if (strcmp(command, "/quit") == 0) { + break; + + // /help -> print help to console + } else if (strncmp(command, "/help", 5) == 0) { + cons_help(); + inp_clear(); + + // /who -> request roster + } else if (strncmp(command, "/who", 4) == 0) { + jabber_roster_request(); + inp_clear(); + + // /msg -> send message to a user + } else if (strncmp(command, "/msg ", 5) == 0) { + char *usr_msg = NULL; + char *usr = NULL; + char *msg = NULL; + + usr_msg = strndup(command+5, strlen(command)-5); + usr = strtok(usr_msg, " "); + if (usr != NULL) { + msg = strndup(command+5+strlen(usr)+1, strlen(command)-(5+strlen(usr)+1)); + if (msg != NULL) { + jabber_send(msg, usr); + show_outgoing_msg("me", usr, msg); + } + } + inp_clear(); + + // /close -> close the current chat window, if in chat + } else if (strncmp(command, "/close", 6) == 0) { + if (in_chat()) { + close_win(); + } else { + cons_bad_command(command); + } + inp_clear(); + + // send message to recipient, if in chat + } else { + if (in_chat()) { + char recipient[100] = ""; + get_recipient(recipient); + jabber_send(command, recipient); + show_outgoing_msg("me", recipient, command); + } else { + cons_bad_command(command); + } + inp_clear(); + } + } + + jabber_disconnect(); +} + diff --git a/profanity.h b/profanity.h new file mode 100644 index 00000000..785317a2 --- /dev/null +++ b/profanity.h @@ -0,0 +1,6 @@ +#ifndef PROFANITY_H +#define PROFANITY_H + +void profanity_start(void); + +#endif