Index: irc_commands.c
===================================================================
--- irc_commands.c	(revision 1783ab6964c9a8ffc3488bb5243f0b15858f4e74)
+++ irc_commands.c	(revision 269580c6302a677e07176494bd314c7e2a8f488f)
@@ -365,5 +365,5 @@
 				cmd[2][strlen(cmd[2])-1] = '\0';
 			
-			ctcp = split_command_parts( cmd[2] + 1 );
+			ctcp = split_command_parts( cmd[2] + 1, 0 );
 			iu->f->ctcp( iu, ctcp );
 		}
Index: lib/misc.c
===================================================================
--- lib/misc.c	(revision 1783ab6964c9a8ffc3488bb5243f0b15858f4e74)
+++ lib/misc.c	(revision 269580c6302a677e07176494bd314c7e2a8f488f)
@@ -682,5 +682,5 @@
    white\ space in 'various ways'. Returns a NULL-terminated static
    char** so watch out with nested use! Definitely not thread-safe. */
-char **split_command_parts( char *command )
+char **split_command_parts( char *command, int limit )
 {
 	static char *cmd[IRC_MAX_ARGS+1];
@@ -692,9 +692,10 @@
 	k = 1;
 	for( s = command; *s && k < IRC_MAX_ARGS; s ++ )
+	{
 		if( *s == ' ' && !q )
 		{
 			*s = 0;
 			while( *++s == ' ' );
-			if( *s == '"' || *s == '\'' )
+			if( k != limit && (*s == '"' || *s == '\'') )
 			{
 				q = *s;
@@ -704,4 +705,7 @@
 			{
 				cmd[k++] = s;
+				if (limit && k > limit) {
+					break;
+				}
 				s --;
 			}
@@ -722,4 +726,5 @@
 			q = *s = 0;
 		}
+	}
 	
 	/* Full zero-padding for easier argc checking. */
Index: lib/misc.h
===================================================================
--- lib/misc.h	(revision 1783ab6964c9a8ffc3488bb5243f0b15858f4e74)
+++ lib/misc.h	(revision 269580c6302a677e07176494bd314c7e2a8f488f)
@@ -67,5 +67,5 @@
 G_MODULE_EXPORT gboolean ssl_sockerr_again( void *ssl );
 G_MODULE_EXPORT int md5_verify_password( char *password, char *hash );
-G_MODULE_EXPORT char **split_command_parts( char *command );
+G_MODULE_EXPORT char **split_command_parts( char *command, int limit );
 G_MODULE_EXPORT char *get_rfc822_header( const char *text, const char *header, int len );
 
Index: protocols/twitter/twitter.c
===================================================================
--- protocols/twitter/twitter.c	(revision 1783ab6964c9a8ffc3488bb5243f0b15858f4e74)
+++ protocols/twitter/twitter.c	(revision 269580c6302a677e07176494bd314c7e2a8f488f)
@@ -602,5 +602,5 @@
 
 	cmds = g_strdup(message);
-	cmd = split_command_parts(cmds);
+	cmd = split_command_parts(cmds, 2);
 
 	if (cmd[0] == NULL) {
@@ -662,5 +662,5 @@
 			goto eof;
 		}
-		message = new = g_strdup_printf("@%s %s", bu->handle, message + (cmd[2] - cmd[0]));
+		message = new = g_strdup_printf("@%s %s", bu->handle, cmd[2]);
 		in_reply_to = id;
 		allow_post = TRUE;
Index: root_commands.c
===================================================================
--- root_commands.c	(revision 1783ab6964c9a8ffc3488bb5243f0b15858f4e74)
+++ root_commands.c	(revision 269580c6302a677e07176494bd314c7e2a8f488f)
@@ -32,5 +32,5 @@
 void root_command_string( irc_t *irc, char *command )
 {
-	root_command( irc, split_command_parts( command ) );
+	root_command( irc, split_command_parts( command, 0 ) );
 }
 
Index: tests/check_util.c
===================================================================
--- tests/check_util.c	(revision 1783ab6964c9a8ffc3488bb5243f0b15858f4e74)
+++ tests/check_util.c	(revision 269580c6302a677e07176494bd314c7e2a8f488f)
@@ -167,4 +167,48 @@
 	http_encode( s );
 	fail_unless( strcmp( s, "ee%C3%ABee%21%21..." ) == 0 );
+END_TEST
+
+struct {
+	int limit;
+	char *command;
+	char *expected[IRC_MAX_ARGS+1];
+} split_tests[] = {
+	{
+		0, "account add etc \"user name with spaces\" 'pass\\ word'",
+		{"account", "add", "etc", "user name with spaces", "pass\\ word", NULL},
+	},
+	{
+		0, "channel set group Close\\ friends",
+		{"channel", "set", "group", "Close friends", NULL},
+	},
+	{
+		2, "reply wilmer \"testing in C is a PITA\", you said.",
+		{"reply", "wilmer", "\"testing in C is a PITA\", you said.", NULL},
+	},
+	{
+		4, "one space  two  spaces  limit  limit",
+		{"one", "space", "two", "spaces", "limit  limit", NULL},
+	},
+	{
+		0, NULL,
+		{NULL}
+	},
+};
+
+START_TEST(test_split_command_parts)
+	int i;
+	for (i = 0; split_tests[i].command; i++) {
+		char *cmd = g_strdup(split_tests[i].command);
+		char **split = split_command_parts(cmd, split_tests[i].limit);
+		char **expected = split_tests[i].expected;
+
+		int j;
+		for (j = 0; split[j] && expected[j]; j++) {
+			fail_unless (strcmp(split[j], expected[j]) == 0,
+				"(%d) split_command_parts broken: split(\"%s\")[%d] -> %s (expected: %s)",
+				i, split_tests[i].command, j, split[j], expected[j]);
+		}
+		g_free(cmd);
+	}
 END_TEST
 
@@ -183,4 +227,5 @@
 	tcase_add_test (tc_core, test_word_wrap);
 	tcase_add_test (tc_core, test_http_encode);
+	tcase_add_test (tc_core, test_split_command_parts);
 	return s;
 }
