diff --git a/Makefile b/Makefile index e6304e78..7102b0be 100644 --- a/Makefile +++ b/Makefile @@ -5,8 +5,8 @@ TESTLIB = -L ~/lib -l headunit CPPLIB = -lstdc++ CFLAGS = -I ~/include -O3 $(WARNS) $(LIBS) OBJS = log.o windows.o title_bar.o status_bar.o input_win.o jabber.o \ - profanity.o util.o command.o history.o main.o -TESTOBJS = test_history.o history.o + profanity.o util.o command.o history.o contact_list.o main.o +TESTOBJS = test_history.o history.o test_contact_list.o contact_list.o profanity: $(OBJS) $(CC) -o profanity $(OBJS) $(LIBS) @@ -16,14 +16,16 @@ windows.o: windows.h util.h title_bar.o: windows.h status_bar.o: windows.h util.h input_win.o: windows.h -jabber.o: jabber.h log.h windows.h +jabber.o: jabber.h log.h windows.h contact_list.h profanity.o: log.h windows.h jabber.h command.h history.h util.o: util.h -command.o: command.h util.h history.h +command.o: command.h util.h history.h contact_list.h history.o: history.h +contact_list.o: contact_list.h main.o: profanity.h test_history.o: history.h +test_contact_list.o: contact_list.h testsuite: testsuite.h $(TESTOBJS) $(CC) $(CFLAGS) $(CPPLIB) testsuite.c $(TESTOBJS) -o testsuite $(TESTLIB) diff --git a/command.c b/command.c index c3113e44..41792315 100644 --- a/command.c +++ b/command.c @@ -24,6 +24,7 @@ #include #include #include "command.h" +#include "contact_list.h" #include "history.h" #include "jabber.h" #include "windows.h" @@ -33,6 +34,7 @@ static int _handle_command(char *command, char *inp); static int _cmd_quit(void); static int _cmd_help(void); static int _cmd_who(void); +static int _cmd_pres(void); static int _cmd_connect(char *inp); static int _cmd_msg(char *inp); static int _cmd_close(char *inp); @@ -72,6 +74,8 @@ static int _handle_command(char *command, char *inp) result = _cmd_help(); } else if (strcmp(command, "/who") == 0) { result = _cmd_who(); + } else if (strcmp(command, "/pres") == 0) { + result = _cmd_pres(); } else if (strcmp(command, "/msg") == 0) { result = _cmd_msg(inp); } else if (strcmp(command, "/close") == 0) { @@ -142,6 +146,19 @@ static int _cmd_who(void) return TRUE; } +static int _cmd_pres(void) +{ + struct contact_list *list = get_contact_list(); + + int i; + for (i = 0; i < list->size; i++) { + char *contact = list->contacts[i]; + cons_show(contact); + } + + return TRUE; +} + static int _cmd_msg(char *inp) { char *usr = NULL; diff --git a/contact_list.c b/contact_list.c new file mode 100644 index 00000000..e8de88f1 --- /dev/null +++ b/contact_list.c @@ -0,0 +1,103 @@ +#include +#include + +#include "contact_list.h" + +// internal contact list +struct _contact_t { + char *contact; + struct _contact_t *next; + int last; +}; + +// the contact list +static struct _contact_t *_contact_list = NULL; + +void contact_list_clear(void) +{ + if (_contact_list == NULL) { + return; + } + else { + struct _contact_t *curr = _contact_list; + while(!curr->last) { + free(curr->contact); + curr = curr->next; + } + free(_contact_list); + } + + _contact_list = NULL; +} + +int contact_list_add(char *contact) +{ + if (_contact_list == NULL) { + // create the new one + _contact_list = (struct _contact_t *) malloc(sizeof(struct _contact_t)); + _contact_list->contact = + (char *) malloc((strlen(contact)+1) * sizeof(char)); + strcpy(_contact_list->contact, contact); + _contact_list->last = 1; + + // complete the circle + _contact_list->next = _contact_list; + + } else { + // go through entries + struct _contact_t *curr = _contact_list; + while (!curr->last) { + // if already there, return without adding + if (strcmp(curr->contact, contact) == 0) + return 1; + + // move on + curr = curr->next; + } + + // create the new one + struct _contact_t *new = + (struct _contact_t *) malloc(sizeof(struct _contact_t)); + new->contact = (char *) malloc((strlen(contact)+1) * sizeof(char)); + strcpy(new->contact, contact); + new->last = 1; + + // point the old one to it + curr->next = new; + curr->last = 0; + } + + return 0; +} + +struct contact_list *get_contact_list(void) +{ + struct contact_list *list = + (struct contact_list *) malloc(sizeof(struct contact_list)); + + if (_contact_list == NULL) { + list->contacts = NULL; + list->size = 0; + return list; + } else { + struct _contact_t *curr = _contact_list; + list->contacts = (char **) malloc(sizeof(char **)); + int count = 0; + while(1) { + list->contacts[count] = + (char *) malloc((strlen(curr->contact)+1) * sizeof(char)); + strcpy(list->contacts[count], curr->contact); + count++; + + if (curr->last) + break; + else { + curr = curr->next; + } + } + + list->size = count; + + return list; + } +} diff --git a/contact_list.h b/contact_list.h new file mode 100644 index 00000000..13efe3d9 --- /dev/null +++ b/contact_list.h @@ -0,0 +1,13 @@ +#ifndef CONTACT_LIST_H +#define CONTACT_LIST_H + +struct contact_list { + char **contacts; + int size; +}; + +void contact_list_clear(void); +int contact_list_add(char *contact); +struct contact_list *get_contact_list(void); + +#endif diff --git a/jabber.c b/jabber.c index cd291858..a77117dc 100644 --- a/jabber.c +++ b/jabber.c @@ -25,6 +25,7 @@ #include "jabber.h" #include "log.h" +#include "contact_list.h" #include "windows.h" #define PING_INTERVAL 120000 // 2 minutes @@ -310,10 +311,12 @@ static int _jabber_presence_handler(xmpp_conn_t * const conn, else status_str = NULL; - if (type == NULL) // online + if (type == NULL) {// online win_contact_online(short_from, show_str, status_str); - else // offline + contact_list_add(short_from); + } else {// offline win_contact_offline(short_from, show_str, status_str); + } win_page_off(); diff --git a/test_contact_list.c b/test_contact_list.c new file mode 100644 index 00000000..598e7485 --- /dev/null +++ b/test_contact_list.c @@ -0,0 +1,58 @@ +#include +#include "contact_list.h" + +static void beforetest(void) +{ + contact_list_clear(); +} + +static void empty_list_when_none_added(void) +{ + struct contact_list *list = get_contact_list(); + assert_int_equals(0, list->size); +} + +static void contains_one_element(void) +{ + contact_list_add("James"); + struct contact_list *list = get_contact_list(); + assert_int_equals(1, list->size); +} + +static void first_element_correct(void) +{ + contact_list_add("James"); + struct contact_list *list = get_contact_list(); + + assert_string_equals("James", list->contacts[0]); +} + +static void contains_two_elements(void) +{ + contact_list_add("James"); + contact_list_add("Dave"); + struct contact_list *list = get_contact_list(); + + assert_int_equals(2, list->size); +} + +static void first_and_second_elements_correct(void) +{ + contact_list_add("James"); + contact_list_add("Dave"); + struct contact_list *list = get_contact_list(); + + assert_string_equals("James", list->contacts[0]); + assert_string_equals("Dave", list->contacts[1]); +} + +void register_contact_list_tests(void) +{ + TEST_MODULE("contact_list tests"); + BEFORETEST(beforetest); + TEST(empty_list_when_none_added); + TEST(contains_one_element); + TEST(first_element_correct); + TEST(contains_two_elements); + TEST(first_and_second_elements_correct); +} diff --git a/test_history.c b/test_history.c index 8e1becc9..f455d7d1 100644 --- a/test_history.c +++ b/test_history.c @@ -29,7 +29,7 @@ static void append_then_previous_returns_appended(void) { history_append("try append"); char *prev = history_previous(); - assert_string_equals(prev, "try append"); + assert_string_equals("try append", prev); } static void append_then_next_returns_null(void) @@ -67,7 +67,7 @@ static void navigate_to_correct_item(void) history_previous(); // cmd2 char *str = history_next(); // cmd3 - assert_string_equals(str, "cmd3"); + assert_string_equals("cmd3", str); } static void append_previous_item(void) @@ -91,25 +91,25 @@ static void append_previous_item(void) history_append(str); char *cmd3_1 = history_previous(); - assert_string_equals(cmd3_1, "cmd3"); + assert_string_equals("cmd3", cmd3_1); char *cmd6 = history_previous(); - assert_string_equals(cmd6, "cmd6"); + assert_string_equals("cmd6", cmd6); char *cmd5 = history_previous(); - assert_string_equals(cmd5, "cmd5"); + assert_string_equals("cmd5", cmd5); char *cmd4 = history_previous(); - assert_string_equals(cmd4, "cmd4"); + assert_string_equals("cmd4", cmd4); char *cmd3 = history_previous(); - assert_string_equals(cmd3, "cmd3"); + assert_string_equals("cmd3", cmd3); char *cmd2 = history_previous(); - assert_string_equals(cmd2, "cmd2"); + assert_string_equals("cmd2", cmd2); char *cmd1 = history_previous(); - assert_string_equals(cmd1, "cmd1"); + assert_string_equals("cmd1", cmd1); char *end = history_previous(); assert_is_null(end); diff --git a/testsuite.c b/testsuite.c index af9b6c73..bcae9a91 100644 --- a/testsuite.c +++ b/testsuite.c @@ -4,6 +4,7 @@ int main(void) { register_history_tests(); + register_contact_list_tests(); run_suite(); return 0; } diff --git a/testsuite.h b/testsuite.h index 0e67be19..9a326a44 100644 --- a/testsuite.h +++ b/testsuite.h @@ -2,5 +2,6 @@ #define TESTSUITE_H void register_history_tests(void); +void register_contact_list_tests(void); #endif