aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWilmer van der Gaast <wilmer@gaast.net>2007-10-12 01:06:50 +0100
committerWilmer van der Gaast <wilmer@gaast.net>2007-10-12 01:06:50 +0100
commitd444c09e6c7ac6fc3c1686af0e63c09805d8cd00 (patch)
treeb567c3ee431a72ed68713f023c977781f10e23cb
parent285b55d3b38d6e1c8b3fc4a64945f2bb9ace07f4 (diff)
Added word_wrap() function to misc.c and using it at the right places so
that long messages in groupchats also get wrapped properly (instead of truncated).
-rw-r--r--lib/misc.c48
-rw-r--r--lib/misc.h2
-rw-r--r--protocols/nogaim.c46
-rw-r--r--tests/check_util.c58
4 files changed, 121 insertions, 33 deletions
diff --git a/lib/misc.c b/lib/misc.c
index 9061af39..5e385d4a 100644
--- a/lib/misc.c
+++ b/lib/misc.c
@@ -544,3 +544,51 @@ struct ns_srv_reply *srv_lookup( char *service, char *protocol, char *domain )
return reply;
}
+
+/* Word wrapping. Yes, I know this isn't UTF-8 clean. I'm willing to take the risk. */
+char *word_wrap( char *msg, int line_len )
+{
+ GString *ret = g_string_sized_new( strlen( msg ) + 16 );
+
+ while( strlen( msg ) > line_len )
+ {
+ int i;
+
+ /* First try to find out if there's a newline already. Don't
+ want to add more splits than necessary. */
+ for( i = line_len; i > 0 && msg[i] != '\n'; i -- );
+ if( msg[i] == '\n' )
+ {
+ g_string_append_len( ret, msg, i + 1 );
+ msg += i + 1;
+ continue;
+ }
+
+ for( i = line_len; i > 0; i -- )
+ {
+ if( msg[i] == '-' )
+ {
+ g_string_append_len( ret, msg, i + 1 );
+ g_string_append_c( ret, '\n' );
+ msg += i + 1;
+ break;
+ }
+ else if( msg[i] == ' ' )
+ {
+ g_string_append_len( ret, msg, i );
+ g_string_append_c( ret, '\n' );
+ msg += i + 1;
+ break;
+ }
+ }
+ if( i == 0 )
+ {
+ g_string_append_len( ret, msg, line_len );
+ g_string_append_c( ret, '\n' );
+ msg += line_len;
+ }
+ }
+ g_string_append( ret, msg );
+
+ return g_string_free( ret, FALSE );
+}
diff --git a/lib/misc.h b/lib/misc.h
index 55dabfc4..1d76f7f2 100644
--- a/lib/misc.h
+++ b/lib/misc.h
@@ -63,4 +63,6 @@ G_MODULE_EXPORT int bool2int( char *value );
G_MODULE_EXPORT struct ns_srv_reply *srv_lookup( char *service, char *protocol, char *domain );
+G_MODULE_EXPORT char *word_wrap( char *msg, int line_len );
+
#endif
diff --git a/protocols/nogaim.c b/protocols/nogaim.c
index d7b26b74..d90870ad 100644
--- a/protocols/nogaim.c
+++ b/protocols/nogaim.c
@@ -605,6 +605,7 @@ void imcb_buddy_status( struct im_connection *ic, const char *handle, int flags,
void imcb_buddy_msg( struct im_connection *ic, char *handle, char *msg, u_int32_t flags, time_t sent_at )
{
irc_t *irc = ic->irc;
+ char *wrapped;
user_t *u;
u = user_findhandle( ic, handle );
@@ -647,37 +648,9 @@ void imcb_buddy_msg( struct im_connection *ic, char *handle, char *msg, u_int32_
( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->irc->set, "strip_html" ) ) )
strip_html( msg );
- while( strlen( msg ) > 425 )
- {
- char tmp, *nl;
-
- tmp = msg[425];
- msg[425] = 0;
-
- /* If there's a newline/space in this string, split up there,
- looks a bit prettier. */
- if( ( nl = strrchr( msg, '\n' ) ) || ( nl = strrchr( msg, ' ' ) ) )
- {
- msg[425] = tmp;
- tmp = *nl;
- *nl = 0;
- }
-
- irc_msgfrom( irc, u->nick, msg );
-
- /* Move on. */
- if( nl )
- {
- *nl = tmp;
- msg = nl + 1;
- }
- else
- {
- msg[425] = tmp;
- msg += 425;
- }
- }
- irc_msgfrom( irc, u->nick, msg );
+ wrapped = word_wrap( msg, 425 );
+ irc_msgfrom( irc, u->nick, wrapped );
+ g_free( wrapped );
}
void imcb_buddy_typing( struct im_connection *ic, char *handle, u_int32_t flags )
@@ -736,6 +709,7 @@ void imcb_chat_removed( struct groupchat *c )
void imcb_chat_msg( struct groupchat *c, char *who, char *msg, u_int32_t flags, time_t sent_at )
{
struct im_connection *ic = c->ic;
+ char *wrapped;
user_t *u;
/* Gaim sends own messages through this too. IRC doesn't want this, so kill them */
@@ -748,10 +722,16 @@ void imcb_chat_msg( struct groupchat *c, char *who, char *msg, u_int32_t flags,
( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->irc->set, "strip_html" ) ) )
strip_html( msg );
+ wrapped = word_wrap( msg, 425 );
if( c && u )
- irc_privmsg( ic->irc, u, "PRIVMSG", c->channel, "", msg );
+ {
+ irc_privmsg( ic->irc, u, "PRIVMSG", c->channel, "", wrapped );
+ }
else
- imcb_log( ic, "Message from/to conversation %s@0x%x (unknown conv/user): %s", who, (int) c, msg );
+ {
+ imcb_log( ic, "Message from/to conversation %s@0x%x (unknown conv/user): %s", who, (int) c, wrapped );
+ }
+ g_free( wrapped );
}
struct groupchat *imcb_chat_new( struct im_connection *ic, char *handle )
diff --git a/tests/check_util.c b/tests/check_util.c
index 284ddba3..b00d645b 100644
--- a/tests/check_util.c
+++ b/tests/check_util.c
@@ -103,6 +103,63 @@ START_TEST(test_set_url_username_pwd)
fail_unless (url.port == 1080);
END_TEST
+struct
+{
+ char *orig;
+ int line_len;
+ char *wrapped;
+} word_wrap_tests[] = {
+ {
+ "Line-wrapping is not as easy as it seems?",
+ 16,
+ "Line-wrapping is\nnot as easy as\nit seems?"
+ },
+ {
+ "Line-wrapping is not as easy as it seems?",
+ 8,
+ "Line-\nwrapping\nis not\nas easy\nas it\nseems?"
+ },
+ {
+ "Line-wrapping is\nnot as easy as it seems?",
+ 8,
+ "Line-\nwrapping\nis\nnot as\neasy as\nit\nseems?"
+ },
+ {
+ "a aa aaa aaaa aaaaa aaaaaa aaaaaaa aaaaaaaa",
+ 5,
+ "a aa\naaa\naaaa\naaaaa\naaaaa\na\naaaaa\naa\naaaaa\naaa",
+ },
+ {
+ "aaaaaaaa aaaaaaa aaaaaa aaaaa aaaa aaa aa a",
+ 5,
+ "aaaaa\naaa\naaaaa\naa\naaaaa\na\naaaaa\naaaa\naaa\naa a",
+ },
+ {
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
+ 5,
+ "aaaaa\naaaaa\naaaaa\naaaaa\naaaaa\naaaaa\naaaaa\na",
+ },
+ {
+ NULL
+ }
+};
+
+START_TEST(test_word_wrap)
+ int i;
+
+ for( i = 0; word_wrap_tests[i].orig && *word_wrap_tests[i].orig; i ++ )
+ {
+ char *wrapped = word_wrap( word_wrap_tests[i].orig, word_wrap_tests[i].line_len );
+
+ fail_unless( strcmp( word_wrap_tests[i].wrapped, wrapped ) == 0,
+ "%s (line_len = %d) should wrap to `%s', not to `%s'",
+ word_wrap_tests[i].orig, word_wrap_tests[i].line_len,
+ word_wrap_tests[i].wrapped, wrapped );
+
+ g_free( wrapped );
+ }
+END_TEST
+
Suite *util_suite (void)
{
Suite *s = suite_create("Util");
@@ -115,5 +172,6 @@ Suite *util_suite (void)
tcase_add_test (tc_core, test_set_url_port);
tcase_add_test (tc_core, test_set_url_username);
tcase_add_test (tc_core, test_set_url_username_pwd);
+ tcase_add_test (tc_core, test_word_wrap);
return s;
}