changeset 22735:5644a112b44f

doveadm-auth-server: Further fix json problems Remove excess comma from start, forgotten in 3e06a782584ea2c4ae2a8cb0451e9952f05f9dc8
author Aki Tuomi <aki.tuomi@dovecot.fi>
date Wed, 15 Nov 2017 14:29:59 +0200
parents d5d2a69361d6
children 639251a1d58e
files src/doveadm/doveadm-auth-server.c
diffstat 1 files changed, 15 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/src/doveadm/doveadm-auth-server.c	Wed Nov 15 10:28:37 2017 +0200
+++ b/src/doveadm/doveadm-auth-server.c	Wed Nov 15 14:29:59 2017 +0200
@@ -224,11 +224,16 @@
 }
 
 static void cmd_user_mail_input_field(const char *key, const char *value,
-				      const char *show_field)
+				      const char *show_field, bool *first)
 {
 	string_t *jvalue = t_str_new(128);
 	if (show_field != NULL && strcmp(show_field, key) != 0) return;
-	o_stream_nsend_str(doveadm_print_ostream, ",");
+	/* do not emit comma on first field. we need to keep track
+	   of when the first field actually gets printed as it
+	   might change due to show_field */
+	if (!*first)
+		o_stream_nsend_str(doveadm_print_ostream, ",");
+	*first = FALSE;
 	json_append_escaped(jvalue, key);
 	o_stream_nsend_str(doveadm_print_ostream, "\"");
 	o_stream_nsend_str(doveadm_print_ostream, str_c(jvalue));
@@ -248,17 +253,16 @@
 	const struct mail_storage_settings *mail_set;
 	const char *key, *value;
 	unsigned int i;
+	bool first = TRUE;
 
-	if (strcmp(input->username, user->username) != 0) {
-		cmd_user_mail_input_field("user", user->username, show_field);
-		o_stream_nsend_str(doveadm_print_ostream, ",");
-	}
-	cmd_user_mail_input_field("uid", user->set->mail_uid, show_field);
-	cmd_user_mail_input_field("gid", user->set->mail_gid, show_field);
-	cmd_user_mail_input_field("home", user->set->mail_home, show_field);
+	if (strcmp(input->username, user->username) != 0)
+		cmd_user_mail_input_field("user", user->username, show_field, &first);
+	cmd_user_mail_input_field("uid", user->set->mail_uid, show_field, &first);
+	cmd_user_mail_input_field("gid", user->set->mail_gid, show_field, &first);
+	cmd_user_mail_input_field("home", user->set->mail_home, show_field, &first);
 
 	mail_set = mail_user_set_get_storage_set(user);
-	cmd_user_mail_input_field("mail", mail_set->mail_location, show_field);
+	cmd_user_mail_input_field("mail", mail_set->mail_location, show_field, &first);
 
 	if (userdb_fields != NULL) {
 		for (i = 0; userdb_fields[i] != NULL; i++) {
@@ -274,7 +278,7 @@
 			    strcmp(key, "home") != 0 &&
 			    strcmp(key, "mail") != 0 &&
 			    *key != '\0') {
-				cmd_user_mail_input_field(key, value, show_field);
+				cmd_user_mail_input_field(key, value, show_field, &first);
 			}
 		}
 	}