diff --git a/Makefile b/Makefile index 9020ddd6..706fe2d2 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,8 @@ 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 contact_list.o main.o -TESTOBJS = test_history.o history.o test_contact_list.o contact_list.o +TESTOBJS = test_history.o history.o test_contact_list.o contact_list.o \ + test_util.o util.o profanity: $(OBJS) $(CC) -o profanity $(OBJS) $(LIBS) @@ -26,6 +27,7 @@ main.o: profanity.h test_history.o: history.h test_contact_list.o: contact_list.h +test_util.o: util.h testsuite: testsuite.h $(TESTOBJS) $(CC) $(CFLAGS) $(CPPLIB) testsuite.c $(TESTOBJS) -o testsuite $(TESTLIB) diff --git a/jabber.c b/jabber.c index 10d0115b..8244c257 100644 --- a/jabber.c +++ b/jabber.c @@ -27,6 +27,7 @@ #include "log.h" #include "contact_list.h" #include "windows.h" +#include "util.h" #define PING_INTERVAL 120000 // 2 minutes @@ -133,6 +134,8 @@ void jabber_process_events(void) void jabber_send(const char * const msg, const char * const recipient) { + char *coded_msg = str_replace(msg, "&", "&"); + xmpp_stanza_t *reply, *body, *text; reply = xmpp_stanza_new(jabber_conn.ctx); @@ -144,7 +147,7 @@ void jabber_send(const char * const msg, const char * const recipient) xmpp_stanza_set_name(body, "body"); text = xmpp_stanza_new(jabber_conn.ctx); - xmpp_stanza_set_text(text, msg); + xmpp_stanza_set_text(text, coded_msg); xmpp_stanza_add_child(body, text); xmpp_stanza_add_child(reply, body); @@ -221,7 +224,12 @@ static void _jabber_conn_handler(xmpp_conn_t * const conn, jabber_conn.conn_status = JABBER_CONNECTED; } else { - cons_bad_show("Login failed."); + if (jabber_conn.conn_status == JABBER_CONNECTED) { + cons_bad_show("Lost connection."); + win_disconnected(); + } else { + cons_bad_show("Login failed."); + } win_page_off(); log_msg(CONN, "disconnected"); xmpp_stop(ctx); diff --git a/status_bar.c b/status_bar.c index d15997b4..c343f3fe 100644 --- a/status_bar.c +++ b/status_bar.c @@ -96,6 +96,23 @@ void status_bar_active(const int win) dirty = TRUE; } +void status_bar_new(const int win) +{ + int active_pos = 1 + ((win -1) * 3); + + int rows, cols; + getmaxyx(stdscr, rows, cols); + + wattron(status_bar, A_BLINK); + if (win < 9) + mvwprintw(status_bar, 0, cols - 29 + active_pos, "%d", win+1); + else + mvwprintw(status_bar, 0, cols - 29 + active_pos, "10"); + wattroff(status_bar, A_BLINK); + + dirty = TRUE; +} + void status_bar_get_password(void) { mvwprintw(status_bar, 0, 9, "Enter password:"); diff --git a/test_util.c b/test_util.c new file mode 100644 index 00000000..ae936d9a --- /dev/null +++ b/test_util.c @@ -0,0 +1,164 @@ +#include +#include +#include "util.h" + +void replace_one_substr(void) +{ + char *string = "it is a string"; + char *sub = "is"; + char *new = "was"; + + char *result = str_replace(string, sub, new); + + assert_string_equals("it was a string", result); +} + +void replace_one_substr_beginning(void) +{ + char *string = "it is a string"; + char *sub = "it"; + char *new = "that"; + + char *result = str_replace(string, sub, new); + + assert_string_equals("that is a string", result); +} + +void replace_one_substr_end(void) +{ + char *string = "it is a string"; + char *sub = "string"; + char *new = "thing"; + + char *result = str_replace(string, sub, new); + + assert_string_equals("it is a thing", result); +} + +void replace_two_substr(void) +{ + char *string = "it is a is string"; + char *sub = "is"; + char *new = "was"; + + char *result = str_replace(string, sub, new); + + assert_string_equals("it was a was string", result); +} + +void replace_char(void) +{ + char *string = "some & a thing & something else"; + char *sub = "&"; + char *new = "&"; + + char *result = str_replace(string, sub, new); + + assert_string_equals("some & a thing & something else", result); +} + +void replace_when_none(void) +{ + char *string = "its another string"; + char *sub = "haha"; + char *new = "replaced"; + + char *result = str_replace(string, sub, new); + + assert_string_equals("its another string", result); +} + +void replace_when_match(void) +{ + char *string = "hello"; + char *sub = "hello"; + char *new = "goodbye"; + + char *result = str_replace(string, sub, new); + + assert_string_equals("goodbye", result); +} + +void replace_when_string_empty(void) +{ + char *string = ""; + char *sub = "hello"; + char *new = "goodbye"; + + char *result = str_replace(string, sub, new); + + assert_string_equals("", result); +} + +void replace_when_string_null(void) +{ + char *string = NULL; + char *sub = "hello"; + char *new = "goodbye"; + + char *result = str_replace(string, sub, new); + + assert_is_null(result); +} + +void replace_when_sub_empty(void) +{ + char *string = "hello"; + char *sub = ""; + char *new = "goodbye"; + + char *result = str_replace(string, sub, new); + + assert_string_equals("hello", result); +} + +void replace_when_sub_null(void) +{ + char *string = "hello"; + char *sub = NULL; + char *new = "goodbye"; + + char *result = str_replace(string, sub, new); + + assert_string_equals("hello", result); +} + +void replace_when_new_empty(void) +{ + char *string = "hello"; + char *sub = "hello"; + char *new = ""; + + char *result = str_replace(string, sub, new); + + assert_string_equals("", result); +} + +void replace_when_new_null(void) +{ + char *string = "hello"; + char *sub = "hello"; + char *new = NULL; + + char *result = str_replace(string, sub, new); + + assert_string_equals("hello", result); +} + +void register_util_tests(void) +{ + TEST_MODULE("util tests"); + TEST(replace_one_substr); + TEST(replace_one_substr_beginning); + TEST(replace_one_substr_end); + TEST(replace_two_substr); + TEST(replace_char); + TEST(replace_when_none); + TEST(replace_when_match); + TEST(replace_when_string_empty); + TEST(replace_when_string_null); + TEST(replace_when_sub_empty); + TEST(replace_when_sub_null); + TEST(replace_when_new_empty); + TEST(replace_when_new_null); +} diff --git a/testsuite.c b/testsuite.c index bcae9a91..88c9cdc1 100644 --- a/testsuite.c +++ b/testsuite.c @@ -5,6 +5,7 @@ int main(void) { register_history_tests(); register_contact_list_tests(); + register_util_tests(); run_suite(); return 0; } diff --git a/testsuite.h b/testsuite.h index 9a326a44..2aa34ca5 100644 --- a/testsuite.h +++ b/testsuite.h @@ -3,5 +3,6 @@ void register_history_tests(void); void register_contact_list_tests(void); +void register_util_tests(void); #endif diff --git a/util.c b/util.c index 746bf916..bfe21263 100644 --- a/util.c +++ b/util.c @@ -23,6 +23,7 @@ #include #include #include +#include void get_time(char *thetime) { @@ -54,3 +55,45 @@ char *trim(char *str) return str; } +char * str_replace (const char *string, const char *substr, + const char *replacement) { + char *tok = NULL; + char *newstr = NULL; + char *oldstr = NULL; + char *head = NULL; + + if (string == NULL) + return NULL; + + if ( substr == NULL || + replacement == NULL || + (strcmp(substr, "") == 0)) + return strdup (string); + + newstr = strdup (string); + head = newstr; + + while ( (tok = strstr ( head, substr ))) { + oldstr = newstr; + newstr = malloc ( strlen ( oldstr ) - strlen ( substr ) + + strlen ( replacement ) + 1 ); + + if ( newstr == NULL ) { + free (oldstr); + return NULL; + } + + memcpy ( newstr, oldstr, tok - oldstr ); + memcpy ( newstr + (tok - oldstr), replacement, strlen ( replacement ) ); + memcpy ( newstr + (tok - oldstr) + strlen( replacement ), + tok + strlen ( substr ), + strlen ( oldstr ) - strlen ( substr ) - ( tok - oldstr ) ); + memset ( newstr + strlen ( oldstr ) - strlen ( substr ) + + strlen ( replacement ) , 0, 1 ); + + head = newstr + (tok - oldstr) + strlen( replacement ); + free (oldstr); + } + + return newstr; +} diff --git a/util.h b/util.h index 7d6e4a66..0982f816 100644 --- a/util.h +++ b/util.h @@ -24,6 +24,8 @@ #define UTIL_H void get_time(char *thetime); -char *trim(char *str); +char * trim(char *str); +char * str_replace(const char *string, const char *substr, + const char *replacement); #endif diff --git a/windows.c b/windows.c index 3acda7b7..ec30310f 100644 --- a/windows.c +++ b/windows.c @@ -168,12 +168,15 @@ void win_show_incomming_msg(const char * const from, const char * const message) _win_show_time(win); _win_show_user(win, short_from, 1); _win_show_message(win, message); - - status_bar_active(win_index); - _cons_show_incoming_message(short_from, win_index); - - if (win_index == _curr_prof_win) + + if (win_index == _curr_prof_win) { + status_bar_active(win_index); dirty = TRUE; + } else { + status_bar_new(win_index); + _cons_show_incoming_message(short_from, win_index); + } + } void win_show_outgoing_msg(const char * const from, const char * const to, @@ -190,8 +193,11 @@ void win_show_outgoing_msg(const char * const from, const char * const to, status_bar_active(win_index); - if (win_index == _curr_prof_win) + if (win_index == _curr_prof_win) { dirty = TRUE; + } else { + status_bar_new(win_index); + } } void win_contact_online(const char * const from, const char * const show, @@ -224,6 +230,26 @@ void win_contact_offline(const char * const from, const char * const show, dirty = TRUE; } +void win_disconnected(void) +{ + int i; + // show message in all active chats + for (i = 1; i < NUM_WINS; i++) { + if (strcmp(_wins[i].from, "") != 0) { + WINDOW *win = _wins[_curr_prof_win].win; + _win_show_time(win); + wattron(win, COLOR_PAIR(6)); + wprintw(win, "%s\n", "Lost connection."); + wattroff(win, COLOR_PAIR(6)); + + // if current win, set dirty + if (i == _curr_prof_win) { + dirty = TRUE; + } + } + } +} + void cons_help(void) { cons_show(""); @@ -415,10 +441,12 @@ static void _win_switch_if_active(const int i) _curr_prof_win = i; win_page_off(); - if (i == 0) + if (i == 0) { title_bar_title(); - else + } else { title_bar_show(_wins[i].from); + status_bar_active(i); + } } dirty = TRUE; diff --git a/windows.h b/windows.h index 8d21e906..ed9eabfc 100644 --- a/windows.h +++ b/windows.h @@ -65,6 +65,7 @@ void win_contact_online(const char * const from, const char * const show, const char * const status); void win_contact_offline(const char * const from, const char * const show, const char * const status); +void win_disconnected(void); // console window actions void cons_help(void); @@ -85,6 +86,7 @@ void status_bar_get_password(void); void status_bar_print_message(const char * const msg); void status_bar_inactive(const int win); void status_bar_active(const int win); +void status_bar_new(const int win); void status_bar_update_time(void); // input window actions