1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

Handle ampersand

Replaced with & in messages
This commit is contained in:
James Booth 2012-04-19 22:26:12 +01:00
parent 9805b2b2b2
commit 366eecc195
7 changed files with 118 additions and 3 deletions

View File

@ -6,7 +6,8 @@ CPPLIB = -lstdc++
CFLAGS = -I ~/include -O3 $(WARNS) $(LIBS) CFLAGS = -I ~/include -O3 $(WARNS) $(LIBS)
OBJS = log.o windows.o title_bar.o status_bar.o input_win.o jabber.o \ 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 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) profanity: $(OBJS)
$(CC) -o profanity $(OBJS) $(LIBS) $(CC) -o profanity $(OBJS) $(LIBS)
@ -26,6 +27,7 @@ main.o: profanity.h
test_history.o: history.h test_history.o: history.h
test_contact_list.o: contact_list.h test_contact_list.o: contact_list.h
test_util.o: util.h
testsuite: testsuite.h $(TESTOBJS) testsuite: testsuite.h $(TESTOBJS)
$(CC) $(CFLAGS) $(CPPLIB) testsuite.c $(TESTOBJS) -o testsuite $(TESTLIB) $(CC) $(CFLAGS) $(CPPLIB) testsuite.c $(TESTOBJS) -o testsuite $(TESTLIB)

View File

@ -27,6 +27,7 @@
#include "log.h" #include "log.h"
#include "contact_list.h" #include "contact_list.h"
#include "windows.h" #include "windows.h"
#include "util.h"
#define PING_INTERVAL 120000 // 2 minutes #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) void jabber_send(const char * const msg, const char * const recipient)
{ {
char *coded_msg = str_replace(msg, "&", "&");
xmpp_stanza_t *reply, *body, *text; xmpp_stanza_t *reply, *body, *text;
reply = xmpp_stanza_new(jabber_conn.ctx); 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"); xmpp_stanza_set_name(body, "body");
text = xmpp_stanza_new(jabber_conn.ctx); 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(body, text);
xmpp_stanza_add_child(reply, body); xmpp_stanza_add_child(reply, body);

68
test_util.c Normal file
View File

@ -0,0 +1,68 @@
#include <stdlib.h>
#include <head-unit.h>
#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 = "&amp;";
char *result = str_replace(string, sub, new);
assert_string_equals("some &amp; a thing &amp; 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);
}

View File

@ -5,6 +5,7 @@ int main(void)
{ {
register_history_tests(); register_history_tests();
register_contact_list_tests(); register_contact_list_tests();
register_util_tests();
run_suite(); run_suite();
return 0; return 0;
} }

View File

@ -3,5 +3,6 @@
void register_history_tests(void); void register_history_tests(void);
void register_contact_list_tests(void); void register_contact_list_tests(void);
void register_util_tests(void);
#endif #endif

38
util.c
View File

@ -23,6 +23,7 @@
#include <time.h> #include <time.h>
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
#include <stdlib.h>
void get_time(char *thetime) void get_time(char *thetime)
{ {
@ -54,3 +55,40 @@ char *trim(char *str)
return 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;
}

4
util.h
View File

@ -24,6 +24,8 @@
#define UTIL_H #define UTIL_H
void get_time(char *thetime); 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 #endif