# HG changeset patch
# Node ID ef264aacd3b1
# Parent  811f14a8e610
diff --git a/ibis/ibismessage.c b/ibis/ibismessage.c
--- a/ibis/ibismessage.c
+++ b/ibis/ibismessage.c
@@ -64,9 +64,27 @@
 			g_string_append(str, " ");
 		}
 
-		/* If this is the last param we need to prefix it with a :. */
+		/* We only need the prefix if the parameter is empty, starts with a
+		 * : or contains a space.
+		 */
 		if(message->params[i + 1] == NULL) {
-			g_string_append_printf(str, ":%s", message->params[i]);
+			const char *param = message->params[i];
+			gboolean prefix = FALSE;
+
+			/* We only need the prefix if the parameter is empty, starts with a
+			 * : or contains a space.
+			 */
+			if(param[0] == '\0' || param[0] == ':') {
+				prefix = TRUE;
+			} else if(g_strstr_len(param, -1, " ") != NULL) {
+				prefix = TRUE;
+			}
+
+			if(prefix) {
+				g_string_append_c(str, ':');
+			}
+
+			g_string_append_printf(str, "%s", param);
 		} else {
 			g_string_append(str, message->params[i]);
 		}
diff --git a/ibis/tests/test_message.c b/ibis/tests/test_message.c
--- a/ibis/tests/test_message.c
+++ b/ibis/tests/test_message.c
@@ -157,7 +157,7 @@
 }
 
 static void
-test_ibis_message_serialize_parameters(void) {
+test_ibis_message_serialize_parameters_normal(void) {
 	IbisMessage *message = NULL;
 	char *serialized = NULL;
 
@@ -166,7 +166,55 @@
 
 	serialized = ibis_message_serialize_to_string(message);
 
-	g_assert_cmpstr(serialized, ==, "foo bar :baz\r\n");
+	g_assert_cmpstr(serialized, ==, "foo bar baz\r\n");
+
+	g_clear_pointer(&serialized, g_free);
+	g_assert_finalize_object(message);
+}
+
+static void
+test_ibis_message_serialize_parameters_last_empty(void) {
+	IbisMessage *message = NULL;
+	char *serialized = NULL;
+
+	message = g_object_new(IBIS_TYPE_MESSAGE, NULL);
+	ibis_message_set_params(message, "", NULL);
+
+	serialized = ibis_message_serialize_to_string(message);
+
+	g_assert_cmpstr(serialized, ==, ":\r\n");
+
+	g_clear_pointer(&serialized, g_free);
+	g_assert_finalize_object(message);
+}
+
+static void
+test_ibis_message_serialize_parameters_last_starts_with_colon(void) {
+	IbisMessage *message = NULL;
+	char *serialized = NULL;
+
+	message = g_object_new(IBIS_TYPE_MESSAGE, NULL);
+	ibis_message_set_params(message, ":foo", NULL);
+
+	serialized = ibis_message_serialize_to_string(message);
+
+	g_assert_cmpstr(serialized, ==, "::foo\r\n");
+
+	g_clear_pointer(&serialized, g_free);
+	g_assert_finalize_object(message);
+}
+
+static void
+test_ibis_message_serialize_parameters_last_contains_space(void) {
+	IbisMessage *message = NULL;
+	char *serialized = NULL;
+
+	message = g_object_new(IBIS_TYPE_MESSAGE, NULL);
+	ibis_message_set_params(message, "I'm in space!!!!", NULL);
+
+	serialized = ibis_message_serialize_to_string(message);
+
+	g_assert_cmpstr(serialized, ==, ":I'm in space!!!!\r\n");
 
 	g_clear_pointer(&serialized, g_free);
 	g_assert_finalize_object(message);
@@ -185,7 +233,7 @@
 
 	serialized = ibis_message_serialize_to_string(message);
 
-	g_assert_cmpstr(serialized, ==, "foo bar baz :asdf\r\n");
+	g_assert_cmpstr(serialized, ==, "foo bar baz asdf\r\n");
 
 	g_clear_pointer(&serialized, g_free);
 	g_assert_finalize_object(message);
@@ -233,7 +281,7 @@
 
 	serialized = ibis_message_serialize_to_string(message);
 
-	g_assert_cmpstr(serialized, ==, ":coolguy foo bar baz :asdf\r\n");
+	g_assert_cmpstr(serialized, ==, ":coolguy foo bar baz asdf\r\n");
 
 	g_clear_pointer(&serialized, g_free);
 	g_assert_finalize_object(message);
@@ -385,7 +433,7 @@
 
 	serialized = ibis_message_serialize_to_string(message);
 
-	g_assert_cmpstr(serialized, ==, ":coolguy foo b\tar :baz\r\n");
+	g_assert_cmpstr(serialized, ==, ":coolguy foo b\tar baz\r\n");
 
 	g_clear_pointer(&serialized, g_free);
 	g_assert_finalize_object(message);
@@ -476,7 +524,7 @@
 	serialized = ibis_message_serialize_to_string(message);
 
 	g_assert_cmpstr(serialized, ==,
-	                "@a=b\\\\and\\nk;d=gh\\:764 foo par1 :par2\r\n");
+	                "@a=b\\\\and\\nk;d=gh\\:764 foo par1 par2\r\n");
 
 	g_clear_pointer(&serialized, g_free);
 	g_assert_finalize_object(message);
@@ -525,8 +573,14 @@
 	                test_ibis_message_serialize_source);
 	g_test_add_func("/ibis/message/serialize/command",
 	                test_ibis_message_serialize_command);
-	g_test_add_func("/ibis/message/serialize/parameters",
-	                test_ibis_message_serialize_parameters);
+	g_test_add_func("/ibis/message/serialize/parameters/normal",
+	                test_ibis_message_serialize_parameters_normal);
+	g_test_add_func("/ibis/message/serialize/parameters/last_empty",
+	                test_ibis_message_serialize_parameters_last_empty);
+	g_test_add_func("/ibis/message/serialize/parameters/last_starts_with_colon",
+	                test_ibis_message_serialize_parameters_last_starts_with_colon);
+	g_test_add_func("/ibis/message/serialize/parameters/last_contains_space",
+	                test_ibis_message_serialize_parameters_last_contains_space);
 
 	/* These tests are based on the msg-join tests from
 	 * https://github.com/ircdocs/parser-tests/blob/master/tests/msg-join.yaml
