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:
parent
9805b2b2b2
commit
366eecc195
4
Makefile
4
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)
|
||||
|
5
jabber.c
5
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);
|
||||
|
||||
|
68
test_util.c
Normal file
68
test_util.c
Normal 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 = "&";
|
||||
|
||||
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);
|
||||
}
|
@ -5,6 +5,7 @@ int main(void)
|
||||
{
|
||||
register_history_tests();
|
||||
register_contact_list_tests();
|
||||
register_util_tests();
|
||||
run_suite();
|
||||
return 0;
|
||||
}
|
||||
|
@ -3,5 +3,6 @@
|
||||
|
||||
void register_history_tests(void);
|
||||
void register_contact_list_tests(void);
|
||||
void register_util_tests(void);
|
||||
|
||||
#endif
|
||||
|
38
util.c
38
util.c
@ -23,6 +23,7 @@
|
||||
#include <time.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user