Ticket #1044: 0001-Add-limit-param-to-split_command_parts-fix-twitter-q.patch
File 0001-Add-limit-param-to-split_command_parts-fix-twitter-q.patch, 5.8 KB (added by , at 2014-06-23T10:23:33Z) |
---|
-
irc_commands.c
From 5077e5607b08a28d008d908594222f4820fcd66b Mon Sep 17 00:00:00 2001 From: dequis <dx@dxzone.com.ar> Date: Mon, 23 Jun 2014 06:47:07 -0300 Subject: [PATCH] Add limit param to split_command_parts(), fix twitter quotes bug Only took me a few months to write. I even added a test case. --- irc_commands.c | 2 +- lib/misc.c | 9 +++++++-- lib/misc.h | 2 +- protocols/twitter/twitter.c | 4 ++-- root_commands.c | 2 +- tests/check_util.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 57 insertions(+), 7 deletions(-) diff --git a/irc_commands.c b/irc_commands.c index 1993012..73b781f 100644
a b static void irc_cmd_privmsg( irc_t *irc, char **cmd ) 364 364 if( cmd[2][strlen(cmd[2])-1] == '\001' ) 365 365 cmd[2][strlen(cmd[2])-1] = '\0'; 366 366 367 ctcp = split_command_parts( cmd[2] + 1 );367 ctcp = split_command_parts( cmd[2] + 1, 0 ); 368 368 iu->f->ctcp( iu, ctcp ); 369 369 } 370 370 else if( iu->f->privmsg ) -
lib/misc.c
diff --git a/lib/misc.c b/lib/misc.c index 49c4aa5..2506e29 100644
a b int md5_verify_password( char *password, char *hash ) 681 681 /* Split commands (root-style, *not* IRC-style). Handles "quoting of" 682 682 white\ space in 'various ways'. Returns a NULL-terminated static 683 683 char** so watch out with nested use! Definitely not thread-safe. */ 684 char **split_command_parts( char *command )684 char **split_command_parts( char *command, int limit ) 685 685 { 686 686 static char *cmd[IRC_MAX_ARGS+1]; 687 687 char *s, q = 0; … … char **split_command_parts( char *command ) 691 691 cmd[0] = command; 692 692 k = 1; 693 693 for( s = command; *s && k < IRC_MAX_ARGS; s ++ ) 694 { 694 695 if( *s == ' ' && !q ) 695 696 { 696 697 *s = 0; 697 698 while( *++s == ' ' ); 698 if( *s == '"' || *s == '\'')699 if( k != limit && (*s == '"' || *s == '\'') ) 699 700 { 700 701 q = *s; 701 702 s ++; … … char **split_command_parts( char *command ) 703 704 if( *s ) 704 705 { 705 706 cmd[k++] = s; 707 if (limit && k > limit) { 708 break; 709 } 706 710 s --; 707 711 } 708 712 else … … char **split_command_parts( char *command ) 721 725 { 722 726 q = *s = 0; 723 727 } 728 } 724 729 725 730 /* Full zero-padding for easier argc checking. */ 726 731 while( k <= IRC_MAX_ARGS ) -
lib/misc.h
diff --git a/lib/misc.h b/lib/misc.h index 059c823..674a61b 100644
a b G_MODULE_EXPORT void srv_free( struct ns_srv_reply **srv ); 66 66 G_MODULE_EXPORT char *word_wrap( const char *msg, int line_len ); 67 67 G_MODULE_EXPORT gboolean ssl_sockerr_again( void *ssl ); 68 68 G_MODULE_EXPORT int md5_verify_password( char *password, char *hash ); 69 G_MODULE_EXPORT char **split_command_parts( char *command );69 G_MODULE_EXPORT char **split_command_parts( char *command, int limit ); 70 70 G_MODULE_EXPORT char *get_rfc822_header( const char *text, const char *header, int len ); 71 71 72 72 #endif -
protocols/twitter/twitter.c
diff --git a/protocols/twitter/twitter.c b/protocols/twitter/twitter.c index 70e1106..5aefc00 100644
a b static void twitter_handle_command(struct im_connection *ic, char *message) 601 601 bee_user_t *bu = NULL; 602 602 603 603 cmds = g_strdup(message); 604 cmd = split_command_parts(cmds );604 cmd = split_command_parts(cmds, 2); 605 605 606 606 if (cmd[0] == NULL) { 607 607 goto eof; … … static void twitter_handle_command(struct im_connection *ic, char *message) 661 661 "post any statuses recently", cmd[1]); 662 662 goto eof; 663 663 } 664 message = new = g_strdup_printf("@%s %s", bu->handle, message + (cmd[2] - cmd[0]));664 message = new = g_strdup_printf("@%s %s", bu->handle, cmd[2]); 665 665 in_reply_to = id; 666 666 allow_post = TRUE; 667 667 } else if (g_strcasecmp(cmd[0], "post") == 0) { -
root_commands.c
diff --git a/root_commands.c b/root_commands.c index 2c153f0..ca40323 100644
a b 31 31 32 32 void root_command_string( irc_t *irc, char *command ) 33 33 { 34 root_command( irc, split_command_parts( command ) );34 root_command( irc, split_command_parts( command, 0 ) ); 35 35 } 36 36 37 37 #define MIN_ARGS( x, y... ) \ -
tests/check_util.c
diff --git a/tests/check_util.c b/tests/check_util.c index c323241..dc73d64 100644
a b START_TEST(test_http_encode) 168 168 fail_unless( strcmp( s, "ee%C3%ABee%21%21..." ) == 0 ); 169 169 END_TEST 170 170 171 struct { 172 int limit; 173 char *command; 174 char *expected[IRC_MAX_ARGS+1]; 175 } split_tests[] = { 176 { 177 0, "account add etc \"user name with spaces\" 'pass\\ word'", 178 {"account", "add", "etc", "user name with spaces", "pass\\ word", NULL}, 179 }, 180 { 181 0, "channel set group Close\\ friends", 182 {"channel", "set", "group", "Close friends", NULL}, 183 }, 184 { 185 2, "reply wilmer \"testing in C is a PITA\", you said.", 186 {"reply", "wilmer", "\"testing in C is a PITA\", you said.", NULL}, 187 }, 188 { 189 4, "one space two spaces limit limit", 190 {"one", "space", "two", "spaces", "limit limit", NULL}, 191 }, 192 { 193 0, NULL, 194 {NULL} 195 }, 196 }; 197 198 START_TEST(test_split_command_parts) 199 int i; 200 for (i = 0; split_tests[i].command; i++) { 201 char *cmd = g_strdup(split_tests[i].command); 202 char **split = split_command_parts(cmd, split_tests[i].limit); 203 char **expected = split_tests[i].expected; 204 205 int j; 206 for (j = 0; split[j] && expected[j]; j++) { 207 fail_unless (strcmp(split[j], expected[j]) == 0, 208 "(%d) split_command_parts broken: split(\"%s\")[%d] -> %s (expected: %s)", 209 i, split_tests[i].command, j, split[j], expected[j]); 210 } 211 g_free(cmd); 212 } 213 END_TEST 214 171 215 Suite *util_suite (void) 172 216 { 173 217 Suite *s = suite_create("Util"); … … Suite *util_suite (void) 182 226 tcase_add_test (tc_core, test_set_url_username_pwd); 183 227 tcase_add_test (tc_core, test_word_wrap); 184 228 tcase_add_test (tc_core, test_http_encode); 229 tcase_add_test (tc_core, test_split_command_parts); 185 230 return s; 186 231 }