From 366eecc19569f14a6081e1acb6efd72a372982da Mon Sep 17 00:00:00 2001 From: James Booth Date: Thu, 19 Apr 2012 22:26:12 +0100 Subject: [PATCH] Handle ampersand Replaced with & in messages --- Makefile | 4 +++- jabber.c | 5 +++- test_util.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++ testsuite.c | 1 + testsuite.h | 1 + util.c | 38 ++++++++++++++++++++++++++++++ util.h | 4 +++- 7 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 test_util.c 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 fbd0ccdd..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); diff --git a/test_util.c b/test_util.c new file mode 100644 index 00000000..e2327be5 --- /dev/null +++ b/test_util.c @@ -0,0 +1,68 @@ +#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 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); +} 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..1452d357 100644 --- a/util.c +++ b/util.c @@ -23,6 +23,7 @@ #include #include #include +#include void get_time(char *thetime) { @@ -54,3 +55,40 @@ 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 ( substr == NULL || replacement == NULL ) + 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